Page 1 of 1

Creating Games in C# - Part I The Game Loop

#1 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Post icon  Posted 16 April 2009 - 04:25 PM

Creating a Game Loop in C#

I posted a tutorial in the C# section about using Managed DIrectX. Well, I want to try and help those of you who can't use XNA because it requires Shader Model 1.1 or greater a chance to use C# to make a game. C# is a good choice for beginners because it takes a lot of the complexities out of windows programming and knowing the ins and outs of the Windows API.

Before you can get started there are a few things that you need to do. First you have to create a timer class. I know that C# ships with a Timer control. This timer is good but the class that I will show you will be able to be upgraded later.

Go head an create a new Windows application in Visual C#. (I use Visual C# 2008 Express Edition but any C# IDE should work, I'm not doing anything mystical.)

Once you have your application ready add a new class to your project and call it Timer. (I will be using this class in future tutorials so save your project and it will be added to as the tutorials continue.)

In this class you will be importing a method from the kernel32.dll so you will need to add a using statements to your class:

using System.Runtime.InteropServices;



This class allows you to access native code in your program.

I will be using an attribute to import a native method into the program. I'm not going to go into how attributes work, I want to concentrate on making a game loop. If you are interested in learning how attributes work I suggest you go to the MSDN library, look up the C# Programming Guide and open the Attributes topic. For now all you need to know is that you can use the [DllImport("file.dll")] attribute to import a method form a native code dll. You will be using one native code method in this class. This is how you import the method:

[DllImport("kernel32.dll")]
private static extern long GetTickCount();



The method is used to get the current tick count of your system.
You will need one private field for this class. It is used to store the current tick count:
private long StartTick = 0;



Now it is time to create the Constructor for the class. The constuctor is simple, all it does is call the Reset method to reset the tick count to the current tick.

public Timer()
{
    Reset();
}



The Reset function is also simple. It just sets the StartTick variable to the current tick count. Here is the code:

public void Reset()
{
    StartTick = GetTickCount();
}



There is one more method to add to the Timer class. It is a method to get how many ticks have elapsed since the last Reset of the timer. This is the code:

public long GetTicks()
{
    long currentTick = 0;
    currentTick = GetTickCount();

    return currentTick - StartTick;
}



Now you have a more accurate timer you can add to your C# games. In future tutorials the timer will be updated to include more features and I plan on making it a class library that can be easily added to your projects.

With the Timer class finished it is time to turn to the Program.cs file. I will be making a few changes to this file. First you need to find this line and delete it:

Application.Run(new Form1());



In it's place add this code:

Form1 myForm = new Form1();

myForm.Show();
myForm.GameLoop();



This code creates a new form, displays it calls a methed that you going to write to make the game loop.

That just leaves the form to code. You need to add a variable to the code for the timer. This is the code:

Timer timer = new Timer();



All that is left is to write three methods. One public method and two private methods. The private methods you will have to fill in when you are writing your games. The public method is the interesting method. This is the code for the methods:

public void GameLoop()
{
    while (this.Created)
    {
        timer.Reset();
        GameLogic();
        RenderScene();
        Application.DoEvents();
        while (timer.GetTicks() < 50) ;
    }
}

private void RenderScene()
{
    // TODO: Write the code to render your scene here
}

private void GameLogic()
{
    //TODO: Write your game logic here
}



So, what does the GameLoop method do? It runs until the form is closed. In each loop, the Timer is reset. Your GameLogic and RenderScene methods are called in order. Any events for the form are processed. Then comes the interesting part. This is where you write the code for handling the timing of the program. In this case the program pauses if less than 50 ticks have passed by. For your games you will have to adjust this number to get the timing right.

This is what you could do to create a game loop in C#.

Here are the complete listing so the Timer.cs file, Program.cs file and Form1.cs files:

Timer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace GameProject
{
    class Timer
    {
        [DllImport("kernel32.dll")]
        private static extern long GetTickCount();

        private long StartTick = 0;

        public Timer()
        {
            Reset();
        }

        public void Reset()
        {
            StartTick = GetTickCount();
        }

        public long GetTicks()
        {
            long currentTick = 0;
            currentTick = GetTickCount();

            return currentTick - StartTick;
        }

    }
}



Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace GameProject
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Form1 myForm = new Form1();

            myForm.Show();
            myForm.GameLoop();
            
        }
    }
}



Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GameProject
{
    public partial class Form1 : Form
    {
        Timer timer = new Timer();

        public Form1()
        {
            InitializeComponent();

        }

        public void GameLoop()
        {
            while (this.Created)
            {
                timer.Reset();
                GameLogic();
                RenderScene();
                Application.DoEvents();
                while (timer.GetTicks() < 50) ;
            }
        }

        private void RenderScene()
        {
            // TODO: Add code for rendering your scene here
        }

        private void GameLogic()
        {
            // TODO: Add code for your game logic here
        }

    }

}



Is This A Good Question/Topic? 2
  • +

Replies To: Creating Games in C# - Part I

#2 eljayuk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-July 09

Posted 21 July 2009 - 07:10 AM

Thanks for that tutorial, i found it useful as a starting point for developing a game with windows Worms. I was hoping you were looking to expand upon it, looking at how to implement bits in the GameLogic and Renderer sections. (Implementing the Cellular Automata in a forms app might make a good example ;)

You might also be able to to help with a few specific noobie questions...

I'm looking to make a kind of simulation which ticks along all by itself until a user pokes something for information or pauses it to carry out some action. I've not quite figured out how to link the game logic to the form, such that I can populate form fields from within the logic. (say, a label which takes a int value, to be updated each "tick"). Would I just pass a reference to the main form to the logic object as a reference?

I'd like to have a nice scrolly tile grid type map which can be clicked on to highlight specific tiles. Is a simple picturebox the best way to achieve this?

Thanks in advance!
Was This Post Helpful? 0
  • +
  • -

#3 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Posted 21 July 2009 - 01:56 PM

View Posteljayuk, on 21 Jul, 2009 - 08:10 AM, said:

Thanks for that tutorial, i found it useful as a starting point for developing a game with windows Worms. I was hoping you were looking to expand upon it, looking at how to implement bits in the GameLogic and Renderer sections. (Implementing the Cellular Automata in a forms app might make a good example ;)

You might also be able to to help with a few specific noobie questions...

I'm looking to make a kind of simulation which ticks along all by itself until a user pokes something for information or pauses it to carry out some action. I've not quite figured out how to link the game logic to the form, such that I can populate form fields from within the logic. (say, a label which takes a int value, to be updated each "tick"). Would I just pass a reference to the main form to the logic object as a reference?

I'd like to have a nice scrolly tile grid type map which can be clicked on to highlight specific tiles. Is a simple picturebox the best way to achieve this?

Thanks in advance!


Since you seem interested in creating a game with just C# I will try and write up a couple new tutorials on using the GameLogic and Render methods. It is usually best to render to a bitmap and then blit the bitmap to the form or picture box. This is a double buffering concept. Tiling is an interesting topic. It will take a couple days to write up the tutorials and have them approved. I will see what I can do.
Was This Post Helpful? 0
  • +
  • -

#4 doucet.damon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 06-August 09

Posted 07 August 2009 - 03:18 PM

Why do you P/Invoke the GetTickCount() function, when you could just as easily use System.Environment.TickCount ?

I must say, though, I'm definitely looking forward to future tutorials. :)
Was This Post Helpful? 0
  • +
  • -

#5 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Posted 03 November 2009 - 04:49 AM

Any update on when we can expect #2-infinity?

:^: This was a really well written tutorial, that was easy to follow and left me waiting for more. Thank you very much for it! :^:
Was This Post Helpful? 0
  • +
  • -

#6 harmelyo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-November 09

Posted 20 November 2009 - 08:01 PM

That's quite an introduction! I will be looking forward those tutorial.
Thank you very much for your time
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1