-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
255 lines (193 loc) · 8.15 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <glm/glm.hpp>
#include <glm/gtc/random.hpp>
#include "Game.hpp"
#include "renderer/WindowManager.hpp"
#include "ConstantForce.hpp"
#include "PolygonForce.hpp"
#include "HookForce.hpp"
#include "BrakeForce.hpp"
#include "GraphBrakeForce.hpp"
#include "GraphHookForce.hpp"
//FMOD
#include "fmod.h"
#include "fmod_errors.h"
#include <vector>
static const Uint32 WINDOW_WIDTH = 1024;
static const Uint32 WINDOW_HEIGHT = 1024;
using namespace imac3;
int main() {
WindowManager wm(WINDOW_WIDTH, WINDOW_HEIGHT, "ZIPIX");
wm.setFramerate(30);
// FMOD
FMOD_SYSTEM *system;
/* Création et initialisation d'un objet système */
FMOD_System_Create(&system);
FMOD_System_Init(system, 2, FMOD_INIT_NORMAL, NULL);
FMOD_SOUND* background = NULL;
FMOD_SOUND* firework = NULL;
FMOD_RESULT check;
check = FMOD_System_CreateSound(system, "../music/firework_explode_and_crackle.mp3", FMOD_CREATESAMPLE, 0, &firework);
if (check != FMOD_OK)
{
fprintf(stderr, "Impossible de lire le fichier firework_explode_and_crackle.mp3\n");
exit(EXIT_FAILURE);
}
check = FMOD_System_CreateSound(system, "../music/Timer.mp3", FMOD_SOFTWARE | FMOD_2D | FMOD_CREATESTREAM, 0, &background);
if (check != FMOD_OK)
{
fprintf(stderr, "Impossible de lire le fichier Timer.mp3\n");
exit(EXIT_FAILURE);
}
FMOD_CHANNEL *backgroundChannel = NULL;
FMOD_CHANNEL *fireworkChannel = NULL;
FMOD_System_GetChannel(system, 0, &backgroundChannel);
FMOD_System_GetChannel(system, 1, &fireworkChannel);
FMOD_Sound_SetLoopCount(background, -1);
FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, background, 0, &backgroundChannel);
// Time
time_t beginTime;
time_t currentTime;
time(&beginTime);
// Managers and Renderer
ParticleRenderer2D renderer;
ParticleManager snakeManager;
snakeManager.setHeadColor(glm::vec3(0.4f, 0.8f, 0.3f));
ParticleManager redManager;
redManager.setHeadColor(glm::vec3(0.9f, 0.4f, 0.3f));
ParticleManager blueManager;
blueManager.setHeadColor(glm::vec3(0.2f, 0.4f, 0.9f));
ParticleManager foodManager;
ParticleManager bonusManager;
ParticleManager fireworkManager;
ParticleManager autoManager;
// Graph Forces
GraphHookForce graphHook = GraphHookForce(1.f, 0.15f/4.f);
GraphBrakeForce graphBrake = GraphBrakeForce(0.3f, 10.f); // 0.5 = viscosité max
GraphHookForce redGraphHook = GraphHookForce(1.f, 0.15f/4.f);
GraphBrakeForce redGraphBrake = GraphBrakeForce(0.3f, 0.0001f);
GraphHookForce blueGraphHook = GraphHookForce(1.f, 0.15f/4.f);
GraphBrakeForce blueGraphBrake = GraphBrakeForce(0.3f, 0.0001f);
// Box
Polygon box = buildBox(glm::vec3(1.f, 1.f, 1.f), glm::vec2(-1.f, -1.f), 2, 2, true);
// // Ajout des particules
int id = foodManager.addRandomParticle(1);
bonusManager.addParticle(1.5f, glm::vec2(0.5, 0.5), glm::vec2(0, 0), glm::vec2(0, 0), glm::vec3(0.619f, 0.05f, 0.25f));
bonusManager.addParticle(1.5f, glm::vec2(-0.2, 0.6), glm::vec2(0, 0), glm::vec2(0, 0), glm::vec3(0.619f, 0.05f, 0.25f));
bonusManager.addParticle(1.5f, glm::vec2(0.8, -0.7), glm::vec2(0, 0), glm::vec2(0, 0), glm::vec3(0.619f, 0.05f, 0.25f));
bonusManager.addParticle(1.5f, glm::vec2(-0.7, -0.5), glm::vec2(0, 0), glm::vec2(0, 0), glm::vec3(0.619f, 0.05f, 0.25f));
bonusManager.addParticle(1.5f, glm::vec2(0.3, -0.1), glm::vec2(0, 0), glm::vec2(0, 0), glm::vec3(0.619f, 0.05f, 0.25f));
// Copy the food to the autoManager
copyParticle(foodManager, autoManager, id);
// Forces
ConstantForce mg(glm::vec2(0.f, -0.005));
// LeapfrogSolver
LeapfrogSolver leapfrog;
PolygonForce boxForce(box, 1.5f, leapfrog);
// Snake's creation
ParticleGraph snakeGraph = createString(glm::vec2(0.f, 0.0f), glm::vec2(0.f, -0.15f), glm::vec3(0.2f, 0.6f, 0.2f), glm::vec3(0.4f, 0.8f, 0.3f), 4.f, snakeManager);
ParticleGraph redGraph = createString(glm::vec2(0.f, 0.2f), glm::vec2(0.15f, 0.2f), glm::vec3(0.9f, 0.2f, 0.2f), glm::vec3(0.9f, 0.4f, 0.3f), 4.f, redManager);
ParticleGraph blueGraph = createString(glm::vec2(0.f, -0.2f), glm::vec2(-0.15f, -0.2f), glm::vec3(0.2f, 0.2f, 0.6f), glm::vec3(0.2f, 0.4f, 0.9f), 4.f, blueManager);
copyParticle(snakeManager, autoManager, 0);
copyParticle(redManager, autoManager, 0);
copyParticle(blueManager, autoManager, 0);
// Variables
int score = 0;
int bonus = 0;
// Temps s'écoulant entre chaque frame
float dt = 0.f;
bool done = false;
while(!done) {
time(¤tTime);
wm.startMainLoop();
// Renderer
renderer.clear();
// Draw particles
foodManager.drawParticles(renderer);
snakeManager.drawParticles(renderer);
redManager.drawParticles(renderer);
blueManager.drawParticles(renderer);
fireworkManager.drawParticles(renderer);
bonusManager.drawParticles(renderer);
mg.apply(fireworkManager);
// Mise à jour du graph autoGraph
updateParticle(snakeManager, 0, autoManager, 1);
updateParticle(redManager, 0, autoManager, 2);
updateParticle(blueManager, 0, autoManager, 3);
// Force attractive
addAttractiveForce(foodManager, snakeManager);
addAttractiveForce(foodManager, redManager);
addAttractiveForce(foodManager, blueManager);
// Forces
graphHook.setGraph(&snakeGraph);
graphBrake.setGraph(&snakeGraph);
redGraphHook.setGraph(&redGraph);
redGraphBrake.setGraph(&redGraph);
blueGraphHook.setGraph(&blueGraph);
blueGraphBrake.setGraph(&blueGraph);
// Simulation
if(dt != 0) {
//Bonus
if(difftime(currentTime, beginTime) == 10) {
beginTime = currentTime;
time(¤tTime);
addBonus(bonusManager);
bonus++;
}
// Snake - Food
if( checkFoodCollision(snakeGraph, snakeManager, foodManager, fireworkManager, 0.05f, 0) != -1
|| checkFoodCollision(redGraph, redManager, foodManager, fireworkManager, 0.05f, 0) != -1
|| checkFoodCollision(blueGraph, blueManager, foodManager, fireworkManager, 0.05f, 0) != -1) {
FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, firework, 0, &fireworkChannel);
updateParticle(foodManager, 0, autoManager, 0);
bonus++;
}
// Apply forces
graphBrake.setDt(dt);
redGraphBrake.setDt(dt);
blueGraphBrake.setDt(dt);
boxForce.setDt(dt);
boxForce.apply(snakeManager);
boxForce.apply(redManager);
boxForce.apply(blueManager);
graphHook.apply(snakeManager);
graphBrake.apply(snakeManager);
redGraphHook.apply(redManager);
redGraphBrake.apply(redManager);
blueGraphHook.apply(blueManager);
blueGraphBrake.apply(blueManager);
addRepulsiveForce(bonusManager, snakeManager);
addRepulsiveForce(bonusManager, redManager);
addRepulsiveForce(bonusManager, blueManager);
// Leapfrog solver
leapfrog.solve(snakeManager, dt);
leapfrog.solve(autoManager, dt);
leapfrog.solve(redManager, dt);
leapfrog.solve(blueManager, dt);
leapfrog.solve(fireworkManager, dt);
leapfrog.solve(bonusManager, dt);
}
// Gestion des evenements
SDL_Event e;
while(wm.pollEvent(e)) {
switch(e.type) {
default:
break;
case SDL_QUIT:
//FMOD
/* On libère le son et on ferme et libère l'objet système */
FMOD_Sound_Release(background);
FMOD_Sound_Release(firework);
FMOD_System_Close(system);
FMOD_System_Release(system);
done = true;
break;
}
}
// Mise à jour de la fenêtre
dt = wm.update();
}
return EXIT_SUCCESS;
}