diff --git a/src/Audio.c b/src/Audio.c index afd0590..3a304f6 100644 --- a/src/Audio.c +++ b/src/Audio.c @@ -106,6 +106,8 @@ const i32 audio_rendererInit(AudioRenderer *audioRenderer) alGenBuffers(ARRAY_COUNT(audioRenderer->bufferId), audioRenderer->bufferId); AL_CHECK_ERROR(); + //alSourcef(audioRenderer->sourceId[0], AL_PITCH, 2.0f); + return 0; } @@ -117,10 +119,14 @@ void audio_streamVorbis(AudioRenderer *audioRenderer, AudioVorbis *vorbis) if (vorbis->info.channels == 2) audioRenderer->format = AL_FORMAT_STEREO16; else if (vorbis->info.channels != 1) + { +#ifdef DENGINE_DEBUG DEBUG_LOG("audio_streamVorbis() warning: Unaccounted channel format"); + ASSERT(INVALID_CODE_PATH); +#endif + } audioRenderer->audio = vorbis; - AudioVorbis *audio = audioRenderer->audio; } void audio_updateAndPlay(AudioRenderer *audioRenderer) diff --git a/src/WorldTraveller.c b/src/WorldTraveller.c index 57b9095..0c5c239 100644 --- a/src/WorldTraveller.c +++ b/src/WorldTraveller.c @@ -2,6 +2,7 @@ #include "Dengine/Debug.h" #include "Dengine/Platform.h" +#include "Dengine/Audio.h" enum State { @@ -22,7 +23,7 @@ INTERNAL Entity *addEntity(MemoryArena *arena, World *world, v2 pos, v2 size, { #ifdef DENGINE_DEBUG - ASSERT(tex && world); + ASSERT(world); ASSERT(world->freeEntityIndex < world->maxEntities); ASSERT(type < entitytype_count); #endif @@ -166,7 +167,11 @@ void worldTraveller_gameInit(GameState *state, v2 windowSize) { AssetManager *assetManager = &state->assetManager; MemoryArena *arena = &state->arena; - /* Initialise assets */ + /* + ******************* + * INITIALISE ASSETS + ******************* + */ /* Create empty 1x1 4bpp black texture */ u32 bitmap = (0xFF << 24) | (0xFF << 16) | (0xFF << 8) | (0xFF << 0); Texture emptyTex = texture_gen(1, 1, 4, CAST(u8 *)(&bitmap)); @@ -263,6 +268,16 @@ void worldTraveller_gameInit(GameState *state, v2 windowSize) DEBUG_LOG("Animations created"); #endif + /* Load sound */ + + char *audioPath = "data/audio/Nobuo Uematsu - Battle 1.ogg"; + asset_loadVorbis(assetManager, arena, audioPath, audiolist_battle); + audioPath = "data/audio/Yuki Kajiura - Swordland.ogg"; + asset_loadVorbis(assetManager, arena, audioPath, audiolist_overworld); + +#ifdef DENGINE_DEBUG + DEBUG_LOG("Sound assets initialised"); +#endif state->state = state_active; state->currWorldIndex = 0; @@ -325,16 +340,31 @@ void worldTraveller_gameInit(GameState *state, v2 windowSize) World *const world = &state->world[state->currWorldIndex]; world->cameraPos = V2(0.0f, 0.0f); + /* Add world soundscape */ + Renderer *renderer = &state->renderer; + v2 size = V2(10.0f, 10.0f); + v2 pos = V2(0, 0); + enum EntityType type = entitytype_soundscape; + enum Direction dir = direction_null; + Texture *tex = NULL; + b32 collides = FALSE; + Entity *soundscape = + addEntity(arena, world, pos, size, type, dir, tex, collides); + + soundscape->audio = PLATFORM_MEM_ALLOC(arena, 1, AudioRenderer); + audio_rendererInit(soundscape->audio); + audio_streamVorbis(soundscape->audio, + asset_getVorbis(assetManager, audiolist_battle)); + /* Init hero entity */ world->heroIndex = world->freeEntityIndex; - Renderer *renderer = &state->renderer; - v2 size = V2(58.0f, 98.0f); - v2 pos = V2(size.x, CAST(f32) state->tileSize); - enum EntityType type = entitytype_hero; - enum Direction dir = direction_east; - Texture *tex = asset_getTexture(assetManager, texlist_hero); - b32 collides = TRUE; + size = V2(58.0f, 98.0f); + pos = V2(size.x, CAST(f32) state->tileSize); + type = entitytype_hero; + dir = direction_east; + tex = asset_getTexture(assetManager, texlist_hero); + collides = TRUE; Entity *hero = addEntity(arena, world, pos, size, type, dir, tex, collides); /* Populate hero animation references */ @@ -549,11 +579,13 @@ INTERNAL void parseInput(GameState *state, const f32 dt) INTERNAL void updateEntityAnim(Entity *entity, f32 dt) { + if (!entity->tex) return; + // TODO(doyle): Recheck why we have this twice EntityAnim_ *entityAnim = &entity->anim[entity->currAnimId]; - Animation anim = *entityAnim->anim; - i32 frameIndex = anim.frameIndex[entityAnim->currFrame]; - v4 texRect = anim.atlas->texRect[frameIndex]; + Animation anim = *entityAnim->anim; + i32 frameIndex = anim.frameIndex[entityAnim->currFrame]; + v4 texRect = anim.atlas->texRect[frameIndex]; entityAnim->currDuration -= dt; if (entityAnim->currDuration <= 0.0f) @@ -879,7 +911,6 @@ INTERNAL void entityStateSwitch(World *world, Entity *entity, entity->state = newState; } - void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt) { if (dt >= 1.0f) dt = 1.0f; @@ -903,6 +934,12 @@ void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt) for (i32 i = 0; i < world->freeEntityIndex; i++) { Entity *const entity = &world->entities[i]; + + if (entity->audio) + { + audio_updateAndPlay(entity->audio); + } + if (entity->state == entitystate_dead) { #ifdef DENGINE_DEBUG @@ -1030,9 +1067,13 @@ void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt) * Update animations and render entity ************************************************** */ - updateEntityAnim(entity, dt); - /* Calculate region to render */ - renderer_entity(renderer, cameraBounds, entity, V2(0, 0), 0, V4(1, 1, 1, 1)); + if (entity->tex) + { + updateEntityAnim(entity, dt); + /* Calculate region to render */ + renderer_entity(renderer, cameraBounds, entity, V2(0, 0), 0, + V4(1, 1, 1, 1)); + } } // TODO(doyle): Dead hero not accounted for here @@ -1104,7 +1145,6 @@ void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt) V4(1, 0, 0, 1.0f)); } } - #ifdef DENGINE_DEBUG debug_drawUi(state, dt); #endif diff --git a/src/dengine.c b/src/dengine.c index 39a3a41..003def7 100644 --- a/src/dengine.c +++ b/src/dengine.c @@ -93,6 +93,19 @@ int main() glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glCullFace(GL_BACK); + /* + ******************* + * INITIALISE AUDIO + ******************* + */ + i32 result = audio_init(); + if (result) + { +#ifdef DENGINE_DEBUG + ASSERT(INVALID_CODE_PATH); +#endif + } + /* ******************* * INITIALISE GAME @@ -108,40 +121,6 @@ int main() glfwSetWindowUserPointer(window, CAST(void *)(&worldTraveller)); - /* - ******************* - * INITIALISE AUDIO - ******************* - */ - i32 result = audio_init(); - if (result) - { -#ifdef DENGINE_DEBUG - ASSERT(INVALID_CODE_PATH); -#endif - } - - AudioRenderer audioRenderer = {0}; - audio_rendererInit(&audioRenderer); - - /* Load audio assets */ - char *audioPath = "data/audio/Nobuo Uematsu - Battle 1.ogg"; - asset_loadVorbis(&worldTraveller.assetManager, &worldTraveller.arena, - audioPath, audiolist_battle); - audioPath = "data/audio/Yuki Kajiura - Swordland.ogg"; - asset_loadVorbis(&worldTraveller.assetManager, &worldTraveller.arena, - audioPath, audiolist_overworld); - AudioVorbis *audio = - asset_getVorbis(&worldTraveller.assetManager, audiolist_battle); - - audio_streamVorbis(&audioRenderer, audio); - - AudioRenderer overworldAudioRenderer = {0}; - audio_rendererInit(&overworldAudioRenderer); - AudioVorbis *overworldAudio = - asset_getVorbis(&worldTraveller.assetManager, audiolist_overworld); - audio_streamVorbis(&overworldAudioRenderer, overworldAudio); - /* ******************* * GAME LOOP @@ -172,8 +151,6 @@ int main() worldTraveller_gameUpdateAndRender(&worldTraveller, secondsElapsed); GL_CHECK_ERROR(); - audio_updateAndPlay(&audioRenderer); - //audio_updateAndPlay(&overworldAudioRenderer); /* Swap the buffers */ glfwSwapBuffers(window); diff --git a/src/include/Dengine/Audio.h b/src/include/Dengine/Audio.h index a06e4b7..81f9ce6 100644 --- a/src/include/Dengine/Audio.h +++ b/src/include/Dengine/Audio.h @@ -5,14 +5,16 @@ #include "Dengine/Common.h" -typedef struct AudioRenderer +struct AudioRenderer { ALuint sourceId[1]; ALuint bufferId[4]; AudioVorbis *audio; ALuint format; -} AudioRenderer; +}; + +typedef struct AudioRenderer AudioRenderer; const i32 audio_init(); const i32 audio_rendererInit(AudioRenderer *audioRenderer); diff --git a/src/include/Dengine/Entity.h b/src/include/Dengine/Entity.h index 7db3ffc..ed1ea5e 100644 --- a/src/include/Dengine/Entity.h +++ b/src/include/Dengine/Entity.h @@ -5,6 +5,8 @@ #include "Dengine/Math.h" #include "Dengine/Texture.h" +typedef struct AudioRenderer AudioRenderer; + enum Direction { direction_north, @@ -22,6 +24,7 @@ enum EntityType entitytype_npc, entitytype_mob, entitytype_tile, + entitytype_soundscape, entitytype_count, }; @@ -88,6 +91,7 @@ typedef struct Entity enum AnimList currAnimId; EntityStats *stats; + AudioRenderer *audio; } Entity; #endif