conditional compiling for sound support!

This commit is contained in:
Iris Lightshard 2018-11-13 13:30:59 -08:00
parent 3fdb066689
commit 1fed6282f5
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
10 changed files with 83 additions and 10 deletions

View file

@ -1,12 +1,16 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "config.h"
#include "SDL/SDL.h" #include "SDL/SDL.h"
#include "SDL/SDL_image.h" #include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.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 "enum.h"
#include "Engine.h" #include "Engine.h"
@ -53,6 +57,8 @@ void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, S
// sound // sound
// //
#ifdef SOUND_ON
Mix_Music* loadBGM(char* filename) Mix_Music* loadBGM(char* filename)
{ {
Mix_Music* bgmTrack = Mix_LoadMUS(filename); Mix_Music* bgmTrack = Mix_LoadMUS(filename);
@ -66,6 +72,8 @@ Mix_Chunk* loadSFX(char* filename)
return sfxClip; return sfxClip;
} }
#endif
// //
// world // world
// //
@ -183,10 +191,20 @@ void kListen(int* whichKaos)
int init(int argc, char* args[]) int init(int argc, char* args[])
{ {
printf("Initializing SDL\n"); printf("Initializing SDL\n");
#ifdef SOUND_ON
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO) == -1) return 0; if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_AUDIO) == -1) return 0;
printf("Initialized\nOpening soundsystem\n"); printf("Initialized\nOpening soundsystem\n");
if (Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT, 2, 4096) == -1) return 0; if (Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT, 2, 4096) == -1) return 0;
printf("Soundsystem open\nCreating window\n"); 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 (argc >= 2)
{ {
if (strcmp(args[1], "-w")) if (strcmp(args[1], "-w"))
@ -226,8 +244,12 @@ int init(int argc, char* args[])
mapData = malloc(64*sizeof(Room*)); mapData = malloc(64*sizeof(Room*));
mapBuffer= malloc(64*sizeof(Room*)); mapBuffer= malloc(64*sizeof(Room*));
dialogueData = malloc(124*sizeof(Room*)); dialogueData = malloc(124*sizeof(Room*));
#ifdef SOUND_ON
bgmData = malloc(8*sizeof(Mix_Music*)); bgmData = malloc(8*sizeof(Mix_Music*));
sfxData = malloc(24*sizeof(Mix_Chunk*)); sfxData = malloc(24*sizeof(Mix_Chunk*));
#endif
kaosData = malloc(124*sizeof(Kaos*)); kaosData = malloc(124*sizeof(Kaos*));
theatre = malloc(8*sizeof(Scene*)); theatre = malloc(8*sizeof(Scene*));
@ -296,8 +318,11 @@ void cleanup()
free(kaosData); free(kaosData);
free(dialogueData); free(dialogueData);
#ifdef SOUND_ON
free(bgmData); free(bgmData);
free(sfxData); free(sfxData);
#endif
free(theatre); free(theatre);
@ -311,7 +336,11 @@ void cleanup()
SDL_FreeSurface(screen); SDL_FreeSurface(screen);
printf("Closing SDL\n"); printf("Closing SDL\n");
#ifdef SOUND_ON
Mix_CloseAudio(); Mix_CloseAudio();
#endif
TTF_CloseFont(font); TTF_CloseFont(font);
TTF_Quit(); TTF_Quit();
SDL_Quit(); SDL_Quit();
@ -360,10 +389,12 @@ void mainmenu()
long long int eventState = 2; long long int eventState = 2;
warpto(menuBG); warpto(menuBG);
#ifdef SOUND_ON
if (menuBGM == NULL) if (menuBGM == NULL)
Mix_HaltMusic(); Mix_HaltMusic();
else else
Mix_PlayMusic(menuBGM, -1); Mix_PlayMusic(menuBGM, -1);
#endif
while (!select) while (!select)
{ {
@ -469,7 +500,11 @@ void pausemenu()
FILE* saveData; FILE* saveData;
hero->bearing.x = 0; hero->bearing.x = 0;
hero->bearing.y = 0; hero->bearing.y = 0;
#ifdef SOUND_ON
Mix_VolumeMusic(MIX_MAX_VOLUME/3); Mix_VolumeMusic(MIX_MAX_VOLUME/3);
#endif
for (i = 0; i < 64; i++) for (i = 0; i < 64; i++)
{ {
if (rightHere == mapData[i]) if (rightHere == mapData[i])
@ -557,6 +592,9 @@ void pausemenu()
SDL_Flip(screen); SDL_Flip(screen);
timeDilation(); timeDilation();
} }
#ifdef SOUND_ON
Mix_VolumeMusic(MIX_MAX_VOLUME); Mix_VolumeMusic(MIX_MAX_VOLUME);
#endif
} }

View file

@ -6,10 +6,12 @@ void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, S
// SDL sound // SDL sound
#ifdef SOUND_ON
Mix_Music* loadBGM(char* filename); Mix_Music* loadBGM(char* filename);
Mix_Chunk* loadSFX(char* filename); Mix_Chunk* loadSFX(char* filename);
#endif
// world // world
void renderBackground(); void renderBackground();

6
Kaos.c
View file

@ -1,10 +1,10 @@
#include "config.h"
#include "SDL/SDL.h" #include "SDL/SDL.h"
#include "SDL/SDL_image.h" #include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h" #include "SDL/SDL_ttf.h"
#include "SDL/SDL_mixer.h" #include "SDL/SDL_mixer.h"
#include "config.h"
#include "enum.h" #include "enum.h"
#include "Engine.h" #include "Engine.h"
#include "Timer.h" #include "Timer.h"
@ -310,6 +310,7 @@ void deleteFaceEachother(Kaos* target)
free(target); free(target);
} }
#ifdef SOUND_ON
Kaos* newPlaySound(int i) Kaos* newPlaySound(int i)
{ {
Kaos* core = rawKaos(); Kaos* core = rawKaos();
@ -335,6 +336,7 @@ void deletePlaySound(Kaos* target)
free(target->kType); free(target->kType);
free(target); free(target);
} }
#endif
Kaos* newErase(char t, int i) Kaos* newErase(char t, int i)
{ {

4
Kaos.h
View file

@ -46,11 +46,13 @@ typedef struct kaos_FaceEachother
Player* p2; Player* p2;
} FaceEachother; } FaceEachother;
#ifdef SOUND_ON
typedef struct kaos_PlaySound typedef struct kaos_PlaySound
{ {
Kaos* core; Kaos* core;
int i; int i;
} PlaySound; } PlaySound;
#endif
typedef struct kaos_Teleport typedef struct kaos_Teleport
{ {
@ -107,9 +109,11 @@ Kaos* newFaceEachother(Player* p1, Player* p2);
void runFaceEachother(Kaos* self); void runFaceEachother(Kaos* self);
void deleteFaceEachother(Kaos* target); void deleteFaceEachother(Kaos* target);
#ifdef SOUND_ON
Kaos* newPlaySound(int i); Kaos* newPlaySound(int i);
void runPlaySound(Kaos* self); void runPlaySound(Kaos* self);
void deletePlaySound(Kaos* target); void deletePlaySound(Kaos* target);
#endif
Kaos* newErase(char t, int i); Kaos* newErase(char t, int i);
void runErase(Kaos* self); void runErase(Kaos* self);

View file

@ -1,5 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include "config.h"
#include "SDL/SDL.h" #include "SDL/SDL.h"
#include "SDL/SDL_image.h" #include "SDL/SDL_image.h"
#include "SDL/SDL_mixer.h" #include "SDL/SDL_mixer.h"

2
Room.c
View file

@ -1,5 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include "config.h"
#include "SDL/SDL.h" #include "SDL/SDL.h"
#include "SDL/SDL_image.h" #include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h" #include "SDL/SDL_ttf.h"

View file

@ -1,12 +1,12 @@
#include <stdio.h> #include <stdio.h>
#include "config.h"
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include <SDL/SDL_image.h> #include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h> #include <SDL/SDL_ttf.h>
#include <SDL/SDL_mixer.h> #include <SDL/SDL_mixer.h>
#include "config.h"
#include "Scene.h" #include "Scene.h"
#include "Timer.h" #include "Timer.h"
#include "Engine.h" #include "Engine.h"

View file

@ -1,10 +1,10 @@
#include "config.h"
#include "SDL/SDL.h" #include "SDL/SDL.h"
#include "SDL/SDL_image.h" #include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h" #include "SDL/SDL_ttf.h"
#include "SDL/SDL_mixer.h" #include "SDL/SDL_mixer.h"
#include "config.h"
#include "Engine.h" #include "Engine.h"
#include "Timer.h" #include "Timer.h"
#include "Player.h" #include "Player.h"

View file

@ -1,10 +1,12 @@
#include <stdio.h> #include <stdio.h>
#include "config.h"
#include "SDL/SDL.h" #include "SDL/SDL.h"
#include "SDL/SDL_image.h" #include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h" #include "SDL/SDL_ttf.h"
#include "SDL/SDL_mixer.h" #include "SDL/SDL_mixer.h"
#include "config.h"
#include "enum.h" #include "enum.h"
#include "Engine.h" #include "Engine.h"
@ -27,7 +29,10 @@ void bufferData(enum dataChunks chunk)
printf("Loading map chunk\n"); printf("Loading map chunk\n");
switch (chunk){ switch (chunk){
case LEVEL1: case LEVEL1:
#ifdef SOUND_ON
bgmData[0] = loadBGM("assets/snd/bgm/artificial sun stage (mamon machine mix).mp3"); bgmData[0] = loadBGM("assets/snd/bgm/artificial sun stage (mamon machine mix).mp3");
#endif
mapBuffer[0] = newRoom("assets/img/backgrounds/blueroom.gif", 10); mapBuffer[0] = newRoom("assets/img/backgrounds/blueroom.gif", 10);
mapBuffer[1] = newRoom("assets/img/backgrounds/redroom.gif", 4); 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]); deleteTextBox(dialogueData[i]);
for (i = 0; i < c; i++) for (i = 0; i < c; i++)
kaosData[i]->destroy(kaosData[i]); kaosData[i]->destroy(kaosData[i]);
#ifdef SOUND_ON
for (i = 0; i < d; i++) for (i = 0; i < d; i++)
Mix_FreeMusic(bgmData[i]); Mix_FreeMusic(bgmData[i]);
for (i = 0; i < e; i++) for (i = 0; i < e; i++)
Mix_FreeChunk(sfxData[i]); Mix_FreeChunk(sfxData[i]);
#endif
} }
void unloadData(enum dataChunks chunk) void unloadData(enum dataChunks chunk)
@ -212,8 +221,12 @@ void pushBufferData()
printf("Pushing map buffer\n"); printf("Pushing map buffer\n");
mapData = mapBuffer; mapData = mapBuffer;
mapBuffer = (Room**)malloc(64*sizeof(Room*)); mapBuffer = (Room**)malloc(64*sizeof(Room*));
#ifdef SOUND_ON
if (hasMusic) if (hasMusic)
Mix_PlayMusic(bgmData[0], -1); Mix_PlayMusic(bgmData[0], -1);
#endif
printf("Map buffer clean\n"); printf("Map buffer clean\n");
} }
@ -221,10 +234,14 @@ void pager()
{ {
if (thisChunk != nextChunk) if (thisChunk != nextChunk)
{ {
#ifdef SOUND_ON
if (Mix_PlayingMusic()) if (Mix_PlayingMusic())
{ {
Mix_HaltMusic(); Mix_HaltMusic();
} }
#endif
unloadData(thisChunk); unloadData(thisChunk);
pushBufferData(); pushBufferData();
thisChunk=nextChunk; thisChunk=nextChunk;

View file

@ -28,7 +28,9 @@ extern SDL_Surface* nextArrow;
extern SDL_Surface* selectArrow; extern SDL_Surface* selectArrow;
extern SDL_Surface* loadingTxt; extern SDL_Surface* loadingTxt;
#ifdef SOUND_ON
extern Mix_Music* menuBGM; extern Mix_Music* menuBGM;
#endif
extern TTF_Font* font; extern TTF_Font* font;
extern SDL_Color textColor; extern SDL_Color textColor;
@ -39,7 +41,11 @@ extern enum dataChunks nextChunk;
extern Room** mapData; extern Room** mapData;
extern Room** mapBuffer; extern Room** mapBuffer;
extern TextBox** dialogueData; extern TextBox** dialogueData;
#ifdef SOUND_ON
extern Mix_Music** bgmData; extern Mix_Music** bgmData;
extern Mix_Chunk** sfxData; extern Mix_Chunk** sfxData;
#endif
extern Kaos** kaosData; extern Kaos** kaosData;
extern Scene** theatre; extern Scene** theatre;