How to RenderCopy a surface into the window

I am having issues displaying an RGB surface that i created using the CreatRGBsurface function onto the window. I am converting the surface into a texture, then i am copying and presenting it.
However the only thing that comes up is the draw color that i set.

Code:

SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface* surf = NULL;   //Create Surface pointer
SDL_Texture* tex = NULL;    //Create Texture pointer

//create destination rectangle
SDL_Rect destr;
destr.x = 800/2;
destr.y = 600/2;

surf = SDL_CreateRGBSurface(0, 100, 100, 32, 0, 255, 0, 255); //Create RGB surface for texture

//Check if create surface failed
if(surf == NULL){
    cout << SDL_GetError() << endl;
    exit(-1);
}


SDL_Window* window; //Create window pointer

//Create window
window = SDL_CreateWindow("MyWindowTitle", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                          800, 600, 0);

//Create renderer pointer
SDL_Renderer* renderer;

//Create renderer to window
renderer = SDL_CreateRenderer(window, -1, 0);

//Convert the surface to a texture
tex = SDL_CreateTextureFromSurface(renderer, surf);

SDL_FreeSurface(surf);  //Free surface

//check if texture was created
if(tex == NULL){
    cout << SDL_GetError() << endl;
    exit(-1);
}

//Render copy onto the renderer using painters algorithm
SDL_SetRenderDrawColor(renderer, 50, 0, 255, 255);
SDL_RenderCopy(renderer, tex, NULL, &destr);

SDL_RenderClear(renderer);  //clear renderer for drawing
SDL_RenderPresent(renderer);    //present renderer



SDL_Delay(3000);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();

return 0;

Hello and welcome to the forum!

Normally you would first clear the current render target by calling the SDL_RenderClear() function, then render all the textures, primitives etc to the current render target and then finally update the window by calling the SDL_RenderPresent() function.

So try moving up your SDL_RenderClear(renderer) call above your SDL_RenderCopy(renderer, tex, NULL, &destr) call.

Yeah, i tried that, but it did not work.

Check the return value of SDL_RenderCopy() call, to make sure there’s no problem there.

Also, to make sure there’s no problem with the surface creation, load an image from disk and create an SDL_Surface and SDL_Texture from that and try to render it.

After learning how to import an image using SDL_image.h the photo is not displayed. i also rearranged the SDL_RenderClear function. No error has occured.

This is my updated code:

if(SDL_Init(SDL_INIT_EVERYTHING) < 0){
    cout << SDL_GetError() << endl;
    exit(-1);
}

SDL_Surface* surf = NULL;   //Create Surface pointer
SDL_Texture* tex = NULL;    //Create Texture pointer

//create destination rectangle
SDL_Rect destr;
destr.x = 800/2;
destr.y = 600/2;

//surf = SDL_CreateRGBSurface(0, 100, 100, 32, 0, 255, 0, 255); //Create RGB surface for texture
surf = IMG_Load("Test_photo.png");

//Check if create surface failed
if(surf == NULL){
    cout << SDL_GetError() << endl;
    exit(-1);
}


SDL_Window* window; //Create window pointer

//Create window
window = SDL_CreateWindow("MyWindowTitle", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                          800, 600, 0);

//Create renderer pointer
SDL_Renderer* renderer;

//Create renderer to window
renderer = SDL_CreateRenderer(window, -1, 0);

//Convert the surface to a texture
tex = SDL_CreateTextureFromSurface(renderer, surf);

SDL_FreeSurface(surf);  //Free surface

//check if texture was created
if(tex == NULL){
    cout << SDL_GetError() << endl;
    exit(-1);
}

//Render copy onto the renderer using painters algorithm
SDL_SetRenderDrawColor(renderer, 50, 0, 255, 255);
SDL_RenderClear(renderer);  //clear renderer for drawing

if((SDL_RenderCopy(renderer, tex, NULL, &destr)) < 0){
    cout << SDL_GetError() << endl;
   exit(-1);
}



SDL_RenderPresent(renderer);    //present renderer


SDL_Delay(3000);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();

I found out what was wrong. I was putting the square in an idiotic place on the screen. Thanks for the help!