diff options
author | kdx <kikoodx@paranoici.org> | 2023-06-24 11:12:58 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-06-24 11:12:58 +0200 |
commit | 93be6f04190a7063b4d93ed83cdcd919775debb9 (patch) | |
tree | 6e15e846342399b1c38583c9bbc77fbdbf2133b9 | |
parent | 1896068d35f827c67a459d0a7494c12f240219a3 (diff) | |
download | hmle-main.tar.gz |
-rw-r--r-- | Tupfile | 2 | ||||
-rw-r--r-- | src/font.c | 24 | ||||
-rw-r--r-- | src/font.h | 12 | ||||
-rw-r--r-- | src/main.c | 8 | ||||
-rw-r--r-- | src/picofont.h | 20 | ||||
-rw-r--r-- | src/texture.c | 16 | ||||
-rw-r--r-- | src/texture.h | 4 |
7 files changed, 83 insertions, 3 deletions
@@ -1,7 +1,7 @@ CC = zig cc LD = $(CC) SFLAGS = -Wall -Wextra -CFLAGS = -std=c2x $(SFLAGS) -Isrc +CFLAGS = -std=c2x $(SFLAGS) -Isrc -Ibuild/res LIBS = -lm -lSDL2 -lSDL2_image : foreach src/*.c |> cp %f %o |> build/%f diff --git a/src/font.c b/src/font.c new file mode 100644 index 0000000..3e620aa --- /dev/null +++ b/src/font.c @@ -0,0 +1,24 @@ +#include "font.h" +#include "picofont.h" +#include "log.h" + +int +font_init(Font *this, Window *win) +{ + memset(this, 0, sizeof(*this)); + + SDL_RWops *const rw = + SDL_RWFromMem(res_picofont_png, sizeof(res_picofont_png)); + if (rw == NULL) { + log_error("%s", SDL_GetError()); + return -1; + } + + return texture_load_rw(&this->tex, win, rw); +} + +void +font_deinit(Font *this) +{ + texture_deinit(&this->tex); +} diff --git a/src/font.h b/src/font.h new file mode 100644 index 0000000..1a5f9b5 --- /dev/null +++ b/src/font.h @@ -0,0 +1,12 @@ +#pragma once +#include "window.h" +#include "texture.h" + +typedef struct Font { + Texture tex; +} Font; + +[[nodiscard]] +int font_init(Font *this, Window *win); + +void font_deinit(Font *this); @@ -3,10 +3,12 @@ #include "window.h" #include "tileset.h" #include "map.h" +#include "font.h" #include <errno.h> #include <SDL2/SDL.h> Window window = {}; +Font font = {}; Tileset tileset = {}; Map map = {}; @@ -36,6 +38,11 @@ int init(const char *tset_path) return deinit(), -1; } + if (font_init(&font, &window)) { + log_fatal("font_init failed"); + return deinit(), -1; + } + if (tileset_init(&tileset, &window, tset_path, 16, 16)) { log_fatal("tileset_init failed"); return deinit(), -1; @@ -54,6 +61,7 @@ deinit(void) { map_deinit(&map); tileset_deinit(&tileset); + font_deinit(&font); window_deinit(&window); sdl_deinit(); } diff --git a/src/picofont.h b/src/picofont.h new file mode 100644 index 0000000..672f6e8 --- /dev/null +++ b/src/picofont.h @@ -0,0 +1,20 @@ +static unsigned char res_picofont_png[] = { +137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,64, +0,0,0,36,2,3,0,0,0,68,98,212,1,0,0,0,9,80,76,84, +69,0,0,0,255,0,255,255,255,255,185,28,98,239,0,0,0,1,116,82, +78,83,0,64,230,216,102,0,0,0,238,73,68,65,84,40,207,125,146,59, +114,196,48,12,67,89,242,24,44,117,76,150,56,6,75,28,51,143,222,120, +243,113,38,178,11,15,4,1,4,228,136,146,172,169,42,5,43,3,192,147, +211,157,85,0,181,175,78,77,116,218,225,5,34,236,86,44,163,110,6,167, +98,53,58,170,227,177,60,182,220,70,1,139,54,252,76,225,33,161,225,74, +0,31,182,215,3,141,60,0,157,112,120,46,198,122,94,219,105,4,120,235, +233,82,246,193,232,8,179,246,160,33,53,46,141,9,128,20,68,107,205,180, +171,248,0,232,23,67,23,99,1,14,51,7,100,15,189,104,254,200,178,147, +106,23,98,135,49,214,165,46,32,187,146,26,233,211,11,48,54,0,71,122, +72,177,83,101,127,2,135,202,79,225,66,215,135,86,159,89,126,3,73,135, +132,243,59,92,232,149,69,155,101,195,225,123,101,241,157,37,86,254,223,44, +223,214,94,241,143,112,212,158,245,14,87,78,38,182,184,202,185,179,192,216, +223,224,43,203,106,60,179,124,0,98,78,175,209,167,21,116,19,0,0,0, +0,73,69,78,68,174,66,96,130 +}; + diff --git a/src/texture.c b/src/texture.c index 4748624..b70cfae 100644 --- a/src/texture.c +++ b/src/texture.c @@ -28,11 +28,11 @@ texture_init_target(Texture *this, Window *win, int w, int h) } int -texture_load(Texture *this, Window *win, const char *path) +texture_load_rw(Texture *this, Window *win, SDL_RWops *rw) { memset(this, 0, sizeof(*this)); - this->ptr = IMG_LoadTexture(win->ren, path); + this->ptr = IMG_LoadTexture_RW(win->ren, rw, 1); if (this->ptr == NULL) { log_error("%s", SDL_GetError()); return texture_deinit(this), -1; @@ -49,6 +49,18 @@ texture_load(Texture *this, Window *win, const char *path) return 0; } +int +texture_load(Texture *this, Window *win, const char *path) +{ + SDL_RWops *const rw = SDL_RWFromFile(path, "rb"); + if (rw == NULL) { + log_error("%s", SDL_GetError()); + return -1; + } + + return texture_load_rw(this, win, rw); +} + void texture_deinit(Texture *this) { diff --git a/src/texture.h b/src/texture.h index 1fcdd69..0e46de7 100644 --- a/src/texture.h +++ b/src/texture.h @@ -20,6 +20,10 @@ int texture_init_target(Texture *this, Window *win, int w, int h); /* return -1 on error */ [[nodiscard]] +int texture_load_rw(Texture *this, Window *win, SDL_RWops *rw); + +/* return -1 on error */ +[[nodiscard]] int texture_load(Texture *this, Window *win, const char *path); void texture_deinit(Texture *this); |