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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Abc

Pages: [1]
1
General / How to realize a state machine based SFML?
« on: April 20, 2024, 01:38:41 pm »
Hello everyone.
I am trying to realize a simple game engine based on SFML. It has a state machine, that represents the LIFO queue. My base state class looks something like this:

class AbstractScene
{
    public:
    virtual handleEvents(const sf::Event& event) = 0;

    virtual updateLogic(const sf::Time& time) = 0;

    virtual draw() = 0;
};

Methods of the state machine are cached to be called simultaneously when the state machine has finished handle events, update logic and draw all states.

I pushed the Level state. I set pause and pushed Menu state by pressing a mouse button. It works, but I can get a few identical events (sf::Event::MouseButtonPressed) at the one frame loop and I have the three same states (but I should have the only one):

class Level : public AbstractScene
{
    public:
    void handleEvents(const sf::Event& event) override
    {
         if(event.type == sf::MouseButtonPressed)
         {
              // append new scene
              stateMachine.push(new Menu());
         }
       
         ...
    }
};

I can add a boolean flag that will collect the general value of this event and I will handle it at the "updateLogic" method. However, my state machine blocks this method when it paused.

So how and where should I change the states and how should I realize the game pause?

PS: sorry for my bad Engish. Thank you very much!

2
General / How to share assets between the scenes?
« on: March 19, 2024, 09:28:28 pm »
Hello,
I am trying to create my own game engine based on SFML. I created the scenes, that has the local assets. But I don't understand how to share assets between two scenes, that could replace each other.

Approximate code:

class Level : public AbstractScene
{
public:
    Engine& engine;
    std::unordered_map<sf::Image> images

   Level()
   {
        images["img1"].loadFromFile("img1.png"); // load image
   }

    void update()
    {
         sf::Texture texture(assets.images["img1"]); // use image
         ...
         engine.setScene(new MiniLevel());
    }
};

class MiniLevel: public AbstractScene
{
public:
    Engine& engine;
    std::unordered_map<sf::Image> images;
    LevelImages& levelImages; // Level image

    MiniLevel(LevelImages& img) : // not real code
         levelImages(img)
    {
    }

    void update()
    {
         sf::Texture texture(levelImages["img1"]); // use Level image
         ...
         engine.setScene(new MiniLevel());
    }
};
 

I know I can use smart pointers to copy images from the Level to MiniLevel, but how to copy it to the Level again? Level class should load images when first created and than only copy it from the MiniLevel. Maybe should I use another class "Level" with the general assets for the scenes?

PS: sorry for my bad English.

Pages: [1]