tileset and tile draw

This commit is contained in:
KikooDX 2022-03-01 01:54:20 +01:00
parent d949941fc9
commit 2a4407adc4
4 changed files with 103 additions and 17 deletions

View File

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

109
inc/lzy.h
View File

@ -25,11 +25,16 @@
#ifdef __cplusplus
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, 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 <SDL.h>
#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 <SDL.h>
#endif
#ifndef LZY_SDL_IMAGE_INCLUDE
#define LZY_SDL_IMAGE_INCLUDE <SDL_image.h>
#endif
#ifdef FXCG50
#include <gint/cpu.h>
@ -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 <SDL.h>
#include LZY_SDL_INCLUDE
#include LZY_SDL_IMAGE_INCLUDE
#include <stdint.h>
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;

BIN
res/tset.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -1,6 +1,6 @@
#define LZY_IMPLEMENTATION
#define LZY_GINT_TILESET bimg_tset
#include "lzy.h"
#include <stdint.h>
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());
}