Implement Scene and Level Scene
Changelog: - Add actions and assets headers, not implemented and not used - move original main to entManager_test - Add proper main to create window and display nothing - Implement scene base class and level scene - Add a simple level scene test - Update CMakeLists to compile main and testsscene_man
parent
4a926f8c53
commit
6c03078db9
|
@ -4,6 +4,10 @@ set(CMAKE_C_FLAGS "-Wall")
|
||||||
cmake_minimum_required(VERSION 3.22.1)
|
cmake_minimum_required(VERSION 3.22.1)
|
||||||
project(${PROJECT_NAME} C)
|
project(${PROJECT_NAME} C)
|
||||||
set(CMAKE_C_STANDARD 99)
|
set(CMAKE_C_STANDARD 99)
|
||||||
|
set(RAYLIB_DIR $ENV{HOME}/Documents/Coding/raylib/out/)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
add_subdirectory(sc)
|
add_subdirectory(sc)
|
||||||
add_executable(${PROJECT_NAME}
|
add_executable(${PROJECT_NAME}
|
||||||
|
@ -13,10 +17,70 @@ add_executable(${PROJECT_NAME}
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(${PROJECT_NAME}
|
target_include_directories(${PROJECT_NAME}
|
||||||
PUBLIC
|
PRIVATE
|
||||||
${CMAKE_CURRENT_LIST_DIR}
|
${CMAKE_CURRENT_LIST_DIR}
|
||||||
|
${RAYLIB_DIR}/include
|
||||||
)
|
)
|
||||||
|
target_link_directories(${PROJECT_NAME}
|
||||||
|
PRIVATE
|
||||||
|
${RAYLIB_DIR}/lib
|
||||||
|
)
|
||||||
|
|
||||||
target_link_libraries(${PROJECT_NAME}
|
target_link_libraries(${PROJECT_NAME}
|
||||||
sc_queue
|
sc_queue
|
||||||
sc_map
|
sc_map
|
||||||
|
raylib
|
||||||
|
m
|
||||||
|
)
|
||||||
|
|
||||||
|
add_executable(EntManager_test
|
||||||
|
entManager_test.c
|
||||||
|
entManager.c
|
||||||
|
mempool.c
|
||||||
|
)
|
||||||
|
target_compile_options(EntManager_test PRIVATE -fsanitize=address -gdwarf-4)
|
||||||
|
target_link_options(EntManager_test PRIVATE -fsanitize=address -gdwarf-4)
|
||||||
|
target_include_directories(EntManager_test
|
||||||
|
PRIVATE
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}
|
||||||
|
${RAYLIB_DIR}/include
|
||||||
|
)
|
||||||
|
target_link_directories(EntManager_test
|
||||||
|
PRIVATE
|
||||||
|
${RAYLIB_DIR}/lib
|
||||||
|
)
|
||||||
|
target_link_libraries(EntManager_test
|
||||||
|
sc_queue
|
||||||
|
sc_map
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
add_executable(scene_test
|
||||||
|
scene_test.c
|
||||||
|
scene_impl.c
|
||||||
|
scene.c
|
||||||
|
entManager.c
|
||||||
|
mempool.c
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(scene_test
|
||||||
|
PRIVATE
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}
|
||||||
|
${RAYLIB_DIR}/include
|
||||||
|
)
|
||||||
|
|
||||||
|
target_compile_options(scene_test PRIVATE -fsanitize=address -gdwarf-4)
|
||||||
|
target_link_options(scene_test PRIVATE -fsanitize=address -gdwarf-4)
|
||||||
|
|
||||||
|
target_link_directories(scene_test
|
||||||
|
PRIVATE
|
||||||
|
${RAYLIB_DIR}/lib
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(scene_test
|
||||||
|
sc_queue
|
||||||
|
sc_map
|
||||||
|
sc_array
|
||||||
|
raylib
|
||||||
|
m
|
||||||
)
|
)
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
#ifndef __ACTIONS_H
|
||||||
|
#define __ACTIONS_H
|
||||||
|
typedef enum ActionType
|
||||||
|
{
|
||||||
|
ACTION_UP
|
||||||
|
}ActionType_t;
|
||||||
|
#endif // __ACTIONS_H
|
|
@ -0,0 +1,29 @@
|
||||||
|
#ifndef __ASSETS_H
|
||||||
|
#define __ASSETS_H
|
||||||
|
#include "sc/map/sc_map.h"
|
||||||
|
#include "raylib.h"
|
||||||
|
|
||||||
|
typedef struct Animation
|
||||||
|
{
|
||||||
|
Image* sprite;
|
||||||
|
int frame_count;
|
||||||
|
int current_frame;
|
||||||
|
int speed;
|
||||||
|
Vector2 size;
|
||||||
|
char* name;
|
||||||
|
}Animation_t;
|
||||||
|
|
||||||
|
typedef struct Assets
|
||||||
|
{
|
||||||
|
}Assets_t;
|
||||||
|
#endif // __ASSETS_H
|
||||||
|
|
||||||
|
void add_texture(Assets_t *assets, char *name, char *path);
|
||||||
|
void add_animation(Assets_t *assets, char *name, char *path);
|
||||||
|
void add_sound(Assets_t *assets, char *name, char *path);
|
||||||
|
void add_font(Assets_t *assets, char *name, char *path);
|
||||||
|
|
||||||
|
Image* get_texture(Assets_t *assets);
|
||||||
|
Animation_t* get_animation(Assets_t *assets);
|
||||||
|
Sound* get_sound(Assets_t *assets);
|
||||||
|
Font* get_font(Assets_t *assets);
|
|
@ -0,0 +1,47 @@
|
||||||
|
#include "mempool.h"
|
||||||
|
#include "entManager.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
init_memory_pools();
|
||||||
|
|
||||||
|
puts("Init-ing manager and memory pool");
|
||||||
|
EntityManager_t manager;
|
||||||
|
init_entity_manager(&manager);
|
||||||
|
|
||||||
|
puts("Creating two entities");
|
||||||
|
Entity_t *p_ent = add_entity(&manager, PLAYER_ENT_TAG);
|
||||||
|
CBBox_t * p_bbox = (CBBox_t *)add_component(&manager, p_ent, CBBOX_COMP_T);
|
||||||
|
p_bbox->x = 15;
|
||||||
|
p_ent = add_entity(&manager, ENEMY_ENT_TAG);
|
||||||
|
p_bbox = (CBBox_t *)add_component(&manager, p_ent, CBBOX_COMP_T);
|
||||||
|
p_bbox->x = 40;
|
||||||
|
update_entity_manager(&manager);
|
||||||
|
|
||||||
|
puts("Print and remove the entities");
|
||||||
|
unsigned long idx = 0;
|
||||||
|
sc_map_foreach(&manager.entities, idx, p_ent)
|
||||||
|
{
|
||||||
|
p_bbox = (CBBox_t *)get_component(&manager, p_ent, CBBOX_COMP_T);
|
||||||
|
printf("BBOX x: %d\n", p_bbox->x);
|
||||||
|
remove_entity(&manager, idx);
|
||||||
|
}
|
||||||
|
puts("");
|
||||||
|
update_entity_manager(&manager);
|
||||||
|
|
||||||
|
puts("Print again, should show nothing");
|
||||||
|
sc_map_foreach(&manager.entities, idx, p_ent)
|
||||||
|
{
|
||||||
|
p_bbox = (CBBox_t *)get_component(&manager, p_ent, CBBOX_COMP_T);
|
||||||
|
printf("BBOX x: %d\n", p_bbox->x);
|
||||||
|
remove_entity(&manager, idx);
|
||||||
|
}
|
||||||
|
puts("");
|
||||||
|
|
||||||
|
puts("Freeing manager and memory pool");
|
||||||
|
free_entity_manager(&manager);
|
||||||
|
|
||||||
|
free_memory_pools();
|
||||||
|
return 0;
|
||||||
|
}
|
72
main.c
72
main.c
|
@ -1,47 +1,39 @@
|
||||||
#include "mempool.h"
|
#include "raylib.h"
|
||||||
#include "entManager.h"
|
#include "scene.h"
|
||||||
#include <stdio.h>
|
#define N_SCENES 3
|
||||||
|
|
||||||
|
Scene_t scenes[N_SCENES];
|
||||||
|
unsigned int current_scene;
|
||||||
|
|
||||||
|
const int screenWidth = 800;
|
||||||
|
const int screenHeight = 450;
|
||||||
|
|
||||||
|
static void load_assets(void)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
init_memory_pools();
|
// Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
//char dir[7];
|
||||||
|
|
||||||
puts("Init-ing manager and memory pool");
|
InitWindow(screenWidth, screenHeight, "raylib");
|
||||||
EntityManager_t manager;
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||||
init_entity_manager(&manager);
|
load_assets();
|
||||||
|
//Camera2D camera = { 0 };
|
||||||
puts("Creating two entities");
|
//camera.offset = (Vector2){0,0};
|
||||||
Entity_t *p_ent = add_entity(&manager, PLAYER_ENT_TAG);
|
//camera.rotation = 0.0f;
|
||||||
CBBox_t * p_bbox = (CBBox_t *)add_component(&manager, p_ent, CBBOX_COMP_T);
|
//camera.zoom = 1.0f;
|
||||||
p_bbox->x = 15;
|
while (!WindowShouldClose())
|
||||||
p_ent = add_entity(&manager, ENEMY_ENT_TAG);
|
|
||||||
p_bbox = (CBBox_t *)add_component(&manager, p_ent, CBBOX_COMP_T);
|
|
||||||
p_bbox->x = 40;
|
|
||||||
update_entity_manager(&manager);
|
|
||||||
|
|
||||||
puts("Print and remove the entities");
|
|
||||||
unsigned long idx = 0;
|
|
||||||
sc_map_foreach(&manager.entities, idx, p_ent)
|
|
||||||
{
|
{
|
||||||
p_bbox = (CBBox_t *)get_component(&manager, p_ent, CBBOX_COMP_T);
|
// TODO: Handle keystrokes/input here
|
||||||
printf("BBOX x: %d\n", p_bbox->x);
|
// TODO: Update Scene here or scene changes
|
||||||
remove_entity(&manager, idx);
|
|
||||||
|
BeginDrawing();
|
||||||
|
// TODO: Call the current scene Render function
|
||||||
|
ClearBackground(RAYWHITE);
|
||||||
|
EndDrawing();
|
||||||
}
|
}
|
||||||
puts("");
|
CloseWindow();
|
||||||
update_entity_manager(&manager);
|
|
||||||
|
|
||||||
puts("Print again, should show nothing");
|
|
||||||
sc_map_foreach(&manager.entities, idx, p_ent)
|
|
||||||
{
|
|
||||||
p_bbox = (CBBox_t *)get_component(&manager, p_ent, CBBOX_COMP_T);
|
|
||||||
printf("BBOX x: %d\n", p_bbox->x);
|
|
||||||
remove_entity(&manager, idx);
|
|
||||||
}
|
|
||||||
puts("");
|
|
||||||
|
|
||||||
puts("Freeing manager and memory pool");
|
|
||||||
free_entity_manager(&manager);
|
|
||||||
|
|
||||||
free_memory_pools();
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
#include "scene.h"
|
||||||
|
|
||||||
|
void init_scene(Scene_t *scene, SceneType_t scene_type, system_func_t render_func)
|
||||||
|
{
|
||||||
|
sc_map_init_64(&scene->action_map, 32, 0);
|
||||||
|
sc_queue_init(&scene->action_queue);
|
||||||
|
sc_array_init(&scene->systems);
|
||||||
|
init_entity_manager(&scene->ent_manager);
|
||||||
|
|
||||||
|
scene->scene_type = scene_type;
|
||||||
|
scene->render_function = render_func;
|
||||||
|
scene->paused = false;
|
||||||
|
scene->has_ended = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_scene(Scene_t *scene)
|
||||||
|
{
|
||||||
|
sc_map_term_64(&scene->action_map);
|
||||||
|
sc_queue_term(&scene->action_queue);
|
||||||
|
sc_array_term(&scene->systems);
|
||||||
|
free_entity_manager(&scene->ent_manager);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void update_scene(Scene_t *scene)
|
||||||
|
{
|
||||||
|
system_func_t sys;
|
||||||
|
sc_array_foreach(&scene->systems, sys)
|
||||||
|
{
|
||||||
|
sys(scene);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void render_scene(Scene_t *scene)
|
||||||
|
{
|
||||||
|
scene->render_function(scene);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void queue_action(Scene_t *scene, ActionType_t action)
|
||||||
|
{
|
||||||
|
sc_queue_add_last(&scene->action_queue, action);
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
#ifndef __SCENE_H
|
||||||
|
#define __SCENE_H
|
||||||
|
#include "entManager.h"
|
||||||
|
#include "actions.h"
|
||||||
|
#include "sc/array/sc_array.h"
|
||||||
|
typedef enum SceneType
|
||||||
|
{
|
||||||
|
LEVEL_SCENE = 0,
|
||||||
|
}SceneType_t;
|
||||||
|
|
||||||
|
typedef struct Scene Scene_t;
|
||||||
|
typedef void(*system_func_t)(Scene_t *);
|
||||||
|
sc_array_def(system_func_t, systems);
|
||||||
|
|
||||||
|
struct Scene
|
||||||
|
{
|
||||||
|
struct sc_map_64 action_map; // key -> actions
|
||||||
|
struct sc_queue_64 action_queue;
|
||||||
|
struct sc_array_systems systems;
|
||||||
|
system_func_t render_function;
|
||||||
|
EntityManager_t ent_manager;
|
||||||
|
SceneType_t scene_type;
|
||||||
|
void * scene_data;
|
||||||
|
bool paused;
|
||||||
|
bool has_ended;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern void update_scene(Scene_t *scene);
|
||||||
|
extern void render_scene(Scene_t *scene);
|
||||||
|
extern void queue_action(Scene_t *scene, ActionType_t action);
|
||||||
|
|
||||||
|
void init_scene(Scene_t *scene, SceneType_t scene_type, system_func_t render_func);
|
||||||
|
void free_scene(Scene_t *scene);
|
||||||
|
|
||||||
|
#endif // __SCENE_H
|
|
@ -0,0 +1,29 @@
|
||||||
|
#include "scene_impl.h"
|
||||||
|
#include "raylib.h"
|
||||||
|
|
||||||
|
static void level_scene_render_func(Scene_t* scene)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void movement_update_system(Scene_t* scene)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void init_level_scene(LevelScene_t *scene)
|
||||||
|
{
|
||||||
|
init_scene(&scene->scene, LEVEL_SCENE, &level_scene_render_func);
|
||||||
|
scene->scene.scene_data = &scene->data;
|
||||||
|
scene->data.player = NULL;
|
||||||
|
|
||||||
|
// insert level scene systems
|
||||||
|
sc_array_add(&scene->scene.systems, &movement_update_system);
|
||||||
|
}
|
||||||
|
void free_level_scene(LevelScene_t *scene)
|
||||||
|
{
|
||||||
|
free_scene(&scene->scene);
|
||||||
|
}
|
||||||
|
|
||||||
|
void reload_level_scene(LevelScene_t *scene)
|
||||||
|
{
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
/* This implements the scene
|
||||||
|
* ie it will insert the scene-specific data and systems
|
||||||
|
* based on the function called
|
||||||
|
* */
|
||||||
|
#ifndef __SCENE_IMPL_H
|
||||||
|
#define __SCENE_IMPL_H
|
||||||
|
#include "scene.h"
|
||||||
|
typedef struct LevelSceneData
|
||||||
|
{
|
||||||
|
Entity_t * player;
|
||||||
|
}LevelSceneData_t ;
|
||||||
|
typedef struct LevelScene
|
||||||
|
{
|
||||||
|
Scene_t scene;
|
||||||
|
LevelSceneData_t data;
|
||||||
|
}LevelScene_t;
|
||||||
|
|
||||||
|
void init_level_scene(LevelScene_t *scene);
|
||||||
|
void free_level_scene(LevelScene_t *scene);
|
||||||
|
void reload_level_scene(LevelScene_t *scene);
|
||||||
|
#endif // __SCENE_IMPL_H
|
|
@ -0,0 +1,11 @@
|
||||||
|
#include "mempool.h"
|
||||||
|
#include "scene_impl.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
LevelScene_t scene;
|
||||||
|
init_level_scene(&scene);
|
||||||
|
update_scene(&scene.scene);
|
||||||
|
free_level_scene(&scene);
|
||||||
|
}
|
Loading…
Reference in New Issue