#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
#include <vector>
#include <random>

// Размеры окна
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;

// Размеры барабанов
const int DRUM_WIDTH = 100;
const int DRUM_HEIGHT = 100;

// Общее количество барабанов
const int NUM_DRUMS = 5;

// Количество текстур на каждом барабане
const int NUM_TEXTURES = 3;

// Скорость вращения барабанов
std::vector<int> drumSpeeds = { 5, 4, 3, 2, 1 };


// Структура для хранения информации о барабане
struct Drum
{
    int x; // Позиция по X
    int y; // Позиция по Y
    int currentTexture; // Индекс текущей текстуры на барабане
    int rotation; // Угол поворота барабана
    int speed; // Скорость вращения барабана
    std::vector<SDL_Texture*> textures; // Вектор текстур для барабана
};

// Инициализация SDL и создание окна
bool init(SDL_Window*& window, SDL_Renderer*& renderer)
{
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        std::cerr << "Failed to initialize SDL: " << SDL_GetError() << std::endl;
        return false;
    }

    if (!IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)
    {
        std::cerr << "Failed to initialize SDL_image: " << IMG_GetError() << std::endl;
        return false;
    }

    window = SDL_CreateWindow("Slot Machine", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
    if (!window)
    {
        std::cerr << "Failed to create window: " << SDL_GetError() << std::endl;
        return false;
    }

    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    if (!renderer)
    {
        std::cerr << "Failed to create renderer: " << SDL_GetError() << std::endl;
        return false;
    }

    return true;
}

// Загрузка текстуры из файла
SDL_Texture* loadTexture(const std::string& filePath, SDL_Renderer* renderer)
{
    SDL_Surface* surface = IMG_Load(filePath.c_str());
    if (!surface)
    {
        std::cerr << "Failed to load image: " << filePath << ", " << IMG_GetError() << std::endl;
        return nullptr;
    }

    SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
    if (!texture)
    {
        std::cerr << "Failed to create texture: " << SDL_GetError() << std::endl;
        return nullptr;
    }

    SDL_FreeSurface(surface);

    return texture;
}

// Освобождение ресурсов
void cleanup(SDL_Window* window, SDL_Renderer* renderer, std::vector<Drum>& drums)
{
    for (Drum& drum : drums)
    {
        for (size_t i = 0; i < drum.textures.size(); i++)
        {           
           SDL_DestroyTexture(drum.textures[i]);
        }
    }

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    IMG_Quit();
    SDL_Quit();
}

// Инициализация барабанов
void initDrums(SDL_Renderer* renderer, std::vector<Drum>& drums)
{
    std::vector<std::vector<std::string>> texturePaths = {
        { "res/01spin.png", "res/11spin.png", "res/21spin.png" },
        { "res/02spin.png", "res/12spin.png", "res/22spin.png" },
        { "res/03spin.png", "res/13spin.png", "res/23spin.png" },
        { "res/04spin.png", "res/14spin.png", "res/24spin.png" },
        { "res/05spin.png", "res/15spin.png", "res/25spin.png" } };

        int totalDrumsWidth = NUM_DRUMS * DRUM_WIDTH;
        int totalSpacingWidth = (NUM_DRUMS - 1) * 20;
        int totalWidth = totalDrumsWidth + totalSpacingWidth;
        int startX = (WINDOW_WIDTH - totalWidth) / 2;
               
    for (int i = 0; i < NUM_DRUMS; i++)
    {
        Drum drum;      
        drum.x = startX + i * (DRUM_WIDTH + 20);
        drum.y = WINDOW_HEIGHT / 20;
        drum.currentTexture = 0;
        drum.rotation = 0;
        drum.speed = drumSpeeds[i] * 10;

        
        for (int j = 0; j < NUM_TEXTURES; j++) {
            SDL_Texture* tmp = IMG_LoadTexture(renderer, texturePaths[i][j].c_str());
            drum.textures.push_back(tmp);
        }
        
        drums.push_back(drum);

    }
      
}

// Отрисовка барабанов
void renderDrums(SDL_Renderer* renderer, std::vector<Drum>& drums)
{
    
    for (size_t i = 0; i < drums.size(); i++)
    {
        SDL_Rect drumRect = { drums[i].x, drums[i].y, DRUM_WIDTH, DRUM_HEIGHT };
        drumRect.y += DRUM_HEIGHT; SDL_RenderCopy(renderer, drums[i].textures[drums[i].currentTexture], nullptr, &drumRect);
    }

    
}

// Обновление барабанов
void updateDrums(std::vector<Drum>& drums)
{
   for (Drum& drum : drums)
    {
        drum.rotation += drum.speed;

        if (drum.rotation >= 360)
        {
            drum.rotation = 0;

            drum.currentTexture++;
            if (drum.currentTexture >= NUM_TEXTURES)
            {
                drum.currentTexture = 0;
            }
        }
    }

   
}

int main(int argc, char* args[])
{
    SDL_Window* window = nullptr;
    SDL_Renderer* renderer = nullptr;
    std::vector<Drum> drums;

    if (!init(window, renderer))
    {
        return 1;
    }

    initDrums(renderer, drums);


    SDL_Event event;
    bool isRunning = true;
    bool isRotating = false;
    Uint32 startTime = 0;

    while (isRunning)
    {
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT)
            {
                isRunning = false;
            }
            else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_SPACE)
            {
                isRotating = true;
                startTime = SDL_GetTicks();

            }
        }

        if (isRotating)
        {
            Uint32 currentTime = SDL_GetTicks();
            Uint32 elapsedTime = currentTime - startTime;

            if (elapsedTime >= 4000)
            {
                isRotating = false;
            }
            else
            {
                updateDrums(drums);
            }
        }

        

        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
        SDL_RenderClear(renderer);

        renderDrums(renderer, drums);

        SDL_RenderPresent(renderer);
    }

    cleanup(window, renderer, drums);

    return 0;
} Что делает этот код?