From 2a4407adc4ab9c56f5bd0b265553df044f861e8a Mon Sep 17 00:00:00 2001 From: KikooDX Date: Tue, 1 Mar 2022 01:54:20 +0100 Subject: [PATCH] tileset and tile draw --- CMakeLists.txt | 1 + inc/lzy.h | 109 ++++++++++++++++++++++++++++++++++++++++++++----- res/tset.png | Bin 0 -> 1491 bytes src/main.c | 10 ++--- 4 files changed, 103 insertions(+), 17 deletions(-) create mode 100644 res/tset.png diff --git a/CMakeLists.txt b/CMakeLists.txt index 295b474..413ec93 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ set(SOURCES ) set(ASSETS + res/tset.png ) fxconv_declare_assets(${ASSETS} WITH_METADATA) diff --git a/inc/lzy.h b/inc/lzy.h index 52296bb..0c24e97 100644 --- a/inc/lzy.h +++ b/inc/lzy.h @@ -25,11 +25,16 @@ #ifdef __cplusplus extern "C" { #endif + +#ifndef LZY_SDL_INCLUDE +#define LZY_SDL_INCLUDE +#endif + #include #define LZY_UNUSED(x) (void)(x) -int LZY_Init(const char *title, int target_fps); +int LZY_Init(const char *title, const char *tileset, int target_fps); void LZY_Quit(void); int LZY_DrawBegin(void); int LZY_DrawEnd(void); @@ -38,6 +43,7 @@ void LZY_DrawSetColorNone(void); int LZY_DrawClear(void); int LZY_DrawRect(int x, int y, unsigned int w, unsigned int h); int LZY_DrawFillRect(int x, int y, unsigned int w, unsigned int h); +int LZY_DrawTile(unsigned int id, int x, int y); void LZY_CycleEvents(void); int LZY_KeyDown(unsigned int key); int LZY_ShouldQuit(void); @@ -55,7 +61,7 @@ enum LZY_ScanCode { LZYK_X = KEY_OPTN, }; #else /* end FXCG50, begin SDL2 */ -#include +#include LZY_SDL_INCLUDE enum LZY_ScanCode { LZYK_LEFT, LZYK_RIGHT, @@ -83,6 +89,12 @@ enum LZY_ScanCode { #ifndef LZY_DISPLAY_HEIGHT #define LZY_DISPLAY_HEIGHT 224 #endif +#ifndef LZY_SDL_INCLUDE +#define LZY_SDL_INCLUDE +#endif +#ifndef LZY_SDL_IMAGE_INCLUDE +#define LZY_SDL_IMAGE_INCLUDE +#endif #ifdef FXCG50 #include @@ -95,11 +107,21 @@ static color_t draw_color = C_BLACK; static int should_quit = 0; static int timer = 0; static volatile int has_ticked = 0; +#ifdef LZY_GINT_TILESET +static unsigned int tset_width, tset_height; +#endif static int timer_callback(volatile int *); -int LZY_Init(const char *title, int target_fps) { +int LZY_Init(const char *title, const char *tileset, int target_fps) { +#ifdef LZY_GINT_TILESET + extern bopti_image_t LZY_GINT_TILESET; + tset_width = LZY_GINT_TILESET.width / LZY_TILE_SIZE; + tset_height = LZY_GINT_TILESET.height / LZY_TILE_SIZE; +#endif LZY_UNUSED(title); + LZY_UNUSED(tileset); + if (target_fps > 0) { timer = timer_configure(TIMER_ANY, 1000000 / target_fps, GINT_CALL(timer_callback, &has_ticked)); @@ -159,6 +181,28 @@ int LZY_DrawFillRect(int x, int y, unsigned int w, unsigned int h) { return 0; } +int LZY_DrawTile(unsigned int id, int x, int y) { +#ifndef LZY_GINT_TILESET + LZY_UNUSED(id); + LZY_UNUSED(x); + LZY_UNUSED(y); + return -1; +#else + extern bopti_image_t LZY_GINT_TILESET; + int ix, iy; + + if (id >= tset_width * tset_height) + return -1; + + ix = id % tset_width * LZY_TILE_SIZE; + iy = id % tset_height * LZY_TILE_SIZE; + dsubimage(x, y, &LZY_GINT_TILESET, ix, iy, LZY_TILE_SIZE, LZY_TILE_SIZE, + DIMAGE_NONE); + + return 0; +#endif +} + void LZY_CycleEvents(void) { clearevents(); should_quit = should_quit || keydown(KEY_EXIT); @@ -181,20 +225,23 @@ const char *LZY_GetError(void) { } #else /* end FXCG50, begin SDL2 */ -#include +#include LZY_SDL_INCLUDE +#include LZY_SDL_IMAGE_INCLUDE #include static const char *error = NULL; static SDL_Window *window = NULL; static SDL_Renderer *renderer = NULL; static SDL_Texture *target = NULL; +static SDL_Texture *tset = NULL; +static int tset_width, tset_height; static int should_quit = 0; static int input[LZYK_COUNT] = {0}; static int fps_limiter = 0; static uint64_t min_dt; static uint64_t next_time; -int LZY_Init(const char *title, int target_fps) { +int LZY_Init(const char *title, const char *tileset, int target_fps) { if (SDL_Init(SDL_INIT_VIDEO) < 0) { error = SDL_GetError(); return -1; @@ -226,6 +273,19 @@ int LZY_Init(const char *title, int target_fps) { return -4; } + tset = IMG_LoadTexture(renderer, tileset); + if (tset == NULL) { + error = IMG_GetError(); + return -5; + } + + if (SDL_QueryTexture(tset, NULL, NULL, &tset_width, &tset_height) < 0) { + error = SDL_GetError(); + return -6; + } + tset_width /= LZY_TILE_SIZE; + tset_height /= LZY_TILE_SIZE; + fps_limiter = target_fps > 0; if (fps_limiter) { min_dt = 1000 / target_fps; @@ -236,6 +296,11 @@ int LZY_Init(const char *title, int target_fps) { } void LZY_Quit(void) { + if (tset != NULL) { + SDL_DestroyTexture(tset); + tset = NULL; + } + if (target != NULL) { SDL_DestroyTexture(target); target = NULL; @@ -359,14 +424,36 @@ int LZY_DrawFillRect(int x, int y, unsigned int w, unsigned int h) { return 0; } +int LZY_DrawTile(unsigned int id, int x, int y) { + SDL_Rect src, dst; + + if (id >= (unsigned int)(tset_width * tset_height)) { + error = "id exceeds boundaries"; + return -1; + } + + src.x = (id % tset_width) * LZY_TILE_SIZE; + src.y = (id / tset_width) * LZY_TILE_SIZE; + src.w = LZY_TILE_SIZE; + src.h = LZY_TILE_SIZE; + + dst.x = x; + dst.y = y; + dst.w = LZY_TILE_SIZE; + dst.h = LZY_TILE_SIZE; + + if (SDL_RenderCopy(renderer, tset, &src, &dst) < 0) { + error = SDL_GetError(); + return -2; + } + + return 0; +} + void LZY_CycleEvents(void) { static const SDL_Scancode sc[LZYK_COUNT] = { - SDL_SCANCODE_LEFT, - SDL_SCANCODE_RIGHT, - SDL_SCANCODE_UP, - SDL_SCANCODE_DOWN, - SDL_SCANCODE_Z, - SDL_SCANCODE_X, + SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_UP, + SDL_SCANCODE_DOWN, SDL_SCANCODE_Z, SDL_SCANCODE_X, }; SDL_Event e; diff --git a/res/tset.png b/res/tset.png new file mode 100644 index 0000000000000000000000000000000000000000..ea428d6ddb2235bb9f52b6368c877e92f24f309b GIT binary patch literal 1491 zcmV;^1uXiBP);Vh|MvE>TGIlY7s!KHmS-TeK zp4o#0G)sUMMV4eMT*-p&bax$I{7RF&yGPzhI?4P^ymypincsVS?~&omQhA=^csw5K z6ONCMZ#)l&IQ)2es?R?-KbIeWy!-~ouU@~?=RbXNPk#LF>=4I7h%C!k)5;&_j4^(} ziI%(h5%DMg$=|XK*W;7Ftmgmr^79Y7yNt2J=@gCSmZJx67-MfokMJ$ULX=RLCZbQJ zUlN6WSrqyeWmz`Aso8AS2>tW{oqj7<*%|@a^Z}5Kh!Y2Ng^V%IIc2Imt%dY6#sK%7 zogKv}giwq%ouS0y<=_k@7Fjyq4#XQA00VrHCn&GlN&jRriK0liM^O~V@q9j4d|*DG zbI!ZnZiU-la60Aug5PCFp6n7GPNxdxG6@PQhtnw=JyIx_Nl;L6NP)QR2S%fj9sRwY z75(>r{>qB}@4x*l%d**QhV&;%BE+ZEE(}9JA125}LLA4PPA5&%EXyk0fZ_wT1%O@# z6y>6oTQq#IscBX}!J!7gNMD2jxPe>}fDaU50B&HUf6yN&mgn!En|A?Dk_Znqo9aS#MS5P(-?Sr&%j z*2iEsHy{>@s}T@inG^v1GVz1{V9+1P7EvB-YSPba>9>jf^7aSmO_C%)vk*cE0p5#W z0k#EM-EJ2g09F@1n2w^h&L8Gqu=`uH8!RL0D+PeQ?lL4F zc6YzNnGwkAuI~d!58l|80mAv4w2l7y;+%_X)$gA+vI)?`e+v_pjZo-Tb+wcJJkKHT z$0AsMTX{x?dwrUwSS@UK7pQ!p;cc@{rF4Nxz%X`!0F0sR?jGQ+`$SCn!7}lkNgl(tmJ%F5d;Jr2pxYdw3UUGMV&DXAVW8p99#XKsC+* zHc`%Zfs7&2a1f@ocbYv23n_u2GG?^lY`E9!!9Jjz2gh+N=k&5yCrQ%C5!mK-fmSd5 zvO`HOq<4YJb0LQ=P{?SfT_w}nb29=chs!_muWh^{;cz<$vV7Skbc>g5F(A8yZqWn0 zydmfomm=8VGF%Jk$C4n5qMe(nl`&tJw#0!|O0xymq1~XT4=|!z4O~*7C6!Pw_V#f6 z!{w!U!r2)<4)_1S@%z7D>ht?|P6j^}{&UOt{{VV>r`&R@u!g%mR{G`oq>^a)_n%*t zw-n2=9KczH^cSdzexEsPv|jA(0SUyxaQ_*Q(7$s+AGFQ^JpEoe5&!_Qp<5Ir003k| zx43NYEAx0F{pj&T`s;jouk~&M#A(F1?hyW0;Q*C|0cEc!iT&OIw$cZb2@2eL;CG{+ z)wvAk>GxWTfOX7&>UKCh{a$Mf|2hxyD?J60W52QwY(NU+>Gx9T*VkQ{AlHOO#1;3j z1w(lHy>7F(r{Bw`K%RcDwfO*Lf-%pjnf!o4Pruh@sgd)qHPiqU`u*9k5C4bkrv9jp01$;osbCgAwEp9#_JpEp4^aMH5>lS6jmGiGgPQ+Gm7s!hK t;r=r#`h8)riRddNlv|7!A1W static void draw_player(int x, int y); @@ -8,7 +8,7 @@ int main(void) { int x = 0; int y = 0; - if (LZY_Init("lzy example", 30)) { + if (LZY_Init("lzy example", "res/tset.png", 30)) { LZY_Log(LZY_GetError()); LZY_Quit(); return 1; @@ -43,8 +43,6 @@ int main(void) { } static void draw_player(int x, int y) { - LZY_DrawSetColor(0x00, 0xff, 0xff); - LZY_DrawFillRect(x, y, 8, 8); - LZY_DrawSetColor(0xff, 0x00, 0x00); - LZY_DrawRect(x + 1, y + 1, 6, 6); + if (LZY_DrawTile(4, x, y)) + LZY_Log(LZY_GetError()); }