From 7054e477300a012d0e0dd6a0b8dfb7c20699559e Mon Sep 17 00:00:00 2001 From: KikooDX Date: Tue, 1 Mar 2022 15:13:35 +0100 Subject: [PATCH] font char draw --- CMakeLists.txt | 1 + inc/lzy.h | 117 ++++++++++++++++++++++++++++++++++------ res/font.png | Bin 0 -> 1327 bytes res/fxconv-metadata.txt | 3 ++ src/main.c | 6 ++- 5 files changed, 110 insertions(+), 17 deletions(-) create mode 100644 res/font.png diff --git a/CMakeLists.txt b/CMakeLists.txt index 413ec93..4bb84db 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,7 @@ set(SOURCES set(ASSETS res/tset.png + res/font.png ) fxconv_declare_assets(${ASSETS} WITH_METADATA) diff --git a/inc/lzy.h b/inc/lzy.h index 0c24e97..560ad8c 100644 --- a/inc/lzy.h +++ b/inc/lzy.h @@ -26,15 +26,12 @@ 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, const char *tileset, int target_fps); +int LZY_Init(const char *title, int target_fps, const char *tset_path, + const char *font_path); void LZY_Quit(void); int LZY_DrawBegin(void); int LZY_DrawEnd(void); @@ -44,6 +41,7 @@ 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); +int LZY_DrawChar(unsigned char chr, int x, int y); void LZY_CycleEvents(void); int LZY_KeyDown(unsigned int key); int LZY_ShouldQuit(void); @@ -61,6 +59,9 @@ enum LZY_ScanCode { LZYK_X = KEY_OPTN, }; #else /* end FXCG50, begin SDL2 */ +#ifndef LZY_SDL_INCLUDE +#define LZY_SDL_INCLUDE +#endif #include LZY_SDL_INCLUDE enum LZY_ScanCode { LZYK_LEFT, @@ -80,8 +81,11 @@ enum LZY_ScanCode { /* implementation */ #ifdef LZY_IMPLEMENTATION #undef LZY_IMPLEMENTATION -#ifndef LZY_TILE_SIZE -#define LZY_TILE_SIZE 16 +#ifndef LZY_SDL_INCLUDE +#define LZY_SDL_INCLUDE +#endif +#ifndef LZY_SDL_IMAGE_INCLUDE +#define LZY_SDL_IMAGE_INCLUDE #endif #ifndef LZY_DISPLAY_WIDTH #define LZY_DISPLAY_WIDTH 396 @@ -89,11 +93,17 @@ enum LZY_ScanCode { #ifndef LZY_DISPLAY_HEIGHT #define LZY_DISPLAY_HEIGHT 224 #endif -#ifndef LZY_SDL_INCLUDE -#define LZY_SDL_INCLUDE +#ifndef LZY_TILE_SIZE +#define LZY_TILE_SIZE 16 #endif -#ifndef LZY_SDL_IMAGE_INCLUDE -#define LZY_SDL_IMAGE_INCLUDE +#ifndef LZY_CHR_WIDTH +#define LZY_CHR_WIDTH LZY_TILE_SIZE +#endif +#ifndef LZY_CHR_HEIGHT +#define LZY_CHR_HEIGHT LZY_TILE_SIZE +#endif +#ifndef LZY_FIRST_CHR +#define LZY_FIRST_CHR ' ' #endif #ifdef FXCG50 @@ -110,17 +120,27 @@ static volatile int has_ticked = 0; #ifdef LZY_GINT_TILESET static unsigned int tset_width, tset_height; #endif +#ifdef LZY_GINT_FONT +static unsigned int font_width, font_height; +#endif static int timer_callback(volatile int *); -int LZY_Init(const char *title, const char *tileset, int target_fps) { +int LZY_Init(const char *title, int target_fps, const char *tset_path, + const char *font_path) { #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 +#ifdef LZY_GINT_FONT + extern bopti_image_t LZY_GINT_FONT; + font_width = LZY_GINT_FONT.width / LZY_TILE_SIZE; + font_height = LZY_GINT_FONT.height / LZY_TILE_SIZE; #endif LZY_UNUSED(title); - LZY_UNUSED(tileset); + LZY_UNUSED(tset_path); + LZY_UNUSED(font_path); if (target_fps > 0) { timer = timer_configure(TIMER_ANY, 1000000 / target_fps, @@ -203,6 +223,29 @@ int LZY_DrawTile(unsigned int id, int x, int y) { #endif } +int LZY_DrawChar(unsigned char chr, int x, int y) { +#ifndef LZY_GINT_FONT + LZY_UNUSED(chr); + LZY_UNUSED(x); + LZY_UNUSED(y); + return -1; +#else + extern bopti_image_t LZY_GINT_FONT; + const unsigned int id = (unsigned int)chr - LZY_FIRST_CHR; + int ix, iy; + + if (id >= font_width * font_height) + return -1; + + ix = (id % font_width) * LZY_CHR_WIDTH; + iy = (id / font_width) * LZY_CHR_HEIGHT; + dsubimage(x, y, &LZY_GINT_FONT, ix, iy, LZY_CHR_WIDTH, LZY_CHR_HEIGHT, + DIMAGE_NONE); + + return 0; +#endif +} + void LZY_CycleEvents(void) { clearevents(); should_quit = should_quit || keydown(KEY_EXIT); @@ -234,14 +277,16 @@ 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 SDL_Texture *font = NULL; +static int tset_width, tset_height, font_width, font_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, const char *tileset, int target_fps) { +int LZY_Init(const char *title, int target_fps, const char *tset_path, + const char *font_path) { if (SDL_Init(SDL_INIT_VIDEO) < 0) { error = SDL_GetError(); return -1; @@ -273,7 +318,7 @@ int LZY_Init(const char *title, const char *tileset, int target_fps) { return -4; } - tset = IMG_LoadTexture(renderer, tileset); + tset = IMG_LoadTexture(renderer, tset_path); if (tset == NULL) { error = IMG_GetError(); return -5; @@ -286,6 +331,19 @@ int LZY_Init(const char *title, const char *tileset, int target_fps) { tset_width /= LZY_TILE_SIZE; tset_height /= LZY_TILE_SIZE; + font = IMG_LoadTexture(renderer, font_path); + if (font == NULL) { + error = IMG_GetError(); + return -7; + } + + if (SDL_QueryTexture(font, NULL, NULL, &font_width, &font_height) < 0) { + error = SDL_GetError(); + return -8; + } + font_width /= LZY_CHR_WIDTH; + font_height /= LZY_CHR_HEIGHT; + fps_limiter = target_fps > 0; if (fps_limiter) { min_dt = 1000 / target_fps; @@ -450,6 +508,33 @@ int LZY_DrawTile(unsigned int id, int x, int y) { return 0; } +int LZY_DrawChar(unsigned char chr, int x, int y) { + const unsigned int id = (unsigned int)chr - LZY_FIRST_CHR; + SDL_Rect src, dst; + + if (id >= (unsigned int)(font_width * font_height)) { + error = "chr exceeds boundaries"; + return -1; + } + + src.x = (id % font_width) * LZY_CHR_WIDTH; + src.y = (id / font_width) * LZY_CHR_HEIGHT; + src.w = LZY_CHR_WIDTH; + src.h = LZY_CHR_HEIGHT; + + dst.x = x; + dst.y = y; + dst.w = LZY_CHR_WIDTH; + dst.h = LZY_CHR_HEIGHT; + + if (SDL_RenderCopy(renderer, font, &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, diff --git a/res/font.png b/res/font.png new file mode 100644 index 0000000000000000000000000000000000000000..4acde232de8f011a846ee913d658a63cadb04439 GIT binary patch literal 1327 zcmV+~18wcL$S?q%&$mQ7;s2$;V7ops2d_6r9)eGZ`BH{e*a@{?ze}21>a4 z9m2gaR@R48Z4N-}weR1$KTluLXnsZ(2UR0pi9c*yV>-|IO#sxg;|(?xY~21Tzlb;N z#`ERwz{7twP;i`7yH~89<;`62r+R5u2$MTs-(7kTk3k_gj;$GqLsZI^YWP|1e}xOi z^t90;?zdc)?_dcA6AR5WaZt6gINwqwVR`iRX}v6;MnxU~KCKJui#oy2ldcW#c@ZCU#gCcdqua zfNCMg)qh{K)6atRwZu@W3C$xv02CoukJEZZWf)}bI+!AD%?|*bVi*&C2=wzQnBRf_ z9sjl854ex=s2GH9@}&N<`M*jO%2cqhX_xC6sZ~Bu$)8GvsL6Pa1jKOk(Tj-U{p)Xr z;$h?SF#1qQvy?I5Nbq~>I}O(0?u~_2qmamKH=t3ph$tpPzwauf`(@{iX)11IEziHi z;8(vwhxTY)Zf%F(xJ4o)SeMLbHd=Z?*fnzxK$-z(B@rhV9?pX&Qt7u@ex3}$*8Iy} zI8-hKP9cm`_lkuINF3{X+v2qiH0^`^-Zv*mf*)4~qQ!fiJYx!k#sLQ?@v-<_lin4S zN;>!mS<5!Y;wr|6IS-(g2aTU&-O@k7UtW=h|CnM`N_M22l(M(7Z2M zcaQznCj`1@>=uTu`zZ3fROMhpel*dal7yU}RI*deDcVF-W-6@_#nnmn9@Ul9W|HN% zpA({d2DX5nvlnf9!V76WjQ`J!+1M!Re8RnbjNV_PJoWQzNFIY7K*mG>*K7kqywSzY z+DMhn2tN~krvQ+Fe(W^%QA)FxF=<=-Jtt?;;1h;Q%;V9lr#s27gED4GT zjTKuNkK-Z-Hs)hNg3#Ihso}D6#T$K%kA_Pfw?0S{KVs*F<|shucHH@YodQWJQs)EA zR8XjkF;%&J@n4x12IL5n{7!Je5gUl^5X_)n{@}=Y$3oXm7{$Vk-4RDmOAZ?YG6^6^ z2b}#SKMzq8xJO^x%~&jG?>*-Oh9-^7FQ%ZwlM#oQ~D5pah`A3w?CT3<>GBGl}!Ku002ovPDHLkV1i~jezpJr literal 0 HcmV?d00001 diff --git a/res/fxconv-metadata.txt b/res/fxconv-metadata.txt index 8ca05ea..cbf605c 100644 --- a/res/fxconv-metadata.txt +++ b/res/fxconv-metadata.txt @@ -1,3 +1,6 @@ tset.png: type: bopti-image name: bimg_tset +font.png: + type: bopti-image + name: bimg_font diff --git a/src/main.c b/src/main.c index a051038..9ed25a7 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,9 @@ #define LZY_IMPLEMENTATION #define LZY_GINT_TILESET bimg_tset +#define LZY_GINT_FONT bimg_font +#define LZY_CHR_WIDTH 16 +#define LZY_CHR_HEIGHT 16 +#define LZY_FIRST_CHR ' ' #include "lzy.h" static void draw_player(int x, int y); @@ -8,7 +12,7 @@ int main(void) { int x = 0; int y = 0; - if (LZY_Init("lzy example", "res/tset.png", 30)) { + if (LZY_Init("lzy example", 30, "res/tset.png", "res/font.png")) { LZY_Log(LZY_GetError()); LZY_Quit(); return 1;