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 <string.h>
#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
}

View file

@ -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();

6
Kaos.c
View file

@ -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)
{

4
Kaos.h
View file

@ -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);

View file

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

2
Room.c
View file

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

View file

@ -1,12 +1,12 @@
#include <stdio.h>
#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 "Scene.h"
#include "Timer.h"
#include "Engine.h"

View file

@ -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"

View file

@ -1,10 +1,12 @@
#include <stdio.h>
#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;

View file

@ -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;