I'm trying to implement a simple color wheel in c using SDL. The program should print a sequence of different colored bars, that scroll across the screen and loop back to the beginning when they've traversed the whole screen, in effect making a sort of scrolling color "wheel". What I have doesn't quite work right, and also isn't particularly reusable. Can anyone advise me on how to fix what I have with a more compact solution? Here's what I have so far:
#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();
}
I figured it out:
#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();
}