From bba12b9b3935c5fd01e01c13d4efa2ae43ebb6ee Mon Sep 17 00:00:00 2001 From: En Yi Date: Mon, 14 Aug 2023 21:54:25 +0800 Subject: [PATCH] Add Sandbox button in main program Changelog: - First button now loads the levelpack --- main.c | 18 ++++++++++-------- scenes/editor_scene.c | 2 +- scenes/engine/EC/mempool.c | 12 ++++++------ scenes/game_scene.c | 8 +++++++- scenes/menu_scene.c | 16 ++++++++++++---- scenes/scene_impl.h | 2 +- 6 files changed, 37 insertions(+), 21 deletions(-) diff --git a/main.c b/main.c index 4aba3ab..5b7dd8c 100644 --- a/main.c +++ b/main.c @@ -4,12 +4,12 @@ #include "ent_impl.h" #include "mempool.h" #include -#define N_SCENES 3 +#define N_SCENES 4 Scene_t *scenes[N_SCENES]; static GameEngine_t engine = { .scenes = scenes, - .max_scenes = 2, + .max_scenes = 3, .curr_scene = 0, .assets = {0} }; @@ -33,9 +33,13 @@ int main(void) load_from_infofile("res/assets.info", &engine.assets); init_player_creation("res/player_spr.info", &engine.assets); + LevelScene_t sandbox_scene; + sandbox_scene.scene.engine = &engine; + init_sandbox_scene(&sandbox_scene); + LevelScene_t level_scene; level_scene.scene.engine = &engine; - init_sandbox_scene(&level_scene); + init_game_scene(&level_scene); LevelPack_t* pack = get_level_pack(&engine.assets, "TestLevels"); if (pack != NULL) { @@ -49,12 +53,9 @@ int main(void) init_menu_scene(&menu_scene); scenes[0] = &menu_scene.scene; scenes[1] = &level_scene.scene; + scenes[2] = &sandbox_scene.scene; change_scene(&engine, 0); - //Camera2D camera = { 0 }; - //camera.offset = (Vector2){0,0}; - //camera.rotation = 0.0f; - //camera.zoom = 1.0f; while (!WindowShouldClose()) { // This entire key processing relies on the assumption that a pressed key will @@ -99,7 +100,8 @@ int main(void) sc_queue_clear(&key_buffer); } } - free_sandbox_scene(&level_scene); + free_sandbox_scene(&sandbox_scene); + free_game_scene(&level_scene); free_menu_scene(&menu_scene); sc_queue_term(&key_buffer); term_assets(&engine.assets); diff --git a/scenes/editor_scene.c b/scenes/editor_scene.c index 41c07e6..abba6cf 100644 --- a/scenes/editor_scene.c +++ b/scenes/editor_scene.c @@ -719,7 +719,7 @@ static void restart_editor_level(Scene_t* scene) } } -void level_do_action(Scene_t* scene, ActionType_t action, bool pressed) +static void level_do_action(Scene_t* scene, ActionType_t action, bool pressed) { CPlayerState_t* p_playerstate; sc_map_foreach_value(&scene->ent_manager.component_map[CPLAYERSTATE_T], p_playerstate) diff --git a/scenes/engine/EC/mempool.c b/scenes/engine/EC/mempool.c index 085cd41..b4faaa1 100644 --- a/scenes/engine/EC/mempool.c +++ b/scenes/engine/EC/mempool.c @@ -11,15 +11,15 @@ static CBBox_t bbox_buffer[MAX_COMP_POOL_SIZE]; static CTransform_t ctransform_buffer[MAX_COMP_POOL_SIZE]; static CTileCoord_t ctilecoord_buffer[MAX_COMP_POOL_SIZE]; static CMovementState_t cmstate_buffer[MAX_COMP_POOL_SIZE]; -static CJump_t cjump_buffer[1]; // Only player is expected to have this -static CPlayerState_t cplayerstate_buffer[1]; // Only player is expected to have this +static CJump_t cjump_buffer[8]; // Only player is expected to have this +static CPlayerState_t cplayerstate_buffer[8]; // Only player is expected to have this static CContainer_t ccontainer_buffer[MAX_COMP_POOL_SIZE]; static CHitBoxes_t chitboxes_buffer[MAX_COMP_POOL_SIZE]; static CHurtbox_t churtbox_buffer[MAX_COMP_POOL_SIZE]; static CSprite_t csprite_buffer[MAX_COMP_POOL_SIZE]; static CMoveable_t cmoveable_buffer[MAX_COMP_POOL_SIZE]; static CLifeTimer_t clifetimer_buffer[MAX_COMP_POOL_SIZE]; -static CWaterRunner_t cwaterrunner_buffer[4]; +static CWaterRunner_t cwaterrunner_buffer[32]; typedef struct ULongCircBuffer { unsigned long* buffer; // data buffer @@ -83,15 +83,15 @@ static MemPool_t comp_mempools[N_COMPONENTS] = { {ctransform_buffer, MAX_COMP_POOL_SIZE, sizeof(CTransform_t), NULL, {0}}, {ctilecoord_buffer, MAX_COMP_POOL_SIZE, sizeof(CTileCoord_t), NULL, {0}}, {cmstate_buffer, MAX_COMP_POOL_SIZE, sizeof(CMovementState_t), NULL, {0}}, - {cjump_buffer, 1, sizeof(CJump_t), NULL, {0}}, - {cplayerstate_buffer, 1, sizeof(CPlayerState_t), NULL, {0}}, + {cjump_buffer, 8, sizeof(CJump_t), NULL, {0}}, + {cplayerstate_buffer, 8, sizeof(CPlayerState_t), NULL, {0}}, {ccontainer_buffer, MAX_COMP_POOL_SIZE, sizeof(CContainer_t), NULL, {0}}, {chitboxes_buffer, MAX_COMP_POOL_SIZE, sizeof(CHitBoxes_t), NULL, {0}}, {churtbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CHurtbox_t), NULL, {0}}, {csprite_buffer, MAX_COMP_POOL_SIZE, sizeof(CSprite_t), NULL, {0}}, {cmoveable_buffer, MAX_COMP_POOL_SIZE, sizeof(CMoveable_t), NULL, {0}}, {clifetimer_buffer, MAX_COMP_POOL_SIZE, sizeof(CLifeTimer_t), NULL, {0}}, - {cwaterrunner_buffer, 4, sizeof(CWaterRunner_t), NULL, {0}}, + {cwaterrunner_buffer, 32, sizeof(CWaterRunner_t), NULL, {0}}, }; static MemPool_t ent_mempool = { .buffer = entity_buffer, diff --git a/scenes/game_scene.c b/scenes/game_scene.c index 63eacfe..9cc7716 100644 --- a/scenes/game_scene.c +++ b/scenes/game_scene.c @@ -315,7 +315,7 @@ static void level_scene_render_func(Scene_t* scene) EndDrawing(); } -void level_do_action(Scene_t* scene, ActionType_t action, bool pressed) +static void level_do_action(Scene_t* scene, ActionType_t action, bool pressed) { CPlayerState_t* p_playerstate; sc_map_foreach_value(&scene->ent_manager.component_map[CPLAYERSTATE_T], p_playerstate) @@ -356,6 +356,12 @@ void level_do_action(Scene_t* scene, ActionType_t action, bool pressed) case ACTION_PREVLEVEL: load_prev_level_tilemap((LevelScene_t*)scene); break; + case ACTION_EXIT: + if(scene->engine != NULL) + { + change_scene(scene->engine, 0); + } + break; default: break; } diff --git a/scenes/menu_scene.c b/scenes/menu_scene.c index 20d07d6..a13c6a4 100644 --- a/scenes/menu_scene.c +++ b/scenes/menu_scene.c @@ -10,8 +10,9 @@ static void menu_scene_render_func(Scene_t* scene) ClearBackground(RAYWHITE); DrawText("This is a game", 25, 220, 12, BLACK); UI_button(data->buttons, "Start"); - UI_button(data->buttons + 1, "Continue"); - UI_button(data->buttons + 2, "Exit"); + UI_button(data->buttons + 1, "Sandbox"); + UI_button(data->buttons + 2, "Continue"); + UI_button(data->buttons + 3, "Exit"); EndDrawing(); } @@ -57,6 +58,9 @@ static void menu_do_action(Scene_t* scene, ActionType_t action, bool pressed) case 0: change_scene(scene->engine, 1); break; + case 1: + change_scene(scene->engine, 2); + break; default: break; } @@ -128,7 +132,6 @@ void init_menu_scene(MenuScene_t* scene) .state = STATE_NORMAL, .alpha = 1.0 }; - scene->data.buttons[1] = (UIComp_t) { .bbox = {25,300,125,30}, .state = STATE_NORMAL, @@ -139,7 +142,12 @@ void init_menu_scene(MenuScene_t* scene) .state = STATE_NORMAL, .alpha = 1.0 }; - scene->data.max_comp = 3; + scene->data.buttons[3] = (UIComp_t) { + .bbox = {25,390,125,30}, + .state = STATE_NORMAL, + .alpha = 1.0 + }; + scene->data.max_comp = 4; scene->data.selected_comp = 0; scene->data.mode = KEYBOARD_MODE; diff --git a/scenes/scene_impl.h b/scenes/scene_impl.h index 13b285d..0eb9f8f 100644 --- a/scenes/scene_impl.h +++ b/scenes/scene_impl.h @@ -53,7 +53,7 @@ typedef enum GuiMode { } GuiMode_t; typedef struct MenuSceneData { - UIComp_t buttons[3]; + UIComp_t buttons[4]; int selected_comp; int max_comp; GuiMode_t mode;