• Advertisement
Sign in to follow this  

game run loop

This topic is 2161 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to implement a simple game loop for animating a ball in breakout using c# and gdi+ I have chosen this project because I want to learn c# better.here is the code I am using.


[font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Image[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] curImage17 = [/font][/font][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"][font="Consolas"][size="2"][color="#2b91af"]Image[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"].FromFile([/font][/font][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"][font="Consolas"][size="2"][color="#a31515"]@"c:\Users\phil\Desktop\ball.bmp"[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]);[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]g.DrawImage(curImage17, 400 + j, 300 + k);[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]j += 5;[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]k+=5;[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]I know I have done this project before but never from the c# prespective.I have to admit I really like c# it is nice and clean.[/font][/font]

Share this post


Link to post
Share on other sites
Advertisement
If you want to make games using C#, I suggest you grab either XNA or SlimDX. They offer you a lot of stuff, for example built-in update and draw loops, so you could do this (in XNA):


public override void Update(GameTime gameTime)
{
j += 5;
k += 5;
}

public override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
spriteBatch.Draw(ballTexture, new Vector2(j, k), Color.White);
spriteBatch.End();
}

Share this post


Link to post
Share on other sites
Surprisingly enough, coming up with a game loop in pure C# is not an easy task. A simple google search will convince you of that.
Most C# "gaming" solutions (SlimDX, XNA, OpenTK etc) come with a built in game looped window class that will hide this tricky part from you.
If you are left implementing this by yourself this code I use might point you into a useful direction.. the idea is to intercept the "OnIdle" event of the Application object. This event is not really doing what you expect, because it only gets called WHEN the application BECOMES idle.

void Application_Idle(object sender, EventArgs e)
{
while (isStillIdle())
{
onIdle();

}

}


bool isStillIdle()
{
Message m = new Message();
return !Win32.PeekMessage(out m, new HandleRef(null, System.IntPtr.Zero), 0, 0, 0);
}


So within this function I start peeking at the message queue and break out as soon there is a message in there. It's not the most elegant and performant solution but it gets the job done well and without generating too much garbage. You'll still have to look into making a delegate for the Win32 stuff: PeekMessage function and Message struct .. also easy to find on google.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement