C# and the game loop

My primary interest in learning C# is to write games and yet it appears that much of the C# beginner tutorials focus code that is largely event-driven.

I also caught a few discussions in blogs recently that had a intense debate on how to get C# to properly execute a real-time game loop and from what I could discern of the discussions, this task seemed to have an air of in-depth "hacking" about it - as though C# is simply not designed to do things this way.

The discussions on the blog sites were also very unclear on the topic of game loops and as a relative beginner, the explanations behind the issues were simply beyond me.

Now given that C# is being hailed as a great language to create games, how come there's no clear discussion of this issue of forcing C# to construct a suitably efficient real-time game loop in any game-programming specific tutorials?

Or is it the case that until we are extremely profficient in C#, beginners or intermediate users will be limited to creating event-based game programs (I'm thinking turn-based here)?

Does anyone know of a good place that discusses this apparently tricky issue of real-time game loops with regards to beginner programmers. Those blogs I mentioned clearly assume you already have lots of C# knowledge in order to grasp the concepts involved.

- Seef

[1357 byte] By [Seefer] at [2007-12-25]
# 1
I would say your desires to create a fast effective game loop should come in time, as you program your game. Until you can point out what is not running correctly for your desired intent, it can't be fixed. What kind of game are you wanting to program? Can the event driven nature of c# not program it effectively? Can you launch some threads to take care of your background code? Without a great deal more understanding what you want this is a very hard question to help with.
shadowmite at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 2
Hi Seefer,
Game programming is a long way off, if you are just starting with C# (or any programming language for that fact).
However, I would strongly recomend that you start by learning all the basics, and using them to build apon (as myself and others in this forum have done). You will soon find that parts of the C# syanx stick and then more and more and before you know it you can write a whole application from inside your head.
But its all about getting the stuff to stay in your head, or having a good selection of snippits ;-)
Anyhow, have a look through some of these, they teach you some of the basics, but still let you do what you want to do, I guess its no good learning to write a game by learning about how to be an accountant!!

http://msdn.microsoft.com/coding4fun/gamedevelopment/beginning/default.aspx
http://msdn.microsoft.com/coding4fun/gamedevelopment/rocketcmd/default.aspx
http://www.codeproject.com/directx/invasion.asp
http://www.codeproject.com/csharp/tetriscontrol.asp

Hope this helps you out, oh and I would start with the CodeProject links first.

Scott

ScottMcKeown at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 3

I'm not querying how you make a real-time game loop fast and efficient, I'm wondering how you even get a real-time game loop implemented in C#?

I was thinking any standard real-time game needs to have such tasks as AI, graphics rendering, game input device reading etc continually running and updating per frame? I thought that's how the majority of real-time games behave? So unless you are strictly interested in games that are ideal for event-driven button pushing like a card games, how do you achieve real-time processing in C#? All the project templates I see in C# are event-driven and I've not seen any tutorials that tackle the fundamentals of program flow and class design based around real-time game loops.

From what I could make out of the blogs on this 'game loop' topic, it seemed that there is no straightforward way to code C# so that it goes beyond it's event-driven nature. You had to resort to some fancy footwork and manual coding of classes beyond the project templates you are given by C#.

As for using multiple threads, I believe most games today are single threaded (this will likely change soon with multi-core processors becoming common) and I'm told threads are are not a good place to have such things as AI code and frame drawing happening due to synchronisation complexities? Multiple threads are more commonly restricted to reading mouse and game controller input devices asynchronously to ensure smooth interactivity.

Basically, the question is. How do you create a C# program where your game routines that handle AI, rendering, input are called continuosly while keeping your application Window friendly, handling necessary Window events so it doesn't lock up your system? My limited understanding is that it's something to do with such application methods like App.OnIdle or using other C# calls such as App.DoEvents. Yet I do not see clear explanations of such fundamental real-time techniques in any C# Express game programming tutorials.

I once tried getting my head around C++ Windows game programming and I managed to get some grasp of how you achieve a real-time game code framework within the event-driven Windows ebvironment. There was talk of a message queue and Windos Callback Porcedure and you basically set things up so that a game loop was continuously run while at the same time checking for whether there were any Windows Messages to process. If a Windows Message did occur, you jumped out of the game loop to process it and, when finished, you jumped right back into your game loop.

I'm not at all sure how to go about this in C# and there seems scant information or tutorials on accomplishing this. Maybe I'm looking in the wrong places for C# game programming tutorials :)

Any tips would be welcome.

Seefer at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 4

What you say makes sense but I have to admit to being totally turned off learning a programming language by writing the kind of mundane bank account style exercises I see littering most C# books. I prefer coding examples to be less trivial and to actually be leading somewhere :)

I'd much rather see a C# language tutorial that uses game programming exercises - how about covering arrays or structures by using an example of a character's Inventory of items a'la RPG game? They don't even need to cover super-complex graphics. You could target it around a simple Space Invaders or Tetris-like game. Heck you could even limit the graphics to standard character text visuals but you would at least be learning about such funadmental game concepts like collision detection, moving things on screen (even if they're simple character text printing visuals) etc?

A friend of mine learning C++ showed me a book called Beginning C++ Game Programming (by Michael Dawson, published by Premier Press) that contained examples geared towards simple game concepts. From what I could make out (C++ makes my head swim), it looked quite good.

I had the same motivational problem when learning the guitar. There were plenty of boring classical guitar tutors in my area but hardly any tutors whose passion is for rock guitar and the techniques that come with that :)

I'll check out those links as it appears to be game related content. Hopefully all my questions will be answered there...thanks.

Seefer at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 5

Since you are using the Visual C# Express Edition you may want to take a look at the XNA Framework (currently in Beta):

http://msdn.microsoft.com/directx/XNA/default.aspx

and XNA Game Studio Express

http://msdn.microsoft.com/directx/xna/gse/

MikeDanes at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 6
Seefer,
The links that I posted are C# Applications that are the said types of games, and they will also take you through step by step and also help you understand the language.
I was simply trying to point out that you have a long road ahead of you and did not want to put you off learning C#, I belive in honesty!
Also there is a C# version of the book that you mention (click this link)
http://www.amazon.co.uk/Programming-Absolute-Beginner-Beginners-S/dp/1931841160/sr=1-28/qid=1161339505/ref=sr_1_28/026-2447449-2414807?ie=UTF8&s=books

Scott

ScottMcKeown at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...