Compare commits

...

3 Commits

Author SHA1 Message Date
En Yi 7c86e0b3c5 Implement initial particle system
Changelog:
- Can only to one-shot burst emitter
- Add test for it
2023-10-21 14:37:54 +08:00
En Yi 290dafdf86 Rework scene rendering function
Split off- and on-screen rendering. Off-screen rendering would be a
system now
2023-10-21 10:48:21 +08:00
En Yi 7b2c48524d Add skeleton for particle system 2023-10-18 21:45:19 +08:00
11 changed files with 498 additions and 216 deletions

View File

@ -139,6 +139,19 @@ target_link_libraries(assets_test
${GAME_LIBS} ${GAME_LIBS}
) )
add_executable(particle_test
particle_test.c
)
target_include_directories(particle_test
PRIVATE
${CMAKE_CURRENT_LIST_DIR}
)
target_compile_options(particle_test PRIVATE -fsanitize=address -gdwarf-4)
target_link_options(particle_test PRIVATE -fsanitize=address -gdwarf-4)
target_link_libraries(particle_test
${GAME_LIBS}
)
if (BUILD_TESTING) if (BUILD_TESTING)
find_package(cmocka 1.1.0 REQUIRED) find_package(cmocka 1.1.0 REQUIRED)
add_subdirectory(tests) add_subdirectory(tests)

View File

@ -8,6 +8,7 @@ add_library(lib_engine STATIC
rres.c rres.c
mempool.c mempool.c
entManager.c entManager.c
particle_sys.c
${LIBZSTD_DIR}/lib/libzstd.a ${LIBZSTD_DIR}/lib/libzstd.a
) )
target_include_directories(lib_engine target_include_directories(lib_engine

View File

@ -119,7 +119,9 @@ inline void update_scene(Scene_t* scene)
inline void render_scene(Scene_t* scene) inline void render_scene(Scene_t* scene)
{ {
BeginDrawing();
scene->render_function(scene); scene->render_function(scene);
EndDrawing();
} }
inline void do_action(Scene_t* scene, ActionType_t action, bool pressed) inline void do_action(Scene_t* scene, ActionType_t action, bool pressed)

View File

@ -4,6 +4,7 @@
#include "collisions.h" #include "collisions.h"
#include "sc/array/sc_array.h" #include "sc/array/sc_array.h"
#include "assets.h" #include "assets.h"
#include "particle_sys.h"
typedef struct Scene Scene_t; typedef struct Scene Scene_t;
@ -21,6 +22,7 @@ typedef struct GameEngine {
unsigned int curr_scene; unsigned int curr_scene;
Assets_t assets; Assets_t assets;
SFXList_t sfx_list; SFXList_t sfx_list;
ParticleSystem_t part_sys;
// Maintain own queue to handle key presses // Maintain own queue to handle key presses
struct sc_queue_32 key_buffer; struct sc_queue_32 key_buffer;
} GameEngine_t; } GameEngine_t;

View File

@ -9,6 +9,9 @@
#define MAX_NAME_LEN 32 #define MAX_NAME_LEN 32
#define MAX_LEVEL_PACK 4 #define MAX_LEVEL_PACK 4
#define N_SFX 18 #define N_SFX 18
#define MAX_PARTICLE_CONF 8
#define MAX_PARTICLE_EMITTER 16
#define MAX_PARTICLES 10
#define MAX_TILE_TYPES 16 #define MAX_TILE_TYPES 16

View File

@ -0,0 +1,126 @@
#include "particle_sys.h"
#include <string.h>
#include <stdlib.h>
#include <math.h>
// TEMPORARY VARIABLE: NEED TO FIND A WAY TO DEAL WITH THIS
#define DELTA_T 0.017
void init_particle_system(ParticleSystem_t* system)
{
memset(system, 0, sizeof(ParticleSystem_t));
sc_queue_init(&system->free_list);
for ( uint32_t i = 1; i <= MAX_PARTICLE_EMITTER; ++i)
{
sc_queue_add_last(&system->free_list, i);
}
system->tail_idx = 0;
}
void add_particle_emitter(ParticleSystem_t* system, const ParticleEmitter_t* in_emitter)
{
if (sc_queue_empty(&system->free_list)) return;
uint32_t idx = sc_queue_del_first(&system->free_list);
system->emitter_list[system->tail_idx].next = idx;
system->tail_idx = idx;
system->emitter_list[idx].next = 0;
system->emitters[idx] = *in_emitter;
system->emitters[idx].active = true;
if (system->emitters[idx].n_particles > MAX_PARTICLES)
{
system->emitters[idx].n_particles = MAX_PARTICLES;
}
ParticleEmitter_t* emitter = system->emitters + idx;
// Generate particles based on type
for (uint32_t i = 0; i < emitter->n_particles; ++i)
{
uint32_t lifetime = (emitter->config.particle_lifetime[1] - emitter->config.particle_lifetime[0]);
emitter->particles[i].timer = emitter->config.particle_lifetime[0];
emitter->particles[i].timer += rand() % lifetime;
emitter->particles[i].alive = true;
float angle = emitter->config.launch_range[1] - emitter->config.launch_range[0];
angle *= (float)rand() / (float)RAND_MAX;
angle += emitter->config.launch_range[0];
if(angle > 360) angle -= 360;
if(angle < -360) angle += 360;
angle *= PI / 180;
float speed = emitter->config.speed_range[1] - emitter->config.speed_range[0];
speed *= (float)rand() / (float)RAND_MAX;
speed += emitter->config.speed_range[0];
emitter->particles[i].velocity.x = speed * cos(angle);
emitter->particles[i].velocity.y = speed * sin(angle);
emitter->particles[i].position = emitter->position;
}
}
void update_particle_system(ParticleSystem_t* system)
{
uint32_t emitter_idx = system->emitter_list[0].next;
uint32_t last_idx = 0;
while (emitter_idx != 0)
{
uint32_t next_idx = system->emitter_list[emitter_idx].next;
ParticleEmitter_t* emitter = system->emitters + emitter_idx;
uint32_t inactive_count = 0;
for (uint32_t i = 0; i < emitter->n_particles; ++i)
{
if (emitter->particles[i].alive)
{
if (emitter->config.update_func != NULL)
{
emitter->config.update_func(emitter->particles + i, emitter->config.user_data);
}
// Lifetime update
if (emitter->particles[i].timer > 0) emitter->particles[i].timer--;
if (emitter->particles[i].timer == 0)
{
emitter->particles[i].alive = false;
inactive_count++;
}
}
else
{
inactive_count++;
}
}
if (inactive_count == emitter->n_particles)
{
emitter->active = false;
system->emitter_list[last_idx].next = system->emitter_list[emitter_idx].next;
system->emitter_list[emitter_idx].next = 0;
if (system->tail_idx == emitter_idx)
{
system->tail_idx = last_idx;
}
sc_queue_add_last(&system->free_list, emitter_idx);
emitter_idx = last_idx;
}
last_idx = emitter_idx;
emitter_idx = next_idx;
}
}
void draw_particle_system(ParticleSystem_t* system)
{
uint32_t emitter_idx = system->emitter_list[0].next;
while (emitter_idx != 0)
{
ParticleEmitter_t* emitter = system->emitters + emitter_idx;
for (uint32_t i = 0; i < emitter->n_particles; ++i)
{
if (emitter->particles[i].alive)
{
DrawCircleV(emitter->particles[i].position, 5, BLACK);
}
}
emitter_idx = system->emitter_list[emitter_idx].next;
}
}
void deinit_particle_system(ParticleSystem_t* system)
{
sc_queue_term(&system->free_list);
}

View File

@ -0,0 +1,70 @@
#ifndef _PARTICLE_SYSTEM_H
#define _PARTICLE_SYSTEM_H
#include "raylib.h"
#include "engine_conf.h"
#include "sc_queue.h"
#include <stdint.h>
#include <stdbool.h>
typedef enum PartEmitterType
{
EMITTER_BURST = 0,
} PartEmitterType_t;
typedef struct Particle
{
Texture2D* tex;
Vector2 position;
Vector2 velocity;
Vector2 accleration;
float rotation;
float size;
uint32_t timer;
bool alive;
}Particle_t;
typedef void (*particle_update_func_t)(Particle_t* part, void* user_data);
typedef struct EmitterConfig
{
float launch_range[2];
float speed_range[2];
uint32_t particle_lifetime[2];
PartEmitterType_t type;
void* user_data;
particle_update_func_t update_func;
}EmitterConfig_t;
typedef struct ParticleEmitter
{
EmitterConfig_t config;
Vector2 position;
Particle_t particles[MAX_PARTICLES];
uint32_t n_particles;
uint32_t timer;
bool one_shot;
bool finished;
bool active;
}ParticleEmitter_t;
typedef struct IndexList
{
uint32_t next;
}IndexList_t;
typedef struct ParticleSystem
{
ParticleEmitter_t emitters[MAX_PARTICLE_EMITTER + 1];
IndexList_t emitter_list[MAX_PARTICLE_EMITTER + 1];
struct sc_queue_64 free_list;
uint32_t tail_idx;
uint32_t n_configs;
uint32_t n_emitters;
}ParticleSystem_t;
void init_particle_system(ParticleSystem_t* system);
void add_particle_emitter(ParticleSystem_t* system, const ParticleEmitter_t* in_emitter);
void update_particle_system(ParticleSystem_t* system);
void draw_particle_system(ParticleSystem_t* system);
void deinit_particle_system(ParticleSystem_t* system);
#endif // _PARTICLE_SYSTEM_H

87
particle_test.c 100644
View File

@ -0,0 +1,87 @@
#include "particle_sys.h"
#include <stdio.h>
#include <unistd.h>
#include "raylib.h"
#include "raymath.h"
#include "constants.h"
static const Vector2 GRAVITY = {0, GRAV_ACCEL};
void simple_particle_system_update(Particle_t* part, void* user_data)
{
float delta_time = DELTA_T; // TODO: Will need to think about delta time handling
part->velocity =
Vector2Add(
part->velocity,
Vector2Scale(GRAVITY, delta_time)
);
float mag = Vector2Length(part->velocity);
part->velocity = Vector2Scale(
Vector2Normalize(part->velocity),
(mag > PLAYER_MAX_SPEED)? PLAYER_MAX_SPEED:mag
);
// 3 dp precision
if (fabs(part->velocity.x) < 1e-3) part->velocity.x = 0;
if (fabs(part->velocity.y) < 1e-3) part->velocity.y = 0;
part->position = Vector2Add(
part->position,
Vector2Scale(part->velocity, delta_time)
);
// Level boundary collision
{
if(
part->position.x + part->size < 0 || part->position.x - part->size > 1280
|| part->position.y + part->size < 0 || part->position.y - part->size > 640
)
{
part->timer = 0;
}
}
}
int main(void)
{
InitWindow(1280, 640, "raylib");
SetTargetFPS(60);
static ParticleSystem_t part_sys = {0};
init_particle_system(&part_sys);
EmitterConfig_t conf ={
.launch_range = {0, 360},
.speed_range = {400, 2000},
.particle_lifetime = {30, 110},
.update_func = &simple_particle_system_update
};
ParticleEmitter_t emitter = {
.config = conf,
.n_particles = MAX_PARTICLES,
.one_shot = true,
};
bool key_press = false;
while(!WindowShouldClose())
{
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
key_press = true;
}
else if (key_press && IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
{
emitter.position = GetMousePosition();
add_particle_emitter(&part_sys, &emitter);
key_press = false;
}
update_particle_system(&part_sys);
BeginDrawing();
ClearBackground(RAYWHITE);
draw_particle_system(&part_sys);
EndDrawing();
}
CloseWindow();
}

View File

@ -81,6 +81,106 @@ static inline unsigned int get_tile_idx(int x, int y, const TileGrid_t* tilemap)
static RenderTexture2D selection_section; static RenderTexture2D selection_section;
static void level_scene_render_func(Scene_t* scene) static void level_scene_render_func(Scene_t* scene)
{
LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data);
Entity_t* p_ent;
Rectangle draw_rec = data->game_rec;
draw_rec.x = 0;
draw_rec.y = 0;
draw_rec.height *= -1;
static char buffer[512];
ClearBackground(LIGHTGRAY);
DrawTextureRec(
data->game_viewport.texture,
draw_rec,
(Vector2){data->game_rec.x, data->game_rec.y},
WHITE
);
draw_rec.width = SELECTION_REGION_WIDTH;
draw_rec.height = -(SELECTION_REGION_HEIGHT * 2 + 10);
Vector2 draw_pos = {data->game_rec.x, data->game_rec.y + data->game_rec.height + SELECTION_GAP};
DrawTextureRec(
selection_section.texture,
draw_rec,
draw_pos,
WHITE
);
draw_pos.x = data->game_rec.x + current_spawn_selection * SELECTION_TILE_SIZE;
DrawRectangleLines(
draw_pos.x, draw_pos.y,
SELECTION_TILE_SIZE, SELECTION_TILE_SIZE, GREEN
);
draw_pos.x = data->game_rec.x + (MAX_SPAWN_TYPE + 1) * SELECTION_TILE_SIZE;
sprintf(buffer, "Selection: %s", get_spawn_selection_string(current_spawn_selection));
DrawText(buffer, draw_pos.x, draw_pos.y, 20, BLACK);
draw_pos.y += SELECTION_TILE_SIZE + 5;
sprintf(buffer, "Crate %s on spawn", crate_activation? "active" : "inactive");
DrawText(buffer, draw_pos.x, draw_pos.y, 20, BLACK);
// For DEBUG
const int gui_x = data->game_rec.x + data->game_rec.width + 10;
int gui_y = 15;
#if !defined(PLATFORM_WEB)
sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_ent)
{
CTransform_t* p_ct = get_component(p_ent, CTRANSFORM_COMP_T);
CJump_t* p_cjump = get_component(p_ent, CJUMP_COMP_T);
CPlayerState_t* p_pstate = get_component(p_ent, CPLAYERSTATE_T);
CMovementState_t* p_mstate = get_component(p_ent, CMOVEMENTSTATE_T);
CAirTimer_t* p_air = get_component(p_ent, CAIRTIMER_T);
sprintf(buffer, "Pos: %.3f\n %.3f", p_ct->position.x, p_ct->position.y);
DrawText(buffer, gui_x, gui_y, 12, BLACK);
sprintf(buffer, "Vel: %.3f\n %.3f", p_ct->velocity.x, p_ct->velocity.y);
DrawText(buffer, gui_x + 80, gui_y, 12, BLACK);
gui_y += 45;
sprintf(buffer, "Jumps: %u, %u, %u", p_cjump->jumps, p_cjump->jump_released, p_cjump->coyote_timer);
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
sprintf(buffer, "Crouch: %u", p_pstate->is_crouch);
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
sprintf(buffer, "Water: %s", p_mstate->water_state & 1? "YES":"NO");
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
sprintf(buffer, "Ladder: %u", p_pstate->ladder_state);
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
Vector2 air_pos = {data->game_rec.x + data->game_rec.width - 16, data->game_rec.y + data->game_rec.height - 16};
for (uint8_t i = 0; i < p_air->curr_count; i++)
{
DrawCircleV(air_pos, 16, BLUE);
air_pos.x -= 32;
}
}
#endif
//sprintf(buffer, "Spawn Entity: %s", get_spawn_selection_string(current_spawn_selection));
//DrawText(buffer, gui_x, 240, 12, BLACK);
sprintf(buffer, "Number of Entities: %u", sc_map_size_64v(&scene->ent_manager.entities));
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
sprintf(buffer, "FPS: %u", GetFPS());
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
print_mempool_stats(buffer);
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 300;
sprintf(buffer, "Chests: %u / %u", data->coins.current, data->coins.total);
DrawText(buffer, gui_x, gui_y, 24, BLACK);
}
static void render_editor_game_scene(Scene_t* scene)
{ {
LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data); LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data);
TileGrid_t tilemap = data->tilemap; TileGrid_t tilemap = data->tilemap;
@ -383,102 +483,6 @@ static void level_scene_render_func(Scene_t* scene)
} }
EndMode2D(); EndMode2D();
EndTextureMode(); EndTextureMode();
Rectangle draw_rec = data->game_rec;
draw_rec.x = 0;
draw_rec.y = 0;
draw_rec.height *= -1;
BeginDrawing();
ClearBackground(LIGHTGRAY);
DrawTextureRec(
data->game_viewport.texture,
draw_rec,
(Vector2){data->game_rec.x, data->game_rec.y},
WHITE
);
draw_rec.width = SELECTION_REGION_WIDTH;
draw_rec.height = -(SELECTION_REGION_HEIGHT * 2 + 10);
Vector2 draw_pos = {data->game_rec.x, data->game_rec.y + data->game_rec.height + SELECTION_GAP};
DrawTextureRec(
selection_section.texture,
draw_rec,
draw_pos,
WHITE
);
draw_pos.x = data->game_rec.x + current_spawn_selection * SELECTION_TILE_SIZE;
DrawRectangleLines(
draw_pos.x, draw_pos.y,
SELECTION_TILE_SIZE, SELECTION_TILE_SIZE, GREEN
);
draw_pos.x = data->game_rec.x + (MAX_SPAWN_TYPE + 1) * SELECTION_TILE_SIZE;
sprintf(buffer, "Selection: %s", get_spawn_selection_string(current_spawn_selection));
DrawText(buffer, draw_pos.x, draw_pos.y, 20, BLACK);
draw_pos.y += SELECTION_TILE_SIZE + 5;
sprintf(buffer, "Crate %s on spawn", crate_activation? "active" : "inactive");
DrawText(buffer, draw_pos.x, draw_pos.y, 20, BLACK);
// For DEBUG
const int gui_x = data->game_rec.x + data->game_rec.width + 10;
int gui_y = 15;
#if !defined(PLATFORM_WEB)
sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_ent)
{
CTransform_t* p_ct = get_component(p_ent, CTRANSFORM_COMP_T);
CJump_t* p_cjump = get_component(p_ent, CJUMP_COMP_T);
CPlayerState_t* p_pstate = get_component(p_ent, CPLAYERSTATE_T);
CMovementState_t* p_mstate = get_component(p_ent, CMOVEMENTSTATE_T);
CAirTimer_t* p_air = get_component(p_ent, CAIRTIMER_T);
sprintf(buffer, "Pos: %.3f\n %.3f", p_ct->position.x, p_ct->position.y);
DrawText(buffer, gui_x, gui_y, 12, BLACK);
sprintf(buffer, "Vel: %.3f\n %.3f", p_ct->velocity.x, p_ct->velocity.y);
DrawText(buffer, gui_x + 80, gui_y, 12, BLACK);
gui_y += 45;
sprintf(buffer, "Jumps: %u, %u, %u", p_cjump->jumps, p_cjump->jump_released, p_cjump->coyote_timer);
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
sprintf(buffer, "Crouch: %u", p_pstate->is_crouch);
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
sprintf(buffer, "Water: %s", p_mstate->water_state & 1? "YES":"NO");
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
sprintf(buffer, "Ladder: %u", p_pstate->ladder_state);
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
Vector2 air_pos = {data->game_rec.x + data->game_rec.width - 16, data->game_rec.y + data->game_rec.height - 16};
for (uint8_t i = 0; i < p_air->curr_count; i++)
{
DrawCircleV(air_pos, 16, BLUE);
air_pos.x -= 32;
}
}
#endif
//sprintf(buffer, "Spawn Entity: %s", get_spawn_selection_string(current_spawn_selection));
//DrawText(buffer, gui_x, 240, 12, BLACK);
sprintf(buffer, "Number of Entities: %u", sc_map_size_64v(&scene->ent_manager.entities));
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
sprintf(buffer, "FPS: %u", GetFPS());
DrawText(buffer, gui_x, gui_y, 12, BLACK);
gui_y += 30;
static char mempool_stats[512];
print_mempool_stats(mempool_stats);
DrawText(mempool_stats, gui_x, gui_y, 12, BLACK);
gui_y += 300;
sprintf(buffer, "Chests: %u / %u", data->coins.current, data->coins.total);
DrawText(buffer, gui_x, gui_y, 24, BLACK);
EndDrawing();
} }
static void spawn_chest(Scene_t* scene, unsigned int tile_idx) static void spawn_chest(Scene_t* scene, unsigned int tile_idx)
@ -1004,6 +1008,7 @@ void init_sandbox_scene(LevelScene_t* scene)
sc_array_add(&scene->scene.systems, &player_respawn_system); sc_array_add(&scene->scene.systems, &player_respawn_system);
sc_array_add(&scene->scene.systems, &level_end_detection_system); sc_array_add(&scene->scene.systems, &level_end_detection_system);
sc_array_add(&scene->scene.systems, &toggle_block_system); sc_array_add(&scene->scene.systems, &toggle_block_system);
sc_array_add(&scene->scene.systems, &render_editor_game_scene);
// This avoid graphical glitch, not essential // This avoid graphical glitch, not essential
//sc_array_add(&scene->scene.systems, &update_tilemap_system); //sc_array_add(&scene->scene.systems, &update_tilemap_system);

View File

@ -13,6 +13,92 @@ static Tile_t all_tiles[MAX_N_TILES] = {0};
static void level_scene_render_func(Scene_t* scene) static void level_scene_render_func(Scene_t* scene)
{ {
LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data); LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data);
Rectangle draw_rec = data->game_rec;
draw_rec.x = 0;
draw_rec.y = 0;
draw_rec.height *= -1;
static char buffer[512];
ClearBackground(LIGHTGRAY);
DrawTextureRec(
data->game_viewport.texture,
draw_rec,
(Vector2){data->game_rec.x, data->game_rec.y},
WHITE
);
// For DEBUG
const int gui_x = data->game_rec.x + data->game_rec.width + 10;
//sprintf(buffer, "Spawn Entity: %s", get_spawn_selection_string(current_spawn_selection));
//DrawText(buffer, gui_x, 240, 12, BLACK);
sprintf(buffer, "Number of Entities: %u", sc_map_size_64v(&scene->ent_manager.entities));
DrawText(buffer, gui_x, 270, 12, BLACK);
sprintf(buffer, "FPS: %u", GetFPS());
DrawText(buffer, gui_x, 320, 12, BLACK);
print_mempool_stats(buffer);
DrawText(buffer, gui_x, 350, 12, BLACK);
}
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;
case ACTION_JUMP:
p_playerstate->jump_pressed = pressed;
break;
default:
break;
}
}
if (!pressed)
{
switch (action)
{
case ACTION_RESTART:
reload_level_tilemap((LevelScene_t*)scene);
break;
case ACTION_NEXTLEVEL:
load_next_level_tilemap((LevelScene_t*)scene);
break;
case ACTION_PREVLEVEL:
load_prev_level_tilemap((LevelScene_t*)scene);
break;
case ACTION_EXIT:
if(scene->engine != NULL)
{
change_scene(scene->engine, 0);
}
break;
default:
break;
}
}
}
static void render_regular_game_scene(Scene_t* scene)
{
// This function will render the game scene outside of the intended draw function
// Just for clarity and separation of logic
LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data);
TileGrid_t tilemap = data->tilemap; TileGrid_t tilemap = data->tilemap;
Entity_t* p_ent; Entity_t* p_ent;
@ -31,7 +117,6 @@ static void level_scene_render_func(Scene_t* scene)
max.x = (int)fmin(tilemap.width, max.x + 1); max.x = (int)fmin(tilemap.width, max.x + 1);
max.y = (int)fmin(tilemap.height, max.y + 1); max.y = (int)fmin(tilemap.height, max.y + 1);
BeginTextureMode(data->game_viewport); BeginTextureMode(data->game_viewport);
ClearBackground(WHITE); ClearBackground(WHITE);
BeginMode2D(data->camera.cam); BeginMode2D(data->camera.cam);
@ -115,7 +200,6 @@ static void level_scene_render_func(Scene_t* scene)
} }
} }
char buffer[64] = {0};
sc_map_foreach_value(&scene->ent_manager.entities, p_ent) sc_map_foreach_value(&scene->ent_manager.entities, p_ent)
{ {
CTransform_t* p_ct = get_component(p_ent, CTRANSFORM_COMP_T); CTransform_t* p_ct = get_component(p_ent, CTRANSFORM_COMP_T);
@ -245,115 +329,6 @@ static void level_scene_render_func(Scene_t* scene)
DrawLine(0, val, tilemap.width * TILE_SIZE, val, BLACK); DrawLine(0, val, tilemap.width * TILE_SIZE, val, BLACK);
EndMode2D(); EndMode2D();
EndTextureMode(); EndTextureMode();
Rectangle draw_rec = data->game_rec;
draw_rec.x = 0;
draw_rec.y = 0;
draw_rec.height *= -1;
BeginDrawing();
ClearBackground(LIGHTGRAY);
DrawTextureRec(
data->game_viewport.texture,
draw_rec,
(Vector2){data->game_rec.x, data->game_rec.y},
WHITE
);
// For DEBUG
const int gui_x = data->game_rec.x + data->game_rec.width + 10;
sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_ent)
{
CTransform_t* p_ct = get_component(p_ent, CTRANSFORM_COMP_T);
CJump_t* p_cjump = get_component(p_ent, CJUMP_COMP_T);
CPlayerState_t* p_pstate = get_component(p_ent, CPLAYERSTATE_T);
CMovementState_t* p_mstate = get_component(p_ent, CMOVEMENTSTATE_T);
CAirTimer_t* p_air = get_component(p_ent, CAIRTIMER_T);
sprintf(buffer, "Pos: %.3f\n %.3f", p_ct->position.x, p_ct->position.y);
DrawText(buffer, gui_x, 15, 12, BLACK);
sprintf(buffer, "Vel: %.3f\n %.3f", p_ct->velocity.x, p_ct->velocity.y);
DrawText(buffer, gui_x + 80, 15, 12, BLACK);
//sprintf(buffer, "Accel: %.3f\n %.3f", p_ct->accel.x, p_ct->accel.y);
//DrawText(buffer, tilemap.width * TILE_SIZE + 128, 60, 12, BLACK);
sprintf(buffer, "Jumps: %u", p_cjump->jumps);
DrawText(buffer, gui_x, 60, 12, BLACK);
sprintf(buffer, "Crouch: %u", p_pstate->is_crouch);
DrawText(buffer, gui_x, 90, 12, BLACK);
sprintf(buffer, "Water: %s", p_mstate->water_state & 1? "YES":"NO");
DrawText(buffer, gui_x, 120, 12, BLACK);
sprintf(buffer, "Ladder: %u", p_pstate->ladder_state);
DrawText(buffer, gui_x, 150, 12, BLACK);
Vector2 air_pos = {data->game_rec.x + data->game_rec.width - 16, data->game_rec.y + data->game_rec.height - 16};
for (uint8_t i = 0; i < p_air->curr_count; i++)
{
DrawCircleV(air_pos, 16, BLUE);
air_pos.x -= 32;
}
}
//sprintf(buffer, "Spawn Entity: %s", get_spawn_selection_string(current_spawn_selection));
//DrawText(buffer, gui_x, 240, 12, BLACK);
sprintf(buffer, "Number of Entities: %u", sc_map_size_64v(&scene->ent_manager.entities));
DrawText(buffer, gui_x, 270, 12, BLACK);
sprintf(buffer, "FPS: %u", GetFPS());
DrawText(buffer, gui_x, 320, 12, BLACK);
static char mempool_stats[512];
print_mempool_stats(mempool_stats);
DrawText(mempool_stats, gui_x, 350, 12, BLACK);
EndDrawing();
}
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;
case ACTION_JUMP:
p_playerstate->jump_pressed = pressed;
break;
default:
break;
}
}
if (!pressed)
{
switch (action)
{
case ACTION_RESTART:
reload_level_tilemap((LevelScene_t*)scene);
break;
case ACTION_NEXTLEVEL:
load_next_level_tilemap((LevelScene_t*)scene);
break;
case ACTION_PREVLEVEL:
load_prev_level_tilemap((LevelScene_t*)scene);
break;
case ACTION_EXIT:
if(scene->engine != NULL)
{
change_scene(scene->engine, 0);
}
break;
default:
break;
}
}
} }
void init_game_scene(LevelScene_t* scene) void init_game_scene(LevelScene_t* scene)
@ -397,7 +372,7 @@ void init_game_scene(LevelScene_t* scene)
sc_array_add(&scene->scene.systems, &player_dir_reset_system); sc_array_add(&scene->scene.systems, &player_dir_reset_system);
sc_array_add(&scene->scene.systems, &player_respawn_system); sc_array_add(&scene->scene.systems, &player_respawn_system);
sc_array_add(&scene->scene.systems, &update_water_runner_system); sc_array_add(&scene->scene.systems, &update_water_runner_system);
sc_array_add(&scene->scene.systems, &render_regular_game_scene);
// This avoid graphical glitch, not essential // This avoid graphical glitch, not essential
//sc_array_add(&scene->scene.systems, &update_tilemap_system); //sc_array_add(&scene->scene.systems, &update_tilemap_system);

View File

@ -6,14 +6,12 @@
static void menu_scene_render_func(Scene_t* scene) static void menu_scene_render_func(Scene_t* scene)
{ {
MenuSceneData_t* data = &(CONTAINER_OF(scene, MenuScene_t, scene)->data); MenuSceneData_t* data = &(CONTAINER_OF(scene, MenuScene_t, scene)->data);
BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
DrawText("This is a game", 25, 220, 12, BLACK); DrawText("This is a game", 25, 220, 12, BLACK);
UI_button(data->buttons, "Start"); UI_button(data->buttons, "Start");
UI_button(data->buttons + 1, "Sandbox"); UI_button(data->buttons + 1, "Sandbox");
UI_button(data->buttons + 2, "Continue"); UI_button(data->buttons + 2, "Continue");
UI_button(data->buttons + 3, "Exit"); UI_button(data->buttons + 3, "Exit");
EndDrawing();
} }
static void exec_component_function(Scene_t* scene, int sel) static void exec_component_function(Scene_t* scene, int sel)