-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
288 lines (245 loc) · 9.32 KB
/
main.c
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
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "particle.h"
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define NUM_PARTICLES 100
#define ROTATION_SPEED 0.5f // Radians per second
#define SETTLED_VELOCITY_THRESHOLD 1.0f
#define SETTLED_CHECK_TIME 1.0f // Time to wait before checking if particles settled
#define BOX_SIZE_METERS 1000.0f // Box is 1000 meters on each side
#define ROTATION_ANGLE M_PI // 180 degrees in radians
typedef struct {
Uint32 startTime;
Uint32 currentTime;
} SimulationTime;
void init_particles(Particle* particles) {
for (int i = 0; i < NUM_PARTICLES; i++) {
particles[i].x = rand() % WINDOW_WIDTH;
particles[i].y = rand() % (WINDOW_HEIGHT / 2); // Start in top half
particles[i].vx = (rand() % 200 - 100) / 100.0f; // Random velocity
particles[i].vy = 0;
particles[i].radius = 5;
}
}
void draw_box_labels(SDL_Renderer* renderer, TTF_Font* font, float centerX, float centerY, float boxSize, float angle) {
char label[32];
snprintf(label, sizeof(label), "%.0fm", BOX_SIZE_METERS);
SDL_Color textColor = {255, 255, 255, 255}; // White text
SDL_Surface* textSurface = TTF_RenderText_Solid(font, label, textColor);
if (!textSurface) {
return;
}
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
if (!textTexture) {
SDL_FreeSurface(textSurface);
return;
}
// Calculate positions for labels on each side
//float s = sin(angle);
//`float c = cos(angle);
float halfSize = boxSize / 2.0f;
SDL_Rect textRect = {0, 0, textSurface->w, textSurface->h};
// Draw labels slightly outside each edge of the box
// Bottom
textRect.x = centerX - textSurface->w/2;
textRect.y = centerY + halfSize + 10;
SDL_RenderCopy(renderer, textTexture, NULL, &textRect);
// Top
textRect.y = centerY - halfSize - textSurface->h - 10;
SDL_RenderCopy(renderer, textTexture, NULL, &textRect);
// Left
textRect.x = centerX - halfSize - textSurface->w - 10;
textRect.y = centerY - textSurface->h/2;
SDL_RenderCopy(renderer, textTexture, NULL, &textRect);
// Right
textRect.x = centerX + halfSize + 10;
SDL_RenderCopy(renderer, textTexture, NULL, &textRect);
SDL_DestroyTexture(textTexture);
SDL_FreeSurface(textSurface);
}
void draw_time_counter(SDL_Renderer* renderer, TTF_Font* font, SimulationTime time) {
Uint32 elapsed = time.currentTime - time.startTime;
Uint32 jiffies = (elapsed % 1000) / 16; // 1/60th of a second
Uint32 seconds = (elapsed / 1000) % 60;
Uint32 minutes = (elapsed / 60000) % 60;
Uint32 hours = elapsed / 3600000;
char timeStr[32];
snprintf(timeStr, sizeof(timeStr), "%02u:%02u:%02u:%02u",
hours, minutes, seconds, jiffies);
SDL_Color textColor = {255, 255, 255, 255}; // White text
SDL_Surface* textSurface = TTF_RenderText_Solid(font, timeStr, textColor);
if (!textSurface) {
return;
}
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
if (!textTexture) {
SDL_FreeSurface(textSurface);
return;
}
// Position in top-left corner with a small margin
SDL_Rect textRect = {10, 10, textSurface->w, textSurface->h};
SDL_RenderCopy(renderer, textTexture, NULL, &textRect);
SDL_DestroyTexture(textTexture);
SDL_FreeSurface(textSurface);
}
int main(int argc, char* argv[]) {
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL initialization failed: %s\n", SDL_GetError());
return 1;
}
if (TTF_Init() < 0) {
printf("SDL_ttf initialization failed: %s\n", TTF_GetError());
SDL_Quit();
return 1;
}
// Load font
TTF_Font* font = TTF_OpenFont("Helvetica.ttf", 16);
if (!font) {
printf("Failed to load font: %s\n", TTF_GetError());
TTF_Quit();
SDL_Quit();
return 1;
}
window = SDL_CreateWindow("Physics Box Simulation",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
WINDOW_WIDTH, WINDOW_HEIGHT,
SDL_WINDOW_SHOWN);
if (!window) {
printf("Window creation failed: %s\n", SDL_GetError());
return 1;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
printf("Renderer creation failed: %s\n", SDL_GetError());
return 1;
}
// Initialize particles
srand(time(NULL));
Particle particles[NUM_PARTICLES];
init_particles(particles);
// Initialize box state
BoxState box = {
.angle = 0,
.targetAngle = 0,
.isRotating = false,
.timeSinceLastMove = 0
};
// Initialize simulation time
SimulationTime simTime = {
.startTime = SDL_GetTicks(),
.currentTime = SDL_GetTicks()
};
// Main game loop
bool quit = false;
SDL_Event event;
Uint32 lastTime = SDL_GetTicks();
while (!quit) {
// Handle events
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
else if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_r) {
// Reset simulation
init_particles(particles);
box.angle = 0;
box.targetAngle = 0;
box.isRotating = false;
box.timeSinceLastMove = 0;
simTime.startTime = SDL_GetTicks();
}
}
}
// Update simulation time
simTime.currentTime = SDL_GetTicks();
// Calculate delta time
Uint32 currentTime = SDL_GetTicks();
float deltaTime = (currentTime - lastTime) / 1000.0f;
lastTime = currentTime;
// Check if particles have settled
bool allSettled = true;
for (int i = 0; i < NUM_PARTICLES; i++) {
float vel = sqrt(particles[i].vx * particles[i].vx +
particles[i].vy * particles[i].vy);
if (vel > SETTLED_VELOCITY_THRESHOLD) {
allSettled = false;
box.timeSinceLastMove = 0;
break;
}
}
if (allSettled) {
box.timeSinceLastMove += deltaTime;
if (box.timeSinceLastMove >= SETTLED_CHECK_TIME && !box.isRotating) {
box.isRotating = true;
//box.targetAngle = box.angle + M_PI/2; // 90 degrees
box.targetAngle = box.angle + ROTATION_ANGLE;
}
}
if (box.isRotating) {
box.angle += ROTATION_SPEED * deltaTime;
if (box.angle >= box.targetAngle) {
box.angle = box.targetAngle;
box.isRotating = false;
box.timeSinceLastMove = 0;
}
}
// Update particles
update_particles(particles, NUM_PARTICLES, deltaTime, WINDOW_WIDTH, WINDOW_HEIGHT, &box);
// Render
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// Draw box outline
SDL_SetRenderDrawColor(renderer, 128, 128, 128, 255);
float centerX = WINDOW_WIDTH / 2.0f;
float centerY = WINDOW_HEIGHT / 2.0f;
float minDimension = (WINDOW_WIDTH < WINDOW_HEIGHT) ? WINDOW_WIDTH : WINDOW_HEIGHT;
float boxSize = (minDimension - 40) / sqrtf(2); // 20px border on each side
// Calculate rotated box corners
float c = cos(box.angle);
float s = sin(box.angle);
SDL_Point points[5] = {
{centerX + (-boxSize/2 * c - boxSize/2 * s),
centerY + (-boxSize/2 * s + boxSize/2 * c)},
{centerX + (boxSize/2 * c - boxSize/2 * s),
centerY + (boxSize/2 * s + boxSize/2 * c)},
{centerX + (boxSize/2 * c + boxSize/2 * s),
centerY + (boxSize/2 * s - boxSize/2 * c)},
{centerX + (-boxSize/2 * c + boxSize/2 * s),
centerY + (-boxSize/2 * s - boxSize/2 * c)},
{centerX + (-boxSize/2 * c - boxSize/2 * s),
centerY + (-boxSize/2 * s + boxSize/2 * c)}
};
SDL_RenderDrawLines(renderer, points, 5);
// Draw box labels
draw_box_labels(renderer, font, centerX, centerY, boxSize, box.angle);
// Draw particles
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
for (int i = 0; i < NUM_PARTICLES; i++) {
SDL_Rect rect = {
(int)(particles[i].x - particles[i].radius),
(int)(particles[i].y - particles[i].radius),
(int)(particles[i].radius * 2),
(int)(particles[i].radius * 2)
};
SDL_RenderFillRect(renderer, &rect);
}
// Draw time counter
draw_time_counter(renderer, font, simTime);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
return 0;
}