From 5fe23445897f5cb69ff547acf927c97eab406d7a Mon Sep 17 00:00:00 2001 From: Fierelier Date: Wed, 11 Sep 2024 01:53:56 +0200 Subject: [PATCH] More standardization --- main.c | 61 +++++++++++++++++++++++++++++++++++++++++++-- main.h | 19 ++++++++++++++ main.i | 19 ++++++++++++++ structs.h | 19 ++++++++++++++ test/lua.c | 19 ++++++++++++++ texture.c | 73 ------------------------------------------------------ types.h | 4 +++ 7 files changed, 139 insertions(+), 75 deletions(-) create mode 100644 main.h create mode 100644 main.i create mode 100644 structs.h create mode 100644 test/lua.c delete mode 100644 texture.c create mode 100644 types.h diff --git a/main.c b/main.c index 0aab06e..c393cdd 100644 --- a/main.c +++ b/main.c @@ -1,2 +1,59 @@ -typedef int32_t unigi_ext_type_2d_coord; // Signed 2D coordinate -#include "texture.c" +#include "unigi.ext/main.h" + +void unigi_ext_texture_draw( + unigi_ext_type_texture tex, + unigi_ext_type_texture buffer, + unigi_ext_type_rect texbounds, + unigi_ext_type_rect bufbounds +) { + unigi_ext_type_2d_coord texwidth; + unigi_ext_type_2d_coord texheight; + unigi_ext_type_2d_coord outwidth; + unigi_ext_type_2d_coord outheight; + unigi_ext_type_2d_coord x; + unigi_ext_type_2d_coord y; + unigi_type_resolution_1d_coord texi; + unigi_type_resolution_1d_coord bufi; + unigi_ext_type_2d_coord stepx; + unigi_ext_type_2d_coord stepy; + unigi_ext_type_2d_coord texx; + unigi_ext_type_2d_coord texy; + + unigi_ext_rect_dimensions(texbounds, &texwidth, &texheight); + unigi_ext_rect_dimensions(bufbounds, &outwidth, &outheight); + // We precalculate the step through the texture to avoid division and + // multiplication per pixel. We also expand the range so we can use + // integers. By using 256, we are essentially adding 8 bits of "decimal + // point". If you need more bits (or less), just change this to some other + // power of 2. + stepx = (1 << unigi_ext_texture_fixedpointdepth) * (float)texwidth / outwidth; + stepy = (1 << unigi_ext_texture_fixedpointdepth) * (float)texheight / outheight; + texx = (1 << unigi_ext_texture_fixedpointdepth) * texbounds.x1; + texy = (1 << unigi_ext_texture_fixedpointdepth) * texbounds.y1; + //log("%f,%f step %f,%f", texx, texy, stepx, stepy); + // Buffer is R G B for each pixel, then across then down. If you want X, Y, + // it's: 3 * (x + y * width) + for(y = bufbounds.y1; y < bufbounds.y2; y++) { + texx = texbounds.x1; + for(x = bufbounds.x1; x < bufbounds.x2; x++) { + // And then with the shift, it is log2(256), or whatever you shifted + // by up there. So 512 would be 9, because 2^9 = 512 + texi = (texx >> unigi_ext_texture_fixedpointdepth) + tex.width * (texy >> unigi_ext_texture_fixedpointdepth); // 'texy >> FIXEDPOINTDEPTH' could be calculated per y-iteration + bufi = x + y * buffer.width; // 'y * buffer.width' could be calculated per y-iteration + buffer.pixels[bufi] = tex.pixels[texi]; + texx += stepx; + bufi += 1; + } + texy += stepy; + } +} + +void unigi_ext_rect_dimensions( + unigi_ext_type_rect bounds, + unigi_ext_type_2d_coord * width, + unigi_ext_type_2d_coord * height +) { + *width = abs(bounds.x2 - bounds.x1); + *height = abs(bounds.y2 - bounds.y1); +} + diff --git a/main.h b/main.h new file mode 100644 index 0000000..c084c18 --- /dev/null +++ b/main.h @@ -0,0 +1,19 @@ +#ifndef unigi_header_ext_main +#define unigi_header_ext_main +#define unigi_ext_texture_fixedpointdepth 8 +#include "types.h" +#include "structs.h" + +void unigi_ext_texture_draw( + unigi_ext_type_texture tex, + unigi_ext_type_texture buffer, + unigi_ext_type_rect texbounds, + unigi_ext_type_rect bufbounds +); + +void unigi_ext_rect_dimensions( + unigi_ext_type_rect bounds, + unigi_ext_type_2d_coord * width, + unigi_ext_type_2d_coord * height +); +#endif diff --git a/main.i b/main.i new file mode 100644 index 0000000..62cea86 --- /dev/null +++ b/main.i @@ -0,0 +1,19 @@ +%module unigi +%{ +#include +#include "unigi/types.h" +#include "unigi/structs.h" +#include "unigi/main.h" +#include "unigi.ext/types.h" +#include "unigi.ext/structs.h" +#include "unigi.ext/main.h" +%} +%include +%include +%rename("%(strip:[unigi_])s") ""; +%include "unigi/types.h" +%include "unigi/structs.h" +%include "unigi/main.h" +%include "unigi.ext/types.h" +%include "unigi.ext/structs.h" +%include "unigi.ext/main.h" diff --git a/structs.h b/structs.h new file mode 100644 index 0000000..94d945f --- /dev/null +++ b/structs.h @@ -0,0 +1,19 @@ +#ifndef unigi_header_ext_headers +#define unigi_header_ext_headers +#include "unigi/types.h" +struct unigi_ext_type_texture { + unigi_type_resolution_2d_coord width; + unigi_type_resolution_2d_coord height; + unigi_type_color lq_color; + unigi_type_color * pixels; +}; +typedef struct unigi_ext_type_texture unigi_ext_type_texture; + +struct unigi_ext_type_rect { + unigi_ext_type_2d_coord x1; + unigi_ext_type_2d_coord y1; + unigi_ext_type_2d_coord x2; + unigi_ext_type_2d_coord y2; +}; +typedef struct unigi_ext_type_rect unigi_ext_type_rect; +#endif diff --git a/test/lua.c b/test/lua.c new file mode 100644 index 0000000..1e3bc5c --- /dev/null +++ b/test/lua.c @@ -0,0 +1,19 @@ +#include "unigi/main.h" +#include "unigi.platform.sdl1/main.c" +#include "unigi.ext/main.c" +#include "unigi.ext/main_wrap.c" + +#if LUA_VERSION_NUM < 503 +#include +#endif + + +int main(int argc, char *argv[]) { + lua_State *L = luaL_newstate(); + assert(L != NULL); + luaL_openlibs(L); + luaopen_unigi(L); // Load the wrapped module + luaL_loadfile(L, "mods/main/script/main.lua"); + lua_pcall(L, 0, 0, 0); + return 0; +} diff --git a/texture.c b/texture.c deleted file mode 100644 index cb61ba1..0000000 --- a/texture.c +++ /dev/null @@ -1,73 +0,0 @@ -struct unigi_ext_type_texture { - unigi_type_resolution_2d_coord width; - unigi_type_resolution_2d_coord height; - unigi_type_color lq_color; - unigi_type_color * pixels; -}; -typedef struct unigi_ext_type_texture unigi_ext_type_texture; - -struct unigi_ext_type_rect { - unigi_ext_type_2d_coord x1; - unigi_ext_type_2d_coord y1; - unigi_ext_type_2d_coord x2; - unigi_ext_type_2d_coord y2; -}; -typedef struct unigi_ext_type_rect unigi_ext_type_rect; - -void unigi_ext_rect_dimensions( - unigi_ext_type_rect bounds, - unigi_ext_type_2d_coord * width, - unigi_ext_type_2d_coord * height -) { - *width = abs(bounds.x2 - bounds.x1); - *height = abs(bounds.y2 - bounds.y1); -} - -#define unigi_ext_texture_fixedpointdepth 8 -void unigi_ext_texture_draw( - unigi_ext_type_texture tex, - unigi_ext_type_texture buffer, - unigi_ext_type_rect texbounds, - unigi_ext_type_rect bufbounds -) { - unigi_ext_type_2d_coord texwidth; - unigi_ext_type_2d_coord texheight; - unigi_ext_type_2d_coord outwidth; - unigi_ext_type_2d_coord outheight; - unigi_ext_type_2d_coord x; - unigi_ext_type_2d_coord y; - unigi_type_resolution_1d_coord texi; - unigi_type_resolution_1d_coord bufi; - unigi_ext_type_2d_coord stepx; - unigi_ext_type_2d_coord stepy; - unigi_ext_type_2d_coord texx; - unigi_ext_type_2d_coord texy; - - unigi_ext_rect_dimensions(texbounds, &texwidth, &texheight); - unigi_ext_rect_dimensions(bufbounds, &outwidth, &outheight); - // We precalculate the step through the texture to avoid division and - // multiplication per pixel. We also expand the range so we can use - // integers. By using 256, we are essentially adding 8 bits of "decimal - // point". If you need more bits (or less), just change this to some other - // power of 2. - stepx = (1 << unigi_ext_texture_fixedpointdepth) * (float)texwidth / outwidth; - stepy = (1 << unigi_ext_texture_fixedpointdepth) * (float)texheight / outheight; - texx = (1 << unigi_ext_texture_fixedpointdepth) * texbounds.x1; - texy = (1 << unigi_ext_texture_fixedpointdepth) * texbounds.y1; - //log("%f,%f step %f,%f", texx, texy, stepx, stepy); - // Buffer is R G B for each pixel, then across then down. If you want X, Y, - // it's: 3 * (x + y * width) - for(y = bufbounds.y1; y < bufbounds.y2; y++) { - texx = texbounds.x1; - for(x = bufbounds.x1; x < bufbounds.x2; x++) { - // And then with the shift, it is log2(256), or whatever you shifted - // by up there. So 512 would be 9, because 2^9 = 512 - texi = (texx >> unigi_ext_texture_fixedpointdepth) + tex.width * (texy >> unigi_ext_texture_fixedpointdepth); // 'texy >> FIXEDPOINTDEPTH' could be calculated per y-iteration - bufi = x + y * buffer.width; // 'y * buffer.width' could be calculated per y-iteration - buffer.pixels[bufi] = tex.pixels[texi]; - texx += stepx; - bufi += 1; - } - texy += stepy; - } -} diff --git a/types.h b/types.h new file mode 100644 index 0000000..b7eedc2 --- /dev/null +++ b/types.h @@ -0,0 +1,4 @@ +#ifndef unigi_header_ext_types +#define unigi_header_ext_types +typedef int32_t unigi_ext_type_2d_coord; // Signed 2D coordinate +#endif