Skip to content

Commit 521e9a3

Browse files
committed
extract options class
1 parent 76c54a5 commit 521e9a3

3 files changed

Lines changed: 186 additions & 96 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#ifndef ENGINE_OPTIONS_H_
2+
#define ENGINE_OPTIONS_H_
3+
4+
#include <SFG/Own2dEngine/Game/ui/label.h>
5+
#include <SFG/Own2dEngine/Logger/_include.h>
6+
#include <portaudio.h>
7+
8+
namespace SFG {
9+
namespace Own2dEngine {
10+
namespace Game {
11+
12+
class Options {
13+
public:
14+
bool GetDebug();
15+
void SetDebug( bool value );
16+
17+
void AudioSelectPrevInput();
18+
void AudioSelectNextInput();
19+
void AudioSelectPrevOutput();
20+
void AudioSelectNextOutput();
21+
void SetAudioDisplayLabels( SFG::Own2dEngine::Game::UI::Label* input, SFG::Own2dEngine::Game::UI::Label* output );
22+
void SetAudioDevices( std::pair< std::vector< PaDeviceIndex >, std::vector< PaDeviceIndex > > const& devices );
23+
void SetAudioIndexes( int32_t inputIndex, int32_t outputIndex );
24+
25+
protected:
26+
// some functions maybe
27+
28+
protected:
29+
int32_t audioInputIndex_ = -1;
30+
int32_t audioOutputIndex_ = -1;
31+
32+
private:
33+
SFG::Own2dEngine::Logger::spdlogger logger_ = SFG::Own2dEngine::Logger::LoggerFactory::get_logger( "Options" );
34+
35+
bool debug_ = false;
36+
37+
#pragma region audio options helper variables
38+
SFG::Own2dEngine::Game::UI::Label* audioInputLabel_ = nullptr;
39+
SFG::Own2dEngine::Game::UI::Label* audioOutputLabel_ = nullptr;
40+
std::pair< std::vector< PaDeviceIndex >, std::vector< PaDeviceIndex > > devices_;
41+
#pragma endregion
42+
};
43+
44+
} // namespace Game
45+
} // namespace Own2dEngine
46+
} // namespace SFG
47+
48+
#endif /* ENGINE_OPTIONS_H_ */

game/src/main.cpp

Lines changed: 27 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <SDL2/SDL_ttf.h>
55
#include <SFG/Own2dEngine/Game/audioManager.h>
66
#include <SFG/Own2dEngine/Game/inputManager.h>
7+
#include <SFG/Own2dEngine/Game/options.h>
78
#include <SFG/Own2dEngine/Game/performance.h>
89
#include <SFG/Own2dEngine/Game/ui/button.h>
910
#include <SFG/Own2dEngine/Game/ui/label.h>
@@ -19,82 +20,6 @@ namespace SFGO2DL = SFG::Own2dEngine::Logger;
1920
namespace SFGO2DG = SFG::Own2dEngine::Game;
2021
namespace SFGO2DU = SFG::Own2dEngine::Utils;
2122

22-
struct TempAudioStruct {
23-
SFGO2DG::UI::Label* audioInputLabel = nullptr;
24-
SFGO2DG::UI::Label* audioOutputLabel = nullptr;
25-
PaDeviceIndex inputDeviceIndex = paNoDevice;
26-
PaDeviceIndex outputDeviceIndex = paNoDevice;
27-
std::pair< std::vector< PaDeviceIndex >, std::vector< PaDeviceIndex > > devices;
28-
29-
void SetAudioDeviceIndexes( int32_t inputIndex, int32_t outputIndex ) {
30-
spdlog::trace( "[TempAudioStruct] SetAudioDeviceIndexes( inputIndex: {}, outputIndex: {} )", inputIndex, outputIndex );
31-
inputIndex = SFGO2DU::mod< int32_t >( inputIndex + 1, this->devices.first.size() + 1 ) - 1;
32-
outputIndex = SFGO2DU::mod< int32_t >( outputIndex + 1, this->devices.second.size() + 1 ) - 1;
33-
34-
PaDeviceIndex tmpInput;
35-
PaDeviceIndex tmpOutput;
36-
if( inputIndex < 0 ) {
37-
tmpInput = Pa_GetDefaultInputDevice();
38-
} else {
39-
tmpInput = this->devices.first[inputIndex];
40-
}
41-
if( outputIndex < 0 ) {
42-
tmpOutput = Pa_GetDefaultOutputDevice();
43-
} else {
44-
tmpOutput = this->devices.second[outputIndex];
45-
}
46-
47-
{
48-
PaDeviceInfo const* deviceInfo = nullptr;
49-
PaHostApiInfo const* hostApiInfo = nullptr;
50-
std::string deviceName = "__UNKNOWN__";
51-
std::string apiName = "__UNKNOWN__";
52-
53-
deviceInfo = Pa_GetDeviceInfo( tmpInput );
54-
if( deviceInfo ) {
55-
deviceName = deviceInfo->name;
56-
hostApiInfo = Pa_GetHostApiInfo( deviceInfo->hostApi );
57-
if( hostApiInfo ) {
58-
apiName = hostApiInfo->name;
59-
}
60-
}
61-
if( this->audioInputLabel ) {
62-
this->audioInputLabel->SetText( fmt::format( "Audio Input: ({}) {}", apiName, deviceName ) );
63-
}
64-
65-
deviceInfo = nullptr;
66-
hostApiInfo = nullptr;
67-
deviceName = "__UNKNOWN__";
68-
apiName = "__UNKNOWN__";
69-
70-
deviceInfo = Pa_GetDeviceInfo( tmpOutput );
71-
if( deviceInfo ) {
72-
deviceName = deviceInfo->name;
73-
hostApiInfo = Pa_GetHostApiInfo( deviceInfo->hostApi );
74-
if( hostApiInfo ) {
75-
apiName = hostApiInfo->name;
76-
}
77-
}
78-
if( this->audioOutputLabel ) {
79-
this->audioOutputLabel->SetText( fmt::format( "Audio Output: ({}) {}", apiName, deviceName ) );
80-
}
81-
}
82-
83-
this->inputDeviceIndex = inputIndex;
84-
this->outputDeviceIndex = outputIndex;
85-
86-
SFGO2DG::AudioManager::StopAudio( "Background Music" );
87-
88-
SFGO2DG::AudioManager::Start( tmpInput, tmpOutput );
89-
90-
SFGO2DG::AudioManager::LoadAudioFile( "Background Music", SFGO2DG::AudioManager::AudioType::BGM, "Resources/Audio/8Bit 01 w.wav" );
91-
}
92-
void PrevInput() { this->SetAudioDeviceIndexes( this->inputDeviceIndex - 1, this->outputDeviceIndex ); }
93-
void NextInput() { this->SetAudioDeviceIndexes( this->inputDeviceIndex + 1, this->outputDeviceIndex ); }
94-
void PrevOutput() { this->SetAudioDeviceIndexes( this->inputDeviceIndex, this->outputDeviceIndex - 1 ); }
95-
void NextOutput() { this->SetAudioDeviceIndexes( this->inputDeviceIndex, this->outputDeviceIndex + 1 ); }
96-
};
97-
9823
int main( int argc, char** argv ) noexcept {
9924
int better_main( std::vector< std::string > const& ) noexcept;
10025
std::vector< std::string > args( argv, std::next( argv, static_cast< std::ptrdiff_t >( argc ) ) );
@@ -114,15 +39,15 @@ int better_main( std::vector< std::string > const& args ) noexcept {
11439
#pragma region Initialize Audio
11540
SFGO2DG::AudioManager::init();
11641
SFGO2DG::AudioManager::GetAudioInfos();
117-
118-
TempAudioStruct* audioStruct = new TempAudioStruct();
119-
audioStruct->inputDeviceIndex = paNoDevice;
120-
audioStruct->outputDeviceIndex = paNoDevice;
121-
audioStruct->devices = SFGO2DG::AudioManager::TestABunchOfShit();
122-
123-
audioStruct->SetAudioDeviceIndexes( -1, -1 );
12442
#pragma endregion Initialize Audio
12543

44+
#pragma region Initialize Options
45+
SFGO2DG::Options* options = new SFGO2DG::Options();
46+
options->SetAudioDevices( SFGO2DG::AudioManager::TestABunchOfShit() );
47+
// having this later would be funny
48+
// options->SetAudioIndexes( -1, -1 );
49+
#pragma endregion Initialize Options
50+
12651
#pragma region Initialize SDL
12752
int sdlErrorCode;
12853
spdlog::trace( "better_main - initializing sdl" );
@@ -197,39 +122,39 @@ int better_main( std::vector< std::string > const& args ) noexcept {
197122
#pragma region Options UI
198123
float optionsMenuButtonHeight = 0.0625f;
199124
float optionsMenuYOffset = 0.5f;
125+
SFGO2DG::UI::Label* audioInputLabel;
126+
SFGO2DG::UI::Label* audioOutputLabel;
200127
{
201-
std::function< void() > func = [audioStruct]() { audioStruct->PrevInput(); };
128+
std::function< void() > func = [options]() { options->AudioSelectPrevInput(); };
202129
SFGO2DG::UI::Button* tmp = new SFGO2DG::UI::Button( func, "<", optionsPage, SDL_FRect{ 0.25f, optionsMenuYOffset, 0.0625f, optionsMenuButtonHeight } );
203130
tmp->SetHorizontalAlignment( SFGO2DG::UI::Label::HorizontalAlignment::Centered );
204131
tmp->SetVerticalAlignment( SFGO2DG::UI::Label::VerticalAlignment::Centered );
205132
}
206133
{
207-
audioStruct->audioInputLabel
208-
= new SFGO2DG::UI::Label( "Input Device Name", optionsPage, SDL_FRect{ 0.3125f, optionsMenuYOffset, 0.375f, optionsMenuButtonHeight } );
209-
audioStruct->audioInputLabel->SetHorizontalAlignment( SFGO2DG::UI::Label::HorizontalAlignment::Centered );
210-
audioStruct->audioInputLabel->SetVerticalAlignment( SFGO2DG::UI::Label::VerticalAlignment::Centered );
134+
audioInputLabel = new SFGO2DG::UI::Label( "Input Device Name", optionsPage, SDL_FRect{ 0.3125f, optionsMenuYOffset, 0.375f, optionsMenuButtonHeight } );
135+
audioInputLabel->SetHorizontalAlignment( SFGO2DG::UI::Label::HorizontalAlignment::Centered );
136+
audioInputLabel->SetVerticalAlignment( SFGO2DG::UI::Label::VerticalAlignment::Centered );
211137
}
212138
{
213-
std::function< void() > func = [audioStruct]() { audioStruct->NextInput(); };
139+
std::function< void() > func = [options]() { options->AudioSelectNextInput(); };
214140
SFGO2DG::UI::Button* tmp = new SFGO2DG::UI::Button( func, ">", optionsPage, SDL_FRect{ 0.6875f, optionsMenuYOffset, 0.0625f, optionsMenuButtonHeight } );
215141
tmp->SetHorizontalAlignment( SFGO2DG::UI::Label::HorizontalAlignment::Centered );
216142
tmp->SetVerticalAlignment( SFGO2DG::UI::Label::VerticalAlignment::Centered );
217143
}
218144
optionsMenuYOffset += optionsMenuButtonHeight;
219145
{
220-
std::function< void() > func = [audioStruct]() { audioStruct->PrevOutput(); };
146+
std::function< void() > func = [options]() { options->AudioSelectPrevOutput(); };
221147
SFGO2DG::UI::Button* tmp = new SFGO2DG::UI::Button( func, "<", optionsPage, SDL_FRect{ 0.25f, optionsMenuYOffset, 0.0625f, optionsMenuButtonHeight } );
222148
tmp->SetHorizontalAlignment( SFGO2DG::UI::Label::HorizontalAlignment::Centered );
223149
tmp->SetVerticalAlignment( SFGO2DG::UI::Label::VerticalAlignment::Centered );
224150
}
225151
{
226-
audioStruct->audioOutputLabel
227-
= new SFGO2DG::UI::Label( "Output Device Name", optionsPage, SDL_FRect{ 0.3125f, optionsMenuYOffset, 0.375f, optionsMenuButtonHeight } );
228-
audioStruct->audioOutputLabel->SetHorizontalAlignment( SFGO2DG::UI::Label::HorizontalAlignment::Centered );
229-
audioStruct->audioOutputLabel->SetVerticalAlignment( SFGO2DG::UI::Label::VerticalAlignment::Centered );
152+
audioOutputLabel = new SFGO2DG::UI::Label( "Output Device Name", optionsPage, SDL_FRect{ 0.3125f, optionsMenuYOffset, 0.375f, optionsMenuButtonHeight } );
153+
audioOutputLabel->SetHorizontalAlignment( SFGO2DG::UI::Label::HorizontalAlignment::Centered );
154+
audioOutputLabel->SetVerticalAlignment( SFGO2DG::UI::Label::VerticalAlignment::Centered );
230155
}
231156
{
232-
std::function< void() > func = [audioStruct]() { audioStruct->NextOutput(); };
157+
std::function< void() > func = [options]() { options->AudioSelectNextOutput(); };
233158
SFGO2DG::UI::Button* tmp = new SFGO2DG::UI::Button( func, ">", optionsPage, SDL_FRect{ 0.6875f, optionsMenuYOffset, 0.0625f, optionsMenuButtonHeight } );
234159
tmp->SetHorizontalAlignment( SFGO2DG::UI::Label::HorizontalAlignment::Centered );
235160
tmp->SetVerticalAlignment( SFGO2DG::UI::Label::VerticalAlignment::Centered );
@@ -244,6 +169,7 @@ int better_main( std::vector< std::string > const& args ) noexcept {
244169
tmp->SetHorizontalAlignment( SFGO2DG::UI::Label::HorizontalAlignment::Centered );
245170
tmp->SetVerticalAlignment( SFGO2DG::UI::Label::VerticalAlignment::Centered );
246171
}
172+
options->SetAudioDisplayLabels( audioInputLabel, audioOutputLabel );
247173
#pragma endregion Options UI
248174

249175
for( int i = 0; i < widgetsToCycle.size(); i++ ) {
@@ -272,6 +198,11 @@ int better_main( std::vector< std::string > const& args ) noexcept {
272198
}
273199
SDL_SetRenderDrawBlendMode( mainWindowRenderer, SDL_BlendMode::SDL_BLENDMODE_BLEND );
274200

201+
#pragma region Rest of options
202+
// rest of options?
203+
options->SetAudioIndexes( -1, -1 );
204+
#pragma endregion Rest of options
205+
275206
SDL_ShowWindow( mainWindow );
276207

277208
bool running = true;
@@ -360,7 +291,7 @@ int better_main( std::vector< std::string > const& args ) noexcept {
360291
#pragma endregion Quit SDL
361292

362293
SFGO2DG::AudioManager::Shutdown();
363-
delete audioStruct;
294+
delete options;
364295

365296
SFGO2DG::Performance::endProgram();
366297

game/src/options.cpp

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include "SFG/Own2dEngine/Game/options.h"
2+
3+
#include <SFG/Own2dEngine/Game/audioManager.h>
4+
#include <SFG/Own2dEngine/Utils/_include.h>
5+
6+
namespace SFG {
7+
namespace Own2dEngine {
8+
namespace Game {
9+
10+
bool Options::GetDebug() {
11+
return debug_;
12+
}
13+
14+
void Options::SetDebug( bool value ) {
15+
debug_ = value;
16+
}
17+
18+
void Options::AudioSelectPrevInput() {
19+
SetAudioIndexes( audioInputIndex_ - 1, audioOutputIndex_ );
20+
}
21+
22+
void Options::AudioSelectNextInput() {
23+
SetAudioIndexes( audioInputIndex_ + 1, audioOutputIndex_ );
24+
}
25+
26+
void Options::AudioSelectPrevOutput() {
27+
SetAudioIndexes( audioInputIndex_, audioOutputIndex_ - 1 );
28+
}
29+
30+
void Options::AudioSelectNextOutput() {
31+
SetAudioIndexes( audioInputIndex_, audioOutputIndex_ + 1 );
32+
}
33+
34+
void Options::SetAudioDisplayLabels( SFG::Own2dEngine::Game::UI::Label* input, SFG::Own2dEngine::Game::UI::Label* output ) {
35+
audioInputLabel_ = input;
36+
audioOutputLabel_ = output;
37+
}
38+
39+
void Options::SetAudioDevices( std::pair< std::vector< PaDeviceIndex >, std::vector< PaDeviceIndex > > const& devices ) {
40+
devices_ = devices;
41+
}
42+
43+
void Options::SetAudioIndexes( int32_t inputIndex, int32_t outputIndex ) {
44+
spdlog::trace( "[TempAudioStruct] SetAudioDeviceIndexes( inputIndex: {}, outputIndex: {} )", inputIndex, outputIndex );
45+
inputIndex = SFG::Own2dEngine::Utils::mod< int32_t >( inputIndex + 1, devices_.first.size() + 1 ) - 1;
46+
outputIndex = SFG::Own2dEngine::Utils::mod< int32_t >( outputIndex + 1, devices_.second.size() + 1 ) - 1;
47+
48+
PaDeviceIndex tmpInput;
49+
PaDeviceIndex tmpOutput;
50+
if( inputIndex < 0 ) {
51+
tmpInput = Pa_GetDefaultInputDevice();
52+
} else {
53+
tmpInput = devices_.first[inputIndex];
54+
}
55+
if( outputIndex < 0 ) {
56+
tmpOutput = Pa_GetDefaultOutputDevice();
57+
} else {
58+
tmpOutput = devices_.second[outputIndex];
59+
}
60+
61+
{
62+
PaDeviceInfo const* deviceInfo = nullptr;
63+
PaHostApiInfo const* hostApiInfo = nullptr;
64+
std::string deviceName = "__UNKNOWN__";
65+
std::string apiName = "__UNKNOWN__";
66+
67+
deviceInfo = Pa_GetDeviceInfo( tmpInput );
68+
if( deviceInfo ) {
69+
deviceName = deviceInfo->name;
70+
hostApiInfo = Pa_GetHostApiInfo( deviceInfo->hostApi );
71+
if( hostApiInfo ) {
72+
apiName = hostApiInfo->name;
73+
}
74+
}
75+
if( audioInputLabel_ ) {
76+
audioInputLabel_->SetText( fmt::format( "Audio Input: ({}) {}", apiName, deviceName ) );
77+
}
78+
79+
deviceInfo = nullptr;
80+
hostApiInfo = nullptr;
81+
deviceName = "__UNKNOWN__";
82+
apiName = "__UNKNOWN__";
83+
84+
deviceInfo = Pa_GetDeviceInfo( tmpOutput );
85+
if( deviceInfo ) {
86+
deviceName = deviceInfo->name;
87+
hostApiInfo = Pa_GetHostApiInfo( deviceInfo->hostApi );
88+
if( hostApiInfo ) {
89+
apiName = hostApiInfo->name;
90+
}
91+
}
92+
if( audioOutputLabel_ ) {
93+
audioOutputLabel_->SetText( fmt::format( "Audio Output: ({}) {}", apiName, deviceName ) );
94+
}
95+
}
96+
97+
audioInputIndex_ = inputIndex;
98+
audioOutputIndex_ = outputIndex;
99+
100+
SFG::Own2dEngine::Game::AudioManager::StopAudio( "Background Music" );
101+
102+
SFG::Own2dEngine::Game::AudioManager::Start( tmpInput, tmpOutput );
103+
104+
SFG::Own2dEngine::Game::AudioManager::LoadAudioFile( "Background Music",
105+
SFG::Own2dEngine::Game::AudioManager::AudioType::BGM,
106+
"Resources/Audio/8Bit 01 w.wav" );
107+
}
108+
109+
} // namespace Game
110+
} // namespace Own2dEngine
111+
} // namespace SFG

0 commit comments

Comments
 (0)