added reaaly basic audio

This commit is contained in:
herbglitch 2023-09-13 21:32:30 -06:00
parent 46e26e41e5
commit 9bfcd5552e
8 changed files with 111 additions and 3 deletions

View file

@ -35,6 +35,7 @@ if(ARCHEUS_STD_SDL)
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
find_package(SDL2_ttf REQUIRED)
find_package(SDL2_mixer REQUIRED)
string(APPEND ARCHEUS_STD_FLAGS "-DARC_SDL ")
endif()
@ -75,6 +76,9 @@ set(ARCHEUS_STD_SOURCES
)
set(ARCHEUS_STD_SDL_SOURCES
src/audio/sdl/audio.c
src/audio/sdl/config.c
src/input/sdl/keyboard.c
src/input/sdl/mouse.c
@ -130,7 +134,7 @@ if(ARCHEUS_STD_SDL)
PRIVATE ${SDL2IMAGE_INCLUDE_DIRS}
)
target_link_libraries(archeus_std PUBLIC ${SDL2_LIBRARIES} SDL2_image::SDL2_image SDL2_ttf::SDL2_ttf)
target_link_libraries(archeus_std PUBLIC ${SDL2_LIBRARIES} SDL2_image::SDL2_image SDL2_ttf::SDL2_ttf SDL2_mixer::SDL2_mixer)
endif()
if(ARCHEUS_STD_OPENGL)

16
include/arc/audio/audio.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef ARC_AUDIO_H_
#define ARC_AUDIO_H_
#ifdef __cplusplus
extern "C" {
#endif
typedef struct ARC_Audio ARC_Audio;
void ARC_Audio_Play(ARC_Audio *audio);
#ifdef __cplusplus
}
#endif
#endif // !ARC_AUDIO_H_

View file

@ -0,0 +1,22 @@
#ifndef ARC_AUDIO_CONFIG_H_
#define ARC_AUDIO_CONFIG_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include "arc/std/string.h"
typedef struct ARC_Config ARC_Config;
void ARC_AudioConfig_Init(ARC_Config *config);
uint8_t ARC_Audio_Read(ARC_Config *config, ARC_String *string, void **value);
void ARC_Audio_Delete(ARC_Config *config, ARC_String *string, void *value);
#ifdef __cplusplus
}
#endif
#endif //ARC_AUDIO_CONFIG_H_

View file

@ -0,0 +1,12 @@
#ifndef ARC_SDL_AUDIO_H_
#define ARC_SDL_AUDIO_H_
#ifdef ARC_SDL
#include <SDL2/SDL_mixer.h>
typedef struct ARC_Audio {
Mix_Chunk *chunk;
} ARC_Audio;
#endif // !ARC_SDL
#endif // !ARC_SDL_AUDIO_H_

7
src/audio/sdl/audio.c Normal file
View file

@ -0,0 +1,7 @@
#include "arc/audio/audio.h"
#include "arc/audio/sdl/audio.h"
#include <SDL2/SDL_mixer.h>
void ARC_Audio_Play(ARC_Audio *audio){
Mix_PlayChannel(-1, audio->chunk, 0);
}

43
src/audio/sdl/config.c Normal file
View file

@ -0,0 +1,43 @@
#include "arc/audio/config.h"
#include "arc/audio/sdl/audio.h"
#include <stdio.h>
#include <stdlib.h>
#include "arc/std/config.h"
#include "arc/std/errno.h"
#include "arc/audio/audio.h"
// #define ARC_DEFAULT_CONFIG
#include "arc/std/defaults/config.h"
void ARC_AudioConfig_Init(ARC_Config *config){
ARC_Config_AddKeyCString(config, (char *)"ARC_Audio", 9, ARC_Audio_Read, ARC_Audio_Delete);
}
uint8_t ARC_Audio_Read(ARC_Config *config, ARC_String *string, void **value){
ARC_Config_Get(config, string, value);
if(*value){
return 1;
}
if(string->data[0] != '"' || string->data[string->length - 1] != '"'){
ARC_DEBUG_LOG(arc_errno, "in ARC_Point_Read(config, string, value); no matching quotes: %s", string->data);
arc_errno = ARC_ERRNO_DATA;
return 0;
}
ARC_Audio *audio = (ARC_Audio *)malloc(sizeof(ARC_Audio));
ARC_String *path;
ARC_String_CopySubstring(&path, string, 1, string->length - 2);
audio->chunk = Mix_LoadWAV(path->data);
//TODO: get error message if not loaded
*value = (void *)audio;
return 0;
}
void ARC_Audio_Delete(ARC_Config* config, ARC_String *string, void *value){
Mix_FreeChunk(((ARC_Audio *)value)->chunk);
free((ARC_Audio *)value);
}

View file

@ -15,8 +15,8 @@
#include "arc/input/sdl/mouse.h"
#include "arc/input/sdl/keyboard.h"
#include <SDL.h>
#include <SDL_video.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
#elif ARC_OPENGL
#include "arc/graphics/opengl/window.h"
#include "arc/graphics/opengl/renderer.h"
@ -43,6 +43,8 @@ void ARC_EngineData_Create(ARC_EngineData **data, ARC_Handler_CleanDataFn cleanf
//TEMP
#ifdef ARC_SDL
TTF_Init();
Mix_Init(0);
Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024);
#endif
#ifdef ARC_SDL
@ -85,6 +87,8 @@ void ARC_EngineData_Create(ARC_EngineData **data, ARC_Handler_CleanDataFn cleanf
void ARC_EngineData_Destroy(ARC_EngineData *data){
#ifdef ARC_SDL
free(data->mouse->event);
TTF_Quit();
Mix_Quit();
#endif // ARC_SDL
ARC_Mouse_Destroy(data->mouse);

View file

@ -12,7 +12,7 @@ void ARC_Window_Create(ARC_Window **window, ARC_WindowInfo *info){
return;
}
if(SDL_Init(SDL_INIT_VIDEO) < 0){
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0){
arc_errno = ARC_ERRNO_INIT;
printf("Error: initializing SDL\nSDL Error: %s\n", SDL_GetError());
return;