Refactor out engine init, deinit, and key handling

To avoid repeated code
scene_man
En Yi 2023-08-19 12:20:16 +08:00
parent b30646103d
commit d2a19c5405
6 changed files with 59 additions and 132 deletions

View File

@ -9,9 +9,6 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
// Maintain own queue to handle key presses
struct sc_queue_32 key_buffer;
Scene_t* scenes[1]; Scene_t* scenes[1];
static GameEngine_t engine = static GameEngine_t engine =
{ {
@ -23,7 +20,7 @@ static GameEngine_t engine =
int main(void) int main(void)
{ {
sc_queue_init(&key_buffer); init_engine(&engine);
InitWindow(1280, 640, "raylib"); InitWindow(1280, 640, "raylib");
SetTargetFPS(60); SetTargetFPS(60);
init_memory_pools(); init_memory_pools();
@ -48,38 +45,7 @@ int main(void)
while(true) while(true)
{ {
process_inputs(&engine, &scene.scene);
// 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);
}
update_scene(&scene.scene); update_scene(&scene.scene);
update_entity_manager(&scene.scene.ent_manager); update_entity_manager(&scene.scene.ent_manager);
// This is needed to advance time delta // This is needed to advance time delta
@ -95,7 +61,7 @@ int main(void)
} }
free_scene(&scene.scene); free_scene(&scene.scene);
term_level_scene_data(&scene.data); term_level_scene_data(&scene.data);
sc_queue_term(&key_buffer); deinit_engine(&engine);
term_assets(&engine.assets); term_assets(&engine.assets);
CloseWindow(); CloseWindow();
} }

32
main.c
View File

@ -24,7 +24,7 @@ int main(void)
{ {
// Initialization // Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
sc_queue_init(&key_buffer); init_engine(&engine);
InitWindow(screenWidth, screenHeight, "raylib"); InitWindow(screenWidth, screenHeight, "raylib");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second SetTargetFPS(60); // Set our game to run at 60 frames-per-second
init_memory_pools(); init_memory_pools();
@ -62,33 +62,7 @@ int main(void)
// appear in the polling of raylib // appear in the polling of raylib
Scene_t* curr_scene = engine.scenes[engine.curr_scene]; Scene_t* curr_scene = engine.scenes[engine.curr_scene];
unsigned int sz = sc_queue_size(&key_buffer); process_inputs(&engine, curr_scene);
// 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);
}
update_scene(curr_scene); update_scene(curr_scene);
update_entity_manager(&curr_scene->ent_manager); update_entity_manager(&curr_scene->ent_manager);
@ -108,7 +82,7 @@ int main(void)
free_sandbox_scene(&sandbox_scene); free_sandbox_scene(&sandbox_scene);
free_game_scene(&level_scene); free_game_scene(&level_scene);
free_menu_scene(&menu_scene); free_menu_scene(&menu_scene);
sc_queue_term(&key_buffer); deinit_engine(&engine);
term_assets(&engine.assets); term_assets(&engine.assets);
CloseWindow(); CloseWindow();
} }

View File

@ -5,8 +5,6 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
// Maintain own queue to handle key presses
struct sc_queue_32 key_buffer;
Scene_t* scenes[1]; Scene_t* scenes[1];
static GameEngine_t engine = static GameEngine_t engine =
@ -19,7 +17,7 @@ static GameEngine_t engine =
int main(void) int main(void)
{ {
sc_queue_init(&key_buffer); init_engine(&engine);
InitWindow(1280, 640, "raylib"); InitWindow(1280, 640, "raylib");
SetTargetFPS(60); SetTargetFPS(60);
init_memory_pools(); init_memory_pools();
@ -47,34 +45,7 @@ int main(void)
// This entire key processing relies on the assumption that a pressed key will // This entire key processing relies on the assumption that a pressed key will
// appear in the polling of raylib // appear in the polling of raylib
process_inputs(&engine, &scene.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(&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);
}
update_scene(&scene.scene); update_scene(&scene.scene);
update_entity_manager(&scene.scene.ent_manager); update_entity_manager(&scene.scene.ent_manager);
@ -83,7 +54,7 @@ int main(void)
if (WindowShouldClose()) break; if (WindowShouldClose()) break;
} }
free_sandbox_scene(&scene); free_sandbox_scene(&scene);
sc_queue_term(&key_buffer); deinit_engine(&engine);
term_assets(&engine.assets); term_assets(&engine.assets);
CloseWindow(); CloseWindow();
} }

View File

@ -1,5 +1,46 @@
#include "engine.h" #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) void change_scene(GameEngine_t* engine, unsigned int idx)
{ {
engine->scenes[engine->curr_scene]->state = SCENE_ENDED; engine->scenes[engine->curr_scene]->state = SCENE_ENDED;

View File

@ -12,6 +12,8 @@ typedef struct GameEngine {
unsigned int max_scenes; unsigned int max_scenes;
unsigned int curr_scene; unsigned int curr_scene;
Assets_t assets; Assets_t assets;
// Maintain own queue to handle key presses
struct sc_queue_32 key_buffer;
} GameEngine_t; } GameEngine_t;
void change_scene(GameEngine_t* engine, unsigned int idx); void change_scene(GameEngine_t* engine, unsigned int idx);
@ -41,6 +43,10 @@ struct Scene {
GameEngine_t *engine; 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 // Inline functions, for convenience
extern void update_scene(Scene_t* scene); extern void update_scene(Scene_t* scene);
extern void render_scene(Scene_t* scene); extern void render_scene(Scene_t* scene);

View File

@ -407,7 +407,7 @@ static void player_simple_movement_system(Scene_t* scene)
int main(void) int main(void)
{ {
sc_queue_init(&key_buffer); init_engine(&engine);
InitWindow(1280, 640, "raylib"); InitWindow(1280, 640, "raylib");
SetTargetFPS(60); SetTargetFPS(60);
init_memory_pools(); init_memory_pools();
@ -457,38 +457,7 @@ int main(void)
while(true) while(true)
{ {
process_inputs(&engine, &scene.scene);
// 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);
}
update_scene(&scene.scene); update_scene(&scene.scene);
update_entity_manager(&scene.scene.ent_manager); update_entity_manager(&scene.scene.ent_manager);
// This is needed to advance time delta // This is needed to advance time delta
@ -504,7 +473,7 @@ int main(void)
} }
free_scene(&scene.scene); free_scene(&scene.scene);
term_level_scene_data(&scene.data); term_level_scene_data(&scene.data);
sc_queue_term(&key_buffer); deinit_engine(&engine);
term_assets(&engine.assets); term_assets(&engine.assets);
CloseWindow(); CloseWindow();
} }