<?php

putenv('XLOCALEDIR=/usr/share/X11/locale');

echo "Simple SDL3 Renderer Test\n";
echo "=========================\n\n";

// Initialize SDL
if (!sdl_init(SDL_INIT_VIDEO)) {
    die('Failed to initialize SDL: ' . sdl_get_error() . "\n");
}

echo "SDL initialized successfully\n";

// Get current video driver
$videoDriver = sdl_get_current_video_driver();
echo 'Current video driver: ' . ($videoDriver ?: 'none') . "\n\n";

// Create window with different flags to test
$flags = SDL_WINDOW_HIDDEN;

echo "Creating window with HIDDEN flag...\n";
$window = sdl_create_window('Renderer Test', 800, 600, $flags);
if (!$window) {
    die('Failed to create window: ' . sdl_get_error() . "\n");
}
echo "Window created successfully\n\n";

// Get video driver after window creation
$videoDriver = sdl_get_current_video_driver();
echo 'Video driver after window: ' . ($videoDriver ?: 'none') . "\n\n";

// Try to create renderer
echo "Attempting to create renderer...\n";
$renderer = sdl_create_renderer($window);
if (!$renderer) {
    $error = sdl_get_error();
    echo "FAILED to create renderer: {$error}\n\n";

    // Try with a visible window instead
    echo "Destroying window and trying with visible window...\n";
    sdl_destroy_window($window);

    $window = sdl_create_window('Renderer Test', 800, 600, 0);
    if (!$window) {
        die('Failed to create visible window: ' . sdl_get_error() . "\n");
    }
    echo "Visible window created\n";

    echo "Attempting to create renderer on visible window...\n";
    $renderer = sdl_create_renderer($window);
    if (!$renderer) {
        echo 'FAILED again: ' . sdl_get_error() . "\n";
    } else {
        echo "SUCCESS on visible window!\n";
        sdl_destroy_renderer($renderer);
    }

    sdl_destroy_window($window);
} else {
    echo "SUCCESS! Renderer created\n";
    sdl_destroy_renderer($renderer);
    sdl_destroy_window($window);
}

sdl_quit();
echo "\nTest completed\n";
