Initial integration of audio to entity structure

This commit is contained in:
Doyle Thai 2016-07-27 18:39:51 +10:00
parent 54dd5c84fa
commit 824d8fd8a0
5 changed files with 85 additions and 56 deletions

View File

@ -106,6 +106,8 @@ const i32 audio_rendererInit(AudioRenderer *audioRenderer)
alGenBuffers(ARRAY_COUNT(audioRenderer->bufferId), audioRenderer->bufferId); alGenBuffers(ARRAY_COUNT(audioRenderer->bufferId), audioRenderer->bufferId);
AL_CHECK_ERROR(); AL_CHECK_ERROR();
//alSourcef(audioRenderer->sourceId[0], AL_PITCH, 2.0f);
return 0; return 0;
} }
@ -117,10 +119,14 @@ void audio_streamVorbis(AudioRenderer *audioRenderer, AudioVorbis *vorbis)
if (vorbis->info.channels == 2) if (vorbis->info.channels == 2)
audioRenderer->format = AL_FORMAT_STEREO16; audioRenderer->format = AL_FORMAT_STEREO16;
else if (vorbis->info.channels != 1) else if (vorbis->info.channels != 1)
{
#ifdef DENGINE_DEBUG
DEBUG_LOG("audio_streamVorbis() warning: Unaccounted channel format"); DEBUG_LOG("audio_streamVorbis() warning: Unaccounted channel format");
ASSERT(INVALID_CODE_PATH);
#endif
}
audioRenderer->audio = vorbis; audioRenderer->audio = vorbis;
AudioVorbis *audio = audioRenderer->audio;
} }
void audio_updateAndPlay(AudioRenderer *audioRenderer) void audio_updateAndPlay(AudioRenderer *audioRenderer)

View File

@ -2,6 +2,7 @@
#include "Dengine/Debug.h" #include "Dengine/Debug.h"
#include "Dengine/Platform.h" #include "Dengine/Platform.h"
#include "Dengine/Audio.h"
enum State enum State
{ {
@ -22,7 +23,7 @@ INTERNAL Entity *addEntity(MemoryArena *arena, World *world, v2 pos, v2 size,
{ {
#ifdef DENGINE_DEBUG #ifdef DENGINE_DEBUG
ASSERT(tex && world); ASSERT(world);
ASSERT(world->freeEntityIndex < world->maxEntities); ASSERT(world->freeEntityIndex < world->maxEntities);
ASSERT(type < entitytype_count); ASSERT(type < entitytype_count);
#endif #endif
@ -166,7 +167,11 @@ void worldTraveller_gameInit(GameState *state, v2 windowSize)
{ {
AssetManager *assetManager = &state->assetManager; AssetManager *assetManager = &state->assetManager;
MemoryArena *arena = &state->arena; MemoryArena *arena = &state->arena;
/* Initialise assets */ /*
*******************
* INITIALISE ASSETS
*******************
*/
/* Create empty 1x1 4bpp black texture */ /* Create empty 1x1 4bpp black texture */
u32 bitmap = (0xFF << 24) | (0xFF << 16) | (0xFF << 8) | (0xFF << 0); u32 bitmap = (0xFF << 24) | (0xFF << 16) | (0xFF << 8) | (0xFF << 0);
Texture emptyTex = texture_gen(1, 1, 4, CAST(u8 *)(&bitmap)); 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"); DEBUG_LOG("Animations created");
#endif #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->state = state_active;
state->currWorldIndex = 0; state->currWorldIndex = 0;
@ -325,16 +340,31 @@ void worldTraveller_gameInit(GameState *state, v2 windowSize)
World *const world = &state->world[state->currWorldIndex]; World *const world = &state->world[state->currWorldIndex];
world->cameraPos = V2(0.0f, 0.0f); 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 */ /* Init hero entity */
world->heroIndex = world->freeEntityIndex; world->heroIndex = world->freeEntityIndex;
Renderer *renderer = &state->renderer; size = V2(58.0f, 98.0f);
v2 size = V2(58.0f, 98.0f); pos = V2(size.x, CAST(f32) state->tileSize);
v2 pos = V2(size.x, CAST(f32) state->tileSize); type = entitytype_hero;
enum EntityType type = entitytype_hero; dir = direction_east;
enum Direction dir = direction_east; tex = asset_getTexture(assetManager, texlist_hero);
Texture *tex = asset_getTexture(assetManager, texlist_hero); collides = TRUE;
b32 collides = TRUE;
Entity *hero = addEntity(arena, world, pos, size, type, dir, tex, collides); Entity *hero = addEntity(arena, world, pos, size, type, dir, tex, collides);
/* Populate hero animation references */ /* Populate hero animation references */
@ -549,11 +579,13 @@ INTERNAL void parseInput(GameState *state, const f32 dt)
INTERNAL void updateEntityAnim(Entity *entity, f32 dt) INTERNAL void updateEntityAnim(Entity *entity, f32 dt)
{ {
if (!entity->tex) return;
// TODO(doyle): Recheck why we have this twice // TODO(doyle): Recheck why we have this twice
EntityAnim_ *entityAnim = &entity->anim[entity->currAnimId]; EntityAnim_ *entityAnim = &entity->anim[entity->currAnimId];
Animation anim = *entityAnim->anim; Animation anim = *entityAnim->anim;
i32 frameIndex = anim.frameIndex[entityAnim->currFrame]; i32 frameIndex = anim.frameIndex[entityAnim->currFrame];
v4 texRect = anim.atlas->texRect[frameIndex]; v4 texRect = anim.atlas->texRect[frameIndex];
entityAnim->currDuration -= dt; entityAnim->currDuration -= dt;
if (entityAnim->currDuration <= 0.0f) if (entityAnim->currDuration <= 0.0f)
@ -879,7 +911,6 @@ INTERNAL void entityStateSwitch(World *world, Entity *entity,
entity->state = newState; entity->state = newState;
} }
void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt) void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt)
{ {
if (dt >= 1.0f) dt = 1.0f; 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++) for (i32 i = 0; i < world->freeEntityIndex; i++)
{ {
Entity *const entity = &world->entities[i]; Entity *const entity = &world->entities[i];
if (entity->audio)
{
audio_updateAndPlay(entity->audio);
}
if (entity->state == entitystate_dead) if (entity->state == entitystate_dead)
{ {
#ifdef DENGINE_DEBUG #ifdef DENGINE_DEBUG
@ -1030,9 +1067,13 @@ void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt)
* Update animations and render entity * Update animations and render entity
************************************************** **************************************************
*/ */
updateEntityAnim(entity, dt); if (entity->tex)
/* Calculate region to render */ {
renderer_entity(renderer, cameraBounds, entity, V2(0, 0), 0, V4(1, 1, 1, 1)); 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 // 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)); V4(1, 0, 0, 1.0f));
} }
} }
#ifdef DENGINE_DEBUG #ifdef DENGINE_DEBUG
debug_drawUi(state, dt); debug_drawUi(state, dt);
#endif #endif

View File

@ -93,6 +93,19 @@ int main()
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glCullFace(GL_BACK); glCullFace(GL_BACK);
/*
*******************
* INITIALISE AUDIO
*******************
*/
i32 result = audio_init();
if (result)
{
#ifdef DENGINE_DEBUG
ASSERT(INVALID_CODE_PATH);
#endif
}
/* /*
******************* *******************
* INITIALISE GAME * INITIALISE GAME
@ -108,40 +121,6 @@ int main()
glfwSetWindowUserPointer(window, CAST(void *)(&worldTraveller)); 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 * GAME LOOP
@ -172,8 +151,6 @@ int main()
worldTraveller_gameUpdateAndRender(&worldTraveller, secondsElapsed); worldTraveller_gameUpdateAndRender(&worldTraveller, secondsElapsed);
GL_CHECK_ERROR(); GL_CHECK_ERROR();
audio_updateAndPlay(&audioRenderer);
//audio_updateAndPlay(&overworldAudioRenderer);
/* Swap the buffers */ /* Swap the buffers */
glfwSwapBuffers(window); glfwSwapBuffers(window);

View File

@ -5,14 +5,16 @@
#include "Dengine/Common.h" #include "Dengine/Common.h"
typedef struct AudioRenderer struct AudioRenderer
{ {
ALuint sourceId[1]; ALuint sourceId[1];
ALuint bufferId[4]; ALuint bufferId[4];
AudioVorbis *audio; AudioVorbis *audio;
ALuint format; ALuint format;
} AudioRenderer; };
typedef struct AudioRenderer AudioRenderer;
const i32 audio_init(); const i32 audio_init();
const i32 audio_rendererInit(AudioRenderer *audioRenderer); const i32 audio_rendererInit(AudioRenderer *audioRenderer);

View File

@ -5,6 +5,8 @@
#include "Dengine/Math.h" #include "Dengine/Math.h"
#include "Dengine/Texture.h" #include "Dengine/Texture.h"
typedef struct AudioRenderer AudioRenderer;
enum Direction enum Direction
{ {
direction_north, direction_north,
@ -22,6 +24,7 @@ enum EntityType
entitytype_npc, entitytype_npc,
entitytype_mob, entitytype_mob,
entitytype_tile, entitytype_tile,
entitytype_soundscape,
entitytype_count, entitytype_count,
}; };
@ -88,6 +91,7 @@ typedef struct Entity
enum AnimList currAnimId; enum AnimList currAnimId;
EntityStats *stats; EntityStats *stats;
AudioRenderer *audio;
} Entity; } Entity;
#endif #endif