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);
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)

View File

@ -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,6 +579,8 @@ 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;
@ -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
**************************************************
*/
if (entity->tex)
{
updateEntityAnim(entity, dt);
/* Calculate region to render */
renderer_entity(renderer, cameraBounds, entity, V2(0, 0), 0, V4(1, 1, 1, 1));
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

View File

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

View File

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

View File

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