<?php

// Test script for SDL_GetNumRenderDrivers and SDL_GetRenderDriver
echo "SDL3 Render Drivers 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\n";

// Get number of render drivers
$numDrivers = \sdl_get_num_render_drivers();
echo "Number of available render drivers: {$numDrivers}\n\n";

// List all available render drivers
echo "Available render drivers:\n";
for ($i = 0; $i < $numDrivers; $i++) {
    $driver = sdl_get_render_driver($i);
    if ($driver !== false) {
        echo "  [{$i}] {$driver}\n";
    } else {
        echo "  [{$i}] Error getting driver\n";
    }
}

echo "\n";

// Try creating a window and renderer
echo "Creating a window...\n";
$window = sdl_create_window('Render Driver Test', 800, 600, SDL_WINDOW_HIDDEN);
if (!$window) {
    die('Failed to create window: ' . sdl_get_error() . "\n");
}
echo "Window created successfully\n\n";

// Try creating a renderer with default (null) name
echo "Attempting to create renderer with default settings...\n";
$renderer = sdl_create_renderer($window);
if (!$renderer) {
    echo 'Failed to create renderer: ' . sdl_get_error() . "\n\n";

    // Try with explicit driver names
    if ($numDrivers > 0) {
        echo "Trying with explicit driver names:\n";
        for ($i = 0; $i < $numDrivers; $i++) {
            $driverName = sdl_get_render_driver($i);
            echo "  Trying '{$driverName}'...\n";
            $renderer = sdl_create_renderer($window, $driverName);
            if ($renderer) {
                echo "  SUCCESS with '{$driverName}'\n";
                sdl_destroy_renderer($renderer);
                break;
            } else {
                echo '  FAILED: ' . sdl_get_error() . "\n";
            }
        }
    }
} else {
    echo "Renderer created successfully!\n";
    sdl_destroy_renderer($renderer);
}

// Cleanup
sdl_destroy_window($window);
sdl_quit();

echo "\nTest completed!\n";
