diff --git a/level_load_test.c b/level_load_test.c index 4849ec1..9b3f464 100644 --- a/level_load_test.c +++ b/level_load_test.c @@ -9,9 +9,6 @@ #include #include -// Maintain own queue to handle key presses -struct sc_queue_32 key_buffer; - Scene_t* scenes[1]; static GameEngine_t engine = { @@ -23,7 +20,7 @@ static GameEngine_t engine = int main(void) { - sc_queue_init(&key_buffer); + init_engine(&engine); InitWindow(1280, 640, "raylib"); SetTargetFPS(60); init_memory_pools(); @@ -48,38 +45,7 @@ int main(void) while(true) { - - // This entire key processing relies on the assumption that a pressed key will - // appear in the polling of raylib - - unsigned int sz = sc_queue_size(&key_buffer); - // Process any existing pressed key - for (size_t i = 0; i < sz; i++) - { - int button = sc_queue_del_first(&key_buffer); - ActionType_t action = sc_map_get_64(&scene.scene.action_map, button); - if (IsKeyReleased(button)) - { - do_action(&scene.scene, action, false); - } - else - { - do_action(&scene.scene, action, true); - sc_queue_add_last(&key_buffer, button); - } - } - - // Detect new key presses - while(true) - { - int button = GetKeyPressed(); - if (button == 0) break; - ActionType_t action = sc_map_get_64(&scene.scene.action_map, button); - if (!sc_map_found(&scene.scene.action_map)) continue; - do_action(&scene.scene, action, true); - sc_queue_add_last(&key_buffer, button); - } - + process_inputs(&engine, &scene.scene); update_scene(&scene.scene); update_entity_manager(&scene.scene.ent_manager); // This is needed to advance time delta @@ -95,7 +61,7 @@ int main(void) } free_scene(&scene.scene); term_level_scene_data(&scene.data); - sc_queue_term(&key_buffer); + deinit_engine(&engine); term_assets(&engine.assets); CloseWindow(); } diff --git a/main.c b/main.c index 63240bc..9632e83 100644 --- a/main.c +++ b/main.c @@ -24,7 +24,7 @@ int main(void) { // Initialization //-------------------------------------------------------------------------------------- - sc_queue_init(&key_buffer); + init_engine(&engine); InitWindow(screenWidth, screenHeight, "raylib"); SetTargetFPS(60); // Set our game to run at 60 frames-per-second init_memory_pools(); @@ -62,33 +62,7 @@ int main(void) // appear in the polling of raylib Scene_t* curr_scene = engine.scenes[engine.curr_scene]; - unsigned int sz = sc_queue_size(&key_buffer); - // Process any existing pressed key - for (size_t i = 0; i < sz; i++) - { - int button = sc_queue_del_first(&key_buffer); - ActionType_t action = sc_map_get_64(&curr_scene->action_map, button); - if (IsKeyReleased(button)) - { - do_action(curr_scene, action, false); - } - else - { - do_action(curr_scene, action, true); - sc_queue_add_last(&key_buffer, button); - } - } - - // Detect new key presses - while(true) - { - int button = GetKeyPressed(); - if (button == 0) break; - ActionType_t action = sc_map_get_64(&curr_scene->action_map, button); - if (!sc_map_found(&curr_scene->action_map)) continue; - do_action(curr_scene, action, true); - sc_queue_add_last(&key_buffer, button); - } + process_inputs(&engine, curr_scene); update_scene(curr_scene); update_entity_manager(&curr_scene->ent_manager); @@ -108,7 +82,7 @@ int main(void) free_sandbox_scene(&sandbox_scene); free_game_scene(&level_scene); free_menu_scene(&menu_scene); - sc_queue_term(&key_buffer); + deinit_engine(&engine); term_assets(&engine.assets); CloseWindow(); } diff --git a/scene_test.c b/scene_test.c index 61d1f6b..37105a8 100644 --- a/scene_test.c +++ b/scene_test.c @@ -5,8 +5,6 @@ #include #include -// Maintain own queue to handle key presses -struct sc_queue_32 key_buffer; Scene_t* scenes[1]; static GameEngine_t engine = @@ -19,7 +17,7 @@ static GameEngine_t engine = int main(void) { - sc_queue_init(&key_buffer); + init_engine(&engine); InitWindow(1280, 640, "raylib"); SetTargetFPS(60); init_memory_pools(); @@ -47,34 +45,7 @@ int main(void) // This entire key processing relies on the assumption that a pressed key will // appear in the polling of raylib - - unsigned int sz = sc_queue_size(&key_buffer); - // Process any existing pressed key - for (size_t i = 0; i < sz; i++) - { - int button = sc_queue_del_first(&key_buffer); - ActionType_t action = sc_map_get_64(&scene.scene.action_map, button); - if (IsKeyReleased(button)) - { - do_action(&scene.scene, action, false); - } - else - { - do_action(&scene.scene, action, true); - sc_queue_add_last(&key_buffer, button); - } - } - - // Detect new key presses - while(true) - { - int button = GetKeyPressed(); - if (button == 0) break; - ActionType_t action = sc_map_get_64(&scene.scene.action_map, button); - if (!sc_map_found(&scene.scene.action_map)) continue; - do_action(&scene.scene, action, true); - sc_queue_add_last(&key_buffer, button); - } + process_inputs(&engine, &scene.scene); update_scene(&scene.scene); update_entity_manager(&scene.scene.ent_manager); @@ -83,7 +54,7 @@ int main(void) if (WindowShouldClose()) break; } free_sandbox_scene(&scene); - sc_queue_term(&key_buffer); + deinit_engine(&engine); term_assets(&engine.assets); CloseWindow(); } diff --git a/scenes/engine/engine.c b/scenes/engine/engine.c index 841a1fc..8bf843a 100644 --- a/scenes/engine/engine.c +++ b/scenes/engine/engine.c @@ -1,5 +1,46 @@ #include "engine.h" +void init_engine(GameEngine_t* engine) +{ + sc_queue_init(&engine->key_buffer); +} + +void deinit_engine(GameEngine_t* engine) +{ + sc_queue_term(&engine->key_buffer); +} + +void process_inputs(GameEngine_t* engine, Scene_t* scene) +{ + unsigned int sz = sc_queue_size(&engine->key_buffer); + // Process any existing pressed key + for (size_t i = 0; i < sz; i++) + { + int button = sc_queue_del_first(&engine->key_buffer); + ActionType_t action = sc_map_get_64(&scene->action_map, button); + if (IsKeyReleased(button)) + { + do_action(scene, action, false); + } + else + { + do_action(scene, action, true); + sc_queue_add_last(&engine->key_buffer, button); + } + } + + // Detect new key presses + while(true) + { + int button = GetKeyPressed(); + if (button == 0) break; + ActionType_t action = sc_map_get_64(&scene->action_map, button); + if (!sc_map_found(&scene->action_map)) continue; + do_action(scene, action, true); + sc_queue_add_last(&engine->key_buffer, button); + } +} + void change_scene(GameEngine_t* engine, unsigned int idx) { engine->scenes[engine->curr_scene]->state = SCENE_ENDED; diff --git a/scenes/engine/engine.h b/scenes/engine/engine.h index d8ca040..8f39ed8 100644 --- a/scenes/engine/engine.h +++ b/scenes/engine/engine.h @@ -12,6 +12,8 @@ typedef struct GameEngine { unsigned int max_scenes; unsigned int curr_scene; Assets_t assets; + // Maintain own queue to handle key presses + struct sc_queue_32 key_buffer; } GameEngine_t; void change_scene(GameEngine_t* engine, unsigned int idx); @@ -41,6 +43,10 @@ struct Scene { GameEngine_t *engine; }; +void init_engine(GameEngine_t* engine); +void deinit_engine(GameEngine_t* engine); +void process_inputs(GameEngine_t* engine, Scene_t* scene); + // Inline functions, for convenience extern void update_scene(Scene_t* scene); extern void render_scene(Scene_t* scene); diff --git a/water_test.c b/water_test.c index f1b4211..2fdd72f 100644 --- a/water_test.c +++ b/water_test.c @@ -407,7 +407,7 @@ static void player_simple_movement_system(Scene_t* scene) int main(void) { - sc_queue_init(&key_buffer); + init_engine(&engine); InitWindow(1280, 640, "raylib"); SetTargetFPS(60); init_memory_pools(); @@ -457,38 +457,7 @@ int main(void) while(true) { - - // This entire key processing relies on the assumption that a pressed key will - // appear in the polling of raylib - - unsigned int sz = sc_queue_size(&key_buffer); - // Process any existing pressed key - for (size_t i = 0; i < sz; i++) - { - int button = sc_queue_del_first(&key_buffer); - ActionType_t action = sc_map_get_64(&scene.scene.action_map, button); - if (IsKeyReleased(button)) - { - do_action(&scene.scene, action, false); - } - else - { - do_action(&scene.scene, action, true); - sc_queue_add_last(&key_buffer, button); - } - } - - // Detect new key presses - while(true) - { - int button = GetKeyPressed(); - if (button == 0) break; - ActionType_t action = sc_map_get_64(&scene.scene.action_map, button); - if (!sc_map_found(&scene.scene.action_map)) continue; - do_action(&scene.scene, action, true); - sc_queue_add_last(&key_buffer, button); - } - + process_inputs(&engine, &scene.scene); update_scene(&scene.scene); update_entity_manager(&scene.scene.ent_manager); // This is needed to advance time delta @@ -504,7 +473,7 @@ int main(void) } free_scene(&scene.scene); term_level_scene_data(&scene.data); - sc_queue_term(&key_buffer); + deinit_engine(&engine); term_assets(&engine.assets); CloseWindow(); }