Skip to content
Gabor Szauer edited this page Aug 20, 2015 · 3 revisions

#Game Structure So far in every demo when we added something to the application we created a static variable in Program.cs for it. While this is one way to make a game, it's not a smart way to make a game. Even simple games can quickly become really complicated, in general we tend to have one game class that contains lists of related objects (A list of enemies, a list of backgroun images, etc). The game calss in turn has a Render function that just loops trough all of it's lists and calls render on them all. This in practice may look something like this:

UML

Often it makes sense to make the game class into a singleton and then hook that singleton into the window.

#Game Singleton Let's make a sample game class (new file, Game.cs). This is going to be a pretty good place for you to start as well

class Game {
    private static Game instance = null;

    public static Game Instance {
        get {
            if (instance == null) {
                instance = new Game();
            }
            return instance;
        }
    }

    private Game() {

    }

    public void Initialize() {
        // LOAD TEXTURES AND AUDIO
    }

    public void Render() {
        // DRAW STUFF
    }

    public void Update(float deltaTime) {
        // UPDATE STUFF
    }

    public void Shutdown() {
        // UNLOAD STUFF
    }
}

Now you can fill in all game specific logic inside of this game class.

How do we hook this up to the application? In Program.cs do the following:

public static void Initialize(object sender, EventArgs e) {
    GraphicsManager.Instance.Initialize(Window);
    TextureManager.Instance.Initialize(Window);
    SoundManager.Instance.Initialize(Window);
    InputManager.Instance.Initialize(Window);

    Game.Instance.Initialize();
}

public static void Update(object sender, FrameEventArgs e) {
    InputManager.Instance.Update();
    Game.Instance.Update((float)e.Time);
}

public static void Render(object sender, FrameEventArgs e) {
    GraphicsManager.Instance.ClearScreen(Color.CadetBlue);

    Game.Instance.Render();

    int FPS = System.Convert.ToInt32(1.0 / e.Time);
    GraphicsManager.Instance.DrawString("FPS: " + FPS, new PointF(5, 5), Color.Black);
    GraphicsManager.Instance.DrawString("FPS: " + FPS, new PointF(4, 4), Color.White);

    GraphicsManager.Instance.SwapBuffers();
}

public static void Shutdown(object sender, EventArgs e) {
    Game.Instance.Shutdown();

    InputManager.Instance.Shutdown();
    SoundManager.Instance.Shutdown();
    TextureManager.Instance.Shutdown();
    GraphicsManager.Instance.Shutdown();
}

This will intialize the game, and call the correct functions of the Game singleton in the correct places. As an added bonus, the FPS will always be displayed as white text with a black shadow on the top left.

#Other ways There are many ways to solve a problem in game development, the Game singleton presented here is just one of them. If you want you can come up with your own solution. There is no right or wrong way to do things.

Clone this wiki locally