Simple2D Wrapper Help
- Posted by Icy_Viking Apr 18, 2023
- 586 views
Hello all,
Okay once again I'm having a issue with callbacks. After messing around with the Simple2D source code for a couple of hours, I was able to build and export the functions into a DLL. I made a quick test and it works. The example shows a triangle being displayed, however the triangle function is a callback within the create_window() function. I've listed some snippets of code to help. Let me know if more code is needed. If needed I can also put what I have so far into a .zip file.
Note: I added declspec(dllexport) to get the functions exported into a DLL while building the simple2D library.
Note 2: Simple2D relies heavily on the SDL2 library and its add-ons, good thing I already made a SDL2 wrapper and wrappers for those add-ons, lol. Oh also, Simple2D was built using 64-bit compiler, so I have the 64-bit SDL2 DLL and of course the 64-bit version of Euphoria. It won't work on 32-bit version of Euphoria.
https://github.com/simple2d/simple2d/blob/main/include/simple2d.h
//From simple2D.h //Callback functions typedef void (*S2D_Update)(); typedef void (*S2D_Render)(); typedef void (*S2D_On_Key)(S2D_Event e); typedef void (*S2D_On_Mouse)(S2D_Event e); typedef void (*S2D_On_Controller)(S2D_Event e); __declspec(dllexport) S2D_Window *S2D_CreateWindow( const char *title, int width, int height, S2D_Update, S2D_Render, int flags ); //I slightly modified the include file by making it so the functions would be exported to a DLL //S2D_Update and S2D_Render are callbacks.
Wrapper code
include std/ffi.e include SDL.e atom simple = 0 ifdef WINDOWS then simple = open_dll("simple2d.dll") elsifdef LINUX or FREEBSD then simple = open_dll("simple2d.so") end ifdef export constant xS2D_Init = define_c_func(simple,"+S2D_Init",{},C_BOOL) public function S2D_Init() return c_func(xS2D_Init,{}) end function export constant xS2D_CreateWindow = define_c_func(simple,"+S2D_CreateWindow",{C_STRING,C_INT,C_INT,C_POINTER,C_POINTER,C_INT},C_POINTER) public function S2D_CreateWindow(sequence title,atom width,atom height,object update,object render,atom flags) return c_func(xS2D_CreateWindow,{title,width,height,update,render,flags}) --callback should be implemented where update and render but I'm at a lost at how to wrap them end function export constant xS2D_Show = define_c_func(simple,"+S2D_Show",{C_POINTER},C_INT) public function S2D_Show(atom win) return c_func(xS2D_Show,{win}) end function export constant xS2D_DrawTriangle = define_c_proc(simple,"+S2D_DrawTriangle",{C_FLOAT,C_FLOAT,C_FLOAT,C_FLOAT,C_FLOAT,C_FLOAT,C_FLOAT,C_FLOAT,C_FLOAT,C_FLOAT,C_FLOAT,C_FLOAT,C_FLOAT,C_FLOAT,C_FLOAT,C_FLOAT,C_FLOAT,C_FLOAT}) public procedure S2D_DrawTriangle(atom x1,atom y1,atom r1,atom g1,atom b1,atom a1,atom x2,atom y2,atom r2,atom g2,atom b2,atom a2,atom x3,atom y3,atom r3,atom g3,atom b3,atom a3) c_proc(xS2D_DrawTriangle,{x1,y1,r1,g1,b1,a1,x2,y2,r2,g2,b2,a2,x3,y3,r3,g3,b3,a3}) end procedure export constant xS2D_Quit = define_c_proc(simple,"+S2D_Quit",{}) public procedure S2D_Quit() c_proc(xS2D_Quit,{}) end procedure
Simple test
include std/ffi.e --for NULL include simple2d.e atom x = S2D_Init() if x = -1 then puts(1,"Failed to init Simple2D!\n") abort(0) end if procedure triangle() S2D_DrawTriangle(320,50,1,0,0,1, 540,430,0,1,0,1, 100,430,0,0,1,1) end procedure atom win = S2D_CreateWindow("Triangle Test",640,480,NULL,NULL,0) --triangle() should be where the second "NULL" is. This simple test does work except for the triangle showing, a basic window shows up S2D_Show(win) S2D_Quit()