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
168
src/pxl8.c
168
src/pxl8.c
|
|
@ -14,19 +14,66 @@
|
|||
#include "pxl8_hal.h"
|
||||
#include "pxl8_macros.h"
|
||||
#include "pxl8_script.h"
|
||||
#include "pxl8_sys.h"
|
||||
#include "pxl8_types.h"
|
||||
#include "pxl8_ui.h"
|
||||
|
||||
pxl8_game_result pxl8_init(pxl8_game* game, i32 argc, char* argv[]) {
|
||||
if (!game) {
|
||||
return PXL8_GAME_FAILURE;
|
||||
struct pxl8 {
|
||||
pxl8_cart* cart;
|
||||
pxl8_game* game;
|
||||
const pxl8_hal* hal;
|
||||
void* platform_data;
|
||||
};
|
||||
|
||||
pxl8* pxl8_create(const pxl8_hal* hal) {
|
||||
if (!hal) {
|
||||
pxl8_error("HAL cannot be NULL");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!game->hal) {
|
||||
pxl8_error("HAL must be set before calling pxl8_init");
|
||||
return PXL8_GAME_FAILURE;
|
||||
pxl8* sys = (pxl8*)calloc(1, sizeof(pxl8));
|
||||
if (!sys) {
|
||||
pxl8_error("Failed to allocate pxl8 system");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
sys->hal = hal;
|
||||
|
||||
sys->game = (pxl8_game*)calloc(1, sizeof(pxl8_game));
|
||||
if (!sys->game) {
|
||||
pxl8_error("Failed to allocate game");
|
||||
free(sys);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return sys;
|
||||
}
|
||||
|
||||
void pxl8_destroy(pxl8* sys) {
|
||||
if (!sys) return;
|
||||
|
||||
if (sys->game) {
|
||||
free(sys->game);
|
||||
}
|
||||
|
||||
if (sys->cart) {
|
||||
pxl8_cart_destroy(sys->cart);
|
||||
}
|
||||
|
||||
if (sys->hal && sys->platform_data) {
|
||||
sys->hal->destroy(sys->platform_data);
|
||||
}
|
||||
|
||||
free(sys);
|
||||
}
|
||||
|
||||
pxl8_result pxl8_init(pxl8* sys, i32 argc, char* argv[]) {
|
||||
if (!sys || !sys->game) {
|
||||
return PXL8_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
pxl8_game* game = sys->game;
|
||||
|
||||
game->color_mode = PXL8_COLOR_MODE_MEGA;
|
||||
game->resolution = PXL8_RESOLUTION_640x360;
|
||||
|
||||
|
|
@ -45,7 +92,7 @@ pxl8_game_result pxl8_init(pxl8_game* game, i32 argc, char* argv[]) {
|
|||
pack_output = argv[++i];
|
||||
} else {
|
||||
pxl8_error("--pack requires <folder> <output.pxc>");
|
||||
return PXL8_GAME_FAILURE;
|
||||
return PXL8_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
} else if (!script_arg) {
|
||||
script_arg = argv[i];
|
||||
|
|
@ -54,39 +101,42 @@ pxl8_game_result pxl8_init(pxl8_game* game, i32 argc, char* argv[]) {
|
|||
|
||||
if (pack_mode) {
|
||||
pxl8_result result = pxl8_cart_pack(pack_input, pack_output);
|
||||
return (result == PXL8_OK) ? PXL8_GAME_SUCCESS : PXL8_GAME_FAILURE;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (game->repl_mode) {
|
||||
fprintf(stderr, "\033[38;2;184;187;38m[pxl8]\033[0m Starting in REPL mode with script: %s\n", game->script_path);
|
||||
}
|
||||
|
||||
pxl8_info("Starting up");
|
||||
|
||||
game->gfx = pxl8_gfx_create(game->hal, game->color_mode, game->resolution, "pxl8", 1280, 720);
|
||||
sys->platform_data = sys->hal->create(game->color_mode, game->resolution, "pxl8", 1280, 720);
|
||||
if (!sys->platform_data) {
|
||||
pxl8_error("Failed to create platform context");
|
||||
return PXL8_ERROR_INITIALIZATION_FAILED;
|
||||
}
|
||||
|
||||
game->gfx = pxl8_gfx_create(sys->hal, sys->platform_data, game->color_mode, game->resolution);
|
||||
if (!game->gfx) {
|
||||
pxl8_error("Failed to create graphics context");
|
||||
return PXL8_GAME_FAILURE;
|
||||
return PXL8_ERROR_INITIALIZATION_FAILED;
|
||||
}
|
||||
|
||||
if (pxl8_gfx_load_font_atlas(game->gfx) != PXL8_OK) {
|
||||
pxl8_error("Failed to load font atlas");
|
||||
return PXL8_GAME_FAILURE;
|
||||
return PXL8_ERROR_INITIALIZATION_FAILED;
|
||||
}
|
||||
|
||||
game->ui = pxl8_ui_create(game->gfx);
|
||||
if (!game->ui) {
|
||||
pxl8_error("Failed to create UI");
|
||||
pxl8_gfx_destroy(game->gfx);
|
||||
return PXL8_GAME_FAILURE;
|
||||
return PXL8_ERROR_INITIALIZATION_FAILED;
|
||||
}
|
||||
|
||||
game->script = pxl8_script_create();
|
||||
if (!game->script) {
|
||||
pxl8_error("Failed to initialize scripting: %s", pxl8_script_get_last_error(game->script));
|
||||
pxl8_gfx_destroy(game->gfx);
|
||||
return PXL8_GAME_FAILURE;
|
||||
return PXL8_ERROR_INITIALIZATION_FAILED;
|
||||
}
|
||||
|
||||
const char* cart_path = script_arg ? script_arg : "demo";
|
||||
|
|
@ -97,19 +147,19 @@ pxl8_game_result pxl8_init(pxl8_game* game, i32 argc, char* argv[]) {
|
|||
|
||||
if (is_cart) {
|
||||
char* original_cwd = getcwd(NULL, 0);
|
||||
game->cart = pxl8_cart_create();
|
||||
if (!game->cart) {
|
||||
sys->cart = pxl8_cart_create();
|
||||
if (!sys->cart) {
|
||||
pxl8_error("Failed to create cart");
|
||||
return PXL8_GAME_FAILURE;
|
||||
return PXL8_ERROR_INITIALIZATION_FAILED;
|
||||
}
|
||||
if (pxl8_cart_load(game->cart, cart_path) == PXL8_OK) {
|
||||
pxl8_script_set_cart_path(game->script, pxl8_cart_get_base_path(game->cart), original_cwd);
|
||||
pxl8_cart_mount(game->cart);
|
||||
if (pxl8_cart_load(sys->cart, cart_path) == PXL8_OK) {
|
||||
pxl8_script_set_cart_path(game->script, pxl8_cart_get_base_path(sys->cart), original_cwd);
|
||||
pxl8_cart_mount(sys->cart);
|
||||
strcpy(game->script_path, "main.fnl");
|
||||
pxl8_info("Loaded cart: %s", pxl8_cart_get_name(game->cart));
|
||||
pxl8_info("Loaded cart: %s", pxl8_cart_get_name(sys->cart));
|
||||
} else {
|
||||
pxl8_error("Failed to load cart: %s", cart_path);
|
||||
return PXL8_GAME_FAILURE;
|
||||
return PXL8_ERROR_INITIALIZATION_FAILED;
|
||||
}
|
||||
free(original_cwd);
|
||||
} else if (script_arg) {
|
||||
|
|
@ -119,8 +169,8 @@ pxl8_game_result pxl8_init(pxl8_game* game, i32 argc, char* argv[]) {
|
|||
|
||||
pxl8_script_set_gfx(game->script, game->gfx);
|
||||
pxl8_script_set_input(game->script, &game->input);
|
||||
pxl8_script_set_sys(game->script, sys);
|
||||
pxl8_script_set_ui(game->script, game->ui);
|
||||
pxl8_script_set_game(game->script, game);
|
||||
|
||||
if (game->script_path[0] != '\0') {
|
||||
pxl8_result result = pxl8_script_load_main(game->script, game->script_path);
|
||||
|
|
@ -131,19 +181,20 @@ pxl8_game_result pxl8_init(pxl8_game* game, i32 argc, char* argv[]) {
|
|||
}
|
||||
}
|
||||
|
||||
game->last_time = game->hal->get_ticks();
|
||||
game->last_time = sys->hal->get_ticks();
|
||||
game->running = true;
|
||||
|
||||
|
||||
return PXL8_GAME_CONTINUE;
|
||||
return PXL8_OK;
|
||||
}
|
||||
|
||||
pxl8_game_result pxl8_update(pxl8_game* game) {
|
||||
if (!game) {
|
||||
return PXL8_GAME_FAILURE;
|
||||
pxl8_result pxl8_update(pxl8* sys) {
|
||||
if (!sys || !sys->game) {
|
||||
return PXL8_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
u64 current_time = game->hal->get_ticks();
|
||||
pxl8_game* game = sys->game;
|
||||
|
||||
u64 current_time = sys->hal->get_ticks();
|
||||
f32 dt = (f32)(current_time - game->last_time) / 1000000000.0f;
|
||||
|
||||
game->last_time = current_time;
|
||||
|
|
@ -236,14 +287,15 @@ pxl8_game_result pxl8_update(pxl8_game* game) {
|
|||
pxl8_script_call_function_f32(game->script, "update", dt);
|
||||
}
|
||||
|
||||
return PXL8_GAME_CONTINUE;
|
||||
return PXL8_OK;
|
||||
}
|
||||
|
||||
pxl8_game_result pxl8_frame(pxl8_game* game) {
|
||||
if (!game) {
|
||||
return PXL8_GAME_FAILURE;
|
||||
pxl8_result pxl8_frame(pxl8* sys) {
|
||||
if (!sys || !sys->game) {
|
||||
return PXL8_ERROR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
pxl8_game* game = sys->game;
|
||||
pxl8_bounds bounds = pxl8_gfx_get_bounds(game->gfx);
|
||||
|
||||
if (game->script_loaded) {
|
||||
|
|
@ -284,11 +336,15 @@ pxl8_game_result pxl8_frame(pxl8_game* game) {
|
|||
game->input.mouse_wheel_x = 0;
|
||||
game->input.mouse_wheel_y = 0;
|
||||
|
||||
return game->running ? PXL8_GAME_CONTINUE : PXL8_GAME_SUCCESS;
|
||||
game->frame_count++;
|
||||
|
||||
return PXL8_OK;
|
||||
}
|
||||
|
||||
void pxl8_quit(pxl8_game* game) {
|
||||
if (!game) return;
|
||||
void pxl8_quit(pxl8* sys) {
|
||||
if (!sys || !sys->game) return;
|
||||
|
||||
pxl8_game* game = sys->game;
|
||||
|
||||
if (game->repl_mode && game->repl) {
|
||||
fprintf(stderr, "\r\033[K");
|
||||
|
|
@ -299,10 +355,8 @@ void pxl8_quit(pxl8_game* game) {
|
|||
|
||||
pxl8_info("Shutting down");
|
||||
|
||||
if (game->cart) {
|
||||
pxl8_cart_unload(game->cart);
|
||||
free(game->cart);
|
||||
game->cart = NULL;
|
||||
if (sys->cart) {
|
||||
pxl8_cart_unmount(sys->cart);
|
||||
}
|
||||
|
||||
pxl8_gfx_destroy(game->gfx);
|
||||
|
|
@ -310,6 +364,28 @@ void pxl8_quit(pxl8_game* game) {
|
|||
if (game->ui) pxl8_ui_destroy(game->ui);
|
||||
}
|
||||
|
||||
f32 pxl8_game_get_fps(const pxl8_game* game) {
|
||||
return game ? game->fps : 0.0f;
|
||||
bool pxl8_is_running(const pxl8* sys) {
|
||||
return sys && sys->game && sys->game->running;
|
||||
}
|
||||
|
||||
void pxl8_set_running(pxl8* sys, bool running) {
|
||||
if (sys && sys->game) {
|
||||
sys->game->running = running;
|
||||
}
|
||||
}
|
||||
|
||||
f32 pxl8_get_fps(const pxl8* sys) {
|
||||
return (sys && sys->game) ? sys->game->fps : 0.0f;
|
||||
}
|
||||
|
||||
pxl8_gfx* pxl8_get_gfx(pxl8* sys) {
|
||||
return (sys && sys->game) ? sys->game->gfx : NULL;
|
||||
}
|
||||
|
||||
pxl8_input_state* pxl8_get_input(pxl8* sys) {
|
||||
return (sys && sys->game) ? &sys->game->input : NULL;
|
||||
}
|
||||
|
||||
pxl8_resolution pxl8_get_resolution(pxl8* sys) {
|
||||
return (sys && sys->game) ? sys->game->resolution : PXL8_RESOLUTION_640x360;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue