-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
88 lines (74 loc) · 2.85 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
#include <SDL.h>
#include <SDL_ttf.h>
#include "simulation.h"
#include "gui.h"
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
TTF_Init();
SDL_Window* simWindow = SDL_CreateWindow("Particle Simulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1200, 800, 0);
SDL_Renderer* simRenderer = SDL_CreateRenderer(simWindow, -1, SDL_RENDERER_ACCELERATED);
SDL_Window* guiWindow = SDL_CreateWindow("Controls", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 400, 600, 0);
SDL_Renderer* guiRenderer = SDL_CreateRenderer(guiWindow, -1, SDL_RENDERER_ACCELERATED);
TTF_Font* font = TTF_OpenFont("/Users/aaronmclean/Library/Fonts/3270-Regular.ttf", 128);
if (!font) {
printf("Failed to load font: %s\n", TTF_GetError());
SDL_DestroyRenderer(simRenderer);
SDL_DestroyWindow(simWindow);
SDL_DestroyRenderer(guiRenderer);
SDL_DestroyWindow(guiWindow);
TTF_Quit();
SDL_Quit();
return -1;
}
Simulation simulation;
GUI gui(guiRenderer, font);
bool running = true;
SDL_Event event;
Uint32 lastSpawnTime = 0;
const Uint32 spawnInterval = 50;
bool mouseDown = false;
int mouseX = 0, mouseY = 0;
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = false;
} else if (event.type == SDL_MOUSEBUTTONDOWN) {
if (event.button.button == SDL_BUTTON_LEFT) {
mouseDown = true;
mouseX = event.button.x;
mouseY = event.button.y;
}
} else if (event.type == SDL_MOUSEBUTTONUP) {
if (event.button.button == SDL_BUTTON_LEFT) {
mouseDown = false;
}
} else if (event.type == SDL_MOUSEMOTION && mouseDown) {
mouseX = event.motion.x;
mouseY = event.motion.y;
}
gui.handleEvent(event, simulation);
}
Uint32 currentTime = SDL_GetTicks();
if (mouseDown && currentTime - lastSpawnTime >= spawnInterval) {
simulation.spawnParticlesAtMouse(mouseX, mouseY, 1);
lastSpawnTime = currentTime;
}
simulation.update(0.10f);
SDL_SetRenderDrawColor(simRenderer, 0, 0, 0, 255);
SDL_RenderClear(simRenderer);
simulation.render(simRenderer);
SDL_RenderPresent(simRenderer);
SDL_SetRenderDrawColor(guiRenderer, 150, 150, 150, 255);
SDL_RenderClear(guiRenderer);
gui.render(simulation);
SDL_RenderPresent(guiRenderer);
}
SDL_DestroyRenderer(simRenderer);
SDL_DestroyWindow(simWindow);
SDL_DestroyRenderer(guiRenderer);
SDL_DestroyWindow(guiWindow);
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
return 0;
}