[ SDL2 — Part 3] Drawing rectangles

Previous part

The SDL 2 coordinate system
struct SDL_Rect {
uint16 x// : the x position of the rectangle
uint16 y// : the y position of the rectangle
uint16 w // : the width of the rectangle
uint16 h //: the height of the rectangle
}

Drawing a rectangle

int SDL_RenderDrawRect
(
SDL_Renderer* renderer,
SDL_Rect* rectangle
)

And now the fun stuff

// … After setting up renderer and window// Set color of renderer to red
SDL_SetRenderDrawColor( renderer, 255, 0, 0, 255 );
// Clear the window and make it all red
SDL_RenderClear( renderer );
// Create a rectangle
SDL_Rect r;
r.x = 50;
r.y = 50;
r.w = 50;
r.h = 50;
// Render our SDL_Rect
// The top left pos of the rect will be at 50, 50
// It will stretch 50 to the left and 50 towards the bottom
SDL_RenderDrawRect( renderer, &r );
// Render the changes above
// ( which up until now had just happened behind the scenes )
SDL_RenderPresent( renderer);
// After setting up renderer and window
// Set color of renderer to red
SDL_SetRenderDrawColor( renderer, 255, 0, 0, 255 );
// Clear the window and make it all red
SDL_RenderClear( renderer );
// Create a rectangle
SDL_Rect r;
r.x = 50;
r.y = 50;
r.w = 50;
r.h = 50;
// Change color to blue!
SDL_SetRenderDrawColor( renderer, 0, 0, 255, 255 );
// Render our SDL_Rect
// The rectangle will now be blue
SDL_RenderDrawRect( renderer, &r );
// Render the changes above
SDL_RenderPresent( renderer);

Filling it up…

int SDL_RenderFillRect
(
SDL_Renderer* renderer,
SDL_Rect rectangle
)

Conclusion

Next part

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store