refactor a bit into pxl8 sys struct
This commit is contained in:
parent
b2682a2d40
commit
f19b06d705
10 changed files with 217 additions and 148 deletions
104
src/pxl8_sdl3.c
104
src/pxl8_sdl3.c
|
|
@ -5,8 +5,8 @@
|
|||
#include <SDL3/SDL_main.h>
|
||||
|
||||
#include "pxl8_atlas.h"
|
||||
#include "pxl8_game.h"
|
||||
#include "pxl8_macros.h"
|
||||
#include "pxl8_sys.h"
|
||||
|
||||
typedef struct pxl8_sdl3_context {
|
||||
SDL_Texture* atlas_texture;
|
||||
|
|
@ -236,75 +236,73 @@ SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) {
|
|||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
pxl8_game* game = (pxl8_game*)SDL_calloc(1, sizeof(pxl8_game));
|
||||
if (!game) {
|
||||
pxl8_error("Failed to allocate game instance");
|
||||
pxl8* sys = pxl8_create(&pxl8_hal_sdl3);
|
||||
if (!sys) {
|
||||
pxl8_error("Failed to create pxl8 system");
|
||||
SDL_Quit();
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
game->hal = &pxl8_hal_sdl3;
|
||||
|
||||
pxl8_game_result result = pxl8_init(game, argc, argv);
|
||||
if (result != PXL8_GAME_CONTINUE) {
|
||||
SDL_free(game);
|
||||
pxl8_result result = pxl8_init(sys, argc, argv);
|
||||
if (result != PXL8_OK) {
|
||||
pxl8_destroy(sys);
|
||||
SDL_Quit();
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
*appstate = game;
|
||||
*appstate = sys;
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
SDL_AppResult SDL_AppIterate(void* appstate) {
|
||||
pxl8_game* game = (pxl8_game*)appstate;
|
||||
|
||||
if (!game) {
|
||||
pxl8* sys = (pxl8*)appstate;
|
||||
if (!sys) {
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
pxl8_game_result update_result = pxl8_update(game);
|
||||
if (update_result == PXL8_GAME_FAILURE) {
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
if (update_result == PXL8_GAME_SUCCESS) {
|
||||
return SDL_APP_SUCCESS;
|
||||
}
|
||||
|
||||
pxl8_game_result frame_result = pxl8_frame(game);
|
||||
if (frame_result == PXL8_GAME_FAILURE) {
|
||||
pxl8_result update_result = pxl8_update(sys);
|
||||
if (update_result != PXL8_OK) {
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
return (frame_result == PXL8_GAME_SUCCESS) ? SDL_APP_SUCCESS : SDL_APP_CONTINUE;
|
||||
pxl8_result frame_result = pxl8_frame(sys);
|
||||
if (frame_result != PXL8_OK) {
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
return pxl8_is_running(sys) ? SDL_APP_CONTINUE : SDL_APP_SUCCESS;
|
||||
}
|
||||
|
||||
SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
|
||||
pxl8_game* game = (pxl8_game*)appstate;
|
||||
pxl8* sys = (pxl8*)appstate;
|
||||
if (!sys) {
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
if (!game) {
|
||||
pxl8_input_state* input = pxl8_get_input(sys);
|
||||
if (!input) {
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
switch (event->type) {
|
||||
case SDL_EVENT_QUIT:
|
||||
game->running = false;
|
||||
pxl8_set_running(sys, false);
|
||||
break;
|
||||
|
||||
case SDL_EVENT_KEY_DOWN: {
|
||||
#ifdef DEBUG
|
||||
if (event->key.key == SDLK_ESCAPE) {
|
||||
game->running = false;
|
||||
pxl8_set_running(sys, false);
|
||||
}
|
||||
#endif
|
||||
|
||||
SDL_Scancode scancode = event->key.scancode;
|
||||
if (scancode < 256) {
|
||||
if (!game->input.keys_down[scancode]) {
|
||||
game->input.keys_pressed[scancode] = true;
|
||||
if (!input->keys_down[scancode]) {
|
||||
input->keys_pressed[scancode] = true;
|
||||
}
|
||||
game->input.keys_down[scancode] = true;
|
||||
game->input.keys_released[scancode] = false;
|
||||
input->keys_down[scancode] = true;
|
||||
input->keys_released[scancode] = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
@ -312,9 +310,9 @@ SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
|
|||
case SDL_EVENT_KEY_UP: {
|
||||
SDL_Scancode scancode = event->key.scancode;
|
||||
if (scancode < 256) {
|
||||
game->input.keys_down[scancode] = false;
|
||||
game->input.keys_pressed[scancode] = false;
|
||||
game->input.keys_released[scancode] = true;
|
||||
input->keys_down[scancode] = false;
|
||||
input->keys_pressed[scancode] = false;
|
||||
input->keys_released[scancode] = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
@ -322,11 +320,11 @@ SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
|
|||
case SDL_EVENT_MOUSE_BUTTON_DOWN: {
|
||||
u8 button = event->button.button - 1;
|
||||
if (button < 3) {
|
||||
if (!game->input.mouse_buttons_down[button]) {
|
||||
game->input.mouse_buttons_pressed[button] = true;
|
||||
if (!input->mouse_buttons_down[button]) {
|
||||
input->mouse_buttons_pressed[button] = true;
|
||||
}
|
||||
game->input.mouse_buttons_down[button] = true;
|
||||
game->input.mouse_buttons_released[button] = false;
|
||||
input->mouse_buttons_down[button] = true;
|
||||
input->mouse_buttons_released[button] = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
@ -334,15 +332,16 @@ SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
|
|||
case SDL_EVENT_MOUSE_BUTTON_UP: {
|
||||
u8 button = event->button.button - 1;
|
||||
if (button < 3) {
|
||||
game->input.mouse_buttons_down[button] = false;
|
||||
game->input.mouse_buttons_pressed[button] = false;
|
||||
game->input.mouse_buttons_released[button] = true;
|
||||
input->mouse_buttons_down[button] = false;
|
||||
input->mouse_buttons_pressed[button] = false;
|
||||
input->mouse_buttons_released[button] = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_EVENT_MOUSE_MOTION: {
|
||||
if (!game->gfx) break;
|
||||
pxl8_gfx* gfx = pxl8_get_gfx(sys);
|
||||
if (!gfx) break;
|
||||
|
||||
i32 window_mouse_x = (i32)event->motion.x;
|
||||
i32 window_mouse_y = (i32)event->motion.y;
|
||||
|
|
@ -354,19 +353,20 @@ SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
|
|||
SDL_GetWindowSize(window, &window_width, &window_height);
|
||||
|
||||
i32 render_width, render_height;
|
||||
pxl8_gfx_get_resolution_dimensions(game->resolution, &render_width, &render_height);
|
||||
pxl8_resolution resolution = pxl8_get_resolution(sys);
|
||||
pxl8_gfx_get_resolution_dimensions(resolution, &render_width, &render_height);
|
||||
|
||||
pxl8_bounds window_bounds = {0, 0, window_width, window_height};
|
||||
pxl8_viewport vp = pxl8_gfx_viewport(window_bounds, render_width, render_height);
|
||||
|
||||
game->input.mouse_x = (i32)((window_mouse_x - vp.offset_x) / vp.scale);
|
||||
game->input.mouse_y = (i32)((window_mouse_y - vp.offset_y) / vp.scale);
|
||||
input->mouse_x = (i32)((window_mouse_x - vp.offset_x) / vp.scale);
|
||||
input->mouse_y = (i32)((window_mouse_y - vp.offset_y) / vp.scale);
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_EVENT_MOUSE_WHEEL: {
|
||||
game->input.mouse_wheel_x = (i32)event->wheel.x;
|
||||
game->input.mouse_wheel_y = (i32)event->wheel.y;
|
||||
input->mouse_wheel_x = (i32)event->wheel.x;
|
||||
input->mouse_wheel_y = (i32)event->wheel.y;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -377,10 +377,10 @@ SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) {
|
|||
void SDL_AppQuit(void* appstate, SDL_AppResult result) {
|
||||
(void)result;
|
||||
|
||||
pxl8_game* game = (pxl8_game*)appstate;
|
||||
if (game) {
|
||||
pxl8_quit(game);
|
||||
SDL_free(game);
|
||||
pxl8* sys = (pxl8*)appstate;
|
||||
if (sys) {
|
||||
pxl8_quit(sys);
|
||||
pxl8_destroy(sys);
|
||||
}
|
||||
|
||||
SDL_Quit();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue