<?php

// SDL initialisieren
if (!sdl_init(SDL_INIT_VIDEO)) {
    die("sdl_init failed: " . sdl_get_error() . "\n");
}

echo "SDL initialized successfully\n";

// Fenster erstellen
$window = sdl_create_window(
    "Simple Rectangle Test",
    800, 600,
    SDL_WINDOW_RESIZABLE
);

if (!$window) {
    die("sdl_create_window failed: " . sdl_get_error() . "\n");
}

echo "Window created successfully\n";

// Renderer erstellen
$renderer = sdl_create_renderer($window, null);

if (!$renderer) {
    sdl_destroy_window($window);
    die("sdl_create_renderer failed: " . sdl_get_error() . "\n");
}

echo "Renderer created successfully\n";

// Event Loop
$running = true;

while ($running) {
    // Events verarbeiten
    while ($event = sdl_poll_event()) {
        if ($event['type'] === SDL_EVENT_QUIT) {
            $running = false;
        } elseif ($event['type'] === SDL_EVENT_KEY_DOWN) {
            if ($event['keycode'] === SDLK_ESCAPE || $event['keycode'] === SDLK_Q) {
                $running = false;
            }
        }
    }

    // Bildschirm löschen (schwarz)
    sdl_set_render_draw_color($renderer, 0, 0, 0, 255);
    sdl_render_clear($renderer);

    // Rotes Rechteck zeichnen (gefüllt)
    sdl_set_render_draw_color($renderer, 255, 0, 0, 255);
    sdl_render_fill_rect($renderer, ['x' => 300, 'y' => 200, 'w' => 200, 'h' => 150]);

    // Grünes Rechteck zeichnen (Umriss)
    sdl_set_render_draw_color($renderer, 0, 255, 0, 255);
    sdl_render_rect($renderer, ['x' => 350, 'y' => 250, 'w' => 100, 'h' => 100]);

    // Blaues Rechteck zeichnen (klein, gefüllt)
    sdl_set_render_draw_color($renderer, 0, 0, 255, 255);
    sdl_render_fill_rect($renderer, ['x' => 100, 'y' => 100, 'w' => 50, 'h' => 50]);

    // Renderer anzeigen
    sdl_render_present($renderer);

    // Kleine Pause um CPU zu schonen
    sdl_delay(16); // ~60 FPS
}

// Aufräumen
sdl_destroy_renderer($renderer);
sdl_destroy_window($window);
sdl_quit();

echo "Cleanup complete\n";
