diff --git a/lua_translate b/lua_translate index c9353cc..dcf177a 100755 --- a/lua_translate +++ b/lua_translate @@ -16,13 +16,9 @@ typesOut = { "char *": "lua_pushstring" } -functionBlacklist = [ - "engine_malloc" -] - -statics = toml.loads(open("src/engine/values/statics.toml").read()) -functions = toml.loads(open("src/engine/values/functions.toml").read()) -ofile = open("src/engine/lua.c","w") +statics = toml.loads(open("src/values/statics.toml").read()) +functions = toml.loads(open("src/values/functions.toml").read()) +ofile = open("src/lua.c","w") ofile.write('''\ #include @@ -34,7 +30,8 @@ lua_State * engine_lua_state; ''') for func in functions: - if func in functionBlacklist: continue + if "lua" in functions[func]: + if functions[func]["lua"] in ["no","manual"]: continue invarCount = 1 funcnew = "engine_luaf_" +func.replace("engine_","",1) ofile.write('int ' +funcnew+ '(lua_State *L) {\n') @@ -76,7 +73,8 @@ for static in statics: ofile.write('\tlua_setglobal(engine_lua_state,"' +static+ '");\n') for func in functions: - if func in functionBlacklist: continue + if "lua" in functions[func]: + if functions[func]["lua"] in ["no","manual"]: continue funcnew = "engine_luaf_" +func.replace("engine_","",1) ofile.write('\tlua_pushcfunction(engine_lua_state,' +funcnew+ ');\n') ofile.write('\tlua_setglobal (engine_lua_state,"' +func+ '");\n') diff --git a/mods/main/script/main.lua b/mods/main/script/main.lua index af815c7..2846993 100644 --- a/mods/main/script/main.lua +++ b/mods/main/script/main.lua @@ -1,8 +1,9 @@ function main() math.randomseed(os.time()) - engine_window_init(96,64,"Game") + window = engine_window_create(96,64,"Game") texture = engine_texture_create(8,8) - engine_texture_from_file(texture,bp("texture/fier.rgba")) + engine_rendertarget_set(texture) + engine_rendertarget_draw_file(bp("texture/fier.rgba")) while true do event = engine_event_get() @@ -19,8 +20,8 @@ end lastFrame = 0 function tick() - engine_texture_render_2d(texture,math.random(-8,102),math.random(-8,72)) - engine_window_present() + engine_rendertarget_draw_texture(texture,math.random(-8,102),math.random(-8,72)) + engine_window_present(window) trackFps() local curFrame = engine_time_get() local wait = 16 - (curFrame - lastFrame) diff --git a/src/engine/main.c b/src/engine.c similarity index 55% rename from src/engine/main.c rename to src/engine.c index 2d3703a..3613279 100644 --- a/src/engine/main.c +++ b/src/engine.c @@ -1,3 +1,27 @@ +// MEMORY +void * engine_memory_alloc(void * pnt,size_t size) { + void * mem = realloc(pnt,size); + if (mem == NULL) { + printf("ERROR: Could not allocate memory.\n"); + exit(1); + } + return mem; +} + +void engine_memory_free(void * pnt) { + free(pnt); +} + +// TEXTURES +struct ENGINE_TEXTURE { int width; int height; struct ENGINE_FRONTEND_TEXTURE * fe_texture; }; + +// COLOR +struct ENGINE_COLOR { char r; char g; char b; char a; }; + +// WINDOWS +struct ENGINE_WINDOW { struct ENGINE_TEXTURE * texture; struct ENGINE_FRONTEND_WINDOW * fe_window; }; + +// EVENTS struct ENGINE_EVENT { char type; void * data; }; static char ENGINE_EVENT_TYPE_UNKNOWN = 0; struct ENGINE_EVENT_UNKNOWN { }; @@ -8,20 +32,7 @@ struct ENGINE_EVENT_EXIT { }; static char ENGINE_EVENT_TYPE_INPUT_KB = 3; struct ENGINE_EVENT_INPUT_KB { char key; char pressed; }; -void * engine_malloc(void * pnt,size_t size) { - void * mem = realloc(pnt,size); - if (mem == NULL) { - printf("ERROR: Could not allocate memory.\n"); - exit(1); - } - return mem; -} - -void engine_free(void *pnt) { - free(pnt); -} - void engine_event_free(struct ENGINE_EVENT * event) { - engine_free(event->data); - engine_free(event); -} + engine_memory_free(event->data); + engine_memory_free(event); +} \ No newline at end of file diff --git a/src/engine/frontend/generic/textures.c b/src/engine/frontend/generic/textures.c deleted file mode 100644 index d1dc1fd..0000000 --- a/src/engine/frontend/generic/textures.c +++ /dev/null @@ -1,93 +0,0 @@ -// Mostly for fun (but can very slightly increase performance for large textures). If set to 1, does not clear texture's affiliated and leaves garbage. -#define ENGINE_GENERIC_TEXTURE_NOCLEAR 0 - -struct ENGINE_GENERIC_TEXTURE { char * pixels; }; - -struct ENGINE_TEXTURE * engine_texture_create(int width,int height) { - // frontend texture - char * pixels = engine_malloc(NULL,sizeof(char) * width * height * 4); - #if (ENGINE_GENERIC_TEXTURE_NOCLEAR == 0) - memset(pixels,0,sizeof(char) * width * height * 4); - #endif - struct ENGINE_GENERIC_TEXTURE * fe_texture = engine_malloc(NULL,sizeof(struct ENGINE_GENERIC_TEXTURE)); - fe_texture->pixels = pixels; - - // engine texture - struct ENGINE_TEXTURE * texture = engine_malloc(NULL,sizeof(struct ENGINE_TEXTURE)); - texture->width = width; - texture->height = height; - texture->fe_texture = (void *)fe_texture; - return texture; -} - -struct ENGINE_GENERIC_TEXTURE_COLOR { char r; char g; char b; char a; }; -struct ENGINE_GENERIC_TEXTURE_COLOR engine_generic_texture_color; - -void engine_texture_color_set(char r,char g,char b,char a) { - engine_generic_texture_color.r = r; - engine_generic_texture_color.g = g; - engine_generic_texture_color.b = b; - engine_generic_texture_color.a = a; -} - -void engine_texture_draw_pixel(struct ENGINE_TEXTURE * texture,int x,int y) { - char * pixels = ((struct ENGINE_GENERIC_TEXTURE *)(texture->fe_texture))->pixels; - long pixelIndex = (x + y * texture->width) * 4; - pixels[pixelIndex + 0] = engine_generic_texture_color.r; - pixels[pixelIndex + 1] = engine_generic_texture_color.g; - pixels[pixelIndex + 2] = engine_generic_texture_color.b; - pixels[pixelIndex + 3] = engine_generic_texture_color.a; -} - -void engine_texture_destroy(struct ENGINE_TEXTURE * texture) { - struct ENGINE_GENERIC_TEXTURE * fe_texture = (struct ENGINE_GENERIC_TEXTURE *)(texture->fe_texture); - engine_free(fe_texture->pixels); - engine_free(fe_texture); - engine_free(texture); -} - -void engine_texture_render_2d(struct ENGINE_TEXTURE * texture,int x,int y) { - int sx = 0; - if (x < 0) { - sx -= x; - } - if (sx > texture->width) { return; } // unnecessary? maybe faster with a lot of offscreen textures? - - int sy = 0; - if (y < 0) { - sy -= y; - } - if (sy > texture->height) { return; } // unnecessary? maybe faster with a lot of offscreen textures? - - int ex = texture->width; - if (x + ex > engine_width) { - ex -= (x + ex) - engine_width; - if (ex < 0) { return; } // unnecessary? maybe faster with a lot of offscreen textures? - } - - int ey = texture->height; - if (y + ey > engine_height) { - ey -= (y + ey) - engine_height; - if (ey < 0) { return; } // unnecessary? maybe faster with a lot of offscreen textures? - } - - char * pixels = ((struct ENGINE_GENERIC_TEXTURE *)(texture->fe_texture))->pixels; - int pixelIndex; - int tx = sx; - while (sy < ey) { - while (tx < ex) { - pixelIndex = (tx + sy * texture->width)*4; - engine_surface_color_set(pixels[pixelIndex],pixels[pixelIndex + 1],pixels[pixelIndex + 2],pixels[pixelIndex + 3]); - engine_surface_draw_pixel(x + tx,y + sy); - ++tx; - } - tx = sx; - ++sy; - } -} - -void engine_texture_from_file(struct ENGINE_TEXTURE * texture,char * fpath) { - FILE * f = fopen(fpath,"r"); - fgets(((struct ENGINE_GENERIC_TEXTURE *)(texture->fe_texture))->pixels,texture->width * texture->height * 4,f); - fclose(f); -} diff --git a/src/engine/frontend/sdl/main.c b/src/engine/frontend/sdl/main.c deleted file mode 100644 index 9f93548..0000000 --- a/src/engine/frontend/sdl/main.c +++ /dev/null @@ -1,296 +0,0 @@ -// With much thanks to https://benedicthenshaw.com/soft_render_sdl2.html - -// --- SETTINGS --- -// BUFFER -// How to buffer pixels. -// 0: Use SDL_Surface (fastest in software) -// 1: Use SDL_Renderer (slow?) -// TODO: Add support for GPU texture -#define ENGINE_SDL_BUFFERTYPE 0 - -// TRANSPARENCY -// How to draw transparent pixels (SDL_Surface buffer only). -// 0: Do not handle transparency (Fastest but non-square textures will have a halo around them) -// 1: 1-bit transparency (Alpha values under 128 are not drawn) -// 2: 8-bit transparency (Nice but slow) -// TODO: Add support for SDL_Renderer -#define ENGINE_SDL_TRANSPARENCY 1 - -// --- HACKS --- -// BUFFER -// Use static color conversion. This will speed up rendering, however, the wrong colors may be displayed depending on platform. This only has an effect if ENGINE_SDL_BUFFERTYPE is set to 0. -// 0 = Disable -// 1 = RGBA (raw) -// 2 = BGRA (common) -// TODO: Add custom mode with quicker conversion of known types, and slow conversion for unknown types. -#define ENGINE_SDL_COLOR 2 - -// INTEGER UPSCALING -// 2 makes the surface scaled 2x, 3 = 3x, etc. -// TODO: Fix ENGINE_SDL_TRANSPARENCY 2 -#define ENGINE_SDL_SCALE 2 - -// SKIP: COLOR -// Skip setting colors every X requests. Set to 2 to skip every second request, 3 for every third, etc. -#define ENGINE_SDL_SKIP_COLOR_INTERVAL 0 // 2 -// How many requests to skip, when a skip is encountered. -#define ENGINE_SDL_SKIP_COLOR_AMOUNT 2 -// Whether to alternate fields. TODO -#define ENGINE_SDL_SKIP_COLOR_ALTERNATE 0 - -// SKIP: PIXEL -// Skip setting pixel every X requests. -#define ENGINE_SDL_SKIP_PIXEL_INTERVAL 0 // 2 -#define ENGINE_SDL_SKIP_PIXEL_AMOUNT 2 -#define ENGINE_SDL_SKIP_PIXEL_ALTERNATE 0 - -// SKIP: FRAME -#define ENGINE_SDL_SKIP_FRAME_INTERVAL 1 -#define ENGINE_SDL_SKIP_FRAME_AMOUNT 1 -// Entirely skip changing any colors and pixels on skipped frames, without this, this is pretty useless -#define ENGINE_SDL_SKIP_FRAME_DROP 0 - -// DO NOT TOUCH THESE -#if (ENGINE_SDL_SKIP_COLOR_INTERVAL > 0 && ENGINE_SDL_SKIP_COLOR_AMOUNT > 0) - #define ENGINE_SDL_SKIP_COLOR 1 -#endif - -#if (ENGINE_SDL_SKIP_PIXEL_INTERVAL > 0 && ENGINE_SDL_SKIP_PIXEL_AMOUNT > 0) - #define ENGINE_SDL_SKIP_PIXEL 1 -#endif - -#if (ENGINE_SDL_SKIP_FRAME_INTERVAL > 0 && ENGINE_SDL_SKIP_FRAME_AMOUNT > 0) - #define ENGINE_SDL_SKIP_FRAME 1 -#endif - -#include -SDL_Window * engine_sdl_window; - -#if (ENGINE_SDL_BUFFERTYPE == 0) - SDL_Surface * engine_sdl_surface; - Uint32 * engine_sdl_pixels; -#endif - -#if (ENGINE_SDL_BUFFERTYPE == 1) - SDL_Renderer * engine_sdl_renderer; -#endif - -int engine_width = 0; -int engine_height = 0; - -struct ENGINE_EVENT * engine_event_get() { - struct ENGINE_EVENT * event = engine_malloc(NULL,sizeof(struct ENGINE_EVENT)); - SDL_Event sdlevent; - - if (!SDL_PollEvent(&sdlevent)) { - struct ENGINE_EVENT_NONE * data = engine_malloc(NULL,sizeof(struct ENGINE_EVENT_NONE)); - event->type = ENGINE_EVENT_TYPE_NONE; - event->data = data; - return event; - } - - if (sdlevent.type == SDL_QUIT) { - struct ENGINE_EVENT_EXIT * data = engine_malloc(NULL,sizeof(struct ENGINE_EVENT_EXIT));; - event->type = ENGINE_EVENT_TYPE_EXIT; - event->data = data; - return event; - } - - if (sdlevent.type == SDL_KEYDOWN || sdlevent.type == SDL_KEYUP) { - if (sdlevent.key.repeat != 0) { goto unknown; } - struct ENGINE_EVENT_INPUT_KB * data = engine_malloc(NULL,sizeof(struct ENGINE_EVENT_INPUT_KB)); - data->pressed = (sdlevent.type == SDL_KEYDOWN); - data->key = sdlevent.key.keysym.scancode; - event->type = ENGINE_EVENT_TYPE_INPUT_KB; - event->data = data; - return event; - } - - unknown: - struct ENGINE_EVENT_UNKNOWN * data = engine_malloc(NULL,sizeof(struct ENGINE_EVENT_INPUT_KB)); - event->type = ENGINE_EVENT_TYPE_UNKNOWN; - event->data = data; - return event; -} - -void engine_time_sleep(long long ms) { - SDL_Delay(ms); -} - -long long engine_time_get() { - return SDL_GetTicks(); -} - -#ifdef ENGINE_SDL_SKIP_FRAME - char engine_sdl_skipf_interval = 0; - char engine_sdl_skipf_amount = 0; -#endif - -#ifdef ENGINE_SDL_SKIP_COLOR - char engine_sdl_skipc_interval = 0; - char engine_sdl_skipc_amount = 0; -#endif - -Uint32 engine_sdl_color; -void engine_surface_color_set(char r,char g,char b,char a) { - #ifdef ENGINE_SDL_SKIP_COLOR - if (engine_sdl_skipc_interval == 0) { - if (engine_sdl_skipc_amount == 0) { - engine_sdl_skipc_interval = ENGINE_SDL_SKIP_COLOR_INTERVAL - 1; - engine_sdl_skipc_amount = ENGINE_SDL_SKIP_COLOR_AMOUNT; - } else { - --engine_sdl_skipc_amount; - return; - } - } else { - --engine_sdl_skipc_interval; - } - #endif - - #if (ENGINE_SDL_SKIP_FRAME == 1 && ENGINE_SDL_SKIP_FRAME_DROP == 1) - if (engine_sdl_skipf_interval == 0 && engine_sdl_skipf_amount > 0) { - return; - } - #endif - - #if (ENGINE_SDL_BUFFERTYPE == 0) - #if (ENGINE_SDL_COLOR != 0 || ENGINE_SDL_TRANSPARENCY > 0) - Uint8 * rawcolor = (Uint8 *)&engine_sdl_color; // is doing this ok? - #endif - - #if (ENGINE_SDL_COLOR == 1) - rawcolor[0] = (Uint8)r; - rawcolor[1] = (Uint8)g; - rawcolor[2] = (Uint8)b; - rawcolor[3] = (Uint8)a; - #elif (ENGINE_SDL_COLOR == 2) - rawcolor[0] = (Uint8)b; - rawcolor[1] = (Uint8)g; - rawcolor[2] = (Uint8)r; - rawcolor[3] = (Uint8)a; - #else - engine_sdl_color = SDL_MapRGBA(engine_sdl_surface->format,(Uint8)r,(Uint8)g,(Uint8)b,(Uint8)a); - #if (ENGINE_SDL_TRANSPARENCY > 0) - rawcolor[3] = (Uint8)a; - #endif - #endif - #endif - - #if (ENGINE_SDL_BUFFERTYPE == 1) - SDL_SetRenderDrawColor(engine_sdl_renderer,r,g,b,a); - #endif -} - -#ifdef ENGINE_SDL_SKIP_PIXEL - char engine_sdl_skipp_interval = 0; - char engine_sdl_skipp_amount = 0; -#endif - -void engine_surface_draw_pixel(int x,int y) { - #ifdef ENGINE_SDL_SKIP_PIXEL - if (engine_sdl_skipp_interval == 0) { - if (engine_sdl_skipp_amount == 0) { - engine_sdl_skipp_interval = ENGINE_SDL_SKIP_PIXEL_INTERVAL - 1; - engine_sdl_skipp_amount = ENGINE_SDL_SKIP_PIXEL_AMOUNT; - } else { - --engine_sdl_skipp_amount; - return; - } - } else { - --engine_sdl_skipp_interval; - } - #endif - - #if (ENGINE_SDL_SKIP_FRAME == 1 && ENGINE_SDL_SKIP_FRAME_DROP == 1) - if (engine_sdl_skipf_interval == 0 && engine_sdl_skipf_amount > 0) { - return; - } - #endif - - #if (ENGINE_SDL_BUFFERTYPE == 0) - #if (ENGINE_SDL_TRANSPARENCY == 1) - if (((Uint8 *)&engine_sdl_color)[3] < 128) { // is doing this ok? - return; - } - #endif - - #if (ENGINE_SDL_TRANSPARENCY == 2) - Uint8 * rawcolor = (Uint8 *)&engine_sdl_color; - if (rawcolor[3] < 255) { - Uint8 * sourcecolor = (Uint8 *)&engine_sdl_pixels[x + y * engine_width]; - float alpha = (float)rawcolor[3] / 255; - sourcecolor[0] = sourcecolor[0] + ((rawcolor[0] - sourcecolor[0])*alpha); - sourcecolor[1] = sourcecolor[1] + ((rawcolor[1] - sourcecolor[1])*alpha); - sourcecolor[2] = sourcecolor[2] + ((rawcolor[2] - sourcecolor[2])*alpha); - return; - } - #endif - - #if (ENGINE_SDL_SCALE == 1) - engine_sdl_pixels[x + y * engine_width] = engine_sdl_color; - #else - char xi = 0; - while (xi < ENGINE_SDL_SCALE) { - char yi = 0; - while (yi < ENGINE_SDL_SCALE) { - engine_sdl_pixels[((x * ENGINE_SDL_SCALE) + xi) + ((y * ENGINE_SDL_SCALE) + yi) * (engine_width * ENGINE_SDL_SCALE)] = engine_sdl_color; - ++yi; - } - ++xi; - } - #endif - #endif - - #if (ENGINE_SDL_BUFFERTYPE == 1) - SDL_RenderDrawPoint(engine_sdl_renderer,x,y); - #endif -} - -void engine_window_present() { - #ifdef ENGINE_SDL_SKIP_FRAME - if (engine_sdl_skipf_interval == 0) { - if (engine_sdl_skipf_amount == 0) { - engine_sdl_skipf_interval = ENGINE_SDL_SKIP_FRAME_INTERVAL - 1; - engine_sdl_skipf_amount = ENGINE_SDL_SKIP_FRAME_AMOUNT; - } else { - --engine_sdl_skipf_amount; - return; - } - } else { - --engine_sdl_skipf_interval; - } - #endif - - #if (ENGINE_SDL_BUFFERTYPE == 0) - SDL_UpdateWindowSurface(engine_sdl_window); - #endif - - #if (ENGINE_SDL_BUFFERTYPE == 1) - SDL_RenderPresent(engine_sdl_renderer); - #endif -} - -void engine_window_init(int width,int height,char *title) { - SDL_Init(SDL_INIT_VIDEO); - engine_sdl_window = SDL_CreateWindow(title,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width * ENGINE_SDL_SCALE,height * ENGINE_SDL_SCALE,0); - #if (ENGINE_SDL_BUFFERTYPE == 0) - engine_sdl_surface = SDL_GetWindowSurface(engine_sdl_window); - engine_sdl_pixels = engine_sdl_surface->pixels; - #endif - - #if (ENGINE_SDL_BUFFERTYPE == 1) - engine_sdl_renderer = SDL_CreateRenderer(engine_sdl_window,-1,0); - #endif - - engine_width = width; - engine_height = height; - - #if (ENGINE_SDL_BUFFERTYPE == 1) - engine_surface_color_set(0,0,0,255); - #ifdef ENGINE_SDL_SKIP_COLOR - engine_sdl_skipc_interval = 0; - engine_sdl_skipc_amount = 0; - #endif - SDL_RenderClear(engine_sdl_renderer); - #endif -} diff --git a/src/engine/textures.c b/src/engine/textures.c deleted file mode 100644 index 5a43c10..0000000 --- a/src/engine/textures.c +++ /dev/null @@ -1 +0,0 @@ -struct ENGINE_TEXTURE { int width; int height; void * fe_texture; }; diff --git a/src/engine/values/functions.toml b/src/engine/values/functions.toml deleted file mode 100644 index 3f499fd..0000000 --- a/src/engine/values/functions.toml +++ /dev/null @@ -1,101 +0,0 @@ -# MEMORY -[engine_malloc] -type = "void *" -arguments = ["void *","size_t"] -argNames = ["ptr", "size"] -description = "Allocate memory, equivalent to C's realloc(), with the difference that it quits on failure." - -[engine_free] -type = "void" -arguments = ["void *"] -argNames = ["ptr"] -description = "Allocate memory, equivalent to C's free()." - -# WINDOW -[engine_window_init] -type = "void" -arguments = ["int", "int", "char *"] -argNames = ["width","height","title"] -description = "Creates a window." - -[engine_window_present] -type = "void" -arguments = [] -argNames = [] -description = "Updates the window's surface." - -# SURFACE -[engine_surface_color_set] -type = "void" -arguments = ["char","char","char","char"] -argNames = ["r", "g", "b", "a"] -description = "Set the color that will be drawn to the window surface." - -[engine_surface_draw_pixel] -type = "void" -arguments = ["int","int"] -argNames = ["x", "y"] -description = "Draw a pixel to the window surface." - -# TIME -[engine_time_get] -type = "long long" -arguments = [] -argNames = [] -description = "Get the time passed since program start in milliseconds." - -[engine_time_sleep] -type = "void" -arguments = ["long long"] -argNames = ["ms"] -description = "Halt the program for some amount of time." - -# LOGIC -[engine_event_get] -type = "struct ENGINE_EVENT *" -arguments = [] -argNames = [] -description = "Get an event. If the type is ENGINE_EVENT_TYPE_NONE, there are no more events. Needs to be freed with engine_free()." - -[engine_event_free] -type = "void" -arguments = ["struct ENGINE_EVENT *"] -argNames = ["event"] -description = "Free the event instance from memory." - -# TEXTURES -[engine_texture_create] -type = "struct ENGINE_TEXTURE *" -arguments = ["int", "int"] -argNames = ["width","height"] -description = "Creates a texture. Needs to be freed when no longer needed with engine_texture_destroy()." - -[engine_texture_color_set] -type = "void" -arguments = ["char","char","char","char"] -argNames = ["r", "g", "b", "a"] -description = "Set the color that will be drawn to the texture." - -[engine_texture_draw_pixel] -type = "void" -arguments = ["struct ENGINE_TEXTURE *","int","int"] -argNames = ["texture", "x", "y"] -description = "Draw a pixel to the texture." - -[engine_texture_destroy] -type = "void" -arguments = ["struct ENGINE_TEXTURE *"] -argNames = ["texture"] -description = "Destroy a texture, freeing up memory." - -[engine_texture_render_2d] -type = "void" -arguments = ["struct ENGINE_TEXTURE *","int","int"] -argNames = ["texture", "x", "y"] -description = "Render texture to the window surface." - -[engine_texture_from_file] -type = "void" -arguments = ["struct ENGINE_TEXTURE *","char *"] -argNames = ["texture", "fpath"] -description = "Read a raw RGBA image file into a texture." diff --git a/src/frontend/null/main.c b/src/frontend/null/main.c new file mode 100644 index 0000000..7f1a51e --- /dev/null +++ b/src/frontend/null/main.c @@ -0,0 +1,76 @@ +// This is the null frontend. It is functional for most cases, but it won't display anything or accept inputs. It's meant to be the most simple implementation of the API possible, without external dependencies. As a developer, you can use this as a clean slate of sorts. + +// EVENTS +struct ENGINE_EVENT * engine_event_get() { + struct ENGINE_EVENT * event = engine_memory_alloc(NULL,sizeof(struct ENGINE_EVENT)); + struct ENGINE_EVENT_NONE * data = engine_memory_alloc(NULL,sizeof(struct ENGINE_EVENT_NONE)); + event->type = ENGINE_EVENT_TYPE_NONE; + event->data = data; + return event; +} + +// TEXTURE +struct ENGINE_TEXTURE * engine_texture_create(int width,int height) { + struct ENGINE_TEXTURE * texture = engine_memory_alloc(NULL,sizeof(struct ENGINE_TEXTURE)); + struct ENGINE_FRONTEND_TEXTURE * fe_texture = engine_memory_alloc(NULL,sizeof(struct ENGINE_FRONTEND_TEXTURE)); + texture->width = width; + texture->height = height; + texture->fe_texture = fe_texture; + return texture; +} + +void engine_texture_destroy(struct ENGINE_TEXTURE * engine_texture) { + engine_memory_free(engine_texture -> fe_texture); + engine_memory_free(engine_texture); +} + +struct ENGINE_COLOR engine_texture_color_get(struct ENGINE_TEXTURE * engine_texture,int x,int y) { + struct ENGINE_COLOR color; + color.r = 0; + color.g = 0; + color.b = 0; + color.a = 255; + return color; +} + +// COLOR +void engine_color_set(char r,char g,char b,char a) { +} + +// RENDERTARGET +struct ENGINE_TEXTURE * engine_rendertarget; +void engine_rendertarget_set(struct ENGINE_TEXTURE * texture) { + engine_rendertarget = texture; +} + +void engine_rendertarget_draw_point(int x,int y) { +} + +// TIME +long long engine_frontend_time = 0; +long long engine_time_get() { + engine_frontend_time += 1; + return engine_frontend_time; +} + +void engine_time_sleep(int ms) { +} + +// WINDOW +struct ENGINE_WINDOW * engine_window_create(int width,int height,char * title) { + struct ENGINE_WINDOW * window = engine_memory_alloc(NULL,sizeof(struct ENGINE_WINDOW)); + struct ENGINE_TEXTURE * texture = engine_texture_create(width,height); + struct ENGINE_FRONTEND_WINDOW * fe_window = engine_memory_alloc(NULL,sizeof(struct ENGINE_FRONTEND_WINDOW)); + window -> texture = texture; + window -> fe_window = fe_window; + return window; +} + +void engine_window_present(struct ENGINE_WINDOW * window) { +} + +void engine_window_destroy(struct ENGINE_WINDOW * window) { + engine_memory_free(window -> texture); + engine_memory_free(window -> fe_window); + engine_memory_free(window); +} \ No newline at end of file diff --git a/src/frontend/null/structs.c b/src/frontend/null/structs.c new file mode 100644 index 0000000..29526f4 --- /dev/null +++ b/src/frontend/null/structs.c @@ -0,0 +1,2 @@ +struct ENGINE_FRONTEND_TEXTURE { }; +struct ENGINE_FRONTEND_WINDOW { }; diff --git a/src/helpers.c b/src/helpers.c new file mode 100644 index 0000000..855c4e9 --- /dev/null +++ b/src/helpers.c @@ -0,0 +1,58 @@ +void engine_rendertarget_draw_texture(struct ENGINE_TEXTURE * texture,int x,int y) { + int sx = 0; + if (x < 0) { + sx -= x; + } + if (sx > texture->width) { return; } // unnecessary? maybe faster with a lot of offscreen textures? + + int sy = 0; + if (y < 0) { + sy -= y; + } + if (sy > texture->height) { return; } // unnecessary? maybe faster with a lot of offscreen textures? + + int ex = texture->width; + if (x + ex > engine_rendertarget->width) { + ex -= (x + ex) - engine_rendertarget->width; + if (ex < 0) { return; } // unnecessary? maybe faster with a lot of offscreen textures? + } + + int ey = texture->height; + if (y + ey > engine_rendertarget->height) { + ey -= (y + ey) - engine_rendertarget->height; + if (ey < 0) { return; } // unnecessary? maybe faster with a lot of offscreen textures? + } + + struct ENGINE_COLOR color; + int tx = sx; + while (sy < ey) { + while (tx < ex) { + color = engine_texture_color_get(texture,tx,sy); + engine_color_set(color.r,color.g,color.b,color.a); + engine_rendertarget_draw_point(x + tx,y + sy); + ++tx; + } + tx = sx; + ++sy; + } +} + +void engine_rendertarget_draw_file(char * fpath) { + char buffer[engine_rendertarget->width * engine_rendertarget->height * 4]; + FILE * f = fopen(fpath,"r"); + fgets(buffer,engine_rendertarget->width * engine_rendertarget->height * 4,f); + fclose(f); + int x = 0; + int y = 0; + int colorindex = 0; + while (y < engine_rendertarget->height) { + while (x < engine_rendertarget->width) { + colorindex = x + y * engine_rendertarget->width * 4; + engine_color_set(buffer[colorindex],buffer[colorindex+1],buffer[colorindex+2],buffer[colorindex+3]); + engine_rendertarget_draw_point(x,y); + x += 1; + } + x = 0; + y += 1; + } +} \ No newline at end of file diff --git a/src/engine/lua.c b/src/lua.c similarity index 61% rename from src/engine/lua.c rename to src/lua.c index ab716f9..7faa167 100644 --- a/src/engine/lua.c +++ b/src/lua.c @@ -4,50 +4,9 @@ lua_State * engine_lua_state; #include "lua_manual.c" -int engine_luaf_free(lua_State *L) { +int engine_luaf_memory_free(lua_State *L) { void * ptr = lua_touserdata(L,1); - engine_free(ptr); - return 0; -} - -int engine_luaf_window_init(lua_State *L) { - int width = luaL_checkinteger(L,1); - int height = luaL_checkinteger(L,2); - char * title = (char *)luaL_checkstring(L,3); - engine_window_init(width,height,title); - return 0; -} - -int engine_luaf_window_present(lua_State *L) { - engine_window_present(); - return 0; -} - -int engine_luaf_surface_color_set(lua_State *L) { - char r = luaL_checkinteger(L,1); - char g = luaL_checkinteger(L,2); - char b = luaL_checkinteger(L,3); - char a = luaL_checkinteger(L,4); - engine_surface_color_set(r,g,b,a); - return 0; -} - -int engine_luaf_surface_draw_pixel(lua_State *L) { - int x = luaL_checkinteger(L,1); - int y = luaL_checkinteger(L,2); - engine_surface_draw_pixel(x,y); - return 0; -} - -int engine_luaf_time_get(lua_State *L) { - long long outvar = engine_time_get(); - lua_pushinteger(L,outvar); - return 1; -} - -int engine_luaf_time_sleep(lua_State *L) { - long long ms = luaL_checkinteger(L,1); - engine_time_sleep(ms); + engine_memory_free(ptr); return 0; } @@ -71,41 +30,78 @@ int engine_luaf_texture_create(lua_State *L) { return 1; } -int engine_luaf_texture_color_set(lua_State *L) { - char r = luaL_checkinteger(L,1); - char g = luaL_checkinteger(L,2); - char b = luaL_checkinteger(L,3); - char a = luaL_checkinteger(L,4); - engine_texture_color_set(r,g,b,a); - return 0; -} - -int engine_luaf_texture_draw_pixel(lua_State *L) { - struct ENGINE_TEXTURE * texture = lua_touserdata(L,1); - int x = luaL_checkinteger(L,2); - int y = luaL_checkinteger(L,3); - engine_texture_draw_pixel(texture,x,y); - return 0; -} - int engine_luaf_texture_destroy(lua_State *L) { struct ENGINE_TEXTURE * texture = lua_touserdata(L,1); engine_texture_destroy(texture); return 0; } -int engine_luaf_texture_render_2d(lua_State *L) { - struct ENGINE_TEXTURE * texture = lua_touserdata(L,1); - int x = luaL_checkinteger(L,2); - int y = luaL_checkinteger(L,3); - engine_texture_render_2d(texture,x,y); +int engine_luaf_color_set(lua_State *L) { + char r = luaL_checkinteger(L,1); + char g = luaL_checkinteger(L,2); + char b = luaL_checkinteger(L,3); + char a = luaL_checkinteger(L,4); + engine_color_set(r,g,b,a); return 0; } -int engine_luaf_texture_from_file(lua_State *L) { +int engine_luaf_rendertarget_set(lua_State *L) { struct ENGINE_TEXTURE * texture = lua_touserdata(L,1); - char * fpath = (char *)luaL_checkstring(L,2); - engine_texture_from_file(texture,fpath); + engine_rendertarget_set(texture); + return 0; +} + +int engine_luaf_rendertarget_draw_point(lua_State *L) { + int x = luaL_checkinteger(L,1); + int y = luaL_checkinteger(L,2); + engine_rendertarget_draw_point(x,y); + return 0; +} + +int engine_luaf_rendertarget_draw_texture(lua_State *L) { + struct ENGINE_TEXTURE * texture = lua_touserdata(L,1); + int x = luaL_checkinteger(L,2); + int y = luaL_checkinteger(L,3); + engine_rendertarget_draw_texture(texture,x,y); + return 0; +} + +int engine_luaf_rendertarget_draw_file(lua_State *L) { + char * fpath = (char *)luaL_checkstring(L,1); + engine_rendertarget_draw_file(fpath); + return 0; +} + +int engine_luaf_time_sleep(lua_State *L) { + int ms = luaL_checkinteger(L,1); + engine_time_sleep(ms); + return 0; +} + +int engine_luaf_time_get(lua_State *L) { + long long outvar = engine_time_get(); + lua_pushinteger(L,outvar); + return 1; +} + +int engine_luaf_window_create(lua_State *L) { + int width = luaL_checkinteger(L,1); + int height = luaL_checkinteger(L,2); + char * title = (char *)luaL_checkstring(L,3); + struct ENGINE_WINDOW * outvar = engine_window_create(width,height,title); + lua_pushlightuserdata(L,outvar); + return 1; +} + +int engine_luaf_window_present(lua_State *L) { + struct ENGINE_WINDOW * window = lua_touserdata(L,1); + engine_window_present(window); + return 0; +} + +int engine_luaf_window_destroy(lua_State *L) { + struct ENGINE_WINDOW * window = lua_touserdata(L,1); + engine_window_destroy(window); return 0; } @@ -121,36 +117,36 @@ void engine_lua_init() { lua_setglobal(engine_lua_state,"ENGINE_EVENT_TYPE_EXIT"); lua_pushinteger(engine_lua_state,ENGINE_EVENT_TYPE_INPUT_KB); lua_setglobal(engine_lua_state,"ENGINE_EVENT_TYPE_INPUT_KB"); - lua_pushcfunction(engine_lua_state,engine_luaf_free); - lua_setglobal (engine_lua_state,"engine_free"); - lua_pushcfunction(engine_lua_state,engine_luaf_window_init); - lua_setglobal (engine_lua_state,"engine_window_init"); - lua_pushcfunction(engine_lua_state,engine_luaf_window_present); - lua_setglobal (engine_lua_state,"engine_window_present"); - lua_pushcfunction(engine_lua_state,engine_luaf_surface_color_set); - lua_setglobal (engine_lua_state,"engine_surface_color_set"); - lua_pushcfunction(engine_lua_state,engine_luaf_surface_draw_pixel); - lua_setglobal (engine_lua_state,"engine_surface_draw_pixel"); - lua_pushcfunction(engine_lua_state,engine_luaf_time_get); - lua_setglobal (engine_lua_state,"engine_time_get"); - lua_pushcfunction(engine_lua_state,engine_luaf_time_sleep); - lua_setglobal (engine_lua_state,"engine_time_sleep"); + lua_pushcfunction(engine_lua_state,engine_luaf_memory_free); + lua_setglobal (engine_lua_state,"engine_memory_free"); lua_pushcfunction(engine_lua_state,engine_luaf_event_get); lua_setglobal (engine_lua_state,"engine_event_get"); lua_pushcfunction(engine_lua_state,engine_luaf_event_free); lua_setglobal (engine_lua_state,"engine_event_free"); lua_pushcfunction(engine_lua_state,engine_luaf_texture_create); lua_setglobal (engine_lua_state,"engine_texture_create"); - lua_pushcfunction(engine_lua_state,engine_luaf_texture_color_set); - lua_setglobal (engine_lua_state,"engine_texture_color_set"); - lua_pushcfunction(engine_lua_state,engine_luaf_texture_draw_pixel); - lua_setglobal (engine_lua_state,"engine_texture_draw_pixel"); lua_pushcfunction(engine_lua_state,engine_luaf_texture_destroy); lua_setglobal (engine_lua_state,"engine_texture_destroy"); - lua_pushcfunction(engine_lua_state,engine_luaf_texture_render_2d); - lua_setglobal (engine_lua_state,"engine_texture_render_2d"); - lua_pushcfunction(engine_lua_state,engine_luaf_texture_from_file); - lua_setglobal (engine_lua_state,"engine_texture_from_file"); + lua_pushcfunction(engine_lua_state,engine_luaf_color_set); + lua_setglobal (engine_lua_state,"engine_color_set"); + lua_pushcfunction(engine_lua_state,engine_luaf_rendertarget_set); + lua_setglobal (engine_lua_state,"engine_rendertarget_set"); + lua_pushcfunction(engine_lua_state,engine_luaf_rendertarget_draw_point); + lua_setglobal (engine_lua_state,"engine_rendertarget_draw_point"); + lua_pushcfunction(engine_lua_state,engine_luaf_rendertarget_draw_texture); + lua_setglobal (engine_lua_state,"engine_rendertarget_draw_texture"); + lua_pushcfunction(engine_lua_state,engine_luaf_rendertarget_draw_file); + lua_setglobal (engine_lua_state,"engine_rendertarget_draw_file"); + lua_pushcfunction(engine_lua_state,engine_luaf_time_sleep); + lua_setglobal (engine_lua_state,"engine_time_sleep"); + lua_pushcfunction(engine_lua_state,engine_luaf_time_get); + lua_setglobal (engine_lua_state,"engine_time_get"); + lua_pushcfunction(engine_lua_state,engine_luaf_window_create); + lua_setglobal (engine_lua_state,"engine_window_create"); + lua_pushcfunction(engine_lua_state,engine_luaf_window_present); + lua_setglobal (engine_lua_state,"engine_window_present"); + lua_pushcfunction(engine_lua_state,engine_luaf_window_destroy); + lua_setglobal (engine_lua_state,"engine_window_destroy"); engine_lua_init_manual(); luaL_loadfile(engine_lua_state,"mods/main/script/main.lua"); lua_call(engine_lua_state,0,0); diff --git a/src/engine/lua_manual.c b/src/lua_manual.c similarity index 60% rename from src/engine/lua_manual.c rename to src/lua_manual.c index befc5b9..5853532 100644 --- a/src/engine/lua_manual.c +++ b/src/lua_manual.c @@ -18,7 +18,21 @@ int engine_luaf_lua_event_get_data(lua_State *L) { return 1; } +int engine_luaf_texture_get_color(lua_State *L) { + struct ENGINE_TEXTURE * texture = lua_touserdata(L,1); + int x = luaL_checkinteger(L,2); + int y = luaL_checkinteger(L,3); + struct ENGINE_COLOR color = engine_texture_color_get(texture,x,y); + lua_pushinteger(L,color.r); + lua_pushinteger(L,color.g); + lua_pushinteger(L,color.b); + lua_pushinteger(L,color.a); + return 4; +} + void engine_lua_init_manual() { lua_pushcfunction(engine_lua_state,engine_luaf_lua_event_get_data); lua_setglobal (engine_lua_state,"engine_lua_event_get_data"); + lua_pushcfunction(engine_lua_state,engine_luaf_texture_get_color); + lua_setglobal (engine_lua_state,"engine_texture_get_color"); } diff --git a/src/main.c b/src/main.c index ed318ef..1617ca3 100644 --- a/src/main.c +++ b/src/main.c @@ -1,16 +1,14 @@ #include #include -#include "engine/main.c" -#include "engine/textures.c" -#include "engine/frontend/sdl/main.c" -#include "engine/frontend/generic/textures.c" -#include "engine/lua.c" +#include "frontend/null/structs.c" +//#include "frontend/sdl/structs.c" +#include "engine.c" +#include "frontend/null/main.c" +#include "helpers.c" +//#include "frontend/sdl/main.c" +#include "lua.c" int main(int argc, char **argv) { - for (int i = 0; i < argc; ++i) { - printf("argv[%d]: %s\n", i, argv[i]); - } - engine_lua_init(); return 0; } diff --git a/src/values/functions.toml b/src/values/functions.toml new file mode 100644 index 0000000..1fe6061 --- /dev/null +++ b/src/values/functions.toml @@ -0,0 +1,110 @@ +# MEMORY +[engine_memory_alloc] +type = "void *" +arguments = ["void *","size_t"] +argNames = ["ptr", "size"] +description = "Allocate memory, equivalent to C's realloc(), with the difference that it quits on failure." +lua = "no" + +[engine_memory_free] +type = "void" +arguments = ["void *"] +argNames = ["ptr"] +description = "Free memory, equivalent to C's free()." + +# EVENTS +[engine_event_get] +type = "struct ENGINE_EVENT *" +arguments = [] +argNames = [] +description = "Get an event, like the window closing or inputs." + +[engine_event_free] +type = "void" +arguments = ["struct ENGINE_EVENT *"] +argNames = ["event"] +description = "Free an event you have handled from memory." + +# TEXTURES +[engine_texture_create] +type = "struct ENGINE_TEXTURE *" +arguments = ["int", "int"] +argNames = ["width","height"] +description = "Create a texture." + +[engine_texture_color_get] +type = "struct ENGINE_COLOR" +arguments = ["struct ENGINE_TEXTURE *","int","int"] +argNames = ["texture", "x", "y"] +description = "Get color out of a texture." +lua = "manual" + +[engine_texture_destroy] +type = "void" +arguments = ["struct ENGINE_TEXTURE *"] +argNames = ["texture"] +description = "Free a texture from memory." + +# COLOR +[engine_color_set] +type = "void" +arguments = ["char","char","char","char"] +argNames = ["r", "g", "b", "a"] +description = "Set the color that points (pixels) should be drawn in." + +# RENDERTARGET +[engine_rendertarget_set] +type = "void" +arguments = ["struct ENGINE_TEXTURE *"] +argNames = ["texture"] +description = "Set which texture to draw into." + +[engine_rendertarget_draw_point] +type = "void" +arguments = ["int","int"] +argNames = ["x", "y"] +description = "Draw a pixel." + +[engine_rendertarget_draw_texture] +type = "void" +arguments = ["struct ENGINE_TEXTURE *","int","int"] +argNames = ["texture", "x", "y"] +description = "Draw a texture." + +[engine_rendertarget_draw_file] +type = "void" +arguments = ["char *"] +argNames = ["fpath"] +description = "Draw a file into the target." + +# TIME +[engine_time_sleep] +type = "void" +arguments = ["int"] +argNames = ["ms"] +description = "Wait a certain amount of time." + +[engine_time_get] +type = "long long" +arguments = [] +argNames = [] +description = "Get how long the program has been running for, in milliseconds." + +# WINDOW +[engine_window_create] +type = "struct ENGINE_WINDOW *" +arguments = ["int", "int", "char *"] +argNames = ["width","height","title"] +description = "Create a window." + +[engine_window_present] +type = "void" +arguments = ["struct ENGINE_WINDOW *"] +argNames = ["window"] +description = "Update a windows' contents." + +[engine_window_destroy] +type = "void" +arguments = ["struct ENGINE_WINDOW *"] +argNames = ["window"] +description = "Destroy a window." diff --git a/src/engine/values/statics.toml b/src/values/statics.toml similarity index 100% rename from src/engine/values/statics.toml rename to src/values/statics.toml