Multiple windows and image

Hi!
I want to make multiple windows and render an image on one i do the tutorial of lazyfoo for multiple windows but i don’t know how to render an image on a specific render i have this:
class LWindow
{
public:
//Intializes internals
LWindow();

//Creates window
bool init();

//Handles window events
void handleEvent( SDL_Event& e );

//Focuses on window
void focus();

//Shows windows contents
void render();

//Deallocates internals
void free();

//Window dimensions
int getWidth();
int getHeight();

//Window focii
bool hasMouseFocus();
bool hasKeyboardFocus();
bool isMinimized();
bool isShown();

private:
//Window data
SDL_Window* mWindow;
SDL_Renderer* mRenderer2;
int mWindowID;

//Window dimensions
int mWidth;
int mHeight;

//Window focus
bool mMouseFocus;
bool mKeyboardFocus;
bool mFullScreen;
bool mMinimized;
bool mShown;

};

To render an image you have to load it first into an SDL_Texture. This is explained in lesson 7 of the Lazy Foo tutorials. I’m guessing you already went over that.

An SDL_Texture belongs to a specific SDL_Renderer and that renderer belongs to a specific SDL_Window. These dependencies require you to come up with some system to manage them. As stated at the end of the Multiple Window lesson, there are many ways to solve this and it can be very simple or very complex depending on your application.

A very simple example: Let’s say we just want to display one unique image per window. The main problem we’re facing is to detect which window is initializing so that the correct image gets loaded. One approach to solve this is to rely on the initialization order to select the images. Using Lazy Foo’s 36_multiple_windows.cpp as a starting point, let’s just pass that number into the LWindow::init function and add some code which will react to it.

  • Change the declaration and definition of LWindow::init to take an int argument.
  • Add a SDL_Texture member to the LWindow class.
  • In the LWindow::LWindow constructor, set the new SDL_Texture member to NULL.
  • In the LWindow::init function , after the renderer was correctly created, add some code that loads your image to the SDL_Texture member. You can use the new argument that gets passed in to create a filename for example:
char filename[256];
SDL_snprintf(filename, sizeof(filename), "%d.bmp", id); /* id is the new argument */
SDL_Surface * imgsurface = SDL_LoadBMP(filename);
if (imgsurface != NULL) {
	mTexture = SDL_CreateTextureFromSurface(mRenderer, imgsurface); /* mTexture is the new class member */
	SDL_FreeSurface(imgsurface);
}
  • In the LWindow::render() function, you can now draw the SDL_Texture if it isn’t NULL. Add a simple SDL_RenderCopy between SDL_RenderClear and SDL_RenderPresent:
    ``

    if (mTexture != NULL)
    SDL_RenderCopy(mRenderer, mTexture, NULL, NULL);

  • The functions ::init and ::main need to pass the correct number to LWindow::init. ::init always creates the first window, so that can just pass 0. ::main needs to pass the variable i.

  • Proper cleanup with SDL_DestroyTexture would be preferable but is not necessary in this simple example.

Now you should be able to create BMP images with the names 0.bmp, 1.bmp, and 2.bmp which should show up in their respective window.

1 Like

Thanks i do that and its works fine ! :grin: