From 9bb961f63f3e14e84878adfe3057a7ca51de7f81 Mon Sep 17 00:00:00 2001 From: Fierelier Date: Fri, 19 May 2023 01:01:02 +0200 Subject: [PATCH] Fix engine_texture_render_2d --- src/engine/frontend/generic/textures.c | 52 +++++++++++++++++--------- src/engine/lua.c | 6 +-- src/engine/values/functions.toml | 2 +- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/engine/frontend/generic/textures.c b/src/engine/frontend/generic/textures.c index 637e421..d1dc1fd 100644 --- a/src/engine/frontend/generic/textures.c +++ b/src/engine/frontend/generic/textures.c @@ -46,27 +46,43 @@ void engine_texture_destroy(struct ENGINE_TEXTURE * texture) { engine_free(texture); } -void engine_texture_render_2d(struct ENGINE_TEXTURE * texture,int sx,int sy) { - char * pixels = ((struct ENGINE_GENERIC_TEXTURE *)(texture->fe_texture))->pixels; - int ex = sx + texture->width; - if (ex > engine_width) { ex = engine_width; } - int ey = sy + texture->height; - if (ey > engine_height) { ey = engine_height; } - int x = sx; - if (x < 0) { x = 0; } - int y = sy; - if (y < 0) { y = 0; } - long pixelIndex = 0; +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? - while (y < ey) { - while (x < ex) { - pixelIndex = ((x - sx) + (y - sy) * texture->width)*4; + 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,y); - ++x; + engine_surface_draw_pixel(x + tx,y + sy); + ++tx; } - x = sx; - ++y; + tx = sx; + ++sy; } } diff --git a/src/engine/lua.c b/src/engine/lua.c index 59bdd32..ab716f9 100644 --- a/src/engine/lua.c +++ b/src/engine/lua.c @@ -96,9 +96,9 @@ int engine_luaf_texture_destroy(lua_State *L) { int engine_luaf_texture_render_2d(lua_State *L) { struct ENGINE_TEXTURE * texture = lua_touserdata(L,1); - int sx = luaL_checkinteger(L,2); - int sy = luaL_checkinteger(L,3); - engine_texture_render_2d(texture,sx,sy); + int x = luaL_checkinteger(L,2); + int y = luaL_checkinteger(L,3); + engine_texture_render_2d(texture,x,y); return 0; } diff --git a/src/engine/values/functions.toml b/src/engine/values/functions.toml index 8bbdcc6..3f499fd 100644 --- a/src/engine/values/functions.toml +++ b/src/engine/values/functions.toml @@ -91,7 +91,7 @@ description = "Destroy a texture, freeing up memory." [engine_texture_render_2d] type = "void" arguments = ["struct ENGINE_TEXTURE *","int","int"] -argNames = ["texture", "sx", "sy"] +argNames = ["texture", "x", "y"] description = "Render texture to the window surface." [engine_texture_from_file]