git repos / blockattack-game

commit 524f188c

sago007 · 2017-04-22 09:01
524f188c7dec870fe9dc50afa83c0f86613e2beb patch · browse files
parent 6082686bdf3960ac115c5c46c9415824fa0cc190

Now uses handlers for all but fonts

Changed files

M source/code/BlockGameSdl.inc before
M source/code/DialogBox.cpp before
M source/code/global.hpp before
M source/code/main.cpp before
M source/code/mainVars.inc before
M source/code/sago/SagoDataHolder.cpp before
M source/code/sago/SagoDataHolder.hpp before
diff --git a/source/code/BlockGameSdl.inc b/source/code/BlockGameSdl.inc index 40c4898..8fe0145 100644 --- a/source/code/BlockGameSdl.inc +++ b/source/code/BlockGameSdl.inc
@@ -109,7 +109,7 @@ public:
if (!globalData.SoundEnabled) {
return;
}
- Mix_PlayChannel(1, applause, 0);
+ Mix_PlayChannel(1, applause.get(), 0);
}
void DrawEvent() const override {
@@ -120,19 +120,19 @@ public:
if (!globalData.SoundEnabled) {
return;
}
- Mix_PlayChannel(0, boing, 0);
+ Mix_PlayChannel(0, boing.get(), 0);
}
void LongChainDoneEvent() const override {
if (!globalData.SoundEnabled) {
return;
}
- Mix_PlayChannel(1, applause, 0);
+ Mix_PlayChannel(1, applause.get(), 0);
}
void TimeTrialEndEvent() const override {
if (!globalData.NoSound && globalData.SoundEnabled) {
- Mix_PlayChannel(1,counterFinalChunk,0);
+ Mix_PlayChannel(1,counterFinalChunk.get(),0);
}
}
@@ -140,7 +140,7 @@ public:
if (!globalData.SoundEnabled) {
return;
}
- Mix_PlayChannel(1, applause, 0);
+ Mix_PlayChannel(1, applause.get(), 0);
}
private:
//Draws all the bricks to the board (including garbage)
@@ -368,7 +368,7 @@ public:
if (ticks<gameStartedAt) {
int currentCounter = abs((int)ticks-(int)gameStartedAt)/1000;
if ( (currentCounter!=lastCounter) && (globalData.SoundEnabled)&&(!globalData.NoSound)) {
- Mix_PlayChannel(1,counterChunk,0);
+ Mix_PlayChannel(1, counterChunk.get(), 0);
}
lastCounter = currentCounter;
switch (currentCounter) {
@@ -390,14 +390,14 @@ public:
int currentCounter = (ticks-(int)gameStartedAt)/1000;
if (currentCounter!=lastCounter) {
if (currentCounter>115 && currentCounter<120) {
- Mix_PlayChannel(1,counterChunk,0);
+ Mix_PlayChannel(1, counterChunk.get(), 0);
}
}
lastCounter = currentCounter;
}
else {
if ( (0==lastCounter) && (globalData.SoundEnabled)&&(!globalData.NoSound)) {
- Mix_PlayChannel(1, counterFinalChunk, 0);
+ Mix_PlayChannel(1, counterFinalChunk.get(), 0);
}
lastCounter = -1;
}
diff --git a/source/code/DialogBox.cpp b/source/code/DialogBox.cpp index b7c6057..3390c54 100644 --- a/source/code/DialogBox.cpp +++ b/source/code/DialogBox.cpp
@@ -115,7 +115,7 @@ void DialogBox::Draw(SDL_Renderer* target) {
void DialogBox::ProcessInput(const SDL_Event& event, bool& processed) {
if (event.type == SDL_TEXTINPUT) {
if ((rk->ReadKey(event))&&(globalData.SoundEnabled)&&(!globalData.NoSound)) {
- Mix_PlayChannel(1, globalData.typingChunk, 0);
+ Mix_PlayChannel(1, globalData.typingChunk.get(), 0);
}
}
@@ -130,7 +130,7 @@ void DialogBox::ProcessInput(const SDL_Event& event, bool& processed) {
}
else {
if ((rk->ReadKey(event))&&(globalData.SoundEnabled)&&(!globalData.NoSound)) {
- Mix_PlayChannel(1, globalData.typingChunk, 0);
+ Mix_PlayChannel(1, globalData.typingChunk.get(), 0);
}
}
}
diff --git a/source/code/global.hpp b/source/code/global.hpp index b7f47e4..c24454e 100644 --- a/source/code/global.hpp +++ b/source/code/global.hpp
@@ -63,7 +63,7 @@ struct GlobalData {
std::string player1name;
std::string player2name;
SDL_Renderer *screen = nullptr; //The whole screen;
- Mix_Chunk *typingChunk;
+ sago::SoundHandler typingChunk;
sago::SagoSprite mouse;
bool highPriority = false;
bool NoSound = false;
diff --git a/source/code/main.cpp b/source/code/main.cpp index 2b4df50..1b1b00c 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp
@@ -203,23 +203,23 @@ static int InitImages(sago::SagoSpriteHolder& holder) {
//Loads the sound if sound present
if (!globalData.NoSound) {
//And here the music:
- bgMusic = holder.GetDataHolder().getMusicPtr("bgmusic");
- highbeatMusic = holder.GetDataHolder().getMusicPtr("highbeat");
+ bgMusic = holder.GetDataHolder().getMusicHandler("bgmusic");
+ highbeatMusic = holder.GetDataHolder().getMusicHandler("highbeat");
//the music... we just hope it exists, else the user won't hear anything
//Same goes for the sounds
- boing = holder.GetDataHolder().getSoundPtr("pop");
- applause = holder.GetDataHolder().getSoundPtr("applause");
- photoClick = holder.GetDataHolder().getSoundPtr("cameraclick");
- globalData.typingChunk = holder.GetDataHolder().getSoundPtr("typing");
- counterChunk = holder.GetDataHolder().getSoundPtr("counter");
- counterFinalChunk = holder.GetDataHolder().getSoundPtr("counter_final");
+ boing = holder.GetDataHolder().getSoundHandler("pop");
+ applause = holder.GetDataHolder().getSoundHandler("applause");
+ photoClick = holder.GetDataHolder().getSoundHandler("cameraclick");
+ globalData.typingChunk = holder.GetDataHolder().getSoundHandler("typing");
+ counterChunk = holder.GetDataHolder().getSoundHandler("counter");
+ counterFinalChunk = holder.GetDataHolder().getSoundHandler("counter_final");
const int soundVolume = 84; //0-128
- Mix_VolumeChunk(boing, soundVolume);
- Mix_VolumeChunk(applause, soundVolume);
- Mix_VolumeChunk(photoClick, soundVolume);
- Mix_VolumeChunk(globalData.typingChunk, soundVolume);
- Mix_VolumeChunk(counterChunk, soundVolume);
- Mix_VolumeChunk(counterFinalChunk, soundVolume);
+ Mix_VolumeChunk(boing.get(), soundVolume);
+ Mix_VolumeChunk(applause.get(), soundVolume);
+ Mix_VolumeChunk(photoClick.get(), soundVolume);
+ Mix_VolumeChunk(globalData.typingChunk.get(), soundVolume);
+ Mix_VolumeChunk(counterChunk.get(), soundVolume);
+ Mix_VolumeChunk(counterFinalChunk.get(), soundVolume);
} //All sound has been loaded or not
return 0;
} //InitImages()
@@ -332,7 +332,7 @@ void writeScreenShot() {
SDL_FreeSurface(sreenshotSurface);
if (!globalData.NoSound) {
if (globalData.SoundEnabled) {
- Mix_PlayChannel(1, photoClick, 0);
+ Mix_PlayChannel(1, photoClick.get(), 0);
}
}
}
@@ -1566,20 +1566,20 @@ int runGame(Gametype gametype, int level) {
//Sees if music is stopped and if music is enabled
if ((!globalData.NoSound)&&(!Mix_PlayingMusic())&&(globalData.MusicEnabled)&&(!bNearDeath)) {
// then starts playing it.
- Mix_PlayMusic(bgMusic, -1); //music loop
+ Mix_PlayMusic(bgMusic.get(), -1); //music loop
Mix_VolumeMusic((MIX_MAX_VOLUME*3)/10);
}
if (bNearDeath!=bNearDeathPrev) {
if (bNearDeath) {
if (!globalData.NoSound &&(globalData.MusicEnabled)) {
- Mix_PlayMusic(highbeatMusic, 1);
+ Mix_PlayMusic(highbeatMusic.get(), 1);
Mix_VolumeMusic((MIX_MAX_VOLUME*5)/10);
}
}
else {
if (!globalData.NoSound &&(globalData.MusicEnabled)) {
- Mix_PlayMusic(bgMusic, -1);
+ Mix_PlayMusic(bgMusic.get(), -1);
Mix_VolumeMusic((MIX_MAX_VOLUME*3)/10);
}
}
diff --git a/source/code/mainVars.inc b/source/code/mainVars.inc index 447cfd9..5f5e566 100644 --- a/source/code/mainVars.inc +++ b/source/code/mainVars.inc
@@ -97,13 +97,13 @@ const int bExitOffset = 140; //pixels from the buttom right corner to the top le
static NFont nf_standard_small_font;
-static Mix_Music *bgMusic; //backgroundMusic
-static Mix_Music *highbeatMusic; //Background music with higher beat
-static Mix_Chunk *boing; //boing sound when clearing
-static Mix_Chunk *applause; //Applause, then the player is good
-static Mix_Chunk *photoClick; //clickSound
-static Mix_Chunk *counterChunk; //When counting down
-static Mix_Chunk *counterFinalChunk;
+static sago::MusicHandler bgMusic; //backgroundMusic
+static sago::MusicHandler highbeatMusic; //Background music with higher beat
+static sago::SoundHandler boing; //boing sound when clearing
+static sago::SoundHandler applause; //Applause, then the player is good
+static sago::SoundHandler photoClick; //clickSound
+static sago::SoundHandler counterChunk; //When counting down
+static sago::SoundHandler counterFinalChunk;
static bool bMouseUp; //true if the mouse(1) is unpressed
static bool bMouseUp2; //true if the mouse(2) is unpressed
diff --git a/source/code/sago/SagoDataHolder.cpp b/source/code/sago/SagoDataHolder.cpp index 61961a8..4e959e1 100644 --- a/source/code/sago/SagoDataHolder.cpp +++ b/source/code/sago/SagoDataHolder.cpp
@@ -286,8 +286,48 @@ SDL_Texture* TextureHandler::get() {
return data;
}
+
+MusicHandler::MusicHandler(const SagoDataHolder* holder, const std::string& musicName) {
+ this->holder = holder;
+ this->version = this->holder->getVersion();
+ this->musicName = musicName;
+ this->data = this->holder->getMusicPtr(this->musicName);
+}
+
+Mix_Music* MusicHandler::get() {
+ if (version != holder->getVersion()) {
+ //The holder has been invalidated
+ this->data = this->holder->getMusicPtr(musicName);
+ }
+ return data;
+}
+
+SoundHandler::SoundHandler(const SagoDataHolder* holder, const std::string& soundName) {
+ this->holder = holder;
+ this->version = this->holder->getVersion();
+ this->soundName = soundName;
+ this->data = this->holder->getSoundPtr(this->soundName);
+}
+
+Mix_Chunk* SoundHandler::get() {
+ if (version != holder->getVersion()) {
+ //The holder has been invalidated
+ this->data = this->holder->getSoundPtr(soundName);
+ }
+ return data;
+}
+
+
TextureHandler SagoDataHolder::getTextureHandler(const std::string &textureName) const {
return TextureHandler(this, textureName);
}
+MusicHandler SagoDataHolder::getMusicHandler(const std::string &musicName) const {
+ return MusicHandler(this, musicName);
+}
+
+SoundHandler SagoDataHolder::getSoundHandler(const std::string &soundName) const {
+ return SoundHandler(this, soundName);
+}
+
} //name space sago
diff --git a/source/code/sago/SagoDataHolder.hpp b/source/code/sago/SagoDataHolder.hpp index f771599..4cc0185 100644 --- a/source/code/sago/SagoDataHolder.hpp +++ b/source/code/sago/SagoDataHolder.hpp
@@ -48,6 +48,32 @@ private:
Uint64 version = 0;
};
+
+class MusicHandler {
+public:
+ MusicHandler() {};
+ MusicHandler(const SagoDataHolder* holder, const std::string &musicName);
+ Mix_Music* get();
+private:
+ std::string musicName;
+ const SagoDataHolder* holder = nullptr;
+ Mix_Music* data = nullptr;
+ Uint64 version = 0;
+};
+
+
+class SoundHandler {
+public:
+ SoundHandler() {};
+ SoundHandler(const SagoDataHolder* holder, const std::string &soundName);
+ Mix_Chunk* get();
+private:
+ std::string soundName;
+ const SagoDataHolder* holder = nullptr;
+ Mix_Chunk* data = nullptr;
+ Uint64 version = 0;
+};
+
class SagoDataHolder {
public:
/**
@@ -65,7 +91,9 @@ public:
TextureHandler getTextureHandler(const std::string &textureName) const;
TTF_Font* getFontPtr(const std::string &fontName, int ptsize) const;
Mix_Music* getMusicPtr(const std::string &musicName) const;
+ MusicHandler getMusicHandler(const std::string &musicName) const;
Mix_Chunk* getSoundPtr(const std::string &soundName) const;
+ SoundHandler getSoundHandler(const std::string &soundName) const;
void setVerbose(bool value);
/**
* Invalidates all pointers returned by any of the get variables