Hi!
I have for a school assignment created a small game, it is divided into 2 different projects, one project with the form and one with a DLL-file containing the game.
The game loop is very simple and looks like this:
private void GameLoop(Graphics g)
{
int lastTick = Kernel32.GetTickCount();
do
{
if (terminated)
break;
while ((lastTick + 50) > Kernel32.GetTickCount())
Application.DoEvents();
while (gamePaused)
Application.DoEvents();
g.FillRectangle(new SolidBrush(Color.White), 0, 0, 800, 640);
DrawWalls(g);
MoveMonsters();
DrawMonsters(g);
lastTick = Kernel32.GetTickCount();
}
while (true);
gameRunning = false;
}
It works as intended and redraws the panel on the form page. On the form page i have a button to quit the current game, this is done simply by the main form calling the game.dll's command TerminateGame() witch set's terminated to true, this also works as intended. Now my problem is when the user clicks on the form close button or presses F4.
Then i tried to do the same thing:
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (game.IsRunning)
game.TerminateGame();
}
But then i keep getting this error: A generic error occurred in GDI+. and it points to this line: g.FillRectangle(new SolidBrush(Color.White), 0, 0, 800, 640);
I have no idea why it works when i press the button that just terminates the game and why its not working when the Form is closing, its the same method call.
The form closes perfectly if i first press the button and then press F4, it's just when i just press F4 that i keep getting this.
Any ideas?