-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
158 lines (134 loc) · 4.43 KB
/
main.cpp
File metadata and controls
158 lines (134 loc) · 4.43 KB
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
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <iostream>
#include <cstdlib>
#include <sstream>
#include "simulation.h"
int main(int argc, char* argv[]) {
int num_particles = 8000; // default
if (argc > 1) {
char* endptr;
long n = std::strtol(argv[1], &endptr, 10);
if (endptr == argv[1] || n <= 0) {
std::cerr << "Usage: " << argv[0] << " <N>\n";
std::cerr << " N: number of particles (1-500)\n";
return 1;
}
num_particles = static_cast<int>(n);
}
const int WIDTH = 1000;
const int HEIGHT = 1000;
const double DT = 0.016; // ~60 FPS
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
std::cerr << "SDL_Init failed: " << SDL_GetError() << "\n";
return 1;
}
if (TTF_Init() != 0) {
std::cerr << "TTF_Init failed: " << TTF_GetError() << "\n";
SDL_Quit();
return 1;
}
SDL_Window* window = SDL_CreateWindow(
"Pixel Gravity Simulation",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
WIDTH,
HEIGHT,
SDL_WINDOW_SHOWN
);
if (!window) {
std::cerr << "SDL_CreateWindow failed: " << SDL_GetError() << "\n";
SDL_Quit();
return 1;
}
SDL_Renderer* renderer = SDL_CreateRenderer(
window,
-1,
SDL_RENDERER_ACCELERATED
);
if (!renderer) {
std::cerr << "SDL_CreateRenderer failed: " << SDL_GetError() << "\n";
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
Simulation sim(WIDTH, HEIGHT, num_particles, 0.5);
// Load font for FPS display
TTF_Font* font = TTF_OpenFont("/System/Library/Fonts/Helvetica.ttc", 16);
if (!font) {
std::cerr << "Failed to load font: " << TTF_GetError() << "\n";
TTF_Quit();
SDL_Quit();
return 1;
}
bool running = true;
Uint32 frame_start;
Uint32 frame_time;
Uint32 last_fps_update = 0;
int frame_count = 0;
double fps = 0.0;
int frame_counter = 0;
while (running) {
frame_start = SDL_GetTicks();
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = false;
} else if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_ESCAPE) {
running = false;
}
}
}
sim.applyGravity();
sim.verletStep(DT);
sim.resolvePixelCollisions();
// Log quadtree node count every 60 frames
frame_counter++;
if (frame_counter % 60 == 0) {
int node_count = Simulation::getQuadtreeNodeCount();
double maxSpeed = sim.getMaxSpeed();
std::cerr << "Frame " << frame_counter << ": Quadtree nodes = " << node_count
<< ", max speed = " << maxSpeed << std::endl;
sim.logMemoryStats();
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
sim.render(renderer);
// Calculate FPS
Uint32 current_time = SDL_GetTicks();
frame_count++;
if (current_time - last_fps_update >= 500) { // Update every 500ms
fps = frame_count * 1000.0 / (current_time - last_fps_update);
frame_count = 0;
last_fps_update = current_time;
}
// Render FPS text in top right corner
std::ostringstream fps_text;
fps_text << "FPS: " << static_cast<int>(fps);
SDL_Color text_color = {255, 255, 255, 255};
SDL_Surface* surface = TTF_RenderText_Solid(font, fps_text.str().c_str(), text_color);
if (surface) {
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
if (texture) {
int text_width = surface->w;
int text_height = surface->h;
SDL_Rect dest_rect = {WIDTH - text_width - 10, 10, text_width, text_height};
SDL_RenderCopy(renderer, texture, NULL, &dest_rect);
SDL_DestroyTexture(texture);
}
SDL_FreeSurface(surface);
}
SDL_RenderPresent(renderer);
frame_time = SDL_GetTicks() - frame_start;
if (frame_time < 16) {
SDL_Delay(16 - frame_time);
}
}
TTF_CloseFont(font);
TTF_Quit();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}