SDL 颜色 "wheel"

SDL color "wheel"

我正在尝试使用 SDL 在 c 中实现一个简单的色轮。该程序应该打印一系列不同颜色的条,它们在屏幕上滚动并在穿过整个屏幕后循环回到开头,实际上形成了一种滚动颜色 "wheel"。我所拥有的东西不太好用,也不是特别可重用。谁能告诉我如何用更紧凑的解决方案修复我所拥有的东西?这是我目前所拥有的:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <SDL2/SDL.h>

static const int width = 800;
static const int height = 600;
static const int rectW = width/4;

void sdlT()
{
    // Initialize SDL
SDL_Init(SDL_INIT_VIDEO);

// Create a SDL window
SDL_Window *window = SDL_CreateWindow("Hello, SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL);

// Create a renderer
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

bool running = true;
SDL_Event event;
int x = 0;
int y = 0;
int count = 0;
while(running)
{
    // Check for events
    while(SDL_PollEvent(&event))
    {
        //close SDL if user clicks window 'x'
        if(event.type == SDL_QUIT)
        {
            running = false;
        }
    }

    // Clear screen
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    SDL_RenderClear(renderer);

    SDL_Rect rect = {0, y, x < rectW ? x : x-rectW*count, height};
    SDL_Rect rect2 = {x, y, rectW, height};
    SDL_Rect rect3 = {x+rectW, y, rectW, height};
    SDL_Rect rect4 = {x+rectW*count, y, rectW, height};


    SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
    if(x < rectW){
        SDL_RenderFillRect(renderer, &rect);//fills w/ current color
    }else if(x >= rectW && x < (rectW *2)){
        SDL_RenderFillRect(renderer, &rect2);
    }else if(x>=(rectW *2) && x < (rectW *3)){
        SDL_RenderFillRect(renderer, &rect3);
    }else if (x>=(rectW *3) && x < (rectW *4)){
        SDL_RenderFillRect(renderer, &rect4);
    }


    SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
    if(x < rectW){
        SDL_RenderFillRect(renderer, &rect2);//fills w/ current color
    }else if(x >= rectW && x < (rectW *2)){
        SDL_RenderFillRect(renderer, &rect3);
    }else if(x>=(rectW *2) && x < (rectW *3)){
        SDL_RenderFillRect(renderer, &rect4);
    }else if (x>=(rectW *3) && x < (rectW *4)){
        SDL_RenderFillRect(renderer, &rect);
    }

    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    if(x < rectW){
        SDL_RenderFillRect(renderer, &rect3);//fills w/ current color
    }else if(x >= rectW && x < (rectW *2)){
        SDL_RenderFillRect(renderer, &rect4);
    }else if(x>=(rectW *2) && x < (rectW *3)){
        SDL_RenderFillRect(renderer, &rect);
    }else if (x>=(rectW *3) && x < (rectW *4)){
        SDL_RenderFillRect(renderer, &rect2);
    }

    SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255);
    if(x < rectW){
        SDL_RenderFillRect(renderer, &rect4);//fills w/ current color
    }else if(x >= rectW && x < (rectW *2)){
        SDL_RenderFillRect(renderer, &rect);
    }else if(x>=(rectW *2) && x < (rectW *3)){
        SDL_RenderFillRect(renderer, &rect2);
    }else if (x>=(rectW *3) && x < (rectW *4)){
        SDL_RenderFillRect(renderer, &rect3);
    }

    // Show what was drawn
    SDL_RenderPresent(renderer);
    x++;
    if(x >= rectW){
        count++;
        x= 0;
    }
}

// Release resources
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();

}

我想通了:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <SDL2/SDL.h>

static const int width = 800;
static const int height = 600;
static const int rectW = width/2;


int getColor(int count){
    if(count >=3){
        count = count-3;
    }
    return count;
}

void sdlT()
{
    // Initialize SDL
    SDL_Init(SDL_INIT_VIDEO);

    // Create a SDL window
    SDL_Window *window = SDL_CreateWindow("Hello, SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL);

    // Create a renderer (accelerated and in sync with the display refresh rate)
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

    bool running = true;
    SDL_Event event;
    int x = 0;
    int y = 0;
    int count = 3;
    int arr[3][3] = {{0,0,255},{0,255,0},{255,0,0}};



    while(running)
    {
                // Process events
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_QUIT)
            {
                running = false;
            }
        }

        // Clear screen with black
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        SDL_RenderClear(renderer);

        SDL_Rect rect = {0, y, x < rectW ? x : rectW, height};
        SDL_Rect rect2 = {x, y, rectW, height};
        SDL_Rect rect3 = {x+rectW, y, rectW, height};


        SDL_SetRenderDrawColor(renderer, arr[getColor(count)][0], arr[getColor(count)][1], arr[getColor(count)][2], 255);
        SDL_RenderFillRect(renderer, &rect);

        SDL_SetRenderDrawColor(renderer, arr[getColor(count+1)][0], arr[getColor(count+1)][1], arr[getColor(count+1)][2], 255);
        SDL_RenderFillRect(renderer, &rect2);

        SDL_SetRenderDrawColor(renderer, arr[getColor(count+2)][0], arr[getColor(count+2)][1], arr[getColor(count+2)][2], 255);
        SDL_RenderFillRect(renderer, &rect3);

        // Show what was drawn
        SDL_RenderPresent(renderer);
        x++;
        if(x >= rectW){
            x= 0;
            count--;
        }
        if(count == 0){
            count = 3;
        }
    }

    // Release resources
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
}