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

Author Topic: Interface trouble  (Read 1243 times)

0 Members and 1 Guest are viewing this topic.

Introhart

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Interface trouble
« on: October 21, 2019, 07:55:58 pm »
Ok, so... I have a small trouble with drawing of UI.
I have a map, a character etc... everything is working good. But I can't to draw anyting relative to the coordinates of the window. I need it to draw an interface.
( shortly... how to draw a rectangle it always would be located in the top left corner of the window?)



my main file

#include <SFML/Graphics.hpp>
#include "GraphicsLib.cpp"
#include "character.cpp"
#include <iostream>



int main()
{
        sf::RenderWindow window(sf::VideoMode(720, 480), "Tilemap");

        Hero hero;
        sf::View view;

        view.reset(sf::FloatRect(0, 0, 720, 480));
        TileMap map;

        map.load("testMap.txt");
        hero.setWayMap(map.wayMap, map.height, map.width);


        sf::RectangleShape r(sf::Vector2f(120.f, 50.f));
        r.setPosition(sf::Vector2f(10, 0));

       
       
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
               
                window.clear();


                window.draw(hero.sprite);
                window.draw(map);
                window.setView(view);
                window.draw(hero.sprite);
                view.setCenter(hero.sprite.getPosition().x + hero.img.getSize().x / 2, hero.sprite.getPosition().y + hero.img.getSize().y / 2);
                hero.move();


                window.draw(r);
                window.display();
        }

        return 0;
}
 
« Last Edit: October 21, 2019, 07:58:17 pm by Introhart »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Interface trouble
« Reply #1 on: October 21, 2019, 10:56:33 pm »
The top-left co-ordinate of the client section of a window is the view centre minus half of the view size.
e.g.
sf::Vector2f topLeftCoord{ view.getCenter() - view.getSize() / 2.f };

However, there is another approach which makes life a lot easier: use two views instead of one.

Use your main game "scene" view to centre on the player and move around the world (for example).
Then, use a top layer "UI" view to draw the user interface.
This "UI view" would be set to the size required (usually the same size as the client window) and would also never move.
The "scene view" would be moved around as you do already.

Then, you just switch views before drawing (and also before doing anything that takes the view into account such as converting co-ordinates like the mouse position).
e.g.
window.clear();
window.setView(sceneView);
window.draw(map);
window.draw(hero.sprite);
window.setView(uiView);
window.draw(r); // your test rectangle
window.draw(ui); // the rest of the ui you want to draw can be drawn here
window.display();

You can set up the ui view before the main loop and never need to change it (in almost all cases).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything