MVC programming [solved]

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

MVC programming [solved]

Postby djgriff » Fri Jul 13, 2012 12:14 pm

Hi ,

I am implementing MVC as best i can into my game structure, but i am not sure of the best way to create multiple lvl's. Is it best to create a different class for each level, or create a different game state for each lvl in the ' WorldRenderer class'

Any suggestions welcome.

Thanks

Daniel
Last edited by djgriff on Mon Aug 27, 2012 4:31 pm, edited 1 time in total.
djgriff
 
Posts: 253
Joined: Tue Jan 03, 2012 8:11 am

Re: MVC programming

Postby Aare » Sat Jul 14, 2012 7:39 am

I gues there is no "general rule" - think about what will fit You most, and allow You to reuse code as much as possible while giving enough flexibility.
In my game, I`m handling multiple levels by extending custom "GameLevel" class that inherit from Screen class - it allow me to simply pass it to the rendering loop. It work for me.
Just one tip - don`t go crazy on keeping it "ultra MVC correct" - for example, as calling multiple functions on android seem to be expensive, there is no need to create two methods for calculating, and drawing as You can do it in one.
Aare
 
Posts: 69
Joined: Mon Sep 05, 2011 10:02 pm

Re: MVC programming

Postby djgriff » Sun Jul 15, 2012 10:49 am

Hi Thanks for the reply.. i will keep in mind your suggestions on keeping it lean with coding. think i will try your approach to lvling.. managed to find a few examples on you tube, but not too much in regards to best management practice. guess like you said it comes down to individual preference.

Cheers

Daniel
djgriff
 
Posts: 253
Joined: Tue Jan 03, 2012 8:11 am

Re: MVC programming

Postby felixwatts » Wed Aug 01, 2012 10:04 am

One thing that can be confusing when trying to separate model from presentation in the context of a game, is that some things that would normally be considered part of presentation in a 'normal' application are more rightly part of the model in a game.

Let's say you have a game with a tiled world. Each tile has some properties and an appearance. We have two options,

1) Do not specify the appearance of the tile in the model. Let the presentation layer choose what each tile looks like based on its properties.
2) Let each tile in the model specify its own appearance, for example by specifying a texture for itself.

Method 1 might appear to be a proper MVC approach and method 2 might look like its mixing model and presentation, but I don't think so. The key here is the difference between style and content. In a game, graphics are content, not style. Content should be part of the model, not the presentation layer.

Of course this does depend on the type of game you are making. For example in a puzzle game like tetris or chess the graphics might be considered style rather than content, since one could change the graphics entirely and the game would remain unchanged.

I have found that specifying some graphical elements, such as textures or colours, in the model has given me great flexibility and meant I can do certain things easily and neatly, however the downside if you do this is that your model will become dependent on libgdx so you wont be able to port to other platforms or frameworks by simply swapping out the presentation layer.

The key is not to get hung up on this however, just get something working as soon as possible and refactor later if neccesary!

Good luck
felixwatts
 
Posts: 75
Joined: Fri Dec 30, 2011 3:54 pm

Re: MVC programming

Postby djgriff » Wed Aug 01, 2012 2:02 pm

Cheers for the reply,

I think i am coming round to the philosophy of creating the darn thing then reflecting later.

The one point i am pondering currently is where to place the game states.

If i am placing the game states in the GameScreen (controller layer) i am attaching textures to certain states which i am guessing should be in the WorldRenderer(view) class.

Would one say it is ok to have texture(graphics)rendered from the controller layer?

I find it all kind of gets confusing to an extend and end up chasing my tail.

best

Daniel
djgriff
 
Posts: 253
Joined: Tue Jan 03, 2012 8:11 am

Re: MVC programming

Postby broken-e » Wed Aug 01, 2012 4:46 pm

I'm not very experienced with MVC but it seems to me that games have many more layers than just those 3. For one thing, there's the game world and there's the UI, which both may have their own sort of MVC structure.

My game objects have both logic and render code in them, because they are tightly linked. I can change the 'view' of them by changing the images on the png files.

There's some sparse insight from a novice diy-learning dev, whatever it's worth. :mrgreen:
broken-e
 
Posts: 667
Joined: Sun Feb 06, 2011 7:20 am

Re: MVC programming

Postby felixwatts » Wed Aug 01, 2012 6:36 pm

The one point i am pondering currently is where to place the game states.


I think the only sensible place to put any game state is in the model.

Here is how I would (and do!) organise it:

1) Model: put all game logic and state here and nothing else.
2) View: Renders the state of the model once per frame.
3) Controller: Just a class implementing InputProcessor that does pretty much nothing other than translating input events to method calls on the model.

I'm not exactly sure what you mean by 'game state' but if you want to change the tileset that the renderer uses based on, say, whether the current level is a lava world or a forest world, then specify which type of world it is in the model (I guess if you have a Map or Level class then it would be a property of that), and when rendering (in the view code) look at that property of the model in order to choose your tileset. Make sense?

Basically, always store any information in the most suitable and relevant place. If you don't have a suitable and relevant place for a particular thing, then make one and put all similar/related things there.

Would one say it is ok to have texture(graphics)rendered from the controller layer?


No. Not game graphics anyway. You might render buttons and such for convenience, since they are conceptually part of the controller.

I find it all kind of gets confusing to an extend and end up chasing my tail.


Like we said before, this kind of stuff is much less important than getting something on the screen. Once you have stuff moving on the screen things get fun and the architectural work will just kind of get done in the background as you make your amazing game! Your first few games will probably end up with horrifically messy code, but you will learn so much and each one will be better than the last. Just enjoy the journey :)
felixwatts
 
Posts: 75
Joined: Fri Dec 30, 2011 3:54 pm

Re: MVC programming

Postby djgriff » Wed Aug 01, 2012 8:11 pm

Hi, thanks for the reply, yes you have cleared up some valid points and think i will just get the darn thing working :).

My point on GameStates is :
for instance you have
Code: Select all
enum State{
READY, RUNNING,PAUSED, NEXT_LVL, GAME_OVER
}

then set the initial game state to constructor stuff..

state = Sate.READY

then once you send a touch input it switches to the Running State that starts the game.

Basically i have implements the states and everything, and rendered the relevant pause buttons and ready message in the Ready state, but the game is already running in the back ground, when the ready message is being presented.

In the RUNNING state i have placed the call to the WorldRenderer class which renders the world class which places all the object, boat, cloud,rain ect. so.. kinda confused as to where you would actually place the states.. i am kinda thinking i should be creating the game states in the WorldRenderer class but not sure...

kinda sticking on this issue.

After your reply though i am thinking i need to create several states for the objects (model) classes, for instance : IDLE,MOVING,DEAD and apply a state to the state of the game, if that makes sense..

This is the first time i am implementing states so please bare with me if i have got this completely tits up, ;)

Cheers

Daniel
djgriff
 
Posts: 253
Joined: Tue Jan 03, 2012 8:11 am

Re: MVC programming

Postby felixwatts » Thu Aug 02, 2012 7:50 am

OK I think this is easier to describe in code.

Here are 4 simple classes, a Model, a View, a Controller and a Game. The Game implements ApplicationListener and ties the other three together. This is not a working example but it should illustrate an answer to your question. The state goes in the model. Only the Model and the View need to know about the state. The View only needs to know about the state because the UI is different depending on the state.

The model is completely unaware of the View and the Controller. It should never call methods or properties on either. The Model exposes its state and functionality via properties, methods and callbacks (events). The View reads the state of the model which tells it everything it needs to draw the current state of the game. The Controller responds to events of the Model in order to display the correct UI at any time. Input events are handled by the Controller and translated into method calls on the Model.

Code: Select all
public class Model
{
   public enum State { READY, RUNNING,PAUSED, NEXT_LVL, GAME_OVER }

   public static final int EV_STATE_CHANGED = 0;
   
   private State _state = State.READY;   
   private IEventHandler _eventHandler;
   
   public Model(IEventHandler eh)
   {
      _eventHandler = eh;
   }
   
   public State getState()
   {
      return _state;
   }
   
   public Array<IEntity> getEntities()
   {
      // ...
   }
   
   public void update(float dt)
   {
      switch(_state)
      {
         case RUNNING:
            // update
            break;
         // no need to update if READY or PAUSED
      }
   }
   
   private void changeStateTo(State state)
   {
      _state = state;      
      _eventHandler.onEvent(EV_STATE_CHANGED, null);
   }

   public void start()
   {
      changeStateTo(State.RUNNING);      
   }

   public void pause()
   {
      changeStateTo(State.PAUSED);   
   }

   public void resume()
   {
      changeStateTo(State.RUNNING);         
   }
}


Code: Select all
public class View
{
   private Model _m;

   public View(Model m)
   {
      _m = m;
   }
   
   public void render()
   {
      for(IEntity e : _m.getEntities())
         renderEntity(e);
   }
   
   private void renderEntity(IEntity e)
   {
      // ..
   }
}


Code: Select all
public class Controller extends Stage implements IEventHandler
{
   private Model _m;
   
   public Controller(Model toControl)
   {
      super(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
      
      _m = toControl;
      
      // add buttons or whatever
   }
   
   private void onButtonStartTouched()
   {
      _m.start();
   }
   
   private void onButtonPauseTouched()
   {
      _m.pause();
   }
   
   private void onButtonResumeTouched()
   {
      _m.resume();
   }

   @Override
   public void onEvent(int eventType, Object arg)
   {
      switch(eventType)
      {
         case Model.EV_STATE_CHANGED:
            if(_m.getState() == State.PAUSED)
            {
               // just paused, make the paused message and resume button visible
            }
            else if(_m.getState() == State.RUNNING)
            {
               // just started/resumed, hide resume button and make main controls visible
            }
      }
   }
}


Code: Select all
public class Game implements ApplicationListener, IEventHandler
{
   private Model _m;
   private View _v;
   private Controller _c;

   @Override
   public void create()
   {
   }

   @Override
   public void resize(int width, int height)
   {
      _m = new Model(this);
      _v = new View(_m);
      _c = new Controller(_m);
      
      Gdx.input.setInputProcessor(_c);
   }

   @Override
   public void render()
   {
      _m.update(Gdx.graphics.getDeltaTime());
      _v.render();
      _c.draw();
   }

   @Override
   public void pause()
   {
   }

   @Override
   public void resume()
   {
   }

   @Override
   public void dispose()
   {
   }

   @Override
   public void onEvent(int eventType, Object arg)
   {
      _c.onEvent(eventType, arg);
   }

}
felixwatts
 
Posts: 75
Joined: Fri Dec 30, 2011 3:54 pm

Re: MVC programming

Postby djgriff » Thu Aug 02, 2012 8:27 am

Hi Felix,

Thanks a million... explains it and makes everything clear!

Thanks for taking the time, really appreciate it!

looks like i have been mixing it all up together.

Many Thanks

Daniel
djgriff
 
Posts: 253
Joined: Tue Jan 03, 2012 8:11 am

Next

Return to General Development

Who is online

Users browsing this forum: No registered users and 4 guests