0

I am currently developing an SFML application which stores a certain value in my model, which the view is displaying.

As SFML operates on a kind of render loop, which refreshes the interface constantly, I need to fetch this value which has to be displayed from the model where it is stored in every iteration to keep it up to date. It is not possible to effectively pass this value from the controller, due to the following:

My SFML loop looks like this:

while (renderWindow->isOpen())
{
    sf::Event event;
    while (renderWindow->pollEvent(event))
    {
        switch (event.type)
        {
        case sf::Event::Closed:         
            renderWindow->close();
            break;
        case sf::Event::MouseButtonPressed:
            HandleLeftClick(event);
            break;
        case sf::Event::MouseButtonReleased:
            HandleLeftClickRelease(event);
            break;
        }
    }
    m_mainWindowView->DrawWindow();
}

As you can see, I do the event handling in my controller, but I pass the whole interface redrawing to my view. I could technically pass the value I want to display with this call, but if I need more values from my controller, this might lead to a lot of parameters later on.

As far as I know, the view should not have a reference to the model. Therefore, my current solution looks like this:

When the view redraws the window, it calls a function in the controller to fetch the value to be displayed from the model. The controller then redirects this request to the model, and therefore returns the data from the model to the view over the controller.

I feel like this approach is not correct, but I can't quite figure out what other options I have with the MVC pattern. I'm not even sure if the SFML event loop allows for a pure MVC approach or if I can't get around adapting it somewhat.

I could theoretically store a pointer to the desired value in the view, but that would kind of create a dependency from the view to the model as well.

Any advice on how to do this in proper fashion?

2

There are two versions or flavours of the MVC pattern.

In the Web version of the MVC pattern, the Controller handles the requests from the outside and acts as a go-between for the Model and the View. In this flavour, the Model and the View don't know about each other.

Originally, however, the MVC pattern looked very differently.
The initial formulation of the MVC pattern stems from the era before graphical operating systems were common place and even stuff like cursor movements and jumping between input fields had to be implemented in the UI code of the application.

In this form of the MVC pattern, the Controller still receives the input and it tells the Model and/or the View what changes need to be made based on the input. The Model and the View are also connected to each other: The View has (read-only) access to the Model to retrieve whatever information it needs to show and the Model informs the View about changes in the model by means of the Observer pattern.

This second, older, form of the MVC pattern seems to fit well with a render loop like you have.

  • So it is perfectly legal to put a reference to the model into my view? That would make things a lot easier – Sossenbinder Sep 29 '17 at 16:52
  • Yes, as long as the view does not try to modify the model in any way. – Bart van Ingen Schenau Sep 29 '17 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.