228 lines
4.0 KiB
C
228 lines
4.0 KiB
C
#ifndef LZY_H_
|
|
#define LZY_H_
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
#include <stdint.h>
|
|
|
|
#define LZY_UNUSED(x) (void)(x)
|
|
|
|
int LZY_Init(const char *title);
|
|
void LZY_Quit(void);
|
|
int LZY_DrawBegin(void);
|
|
int LZY_DrawEnd(void);
|
|
void LZY_DrawSetColor(uint8_t r, uint8_t g, uint8_t b);
|
|
void LZY_DrawSetColorNone(void);
|
|
int LZY_DrawClear(void);
|
|
void LZY_CycleEvents(void);
|
|
int LZY_KeyDown(unsigned int scancode);
|
|
int LZY_ShouldQuit(void);
|
|
void LZY_Log(const char *msg);
|
|
const char *LZY_GetError(void);
|
|
|
|
#ifdef FXCG50
|
|
#include <gint/keycodes.h>
|
|
enum LZY_ScanCode {
|
|
LZY_SC_LEFT = KEY_LEFT,
|
|
LZY_SC_RIGHT = KEY_RIGHT,
|
|
LZY_SC_UP = KEY_UP,
|
|
LZY_SC_DOWN = KEY_DOWN,
|
|
LZY_SC_QUIT = KEY_EXIT,
|
|
};
|
|
#else /* end FXCG50, begin SDL2 */
|
|
#include <SDL.h>
|
|
enum LZY_ScanCode {
|
|
LZY_SC_LEFT = SDL_SCANCODE_LEFT,
|
|
LZY_SC_RIGHT = SDL_SCANCODE_RIGHT,
|
|
LZY_SC_UP = SDL_SCANCODE_UP,
|
|
LZY_SC_DOWN = SDL_SCANCODE_DOWN,
|
|
LZY_SC_QUIT = SDL_SCANCODE_ESCAPE,
|
|
};
|
|
#endif /* SDL2 */
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
#endif /* LZY_H_ */
|
|
|
|
/* implementation */
|
|
#ifdef LZY_IMPLEMENTATION
|
|
#ifndef LZY_TILE_SIZE
|
|
#define LZY_TILE_SIZE 16
|
|
#endif
|
|
#ifndef LZY_DISPLAY_WIDTH
|
|
#define LZY_DISPLAY_WIDTH 400
|
|
#endif
|
|
#ifndef LZY_DISPLAY_HEIGHT
|
|
#define LZY_DISPLAY_HEIGHT 224
|
|
#endif
|
|
|
|
#ifdef FXCG50
|
|
#include <gint/display.h>
|
|
#include <gint/keyboard.h>
|
|
#include <stdint.h>
|
|
|
|
static color_t draw_color = C_BLACK;
|
|
static int should_quit = 0;
|
|
|
|
int LZY_Init(const char *title) {
|
|
LZY_UNUSED(title);
|
|
return 0;
|
|
}
|
|
|
|
void LZY_Quit(void) {}
|
|
|
|
int LZY_DrawBegin(void) {
|
|
return 0;
|
|
}
|
|
|
|
int LZY_DrawEnd(void) {
|
|
dupdate();
|
|
return 0;
|
|
}
|
|
|
|
void LZY_DrawSetColor(uint8_t r, uint8_t g, uint8_t b) {
|
|
draw_color = C_RGB(r << 3, g << 3, b << 3);
|
|
}
|
|
|
|
void LZY_DrawSetColorNone(void) {
|
|
draw_color = C_NONE;
|
|
}
|
|
|
|
int LZY_DrawClear(void) {
|
|
dclear(draw_color);
|
|
return 0;
|
|
}
|
|
|
|
void LZY_CycleEvents(void) {
|
|
clearevents();
|
|
should_quit = should_quit || keydown(LZY_SC_QUIT);
|
|
}
|
|
|
|
int LZY_KeyDown(unsigned int scancode) {
|
|
return keydown(scancode);
|
|
}
|
|
|
|
int LZY_ShouldQuit(void) {
|
|
return should_quit;
|
|
}
|
|
|
|
void LZY_Log(const char *msg) {
|
|
LZY_UNUSED(msg);
|
|
}
|
|
|
|
const char *LZY_GetError(void) {
|
|
return NULL;
|
|
}
|
|
|
|
#else /* end FXCG50, begin SDL2 */
|
|
#include <SDL.h>
|
|
#include <stdint.h>
|
|
|
|
static const char *error = NULL;
|
|
static SDL_Window *window = NULL;
|
|
static SDL_Renderer *renderer = NULL;
|
|
static int should_quit = 0;
|
|
|
|
int LZY_Init(const char *title) {
|
|
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
|
error = SDL_GetError();
|
|
return 1;
|
|
}
|
|
|
|
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, 0);
|
|
|
|
window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED,
|
|
SDL_WINDOWPOS_UNDEFINED, LZY_DISPLAY_WIDTH,
|
|
LZY_DISPLAY_HEIGHT, SDL_WINDOW_SHOWN);
|
|
if (window == NULL) {
|
|
error = SDL_GetError();
|
|
return 2;
|
|
}
|
|
|
|
SDL_SetWindowMinimumSize(window, LZY_DISPLAY_WIDTH, LZY_DISPLAY_HEIGHT);
|
|
|
|
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
|
if (renderer == NULL) {
|
|
error = SDL_GetError();
|
|
return 3;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void LZY_Quit(void) {
|
|
if (renderer != NULL) {
|
|
SDL_DestroyRenderer(renderer);
|
|
renderer = NULL;
|
|
}
|
|
|
|
if (window != NULL) {
|
|
SDL_DestroyWindow(window);
|
|
window = NULL;
|
|
}
|
|
|
|
SDL_Quit();
|
|
}
|
|
|
|
int LZY_DrawBegin(void) {
|
|
return 0;
|
|
}
|
|
|
|
int LZY_DrawEnd(void) {
|
|
SDL_RenderPresent(renderer);
|
|
return 0;
|
|
}
|
|
|
|
void LZY_DrawSetColor(uint8_t r, uint8_t g, uint8_t b) {
|
|
SDL_SetRenderDrawColor(renderer, r, g, b, 0xff);
|
|
}
|
|
|
|
void LZY_DrawSetColorNone(void) {
|
|
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
|
|
}
|
|
|
|
int LZY_DrawClear(void) {
|
|
if (SDL_RenderClear(renderer) < 0) {
|
|
error = SDL_GetError();
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void LZY_CycleEvents(void) {
|
|
SDL_Event e;
|
|
|
|
while (SDL_PollEvent(&e) != 0) {
|
|
switch (e.type) {
|
|
case SDL_QUIT:
|
|
should_quit = 1;
|
|
break;
|
|
case SDL_KEYDOWN:
|
|
break;
|
|
case SDL_KEYUP:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
int LZY_KeyDown(unsigned int scancode) {
|
|
LZY_Log("LZY_KeyDown not implemented");
|
|
return 0;
|
|
}
|
|
|
|
int LZY_ShouldQuit(void) {
|
|
return should_quit;
|
|
}
|
|
|
|
void LZY_Log(const char *msg) {
|
|
SDL_Log(msg);
|
|
}
|
|
|
|
const char *LZY_GetError(void) {
|
|
return error;
|
|
}
|
|
#endif /* SDL2 */
|
|
#endif /* LZY_IMPLEMENTATION */
|