This commit is contained in:
kdx 2023-03-25 21:33:02 +01:00
parent bd2f4968b1
commit 75e92881f1
4 changed files with 67 additions and 66 deletions

View File

@ -19,7 +19,7 @@ set(ASSETS
fxconv_declare_assets(${ASSETS} WITH_METADATA)
add_executable(thyaddin ${SOURCES} ${ASSETS})
target_compile_options(thyaddin PRIVATE -Wall -Wextra -Os)
target_compile_options(thyaddin PRIVATE -Wall -Wextra -Os -std=c2x)
target_link_libraries(thyaddin Gint::Gint)
generate_g3a(TARGET thyaddin OUTPUT "lzy.g3a"

View File

@ -1,5 +1,5 @@
CC ?= gcc
CFLAGS = -std=c99 -Wall -Wextra -pedantic -O3 -I./inc -MMD $(shell sdl2-config --cflags)
CFLAGS = -std=c2x -Wall -Wextra -pedantic -O3 -I./inc -MMD $(shell sdl2-config --cflags)
LDFLAGS = -lSDL2 -lSDL2_image -lSDL2_mixer $(shell sdl2-config --libs)
OBJ_NAME = lzy

4
compile_flags.txt Normal file
View File

@ -0,0 +1,4 @@
-Wall
-Wextra
-std=c2x
-Iinc

125
inc/lzy.h
View File

@ -28,8 +28,6 @@ extern "C" {
#include <stdint.h>
#define LZY_UNUSED(x) (void)(x)
#ifdef FXCG50
#include <gint/keycodes.h>
typedef void LZY_Music;
@ -80,38 +78,42 @@ typedef struct LZY_Event {
LZY_EventUnion u;
} LZY_Event;
int LZY_Init(const char *title, int target_fps, const char *tset_path,
const char *font_path);
[[nodiscard]] 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);
[[nodiscard]] int LZY_DrawBegin(void);
[[nodiscard]] int LZY_DrawEnd(void);
void LZY_DrawSetColor(uint8_t r, uint8_t g, uint8_t b);
void LZY_DrawSetColorNone(void);
int LZY_DrawClear(void);
int LZY_DrawPoint(int x, int y);
int LZY_DrawLine(int x0, int y0, int x1, int y1);
int LZY_DrawRect(int x, int y, int w, int h);
int LZY_DrawFillRect(int x, int y, int w, int h);
int LZY_DrawTile(unsigned int id, int x, int y);
int LZY_DrawTileEx(unsigned int id, int x, int y, unsigned int w,
unsigned int h);
int LZY_DrawChar(unsigned char chr, int x, int y);
int LZY_DrawText(int x, int y, const char *text);
int LZY_DrawTextF(int x, int y, const char *fmt, ...);
LZY_Music *LZY_MusicLoad(const char *path);
int LZY_MusicDestroy(LZY_Music *music);
int LZY_MusicPlay(LZY_Music *music, int loops);
LZY_Sound *LZY_SoundLoad(const char *path);
int LZY_SoundDestroy(LZY_Sound *sound);
[[nodiscard]] int LZY_DrawClear(void);
[[nodiscard]] int LZY_DrawPoint(int x, int y);
[[nodiscard]] int LZY_DrawLine(int x0, int y0, int x1, int y1);
[[nodiscard]] int LZY_DrawRect(int x, int y, int w, int h);
[[nodiscard]] int LZY_FillRect(int x, int y, int w, int h);
[[nodiscard]] int LZY_DrawTile(unsigned int id, int x, int y);
[[nodiscard]] int LZY_DrawTileEx(unsigned int id, int x, int y, unsigned int w,
unsigned int h);
[[nodiscard]] int LZY_DrawChar(unsigned char chr, int x, int y);
[[nodiscard]] int LZY_DrawText(int x, int y, const char *text);
[[nodiscard]] int LZY_DrawTextF(int x, int y, const char *fmt, ...);
[[nodiscard]] LZY_Music *LZY_MusicLoad(const char *path);
[[nodiscard]] int LZY_MusicDestroy(LZY_Music *music);
[[nodiscard]] int LZY_MusicPlay(LZY_Music *music, int loops);
[[nodiscard]] LZY_Sound *LZY_SoundLoad(const char *path);
[[nodiscard]] int LZY_SoundDestroy(LZY_Sound *sound);
void LZY_SoundSetVolume(LZY_Sound *sound, float volume);
int LZY_SoundPlay(LZY_Sound *sound, int loops);
[[nodiscard]] int LZY_SoundPlay(LZY_Sound *sound, int loops);
int LZY_PollEvent(LZY_Event *);
void LZY_CycleEvents(void);
int LZY_KeyDown(unsigned int key);
int LZY_ShouldQuit(void);
void LZY_Log(const char *fmt, ...);
int LZY_LogError(int rv);
const char *LZY_GetError(void);
[[deprecated("renamed to LZY_FillRect")]] [[nodiscard]] int
LZY_DrawFillRect(int x, int y, int w, int h);
#ifdef __cplusplus
}
#endif
@ -193,6 +195,16 @@ int LZY_DrawTextF(int x, int y, const char *fmt, ...) {
return LZY_DrawText(x, y, buf);
}
int LZY_LogError(int rv) {
LZY_Log("%s", LZY_GetError());
return rv;
}
int LZY_DrawFillRect(int x, int y, int w, int h) {
return LZY_FillRect(x, y, w, h);
}
#ifdef FXCG50
#include <gint/cpu.h>
#include <gint/display.h>
@ -214,15 +226,12 @@ static int _LZY_Sign(int n) {
return (n > 0) - (n < 0);
}
int LZY_Init(const char *title, int target_fps, const char *tset_path,
const char *font_path) {
int LZY_Init([[maybe_unused]] const char *title, int target_fps,
[[maybe_unused]] const char *tset_path,
[[maybe_unused]] const char *font_path) {
extern bopti_image_t LZY_GINT_TILESET;
extern bopti_image_t LZY_GINT_FONT;
LZY_UNUSED(title);
LZY_UNUSED(tset_path);
LZY_UNUSED(font_path);
tset_width = LZY_GINT_TILESET.width / LZY_TILE_SIZE;
tset_height = LZY_GINT_TILESET.height / LZY_TILE_SIZE;
@ -291,7 +300,7 @@ int LZY_DrawRect(int x, int y, int w, int h) {
return 0;
}
int LZY_DrawFillRect(int x, int y, int w, int h) {
int LZY_FillRect(int x, int y, int w, int h) {
w += w == 0;
h += h == 0;
x += draw_off_x;
@ -378,40 +387,32 @@ int LZY_DrawText(int x, int y, const char *text) {
return err;
}
LZY_Music *LZY_MusicLoad(const char *path) {
LZY_UNUSED(path);
LZY_Music *LZY_MusicLoad([[maybe_unused]] const char *path) {
return NULL;
}
int LZY_MusicDestroy(LZY_Music *music) {
LZY_UNUSED(music);
int LZY_MusicDestroy([[maybe_unused]] LZY_Music *music) {
return -1;
}
int LZY_MusicPlay(LZY_Music *music, int loops) {
LZY_UNUSED(music);
LZY_UNUSED(loops);
int LZY_MusicPlay([[maybe_unused]] LZY_Music *music,
[[maybe_unused]] int loops) {
return -1;
}
LZY_Sound *LZY_SoundLoad(const char *path) {
LZY_UNUSED(path);
LZY_Sound *LZY_SoundLoad([[maybe_unused]] const char *path) {
return NULL;
}
int LZY_SoundDestroy(LZY_Sound *sound) {
LZY_UNUSED(sound);
int LZY_SoundDestroy([[maybe_unused]] LZY_Sound *sound) {
return -1;
}
void LZY_SoundSetVolume(LZY_Sound *sound, float volume) {
LZY_UNUSED(sound);
LZY_UNUSED(volume);
}
void LZY_SoundSetVolume([[maybe_unused]] LZY_Sound *sound,
[[maybe_unused]] float volume) {}
int LZY_SoundPlay(LZY_Sound *sound, int loops) {
LZY_UNUSED(sound);
LZY_UNUSED(loops);
int LZY_SoundPlay([[maybe_unused]] LZY_Sound *sound,
[[maybe_unused]] int loops) {
return -1;
}
@ -451,9 +452,7 @@ int LZY_ShouldQuit(void) {
return should_quit;
}
void LZY_Log(const char *fmt, ...) {
LZY_UNUSED(fmt);
}
void LZY_Log([[maybe_unused]] const char *fmt, ...) {}
const char *LZY_GetError(void) {
return NULL;
@ -469,12 +468,9 @@ const char *LZY_GetError(void) {
#include <string.h>
static const SDL_Scancode sc[LZYK_COUNT * 2] = {
SDL_SCANCODE_LEFT, SDL_SCANCODE_A,
SDL_SCANCODE_RIGHT, SDL_SCANCODE_D,
SDL_SCANCODE_UP, SDL_SCANCODE_W,
SDL_SCANCODE_DOWN, SDL_SCANCODE_S,
SDL_SCANCODE_Z, SDL_SCANCODE_J,
SDL_SCANCODE_X, SDL_SCANCODE_K,
SDL_SCANCODE_LEFT, SDL_SCANCODE_A, SDL_SCANCODE_RIGHT, SDL_SCANCODE_D,
SDL_SCANCODE_UP, SDL_SCANCODE_W, SDL_SCANCODE_DOWN, SDL_SCANCODE_S,
SDL_SCANCODE_Z, SDL_SCANCODE_J, SDL_SCANCODE_X, SDL_SCANCODE_K,
};
static const SDL_Scancode fullscreen_sc = SDL_SCANCODE_F11;
static const char *error = NULL;
@ -737,7 +733,7 @@ int LZY_DrawRect(int x, int y, int w, int h) {
return 0;
}
int LZY_DrawFillRect(int x, int y, int w, int h) {
int LZY_FillRect(int x, int y, int w, int h) {
const SDL_Rect rect = {x, y, w + (w == 0), h + (h == 0)};
if (SDL_RenderFillRect(renderer, &rect) < 0) {
@ -955,10 +951,10 @@ int LZY_PollEvent(LZY_Event *e) {
if (!sdl_e.key.repeat) {
int i = LZYK_COUNT;
while (i-- > 0) {
if (sc[i*2] ==
sdl_e.key.keysym.scancode ||
sc[i*2 + 1] ==
sdl_e.key.keysym.scancode) {
if (sc[i * 2] ==
sdl_e.key.keysym.scancode ||
sc[i * 2 + 1] ==
sdl_e.key.keysym.scancode) {
e->type = LZY_KEYDOWN;
e->u.key.scancode = i;
input[i] = 1;
@ -979,8 +975,9 @@ int LZY_PollEvent(LZY_Event *e) {
case SDL_KEYUP: {
int i = LZYK_COUNT;
while (i-- > 0) {
if (sc[i*2] == sdl_e.key.keysym.scancode ||
sc[i*2 + 1] == sdl_e.key.keysym.scancode) {
if (sc[i * 2] == sdl_e.key.keysym.scancode ||
sc[i * 2 + 1] ==
sdl_e.key.keysym.scancode) {
e->type = LZY_KEYUP;
e->u.key.scancode = i;
input[i] = 0;