font char draw

This commit is contained in:
KikooDX 2022-03-01 15:13:35 +01:00
parent 2a4407adc4
commit 7054e47730
5 changed files with 110 additions and 17 deletions

View File

@ -13,6 +13,7 @@ set(SOURCES
set(ASSETS
res/tset.png
res/font.png
)
fxconv_declare_assets(${ASSETS} WITH_METADATA)

117
inc/lzy.h
View File

@ -26,15 +26,12 @@
extern "C" {
#endif
#ifndef LZY_SDL_INCLUDE
#define LZY_SDL_INCLUDE <SDL.h>
#endif
#include <stdint.h>
#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 <SDL.h>
#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 <SDL.h>
#endif
#ifndef LZY_SDL_IMAGE_INCLUDE
#define LZY_SDL_IMAGE_INCLUDE <SDL_image.h>
#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 <SDL.h>
#ifndef LZY_TILE_SIZE
#define LZY_TILE_SIZE 16
#endif
#ifndef LZY_SDL_IMAGE_INCLUDE
#define LZY_SDL_IMAGE_INCLUDE <SDL_image.h>
#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,

BIN
res/font.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -1,3 +1,6 @@
tset.png:
type: bopti-image
name: bimg_tset
font.png:
type: bopti-image
name: bimg_font

View File

@ -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;