blog community

Welcome to blog community Sign in | Join | Help
in Search

Erno de Weerd

About software development and training

Game main-loop (the never ending story)

From Ideal Winforms gameloop: Corrections and Additions I have derived my implementation of the game mainloop. The reason I prefer this one is simple: I want 'pure' .NET; no PInvoke if not necessary.
 
  public class TesterForm : System.Windows.Forms.Form
  {
    private enum WindowsMessage
    {
      Paint =  0x000F
    }
 
    private GameEngine _gameEngine;
 
        public void Initialize(GameEngine gameEngine)
    {
#if WINDOWED
      this.Size = new Size(640,480);
#else
      this.FormBorderStyle = FormBorderStyle.None;
      this.Size = Screen.PrimaryScreen.Bounds.Size;
#endif
      this.StartPosition = FormStartPosition.CenterScreen;
      this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
      _gameEngine = gameEngine;
      _gameEngine.Initialize(this);
    }
 
    private void Frame()
    {
      if(_gameEngine != null)
      {
        _gameEngine.ReadInput();
        _gameEngine.ProcessGamelogic();
        _gameEngine.Render();
      }
    }
 
    protected override void WndProc(ref Message m)
    {
      switch (m.Msg)
      {
        case (int)WindowsMessage.Paint:
        {
          Frame();
          break;
        }
        default:
        {
          base.WndProc(ref m);
          break;
        }
      }
    }
  }
}

As you can see there is no Main in this class. It is my GameEngine class (or a sub class of the GameEngine class) that contains the Main and thus is the entrypoint of the application. The GameEngine class passes an instance of itself to the Form. I think that this is a better design.
I'll post my GameEngine class in the near-future.
 
 
Published Monday, May 09, 2005 8:46 AM by Erno de Weerd
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Raimond said:

I don't think it's good design to put a Main entrypoint in a functional class. Instead, functional items should be as decoupled from the execution environment as possible, so that they can be easily called from other environment as wel, for instance unittest frameworks.
I think the better design would be to have a main function, seperate from the Form and the GameEngine class and initialize a Singleton gameengine there...
May 9, 2005 9:26 AM
 

Erno said:

Let me start by stating that I consider the Main entrypoint itself being ugly. In an OO design/language we don't need a Main. It would be more logical to instantiate an object and let its constructor be the entrypoint...

That said, I guess you're right. Up until now I haven't really worried about this because I'm not writing a game (yet) but just the engine. Of course I have considered the use of the engine and the current design leaves all options open; I do not force a particular usage pattern; any method can create a form, a GameEngine object and then pass the engine to the form. I deliberatly chose NOT to create a singleton so I can run serveral engines at the same time. There can only be one engine assigned to a form.
May 9, 2005 9:45 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server, by Telligent Systems