Skip to content

Commit

Permalink
Updating more stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chukobyte committed Mar 24, 2024
1 parent 781697c commit 39d2644
Show file tree
Hide file tree
Showing 17 changed files with 92,613 additions and 145 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ include(Dependencies.cmake)
#--- Link ---#
if (CMAKE_C_COMPILER_ID STREQUAL "MSVC")
# target_link_libraries(${PROJECT_NAME} PUBLIC glad stb_image zip SDL2::SDL2main SDL2::SDL2 freetype Ws2_32)
target_link_libraries(${PROJECT_NAME} PUBLIC cglm glad stb_image zip SDL3::SDL3-static freetype Ws2_32)
target_link_libraries(${PROJECT_NAME} PUBLIC cglm glad stb_image zip miniaudio SDL3::SDL3-static freetype Ws2_32)
elseif (WIN32)
# target_link_libraries(${PROJECT_NAME} PUBLIC glad stb_image zip -lmingw32 -static-libgcc SDL2::SDL2main SDL2::SDL2 freetype -lws2_32)
target_link_libraries(${PROJECT_NAME} PUBLIC cglm glad stb_image zip -lmingw32 -static-libgcc SDL3::SDL3-static freetype -lws2_32)
target_link_libraries(${PROJECT_NAME} PUBLIC cglm glad stb_image zip miniaudio -lmingw32 -static-libgcc SDL3::SDL3-static freetype -lws2_32)
elseif (APPLE)
# target_link_libraries(${PROJECT_NAME} PUBLIC glad stb_image zip -Xlinker SDL2::SDL2main SDL2::SDL2 freetype m)
target_link_libraries(${PROJECT_NAME} PUBLIC cglm glad stb_image zip -Xlinker SDL3::SDL3-static freetype m)
target_link_libraries(${PROJECT_NAME} PUBLIC cglm glad stb_image zip miniaudio -Xlinker SDL3::SDL3-static freetype m)
else ()
# target_link_libraries(${PROJECT_NAME} PUBLIC glad stb_image zip -static-libgcc -Xlinker -export-dynamic SDL2::SDL2main SDL2::SDL2 freetype m)
target_link_libraries(${PROJECT_NAME} PUBLIC cglm glad stb_image zip -static-libgcc -Xlinker -export-dynamic SDL3::SDL3-static freetype m)
target_link_libraries(${PROJECT_NAME} PUBLIC cglm glad stb_image zip miniaudio -static-libgcc -Xlinker -export-dynamic SDL3::SDL3-static freetype m)
endif ()

target_compile_options(${PROJECT_NAME} PUBLIC ${flags})
Expand Down
9 changes: 2 additions & 7 deletions Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,8 @@ if (NOT TARGET kuba_zip)
endif()

if (NOT TARGET miniaudio)
include(FetchContent)
FetchContent_Declare(
miniaudio_content
GIT_REPOSITORY https://github.com/mackron/miniaudio.git
GIT_TAG 0.11.21
)
FetchContent_MakeAvailable(miniaudio_content)
add_library(miniaudio INTERFACE thirdparty/miniaudio/miniaudio.h)
target_include_directories(miniaudio INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/miniaudio")
endif()

if (NOT TARGET stb_image)
Expand Down
212 changes: 105 additions & 107 deletions seika/asset/asset_manager.c
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);
}
54 changes: 27 additions & 27 deletions seika/asset/asset_manager.h
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
70 changes: 70 additions & 0 deletions seika/audio/audio.c
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;
}
Loading

0 comments on commit 39d2644

Please sign in to comment.