-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
689 lines (577 loc) · 24.8 KB
/
main.cpp
File metadata and controls
689 lines (577 loc) · 24.8 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
#include <windows.h>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <thread>
#include <mutex>
#include <random>
#include <fstream>
#include <sstream>
#include <chrono>
#include <algorithm>
#include <cctype>
#include <conio.h>
// ============================================================================
// DEFAULT CONFIGURATION - Used when creating new config.json
// ============================================================================
// Capture settings
int CAPTURE_SIZE = 186;
int CAPTURE_POS_X = 1187;
int CAPTURE_POS_Y = 607;
int FPS = 90;
// Ring detection settings
double RING_OUTER_RADIUS = 89.0;
double RING_INNER_RADIUS = 85.0;
int RING_CENTER_OFFSET_X = -1;
int RING_CENTER_OFFSET_Y = 0;
// Safety check rectangles
int SAFETY_RECT1_X = 63;
int SAFETY_RECT1_Y = 79;
int SAFETY_RECT1_WIDTH = 60;
int SAFETY_RECT1_HEIGHT = 6;
int SAFETY_RECT2_X = 63;
int SAFETY_RECT2_Y = 103;
int SAFETY_RECT2_WIDTH = 60;
int SAFETY_RECT2_HEIGHT = 4;
// Detection thresholds
int MIN_WHITE_PIXELS = 30;
int MIN_RED_PIXELS = 2;
int TIMER_DURATION_MS = 1200;
int RESET_DELAY_MS = 200;
// Color thresholds (0-255)
int WHITE_THRESHOLD = 0xFE;
int RED_THRESHOLD = 50;
int OTHER_CHANNEL_MAX = 150;
int RED_DOMINANCE = 20;
// Key press settings
int SPACE_PRESS_MIN_MS = 50;
int SPACE_PRESS_MAX_MS = 90;
// Save settings
bool SAVE_ENABLED = true;
// ============================================================================
// GLOBALS
// ============================================================================
std::mutex saveMutex;
// ============================================================================
// JSON CONFIGURATION
// ============================================================================
void SaveConfigToJson(const char* filename) {
std::ofstream file(filename);
if (!file) {
std::cerr << "Failed to create config file\n";
return;
}
file << "{\n";
file << " \"capture_size\": " << CAPTURE_SIZE << ",\n";
file << " \"capture_pos_x\": " << CAPTURE_POS_X << ",\n";
file << " \"capture_pos_y\": " << CAPTURE_POS_Y << ",\n";
file << " \"fps\": " << FPS << ",\n";
file << " \"ring_outer_radius\": " << RING_OUTER_RADIUS << ",\n";
file << " \"ring_inner_radius\": " << RING_INNER_RADIUS << ",\n";
file << " \"ring_center_offset_x\": " << RING_CENTER_OFFSET_X << ",\n";
file << " \"ring_center_offset_y\": " << RING_CENTER_OFFSET_Y << ",\n";
file << " \"safety_rect1_x\": " << SAFETY_RECT1_X << ",\n";
file << " \"safety_rect1_y\": " << SAFETY_RECT1_Y << ",\n";
file << " \"safety_rect1_width\": " << SAFETY_RECT1_WIDTH << ",\n";
file << " \"safety_rect1_height\": " << SAFETY_RECT1_HEIGHT << ",\n";
file << " \"safety_rect2_x\": " << SAFETY_RECT2_X << ",\n";
file << " \"safety_rect2_y\": " << SAFETY_RECT2_Y << ",\n";
file << " \"safety_rect2_width\": " << SAFETY_RECT2_WIDTH << ",\n";
file << " \"safety_rect2_height\": " << SAFETY_RECT2_HEIGHT << ",\n";
file << " \"min_white_pixels\": " << MIN_WHITE_PIXELS << ",\n";
file << " \"min_red_pixels\": " << MIN_RED_PIXELS << ",\n";
file << " \"timer_duration_ms\": " << TIMER_DURATION_MS << ",\n";
file << " \"reset_delay_ms\": " << RESET_DELAY_MS << ",\n";
file << " \"white_threshold\": " << WHITE_THRESHOLD << ",\n";
file << " \"red_threshold\": " << RED_THRESHOLD << ",\n";
file << " \"other_channel_max\": " << OTHER_CHANNEL_MAX << ",\n";
file << " \"red_dominance\": " << RED_DOMINANCE << ",\n";
file << " \"space_press_min_ms\": " << SPACE_PRESS_MIN_MS << ",\n";
file << " \"space_press_max_ms\": " << SPACE_PRESS_MAX_MS << ",\n";
file << " \"save_enabled\": " << (SAVE_ENABLED ? "true" : "false") << "\n";
file << "}\n";
file.close();
std::cout << "Configuration saved to " << filename << "\n";
}
std::string trim(const std::string& str) {
size_t first = str.find_first_not_of(" \t\n\r\"");
if (first == std::string::npos) return "";
size_t last = str.find_last_not_of(" \t\n\r\",");
return str.substr(first, (last - first + 1));
}
bool LoadConfigFromJson(const char* filename) {
std::ifstream file(filename);
if (!file) {
return false;
}
std::string line;
while (std::getline(file, line)) {
size_t colon = line.find(':');
if (colon == std::string::npos) continue;
std::string key = trim(line.substr(0, colon));
std::string value = trim(line.substr(colon + 1));
if (key == "capture_size") CAPTURE_SIZE = std::stoi(value);
else if (key == "capture_pos_x") CAPTURE_POS_X = std::stoi(value);
else if (key == "capture_pos_y") CAPTURE_POS_Y = std::stoi(value);
else if (key == "fps") FPS = std::stoi(value);
else if (key == "ring_outer_radius") RING_OUTER_RADIUS = std::stod(value);
else if (key == "ring_inner_radius") RING_INNER_RADIUS = std::stod(value);
else if (key == "ring_center_offset_x") RING_CENTER_OFFSET_X = std::stoi(value);
else if (key == "ring_center_offset_y") RING_CENTER_OFFSET_Y = std::stoi(value);
else if (key == "safety_rect1_x") SAFETY_RECT1_X = std::stoi(value);
else if (key == "safety_rect1_y") SAFETY_RECT1_Y = std::stoi(value);
else if (key == "safety_rect1_width") SAFETY_RECT1_WIDTH = std::stoi(value);
else if (key == "safety_rect1_height") SAFETY_RECT1_HEIGHT = std::stoi(value);
else if (key == "safety_rect2_x") SAFETY_RECT2_X = std::stoi(value);
else if (key == "safety_rect2_y") SAFETY_RECT2_Y = std::stoi(value);
else if (key == "safety_rect2_width") SAFETY_RECT2_WIDTH = std::stoi(value);
else if (key == "safety_rect2_height") SAFETY_RECT2_HEIGHT = std::stoi(value);
else if (key == "min_white_pixels") MIN_WHITE_PIXELS = std::stoi(value);
else if (key == "min_red_pixels") MIN_RED_PIXELS = std::stoi(value);
else if (key == "timer_duration_ms") TIMER_DURATION_MS = std::stoi(value);
else if (key == "reset_delay_ms") RESET_DELAY_MS = std::stoi(value);
else if (key == "white_threshold") WHITE_THRESHOLD = std::stoi(value);
else if (key == "red_threshold") RED_THRESHOLD = std::stoi(value);
else if (key == "other_channel_max") OTHER_CHANNEL_MAX = std::stoi(value);
else if (key == "red_dominance") RED_DOMINANCE = std::stoi(value);
else if (key == "space_press_min_ms") SPACE_PRESS_MIN_MS = std::stoi(value);
else if (key == "space_press_max_ms") SPACE_PRESS_MAX_MS = std::stoi(value);
else if (key == "save_enabled") SAVE_ENABLED = (value == "true");
}
file.close();
return true;
}
// ============================================================================
// STRUCTURES
// ============================================================================
struct PixelPos {
int x, y;
};
// ============================================================================
// UTILITY FUNCTIONS
// ============================================================================
void OpenIndexHtml() {
// Open index.html with default browser
ShellExecute(NULL, "open", "index.html", NULL, NULL, SW_SHOWNORMAL);
}
void PressSpaceKey() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(SPACE_PRESS_MIN_MS, SPACE_PRESS_MAX_MS);
int pressDuration = dis(gen);
INPUT input = {0};
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_SPACE;
input.ki.dwFlags = 0;
SendInput(1, &input, sizeof(INPUT));
Sleep(pressDuration);
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
}
void SaveBitmapWithHighlights(const char* filename, BYTE* originalBits, int width, int height,
const std::vector<PixelPos>& whitePixels,
const std::vector<PixelPos>& redPixels) {
std::lock_guard<std::mutex> lock(saveMutex);
int rowSize = ((width * 3 + 3) / 4) * 4;
DWORD bmpSize = rowSize * height;
BYTE* bits = new BYTE[bmpSize];
memcpy(bits, originalBits, bmpSize);
// Mark safety rectangles in BLUE
for (int y = SAFETY_RECT1_Y; y < SAFETY_RECT1_Y + SAFETY_RECT1_HEIGHT; y++) {
for (int x = SAFETY_RECT1_X; x < SAFETY_RECT1_X + SAFETY_RECT1_WIDTH; x++) {
if (x >= 0 && x < width && y >= 0 && y < height) {
int index = y * rowSize + x * 3;
bits[index + 0] = 0xFF; bits[index + 1] = 0x00; bits[index + 2] = 0x00;
}
}
}
for (int y = SAFETY_RECT2_Y; y < SAFETY_RECT2_Y + SAFETY_RECT2_HEIGHT; y++) {
for (int x = SAFETY_RECT2_X; x < SAFETY_RECT2_X + SAFETY_RECT2_WIDTH; x++) {
if (x >= 0 && x < width && y >= 0 && y < height) {
int index = y * rowSize + x * 3;
bits[index + 0] = 0xFF; bits[index + 1] = 0x00; bits[index + 2] = 0x00;
}
}
}
// Mark white pixels in PINK
for (const auto& pos : whitePixels) {
if (pos.x >= 0 && pos.x < width && pos.y >= 0 && pos.y < height) {
int index = pos.y * rowSize + pos.x * 3;
bits[index + 0] = 0xFF; bits[index + 1] = 0x00; bits[index + 2] = 0xFF;
}
}
// Mark red pixels in YELLOW
for (const auto& pos : redPixels) {
if (pos.x >= 0 && pos.x < width && pos.y >= 0 && pos.y < height) {
int index = pos.y * rowSize + pos.x * 3;
bits[index + 0] = 0x00; bits[index + 1] = 0xFF; bits[index + 2] = 0xFF;
}
}
BITMAPINFOHEADER bi = {0};
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = width;
bi.biHeight = height;
bi.biPlanes = 1;
bi.biBitCount = 24;
bi.biCompression = BI_RGB;
HANDLE hFile = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
BITMAPFILEHEADER bfh = {0};
bfh.bfType = 0x4D42;
bfh.bfSize = sizeof(bfh) + sizeof(bi) + bmpSize;
bfh.bfOffBits = sizeof(bfh) + sizeof(bi);
DWORD written;
WriteFile(hFile, &bfh, sizeof(bfh), &written, NULL);
WriteFile(hFile, &bi, sizeof(bi), &written, NULL);
WriteFile(hFile, bits, bmpSize, &written, NULL);
CloseHandle(hFile);
}
delete[] bits;
}
std::vector<std::vector<PixelPos>> FindConnectedGroups(
const std::vector<PixelPos>& pixels, int width, int height, int minSize) {
std::vector<std::vector<PixelPos>> groups;
std::vector<bool> visited(pixels.size(), false);
std::vector<std::vector<bool>> pixelMap(height, std::vector<bool>(width, false));
for (const auto& p : pixels) {
if (p.x >= 0 && p.x < width && p.y >= 0 && p.y < height) {
pixelMap[p.y][p.x] = true;
}
}
for (size_t i = 0; i < pixels.size(); i++) {
if (visited[i]) continue;
std::vector<PixelPos> group;
std::vector<size_t> toVisit;
toVisit.push_back(i);
while (!toVisit.empty()) {
size_t idx = toVisit.back();
toVisit.pop_back();
if (visited[idx]) continue;
visited[idx] = true;
PixelPos current = pixels[idx];
group.push_back(current);
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
for (int d = 0; d < 4; d++) {
int nx = current.x + dx[d];
int ny = current.y + dy[d];
if (nx >= 0 && nx < width && ny >= 0 && ny < height && pixelMap[ny][nx]) {
for (size_t j = 0; j < pixels.size(); j++) {
if (!visited[j] && pixels[j].x == nx && pixels[j].y == ny) {
toVisit.push_back(j);
break;
}
}
}
}
}
if (group.size() >= minSize) {
groups.push_back(group);
}
}
return groups;
}
// ============================================================================
// PIXEL CHECKING FUNCTIONS
// ============================================================================
bool IsInRing(int x, int y, int centerX, int centerY, double innerRadius, double outerRadius) {
double dx = x - centerX;
double dy = y - centerY;
double distSq = dx * dx + dy * dy;
double innerSq = innerRadius * innerRadius;
double outerSq = outerRadius * outerRadius;
return distSq > innerSq && distSq < outerSq;
}
bool IsWhiteish(BYTE r, BYTE g, BYTE b) {
return r >= WHITE_THRESHOLD && g >= WHITE_THRESHOLD && b >= WHITE_THRESHOLD;
}
bool IsBlack(BYTE r, BYTE g, BYTE b) {
return r == 0 && g == 0 && b == 0;
}
bool IsReddish(BYTE r, BYTE g, BYTE b) {
return r >= RED_THRESHOLD &&
g < OTHER_CHANNEL_MAX &&
b < OTHER_CHANNEL_MAX &&
r >= (g + RED_DOMINANCE) &&
r >= (b + RED_DOMINANCE);
}
bool IsRectangleBlack(BYTE* buf, int bufWidth, int x, int y, int width, int height) {
int rowSize = ((bufWidth * 3 + 3) / 4) * 4;
for (int py = y; py < y + height; py++) {
for (int px = x; px < x + width; px++) {
if (px >= 0 && px < bufWidth && py >= 0) {
int index = py * rowSize + px * 3;
BYTE b = buf[index + 0];
BYTE g = buf[index + 1];
BYTE r = buf[index + 2];
if (!IsBlack(r, g, b)) {
return false;
}
}
}
}
return true;
}
// ============================================================================
// MAIN CAPTURE AND PROCESSING
// ============================================================================
bool CaptureAndProcess(int size, int posX, int posY,
std::vector<PixelPos>& whitePixels,
std::vector<PixelPos>& redPixels,
bool& firstCondition, bool& secondCondition,
LARGE_INTEGER& timerStart, LARGE_INTEGER freq) {
HDC hScreen = GetDC(NULL);
HDC hMem = CreateCompatibleDC(hScreen);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, size, size);
HBITMAP old = (HBITMAP)SelectObject(hMem, hBitmap);
bool ok = BitBlt(hMem, 0, 0, size, size, hScreen, posX, posY, SRCCOPY);
if (!ok) {
SelectObject(hMem, old);
DeleteObject(hBitmap);
DeleteDC(hMem);
ReleaseDC(NULL, hScreen);
return false;
}
BITMAPINFOHEADER bi = {0};
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = size;
bi.biHeight = size;
bi.biPlanes = 1;
bi.biBitCount = 24;
bi.biCompression = BI_RGB;
DWORD sizeBytes = ((size * 3 + 3) & ~3) * size;
BYTE* buf = new BYTE[sizeBytes];
GetDIBits(hMem, hBitmap, 0, size, buf, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
int rowSize = ((size * 3 + 3) / 4) * 4;
int centerX = size / 2 + RING_CENTER_OFFSET_X;
int centerY = size / 2 + RING_CENTER_OFFSET_Y;
if (!firstCondition) {
whitePixels.clear();
redPixels.clear();
bool rect1Black = IsRectangleBlack(buf, size, SAFETY_RECT1_X, SAFETY_RECT1_Y,
SAFETY_RECT1_WIDTH, SAFETY_RECT1_HEIGHT);
bool rect2Black = IsRectangleBlack(buf, size, SAFETY_RECT2_X, SAFETY_RECT2_Y,
SAFETY_RECT2_WIDTH, SAFETY_RECT2_HEIGHT);
if (!rect1Black || !rect2Black) {
delete[] buf;
SelectObject(hMem, old);
DeleteObject(hBitmap);
DeleteDC(hMem);
ReleaseDC(NULL, hScreen);
return false;
}
std::vector<PixelPos> candidatePixels;
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
if (IsInRing(x, y, centerX, centerY, RING_INNER_RADIUS, RING_OUTER_RADIUS)) {
BYTE b = buf[y * rowSize + x * 3 + 0];
BYTE g = buf[y * rowSize + x * 3 + 1];
BYTE r = buf[y * rowSize + x * 3 + 2];
if (IsWhiteish(r, g, b)) {
PixelPos pos;
pos.x = x;
pos.y = y;
candidatePixels.push_back(pos);
}
}
}
}
std::vector<std::vector<PixelPos>> groups =
FindConnectedGroups(candidatePixels, size, size, MIN_WHITE_PIXELS);
for (const auto& group : groups) {
for (const auto& pixel : group) {
whitePixels.push_back(pixel);
}
}
if (!whitePixels.empty()) {
firstCondition = true;
QueryPerformanceCounter(&timerStart);
}
}
else {
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
double elapsed = (now.QuadPart - timerStart.QuadPart) * 1000.0 / freq.QuadPart;
if (elapsed >= TIMER_DURATION_MS) {
firstCondition = false;
whitePixels.clear();
redPixels.clear();
delete[] buf;
SelectObject(hMem, old);
DeleteObject(hBitmap);
DeleteDC(hMem);
ReleaseDC(NULL, hScreen);
Sleep(RESET_DELAY_MS);
return false;
}
// DOUBLE-CHECK: Verify safety rectangles are still black
bool rect1Black = IsRectangleBlack(buf, size, SAFETY_RECT1_X, SAFETY_RECT1_Y,
SAFETY_RECT1_WIDTH, SAFETY_RECT1_HEIGHT);
bool rect2Black = IsRectangleBlack(buf, size, SAFETY_RECT2_X, SAFETY_RECT2_Y,
SAFETY_RECT2_WIDTH, SAFETY_RECT2_HEIGHT);
if (!rect1Black || !rect2Black) {
// Safety rectangles no longer black, reset condition
std::cout << "Safety check failed in second condition - resetting\n";
firstCondition = false;
whitePixels.clear();
redPixels.clear();
delete[] buf;
SelectObject(hMem, old);
DeleteObject(hBitmap);
DeleteDC(hMem);
ReleaseDC(NULL, hScreen);
return false;
}
redPixels.clear();
for (const auto& pos : whitePixels) {
int x = pos.x;
int y = pos.y;
if (x >= 0 && x < size && y >= 0 && y < size) {
BYTE b = buf[y * rowSize + x * 3 + 0];
BYTE g = buf[y * rowSize + x * 3 + 1];
BYTE r = buf[y * rowSize + x * 3 + 2];
if (IsReddish(r, g, b)) {
redPixels.push_back(pos);
}
}
}
if (redPixels.size() >= MIN_RED_PIXELS) {
secondCondition = true;
std::thread spaceThread([]() {
PressSpaceKey();
});
spaceThread.detach();
if (SAVE_ENABLED) {
BYTE* bufCopy = new BYTE[sizeBytes];
memcpy(bufCopy, buf, sizeBytes);
std::vector<PixelPos> whiteCopy = whitePixels;
std::vector<PixelPos> redCopy = redPixels;
std::thread saveThread([bufCopy, size, whiteCopy, redCopy]() {
SaveBitmapWithHighlights("output.bmp", bufCopy, size, size, whiteCopy, redCopy);
delete[] bufCopy;
});
saveThread.detach();
}
}
}
delete[] buf;
SelectObject(hMem, old);
DeleteObject(hBitmap);
DeleteDC(hMem);
ReleaseDC(NULL, hScreen);
return secondCondition;
}
// ============================================================================
// MAIN
// ============================================================================
int main() {
SetProcessDPIAware();
const char* configFile = "config.json";
// Open index.html in browser
OpenIndexHtml();
std::cout << "=== DBD Auto-Skill Check ===\n\n";
// 5 second countdown with reset detection
const int COUNTDOWN_SECONDS = 5;
bool resetRequested = false;
std::string inputBuffer;
std::cout << "Type 'reset' to restore default settings\n";
std::cout << "Starting in ";
auto startTime = std::chrono::steady_clock::now();
auto endTime = startTime + std::chrono::seconds(COUNTDOWN_SECONDS);
while (std::chrono::steady_clock::now() < endTime) {
// Check for keyboard input (non-blocking)
if (_kbhit()) {
char c = _getch();
inputBuffer += std::tolower(c);
// Check if "reset" is in buffer
if (inputBuffer.find("reset") != std::string::npos) {
resetRequested = true;
std::cout << "\n\nReset requested!\n";
break;
}
}
// Calculate remaining time
auto now = std::chrono::steady_clock::now();
auto remaining = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - now);
int remainingSeconds = remaining.count() / 1000;
int progress = ((COUNTDOWN_SECONDS - remainingSeconds) * 100) / COUNTDOWN_SECONDS;
// Update progress bar (only every 100ms to reduce flicker)
static int lastProgress = -1;
if (progress != lastProgress) {
std::cout << "\r";
std::cout << remainingSeconds + 1 << "s [";
for (int i = 0; i < 20; i++) {
if (i < progress / 5) {
std::cout << "=";
} else {
std::cout << " ";
}
}
std::cout << "] " << progress << "% ";
std::cout.flush();
lastProgress = progress;
}
Sleep(50);
}
std::cout << "\n\n";
// Handle reset or load config
if (resetRequested) {
std::cout << "Resetting configuration to defaults...\n";
SaveConfigToJson(configFile);
} else {
if (LoadConfigFromJson(configFile)) {
std::cout << "Configuration loaded from " << configFile << "\n";
} else {
std::cout << "Config file not found, creating default " << configFile << "\n";
SaveConfigToJson(configFile);
}
}
double frameDelay = 1000.0 / FPS;
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
std::cout << "\n=== ACTIVE CONFIGURATION ===\n";
std::cout << "Capture: " << CAPTURE_SIZE << "x" << CAPTURE_SIZE
<< " at (" << CAPTURE_POS_X << "," << CAPTURE_POS_Y << ") @ " << FPS << " FPS\n";
std::cout << "Safety rectangles (must be black):\n";
std::cout << " R1: (" << SAFETY_RECT1_X << "," << SAFETY_RECT1_Y << ") "
<< SAFETY_RECT1_WIDTH << "x" << SAFETY_RECT1_HEIGHT << "\n";
std::cout << " R2: (" << SAFETY_RECT2_X << "," << SAFETY_RECT2_Y << ") "
<< SAFETY_RECT2_WIDTH << "x" << SAFETY_RECT2_HEIGHT << "\n";
std::cout << "Ring: inner=" << RING_INNER_RADIUS << " outer=" << RING_OUTER_RADIUS
<< " offset=(" << RING_CENTER_OFFSET_X << "," << RING_CENTER_OFFSET_Y << ")\n";
std::cout << "Conditions: white>=" << MIN_WHITE_PIXELS << " (connected), red>=" << MIN_RED_PIXELS << "\n";
std::cout << "Thresholds: white>=" << WHITE_THRESHOLD << ", red>=" << RED_THRESHOLD
<< ", other<" << OTHER_CHANNEL_MAX << ", dominance=" << RED_DOMINANCE << "\n";
std::cout << "Timing: timer=" << TIMER_DURATION_MS << "ms, reset=" << RESET_DELAY_MS
<< "ms, space=" << SPACE_PRESS_MIN_MS << "-" << SPACE_PRESS_MAX_MS << "ms\n";
std::cout << "Save: " << (SAVE_ENABLED ? "yes" : "no") << "\n";
std::cout << "============================\n\n";
std::vector<PixelPos> whitePixels;
std::vector<PixelPos> redPixels;
bool firstCondition = false;
bool secondCondition = false;
LARGE_INTEGER timerStart;
while (true) {
LARGE_INTEGER frameStart;
QueryPerformanceCounter(&frameStart);
CaptureAndProcess(CAPTURE_SIZE, CAPTURE_POS_X, CAPTURE_POS_Y,
whitePixels, redPixels,
firstCondition, secondCondition,
timerStart, freq);
if (secondCondition) {
std::cout << "SECOND CONDITION TRUE (detected " << redPixels.size() << " red pixels)\n";
Sleep(RESET_DELAY_MS);
firstCondition = false;
secondCondition = false;
whitePixels.clear();
redPixels.clear();
}
while (true) {
LARGE_INTEGER cur;
QueryPerformanceCounter(&cur);
if ((cur.QuadPart - frameStart.QuadPart) * 1000.0 / freq.QuadPart >= frameDelay)
break;
}
}
}