Compare commits

...

5 Commits

Author SHA1 Message Date
En Yi b17c521dfd Add emitter loading from RRES 2023-11-07 21:58:20 +08:00
En Yi d9b69aa09d Draw air meter in main game scene 2023-11-07 21:53:42 +08:00
En Yi d660b4e5a5 Add sound loading from rres file
Also, fixes mistake in raw data size when loading from RRES file
2023-11-07 21:49:15 +08:00
En Yi 4ede1abcc1 Fix crashes in main application
Changelog:
- Fix crash on empty emitter config
- Fix engine de-init order
- Update assets file to load for main application
2023-11-07 20:47:30 +08:00
En Yi 716393e9f4 Replace fmemopen when adding zst from rres
Changelog:
- Modify zst load function to accept buffer containing the zst data.
    - Manually manage the buffer reading
- Reading from zst file will load entire zst in memory first before
  passing into load function.
2023-11-07 20:46:05 +08:00
9 changed files with 147 additions and 53 deletions

View File

@ -93,8 +93,9 @@ Texture2D* add_texture_rres(Assets_t* assets, const char* name, const char* file
Texture2D* out_tex = NULL;
if (chunk.info.baseSize > 0)
{
uint32_t sz = chunk.info.baseSize - sizeof(int) - (chunk.data.propCount*sizeof(int));
//Expect RAW type of png extension
Image image = LoadImageFromMemory(GetFileExtension(filename), chunk.data.raw, chunk.info.baseSize);
Image image = LoadImageFromMemory(GetFileExtension(filename), chunk.data.raw, sz);
Texture2D tex = LoadTextureFromImage(image);
UnloadImage(image);
@ -108,6 +109,32 @@ Texture2D* add_texture_rres(Assets_t* assets, const char* name, const char* file
return out_tex;
}
Sound* add_sound_rres(Assets_t* assets, const char* name, const char* filename, const RresFileInfo_t* rres_file)
{
uint8_t snd_idx = n_loaded[2];
assert(snd_idx < MAX_SOUNDS);
int res_id = rresGetResourceId(rres_file->dir, filename);
rresResourceChunk chunk = rresLoadResourceChunk(rres_file->fname, res_id);
Sound* out_snd = NULL;
if (chunk.info.baseSize > 0)
{
uint32_t sz = chunk.info.baseSize - sizeof(int) - (chunk.data.propCount*sizeof(int));
Wave wave = LoadWaveFromMemory(GetFileExtension(filename), chunk.data.raw, sz);
Sound snd = LoadSoundFromWave(wave);
UnloadWave(wave);
sfx[snd_idx].sound = snd;
strncpy(sfx[snd_idx].name, name, MAX_NAME_LEN);
sc_map_put_s64(&assets->m_sounds, sfx[snd_idx].name, snd_idx);
n_loaded[2]++;
out_snd = &sfx[snd_idx].sound;
}
rresUnloadResourceChunk(chunk);
return out_snd;
}
Sprite_t* add_sprite(Assets_t* assets, const char* name, Texture2D* texture)
{
uint8_t spr_idx = n_loaded[1];
@ -183,7 +210,7 @@ LevelPack_t* add_level_pack(Assets_t* assets, const char* name, const char* path
}
static LevelPack_t* add_level_pack_zst(Assets_t* assets, const char* name, FILE* file)
static LevelPack_t* add_level_pack_zst(Assets_t* assets, const char* name, const uint8_t* zst_buffer, uint32_t len)
{
LevelPackData_t* pack_info = levelpacks + n_loaded[4];
size_t read = 0;
@ -197,8 +224,14 @@ static LevelPack_t* add_level_pack_zst(Assets_t* assets, const char* name, FILE*
if (input.pos == input.size)
{
puts("Read more");
read = fread(level_decompressor.in_buffer, 1, DECOMPRESSOR_INBUF_LEN, file);
read = (len < DECOMPRESSOR_INBUF_LEN) ? len : DECOMPRESSOR_INBUF_LEN;
memcpy(level_decompressor.in_buffer, zst_buffer, read);
zst_buffer += read;
len -= read;
if (read == 0) break;
input.size = read;
input.pos = 0;
}
@ -234,7 +267,11 @@ static LevelPack_t* add_level_pack_zst(Assets_t* assets, const char* name, FILE*
{
if (input.pos == input.size)
{
read = fread(level_decompressor.in_buffer, 1, DECOMPRESSOR_INBUF_LEN, file);
read = (len < DECOMPRESSOR_INBUF_LEN) ? len : DECOMPRESSOR_INBUF_LEN;
memcpy(level_decompressor.in_buffer, zst_buffer, read);
zst_buffer += read;
len -= read;
if (read == 0) break;
input.size = read;
input.pos = 0;
@ -273,7 +310,11 @@ static LevelPack_t* add_level_pack_zst(Assets_t* assets, const char* name, FILE*
{
if (input.pos == input.size)
{
read = fread(level_decompressor.in_buffer, 1, DECOMPRESSOR_INBUF_LEN, file);
read = (len < DECOMPRESSOR_INBUF_LEN) ? len : DECOMPRESSOR_INBUF_LEN;
memcpy(level_decompressor.in_buffer, zst_buffer, read);
zst_buffer += read;
len -= read;
if (read == 0) break;
input.size = read;
input.pos = 0;
@ -322,9 +363,22 @@ LevelPack_t* uncompress_level_pack(Assets_t* assets, const char* name, const cha
FILE* file = fopen(path, "rb");
if (file == NULL) return NULL;
LevelPack_t* pack = add_level_pack_zst(assets, name, file);
fseek(file, 0L, SEEK_END);
uint32_t sz = ftell(file);
rewind(file);
uint8_t* zst_buffer = malloc(sz);
if (zst_buffer == NULL)
{
fclose(file);
return NULL;
}
fread(zst_buffer, 1, sz, file);
fclose(file);
LevelPack_t* pack = add_level_pack_zst(assets, name, zst_buffer, sz);
free(zst_buffer);
return pack;
}
@ -337,9 +391,8 @@ LevelPack_t* add_level_pack_rres(Assets_t* assets, const char* name, const char*
LevelPack_t* pack = NULL;
if (chunk.info.baseSize > 0 && strcmp(".lpk", (const char*)(chunk.data.props + 1)) == 0)
{
FILE* f_in = fmemopen(chunk.data.raw, chunk.info.baseSize, "rb");
pack = add_level_pack_zst(assets, name, f_in);
fclose(f_in);
uint32_t sz = chunk.info.baseSize - sizeof(int) - (chunk.data.propCount*sizeof(int));
pack = add_level_pack_zst(assets, name, chunk.data.raw, sz);
}
else
{

View File

@ -57,6 +57,7 @@ LevelPack_t* uncompress_level_pack(Assets_t* assets, const char* name, const cha
// Rres version
Texture2D* add_texture_rres(Assets_t* assets, const char* name, const char* filename, const RresFileInfo_t* rres_file);
LevelPack_t* add_level_pack_rres(Assets_t* assets, const char* name, const char* filename, const RresFileInfo_t* rres_file);
Sound* add_sound_rres(Assets_t* assets, const char* name, const char* filename, const RresFileInfo_t* rres_file);
Sprite_t* add_sprite(Assets_t* assets, const char* name, Texture2D* texture);
EmitterConfig_t* add_emitter_conf(Assets_t* assets, const char* name);

View File

@ -3,6 +3,7 @@
void init_engine(GameEngine_t* engine)
{
InitAudioDevice();
sc_queue_init(&engine->key_buffer);
engine->sfx_list.n_sfx = N_SFX;
memset(engine->sfx_list.sfx, 0, engine->sfx_list.n_sfx * sizeof(SFX_t));
@ -15,6 +16,7 @@ void deinit_engine(GameEngine_t* engine)
term_assets(&engine->assets);
free_memory_pools();
sc_queue_term(&engine->key_buffer);
CloseAudioDevice();
}
void process_inputs(GameEngine_t* engine, Scene_t* scene)

View File

@ -20,6 +20,7 @@ void init_particle_system(ParticleSystem_t* system)
void play_particle_emitter(ParticleSystem_t* system, const ParticleEmitter_t* in_emitter)
{
if (in_emitter == NULL) return;
if (in_emitter->config == NULL) return;
if (sc_queue_empty(&system->free_list)) return;
uint32_t idx = sc_queue_del_first(&system->free_list);

11
main.c
View File

@ -28,7 +28,7 @@ int main(void)
InitWindow(screenWidth, screenHeight, "raylib");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
#ifndef NDEBUG
load_from_infofile("res/assets_debug.info", &engine.assets);
load_from_infofile("res/assets.info.raw", &engine.assets);
init_player_creation("res/player_spr.info", &engine.assets);
#else
load_from_rres("res/myresources.rres", &engine.assets);
@ -36,6 +36,12 @@ int main(void)
#endif
init_item_creation(&engine.assets);
load_sfx(&engine, "snd_jump", PLAYER_JMP_SFX);
load_sfx(&engine, "snd_land", PLAYER_LAND_SFX);
load_sfx(&engine, "snd_wdrop", WATER_IN_SFX);
load_sfx(&engine, "snd_bland", BOULDER_LAND_SFX);
load_sfx(&engine, "snd_bubble", BUBBLE_SFX);
LevelScene_t sandbox_scene;
sandbox_scene.scene.engine = &engine;
init_sandbox_scene(&sandbox_scene);
@ -82,6 +88,7 @@ int main(void)
update_entity_manager(&curr_scene->ent_manager);
// This is needed to advance time delta
render_scene(curr_scene);
update_sfx_list(&engine);
if (curr_scene->state != SCENE_PLAYING)
{
@ -92,6 +99,6 @@ int main(void)
free_sandbox_scene(&sandbox_scene);
free_game_scene(&level_scene);
free_menu_scene(&menu_scene);
CloseWindow();
deinit_engine(&engine);
CloseWindow();
}

View File

@ -230,6 +230,7 @@ typedef enum AssetInfoType
TEXTURE_INFO,
SPRITE_INFO,
LEVELPACK_INFO,
SOUND_INFO,
INVALID_INFO
}AssetInfoType_t;
@ -250,7 +251,7 @@ int main(void)
.reserved = 0 // <reserved>
};
#define STARTING_CHUNKS 8
#define STARTING_CHUNKS 64
// Central Directory
rresCentralDir central = {
.count = 0,
@ -307,6 +308,10 @@ int main(void)
{
info_type = LEVELPACK_INFO;
}
else if (strcmp(tmp, "Sound") == 0)
{
info_type = SOUND_INFO;
}
else
{
info_type = INVALID_INFO;
@ -337,6 +342,21 @@ int main(void)
}
}
break;
case SOUND_INFO:
{
// ---- OGG Sound
if (
addRawData(
&header, info_str,
rresFile, ".ogg",
central.entries + central.count
)
)
{
central.count++;
}
}
break;
case LEVELPACK_INFO:
{
// ---- Compressed Level Data

View File

@ -32,7 +32,6 @@ int main(void)
{
InitWindow(1280, 640, "raylib");
SetTargetFPS(60);
InitAudioDevice();
init_engine(&engine);
#ifndef NDEBUG

View File

@ -57,6 +57,21 @@ static bool parse_emitter_info(char* emitter_info_str, EmitterConfig_t* conf)
return data_count == 8;
}
static inline AssetInfoType_t get_asset_type(const char* str)
{
if (strcmp(str, "Texture") == 0) return TEXTURE_INFO;
if (strcmp(str, "Sprite") == 0) return SPRITE_INFO;
if (strcmp(str, "Sound") == 0) return SOUND_INFO;
if (strcmp(str, "Emitter") == 0) return EMITTER_INFO;
if (strcmp(str, "LevelPack") == 0) return LEVELPACK_INFO;
return INVALID_INFO;
}
bool load_from_rres(const char* file, Assets_t* assets)
{
RresFileInfo_t rres_file;
@ -91,22 +106,7 @@ bool load_from_rres(const char* file, Assets_t* assets)
if (tmp[0] == '-')
{
tmp++;
if (strcmp(tmp, "Texture") == 0)
{
info_type = TEXTURE_INFO;
}
else if (strcmp(tmp, "Sprite") == 0)
{
info_type = SPRITE_INFO;
}
else if (strcmp(tmp, "LevelPack") == 0)
{
info_type = LEVELPACK_INFO;
}
else
{
info_type = INVALID_INFO;
}
info_type = get_asset_type(tmp);
}
else
{
@ -141,6 +141,16 @@ bool load_from_rres(const char* file, Assets_t* assets)
printf("Added level pack %s as %s\n", info_str, name);
}
break;
case SOUND_INFO:
{
if (add_sound_rres(assets, name, info_str, &rres_file) == NULL)
{
printf("Unable to add sound at line %lu\n", line_num);
break;
}
printf("Added sound %s as %s\n", info_str, name);
}
break;
case SPRITE_INFO:
{
SpriteInfo_t spr_info = {0};
@ -164,6 +174,19 @@ bool load_from_rres(const char* file, Assets_t* assets)
spr->speed = spr_info.speed;
}
break;
case EMITTER_INFO:
{
EmitterConfig_t parsed_conf;
if (!parse_emitter_info(info_str, &parsed_conf))
{
printf("Parse error for emitter %s", name);
break;
}
EmitterConfig_t* conf = add_emitter_conf(assets, name);
*conf = parsed_conf;
printf("Added Emitter %s\n", name);
}
break;
default:
break;
}
@ -201,30 +224,7 @@ bool load_from_infofile(const char* file, Assets_t* assets)
if (tmp[0] == '-')
{
tmp++;
if (strcmp(tmp, "Texture") == 0)
{
info_type = TEXTURE_INFO;
}
else if (strcmp(tmp, "Sprite") == 0)
{
info_type = SPRITE_INFO;
}
else if (strcmp(tmp, "Sound") == 0)
{
info_type = SOUND_INFO;
}
else if (strcmp(tmp, "Emitter") == 0)
{
info_type = EMITTER_INFO;
}
else if (strcmp(tmp, "LevelPack") == 0)
{
info_type = LEVELPACK_INFO;
}
else
{
info_type = INVALID_INFO;
}
info_type = get_asset_type(tmp);
}
else
{

View File

@ -28,6 +28,17 @@ static void level_scene_render_func(Scene_t* scene)
WHITE
);
Entity_t* p_ent;
sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_ent)
{
CAirTimer_t* p_air = get_component(p_ent, CAIRTIMER_T);
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;
}
}
// 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));