From 1fed6282f56e78c5e5b77a2834b9555298c6b0c0 Mon Sep 17 00:00:00 2001 From: Derek Stevens Date: Tue, 13 Nov 2018 13:30:59 -0800 Subject: [PATCH] conditional compiling for sound support! --- Engine.c | 42 ++++++++++++++++++++++++++++++++++++++++-- Engine.h | 4 +++- Kaos.c | 6 ++++-- Kaos.h | 4 ++++ Player.c | 2 ++ Room.c | 2 ++ Scene.c | 4 ++-- TextBox.c | 4 ++-- WorldData.c | 19 ++++++++++++++++++- extern.h | 6 ++++++ 10 files changed, 83 insertions(+), 10 deletions(-) diff --git a/Engine.c b/Engine.c index 4521729..b696c04 100644 --- a/Engine.c +++ b/Engine.c @@ -1,12 +1,16 @@ #include #include +#include "config.h" + #include "SDL/SDL.h" #include "SDL/SDL_image.h" #include "SDL/SDL_ttf.h" -#include "SDL/SDL_mixer.h" -#include "config.h" +#ifdef SOUND_ON +#include "SDL/SDL_mixer.h" +#endif + #include "enum.h" #include "Engine.h" @@ -53,6 +57,8 @@ void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, S // sound // +#ifdef SOUND_ON + Mix_Music* loadBGM(char* filename) { Mix_Music* bgmTrack = Mix_LoadMUS(filename); @@ -66,6 +72,8 @@ Mix_Chunk* loadSFX(char* filename) return sfxClip; } +#endif + // // world // @@ -183,10 +191,20 @@ void kListen(int* whichKaos) int init(int argc, char* args[]) { printf("Initializing SDL\n"); + +#ifdef SOUND_ON if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO) == -1) return 0; printf("Initialized\nOpening soundsystem\n"); if (Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT, 2, 4096) == -1) return 0; printf("Soundsystem open\nCreating window\n"); + +#else + + if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) == -1) return 0; + printf("Initialized\nCreating window\n"); + +#endif + if (argc >= 2) { if (strcmp(args[1], "-w")) @@ -226,8 +244,12 @@ int init(int argc, char* args[]) mapData = malloc(64*sizeof(Room*)); mapBuffer= malloc(64*sizeof(Room*)); dialogueData = malloc(124*sizeof(Room*)); + +#ifdef SOUND_ON bgmData = malloc(8*sizeof(Mix_Music*)); sfxData = malloc(24*sizeof(Mix_Chunk*)); +#endif + kaosData = malloc(124*sizeof(Kaos*)); theatre = malloc(8*sizeof(Scene*)); @@ -296,8 +318,11 @@ void cleanup() free(kaosData); free(dialogueData); + +#ifdef SOUND_ON free(bgmData); free(sfxData); +#endif free(theatre); @@ -311,7 +336,11 @@ void cleanup() SDL_FreeSurface(screen); printf("Closing SDL\n"); + +#ifdef SOUND_ON Mix_CloseAudio(); +#endif + TTF_CloseFont(font); TTF_Quit(); SDL_Quit(); @@ -360,10 +389,12 @@ void mainmenu() long long int eventState = 2; warpto(menuBG); +#ifdef SOUND_ON if (menuBGM == NULL) Mix_HaltMusic(); else Mix_PlayMusic(menuBGM, -1); +#endif while (!select) { @@ -469,7 +500,11 @@ void pausemenu() FILE* saveData; hero->bearing.x = 0; hero->bearing.y = 0; + +#ifdef SOUND_ON Mix_VolumeMusic(MIX_MAX_VOLUME/3); +#endif + for (i = 0; i < 64; i++) { if (rightHere == mapData[i]) @@ -557,6 +592,9 @@ void pausemenu() SDL_Flip(screen); timeDilation(); } + +#ifdef SOUND_ON Mix_VolumeMusic(MIX_MAX_VOLUME); +#endif } diff --git a/Engine.h b/Engine.h index 273b823..36e4664 100644 --- a/Engine.h +++ b/Engine.h @@ -6,10 +6,12 @@ void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, S // SDL sound + +#ifdef SOUND_ON Mix_Music* loadBGM(char* filename); Mix_Chunk* loadSFX(char* filename); - +#endif // world void renderBackground(); diff --git a/Kaos.c b/Kaos.c index fef8173..7ab3551 100644 --- a/Kaos.c +++ b/Kaos.c @@ -1,10 +1,10 @@ +#include "config.h" + #include "SDL/SDL.h" #include "SDL/SDL_image.h" #include "SDL/SDL_ttf.h" #include "SDL/SDL_mixer.h" -#include "config.h" - #include "enum.h" #include "Engine.h" #include "Timer.h" @@ -310,6 +310,7 @@ void deleteFaceEachother(Kaos* target) free(target); } +#ifdef SOUND_ON Kaos* newPlaySound(int i) { Kaos* core = rawKaos(); @@ -335,6 +336,7 @@ void deletePlaySound(Kaos* target) free(target->kType); free(target); } +#endif Kaos* newErase(char t, int i) { diff --git a/Kaos.h b/Kaos.h index 46f920c..d150ca9 100644 --- a/Kaos.h +++ b/Kaos.h @@ -46,11 +46,13 @@ typedef struct kaos_FaceEachother Player* p2; } FaceEachother; +#ifdef SOUND_ON typedef struct kaos_PlaySound { Kaos* core; int i; } PlaySound; +#endif typedef struct kaos_Teleport { @@ -107,9 +109,11 @@ Kaos* newFaceEachother(Player* p1, Player* p2); void runFaceEachother(Kaos* self); void deleteFaceEachother(Kaos* target); +#ifdef SOUND_ON Kaos* newPlaySound(int i); void runPlaySound(Kaos* self); void deletePlaySound(Kaos* target); +#endif Kaos* newErase(char t, int i); void runErase(Kaos* self); diff --git a/Player.c b/Player.c index d7cfe96..6eadc6b 100644 --- a/Player.c +++ b/Player.c @@ -1,5 +1,7 @@ #include +#include "config.h" + #include "SDL/SDL.h" #include "SDL/SDL_image.h" #include "SDL/SDL_mixer.h" diff --git a/Room.c b/Room.c index 1f476f0..93b7369 100644 --- a/Room.c +++ b/Room.c @@ -1,5 +1,7 @@ #include +#include "config.h" + #include "SDL/SDL.h" #include "SDL/SDL_image.h" #include "SDL/SDL_ttf.h" diff --git a/Scene.c b/Scene.c index f342f54..5aeb8bb 100644 --- a/Scene.c +++ b/Scene.c @@ -1,12 +1,12 @@ #include +#include "config.h" + #include #include #include #include -#include "config.h" - #include "Scene.h" #include "Timer.h" #include "Engine.h" diff --git a/TextBox.c b/TextBox.c index a8c6555..1a48115 100644 --- a/TextBox.c +++ b/TextBox.c @@ -1,10 +1,10 @@ +#include "config.h" + #include "SDL/SDL.h" #include "SDL/SDL_image.h" #include "SDL/SDL_ttf.h" #include "SDL/SDL_mixer.h" -#include "config.h" - #include "Engine.h" #include "Timer.h" #include "Player.h" diff --git a/WorldData.c b/WorldData.c index b84a747..f7b7558 100644 --- a/WorldData.c +++ b/WorldData.c @@ -1,10 +1,12 @@ #include + +#include "config.h" + #include "SDL/SDL.h" #include "SDL/SDL_image.h" #include "SDL/SDL_ttf.h" #include "SDL/SDL_mixer.h" -#include "config.h" #include "enum.h" #include "Engine.h" @@ -27,7 +29,10 @@ void bufferData(enum dataChunks chunk) printf("Loading map chunk\n"); switch (chunk){ case LEVEL1: + +#ifdef SOUND_ON bgmData[0] = loadBGM("assets/snd/bgm/artificial sun stage (mamon machine mix).mp3"); +#endif mapBuffer[0] = newRoom("assets/img/backgrounds/blueroom.gif", 10); mapBuffer[1] = newRoom("assets/img/backgrounds/redroom.gif", 4); @@ -184,10 +189,14 @@ void dataPurge(int a, int b, int c, int d, int e) deleteTextBox(dialogueData[i]); for (i = 0; i < c; i++) kaosData[i]->destroy(kaosData[i]); + +#ifdef SOUND_ON for (i = 0; i < d; i++) Mix_FreeMusic(bgmData[i]); for (i = 0; i < e; i++) Mix_FreeChunk(sfxData[i]); +#endif + } void unloadData(enum dataChunks chunk) @@ -212,8 +221,12 @@ void pushBufferData() printf("Pushing map buffer\n"); mapData = mapBuffer; mapBuffer = (Room**)malloc(64*sizeof(Room*)); + +#ifdef SOUND_ON if (hasMusic) Mix_PlayMusic(bgmData[0], -1); +#endif + printf("Map buffer clean\n"); } @@ -221,10 +234,14 @@ void pager() { if (thisChunk != nextChunk) { + +#ifdef SOUND_ON if (Mix_PlayingMusic()) { Mix_HaltMusic(); } +#endif + unloadData(thisChunk); pushBufferData(); thisChunk=nextChunk; diff --git a/extern.h b/extern.h index 7f8b7ac..17bbe94 100644 --- a/extern.h +++ b/extern.h @@ -28,7 +28,9 @@ extern SDL_Surface* nextArrow; extern SDL_Surface* selectArrow; extern SDL_Surface* loadingTxt; +#ifdef SOUND_ON extern Mix_Music* menuBGM; +#endif extern TTF_Font* font; extern SDL_Color textColor; @@ -39,7 +41,11 @@ extern enum dataChunks nextChunk; extern Room** mapData; extern Room** mapBuffer; extern TextBox** dialogueData; + +#ifdef SOUND_ON extern Mix_Music** bgmData; extern Mix_Chunk** sfxData; +#endif + extern Kaos** kaosData; extern Scene** theatre;