MVC Model – Can It Know Anything About the View?

game developmentmvc

I'm working on a game, and without getting into any details I am using MVC "patterns", "rules" or whatever you want to call it to make the game.

The view includes everything needed to draw things on the screen, the controller passes input to the model. And the Model contains game logic.

Here's my problem, the game is being made for mobile devices that vary in screen sizes. I feel my model needs to know the view size so it can appropriately adjust for it.

I've thought about it for a while I could put all the adjustments in the view, but it just seems inefficient to translate the model positioning to the view's needed positioning every time. If the model knows the size it only needed to adjust itself once.

So my question is can I pass the view size to the model without 'breaking' MVC? I feel personally they are still decoupled this way because a size is just a number, I could still change the view any time as long as it has a size. But I wanted to get a community response on this because I haven't seen many discussions of MVC being used in a game.

(And to be clear I don't want an answer of why I shouldn't use it in a game, but do I break MVC by letting it know the view's size)

EDIT – More details on the game's needs and why I wanted to pass the view.

Some objects positions need to be set relative to the edge of the screen (such as UI elements) so that they appear relatively in the same place. Sprite sizes are not stretched if the window size is wider such as an IPhone 5 screen. They will just be placed relatively to the screen edge. .If I gave it to the view to handle this, I will need a flag saying that this element is positioned say x number of pixels from TOP BOTTOM RIGHT LEFT. Then allow the view to draw it with that information. Its acceptable, I just wanted to know if there was a better way while still being MVC because it seems this way will be repeating a logic over and over, where as if I knew the view size in the model, I could convert all the relative positions into absolute positions in one run, but with this I have to convert on every update to the screen.

Best Answer

If the model doesn't need to know the size of the view (say for edge detection or collisions), then I don't think it needs to have that information. Can your model express locations/distances in some neutral format ("layout units" or something) that will make it easy to scale for the various screen sizes? Or does the screen size affect gameplay (there may be more enemies/targets to track on a larger screen)? If so, then it makes sense to define the playfield size in the model.