-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
92,613 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,107 @@ | ||
#include "asset_manager.h" | ||
|
||
//#include "../data_structures/se_hash_map_string.h" | ||
//#include "../rendering/texture.h" | ||
//#include "../rendering/font.h" | ||
//#include "../audio/audio.h" | ||
//#include "../memory/se_mem.h" | ||
//#include "../utils/se_assert.h" | ||
// | ||
//#define INITIAL_ASSET_HASHMAP_SIZE 8 | ||
// | ||
//SEStringHashMap* texturesMap = NULL; | ||
//SEStringHashMap* fontMap = NULL; | ||
//SEStringHashMap* audioSourceMap = NULL; | ||
// | ||
//void se_asset_manager_initialize() { | ||
// texturesMap = se_string_hash_map_create(INITIAL_ASSET_HASHMAP_SIZE); | ||
// fontMap = se_string_hash_map_create(INITIAL_ASSET_HASHMAP_SIZE); | ||
// audioSourceMap = se_string_hash_map_create(INITIAL_ASSET_HASHMAP_SIZE); | ||
//} | ||
// | ||
//void se_asset_manager_finalize() { | ||
// se_string_hash_map_destroy(texturesMap); | ||
// se_string_hash_map_destroy(fontMap); | ||
// se_string_hash_map_destroy(audioSourceMap); | ||
//} | ||
// | ||
//// --- Texture --- // | ||
//SkaTexture* se_asset_manager_load_texture(const char* fileName, const char* key) { | ||
// SE_ASSERT(texturesMap != NULL); | ||
// SE_ASSERT_FMT(!se_string_hash_map_has(texturesMap, fileName), "Already loaded texture at file path '%'s! Has key '%s'.", fileName, key); | ||
// SkaTexture* texture = ska_texture_create_texture(fileName); | ||
// se_string_hash_map_add(texturesMap, key, texture, sizeof(SkaTexture)); | ||
// SE_MEM_FREE(texture); | ||
// texture = (SkaTexture*) se_string_hash_map_get(texturesMap, key); | ||
// return texture; | ||
//} | ||
// | ||
//SkaTexture* se_asset_manager_load_texture_ex(const char* fileName, const char* key, const char* wrap_s, const char* wrap_t, bool applyNearestNeighbor) { | ||
// SE_ASSERT(texturesMap != NULL); | ||
// SE_ASSERT_FMT(!se_string_hash_map_has(texturesMap, fileName), "Already loaded texture at file path '%'s! Has key '%s'.", fileName, key); | ||
// SkaTexture* texture = ska_texture_create_texture2( | ||
// fileName, | ||
// ska_texture_wrap_string_to_int(wrap_s), | ||
// ska_texture_wrap_string_to_int(wrap_t), | ||
// applyNearestNeighbor | ||
// ); | ||
// se_string_hash_map_add(texturesMap, key, texture, sizeof(SkaTexture)); | ||
// SE_MEM_FREE(texture); | ||
// texture = (SkaTexture*) se_string_hash_map_get(texturesMap, key); | ||
// return texture; | ||
//} | ||
// | ||
//SkaTexture* se_asset_manager_get_texture(const char* key) { | ||
// return (SkaTexture*) se_string_hash_map_get(texturesMap, key); | ||
//} | ||
// | ||
//bool se_asset_manager_has_texture(const char* key) { | ||
// return se_string_hash_map_has(texturesMap, key); | ||
//} | ||
// | ||
//// --- Font --- // | ||
//SkaFont* se_asset_manager_load_font(const char* fileName, const char* key, int size, bool applyNearestNeighbor) { | ||
// SE_ASSERT_FMT(!se_asset_manager_has_font(key), "Font key '%s' already exists!", key); | ||
// SkaFont* font = ska_font_create_font(fileName, size, applyNearestNeighbor); | ||
// SE_ASSERT_FMT(font != NULL, "Failed to load font! file_name: '%s', key: '%s', size: '%d'", fileName, key, size); | ||
// se_string_hash_map_add(fontMap, key, font, sizeof(SkaFont)); | ||
// SE_MEM_FREE(font); | ||
// font = (SkaFont*) se_string_hash_map_get(fontMap, key); | ||
// return font; | ||
//} | ||
// | ||
//SkaFont* se_asset_manager_load_font_from_memory(const char* key, void* buffer, size_t bufferSize, int size, bool applyNearestNeighbor) { | ||
// SE_ASSERT_FMT(!se_asset_manager_has_font(key), "Font key '%s' already exists!", key); | ||
// SkaFont* font = ska_font_create_font_from_memory(buffer, bufferSize, size, applyNearestNeighbor); | ||
// SE_ASSERT_FMT(font != NULL, "Failed to load font! key: '%s', size: '%d'", key, size); | ||
// se_string_hash_map_add(fontMap, key, font, sizeof(SkaFont)); | ||
// SE_MEM_FREE(font); | ||
// font = (SkaFont*) se_string_hash_map_get(fontMap, key); | ||
// return font; | ||
//} | ||
// | ||
// | ||
//SkaFont* se_asset_manager_get_font(const char* key) { | ||
// return (SkaFont*) se_string_hash_map_get(fontMap, key); | ||
//} | ||
// | ||
//bool se_asset_manager_has_font(const char* key) { | ||
// return se_string_hash_map_has(fontMap, key); | ||
//} | ||
// | ||
//// --- Audio Source --- // | ||
//SEAudioSource* se_asset_manager_load_audio_source_wav(const char* fileName, const char* key) { | ||
// SE_ASSERT(audioSourceMap != NULL); | ||
// SE_ASSERT_FMT(!se_string_hash_map_has(audioSourceMap, fileName), "Already loaded audio source at file path '%'s! Has key '%s'.", fileName, key); | ||
// SEAudioSource* newAudioSource = se_audio_load_audio_source_wav(fileName); | ||
// SE_ASSERT_FMT(newAudioSource != NULL, "Audio source is null! file_name = '%s', key = '%s'", fileName, key); | ||
// se_string_hash_map_add(audioSourceMap, key, newAudioSource, sizeof(SEAudioSource)); | ||
// SE_MEM_FREE(newAudioSource); | ||
// return newAudioSource; | ||
//} | ||
// | ||
//SEAudioSource* se_asset_manager_get_audio_source(const char* key) { | ||
// return (SEAudioSource*) se_string_hash_map_get(audioSourceMap, key); | ||
//} | ||
// | ||
//bool se_asset_manager_has_audio_source(const char* key) { | ||
// return se_string_hash_map_has(audioSourceMap, key); | ||
//} | ||
#include "seika/memory.h" | ||
#include "seika/assert.h" | ||
#include "seika/data_structures/hash_map_string.h" | ||
#include "seika/rendering/texture.h" | ||
#include "seika/rendering/font.h" | ||
#include "seika/audio/audio.h" | ||
|
||
static SkaStringHashMap* texturesMap = NULL; | ||
static SkaStringHashMap* fontMap = NULL; | ||
static SkaStringHashMap* audioSourceMap = NULL; | ||
|
||
void ska_asset_manager_initialize() { | ||
texturesMap = ska_string_hash_map_create_default_capacity(); | ||
fontMap = ska_string_hash_map_create_default_capacity(); | ||
audioSourceMap = ska_string_hash_map_create_default_capacity(); | ||
} | ||
|
||
void ska_asset_manager_finalize() { | ||
ska_string_hash_map_destroy(texturesMap); | ||
ska_string_hash_map_destroy(fontMap); | ||
ska_string_hash_map_destroy(audioSourceMap); | ||
} | ||
|
||
// --- Texture --- // | ||
SkaTexture* ska_asset_manager_load_texture(const char* fileName, const char* key) { | ||
SKA_ASSERT(texturesMap != NULL); | ||
SKA_ASSERT_FMT(!ska_string_hash_map_has(texturesMap, fileName), "Already loaded texture at file path '%'s! Has key '%s'.", fileName, key); | ||
SkaTexture* texture = ska_texture_create_texture(fileName); | ||
ska_string_hash_map_add(texturesMap, key, texture, sizeof(SkaTexture)); | ||
SKA_MEM_FREE(texture); | ||
texture = (SkaTexture*) ska_string_hash_map_get(texturesMap, key); | ||
return texture; | ||
} | ||
|
||
SkaTexture* ska_asset_manager_load_texture_ex(const char* fileName, const char* key, const char* wrap_s, const char* wrap_t, bool applyNearestNeighbor) { | ||
SKA_ASSERT(texturesMap != NULL); | ||
SKA_ASSERT_FMT(!ska_string_hash_map_has(texturesMap, fileName), "Already loaded texture at file path '%'s! Has key '%s'.", fileName, key); | ||
SkaTexture* texture = ska_texture_create_texture2( | ||
fileName, | ||
ska_texture_wrap_string_to_int(wrap_s), | ||
ska_texture_wrap_string_to_int(wrap_t), | ||
applyNearestNeighbor | ||
); | ||
ska_string_hash_map_add(texturesMap, key, texture, sizeof(SkaTexture)); | ||
SKA_MEM_FREE(texture); | ||
texture = (SkaTexture*) ska_string_hash_map_get(texturesMap, key); | ||
return texture; | ||
} | ||
|
||
SkaTexture* ska_asset_manager_get_texture(const char* key) { | ||
return (SkaTexture*) ska_string_hash_map_get(texturesMap, key); | ||
} | ||
|
||
bool ska_asset_manager_has_texture(const char* key) { | ||
return ska_string_hash_map_has(texturesMap, key); | ||
} | ||
|
||
// --- Font --- // | ||
SkaFont* ska_asset_manager_load_font(const char* fileName, const char* key, int size, bool applyNearestNeighbor) { | ||
SKA_ASSERT_FMT(!ska_asset_manager_has_font(key), "Font key '%s' already exists!", key); | ||
SkaFont* font = ska_font_create_font(fileName, size, applyNearestNeighbor); | ||
SKA_ASSERT_FMT(font != NULL, "Failed to load font! file_name: '%s', key: '%s', size: '%d'", fileName, key, size); | ||
ska_string_hash_map_add(fontMap, key, font, sizeof(SkaFont)); | ||
SKA_MEM_FREE(font); | ||
font = (SkaFont*) ska_string_hash_map_get(fontMap, key); | ||
return font; | ||
} | ||
|
||
SkaFont* ska_asset_manager_load_font_from_memory(const char* key, void* buffer, size_t bufferSize, int size, bool applyNearestNeighbor) { | ||
SKA_ASSERT_FMT(!ska_asset_manager_has_font(key), "Font key '%s' already exists!", key); | ||
SkaFont* font = ska_font_create_font_from_memory(buffer, bufferSize, size, applyNearestNeighbor); | ||
SKA_ASSERT_FMT(font != NULL, "Failed to load font! key: '%s', size: '%d'", key, size); | ||
ska_string_hash_map_add(fontMap, key, font, sizeof(SkaFont)); | ||
SKA_MEM_FREE(font); | ||
font = (SkaFont*) ska_string_hash_map_get(fontMap, key); | ||
return font; | ||
} | ||
|
||
|
||
SkaFont* ska_asset_manager_get_font(const char* key) { | ||
return (SkaFont*) ska_string_hash_map_get(fontMap, key); | ||
} | ||
|
||
bool ska_asset_manager_has_font(const char* key) { | ||
return ska_string_hash_map_has(fontMap, key); | ||
} | ||
|
||
// --- Audio Source --- // | ||
SkaAudioSource* ska_asset_manager_load_audio_source_wav(const char* fileName, const char* key) { | ||
SKA_ASSERT(audioSourceMap != NULL); | ||
SKA_ASSERT_FMT(!ska_string_hash_map_has(audioSourceMap, fileName), "Already loaded audio source at file path '%'s! Has key '%s'.", fileName, key); | ||
SkaAudioSource* newAudioSource = ska_audio_load_audio_source_wav(fileName); | ||
SKA_ASSERT_FMT(newAudioSource != NULL, "Audio source is null! file_name = '%s', key = '%s'", fileName, key); | ||
ska_string_hash_map_add(audioSourceMap, key, newAudioSource, sizeof(SkaAudioSource)); | ||
SKA_MEM_FREE(newAudioSource); | ||
return newAudioSource; | ||
} | ||
|
||
SkaAudioSource* ska_asset_manager_get_audio_source(const char* key) { | ||
return (SkaAudioSource*) ska_string_hash_map_get(audioSourceMap, key); | ||
} | ||
|
||
bool ska_asset_manager_has_audio_source(const char* key) { | ||
return ska_string_hash_map_has(audioSourceMap, key); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
#pragma once | ||
|
||
//#ifdef __cplusplus | ||
//extern "C" { | ||
//#endif | ||
// | ||
//#include <stddef.h> | ||
//#include <stdbool.h> | ||
// | ||
//void se_asset_manager_initialize(); | ||
//void se_asset_manager_finalize(); | ||
//// --- Texture --- // | ||
//struct SkaTexture* se_asset_manager_load_texture(const char* fileName, const char* key); | ||
//struct SkaTexture* se_asset_manager_load_texture_ex(const char* fileName, const char* key, const char* wrap_s, const char* wrap_t, bool applyNearestNeighbor); | ||
//struct SkaTexture* se_asset_manager_get_texture(const char* key); | ||
//bool se_asset_manager_has_texture(const char* key); | ||
//// --- Font --- // | ||
//struct SkaFont* se_asset_manager_load_font(const char* fileName, const char* key, int size, bool applyNearestNeighbor); | ||
//struct SkaFont* se_asset_manager_load_font_from_memory(const char* key, void* buffer, size_t bufferSize, int size, bool applyNearestNeighbor); | ||
//struct SkaFont* se_asset_manager_get_font(const char* key); | ||
//bool se_asset_manager_has_font(const char* key); | ||
//// -- Audio Source --- // | ||
//struct SEAudioSource* se_asset_manager_load_audio_source_wav(const char* fileName, const char* key); | ||
//struct SEAudioSource* se_asset_manager_get_audio_source(const char* key); | ||
//bool se_asset_manager_has_audio_source(const char* key); | ||
// | ||
//#ifdef __cplusplus | ||
//} | ||
//#endif | ||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include <stddef.h> | ||
#include <stdbool.h> | ||
|
||
void ska_asset_manager_initialize(); | ||
void ska_asset_manager_finalize(); | ||
// --- Texture --- // | ||
struct SkaTexture* ska_asset_manager_load_texture(const char* fileName, const char* key); | ||
struct SkaTexture* ska_asset_manager_load_texture_ex(const char* fileName, const char* key, const char* wrap_s, const char* wrap_t, bool applyNearestNeighbor); | ||
struct SkaTexture* ska_asset_manager_get_texture(const char* key); | ||
bool ska_asset_manager_has_texture(const char* key); | ||
// --- Font --- // | ||
struct SkaFont* ska_asset_manager_load_font(const char* fileName, const char* key, int size, bool applyNearestNeighbor); | ||
struct SkaFont* ska_asset_manager_load_font_from_memory(const char* key, void* buffer, size_t bufferSize, int size, bool applyNearestNeighbor); | ||
struct SkaFont* ska_asset_manager_get_font(const char* key); | ||
bool ska_asset_manager_has_font(const char* key); | ||
// -- Audio Source --- // | ||
struct SkaAudioSource* ska_asset_manager_load_audio_source_wav(const char* fileName, const char* key); | ||
struct SkaAudioSource* ska_asset_manager_get_audio_source(const char* key); | ||
bool ska_asset_manager_has_audio_source(const char* key); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include "audio.h" | ||
|
||
#define MINIAUDIO_IMPLEMENTATION | ||
#include <miniaudio.h> | ||
|
||
#include "seika/memory.h" | ||
#include "seika/logger.h" | ||
#include "seika/asset/asset_file_loader.h" | ||
|
||
static uint32 audioWavSampleRate = SKA_AUDIO_SOURCE_DEFAULT_WAV_SAMPLE_RATE; | ||
|
||
static bool load_wav_data_from_file(const char* file_path, int32* sample_count, int32* channels, int32* sample_rate, void** samples); | ||
|
||
void ska_audio_print_audio_source(SkaAudioSource* audioSource) { | ||
ska_logger_debug("audio source | channels = %d, sample rate = %d, sample count = %d, samples = %x", | ||
audioSource->channels, audioSource->sample_rate, audioSource->sample_count, audioSource->samples); | ||
} | ||
|
||
void ska_audio_set_wav_sample_rate(uint32 wavSampleRate) { | ||
audioWavSampleRate = wavSampleRate; | ||
} | ||
|
||
uint32_t ska_audio_get_wav_sample_rate() { | ||
return audioWavSampleRate; | ||
} | ||
|
||
SkaAudioSource* ska_audio_load_audio_source_wav(const char* fileName) { | ||
int32_t sampleCount; | ||
int32_t channels; | ||
int32_t sampleRate; | ||
void* samples = NULL; | ||
if (!load_wav_data_from_file(fileName, &sampleCount, &channels, &sampleRate, &samples)) { | ||
ska_logger_error("Failed to load audio wav file at '%s'", fileName); | ||
return NULL; | ||
} | ||
SkaAudioSource* newAudioSource = (SkaAudioSource*)SKA_MEM_ALLOCATE_SIZE(sizeof(SkaAudioSource*) + (sampleCount * sizeof(int16_t*))); | ||
newAudioSource->file_path = fileName; | ||
newAudioSource->pitch = 1.0; | ||
newAudioSource->sample_count = sampleCount; | ||
newAudioSource->channels = channels; | ||
newAudioSource->sample_rate = sampleRate; | ||
newAudioSource->samples = samples; | ||
|
||
if ((uint32_t)newAudioSource->sample_rate != audioWavSampleRate) { | ||
ska_logger_error("Sample rate for wav file '%s' is %d instead of the expected sample rate of %d! Audio won't play as expected!", | ||
fileName, sampleRate, audioWavSampleRate); | ||
} | ||
|
||
return newAudioSource; | ||
} | ||
|
||
bool load_wav_data_from_file(const char* file_path, int32_t* sample_count, int32_t* channels, int32_t* sample_rate, void** samples) { | ||
size_t len = 0; | ||
char* file_data = ska_asset_file_loader_read_file_contents_as_string(file_path, &len); | ||
ska_logger_debug("file '%s' size '%u' bytes", file_path, len); | ||
|
||
drwav_uint64 totalPcmFrameCount = 0; | ||
*samples = drwav_open_memory_and_read_pcm_frames_s16(file_data, len, (uint32_t*)channels, (uint32_t*)sample_rate, &totalPcmFrameCount, NULL); | ||
SKA_MEM_FREE(file_data); | ||
|
||
if (!*samples) { | ||
*samples = NULL; | ||
ska_logger_error("Could not load .wav file: %s", file_path); | ||
return false; | ||
} | ||
|
||
*sample_count = (int32_t) totalPcmFrameCount * *channels; | ||
|
||
return true; | ||
} |
Oops, something went wrong.