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

Author Topic: [SOLVED] Help with a switch statement  (Read 7761 times)

0 Members and 1 Guest are viewing this topic.

lockandstrike

  • Newbie
  • *
  • Posts: 32
    • View Profile
[SOLVED] Help with a switch statement
« on: August 06, 2013, 05:06:49 pm »
So here is the thing. I'm starting to learn SFML and although i know C++ there are somethings that i still don't possess total knowledge of. So here the part of my code where I need help:

...
switch (event.type || event.key.code ){

case sf::Keyboard::Escape:
case sf::Event::Closed:

Window.close();
break;
}
...
 

But when i run the program with this code it doesn't allow me to close it neither with the close button neither with the escape key.
Any help would be appreciated.
« Last Edit: August 06, 2013, 08:14:02 pm by lockandstrike »

Semicolon

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Help with a switch statement
« Reply #1 on: August 06, 2013, 05:33:17 pm »
I have no idea if you can use || inside a switch like that...

Here is a snippet from my main-loop. Note that I wrote the class "Input" myself but you could always use sf::Keyboard::isKeyPressed() or something else.

    while(window.isOpen())
        {
                Input::Update();

                sf::Event event;

                while(window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        case sf::Event::KeyPressed:
                                if(hasFocus)
                                        if(Input::KeyDown(sf::Keyboard::Escape))
                                                window.close();
                                break;
                        case sf::Event::LostFocus:
                                hasFocus = GL_FALSE;
                                break;
                        case sf::Event::GainedFocus:
                                hasFocus = GL_TRUE;
                                break;
                        case sf::Event::TextEntered:
                                if(event.text.unicode <= 255)
                                        console.AppendText(static_cast<GLubyte>(event.text.unicode));
                                break;
                        default:
                                break;
                        }
                }
« Last Edit: August 06, 2013, 05:36:04 pm by Semicolon »

lockandstrike

  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: Help with a switch statement
« Reply #2 on: August 06, 2013, 05:52:31 pm »
Ok than tell if this would work as well:

...
switch(event.type){
case sf::Event::Closed:

Window.close();
break;

case sf::Event::KeyPressed:
if(event == sf::Keyboard::Escape){
Window.close();
}
break;
}

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Help with a switch statement
« Reply #3 on: August 06, 2013, 05:57:10 pm »
Everything about SFML is explained in Official Tutorials.
Everything about events is explained here: http://www.sfml-dev.org/tutorials/2.1/window-events.php
Back to C++ gamedev with SFML in May 2023

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Help with a switch statement
« Reply #4 on: August 06, 2013, 05:59:01 pm »
Quote
6.4.2 The switch statement

The switch statement causes control to be transferred to one of several statements depending on the value of a condition.
The condition shall be of integral type, enumeration type, or of a class type for which a single conversion function to integral or enumeration type exists (12.3). If the condition is of class type, the condition is converted by calling that conversion function, and the result of the conversion is used in place of the original condition for the remainder of this section. Integral promotions are performed. Any statement within the switch statement can be labeled with one or more case labels as follows:

case constant-expression:

where the constant-expression shall be an integral constant-expression. The integral constant-expression (5.19) is implicitly converted to the promoted type of the switch condition. No two of the case constants in the same switch shall have the same value after conversion to the promoted type of the switch condition.
In your case, your switch condition which is of bool type (because of the boolean operation) is promoted to an integral type, probably int. As such this int would only be able to take on the values 0 or 1 corresponding to false or true. Since you are comparing your case expressions with 0 or 1, depending on their values, one of the case blocks might get executed, but it might not lead to what you might expect ;). Your code is perfectly legal C++ but semantic bogus ;). I would adopt the code posted by Semicolon or take a look at one of the many examples of SFML usage, all of which contain such a switch statement since this is something you have to do in every application you write.

Ok than tell if this would work as well:

...
switch(event.type){
case sf::Event::Closed:

Window.close();
break;

case sf::Event::KeyPressed:
if(event == sf::Keyboard::Escape){
Window.close();
}
break;
}
Just take a look at the examples... they aren't that hard to understand. You can save yourself a lot of trial and error and even the time of some people who mind replying to this thread...
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

lockandstrike

  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: Help with a switch statement
« Reply #5 on: August 06, 2013, 06:09:02 pm »
Thanks to all I've managed to solve the problem. The thread can be locked.

 

anything