Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Resolved] User interface elements overlayed on window  (Read 3804 times)

0 Members and 1 Guest are viewing this topic.

white_nitro0

  • Newbie
  • *
  • Posts: 6
    • View Profile
[Resolved] User interface elements overlayed on window
« on: February 26, 2014, 11:56:53 am »
Hi,

I have recently started using SFML (I am actually using JSFML, but this question is generally applicable to all bindings) to build a top down shooter type game.

I am interested in building a user interface that displays things like a HUD, an inventory etc, but am not really sure how to separate these from interactive, collide-able game objects (e.g. if I draw a rectangle in the bottom right corner, what is to stop characters walking over it as if it were part of the map).

It seems like using views might be the way to go, but would this mean I have to draw my HUD somewhere miles away from the game area, then make a "Hud Viewport" look at it and display it on top of my game area?

Any examples or code snippets to get me thinking/started would be much appreciated!
« Last Edit: February 26, 2014, 12:25:46 pm by white_nitro0 »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: User interface elements overlayed on window
« Reply #1 on: February 26, 2014, 12:00:40 pm »
In every frame, you should first render your game world with its current view, then switch to the default view and render the GUI. That way, the user interface will always be on top and you don't have to move buttons as the game world scrolls.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

white_nitro0

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: User interface elements overlayed on window
« Reply #2 on: February 26, 2014, 12:17:11 pm »
So something along the lines of (pseudocode):

window.setView(viewCenteredOnPlayer);

//do all of the game processing here

window.draw(allGameElements);

gui.setPosition(//top left etc);

window.setView(defaultView);

window.draw(gui);
 

Thanks for the fast response, I'll have a go at implementing that and let you know how it goes.

white_nitro0

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: User interface elements overlayed on window
« Reply #3 on: February 26, 2014, 12:22:33 pm »
Thanks, that worked a treat!

The only line that I missed was:

window.setView(viewCenteredOnPlayer);

at the very end so that my coordinate conversions etc were calculated as they were before.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: [Resolved] User interface elements overlayed on window
« Reply #4 on: February 26, 2014, 12:30:14 pm »
If the position of your GUI elements doesn't change, it's enough to set it once, before the game loop.

And you don't need to add window.setView(viewCenteredOnPlayer); at the end, if you do it at the beginning of the game loop (before rendering the world). I would separate world and GUI rendering to keep code more readable.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

white_nitro0

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: [Resolved] User interface elements overlayed on window
« Reply #5 on: February 26, 2014, 12:44:55 pm »
The GUI will update as the game is played, so surely I need to draw the GUI once per frame?

At the minute I have a "Game" class, which has a draw method that is called in the main game loop.

draw(RenderWindow window){

     currentScene.draw(window);

     window.setView(defaultView);
     uiManager.draw(window);

     window.setView(mainView);

}

The currentScene then handles rendering of all of its objects, and the uiManager handles drawing the GUI. The window is passed down because a draw() call is made on each of the objects in turn by the currentScene and uiManager. Does this seem like a sensible approach (before I delve further and further into messy code :))

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: [Resolved] User interface elements overlayed on window
« Reply #6 on: February 26, 2014, 01:11:49 pm »
Yep, that sounds reasonable. What I meant is that your code would be a bit more intuitive if you set the view before rendering (and it will also be correct for the first frame):
window.setView(mainView);
currentScene.draw(window);

window.setView(defaultView);
uiManager.draw(window);
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything