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

Author Topic: sf::Event problems  (Read 2549 times)

0 Members and 1 Guest are viewing this topic.

Beta_Ravener

  • Jr. Member
  • **
  • Posts: 51
    • ICQ Messenger - 271426715
    • View Profile
sf::Event problems
« on: October 23, 2010, 01:45:30 pm »
I got strange problems with sf::Event. I'm creating 2 widnows, one for openGL drawings, second is some kind of settings for first one, so you can set parameters to that openGL.

I'm using this function to call updating of second window for main() function, which handles updating the primary one:

Code: [Select]
void Settings::update(){
Win.SetActive();
const sf::Input& Input = Win.GetInput();
std::ostringstream oss;
// Process events
    sf::Event Event;
while (Win.GetEvent(Event)){
if(Event.MouseMoved){
active = Input.GetMouseY() / cellHeight;
}
if ((Event.Key.Code==sf::Key::Return||Event.Key.Code==sf::Key::Right) && !input){
oss << active;
ID += oss.str();
updateText();
}
if (Event.Key.Code==sf::Key::Space||Event.Key.Code==sf::Key::Left||
Event.MouseButton.Button == sf::Mouse::Right){
if(ID.size() > 0)
ID.erase(ID.size()-1);
updateText();
}
if (Event.Key.Code==sf::Key::Up){
active--;
if(active < 0)
active = cellsH-1;
Win.SetCursorPosition(cellWidth/2,active*cellHeight+cellHeight/2);
}
if (Event.Key.Code==sf::Key::Down){
active++;
if(active >= cellsH)
active = 0;
Win.SetCursorPosition(cellWidth/2,active*cellHeight+cellHeight/2);
}
if(Event.MouseButton.Button == sf::Mouse::Left && !input){
active = Event.MouseButton.Y / cellHeight;
oss << active;
ID += oss.str();
updateText();
}
}
}


There's quite much of variables not defined in text because I am using class for second window, however the names should be intuitive. Cells are separated choices to choose in the menu which is specified by ID, and adding active to this ID you go into that choice or back from it.

What's wrong about that is, that when I simply move mouse over window for some time, setting just active(activated cell gets green so just some kind of indicator where are you pointing at), using Event.MouseMoved.
Pressing nothing, moving just mouse it suddenly generates Event.Key.Code=Space etc. jumping into other choices, and as you can see, invalidating my ID because I was just moving mouse, but ID down in menu.

I'll include window creation also:

Code: [Select]
void Settings::init(){

input = false;
ScreenX = 500;
ScreenY = 600;
sideMargin = 40;

Win.Create(sf::VideoMode(ScreenX, ScreenY, 32), "Settings",sf::Style::Titlebar);
Win.PreserveOpenGLStates(true);
Win.GetDefaultView().SetFromRect(sf::FloatRect(0, 0,(float)ScreenX, (float)ScreenY));
Win.SetActive();
Win.EnableKeyRepeat(false);

fontHeight = ScreenY/4;
std::string fontName = "ARIALN.TTF";
if(!font.LoadFromFile(fontName,fontHeight))
std::cout << "Couldn't load font " << fontName << std::endl;

ID += "0";
prepText = new std::wstring[10];
updateText();

resizeWindow();
}

void Settings::resizeWindow(){
Win.SetSize(ScreenX,ScreenY);
Win.GetDefaultView().SetFromRect(sf::FloatRect(0, 0,(float)ScreenX, (float)ScreenY));

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,ScreenX,ScreenY,0);
glGetFloatv (GL_PROJECTION_MATRIX, proj.elements);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( 0, 0, 1, 0, 0, 0, 0, 1, 0);
glGetFloatv (GL_MODELVIEW_MATRIX, model.elements);
}


calling from main looks like this:
Code: [Select]

Settings uInf;
uInf.init();
while (App.IsOpened())
{
uInf.update();
uInf.render();
}


If you need anything else like screenshot I'll add them, but I think it's just a mistake somewhere. However I searched for it for hours now and I am getting exahusted.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Event problems
« Reply #1 on: October 23, 2010, 02:08:30 pm »
You must first check the type of event, you can't use (for instance) Event.Key.Code without knowing first that it's a key pressed event.

Have a look at the tutorial, it is well explained with source code.
Laurent Gomila - SFML developer

Beta_Ravener

  • Jr. Member
  • **
  • Posts: 51
    • ICQ Messenger - 271426715
    • View Profile
sf::Event problems
« Reply #2 on: October 23, 2010, 02:22:38 pm »
Oh I'm sorry I missed that one :/ Thanks for fast reply, but I have still question why is that space or other key even generated if nothing happens?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Event problems
« Reply #3 on: October 23, 2010, 02:27:27 pm »
It's the result of your wrong code, fix it and everything will be ok.
Laurent Gomila - SFML developer

 

anything