I just started learning .NET so chances are this is a big n00b mistake:
I’m trying to make a simple pong game, using a form and then the
System::Drawing::Graphics class to draw the game to the form.
The significant bits of my code look like this:
(Main Game Loop):
void updateGame()
{
//Update Game Elements
(Update Paddle And Ball) (Ex. paddle.x += paddleDirection;)
//Draw Game Elements
//Double Buffer Image, Get Graphics
Bitmap dbImage = new Bitmap(800, 600);
Graphics g = Graphics::FromImage(dbImage);
//Draw Background
g.FillRectangle(Brushes::White, 0, 0, 800, 600);
//Draw Paddle & Ball
g.FillRectangle(Brushes::Black, paddle);
g.FillRectangle(Brushes::Red, ball);
//Dispose Graphics
g.Dispose();
//Draw Double Buffer Image To Form
g = form.CreateGraphics();
g.DrawImage(dbImage, 0, 0);
g.Dispose();
//Sleep
Thread.sleep(15);
//Continue Or Exit
if(contineGame())
{
updateGame();
}
else
{
exitGame();
}
}
(Form Initialization Code)
void InitForm()
{
form = new Form();
form.Text = "Pong"
form.Size = new Size(800, 600);
form.FormBorderStyle = FormBorderStyle::Fixed3D;
form.StartLocation = StartLocation::CenterScreen;
Application::Run(form);
}
PS. This is not the exact code, I just wrote it from memory, so that’s where any typos or
wrong names, or some important lines of code to do with initializing the form would be coming from.
So that is the code.
My issue is is that the game is certainly not updating every 15 milliseconds (about 60 fps), its going much slower, so what I have to do instead is move the paddle/ball larger amounts of distance each time to compensate for it not updating very quickly, and that looks very bad.
In a nutshell, something when it comes to drawing the graphics is slowing the game down a very large amount. I have a feeling it has something to do with me double buffering, but I cannot get rid of that, since that would create some yucky flickering. My question is,
how do I get rid of this lag?
There are several issues with this code that can heavily impact app performance:
Bitmapbuffer every frame and not disposing itThread.Sleep()in GUI threadCode sample, which uses separate thread to count game ticks and WinForms built-in double-buffering:
Even more improvements can be made (for example, invalidating only parts of game screen).