Home > Blockchain >  implement forms in c sfml
implement forms in c sfml

Time:07-14

how should I implement forms and switch between them in SFML?

I have a custom abstract Form class for displaying window and other stuff

class Form {
public:
    Form();

    ~Form();

    void display();

protected:
    sf::RenderWindow *window;
    sf::Event event;

    bool isRunning() const;

    virtual void pollEvents() = 0;

    virtual void update() = 0;

    virtual void render() = 0;

    void initWindow();
};

and I have 3 classes for 3 menus:

  • MainForm
  • OptionsForm
  • GameForm

each inherited class Form:

class OptionsForm : public Form {
public:
    OptionsForm(Form *context);

    ~OptionsForm();

private:
    Form *context;
};

I use Form* context to save the form before this form and when i want to return i dont loose it's data

how I switch between forms:

window->close(); //pointing to the window in the current class
context->display();

but it closes the form completely

I also cant create an instance of the current form because I don't want to lose the current form's class state and data

CodePudding user response:

You need to share the window between all forms and close it ONLY when you want to exit the application.

In your example you're closing the window before displaying anything. If you close the window there's nothing to display to.

Look at the official documentation and note the flow of the example application - https://www.sfml-dev.org/tutorials/2.5/graphics-draw.php#the-drawing-window

  • Related