Skip to content

Commit

Permalink
Option to set the max amount of sounds that will play at once, betwee…
Browse files Browse the repository at this point in the history
…n 0-999
  • Loading branch information
jacob1 committed Mar 31, 2024
1 parent a53595c commit 5e8a28b
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/audio/AudioEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class AudioEngine
int16_t* audio_buf;
uint32_t audio_len;

std::array<std::unique_ptr<Sound>, 100> sounds;
int maxSounds = 100;
std::array<std::unique_ptr<Sound>, 1000> sounds;
std::array<int, 200> counts;
std::atomic<int> playing;

Expand All @@ -28,6 +29,8 @@ class AudioEngine
static void SDL_AudioCallback(void* userdata, uint8_t* stream, int len);

int SoundsPlaying();
int GetMaxSounds();
void SetMaxSounds(int maxSounds);

void Play(int index);

Expand Down
11 changes: 11 additions & 0 deletions src/audio/AudioEngineFake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ int AudioEngine::SoundsPlaying()
return 0;
}


int AudioEngine::GetMaxSounds()
{
return 0;
}

void AudioEngine::SetMaxSounds(int maxSounds)
{

}

void AudioEngine::Play(int index)
{

Expand Down
12 changes: 11 additions & 1 deletion src/audio/AudioEngineReal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,19 @@ int AudioEngine::SoundsPlaying()
return playing;
}

int AudioEngine::GetMaxSounds()
{
return maxSounds;
}

void AudioEngine::SetMaxSounds(int maxSounds)
{
this->maxSounds = maxSounds;
}

void AudioEngine::Play(int index)
{
if (ready && index >= 0 && index <= 199 && (!counts[index] || index > 193))
if (ready && index >= 0 && index <= 199 && (!counts[index] || index > 193) && (playing < maxSounds))
{
SDL_LockAudioDevice(sdlData->device);
for (auto& i : sounds)
Expand Down
10 changes: 10 additions & 0 deletions src/gui/options/OptionsController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ void OptionsController::SetMomentumScroll(bool momentumScroll)
model->SetMomentumScroll(momentumScroll);
}

int OptionsController::GetMaxSounds()
{
return model->GetMaxSounds();
}

void OptionsController::SetMaxSounds(int maxSounds)
{
model->SetMaxSounds(maxSounds);
}

void OptionsController::Exit()
{
view->CloseActiveWindow();
Expand Down
2 changes: 2 additions & 0 deletions src/gui/options/OptionsController.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class OptionsController
void SetIncludePressure(bool includePressure);
void SetPerfectCircle(bool perfectCircle);
void SetMomentumScroll(bool momentumScroll);
int GetMaxSounds();
void SetMaxSounds(int maxSounds);

void Exit();
OptionsView * GetView();
Expand Down
11 changes: 11 additions & 0 deletions src/gui/options/OptionsModel.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "OptionsModel.h"
#include "OptionsView.h"
#include "audio/AudioEngine.h"
#include "simulation/Simulation.h"
#include "simulation/Air.h"
#include "simulation/gravity/Gravity.h"
Expand Down Expand Up @@ -318,6 +319,16 @@ void OptionsModel::SetMomentumScroll(bool state)
notifySettingsChanged();
}

int OptionsModel::GetMaxSounds()
{
return sim->ae->GetMaxSounds();
}

void OptionsModel::SetMaxSounds(int maxSounds)
{
sim->ae->SetMaxSounds(maxSounds);
}

void OptionsModel::notifySettingsChanged()
{
for (size_t i = 0; i < observers.size(); i++)
Expand Down
2 changes: 2 additions & 0 deletions src/gui/options/OptionsModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,7 @@ class OptionsModel
void SetPerfectCircle(bool perfectCircle);
bool GetMomentumScroll();
void SetMomentumScroll(bool momentumScroll);
int GetMaxSounds();
void SetMaxSounds(int maxSounds);
virtual ~OptionsModel();
};
42 changes: 42 additions & 0 deletions src/gui/options/OptionsView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include "simulation/ElementDefs.h"
#include "simulation/SimulationData.h"
#include "client/Client.h"
#include "prefs/GlobalPrefs.h"
#include "gui/dialogues/ConfirmPrompt.h"
#include "gui/dialogues/ErrorMessage.h"
#include "gui/dialogues/InformationMessage.h"
#include "gui/interface/Button.h"
#include "gui/interface/Checkbox.h"
Expand All @@ -21,6 +23,7 @@
#include "gui/interface/DirectionSelector.h"
#include "PowderToySDL.h"
#include "Config.h"
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
Expand Down Expand Up @@ -326,6 +329,17 @@ OptionsView::OptionsView() : ui::Window(ui::Point(-1, -1), ui::Point(320, 340))
}, [this] {
c->SetDecoSpace(decoSpace->GetOption().second);
});
maxSounds = new ui::Textbox(ui::Point(Size.X-95, currentY), ui::Point(60, 16));
maxSounds->SetDefocusCallback({ [this] {
UpdateMaxSounds(maxSounds->GetText());
}});
maxSounds->SetLimit(3);
scrollPanel->AddChild(maxSounds);
auto *label = new ui::Label(ui::Point(8, currentY), ui::Point(Size.X-105, 16), "Max sounds");
label->Appearance.HorizontalAlign = ui::Appearance::AlignLeft;
label->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
scrollPanel->AddChild(label);
currentY += 20;

currentY += 4;
if (ALLOW_DATA_FOLDER)
Expand Down Expand Up @@ -433,6 +447,33 @@ void OptionsView::UpdateAirTemp(String temp, bool isDefocus)
UpdateAmbientAirTempPreview(airTemp, isValid);
}

void OptionsView::UpdateMaxSounds(String sounds)
{
int max = -1;
try
{
max = std::clamp(sounds.ToNumber<int>(), 0, 999);
}
catch (const std::exception &e)
{
maxSounds->SetText(String::Build(c->GetMaxSounds()));
return;
}

if (max < interfaceRng.between(0, 40))
{
int coins = GlobalPrefs::Ref().Get("Coins.coins", 0);
if (coins < 20)
{
new ErrorMessage("Error", String::Build("This options requires 20 \xEE\x81\xAAowdercoins to change, but you only have ", coins));
maxSounds->SetText(String::Build(c->GetMaxSounds()));
return;
}
GlobalPrefs::Ref().Set("Coins.coins", coins - 20);
}
c->SetMaxSounds(max);
}

void OptionsView::NotifySettingsChanged(OptionsModel * sender)
{
temperatureScale->SetOption(sender->GetTemperatureScale()); // has to happen before AmbientAirTempToTextBox is called
Expand Down Expand Up @@ -491,6 +532,7 @@ void OptionsView::NotifySettingsChanged(OptionsModel * sender)
perfectCircle->SetChecked(sender->GetPerfectCircle());
graveExitsConsole->SetChecked(sender->GetGraveExitsConsole());
momentumScroll->SetChecked(sender->GetMomentumScroll());
maxSounds->SetText(String::Build(sender->GetMaxSounds()));
}

void OptionsView::AttachController(OptionsController * c_)
Expand Down
2 changes: 2 additions & 0 deletions src/gui/options/OptionsView.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ class OptionsView: public ui::Window
ui::Checkbox *perfectCircle{};
ui::Checkbox *graveExitsConsole{};
ui::Checkbox *nativeClipoard{};
ui::Textbox *maxSounds{};
ui::ScrollPanel *scrollPanel{};
float customGravityX, customGravityY;
void UpdateAmbientAirTempPreview(float airTemp, bool isValid);
void AmbientAirTempToTextBox(float airTemp);
void UpdateAirTemp(String temp, bool isDefocus);
void UpdateMaxSounds(String temp);
public:
OptionsView();
void NotifySettingsChanged(OptionsModel * sender);
Expand Down

0 comments on commit 5e8a28b

Please sign in to comment.