Add sound loading from rres file

Also, fixes mistake in raw data size when loading from RRES file
scene_man
En Yi 2023-11-07 21:49:15 +08:00
parent 4ede1abcc1
commit d660b4e5a5
7 changed files with 88 additions and 44 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];
@ -364,7 +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)
{
pack = add_level_pack_zst(assets, name, chunk.data.raw, chunk.info.baseSize);
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)

7
main.c
View File

@ -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)
{

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};
@ -201,30 +211,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
{