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

Author Topic: Strange window resize behaviour  (Read 2336 times)

0 Members and 1 Guest are viewing this topic.

unranked86

  • Newbie
  • *
  • Posts: 37
    • View Profile
Strange window resize behaviour
« on: August 07, 2011, 08:52:10 pm »
Hi there!

I created a nice main menu class last week, and today I expanded it with mouse events. It works nicely. But only in my test program. If I try to
use it in my game, it works, but sf::Text.SetPosition() doesn't set the position how I want it. Interestingly the GetRect() returns the correct
position.

I try to show how I use it.

test program
Code: [Select]

// include things

MenuSystem menu;

void init ()
{
// get font file from disk
menu.SetFont(font);
menu.SetPos(50, 100);
}

int main ()
{
sf::RenderWindow App(sf::VideoMode(800, 600m 32), "Menu test 2", sf::Style::Close);

init();

menu.AddEntry("New", NEW);
menu.AddEntry("Load Game", LOAD);
// etc...

while (App.IsOpened())
{
MenuLabel choice = NONE;
// process sf::Event::Closed here

choice = menu.OnEvent(App);

menu.UpdateEntries(); // this just sets the color of the text

App.Clear(sf::Color::Black);

menu.DrawEntries(App); // draw them

App.Display();
}

return 0;
}

This works perfectly like I said before.

And my game...
Code: [Select]

void MenuState::Init ()
{
// load font
mMenu.SetPos(50, 100);
mMenu.SetFont(font);

mMenu.AddEntry("New Game", NEW);
// .... like above
}

State MenuState::Mainloop (sf::RenderWindow& win)
{
while (mRunning)
{
mRunning = EventHandler(win);

Update();

Render(win);
}

return mNextState;
}

bool MenuState::EventHandler (sf::RenderWindow& win)
{
switch(mMenu.OnEvent(win))
{
// ... not relevant
// but if the player selects a menu it returns false
}

return true;  // if no menu was clicked
}

void MenuState::Update ()
{
mMenu.UpdateEntries();
}

void MenuState::Render (sf::RenderWindow& win)
{
// clear screen and draw stuff, not relevant
}


Basically, I use it in the same way in both cases, but the outcome is different. In my test program the text is in the right place, in my game it is not. Even if I tell it the exact position, the text is rendered somewhere else, but GetRect() gives back the right position.

EDIT:
Okay, I just tested it with sf::Style::Fullscreen, and it works like I intended.

EDIT2:
:) I think I figured out what the problem is. Apparently if I set sf::Style::Resize, then position is not correct. So, the problem is with my system, to be exact how it handles the windows. (I use XFCE)
Sorry about this silly topic  :oops:

unranked86

  • Newbie
  • *
  • Posts: 37
    • View Profile
Strange window resize behaviour
« Reply #1 on: August 07, 2011, 10:36:15 pm »
Okay. Maybe I am really doing something wrong.

Here is a little example. It's stupid and messy, but compiles and runs.

Code: [Select]

#include <SFML/Graphics.hpp>

int main ()
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Selecting with mouse - test", sf::Style::Close | sf::Style::Resize);

sf::Text text;
text.SetPosition(50, 50);
text.SetColor(sf::Color::White);
text.SetString("Selected color:");

sf::Text text2 = text;
text2.SetPosition(600, 50);
text2.SetString("Avaliable colors:");

sf::Text text3 = text;
text3.SetPosition(50, 350);
text3.SetString("Test area");

sf::FloatRect rect1(650, 100, 50, 50);
sf::Shape shape1 = sf::Shape::Rectangle(rect1, sf::Color::Red);

sf::FloatRect rect2(650, 150, 50, 50);
sf::Shape shape2 = sf::Shape::Rectangle(rect2, sf::Color::Green);

sf::FloatRect sel(50, 100, 50, 50);
sf::Shape selected = sf::Shape::Rectangle(sel, sf::Color::White);

sf::Color selectedColor = sf::Color::White;

sf::FloatRect test1(50, 400, 50, 50);
sf::FloatRect test2(50, 450, 50, 50);

sf::Shape s1 = sf::Shape::Rectangle(test1, selectedColor);
sf::Shape s2 = sf::Shape::Rectangle(test2, selectedColor);


while (App.IsOpened())
{
sf::Event event;

while (App.PollEvent(event))
{
if (event.Type == sf::Event::Closed)
App.Close();
if (event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Keyboard::Escape)
App.Close();
}

if (sf::Mouse::IsButtonPressed(sf::Mouse::Left))
{

if (rect1.Contains((sf::Vector2f)sf::Mouse::GetPosition(App)))
{
selectedColor = sf::Color::Red;
}
else if (rect2.Contains((sf::Vector2f)sf::Mouse::GetPosition(App)))
{
selectedColor = sf::Color::Green;
}

if (test1.Contains((sf::Vector2f)sf::Mouse::GetPosition(App)))
{
s1.SetColor(selectedColor);
}
if (test2.Contains((sf::Vector2f)sf::Mouse::GetPosition(App)))
{
s2.SetColor(selectedColor);
}
}

selected.SetColor(selectedColor);

App.Clear(sf::Color::Black);

App.Draw(text);
App.Draw(text2);
App.Draw(text3);
App.Draw(selected);
App.Draw(shape1);
App.Draw(shape2);
App.Draw(s1);
App.Draw(s2);

App.Display();
}

return 0;
}


If I resize the window, everything is scaled accordingly, and I can't select any of the colors anymore. Is this how it should be ? Or is it a bug ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Strange window resize behaviour
« Reply #2 on: August 08, 2011, 08:01:47 am »
When the window is resized, mouse coordinates no longer matches scene coordinates (because the view is zoomed). You must use RenderWindow::ConvertCoords to convert the mouse position accordingly.
Laurent Gomila - SFML developer

unranked86

  • Newbie
  • *
  • Posts: 37
    • View Profile
Strange window resize behaviour
« Reply #3 on: August 08, 2011, 05:47:40 pm »
Quote from: "Laurent"
When the window is resized, mouse coordinates no longer matches scene coordinates (because the view is zoomed). You must use RenderWindow::ConvertCoords to convert the mouse position accordingly.

Thanks a lot, it does the trick :) I promise, next time I will read the documentation properly!
And I was wondering that it has to do something with the default sf::View or maybe I have to catch resize event in the event loop. But no :) As expected from SFML, a simple function call and everything is solved.
Thanks, again.