Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Win32.. Game Loop

This thread is locked; no one can reply to it. rss feed Print
Win32.. Game Loop
decsonic
Member #4,150
December 2003

Came Across some problems when handling the WinMain my self,

1// the message "loop"
2GameInit();
3 
4 while( 1 )
5 {
6 if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
7 {
8 if( !GetMessage( &msg, NULL, 0, 0 ) )
9 {
10 return msg.wParam;
11 }
12 TranslateMessage(&msg);
13 DispatchMessage(&msg);
14 } else
15 GameUpdate(); // Run the Game
16 }
17 
18 GameQuit(); // Finish it.
19}
20 
21//Game Update
22int GameUpdate()
23{
24 
25blit(image, buffer, 0,0,0,0, 400,400);
26SendMessage(hwnd,WM_PAINT,0,0);
27}
28 
29//Case WM PAINT
30 HDC hdc;
31 PAINTSTRUCT ps;
32 
33switch(message)
34{
35 case WM_PAINT:
36 hdc = BeginPaint( hwnd, &ps );
37 blit_to_hdc(buffer, hdc, 0, 0, 0, 0, 800, 600);
38 EndPaint( hwnd, &ps );
39 return 0;
40 break;
41//... a few others
42}

buffer is a double buffer.
It draws when i focus away from the window, checked if it actualy goes inside the game update function and WM_PAINT.. and hum yea it does, had a pop up window to check, almost crashed my comp to many off them,
Any one know how to go about this? as im shure my idea isnt the best,
(without multithreading).
cheers :)

Programmer's paranoia: Don't trust anybody's code, not even your own.

Human Modeling Tutorials

gillius
Member #119
April 2000

I don't understand what your problem is? Are you trying to get WM_PAINTs or???

Gillius
Gillius's Programming -- https://gillius.org/

decsonic
Member #4,150
December 2003

Well i dont get why hdc doesnt get blitted, done a few tests, it goes inside WM_PAINT alot and my "Game Update" function (off course since WM paint is forced there.)
Basicly im asking how others do their Game Loop with win32

Programmer's paranoia: Don't trust anybody's code, not even your own.

Human Modeling Tutorials

gillius
Member #119
April 2000

what do you mean HDC... Are you trying to use Allegro and then blit to the HDC? Yes you are. Are you sure that the buffer contains the proper image that you are expecting?

EDIT: do you have to get a HDC every time you paint? I don't know Win32 very well but it seems you store the HDC once and don't get it in WM_PAINT. Honestly I can't remember if you can do that.

Gillius
Gillius's Programming -- https://gillius.org/

Thomas Fjellstrom
Member #476
June 2000
avatar

I found the BeginPaint thing says its supposed to give a valid HDC.. but it doesnt. so I grabbed mine from the ps struct (IIRC, been a while since I looked at my win32 code...)

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

decsonic
Member #4,150
December 2003

ahh 1 thing i forgot,
it draws when its in the background, ie. if i open Winamp or a browser window ect.
Not to experienced with Win32 API, lived happly in the

int main()
{
}

"world" ;)
edit:
a bit late to be posting questions, might be a bit unclear.. sorry hehe.
In short, Is there reason that code posted above shouldnt Blit the buffer?

Programmer's paranoia: Don't trust anybody's code, not even your own.

Human Modeling Tutorials

Kitty Cat
Member #2,815
October 2002
avatar

I think the only time it's not supposed to draw is when it's minimized. If you're app is in the background, it can still be visible, even if only partially, or if the window on top is translucent. It sounds like, though, that you want your game to pause execution when you go into the background. I don't know how to do that.

EDIT:
Oh, I got that backwards.. hmm. I dunno. ???

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

decsonic
Member #4,150
December 2003

the problem is that it doesnt draw when it has focus :P

Programmer's paranoia: Don't trust anybody's code, not even your own.

Human Modeling Tutorials

A J
Member #3,025
December 2002
avatar

are you waiting on a WM_PAINT msg before you draw to the screen ?

i think you will only receive a WM_PAINT when the window changes state or it gets un-obscured by another window.

___________________________
The more you talk, the more AJ is right. - ML

23yrold3yrold
Member #1,134
March 2001
avatar

Program might be waiting for a new message when it has the focus. Maybe set up a WM_TIMER?

And you can call InvalidateRect() instead of sending WM_PAINT messages. That might help too ... actually, yes. That might be your problem. Sending a WM_PAINT message might be totally wasted if there's no "dirty" areas that need updating.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Rick
Member #3,572
June 2003
avatar

Why are you doing it this way? You shouldn't be calling the WM_PAINT message to update the screen. Update the screen in GameUpdate.

========================================================
Actually I think I'm a tad ugly, but some women disagree, mostly Asians for some reason.

Specter Phoenix
Member #1,425
July 2001
avatar

This is the just of my code nowadays from learning WinAPI/DirectX:

1int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
2{
3WNDCLASSEX winclass;
4HWND hwnd;
5MSG msg;
6HDC hdc;
7 
8winclass.cbSize = sizeof(WNDCLASSEX);
9winclass.style = CS_DBCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
10winclass.lpfnWndProc =WindowProc;
11winclass.cbClsExtra =0;
12winclass.cbWndExtra =0;
13winclass.hInstance =hinstance;
14winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
15winclass.hCursor = LoadCursor(NULL, IDC_CROSS);
16winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
17winclass.lpszMenuName =NULL;
18winclass.lpszClassName ="WINCLASS1";
19winclass.hIconSm =LoadIcon(NULL, IDI_APPLICATION);
20 
21if(!RegisterClassEx(&winclass))
22return(0);
23 
24if(!(hwnd = CreateWindowEx(NULL, "WINCLASS1", "Title", WS_POPUP | WS_VISIBLE, 0,0, 400, 300, NULL, NULL, hinstance, NULL)))
25return(0);
26 
27Game_Init();
28 
29while(TRUE)
30{
31if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
32{
33if(msg.message == WM_QUIT)
34break;
35 
36TranslateMessage(&msg);
37 
38DispatchMessage(&msg);
39}//end if
40Game_Main();
41}// end while
42Game_Shutdown();
43 
44return(msg.wParam);
45}// end WinMain

Of course for the WNDCLASSEX I use the shorter form to save typing:

WNDCLASSEX winclass = {
 winclass.cbSize = sizeof(WNDCLASSEX), CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW,
 WindowProc, 
0, 
0, 
hinstance, 
LoadIcon(NULL, IDI_APPLICATION), 
LoadCursor(NULL, IDC_CROSS), 
(HBRUSH)GetStockObject(BLACK_BRUSH), NULL, 
"WINCLASS1", 
LoadIcon(NULL, IDI_APPLICATION)
};

I use WS_POPUP for use with DX:).
[edit]Almost forgot to answer the problem. Unless the screen is constantly updating there is now point in use WM_PAINT cause it will just put the data in the buffer and display it:(.

decsonic
Member #4,150
December 2003

void Update( HWND hWnd )
{
HDC DC = BeginPaint(hWnd,&ps);
 blit_to_hdc( buffer, DC,0,0,0,0,800,600);
EndPaint( hWnd, &ps );
}

atm that just crashes::)

Programmer's paranoia: Don't trust anybody's code, not even your own.

Human Modeling Tutorials

23yrold3yrold
Member #1,134
March 2001
avatar

Do you create 'buffer' anywhere?

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

decsonic
Member #4,150
December 2003

yup its a double buffer, 800,600.
changing to Dx now i think anyways, been avoiding this for to long:P

Programmer's paranoia: Don't trust anybody's code, not even your own.

Human Modeling Tutorials

Go to: