简体   繁体   中英

Sfml c++ different screens with touch

i wanna make a game menu, simply three Buttons(RactangleShape) where i can switch between different Screens in sfml. I find a tutorial for it, but i am coding on a mobile phone, so i need every input control with the touch function, the tutorial is made for pc with keyboard. So i have to change it, but iam a newbie and i have problems with a place in the code that i have to change for the toch input, i cut out the following lines:

if (Event.type == sf::Event::KeyPressed)            
{               
switch 
(Event.key.code)                
{               
    case 
sf::Keyboard::Up:                   menu = 0;                   
break;          
    
case 
sf::Keyboard::Down:                     menu = 1;       
break

;

I'm not experienced with touch display implementation in sfml but inside the events you can find what you need:

接触事件

The place where you are meant to put it depends on what you want to do with the touch. for example if you want something to move consistently like a character on screen onTouch, then you cant put it in events loop as the events loop is only triggered once per event. If you want to have the button or onTouch or whatever to actually do consisntent things such as that walk you'll need to put it in just the general renderWindow loop (under the eventLoop). But because you just want it to switch screens, it can be in the eventLoop since it's a single onTrigger event. The above shows two possibilities of the Touch event- Touch begin and touch end. Same as the key events on desktop. Touch begin triggers when you touch the screen. touch end triggers when you let go. you can manipulate these two triggers to even code a working HOLD event, or a while pressed event.

I also recommend doing the screens in a class OOP fashion, where each individual unique screen has it's own class you manipulate. For ease sake each class should also have an active flag, which will act as the trigger for showing or hiding a screen.

Examples:

#include <iostream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

using namespace std;

class Screen //helperClass
{
public:
    bool active = false;
};

class MainScreen : public Screen
{
public:
    //main Screen items

    void draw(sf::RenderWindow &window)
    {
        //window.draw(items);
    }
};

class SecondScreen : public Screen
{
public:
    //second Screen items
    void draw(sf::RenderWindow &window)
    {
        //window.draw(items);
    }
};

class ThirdScreen : public Screen
{
public:
    //some Third screen items
    void draw(sf::RenderWindow &window)
    {
        //window.draw(items);
    }
};

int main()
{
    MainScreen main; //create the objects
    SecondScreen second;
    ThirdScreen third;
    main.active = true;
    sf::RenderWindow window(sf::VideoMode(1280, 720), "Title");
    window.setFramerateLimit(60);
    while (window.isOpen()) {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event)) {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
            if (event.type == sf::Event::MouseButtonPressed) cout << "hi";
            if (event.type == sf::Event::TouchBegan) //the event
            {
                cout << "opening second screen" << endl;
                second.active = true;
                main.active = false;

            }
        }

        // clear the window with black color
        window.clear(sf::Color::Black);
        // draw everything here...
        if (main.active) main.draw(window); //the cool thing about the objects is you can just use the draw function you made inside them and dont even care about adding or changing anything else here
        else if (second.active) second.draw(window);
        else if (third.active) third.draw(window);
        // end the current frame
        window.display();
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM