Fix engine_texture_render_2d

This commit is contained in:
Fierelier 2023-05-19 01:01:02 +02:00
parent 7f99a9c25c
commit 9bb961f63f
3 changed files with 38 additions and 22 deletions

View File

@ -46,27 +46,43 @@ void engine_texture_destroy(struct ENGINE_TEXTURE * texture) {
engine_free(texture); engine_free(texture);
} }
void engine_texture_render_2d(struct ENGINE_TEXTURE * texture,int sx,int sy) { void engine_texture_render_2d(struct ENGINE_TEXTURE * texture,int x,int y) {
char * pixels = ((struct ENGINE_GENERIC_TEXTURE *)(texture->fe_texture))->pixels; int sx = 0;
int ex = sx + texture->width; if (x < 0) {
if (ex > engine_width) { ex = engine_width; } sx -= x;
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;
while (y < ey) {
while (x < ex) {
pixelIndex = ((x - sx) + (y - 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;
} }
x = sx; if (sx > texture->width) { return; } // unnecessary? maybe faster with a lot of offscreen textures?
++y;
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;
} }
} }

View File

@ -96,9 +96,9 @@ int engine_luaf_texture_destroy(lua_State *L) {
int engine_luaf_texture_render_2d(lua_State *L) { int engine_luaf_texture_render_2d(lua_State *L) {
struct ENGINE_TEXTURE * texture = lua_touserdata(L,1); struct ENGINE_TEXTURE * texture = lua_touserdata(L,1);
int sx = luaL_checkinteger(L,2); int x = luaL_checkinteger(L,2);
int sy = luaL_checkinteger(L,3); int y = luaL_checkinteger(L,3);
engine_texture_render_2d(texture,sx,sy); engine_texture_render_2d(texture,x,y);
return 0; return 0;
} }

View File

@ -91,7 +91,7 @@ description = "Destroy a texture, freeing up memory."
[engine_texture_render_2d] [engine_texture_render_2d]
type = "void" type = "void"
arguments = ["struct ENGINE_TEXTURE *","int","int"] arguments = ["struct ENGINE_TEXTURE *","int","int"]
argNames = ["texture", "sx", "sy"] argNames = ["texture", "x", "y"]
description = "Render texture to the window surface." description = "Render texture to the window surface."
[engine_texture_from_file] [engine_texture_from_file]