-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPGAudioTest.cpp
105 lines (82 loc) · 3.03 KB
/
APGAudioTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <chrono>
#include "APG/core/SDLGame.hpp"
#include "test/APGAudioTest.hpp"
bool APG::APGAudioTest::init() {
soundHandle = audioManager->loadSoundFile("assets/test_sound.wav");
spriteBatch = std::make_unique<SpriteBatch>();
font = fontManager->loadFontFile("assets/test_font.ttf", 12);
fontSprite =
fontManager->renderText(font,
"To test:\nP: load/unload music\nSpace: play from beginning (if loaded)\nA: pause (if playing)\nD: resume (if paused)\nEnter: play test sound\nNumber keys: change volume",
false, FontRenderMethod::NICE);
return true;
}
void APG::APGAudioTest::render(float deltaTime) {
(testHandle == -1 ? clearToRed() : clearToGreen());
glClear(GL_COLOR_BUFFER_BIT);
// Don't do this in production, but it's usefully hacky for testing.
for (int i = SDL_SCANCODE_1; i < SDL_SCANCODE_0; ++i) {
if (inputManager->isKeyJustPressed((SDL_Scancode) i)) {
const float volume = ((float) ((i - SDL_SCANCODE_1) + 1)) / 10.0f;
audioManager->setGlobalVolume(volume);
break;
}
}
if (inputManager->isKeyJustPressed(SDL_SCANCODE_0)) {
audioManager->setGlobalVolume(1.0f);
}
if (inputManager->isKeyJustPressed(SDL_SCANCODE_P)) {
if (testHandle == -1) {
testHandle = audioManager->loadMusicFile("assets/test_music.ogg");
} else {
audioManager->freeMusic(testHandle);
}
}
if (inputManager->isKeyJustPressed(SDL_SCANCODE_A)) {
audioManager->pauseMusic();
} else if (inputManager->isKeyJustPressed(SDL_SCANCODE_D)) {
audioManager->resumeMusic();
}
if (inputManager->isKeyJustPressed(SDL_SCANCODE_SPACE) && testHandle != -1) {
audioManager->playMusic(testHandle);
}
if (inputManager->isKeyJustPressed(SDL_SCANCODE_RETURN)) {
audioManager->playSound(soundHandle);
}
if (inputManager->isCtrlPressed() && inputManager->isKeyJustPressed(SDL_SCANCODE_Q)) {
quit();
}
spriteBatch->begin();
spriteBatch->draw(fontSprite, 5.0f, 5.0f);
spriteBatch->end();
SDL_GL_SwapWindow(window.get());
}
void APG::APGAudioTest::clearToRed() {
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
}
void APG::APGAudioTest::clearToGreen() {
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
}
int main(int argc, char **argv) {
auto apgContext = APG::APGContextBuilder("APG Audio Test", 640, 480).setGLContextVersion(3, 2).build();
auto game = std::make_unique<APG::APGAudioTest>(apgContext);
{
auto logger = spdlog::get("APG");
if (!game->init()) {
logger->critical("Couldn't init audio test.");
return EXIT_FAILURE;
}
auto startTime = std::chrono::high_resolution_clock::now();
logger->info(
"To test:\n\t> P to load/unload music\n\t> Space to play from beginning if loaded\n\t> A to pause if playing\n\t> D to resume if paused\n\t> Enter to play test sound.\n\t> Number keys to change volume");
while (true) {
const auto timeNow = std::chrono::high_resolution_clock::now();
const float deltaTime = std::chrono::duration_cast<std::chrono::milliseconds>(timeNow - startTime).count()
/ 1000.0f;
if (game->update(deltaTime)) {
break;
}
}
}
return EXIT_SUCCESS;
}