-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
194 lines (166 loc) · 6.33 KB
/
main.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <string>
#include "ShaderSystemConfig.h"
#include "Graphics.h"
#include "Configuration.h"
#include "Music.h"
#include "Synchronizer.h"
#include "Textures.h"
#include "Logger.h"
#include "InputDevices.h"
#include "Helpers.h"
#include "Camera.h"
#include "Framebuffer.h"
const std::string VERSION = std::to_string(ShaderSystem_VERSION_MAJOR) + "." + std::to_string(ShaderSystem_VERSION_MINOR);
void mainLoop();
void cleanUp();
void handleKeyboard(GLFWwindow *window, int key, int scancode, int action, int mods);
void handleMouseMove(GLFWwindow *window, double x, double y);
void handleMouseButtons(GLFWwindow *window, int button, int action, int mods);
void musicPause(void *c, int flag);
void musicSetRow(void *c, int row);
int musicPlaying(void *c);
DemoSystem::Configuration *configurations;
DemoSystem::Music music;
DemoSystem::Synchronizer synchronizer;
DemoSystem::Textures textures;
DemoSystem::Logger logger;
DemoSystem::Graphics graphics;
DemoSystem::InputDevices inputDevices;
DemoSystem::Camera camera;
int main(int argc, char *args[])
{
std::string confFile = "configuration.json";
if (argc > 1)
{
confFile = std::string(args[1]);
}
configurations = DemoSystem::Configuration::getInstance();
bool confReadOk = configurations->read(confFile);
graphics.initialize(configurations->shaders, configurations->screen, configurations->demo);
logger.initialize(25, configurations->screen, configurations->demo);
logger.write(DemoSystem::Logger::INFO, "shader system version " + VERSION + " by tahtituho 2023");
logger.write(DemoSystem::Logger::INFO, "opengl vendor: " + std::string((const char *)glGetString(GL_VENDOR)));
logger.write(DemoSystem::Logger::INFO, "opengl renderer: " + std::string((const char *)glGetString(GL_RENDERER)));
logger.write(DemoSystem::Logger::INFO, "opengl version: " + std::string((const char *)glGetString(GL_VERSION)));
logger.write(DemoSystem::Logger::INFO, "shading version: " + std::string((const char *)glGetString(GL_SHADING_LANGUAGE_VERSION)));
logger.write(DemoSystem::Logger::INFO, "bass version: " + music.version());
if (!confReadOk)
{
logger.write(DemoSystem::Logger::ERR, "configuration file not found: " + confFile);
}
if (!music.initialize(configurations->tune.frequency, configurations->tune.file))
{
logger.write(DemoSystem::Logger::ERR, "error initializing song: " + configurations->tune.file);
}
synchronizer.initialize(configurations->tune.BPM, configurations->sync.RPB);
bool rocketClientNeeded = !configurations->demo.release && configurations->sync.enabled;
if (rocketClientNeeded)
{
bool rocketConnection = synchronizer.connectPlayer(configurations->sync.host);
if (!rocketConnection)
{
logger.write(DemoSystem::Logger::ERR, "cannot connect to rocket. make sure that rocket is running and restart shader system.");
}
}
sync_cb functions;
functions.is_playing = &musicPlaying;
functions.pause = &musicPause;
functions.set_row = &musicSetRow;
synchronizer.setFunctions(&functions);
synchronizer.initializeTrackVariables(configurations->trackVariables);
synchronizer.initializeBasicVariables();
textures.setTextures(configurations->textures);
// Main Graphics registrations
graphics.registerLogger(&logger);
graphics.registerKeyboardCallback(&handleKeyboard);
graphics.registerMouseMoveCallback(&handleMouseMove);
graphics.registerMouseButtonsCallback(&handleMouseButtons);
graphics.registerTextures(&textures.textures);
graphics.registerSynchronizer(&synchronizer);
graphics.registerCamera(&camera);
// Main shader configuration
std::string vertexSource = DemoSystem::Helpers::readFile(configurations->shaders.vertex);
std::string fragmentSource = DemoSystem::Helpers::readFile(configurations->shaders.fragment);
graphics.mainShader.initShaders(vertexSource, fragmentSource);
graphics.mainShader.addUniforms();
graphics.mainShader.initFrameBuffer();
// Post-processing shader configuration
std::string vertexPostSource = DemoSystem::Helpers::readFile(configurations->shaders.vertexPost);
std::string fragmentPostSource = DemoSystem::Helpers::readFile(configurations->shaders.fragmentPost);
graphics.postprocessingShader.initShaders(vertexPostSource, fragmentPostSource);
graphics.postprocessingShader.addUniformsPost();
graphics.postprocessingShader.initFrameBuffer();
graphics.postprocessingShader.generateFBO(configurations->screen.width, configurations->screen.height);
inputDevices.initialize(&graphics, &music, &logger, &synchronizer, &camera, configurations->demo.release);
music.play();
mainLoop();
cleanUp();
return 0;
}
void musicPause(void *rr, int flag)
{
if (flag == 1)
{
music.pause();
}
else
{
music.play();
}
}
void musicSetRow(void *rr, int row)
{
double rowRate = *((double *)rr);
music.seek(row / rowRate);
}
int musicPlaying(void *rr)
{
if (music.isPlaying())
{
return 1;
}
else
{
return 0;
}
}
void mainLoop()
{
while (!graphics.shouldStop())
{
// Bind framebuffer and render to framebuffers texture the main shader pass
graphics.postprocessingShader.bind();
double position = music.position();
synchronizer.update(position);
graphics.mainShader.render(position);
camera.update();
// Post-processing pass, unbind framebuffer and draw to screen from framebuffer
graphics.postprocessingShader.unBind();
graphics.postprocessingShader.drawFBO(position);
logger.render();
graphics.swapBuffers();
if (configurations->demo.release == true && music.hasMusicEnded() == true)
{
graphics.initStop();
}
}
}
void handleKeyboard(GLFWwindow *window, int key, int scancode, int action, int mods)
{
inputDevices.handleKeyboard(window, key, scancode, action, mods);
}
void handleMouseMove(GLFWwindow *window, double x, double y)
{
inputDevices.handleMouseMove(window, x, y);
}
void handleMouseButtons(GLFWwindow *window, int button, int action, int mods)
{
inputDevices.handleMouseButtons(window, button, action, mods);
}
void cleanUp()
{
logger.cleanUp();
synchronizer.cleanUp();
music.cleanUp();
graphics.cleanUp();
}