summaryrefslogtreecommitdiff
path: root/lib/graphics.cpp
blob: c41ac0ef8bc1887ad3ce93d588a3bd65fd16613b (plain)
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
#include "graphics.h"
#include <cmath>

#ifdef USE_SDL

#include <SDL2/SDL.h>

// Platform-specific window handle structure
struct WindowHandle {
    SDL_Window* window;
    SDL_Renderer* renderer;
    bool shouldClose;
    
    WindowHandle() : window(nullptr), renderer(nullptr), shouldClose(false) {}
};

// Initialize SDL (called once)
static bool sdlInitialized = false;

static void ensureSDLInit() {
    if (!sdlInitialized) {
        if (SDL_Init(SDL_INIT_VIDEO) < 0) {
            // Handle error - in production, use proper error handling
            return;
        }
        sdlInitialized = true;
    }
}

WindowHandle* createWindow(const char* title, int width, int height) {
    ensureSDLInit();
    
    WindowHandle* handle = new WindowHandle();
    
    handle->window = SDL_CreateWindow(
        title,
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        width,
        height,
        SDL_WINDOW_SHOWN
    );
    
    if (!handle->window) {
        delete handle;
        return nullptr;
    }
    
    handle->renderer = SDL_CreateRenderer(
        handle->window,
        -1,
        SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
    );
    
    if (!handle->renderer) {
        SDL_DestroyWindow(handle->window);
        delete handle;
        return nullptr;
    }
    
    // Enable alpha blending
    SDL_SetRenderDrawBlendMode(handle->renderer, SDL_BLENDMODE_BLEND);
    
    return handle;
}

void destroyWindow(WindowHandle* window) {
    if (!window) return;
    
    if (window->renderer) {
        SDL_DestroyRenderer(window->renderer);
    }
    
    if (window->window) {
        SDL_DestroyWindow(window->window);
    }
    
    delete window;
    
    // Cleanup SDL if this was the last window
    if (sdlInitialized) {
        SDL_Quit();
        sdlInitialized = false;
    }
}

bool windowShouldClose(WindowHandle* window) {
    if (!window) return true;
    return window->shouldClose;
}

void pollEvents(WindowHandle* window) {
    if (!window) return;
    
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
        if (event.type == SDL_QUIT) {
            window->shouldClose = true;
        }
        else if (event.type == SDL_KEYDOWN) {
            if (event.key.keysym.sym == SDLK_ESCAPE) {
                window->shouldClose = true;
            }
        }
    }
}

void swapBuffers(WindowHandle* window) {
    if (!window || !window->renderer) return;
    SDL_RenderPresent(window->renderer);
}

void clearScreen(WindowHandle* window, const Color& color) {
    if (!window || !window->renderer) return;
    
    SDL_SetRenderDrawColor(window->renderer, color.r, color.g, color.b, color.a);
    SDL_RenderClear(window->renderer);
}

void setDrawColor(WindowHandle* window, const Color& color) {
    if (!window || !window->renderer) return;
    SDL_SetRenderDrawColor(window->renderer, color.r, color.g, color.b, color.a);
}

void drawLine(WindowHandle* window, int x1, int y1, int x2, int y2, const Color& color) {
    if (!window || !window->renderer) return;
    
    setDrawColor(window, color);
    SDL_RenderDrawLine(window->renderer, x1, y1, x2, y2);
}

void drawRectangle(WindowHandle* window, int x, int y, int width, int height, const Color& color) {
    if (!window || !window->renderer) return;
    
    SDL_Rect rect = {x, y, width, height};
    setDrawColor(window, color);
    SDL_RenderDrawRect(window->renderer, &rect);
}

void drawFilledRectangle(WindowHandle* window, int x, int y, int width, int height, const Color& color) {
    if (!window || !window->renderer) return;
    
    SDL_Rect rect = {x, y, width, height};
    setDrawColor(window, color);
    SDL_RenderFillRect(window->renderer, &rect);
}

void drawPixel(WindowHandle* window, int x, int y, const Color& color) {
    if (!window || !window->renderer) return;
    
    setDrawColor(window, color);
    SDL_RenderDrawPoint(window->renderer, x, y);
}

// Helper function for drawing circles using midpoint circle algorithm
void drawCircle(WindowHandle* window, int centerX, int centerY, int radius, const Color& color) {
    if (!window || !window->renderer) return;
    
    setDrawColor(window, color);
    
    int x = 0;
    int y = radius;
    int d = 3 - 2 * radius;
    
    auto drawCirclePoints = [&](int xc, int yc, int x, int y) {
        SDL_RenderDrawPoint(window->renderer, xc + x, yc + y);
        SDL_RenderDrawPoint(window->renderer, xc - x, yc + y);
        SDL_RenderDrawPoint(window->renderer, xc + x, yc - y);
        SDL_RenderDrawPoint(window->renderer, xc - x, yc - y);
        SDL_RenderDrawPoint(window->renderer, xc + y, yc + x);
        SDL_RenderDrawPoint(window->renderer, xc - y, yc + x);
        SDL_RenderDrawPoint(window->renderer, xc + y, yc - x);
        SDL_RenderDrawPoint(window->renderer, xc - y, yc - x);
    };
    
    drawCirclePoints(centerX, centerY, x, y);
    
    while (y >= x) {
        x++;
        
        if (d > 0) {
            y--;
            d = d + 4 * (x - y) + 10;
        } else {
            d = d + 4 * x + 6;
        }
        
        drawCirclePoints(centerX, centerY, x, y);
    }
}

void drawFilledCircle(WindowHandle* window, int centerX, int centerY, int radius, const Color& color) {
    if (!window || !window->renderer) return;
    
    setDrawColor(window, color);
    
    for (int y = -radius; y <= radius; y++) {
        for (int x = -radius; x <= radius; x++) {
            if (x * x + y * y <= radius * radius) {
                SDL_RenderDrawPoint(window->renderer, centerX + x, centerY + y);
            }
        }
    }
}

void delay(uint32_t milliseconds) {
    SDL_Delay(milliseconds);
}

#endif // USE_SDL

#ifdef USE_WIN32

#include <windows.h>
#include <vector>

// Platform-specific window handle structure for Win32
struct WindowHandle {
    HWND hwnd;
    HDC hdc;
    HDC memDC;
    HBITMAP memBitmap;
    HBITMAP oldBitmap;
    int width;
    int height;
    bool shouldClose;
    COLORREF currentColor;
    
    WindowHandle() : hwnd(nullptr), hdc(nullptr), memDC(nullptr), 
                     memBitmap(nullptr), oldBitmap(nullptr),
                     width(0), height(0), shouldClose(false), 
                     currentColor(RGB(255, 255, 255)) {}
};

// Global window class name
static const char* WINDOW_CLASS_NAME = "LibGraffikWindowClass";
static bool classRegistered = false;

// Window procedure
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    WindowHandle* handle = (WindowHandle*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
    
    switch (uMsg) {
        case WM_CLOSE:
            if (handle) {
                handle->shouldClose = true;
            }
            return 0;
            
        case WM_KEYDOWN:
            if (wParam == VK_ESCAPE && handle) {
                handle->shouldClose = true;
            }
            return 0;
            
        case WM_PAINT: {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);
            // Will be handled by swapBuffers
            EndPaint(hwnd, &ps);
            return 0;
        }
        
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

WindowHandle* createWindow(const char* title, int width, int height) {
    // Register window class if not already registered
    if (!classRegistered) {
        WNDCLASSA wc = {};
        wc.lpfnWndProc = WindowProc;
        wc.hInstance = GetModuleHandle(nullptr);
        wc.lpszClassName = WINDOW_CLASS_NAME;
        wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
        
        if (!RegisterClassA(&wc)) {
            return nullptr;
        }
        classRegistered = true;
    }
    
    WindowHandle* handle = new WindowHandle();
    handle->width = width;
    handle->height = height;
    
    // Create window
    handle->hwnd = CreateWindowExA(
        0,
        WINDOW_CLASS_NAME,
        title,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        width, height,
        nullptr,
        nullptr,
        GetModuleHandle(nullptr),
        nullptr
    );
    
    if (!handle->hwnd) {
        delete handle;
        return nullptr;
    }
    
    // Store handle in window user data
    SetWindowLongPtr(handle->hwnd, GWLP_USERDATA, (LONG_PTR)handle);
    
    // Get device context
    handle->hdc = GetDC(handle->hwnd);
    
    // Create memory DC for double buffering
    handle->memDC = CreateCompatibleDC(handle->hdc);
    handle->memBitmap = CreateCompatibleBitmap(handle->hdc, width, height);
    handle->oldBitmap = (HBITMAP)SelectObject(handle->memDC, handle->memBitmap);
    
    // Show window
    ShowWindow(handle->hwnd, SW_SHOW);
    UpdateWindow(handle->hwnd);
    
    return handle;
}

void destroyWindow(WindowHandle* window) {
    if (!window) return;
    
    if (window->memDC) {
        if (window->oldBitmap) {
            SelectObject(window->memDC, window->oldBitmap);
        }
        DeleteDC(window->memDC);
    }
    
    if (window->memBitmap) {
        DeleteObject(window->memBitmap);
    }
    
    if (window->hdc) {
        ReleaseDC(window->hwnd, window->hdc);
    }
    
    if (window->hwnd) {
        DestroyWindow(window->hwnd);
    }
    
    delete window;
}

bool windowShouldClose(WindowHandle* window) {
    if (!window) return true;
    return window->shouldClose;
}

void pollEvents(WindowHandle* window) {
    if (!window) return;
    
    MSG msg;
    while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

void swapBuffers(WindowHandle* window) {
    if (!window || !window->hdc || !window->memDC) return;
    
    // Copy from memory DC to window DC
    BitBlt(window->hdc, 0, 0, window->width, window->height,
           window->memDC, 0, 0, SRCCOPY);
}

void clearScreen(WindowHandle* window, const Color& color) {
    if (!window || !window->memDC) return;
    
    RECT rect = {0, 0, window->width, window->height};
    HBRUSH brush = CreateSolidBrush(RGB(color.r, color.g, color.b));
    FillRect(window->memDC, &rect, brush);
    DeleteObject(brush);
}

void setDrawColor(WindowHandle* window, const Color& color) {
    if (!window) return;
    window->currentColor = RGB(color.r, color.g, color.b);
}

void drawLine(WindowHandle* window, int x1, int y1, int x2, int y2, const Color& color) {
    if (!window || !window->memDC) return;
    
    HPEN pen = CreatePen(PS_SOLID, 1, RGB(color.r, color.g, color.b));
    HPEN oldPen = (HPEN)SelectObject(window->memDC, pen);
    
    MoveToEx(window->memDC, x1, y1, nullptr);
    LineTo(window->memDC, x2, y2);
    
    SelectObject(window->memDC, oldPen);
    DeleteObject(pen);
}

void drawRectangle(WindowHandle* window, int x, int y, int width, int height, const Color& color) {
    if (!window || !window->memDC) return;
    
    HPEN pen = CreatePen(PS_SOLID, 1, RGB(color.r, color.g, color.b));
    HPEN oldPen = (HPEN)SelectObject(window->memDC, pen);
    HBRUSH oldBrush = (HBRUSH)SelectObject(window->memDC, GetStockObject(NULL_BRUSH));
    
    Rectangle(window->memDC, x, y, x + width, y + height);
    
    SelectObject(window->memDC, oldPen);
    SelectObject(window->memDC, oldBrush);
    DeleteObject(pen);
}

void drawFilledRectangle(WindowHandle* window, int x, int y, int width, int height, const Color& color) {
    if (!window || !window->memDC) return;
    
    RECT rect = {x, y, x + width, y + height};
    HBRUSH brush = CreateSolidBrush(RGB(color.r, color.g, color.b));
    FillRect(window->memDC, &rect, brush);
    DeleteObject(brush);
}

void drawPixel(WindowHandle* window, int x, int y, const Color& color) {
    if (!window || !window->memDC) return;
    
    SetPixel(window->memDC, x, y, RGB(color.r, color.g, color.b));
}

// Helper function for drawing circles using midpoint circle algorithm
static void drawCirclePoints(HDC dc, int xc, int yc, int x, int y, COLORREF color) {
    SetPixel(dc, xc + x, yc + y, color);
    SetPixel(dc, xc - x, yc + y, color);
    SetPixel(dc, xc + x, yc - y, color);
    SetPixel(dc, xc - x, yc - y, color);
    SetPixel(dc, xc + y, yc + x, color);
    SetPixel(dc, xc - y, yc + x, color);
    SetPixel(dc, xc + y, yc - x, color);
    SetPixel(dc, xc - y, yc - x, color);
}

void drawCircle(WindowHandle* window, int centerX, int centerY, int radius, const Color& color) {
    if (!window || !window->memDC) return;
    
    COLORREF col = RGB(color.r, color.g, color.b);
    int x = 0;
    int y = radius;
    int d = 3 - 2 * radius;
    
    drawCirclePoints(window->memDC, centerX, centerY, x, y, col);
    
    while (y >= x) {
        x++;
        
        if (d > 0) {
            y--;
            d = d + 4 * (x - y) + 10;
        } else {
            d = d + 4 * x + 6;
        }
        
        drawCirclePoints(window->memDC, centerX, centerY, x, y, col);
    }
}

void drawFilledCircle(WindowHandle* window, int centerX, int centerY, int radius, const Color& color) {
    if (!window || !window->memDC) return;
    
    COLORREF col = RGB(color.r, color.g, color.b);
    
    for (int y = -radius; y <= radius; y++) {
        for (int x = -radius; x <= radius; x++) {
            if (x * x + y * y <= radius * radius) {
                SetPixel(window->memDC, centerX + x, centerY + y, col);
            }
        }
    }
}

void delay(uint32_t milliseconds) {
    Sleep(milliseconds);
}

#endif // USE_WIN32

#ifdef USE_X11

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <unistd.h>
#include <cstring>

// Platform-specific window handle structure for X11
struct WindowHandle {
    Display* display;
    Window window;
    GC gc;
    Pixmap backBuffer;
    int width;
    int height;
    bool shouldClose;
    Atom wmDeleteMessage;
    unsigned long currentColor;
    
    WindowHandle() : display(nullptr), window(0), gc(nullptr), 
                     backBuffer(0), width(0), height(0), 
                     shouldClose(false), wmDeleteMessage(0),
                     currentColor(0xFFFFFF) {}
};

// Helper to convert Color to X11 pixel value
static unsigned long colorToPixel(Display* display, const Color& color) {
    int screen = DefaultScreen(display);
    Colormap colormap = DefaultColormap(display, screen);
    XColor xcolor;
    
    xcolor.red = color.r * 257;   // Convert 0-255 to 0-65535
    xcolor.green = color.g * 257;
    xcolor.blue = color.b * 257;
    xcolor.flags = DoRed | DoGreen | DoBlue;
    
    XAllocColor(display, colormap, &xcolor);
    return xcolor.pixel;
}

WindowHandle* createWindow(const char* title, int width, int height) {
    WindowHandle* handle = new WindowHandle();
    handle->width = width;
    handle->height = height;
    
    // Open connection to X server
    handle->display = XOpenDisplay(nullptr);
    if (!handle->display) {
        delete handle;
        return nullptr;
    }
    
    int screen = DefaultScreen(handle->display);
    Window root = RootWindow(handle->display, screen);
    
    // Create window
    handle->window = XCreateSimpleWindow(
        handle->display,
        root,
        0, 0,
        width, height,
        1,
        BlackPixel(handle->display, screen),
        BlackPixel(handle->display, screen)
    );
    
    if (!handle->window) {
        XCloseDisplay(handle->display);
        delete handle;
        return nullptr;
    }
    
    // Set window title
    XStoreName(handle->display, handle->window, title);
    
    // Select input events
    XSelectInput(handle->display, handle->window, 
                 ExposureMask | KeyPressMask | StructureNotifyMask);
    
    // Handle window close event
    handle->wmDeleteMessage = XInternAtom(handle->display, "WM_DELETE_WINDOW", False);
    XSetWMProtocols(handle->display, handle->window, &handle->wmDeleteMessage, 1);
    
    // Create graphics context
    handle->gc = XCreateGC(handle->display, handle->window, 0, nullptr);
    
    // Create back buffer for double buffering
    handle->backBuffer = XCreatePixmap(handle->display, handle->window, 
                                       width, height, 
                                       DefaultDepth(handle->display, screen));
    
    // Map window to screen
    XMapWindow(handle->display, handle->window);
    
    // Wait for window to be mapped
    XEvent event;
    do {
        XNextEvent(handle->display, &event);
    } while (event.type != MapNotify);
    
    return handle;
}

void destroyWindow(WindowHandle* window) {
    if (!window) return;
    
    if (window->display) {
        if (window->backBuffer) {
            XFreePixmap(window->display, window->backBuffer);
        }
        
        if (window->gc) {
            XFreeGC(window->display, window->gc);
        }
        
        if (window->window) {
            XDestroyWindow(window->display, window->window);
        }
        
        XCloseDisplay(window->display);
    }
    
    delete window;
}

bool windowShouldClose(WindowHandle* window) {
    if (!window) return true;
    return window->shouldClose;
}

void pollEvents(WindowHandle* window) {
    if (!window || !window->display) return;
    
    XEvent event;
    while (XPending(window->display) > 0) {
        XNextEvent(window->display, &event);
        
        switch (event.type) {
            case ClientMessage:
                if ((Atom)event.xclient.data.l[0] == window->wmDeleteMessage) {
                    window->shouldClose = true;
                }
                break;
                
            case KeyPress: {
                KeySym key = XLookupKeysym(&event.xkey, 0);
                if (key == XK_Escape) {
                    window->shouldClose = true;
                }
                break;
            }
        }
    }
}

void swapBuffers(WindowHandle* window) {
    if (!window || !window->display || !window->gc) return;
    
    // Copy back buffer to window
    XCopyArea(window->display, window->backBuffer, window->window, window->gc,
              0, 0, window->width, window->height, 0, 0);
    
    // Flush the output buffer
    XFlush(window->display);
}

void clearScreen(WindowHandle* window, const Color& color) {
    if (!window || !window->display || !window->gc) return;
    
    unsigned long pixel = colorToPixel(window->display, color);
    XSetForeground(window->display, window->gc, pixel);
    XFillRectangle(window->display, window->backBuffer, window->gc,
                   0, 0, window->width, window->height);
}

void setDrawColor(WindowHandle* window, const Color& color) {
    if (!window || !window->display || !window->gc) return;
    
    window->currentColor = colorToPixel(window->display, color);
    XSetForeground(window->display, window->gc, window->currentColor);
}

void drawLine(WindowHandle* window, int x1, int y1, int x2, int y2, const Color& color) {
    if (!window || !window->display || !window->gc) return;
    
    setDrawColor(window, color);
    XDrawLine(window->display, window->backBuffer, window->gc, x1, y1, x2, y2);
}

void drawRectangle(WindowHandle* window, int x, int y, int width, int height, const Color& color) {
    if (!window || !window->display || !window->gc) return;
    
    setDrawColor(window, color);
    XDrawRectangle(window->display, window->backBuffer, window->gc, x, y, width, height);
}

void drawFilledRectangle(WindowHandle* window, int x, int y, int width, int height, const Color& color) {
    if (!window || !window->display || !window->gc) return;
    
    setDrawColor(window, color);
    XFillRectangle(window->display, window->backBuffer, window->gc, x, y, width, height);
}

void drawPixel(WindowHandle* window, int x, int y, const Color& color) {
    if (!window || !window->display || !window->gc) return;
    
    setDrawColor(window, color);
    XDrawPoint(window->display, window->backBuffer, window->gc, x, y);
}

// Helper function for drawing circles using midpoint circle algorithm
static void drawCirclePoints(Display* display, Drawable drawable, GC gc,
                             int xc, int yc, int x, int y) {
    XDrawPoint(display, drawable, gc, xc + x, yc + y);
    XDrawPoint(display, drawable, gc, xc - x, yc + y);
    XDrawPoint(display, drawable, gc, xc + x, yc - y);
    XDrawPoint(display, drawable, gc, xc - x, yc - y);
    XDrawPoint(display, drawable, gc, xc + y, yc + x);
    XDrawPoint(display, drawable, gc, xc - y, yc + x);
    XDrawPoint(display, drawable, gc, xc + y, yc - x);
    XDrawPoint(display, drawable, gc, xc - y, yc - x);
}

void drawCircle(WindowHandle* window, int centerX, int centerY, int radius, const Color& color) {
    if (!window || !window->display || !window->gc) return;
    
    setDrawColor(window, color);
    
    int x = 0;
    int y = radius;
    int d = 3 - 2 * radius;
    
    drawCirclePoints(window->display, window->backBuffer, window->gc, 
                    centerX, centerY, x, y);
    
    while (y >= x) {
        x++;
        
        if (d > 0) {
            y--;
            d = d + 4 * (x - y) + 10;
        } else {
            d = d + 4 * x + 6;
        }
        
        drawCirclePoints(window->display, window->backBuffer, window->gc,
                        centerX, centerY, x, y);
    }
}

void drawFilledCircle(WindowHandle* window, int centerX, int centerY, int radius, const Color& color) {
    if (!window || !window->display || !window->gc) return;
    
    setDrawColor(window, color);
    
    // X11 has XFillArc which is more efficient
    XFillArc(window->display, window->backBuffer, window->gc,
             centerX - radius, centerY - radius,
             radius * 2, radius * 2,
             0, 360 * 64);  // Angles in X11 are in 1/64ths of a degree
}

void delay(uint32_t milliseconds) {
    usleep(milliseconds * 1000);
}

#endif // USE_X11