This repository has been archived by the owner on Oct 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
332 lines (282 loc) · 9.84 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <SDL2/SDL.h>
#include <cmath>
#include <iostream>
#include <vector>
#include "ui/imgui_impl_sdl.h"
#include "./draw.h"
#include "./timer.h"
#include "entities/campoVettoriale.h"
#include "entities/charge.h"
#include "entities/lineeDiForza.h"
#include "entities/sorgente.h"
#include "entities/vector.h"
#include "fisica.h"
#include "graph.h"
#include "macros.h"
#include "settings.h"
#include "ui/IconsMaterialDesign.h"
#include "ui/material_icons.h"
#include "ui/ui.h"
#include "utils.h"
#if !SDL_VERSION_ATLEAST(2, 0, 17)
#error This backend requires SDL 2.0.17+ because of SDL_RenderGeometry() function
#endif
SDL_Window *gWindow = NULL;
SDL_Renderer *gRenderer = NULL;
bool init() {
bool success = true;
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
printf("SDL could not be initialized, error: %s\n", SDL_GetError());
success = false;
} else {
// THIS MAKES SURE COMPOSITOR IS NOT DISABLED AUTOMATICALLY ON LINUX
SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
gWindow =
SDL_CreateWindow("Campo Elettrostatico", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT,
window_flags);
if (gWindow == NULL) {
printf("Window could not be initialized, error: %s\n", SDL_GetError());
success = false;
} else {
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if (gRenderer == NULL) {
printf(
"Renderer could not be initialized, error: "
"%s\n",
SDL_GetError());
} else {
SDL_RendererInfo info;
SDL_GetRendererInfo(gRenderer, &info);
SDL_Log("Current SDL_Renderer: %s", info.name);
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_SetRenderDrawBlendMode(gRenderer, SDL_BLENDMODE_BLEND);
}
}
}
return success;
}
Canvas Settings::canvas = Canvas(NULL, {200, 200});
int main(int argc, char **argv) {
if (!init()) {
std::cout << "Init di SDL fallito" << std::endl;
return 1;
}
bool quit = false;
SDL_Event e;
Timer stepTimer;
bool pause = true;
int x, y;
Graph grafico;
vector2 intensitaMouse = {0, 0};
std::vector<Sorgente> sorgenti;
std::vector<Sorgente>::iterator itSorgenti;
std::vector<PuntoDelCampo> punti;
std::vector<PuntoDelCampo>::iterator it;
std::vector<Carica>::iterator itCariche;
std::vector<Carica> cariche;
std::vector<CaricaLineaDiForza> lineeDiForza;
std::vector<vector2> mouseVector;
Sorgente sorgentePrima = Sorgente({100, 300}, 2.5e-9);
Sorgente sorgenteSeconda = Sorgente({400, 300}, 5.2e-8);
sorgenti.push_back(sorgentePrima);
sorgenti.push_back(sorgenteSeconda);
Settings::canvas = Canvas(gRenderer, {200, 200});
setDensity(punti, densita);
// IMGUI
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
(void)io;
io.WantCaptureKeyboard = true;
io.Fonts->AddFontFromMemoryCompressedTTF(robotoFont_compressed_data, robotoFont_compressed_size, 16);
// MATERIAL DESIGN ICONS
static const ImWchar icons_ranges[] = {ICON_MIN_MD, ICON_MAX_16_MD, 0};
ImFontConfig icons_config;
icons_config.MergeMode = true;
icons_config.PixelSnapH = true;
icons_config.GlyphOffset.y += 3.0f;
io.Fonts->AddFontFromMemoryCompressedTTF(material_icons_compressed_data,
material_icons_compressed_size,
16.0f, &icons_config, icons_ranges);
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.IniFilename = NULL;
/** NOT YET SUPPORTED TODO: update when available
* io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
*/
ImGui_ImplSDL2_InitForSDLRenderer(gWindow, gRenderer);
ImGui_ImplSDLRenderer_Init(gRenderer);
while (!quit) {
int wheel = 0;
SDL_GetMouseState(&x, &y);
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_WINDOWEVENT) {
if (e.window.event == SDL_WINDOWEVENT_CLOSE && e.window.windowID == SDL_GetWindowID(gWindow))
quit = true;
if (e.window.event == SDL_WINDOWEVENT_RESIZED) {
SDL_SetWindowSize(gWindow, e.window.data1, e.window.data2);
SCREEN_WIDTH = e.window.data1;
SCREEN_HEIGHT = e.window.data2;
setDensity(punti, densita);
// TODO: resizing window fix for imgui
// ImGuiSDL::Deinitialize();
// ImGuiSDL::Initialize(gRenderer, SCREEN_WIDTH, SCREEN_HEIGHT);
}
} else if (e.type == SDL_QUIT) {
quit = true;
}
if (io.WantCaptureMouse || io.WantCaptureKeyboard) {
ImGui_ImplSDL2_ProcessEvent(&e);
} else {
Settings::canvas.handleEvents(e, x, y);
if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
case SDLK_n:
addSorgenteFunc(sorgenti, -1, -1);
break;
case SDLK_UP:
lunghezza += 10;
break;
case SDLK_DOWN:
lunghezza -= 10;
break;
case SDLK_r:
sorgenti.clear();
break;
case SDLK_k:
grafico.puntiDelGrafico.clear();
break;
case SDLK_SPACE:
addCaricaFunc(cariche, x, y);
break;
case SDLK_p:
pause = !pause;
break;
}
} else if (e.type == SDL_MOUSEBUTTONDOWN) {
} else if (e.type == SDL_MOUSEWHEEL) {
wheel = e.wheel.y;
}
for (itSorgenti = sorgenti.begin(); itSorgenti != sorgenti.end();
itSorgenti++) {
itSorgenti->handleEnvent(e, x, y);
}
}
}
// Finestra impostazioni
// --------------------
// IMGUI
// --------------------
// ImGUI input handling
const int buttons = SDL_GetMouseState(&x, &y);
io.DeltaTime = 1.0f / 1000.0f;
io.MousePos = ImVec2(static_cast<float>(x), static_cast<float>(y));
io.MouseDown[0] = buttons & SDL_BUTTON(SDL_BUTTON_LEFT);
io.MouseDown[1] = buttons & SDL_BUTTON(SDL_BUTTON_RIGHT);
io.MouseWheel = static_cast<float>(wheel);
SDL_SetRenderDrawColor(gRenderer, COLORE(coloreSfondo));
SDL_RenderClear(gRenderer);
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
Settings::canvas.renderCanvas(x, y);
unsigned int sizeCariche = cariche.size();
// Punti del campo vettoriale, ogni vettore rappresenta la forza
// totale del/dei campi elettrico/i
for (itSorgenti = sorgenti.begin(); itSorgenti != sorgenti.end();
itSorgenti++) {
if (itSorgenti->selected) {
itSorgenti->setPosition(
vector2{static_cast<float>(x), static_cast<float>(y)});
}
if (drawCampoVettoriale) {
for (it = punti.begin(); it != punti.end(); it++) {
simulazioneCampo(itSorgenti, it);
}
}
spawnLinee(lineeDiForza, itSorgenti);
// lineeDiForza.push_back(CaricaLineaDiForza(it->getPosition().x + raggio,
// it->getPosition().y + raggio));
// Simulazione delle cariche di prova, non definitivo
// (da unire con l'altra funzione o creare classe base)
simulazioneCampo(itSorgenti, mouseVector, x, y);
if (sizeCariche > 0) {
for (itCariche = cariche.begin(); itCariche != cariche.end();
itCariche++) {
simulazioneCampo(itSorgenti, itCariche, cariche);
}
}
if (drawSorgenti)
itSorgenti->render(gRenderer);
}
// Simulazione movimento cariche di prova
float dt = stepTimer.getTicks() / 1000.f;
if (sizeCariche > 0) {
for (itCariche = cariche.begin(); itCariche != cariche.end();
itCariche++) {
itCariche->computeForces();
if (!pause)
itCariche->updatePosition(dt);
grafico.puntiDelGrafico.push_back(
Point(itCariche->getPosition().x, itCariche->getPosition().y));
}
}
stepTimer.start();
// RENDERING GRIGLIA
// if (drawGrid) {
// RenderGriglia(gRenderer, SCREEN_WIDTH, SCREEN_HEIGHT, scala);
//}
// Rendering cariche di prova
SDL_SetRenderDrawColor(gRenderer, COLORE(coloreCarica));
if (sizeCariche > 0 && drawGraficoCariche) {
for (itCariche = cariche.begin(); itCariche != cariche.end();
itCariche++) {
itCariche->render(gRenderer);
itCariche->emptyVectors();
}
}
// Rendering campo vettoriale
if (drawCampoVettoriale) {
for (it = punti.begin(); it != punti.end(); it++) {
it->computeVectors();
it->render(gRenderer);
it->emptyVectors();
}
}
for (auto vec : mouseVector) {
intensitaMouse.x += vec.x;
intensitaMouse.y += vec.y;
}
intensitaMouse.modulo = sqrt((intensitaMouse.x * intensitaMouse.x) +
(intensitaMouse.y * intensitaMouse.y));
intensitaMouse.x = 0;
intensitaMouse.y = 0;
mouseVector.clear();
// RENDERING GRAFICO
SDL_SetRenderDrawColor(gRenderer, COLORE(coloreGrCariche));
grafico.renderByPoints(gRenderer);
// LINEE DI CAMPO
if (drawLineeDiCampo) {
SDL_SetRenderDrawColor(gRenderer, COLORE(coloreLinee));
for (auto linea : lineeDiForza) {
linea.computeVectors(sorgenti);
linea.render(gRenderer);
linea.emptyVectors();
}
lineeDiForza.clear();
}
renderUi(pause, gWindow, darkMode, intensitaMouse, punti, cariche, sorgenti, grafico);
/* NOT YET SUPPORTED TODO: update when available
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
}
*/
SDL_RenderPresent(gRenderer);
}
ImGui_ImplSDLRenderer_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
SDL_Quit();
return 0;
}