Jump to content

  • Log In with Google      Sign In   
  • Create Account


game run loop


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 18 March 2012 - 10:06 PM

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.


Image curImage17 = Image.FromFile(@"c:\Users\phil\Desktop\ball.bmp");
g.DrawImage(curImage17, 400 + j, 300 + k);
j += 5;
k+=5;
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.

Sponsor:

#2 Manabreak   Members   -  Reputation: 141

Like
0Likes
Like

Posted 18 March 2012 - 10:54 PM

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();
}


#3 kunos   Crossbones+   -  Reputation: 2184

Like
0Likes
Like

Posted 19 March 2012 - 12:29 AM

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.
Stefano Casillo
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

#4 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 19 March 2012 - 08:43 PM

well I have decided to use the space bar to move the ball around the screen,I am working on breakout game using c# this time.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS