H La Vallee
H La Vallee

Reputation: 11

How do I render a box to the screen in C++ using SDL2?

I am trying to figure out the SDL2 library in C++, and can't seem to properly configure code to simply draw a rectangle to the screen. What should I do differently?

#include <SDL.h>
bool success = true;  //success flag for functions, etc
int ScreenWidth = 640;  //screen width and height for SDL window
int ScreenHeight = 480; //
int main(int argc, char* argv[])
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
    SDL_Window* window = SDL_CreateWindow( "Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, ScreenWidth, ScreenHeight, SDL_WINDOW_SHOWN);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    SDL_SetRenderDrawColor( renderer, 0xFF, 0xFF, 0xFF, 0xFF);  

    bool quit = false;
    SDL_Event event;
    while (!quit)
    {
        SDL_RenderClear(renderer);
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT)
                quit = true;
        }
        SDL_Rect box;
        box.w = 30;
        box.h = 30;
        box.x = 50;
        box.y = 50;
        SDL_SetRenderDrawColor(renderer, 0xFF,0x00,0x00,0xFF);
        SDL_RenderFillRect(renderer, &box);
        SDL_RenderPresent(renderer);    
    }
    return 0; //because main is an int task
}

EDIT: This is the full file.

I expect it to draw a red rectangle on the screen, but the window is completely red (of the correct size, name, etc. that quits correctly).

Upvotes: 0

Views: 1891

Answers (3)

H La Vallee
H La Vallee

Reputation: 11

I figured it out!

I needed to put SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); inside of the loop so it would reset the renderer to white each tick. I just had it set it back to white once at the beginning so SDL_RenderClear was filling the window with the current draw color (which was red). Thanks to TinfoilPancakes and genpfault for helping me!

Upvotes: 1

genpfault
genpfault

Reputation: 52166

SDL_SetRenderDrawColor() affects both SDL_RenderFillRect() and SDL_RenderClear().

After drawing the red square you need to re-set the color to white before the next SDL_RenderClear() call:

#include <SDL2/SDL.h>

int main( int argc, char** argv )
{
    SDL_Init( SDL_INIT_EVERYTHING );
    SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" );
    SDL_Window* window = SDL_CreateWindow( "Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN );
    SDL_Renderer* renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );

    bool running = true;
    while( running )
    {
        SDL_Event ev;
        while( SDL_PollEvent( &ev ) )
        {
            if( SDL_QUIT == ev.type )
            {
                running = false;
                break;
            }
        }

        SDL_SetRenderDrawColor( renderer, 0xFF, 0xFF, 0xFF, 0xFF );
        SDL_RenderClear( renderer );

        SDL_Rect box;
        box.w = 30;
        box.h = 30;
        box.x = 50;
        box.y = 50;
        SDL_SetRenderDrawColor( renderer, 0xFF, 0x00, 0x00, 0xFF );
        SDL_RenderFillRect( renderer, &box );

        SDL_RenderPresent( renderer );
    }

    return 0;
}

Upvotes: 1

TinfoilPancakes
TinfoilPancakes

Reputation: 248

You must call SDL_RenderPresent after all your rendering calls. In this case after SDL_RenderFillRect(...)

SDL_RenderClear is used at the beginning of a rendering loop to clear the buffer.

Example from SDL2's Wiki

Upvotes: 2

Related Questions