Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, i have been developing a game-project in Visual C++ 6.0 but now i have an extremely bad problem. When i start my game, it gets a lot of CPU usage - like 60-70%. When i start debugger it gives me a several errors, but i have no idea how to fix it. Throughly i'm using bitmaps and i don't know if that's the cause. Look how it looks - http://img2.imageshack.us/img2/716/guitarmaster.jpg
As you can see "Start Game" is marked and the bitmap has orange background and the others have black background. When an option is marked it puts another bitmap to the place of original. The bitmaps i load in this way

void Game::StartGame(CBitmap *p, CDC *pDC)
{
CDC dcMemory;
BITMAP bm;
dcMemory.CreateCompatibleDC(pDC);
dcMemory.SelectObject(p);
p->GetBitmap(&bm);
int x,y;
x = (screenX*bm.bmWidth)/1280;tretchBlt(rect.right/2-bm.bmWidth/2, 2*y, x,y, &dcMemory, 0,0, bm.bmWidth, bm.bmHeight, SRCCOPY);
}

In my OnInitDialog() function my code is:
CRect rect;
GetClientRect(&rect);
CClientDC clientDC(this);
pDC = new CDC();
Bitmap = new CBitmap();
Bitmap->CreateCompatibleBitmap(&clientDC,rect.right,rect.bottom);
pDC->CreateCompatibleDC(&clientDC);
pDC->SelectObject(Bitmap);


And in WM_TIMER (OnTimer) i use the following:
pDC->illSolidRect(rect, RGB(0,0,0));

and then the bitmap from above i load there

CBitmap startGame;
startGame.LoadBitmap(the_resource_here); // if not marked
CBitmap startGame1;
startGame1.LoadBitmap(the_resource_here); // if marked - here the bitmap file is different
I have a boolean variable to check if the menu item is marked or not - I make that in PreTranslateMessage();
In OnTimer() i go on with:
if(ifStartGame == true)
StartGame(&startGame, pDC);
else
StartGame(&startGame1, pDC);


Now the bad stuff;
My menu items(Start Game... About ... Exit) i control them using Arrow Keys from keyboard(in PreTranslateMessage).
When i press Enter in one of them it calls me a class(for example for start game - one class, for about - other class)
It works perfect until i press Enter on them 5-6 times, and then it stops calling the class windows.

I'll post my PreTranslateMessage() code below:
if (pMsg->message == WM_KEYUP)
{
if(pMsg->wParam == VK_DOWN)
{
if(ifStartChecked == true) // if start is true - it makes the next true and start - false
{
ifHowToPlayChecked = true;
ifStartChecked = false;
}
else if(ifHowToPlayChecked == true)
{
ifHowToPlayChecked = false;
ifAboutChecked = true;
}
else if(ifAboutChecked == true) // HERE I CHANGE THE STATES - I MEAN THE PLAYER CHOOSES AN OPTION
{
ifAboutChecked = false;
ifExitChecked = true;
}
else if(ifExitChecked == true)
{
ifExitChecked = false;
ifStartChecked = true;
}
}

if(pMsg->wParam == VK_RETURN) // IF THE PLAYER PRESSES ENTER IT CHECKS ON WHICH MENU ITEM ENTER IS PRESSED
{
if(ifStartChecked == true)
{
Class1 dlg1;
dlg1.DoModal();
}
else if(ifHowToPlayChecked == true)
{
Class2 dlg2;
dlg2.DoModal();
}
else if(ifAboutChecked== true)
{
Class3 dlg3;
dlg3.DoModal();
}
else if(ifExitChecked == true)
{
exit(0);
}
Posted
Updated 24-Jan-10 2:45am
v2

1 solution

It seems that you have several problems. Try taking them one at a time. Also please enclose your code in <pre></pre> tags and properly indent it. In general, it is also useful to provide the specific errors/messages that you get. (Those are usually also a good place to start looking for your problems yourself.)

It appears to me that there is at least one basic problem in your design. You seem to be trying to draw on the window all over the place - in response to many different events. Normally, you draw on the window only in OnDraw for a dialog or in OnPaint for a window. In response to other events you change variables that indicate the state of the program so OnDraw/OnPaint know what should be displayed and you call Invalidate or InvalidateRect so that OnDraw/OnPaint will be called and told what part of the window to redraw.

I have no idea what you are using a timer for at this point in your program. From what I can see from your screen shot, there is no reason to use one. If there is no good reason to use a timer, don't.

Since you are using a dialog for this window, I would think that you would be using buttons for your options. This simplifies your screen painting, moves the starting of modal dialogs to the OnClicked of the appropriate button and should entirely get rid of your PreTranslateMessage().

I would suggest a book such as "Programming Windows with MFC" by Jeff Prosise. I would also suggest a later version of Visual C++, as version 6 does not work with standard C++.

Good luck.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900