Remove scene hierachy handling coz it sucks

main
En Yi 2025-08-31 16:00:12 +08:00
parent e72aa1c9fc
commit d4bc574d09
4 changed files with 18 additions and 293 deletions

View File

@ -91,7 +91,7 @@ if (NOT EMSCRIPTEN)
add_target_exe(menu_test)
add_target_exe(assets_test)
add_target_exe(particle_test)
add_target_exe(scene_man_test)
#add_target_exe(scene_man_test)
endif()
if (BUILD_TESTING)
find_package(cmocka 1.1.0 REQUIRED)

View File

@ -279,14 +279,15 @@ static void _internal_render_scene(Scene_t* scene)
{
if ((scene->state & SCENE_RENDER_BIT) == 0) return;
if (scene->parent_scene == NULL)
{
ClearBackground(scene->bg_colour);
}
else
{
ClearBackground((Color){255,255,255,0});
}
ClearBackground(scene->bg_colour);
//if (scene->parent_scene == NULL)
//{
// ClearBackground(scene->bg_colour);
//}
//else
//{
// ClearBackground((Color){255,255,255,0});
//}
for (uint8_t i = 0; i < scene->layers.n_layers; ++i)
{
@ -366,28 +367,15 @@ void update_curr_scene(GameEngine_t* engine)
float frame_time = GetFrameTime();
float delta_time = fminf(frame_time, DT);
sc_queue_clear(&engine->scene_stack);
// Wanted to do a parent-child scene handling to allow multi scene handling
// but botched up the implementation and not used
// These are the remnant of that implementation
// Plan better for version 2 of the engine
sc_heap_clear(&engine->scenes_render_order);
sc_queue_add_first(&engine->scene_stack, engine->scenes[engine->curr_scene]);
while (!sc_queue_empty(&engine->scene_stack))
{
Scene_t* scene = sc_queue_del_first(&engine->scene_stack);
update_scene(scene, delta_time);
if (scene->child_scene.next != NULL)
{
sc_queue_add_first(&engine->scene_stack, scene->child_scene.next);
}
if (scene->child_scene.scene != NULL)
{
sc_queue_add_first(&engine->scene_stack, scene->child_scene.scene);
}
sc_heap_add(&engine->scenes_render_order, scene->depth_index, scene);
}
Scene_t* scene = engine->scenes[engine->curr_scene];
update_scene(scene, delta_time);
sc_heap_add(&engine->scenes_render_order, scene->depth_index, scene);
}
void render_curr_scene(GameEngine_t* engine)
@ -403,73 +391,6 @@ void render_curr_scene(GameEngine_t* engine)
EndDrawing();
}
void add_child_scene(GameEngine_t* engine, unsigned int child_idx, unsigned int parent_idx)
{
if (
child_idx >= engine->max_scenes
|| parent_idx >= engine->max_scenes
) return;
Scene_t* child = engine->scenes[child_idx];
Scene_t* parent = engine->scenes[parent_idx];
if (parent == NULL) return;
if (parent->child_scene.scene == NULL)
{
parent->child_scene.scene = child;
}
else
{
Scene_t* curr = parent->child_scene.scene;
while (curr->child_scene.next != NULL)
{
curr = curr->child_scene.next;
}
curr->child_scene.next = child;
}
child->parent_scene = parent;
}
void remove_child_scene(GameEngine_t* engine, unsigned int idx)
{
if (idx >= engine->max_scenes) return;
Scene_t* child = engine->scenes[idx];
if (child == NULL) return;
if (child->parent_scene == NULL) return;
Scene_t* parent = child->parent_scene;
if (parent->child_scene.scene == NULL) return;
Scene_t* prev = NULL;
Scene_t* curr = parent->child_scene.scene;
while (curr != NULL)
{
if (curr == child)
{
if (prev != NULL)
{
prev->child_scene.next = curr->child_scene.next;
}
else
{
parent->child_scene.scene = curr->child_scene.next;
}
break;
}
prev = curr;
curr = curr->child_scene.next;
}
child->parent_scene = NULL;
}
Scene_t* change_active_scene(GameEngine_t* engine, unsigned int idx)
{
assert(idx < engine->max_scenes);

View File

@ -18,11 +18,6 @@ typedef struct SFXList
uint32_t played_sfx;
} SFXList_t;
typedef struct SceneNode {
Scene_t* scene;
Scene_t* next;
} SceneNode_t;
typedef struct GameEngine {
Scene_t **scenes;
unsigned int max_scenes;
@ -71,7 +66,6 @@ struct Scene {
// but too late to change this
uint32_t subsystem_init;
EntityManager_t ent_manager;
Scene_t* parent_scene;
struct sc_map_64 action_map; // key -> actions
struct sc_array_systems systems;
@ -87,7 +81,6 @@ struct Scene {
ParticleSystem_t part_sys;
GameEngine_t *engine;
int8_t depth_index;
SceneNode_t child_scene; // Intrusive Linked List for children scene
};
@ -131,7 +124,5 @@ extern void do_action(Scene_t* scene, ActionType_t action, bool pressed);
void init_scene(Scene_t* scene, action_func_t action_func, uint32_t subsystem_init);
bool add_scene_layer(Scene_t* scene, int width, int height, Rectangle render_area);
void free_scene(Scene_t* scene);
void add_child_scene(GameEngine_t* engine, unsigned int child_idx, unsigned int parent_idx);
void remove_child_scene(GameEngine_t* engine, unsigned int idx);
#endif // __ENGINE_H

View File

@ -1,187 +0,0 @@
#include "constants.h"
#include "scene_impl.h"
#include "ent_impl.h"
#include "water_flow.h"
#include "game_systems.h"
#include "assets_loader.h"
#include "raymath.h"
#include <stdio.h>
#include <unistd.h>
static Tile_t all_tiles[MAX_N_TILES] = {0};
// Maintain own queue to handle key presses
struct sc_queue_32 key_buffer;
Scene_t* scenes[6];
static GameEngine_t engine =
{
.scenes = scenes,
.max_scenes = 6,
.curr_scene = 0,
.assets = {0}
};
#define GAME_LAYER 0
struct DummyScene {
Scene_t scene;
unsigned int number;
float elapsed;
Vector2 text_pos;
};
static void level_scene_render_func(Scene_t* scene)
{
struct DummyScene* data = CONTAINER_OF(scene, struct DummyScene, scene);
char text[32];
sprintf(text, "Scene %u", data->number);
BeginTextureMode(scene->layers.render_layers[0].layer_tex);
DrawText(text, 32 * data->number, 32 * data->number, 12, BLACK);
EndTextureMode();
}
static inline unsigned int get_tile_idx(int x, int y, const TileGrid_t* tilemap)
{
unsigned int tile_x = x / TILE_SIZE;
unsigned int tile_y = y / TILE_SIZE;
if (tile_x < tilemap->width && tile_y < tilemap->height)
{
return tile_y * tilemap->width + tile_x;
}
return MAX_N_TILES;
}
static void print_number_sys(Scene_t* scene)
{
struct DummyScene* data = CONTAINER_OF(scene, struct DummyScene, scene);
data->elapsed += scene->delta_time;
if (data->elapsed > 1.0f)
{
printf("Data: %u\n", data->number);
data->elapsed -= 1.0f;
}
}
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)
{
switch(action)
{
case ACTION_UP:
p_playerstate->player_dir.y = (pressed)? -1 : 0;
break;
case ACTION_DOWN:
p_playerstate->player_dir.y = (pressed)? 1 : 0;
break;
case ACTION_LEFT:
p_playerstate->player_dir.x = (pressed)? -1 : 0;
break;
case ACTION_RIGHT:
p_playerstate->player_dir.x = (pressed)? 1 : 0;
break;
default:
break;
}
}
switch (action)
{
case ACTION_RESTART:
puts("Restarting!");
break;
default:
break;
}
}
int main(void)
{
init_engine(&engine, (Vector2){1280,640});
SetTargetFPS(60);
// TODO: Add render function
// Add a way to switch focused scene
static struct DummyScene dummy_scenes[6];
for (uint8_t i = 0; i < 6; ++i)
{
scenes[i] = &dummy_scenes[i].scene;
init_scene(&dummy_scenes[i].scene, &level_do_action, 0);
dummy_scenes[i].scene.engine = &engine;
dummy_scenes[i].number = i;
add_scene_layer(
&dummy_scenes[i].scene, 1280, 640, (Rectangle){0,0,1280,640}
);
dummy_scenes[i].scene.bg_colour = WHITE;
sc_array_add(&dummy_scenes[i].scene.systems, &print_number_sys);
sc_array_add(&dummy_scenes[i].scene.systems, &level_scene_render_func);
//sc_map_put_64(&scene.scene.action_map, KEY_R, ACTION_RESTART);
//sc_map_put_64(&scene.scene.action_map, KEY_UP, ACTION_UP);
//sc_map_put_64(&scene.scene.action_map, KEY_DOWN, ACTION_DOWN);
//sc_map_put_64(&scene.scene.action_map, KEY_LEFT, ACTION_LEFT);
//sc_map_put_64(&scene.scene.action_map, KEY_RIGHT, ACTION_RIGHT);
//sc_map_put_64(&scene.scene.action_map, KEY_P, ACTION_METAL_TOGGLE);
}
change_active_scene(&engine, 0);
add_child_scene(&engine, 1, 0);
add_child_scene(&engine, 2, 0);
add_child_scene(&engine, 3, 0);
float timer = 0;
while(timer < 1.2f)
{
timer += GetFrameTime();
process_active_scene_inputs(&engine);
update_curr_scene(&engine);
// This is needed to advance time delta
render_curr_scene(&engine);
if (WindowShouldClose()) break;
}
remove_child_scene(&engine, 2);
timer = 0;
while(timer < 1.2f)
{
timer += GetFrameTime();
process_active_scene_inputs(&engine);
update_curr_scene(&engine);
// This is needed to advance time delta
render_curr_scene(&engine);
if (WindowShouldClose()) break;
}
add_child_scene(&engine, 4, 0);
timer = 0;
while(timer < 1.2f)
{
timer += GetFrameTime();
process_active_scene_inputs(&engine);
update_curr_scene(&engine);
// This is needed to advance time delta
render_curr_scene(&engine);
if (WindowShouldClose()) break;
}
add_child_scene(&engine, 2, 1);
timer = 0;
while(timer < 1.2f)
{
timer += GetFrameTime();
process_active_scene_inputs(&engine);
update_curr_scene(&engine);
// This is needed to advance time delta
render_curr_scene(&engine);
if (WindowShouldClose()) break;
}
for (uint8_t i = 0; i < 6; ++i)
{
free_scene(&dummy_scenes[i].scene);
}
deinit_engine(&engine);
}