# Render two images to an SDL_window

I want to create a menu for an SDL game, so I load an Image to an SDL_Window as the background, then I try to load the next image which will be a button but it doesn't display the button to the window, it just displays the background.

I'm a newbie so perhaps it is an obvious mistake.

Here's what I got:

#include <stdio.h>
#define SDL_MAIN_HANDLED
#include "SDL.h"

int main(int argc, char *argv[])
{
int a = 1;
SDL_Event event;

SDL_Window* window;
SDL_Surface* background_surface;
SDL_Texture* background_texture;
SDL_Renderer* background_renderer;
SDL_Surface* button_surface;
SDL_Texture* button_texture;
SDL_Renderer* button_renderer;

SDL_Rect button_pos;
button_pos.h = 65;
button_pos.w = 320;
button_pos.x = 0;
button_pos.y = 0;

SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow(
"Cosmic Racing",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
320,
568,
SDL_WINDOW_OPENGL
);
background_renderer = SDL_CreateRenderer(window, -1, 0);
background_texture = SDL_CreateTextureFromSurface(background_renderer, background_surface);
SDL_RenderCopy(background_renderer, background_texture, NULL, NULL);
SDL_RenderPresent(background_renderer);

button_renderer = SDL_CreateRenderer(window, -1, 0);
button_texture = SDL_CreateTextureFromSurface(button_renderer, button_surface);
SDL_RenderCopy(button_renderer, button_texture, &button_pos, NULL);
SDL_RenderPresent(button_renderer);

while(a)
{
SDL_PollEvent(&event);
if(event.type == SDL_QUIT)
{
a = 0;
}
}

SDL_DestroyTexture(button_texture);
SDL_FreeSurface(button_surface);
SDL_DestroyRenderer(button_renderer);
SDL_DestroyTexture(background_texture);
SDL_FreeSurface(background_surface);
SDL_DestroyRenderer(background_renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}


BTW if you find additional mistakes please tell me. Thank you ;)

• Also, if you didn't know there is a library called SDL_image which supports loading of pngs, jpegs, etc.. – saloomi2012 Mar 16 '14 at 4:37
• Oh thanks I was going to look up for that, since bmps don't support transparency. I'll search for it and what happens. – user3195897 Mar 16 '14 at 4:40
• This tutorial is C++ based, but just look on how he uses the SDL_image function calls and implement them on your own. I am sure you are capable of that – saloomi2012 Mar 16 '14 at 4:48
• Well I'll try to, it's just that I really tried to learn C++ first but just couldn't understand OOP, so I tried C and it was much easier, but I think I'm starting to get it. – user3195897 Mar 16 '14 at 4:53
• oh ignore all the oop stuff, you'll never need it if you can survive in C. Use a C++ compiler (also compiles C) and code C and when you find something useful that is in c++, incorporate it! I never used any OOP in c++ yet, i just use struct's which are also in C. Please, c++ will save you from learning C-Style strings. Here is an example of creating a string in c++ (first add this #include <string>) std::string string = "Hello World!" – saloomi2012 Mar 16 '14 at 4:56

You should use ONE renderer for your rendering needs. Please don't use two.

Don't use the OpenGL flag if you are not going to use it!

For the SINGLE renderer, I am assuming you want gpu rendering so replace the 0 in the last argument in SDL_CreateRenderer() and replace it with SDL_RENDERER_ACCELERATED.

Here is the fixed source code, Used some random images to test it and works fine, also please dont #include <stdio.h> when you are never using it in this code. Please don't leave those pointers unassigned! Please always NULL them when you initialize them to avoid issues and future bugs. Also place the SDL_RenderCopy() & SDL_RenderPresent() in the very end of your while loop to avoid future issues I am predicting you will have. Also added SDL_RenderClear() just to make the code more future proof (lol). Also in SDL_RenderCopy(renderer, button_texture, &button_pos, NULL) you did it wrong, it should be SDL_RenderCopy(renderer, button_texture, NULL, &button_pos);

#define SDL_MAIN_HANDLED
#include "SDL.h"

int main(int argc, char *argv[])
{
int a = 1;
SDL_Event event;

SDL_Window* window = NULL;
SDL_Surface* background_surface = NULL;
SDL_Texture* background_texture = NULL;
SDL_Renderer* renderer = NULL;
SDL_Surface* button_surface = NULL;
SDL_Texture* button_texture = NULL;

SDL_Rect button_pos;
button_pos.w = 320;
button_pos.h = 65;
button_pos.x = 0;
button_pos.y = 0;

SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow(
"Cosmic Racing",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
320,
568,
SDL_WINDOW_SHOWN
);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
background_texture = SDL_CreateTextureFromSurface(renderer, background_surface);

button_texture = SDL_CreateTextureFromSurface(renderer, button_surface);

while(a)
{
SDL_PollEvent(&event);
if(event.type == SDL_QUIT)
{
a = 0;
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, background_texture, NULL, NULL);
SDL_RenderCopy(renderer, button_texture, NULL, &button_pos);
SDL_RenderPresent(renderer);
}

SDL_DestroyTexture(button_texture);
SDL_FreeSurface(button_surface);
SDL_DestroyRenderer(renderer);
SDL_DestroyTexture(background_texture);
SDL_FreeSurface(background_surface);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}