Refactor and prepare for scene changing

Changelog:
- Rename scene.* to engine.* as the game engine is tied to the
  base scene struct
- Combine *_impl.h into a single header
- Rename scene implementation source code file
- Add GameEngine struct and SceneState enum
scene_man
En Yi 2023-02-12 23:30:55 +08:00
parent c5937694e9
commit 1612b4f648
9 changed files with 56 additions and 44 deletions

View File

@ -1,8 +1,8 @@
add_subdirectory(EC) add_subdirectory(EC)
add_library(lib_scenes STATIC add_library(lib_scenes STATIC
scene.c engine.c
scene_impl.c editor_scene.c
menu_impl.c menu_scene.c
game_systems.c game_systems.c
AABB.c AABB.c
gui.c gui.c

View File

@ -1,4 +1,4 @@
#include "scene.h" #include "engine.h"
void init_scene(Scene_t *scene, SceneType_t scene_type, system_func_t render_func, action_func_t action_func) void init_scene(Scene_t *scene, SceneType_t scene_type, system_func_t render_func, action_func_t action_func)
{ {
@ -9,8 +9,7 @@ void init_scene(Scene_t *scene, SceneType_t scene_type, system_func_t render_fun
scene->scene_type = scene_type; scene->scene_type = scene_type;
scene->render_function = render_func; scene->render_function = render_func;
scene->action_function = action_func; scene->action_function = action_func;
scene->paused = false; scene->state = SCENE_ENDED;
scene->has_ended = false;
} }
void free_scene(Scene_t *scene) void free_scene(Scene_t *scene)

View File

@ -1,15 +1,31 @@
#ifndef __SCENE_H #ifndef __ENGINE_H
#define __SCENE_H #define __ENGINE_H
#include "entManager.h" #include "entManager.h"
#include "actions.h" #include "actions.h"
#include "sc/array/sc_array.h" #include "sc/array/sc_array.h"
typedef struct Scene Scene_t;
typedef struct GameEngine
{
Scene_t *scenes;
unsigned int max_scenes;
unsigned int curr_scene;
}GameEngine_t;
void change_scene(GameEngine_t *engine);
typedef enum SceneType typedef enum SceneType
{ {
LEVEL_SCENE = 0, LEVEL_SCENE = 0,
MENU_SCENE, MENU_SCENE,
}SceneType_t; }SceneType_t;
typedef struct Scene Scene_t; typedef enum SceneState
{
SCENE_PLAYING = 0,
SCENE_SUSPENDED,
SCENE_ENDED,
}SceneState_t;
typedef void(*system_func_t)(Scene_t *); typedef void(*system_func_t)(Scene_t *);
typedef void(*action_func_t)(Scene_t *, ActionType_t, bool); typedef void(*action_func_t)(Scene_t *, ActionType_t, bool);
sc_array_def(system_func_t, systems); sc_array_def(system_func_t, systems);
@ -22,9 +38,9 @@ struct Scene
action_func_t action_function; action_func_t action_function;
EntityManager_t ent_manager; EntityManager_t ent_manager;
SceneType_t scene_type; SceneType_t scene_type;
SceneState_t state;
void * scene_data; void * scene_data;
bool paused; GameEngine_t *engine;
bool has_ended;
}; };
// Inline functions, for convenience // Inline functions, for convenience
@ -35,4 +51,4 @@ extern void do_action(Scene_t *scene, ActionType_t action, bool pressed);
void init_scene(Scene_t *scene, SceneType_t scene_type, system_func_t render_func, action_func_t action_func); void init_scene(Scene_t *scene, SceneType_t scene_type, system_func_t render_func, action_func_t action_func);
void free_scene(Scene_t *scene); void free_scene(Scene_t *scene);
#endif // __SCENE_H #endif // __ENGINE_H

View File

@ -1,28 +0,0 @@
#ifndef __MENU_IMPL_H
#define __MENU_IMPL_H
#include "scene.h"
#include "gui.h"
typedef enum GuiMode
{
KEYBOARD_MODE,
MOUSE_MODE
}GuiMode_t;
typedef struct MenuSceneData
{
UIComp_t buttons[3];
int selected_comp;
int max_comp;
GuiMode_t mode;
}MenuSceneData_t;
typedef struct MenuScene
{
Scene_t scene;
MenuSceneData_t data;
}MenuScene_t;
void init_menu_scene(MenuScene_t *scene);
void free_menu_scene(MenuScene_t *scene);
#endif

View File

@ -1,4 +1,4 @@
#include "menu_impl.h" #include "scene_impl.h"
#include "raymath.h" #include "raymath.h"
#include <stdio.h> #include <stdio.h>

View File

@ -4,7 +4,9 @@
* */ * */
#ifndef __SCENE_IMPL_H #ifndef __SCENE_IMPL_H
#define __SCENE_IMPL_H #define __SCENE_IMPL_H
#include "scene.h" #include "engine.h"
#include "gui.h"
typedef struct Tile typedef struct Tile
{ {
bool solid; bool solid;
@ -35,4 +37,27 @@ typedef struct LevelScene
void init_level_scene(LevelScene_t *scene); void init_level_scene(LevelScene_t *scene);
void free_level_scene(LevelScene_t *scene); void free_level_scene(LevelScene_t *scene);
void reload_level_scene(LevelScene_t *scene); void reload_level_scene(LevelScene_t *scene);
typedef enum GuiMode
{
KEYBOARD_MODE,
MOUSE_MODE
}GuiMode_t;
typedef struct MenuSceneData
{
UIComp_t buttons[3];
int selected_comp;
int max_comp;
GuiMode_t mode;
}MenuSceneData_t;
typedef struct MenuScene
{
Scene_t scene;
MenuSceneData_t data;
}MenuScene_t;
void init_menu_scene(MenuScene_t *scene);
void free_menu_scene(MenuScene_t *scene);
#endif // __SCENE_IMPL_H #endif // __SCENE_IMPL_H

2
main.c
View File

@ -1,5 +1,5 @@
#include "raylib.h" #include "raylib.h"
#include "scene.h" #include "engine.h"
#define N_SCENES 3 #define N_SCENES 3
Scene_t scenes[N_SCENES]; Scene_t scenes[N_SCENES];

View File

@ -1,5 +1,5 @@
#include "mempool.h" #include "mempool.h"
#include "menu_impl.h" #include "scene_impl.h"
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>