Created
May 21, 2019 07:22
-
-
Save rootext/39c7b84a8fc04343ee80a6aa7b9d17a8 to your computer and use it in GitHub Desktop.
Dart + SDL2 crash example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:ffi'; | |
import 'dart:convert'; | |
void main() { | |
var sdl = DynamicLibrary.open('SDL2.dll'); | |
loadSDL(sdl); | |
sdlInit(SDL_INIT_VIDEO); | |
var title = CString.fromUtf8('Dart FFI + SDL'); | |
var window = sdlCreateWindow(title, 100, 100, 600, 400, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL); | |
var renderer = sdlCreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); | |
sdlSetRenderDrawColor(renderer, 0, 179, 252, 0); | |
var event = SDL_Event(0); | |
var exit = false; | |
while (!exit) { | |
while (sdlPollEvent(event) != 0) { | |
} | |
if (event.type == SDL_QUIT) { exit = true; } | |
sdlRenderClear(renderer); | |
sdlRenderPresent(renderer); | |
} | |
} | |
const int SDL_INIT_VIDEO = 0x00000020; | |
const int SDL_WINDOW_RESIZABLE = 0x00000020; | |
const int SDL_WINDOW_OPENGL = 0x00000002; | |
const int SDL_QUIT = 0x100; | |
const int SDL_RENDERER_ACCELERATED = 0x00000002; | |
typedef SDL_Init_Native = Int32 Function(Uint32); | |
typedef SDL_Init = int Function(int); | |
typedef SDL_CreateWindow_Native = Int64 Function(CString, Int32, Int32, Int32, Int32, Uint32); | |
typedef SDL_CreateWindow = int Function(CString, int, int, int, int, int); | |
typedef SDL_PollEvent_Native = Int32 Function(SDL_Event); | |
typedef SDL_PollEvent = int Function(SDL_Event); | |
typedef SDL_CreateRenderer_Native = IntPtr Function(IntPtr window, Int32 index, Int32 flags); | |
typedef SDL_CreateRenderer = int Function(int window, int index, int flags); | |
typedef SDL_SetRenderDrawColor_Native = Int32 Function(IntPtr renderer, Int8 r, Int8 g, Int8 b, Int8 a); | |
typedef SDL_SetRenderDrawColor = int Function(int renderer, int r, int g, int b, int a); | |
typedef SDL_RenderClear_Native = Int32 Function(IntPtr renderer); | |
typedef SDL_RenderClear = int Function(int renderer); | |
typedef SDL_RenderPresent_Native = Void Function(IntPtr renderer); | |
typedef SDL_RenderPresent = void Function(int renderer); | |
@struct | |
class SDL_Event extends Pointer<Void> { | |
@Uint32() | |
int type; | |
@Uint32() | |
int timestamp; | |
@Uint32() | |
int windowID; | |
@Uint8() | |
int event; | |
@Uint8() | |
int p1; | |
@Uint8() | |
int p2; | |
@Uint8() | |
int p3; | |
@Int32() | |
int data1; | |
@Int32() | |
int data2; | |
@Uint32() | |
int p4; | |
@Uint32() | |
int p5; | |
@Uint32() | |
int p6; | |
@Uint32() | |
int p7; | |
@Uint32() | |
int p8; | |
@Uint32() | |
int p9; | |
@Uint32() | |
int p10; | |
@Uint32() | |
int p11; | |
external static int sizeOf(); | |
factory SDL_Event(int type) { | |
SDL_Event result = allocate<Uint8>(count: 1 * sizeOf()).cast() | |
..type = type; | |
return result; | |
} | |
} | |
SDL_Init sdlInit; | |
SDL_CreateWindow sdlCreateWindow; | |
SDL_CreateRenderer sdlCreateRenderer; | |
SDL_SetRenderDrawColor sdlSetRenderDrawColor; | |
SDL_RenderClear sdlRenderClear; | |
SDL_RenderPresent sdlRenderPresent; | |
SDL_PollEvent sdlPollEvent; | |
void loadSDL(DynamicLibrary sdl) { | |
sdlInit = sdl.lookupFunction<SDL_Init_Native, SDL_Init>('SDL_Init'); | |
sdlCreateWindow = sdl.lookupFunction<SDL_CreateWindow_Native, SDL_CreateWindow>('SDL_CreateWindow'); | |
sdlCreateRenderer = sdl.lookupFunction<SDL_CreateRenderer_Native, SDL_CreateRenderer>('SDL_CreateRenderer'); | |
sdlSetRenderDrawColor = sdl.lookupFunction<SDL_SetRenderDrawColor_Native, SDL_SetRenderDrawColor>('SDL_SetRenderDrawColor'); | |
sdlRenderClear = sdl.lookupFunction<SDL_RenderClear_Native, SDL_RenderClear>('SDL_RenderClear'); | |
sdlRenderPresent = sdl.lookupFunction<SDL_RenderPresent_Native, SDL_RenderPresent>('SDL_RenderPresent'); | |
sdlPollEvent = sdl.lookupFunction<SDL_PollEvent_Native, SDL_PollEvent>('SDL_PollEvent'); | |
} | |
class CString extends Pointer<Uint8> { | |
CString elementAt(int index) => super.elementAt(index).cast(); | |
factory CString.fromUtf8(String s) { | |
CString result = allocate<Uint8>(count: s.length + 1).cast(); | |
List<int> units = Utf8Encoder().convert(s); | |
for (int i = 0; i < s.length; i++) result.elementAt(i).store(units[i]); | |
result.elementAt(s.length).store(0); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment