Skip to content

Commit 918a66a

Browse files
committed
feat: minimal working interactive
1 parent 1a681a1 commit 918a66a

1 file changed

Lines changed: 60 additions & 32 deletions

File tree

tests/sdl/SDLTestApp.cpp

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@ class RobotTestApp {
3030
exit(1);
3131
}
3232

33-
// Create window
33+
// Create window - IMPORTANT: Use SDL_WINDOW_SHOWN flag to ensure the window is visible
3434
Uint32 windowFlags = SDL_WINDOW_SHOWN;
35-
if (headless) {
36-
windowFlags |= SDL_WINDOW_HIDDEN;
37-
}
3835

3936
window = SDL_CreateWindow(
4037
"Robot CPP Testing Framework",
@@ -48,8 +45,8 @@ class RobotTestApp {
4845
exit(1);
4946
}
5047

51-
// Create renderer
52-
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
48+
// Create renderer with VSYNC to prevent rendering issues
49+
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
5350

5451
if (!renderer) {
5552
std::cerr << "Could not create renderer: " << SDL_GetError() << std::endl;
@@ -60,6 +57,12 @@ class RobotTestApp {
6057
mouseTests = std::make_unique<MouseTests>(renderer, window);
6158
keyboardTests = std::make_unique<KeyboardTests>(renderer, window);
6259
screenTests = std::make_unique<ScreenTests>(renderer, window);
60+
61+
// Force the window to be on top
62+
SDL_RaiseWindow(window);
63+
64+
// Position window consistently
65+
SDL_SetWindowPosition(window, 50, 50);
6366
}
6467

6568
~RobotTestApp() {
@@ -83,18 +86,8 @@ class RobotTestApp {
8386

8487
std::cout << "===== Robot CPP Test Suite =====" << std::endl;
8588

86-
// Make the window visible for tests even in headless mode
87-
// This helps ensure the window is properly composited
88-
if (headless) {
89-
SDL_ShowWindow(window);
90-
}
91-
92-
// Make sure the window is front and center
93-
SDL_RaiseWindow(window);
94-
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
95-
96-
// Wait a bit for window to be fully shown and composited
97-
Robot::delay(1000);
89+
// Make sure the window is properly initialized and visible
90+
prepareForTests();
9891

9992
// Run mouse tests
10093
std::cout << "\n----- Mouse Tests -----" << std::endl;
@@ -127,11 +120,6 @@ class RobotTestApp {
127120
std::cout << "\n===== Test Results =====" << std::endl;
128121
std::cout << (allTestsPassed ? "✅ ALL TESTS PASSED" : "❌ SOME TESTS FAILED") << std::endl;
129122

130-
// Hide window again if in headless mode
131-
if (headless) {
132-
SDL_HideWindow(window);
133-
}
134-
135123
return allTestsPassed;
136124
}
137125

@@ -151,7 +139,7 @@ class RobotTestApp {
151139
}
152140

153141
void render() {
154-
// Clear screen
142+
// Clear screen with a dark gray background
155143
SDL_SetRenderDrawColor(renderer, 40, 40, 40, 255);
156144
SDL_RenderClear(renderer);
157145

@@ -165,10 +153,43 @@ class RobotTestApp {
165153
keyboardTests->draw();
166154
screenTests->draw();
167155

168-
// Present
156+
// Present render to the screen
169157
SDL_RenderPresent(renderer);
170158
}
171159

160+
void prepareForTests() {
161+
std::cout << "Preparing test environment..." << std::endl;
162+
163+
// Make sure window is visible
164+
SDL_ShowWindow(window);
165+
166+
// Ensure window is positioned correctly
167+
SDL_SetWindowPosition(window, 50, 50);
168+
169+
// Make sure the window is on top
170+
SDL_RaiseWindow(window);
171+
172+
// Render several frames to ensure the window is properly displayed
173+
for (int i = 0; i < 5; i++) {
174+
render();
175+
SDL_Delay(100);
176+
}
177+
178+
// Process any pending events
179+
SDL_Event event;
180+
while (SDL_PollEvent(&event)) {
181+
// Just drain the event queue
182+
}
183+
184+
// Additional delay to ensure window is ready
185+
SDL_Delay(500);
186+
187+
// Get and display window position for debugging
188+
int x, y;
189+
SDL_GetWindowPosition(window, &x, &y);
190+
std::cout << "Window position: (" << x << ", " << y << ")" << std::endl;
191+
}
192+
172193
int width, height;
173194
bool running;
174195
bool headless;
@@ -181,25 +202,32 @@ class RobotTestApp {
181202
};
182203

183204
int main(int argc, char* argv[]) {
184-
bool headless = false;
185205
bool runTests = false;
206+
int waitTime = 2000; // Default wait time in ms before tests
186207

187208
// Parse command line arguments
188209
for (int i = 1; i < argc; i++) {
189210
std::string arg = argv[i];
190-
if (arg == "--headless") {
191-
headless = true;
192-
}
193-
else if (arg == "--run-tests") {
211+
if (arg == "--run-tests") {
194212
runTests = true;
195213
}
214+
else if (arg == "--wait-time" && i + 1 < argc) {
215+
waitTime = std::stoi(argv[i + 1]);
216+
i++;
217+
}
196218
}
197219

198-
// Create test application
199-
RobotTestApp app(800, 600, headless);
220+
// Create test application (never headless to ensure window is visible)
221+
RobotTestApp app(800, 600, false);
200222

201223
// Either run tests or interactive mode
202224
if (runTests) {
225+
std::cout << "Initializing test window..." << std::endl;
226+
227+
// Wait before starting tests to ensure window is ready
228+
std::cout << "Waiting " << waitTime/1000.0 << " seconds before starting tests..." << std::endl;
229+
SDL_Delay(waitTime);
230+
203231
bool success = app.runTests();
204232
return success ? 0 : 1;
205233
} else {

0 commit comments

Comments
 (0)