SFML community forums

Help => Graphics => Topic started by: Kvothe on May 14, 2015, 06:49:16 pm

Title: How to redraw sprites added to the window higher up?
Post by: Kvothe on May 14, 2015, 06:49:16 pm
Inside my main function I create a RenderWindow and a Sprite which is then applied as a background image. I then pass a pointer to the window to my MainMenu class which adds a menu to the window - however, inside the draw loop in the MainMenu class it has no knowledge of the background sprite and so it cannot redraw it.

Is there a way (other than passing all the sprites I wish to be redrawn as a vector to the MainMenu class) to let the MainMenu class know that I want all sprites currently attached to the window to be redrawn?
Title: Re: How to redraw sprites added to the window higher up?
Post by: shadowmouse on May 14, 2015, 06:53:26 pm
Can you give some context? As far as I can tell, you want to draw something once and then have it redrawn every time in your game loop. If you want to do this, then you have to have access to all the sprites you want to access at draw time. Or have lots of drawing functions that each invoke window.draw(), and put them all between window.clear() and window.display(). I'm not sure if this is what you're asking for so could you clarify please?
Title: Re: How to redraw sprites added to the window higher up?
Post by: Kvothe on May 14, 2015, 07:04:39 pm
Hopefully the code below makes it clearer - in the "isOpen()" loop inside MainMenu::Run() I want to be able to keep the background sprite in the window (i.e. know it exists and redraw it).

// INSIDE MAIN
// Create background sprite
sf::Sprite background_sprite // Plus other code to set up and attach a texture and scale it correctly...

render_window.draw(background_sprite);

// Run main menu - passing the address of the render window so main menu can draw to it
MainMenu main_menu(&render_window);
main_menu.Run();


// INSIDE MAINMENU::RUN()
// Create another sprite
sf::Sprite main_menu_sprite = ......

sf::Event event;
while(m_render_window->isOpen()){
    while(m_render_window->PollEvent(event)){
        // Code to poll events (just close window currently)
    }

    m_render_window->clear();
    m_render_window->draw(sprite);
   // Somehow know about and redraw background_sprite
    m_render_window->display();
}
 
Title: Re: How to redraw sprites added to the window higher up?
Post by: shadowmouse on May 14, 2015, 07:09:47 pm
No, as I said, you have to draw everything between the window.clear() and window.display(). If you implement a draw function in your classes where you pass it the render window, then draw all of the class' sprites (just window.draw(), no window.clear() or window.display()). Then your draw calls could just look like:
Code: [Select]
m_render_window->clear();
m_render_window->draw(background_sprite);
m_render_window->draw(sprite);
main_menu.draw(m_render_window);
m_render_window->display();
Title: Re: How to redraw sprites added to the window higher up?
Post by: Kvothe on May 14, 2015, 07:14:46 pm
Ok, thanks. Also, just for future reference, how do you display the code like in your post? (it's so much more readable than in mine)
Title: Re: How to redraw sprites added to the window higher up?
Post by: shadowmouse on May 14, 2015, 07:19:03 pm
just use "code" and "/code" tags in square brackets without the =cpp. I actually prefer the =cpp but I only just worked out how to do it because I thought it was =c++ and it didn't work.