I'm trying to write a program that has a translucent background covering the whole screen. After some research it appeared that SDL would be the way to go.
I've written the code to create a full screen window with a background whose alpha is equal to 100 (out of 255), but for some reason it just draws the solid colour. What have I done wrong?
// Initialise SDL
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
this->throwSDLError("SDL_Init Error");
}
// Create the window and renderer
if (SDL_CreateWindowAndRenderer(0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP, &(this->window), &(this->renderer)) != 0) {
this->throwSDLError("Could not create the window and renderer");
}
// Set the blend mode to specify how the alpha channel is used
if (SDL_SetRenderDrawBlendMode(this->renderer, SDL_BLENDMODE_BLEND) != 0) {
this->throwSDLError("Could not set render draw blend mode");
}
// Set the colour to draw
if (SDL_SetRenderDrawColor(this->renderer, 200, 200, 200, 100) != 0) {
this->throwSDLError("Could not set the drawing colour");
}
// Clear the screen using the colour
if (SDL_RenderClear(this->renderer) != 0) {
this->throwSDLError("Could not render the screen");
}
// Present the rendered screen
SDL_RenderPresent(this->renderer);
On Windows, you can create a transparent window by using SetLayeredWindowAttributes to chroma-key the background color from a borderless SDL window.
Code:
// SDL window with transparent background v1.2
#include <SDL.h>
#include <SDL_syswm.h>
#include <Windows.h>
// Makes a window transparent by setting a transparency color.
bool MakeWindowTransparent(SDL_Window* window, COLORREF colorKey) {
// Get window handle (https://stackoverflow.com/a/24118145/3357935)
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version); // Initialize wmInfo
SDL_GetWindowWMInfo(window, &wmInfo);
HWND hWnd = wmInfo.info.win.window;
// Change window type to layered (https://stackoverflow.com/a/3970218/3357935)
SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
// Set transparency color
return SetLayeredWindowAttributes(hWnd, colorKey, 0, LWA_COLORKEY);
}
int main(int argc, char** argv) {
// Get resolution of primary monitor
int desktopWidth = GetSystemMetrics(SM_CXSCREEN);
int desktopHeight = GetSystemMetrics(SM_CYSCREEN);
SDL_Window* window = SDL_CreateWindow("SDL Transparent Window",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
desktopWidth, desktopHeight, SDL_WINDOW_BORDERLESS);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
// Set background color to magenta and clear screen
SDL_SetRenderDrawColor(renderer, 255, 0, 255, 255);
SDL_RenderClear(renderer);
// Draw blue square in top-left corner
SDL_Rect rect1 = {0, 0, 100, 100};
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderFillRect(renderer, &rect1);
// Draw red square in center of the screen
SDL_Rect rect2 = {(desktopWidth-100)/2, (desktopHeight-100)/2, 100, 100};
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &rect2);
// Add window transparency (Magenta will be see-through)
MakeWindowTransparent(window, RGB(255, 0, 255));
// Render the square to the screen
SDL_RenderPresent(renderer);
// Loop until user quits
bool quit = false;
SDL_Event event;
while (!quit) {
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
quit = true;
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Result:
Explanation:
First, create a borderless window with that covers the entire desktop. Choose a solid masking color and use it as your background. (In my case, I used magenta). You can then key-out your masking color with the Win32 API function SetLayeredWindowAttributes.
Any part of the window with this color will be completely see-through. Other windows behind your program can be interacted with as normal. By default, other applications can be moved on top of your borderless window.
If you want your SDL window to always be on top of other windows, you can set the SDL_WINDOW_ALWAYS_ON_TOP flag when creating your window.
See Also
Stack Overflow: Creating a transparent window in C++ Win32
Stack Overflow: How detect current screen resolution?
Microsoft: Layered Windows
Related
I know there are already questions like this on SO but I am new to SDL2 and I have had a look at some tutorials to draw a rectangle and I have had a look at some questions here as well. With the current code below I cannot draw a rectanlge onto the window as it doesn't appear I have also tried to clear the window after the rectangle is meant to be drawn this just sets the entire window to that colour.
#include <iostream>
#include <SDL2\SDL.h>
int main(int argc, char** argv){
SDL_Window* window;
window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer* render;
render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
bool close = false;
SDL_Event event;
while (close == false){
while (SDL_PollEvent(&event)){
if (event.type == SDL_QUIT){
close = true;
}
}
//set colour
SDL_SetRenderDrawColor(render, 255, 0, 0, 255);
SDL_RenderClear(render);
SDL_Rect rect;
rect.x, rect.y, rect.h, rect.w = 50, 50, 50, 50;
SDL_SetRenderDrawColor(render, 0, 0, 255, 255);
SDL_RenderFillRect(render, &rect);
//SDL_RenderClear(render); <-- If this is uncommented it clears the screen to blue.
SDL_RenderPresent(render);
}
SDL_DestroyWindow(window);
SDL_DestroyRenderer(render);
SDL_Quit();
}
This is mean't to draw a blue rectangle onto a red window but I only get the red window and clearing the renderer only set's the window blue. Why does this rectangle not draw? If it is drawing then how come the colour of it ahsnt changed so it doesn't blend in with the background.
I have used these questions to help me get this far:
How to draw a rectangle in SDL 2 and what exactly is a renderer
SDL2 Wont draw Rectangles Correctly
https://dev.to/noah11012/using-sdl2-drawing-rectangles-3hc2
Your issue seems to be with the rect.x, rect.y, rect.h, rect.w = 50, 50, 50, 50;
When I changed it to
rect.x = 50;
rect.y = 50;
rect.h = 50;
rect.w = 50;
The program worked
I have been working on a fairly large app in SDL for a while and recently noticed a strange bug; whenever a window is closed, everything on another window will render completely black. When I draw red a line, it will be black, and when I draw an SDL_Texture, it will draw a black rectangle in place of the image.
After a while, I managed to recreate the problem by making a simplified version of the app from the ground up. The program includes a window class that stores a window, its renderer, and an SDL_Texture. The window class also includes a render function to render things on to its window including the SDL_Texture. There is also a function that allows each window object to handle SDL events such as the window being closed. When the window is closed, the window, renderer, and texture are all destroyed.
class Window {
SDL_Window *window;
SDL_Renderer *renderer;
Uint32 windowID;
SDL_Texture *texture;
void cleanup() {
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_DestroyTexture(texture);
}
bool running = true;
public:
void init() {
window = SDL_CreateWindow("Window", 50, 50, 721, 558, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
windowID = SDL_GetWindowID(window);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
// load texture
SDL_Surface* loadedSurface = IMG_Load("picture.png");
texture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
SDL_FreeSurface(loadedSurface);
}
bool isRunning() {
return running;
}
void render() {
// clear the screen with a green color
SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
SDL_RenderClear(renderer);
// create a rectangle for drawing things
SDL_Rect rect = {0, 0, 100, 100};
// draw a red rectangle
SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
SDL_RenderFillRect(renderer, &rect);
// draw the texture/image on top of the rectangle
if (SDL_RenderCopy(renderer, texture, NULL, &rect) < 0) {
printf("Unable to render texture! Error: %s\n", SDL_GetError());
}
SDL_RenderPresent(renderer);
}
void events(SDL_Event &e) {
if (e.window.windowID == windowID && e.window.event == SDL_WINDOWEVENT_CLOSE) {
running = false;
cleanup();
}
}
};
The main function manages multiple windows at a time, and separately feeds each one of them SDL events:
int main(int argc, const char * argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) { // initialize SDL and IMG
printf("SDL could not initialize! Error: %s\n", SDL_GetError());
} else if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)) {
printf("SDL_image could not initialize! Error: %s\n", IMG_GetError());
} else {
std::vector<Window*> windows; // vector of window objects
// add window objects to the vector
windows.push_back(new Window());
windows.push_back(new Window());
windows.push_back(new Window());
windows.push_back(new Window());
// initialize all windows
for (int i = 0; i < windows.size(); i++) {
windows[i]->init();
}
// game loop
bool loop = true;
while (loop) {
SDL_Delay(50); // delay between each frame
// render all windows
for (int i = 0; i < windows.size(); i++) {
windows[i]->render();
}
// handle new events
SDL_Event e;
while (SDL_PollEvent(&e)) {
// loop backward through windows
// in case one of them has to be
// removed from the vector
for (unsigned long i = windows.size(); i-- > 0;) {
if (windows[i]->isRunning()) {
windows[i]->events(e);
} else {
// delete a window if it has been closed
delete windows[i];
windows.erase(windows.begin() + i);
}
}
}
if (windows.empty()) { // if all windows are closed,
loop = false; // stop the loop
}
}
}
return 0;
}
Now that I have shown my code, I will explain the issue in more detail. When the program starts, four window objects are created, and the windows each show up on the screen. Each of the windows render properly, with a PNG image overlaid on top of a red rectangle. The fourth window, or the last one to be initialized, will only render things such as images, rectangles, and lines in the color black after any other window is closed. The only thing that will render in a different color is SDL_RenderClear, which I used in the above code to render a green background, which exposes the black image and rectangle shapes.
I have tested to make sure the texture, renderer, and window are still valid after this problem occurs, and got no errors.
I would at least like to find what is causing the problem and if there is any possible solution.
You should not destroy window while renderer is still alive. Try executing methods inside of cleanup() in opposite order.
void cleanup()
{
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
}
I've created a window in SDL2 that opens in the color red. I need the window to change color after a few seconds and continue to do so as long as the window is open. So the window may open red, stay for five seconds then become green for five seconds then become blue for five seconds then loop back to red and begin the whole process again.
int WindowOpen() {
bool quit = false;
SDL_Window *window; // Declare a pointer
SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2
// Create an application window with the following settings:
window = SDL_CreateWindow(
"My SDL2 window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);
SDL_Renderer *renderer = NULL;
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Event* MainEvent = new SDL_Event();
SDL_RenderClear(renderer);
// Up until now everything was drawn behind the scenes.
// This will show the new, red contents of the window.
SDL_RenderPresent(renderer);
// Check that the window was successfully created
if (window == NULL) {
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);//red. Was testing to see if window would open red and change to green then blue but this doesn't work.
//SDL_Delay(3000);
//SDL_SetRenderDrawColor(renderer, 300, 150, 0, 155);//green
//SDL_Delay(3000);
//SDL_SetRenderDrawColor(renderer, 129, 150, 500, 105);//blue
while (quit == false && MainEvent->type != SDL_QUIT) { //While quit is false, run window and renderer.
SDL_PollEvent(MainEvent);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
// The window is open: could enter program loop here (see SDL_PollEvent())
//SDL_Delay(3000); // Pause execution for 3000 milliseconds, for example
// Close and destroy the window
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
delete MainEvent;
// Clean up
SDL_Quit();
}
SDL_SetRenderDrawColor is just setting the colour the renderer would paint, if you asked it to. To fill the back buffer with that colour you should follow it up with SDL_RenderClear. With the back buffer filled, to then draw it on the screen you need to call SDL_RenderPresent.
while (quit == false && MainEvent->type != SDL_QUIT) { //While quit is false, run window and renderer.
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);//red. Was testing to see if window would open red and change to green then blue but this doesn't work.
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_Delay(3000);
SDL_SetRenderDrawColor(renderer, 300, 150, 0, 155);//green
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_Delay(3000);
SDL_SetRenderDrawColor(renderer, 129, 150, 500, 105);//blue
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_Delay(3000);
SDL_PollEvent(MainEvent);
}
Changing your main loop to something like that will do similar to what you want, but the delay is three seconds. Also, this way your main loop will take 9 seconds to execute, which means event handling for quit could take up to 9 seconds. This isn't very good, but fixing it is left as an exercise for the reader.
I've written a program using C++ and SDL2 which:
creates a window
gets the window's surface
creates a renderer for the window
renders some filled rectangles onto the window
creates a texture from the window's surface
clears the screen
renders some filled circles onto the window
creates a second texture from the window's surface
enters an event loop, where every time a key is pressed:
if circles are currently being displayed, SDL_RenderCopy() is used to copy the squares texture to the window.
else if squares are currently being displayed, the circles texture is copied to the window.
The program works perfectly if the renderer is created with the SDL_RENDERER_SOFTWARE flag.
If the renderer is created with the SDL_RENDERER_ACCELERATED flag, I find that while I can render direct to the screen, if I create a couple of different textures from the window's surface and then try to copy them back to the window using SDL_RenderCopy(); all I see is a black window.
I can't find any failing SDL calls.
I've wondered whether there might be some incompatibility between the texture format and the renderer - but I'm not sure how to follow this up.
Any help or suggestions?
My environment is :
Windows 10
Visual Studio Community 2015
SDL2 version 2.0.4
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Additional information and source code :
I've added cut down source code to demonstrate the problem below.
Note, to keep down the size, I've removed all of the error checking and consolidated the relevant code into a single main function.
What I get is that the program works for me as expected if I uncomment line 40 so that I am calling SDL_CreateRenderer with the SDL_RENDERER_SOFTWARE flag.
If I un-comment any of the other SDL_CreateRenderer lines instead (line 41-43 : to use hardware acceleration), I see the red and blue squares when they are initially rendered to screen.
But as I press keys, instead of the window flicking between red and blue squares, I'm looking at a black window.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main(int argc, char* args[])
{
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
//And two textures, one for a red square, on for a blue square
SDL_Texture* texture_red = NULL;
SDL_Texture* texture_blue = NULL;
//The window renderer
SDL_Renderer* gRenderer = NULL;
//Initialize SDL
SDL_Init(SDL_INIT_VIDEO);
//Create window
gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
//Get the screen surface
gScreenSurface = SDL_GetWindowSurface(gWindow);
//Create renderer for window
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_SOFTWARE);
//gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
//gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_PRESENTVSYNC);
//gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
/*###########################################################################################
# I can not figure out how to make this program work with hardware acceleration. It works #
# fine when I define the renderer using SDL_RENDERER_SOFTWARE, but doesn't display anything #
# if I define the renerer using the SDL_RENDERER_ACCELERATED flag #
###########################################################################################*/
//Initialize renderer color
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
//Clear screen
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(gRenderer);
//Render red filled quad
SDL_Rect fillRect = { 100, 75, 100, 100 };
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0x00, 0x00, 0xFF);
SDL_RenderFillRect(gRenderer, &fillRect);
//Update the rendered image on screen
SDL_RenderPresent(gRenderer);
//Pause long enough to see it
SDL_Delay(200);
//Create texture_red texture from the screen surface
texture_red = SDL_CreateTextureFromSurface(gRenderer, gScreenSurface);
//Clear screen
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(gRenderer);
//Render blue filled quad
fillRect = { 225, 250, 100, 100 };
SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0xFF, 0xFF);
SDL_RenderFillRect(gRenderer, &fillRect);
//Update the rendered image on screen
SDL_RenderPresent(gRenderer);
//Pause long enough to see it
SDL_Delay(200);
//Create texture_red texture from the screen surface
texture_blue = SDL_CreateTextureFromSurface(gRenderer, gScreenSurface);
//Main loop flag
bool quit = false;
//Flag to keep track of which colour we're currently looking at
bool blue = true;
//Event handler
SDL_Event e;
//While application is running
while (!quit)
{
//Handle events on queue
while (SDL_PollEvent(&e) != 0)
{
//User requests quit
if (e.type == SDL_QUIT)
{
quit = true;
}
//User presses a key
else if (e.type == SDL_KEYDOWN)
{
//Select surfaces based on key press
switch (e.key.keysym.sym)
{
case SDLK_ESCAPE:
quit = true;
break;
default:
if (blue)
{
//Copy surface used to store red image onto the screen surface
SDL_RenderCopy(gRenderer, texture_red, NULL, NULL);
//Update current colour flag
blue = false;
}
else
{
//Copy surface used to store blue image onto the screen surface
SDL_RenderCopy(gRenderer, texture_blue, NULL, NULL);
//Update current colour flag
blue = true;
}
//Update the screen with recent render activity
SDL_RenderPresent(gRenderer);
break;
}
}
}
}
//Deallocate surfaces
SDL_FreeSurface(gScreenSurface);
//Destroy window
SDL_DestroyWindow(gWindow);
gWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
return 0;
}
The SDL_GetWindowSurface() header comment specifically prohibits using it with the SDL_Renderer functionality:
/**
* \brief Get the SDL surface associated with the window.
*
* \return The window's framebuffer surface, or NULL on error.
*
* A new surface will be created with the optimal format for the window,
* if necessary. This surface will be freed when the window is destroyed.
*
* \note You may not combine this with 3D or the rendering API on this window.
*
* \sa SDL_UpdateWindowSurface()
* \sa SDL_UpdateWindowSurfaceRects()
*/
extern DECLSPEC SDL_Surface * SDLCALL SDL_GetWindowSurface(SDL_Window * window);
Use SDL_RENDERER_TARGETTEXTURE & SDL_SetRenderTarget() if you want to capture SDL_Renderer output.
You can use SDL_GetRendererInfo() to query a compatible texture format. Or just blast ahead with SDL_PIXELFORMAT_ARGB8888 like testrendertarget.c does and hope for the best :)
Try adding SDL_RENDERER_PRESENTVSYNC to your renderer's flag (Proposed by someone here).
C++ seems to be the programming language you're using but for those looking for a fully working SDL2.0.8-based C program that does not use GetWindowSurface(), please take a look here:
Mainly,
initWindowAndRenderer
drawText
drawImage
drawVideo
setBgColor
drawVideo
...
Here is an extract with error-checking removed:
// Create a window usable with OpenGL context
window = SDL_CreateWindow("Title", x, y, w, h, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN);
// Select render driver
// - A render driver that supports HW acceleration is used when available
// - Otherwise a render driver supporting software fallback is selected
SDL_RendererInfo renderDriverInfo;
uint32_t rendererFlags = SDL_RENDERER_TARGETTEXTURE;
int32_t nbRenderDrivers = SDL_GetNumRenderDrivers(), index = 0;
while (index < nbRenderDrivers)
{
if (SDL_GetRenderDriverInfo(index, &renderDriverInfo) == 0)
{
if (((renderDriverInfo.flags & rendererFlags) == rendererFlags)
&& ((renderDriverInfo.flags & SDL_RENDERER_ACCELERATED) == SDL_RENDERER_ACCELERATED))
{
// Using render driver with HW acceleration
rendererFlags |= SDL_RENDERER_ACCELERATED;
SDL_SetHint(SDL_HINT_RENDER_DRIVER, renderDriverInfo.name);
break;
}
}
++index;
}
if (index == nbRenderDrivers)
{
// Let SDL use the first render driver supporting software fallback
rendererFlags |= SDL_RENDERER_SOFTWARE;
index = -1;
}
// Create renderer
renderer = SDL_CreateRenderer(window, index, rendererFlags);
I dont exactly understand what renderer is. Can I have multiple renderers or is there always just one?
For example, how can I draw a rectangle with a certain color on a background with a different color using a renderer?
I believe the answer lies in the functions SDL_RenderDrawRect() and SDL_RenderFillRect(). Am I right?
I know how surfaces and bliting works but I dont know what exactly the renderer symbolizes.
If someone could show me how to draw a rectangle, I think i will understand how renderers work.
So far I have this:
#include <SDL.h>
int main(int argc, char* argv[]) {
//Initialization
SDL_Init(SDL_INIT_EVERYTHING);
//Window
SDL_Window *MainWindow = SDL_CreateWindow("My Game Window",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
640, 480,
SDL_WINDOW_SHOWN
);
//Renderer
SDL_Renderer *Background = SDL_CreateRenderer(MainWindow, -1, 0);
SDL_SetRenderDrawColor(Background, 255, 255, 255, 255);
SDL_RenderClear(Background);
SDL_Delay(3000);
//Clean up
SDL_DestroyWindow(MainWindow);
SDL_Quit();
return 0;
}
for the first part of your question see this SO question.
as to why your code doesnt do much:
you are correct that you need to use either SDL_RenderDrawRect(), or SDL_RenderFillRect().
SDL_RenderDrawRect will draw an unfilled rectangle. SDL_RenderFillRect will be filled (hopefully that is obvious).
With SDL_renderer you need to call SDL_RenderPresent to copy the "scene" to the screen.
...
//Renderer
SDL_Renderer* renderer = SDL_CreateRenderer(MainWindow, -1, 0);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer); // fill the scene with white
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); // the rect color (solid red)
SDL_Rect rect(0, 0, 100, 50); // the rectangle
SDL_RenderFillRect(renderer, &rect);
SDL_RenderPresent(renderer); // copy to screen
SDL_Delay(3000);
...