-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
176 lines (136 loc) · 4.46 KB
/
main.cpp
File metadata and controls
176 lines (136 loc) · 4.46 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "World.h"
#include <box2d/box2d.h>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
static const float DESIGN_WIDTH = 800.f;
static const float DESIGN_HEIGHT = 600.f;
static sf::View getLetterboxView(float windowWidth, float windowHeight) {
sf::View view(sf::FloatRect(0, 0, DESIGN_WIDTH, DESIGN_HEIGHT));
float windowRatio = windowWidth / windowHeight;
float viewRatio = DESIGN_WIDTH / DESIGN_HEIGHT;
float sizeX = 1.f, sizeY = 1.f;
float posX = 0.f, posY = 0.f;
if (windowRatio >= viewRatio) {
sizeX = viewRatio / windowRatio;
posX = (1.f - sizeX) / 2.f;
} else {
sizeY = windowRatio / viewRatio;
posY = (1.f - sizeY) / 2.f;
}
view.setViewport(sf::FloatRect(posX, posY, sizeX, sizeY));
return view;
}
static int runGame(const string& screenshotPath, int screenshotAfterFrames) {
sf::RenderWindow app(sf::VideoMode(800, 600, 32), "Monkey Platformer");
World world;
app.setVerticalSyncEnabled(true);
app.setFramerateLimit(50);
sf::View view = getLetterboxView(800, 600);
app.setView(view);
int frameCount = 0;
while (app.isOpen()) {
sf::Event event;
while (app.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
app.close();
}
if (event.type == sf::Event::KeyPressed
&& event.key.code == sf::Keyboard::Escape) {
app.close();
}
if (event.type == sf::Event::Resized) {
view = getLetterboxView(event.size.width, event.size.height);
app.setView(view);
}
}
world.Update();
app.clear(sf::Color::Black);
app.setView(view);
sf::RectangleShape bg(sf::Vector2f(DESIGN_WIDTH, DESIGN_HEIGHT));
bg.setFillColor(sf::Color(135, 206, 235));
app.draw(bg);
world.Draw(app);
app.display();
frameCount++;
if (!screenshotPath.empty() && frameCount >= screenshotAfterFrames) {
sf::Texture texture;
texture.create(app.getSize().x, app.getSize().y);
texture.update(app);
if (texture.copyToImage().saveToFile(screenshotPath)) {
cout << "Screenshot saved to: " << screenshotPath << endl;
} else {
cerr << "Failed to save screenshot to: " << screenshotPath << endl;
return 1;
}
app.close();
}
}
return 0;
}
static int runPhysicsTest() {
World world;
Point startPos = world.GetPlayerPosition();
cout << "Initial player position: (" << startPos.myX << ", " << startPos.myY << ")" << endl;
// The level is roughly 16 rows of 0.20 blocks = 3.2 world units tall
// Player should never fall below the level bounds
const float MAX_Y = 10.0f;
const int SIM_FRAMES = 500; // 10 seconds at 50fps
float minY = startPos.myY;
float maxY = startPos.myY;
bool positionChanged = false;
for (int frame = 0; frame < SIM_FRAMES; frame++) {
world.Update();
Point pos = world.GetPlayerPosition();
if (pos.myY < minY) minY = pos.myY;
if (pos.myY > maxY) maxY = pos.myY;
if (pos.myX != startPos.myX || pos.myY != startPos.myY) {
positionChanged = true;
}
// Check: player hasn't fallen through the world
if (pos.myY > MAX_Y) {
cerr << "FAIL: Player fell through the ground at frame " << frame
<< " (Y=" << pos.myY << " > " << MAX_Y << ")" << endl;
return 1;
}
// Check: position is not NaN (physics explosion)
if (pos.myX != pos.myX || pos.myY != pos.myY) {
cerr << "FAIL: Player position is NaN at frame " << frame << endl;
return 1;
}
}
cout << "After " << SIM_FRAMES << " frames:" << endl;
Point finalPos = world.GetPlayerPosition();
cout << " Final position: (" << finalPos.myX << ", " << finalPos.myY << ")" << endl;
cout << " Y range: [" << minY << ", " << maxY << "]" << endl;
// Check: physics simulation actually did something (player should fall due to gravity)
if (!positionChanged) {
cerr << "FAIL: Player position never changed (physics stuck)" << endl;
return 1;
}
cout << "PASS: Player didn't fall through the ground" << endl;
cout << "PASS: Player position changed (physics working)" << endl;
cout << "PASS: No NaN positions (physics stable)" << endl;
return 0;
}
int main(int argc, char** argv) {
string screenshotPath;
int screenshotAfterFrames = 0;
bool testPhysics = false;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--screenshot") == 0 && i + 2 < argc) {
screenshotPath = argv[i + 1];
screenshotAfterFrames = atoi(argv[i + 2]);
i += 2;
} else if (strcmp(argv[i], "--test-physics") == 0) {
testPhysics = true;
}
}
if (testPhysics) {
return runPhysicsTest();
}
return runGame(screenshotPath, screenshotAfterFrames);
}