Archived

This topic is now archived and is closed to further replies.

hackcow

GDI questions

Recommended Posts

hackcow    122
Hey, a couple questions for all you gdi masters. 1. Double buffering? ---------------------- I have 2 HDC Variables in my code, like this: HDC g_hdcWindow = NULL; HDC g_hdcBackBuffer = NULL; I set the first one like this: g_hdcWindow = GetDC(g_hWnd); And the second like this: g_hdcBackBuffer = CreateCompatibleDC(g_hdcWindow); Then I blt my images to g_hdcBackBuffer (after selecting my bitmap objects into g_hdcMemory, of course): BitBlt (g_hdcBackBuffer, 0, 0, 10, 10, g_hdcMemory, 0, 0, SRCCOPY); Then after a couple more blts to the backbuffer, I do this BitBlt(g_hdcWindow, 0, 0, 50, 50, g_hdcBackBuffer, 0, 0, SRCCOPY); But it doesn''t seem to work. Any ideas?? 2. Meesage Pump? ---------------- In my program, I am doing some simple blts to the window, about 10 times a second, in a timer message in my message pump. My message loop code looks like this: PeekMessage( &msg, NULL, 0U, 0U, PM_NOREMOVE ); while (WM_QUIT != msg.message ){ if (PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE )){ if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)){ TranslateMessage(&msg); DispatchMessage(&msg); } } } I found this taking up 100% of my cpu. so I added this: } else{ Sleep(10); } } Which seemed to do the trick. Is this safe??? Thanks all, and sorry about the big post! Kevin

Share this post


Link to post
Share on other sites
Brannon    122
Sure it's safe. The reason it's taking 100% of the CPU is because you are using PeekMessage(). You are never yielding, so you end up using 100% of each of your timeslices (before the OS preempts you). By calling Sleep(10), you are giving up the remainder of your current timeslice. Sleep(1) should do the trick too.

If that is exactly what your message pump looks like, then just use GetMessage(). It will sleep until you receive a message, and in your case would be slightly "safer" then Sleep(10), because it selectively Sleep()'s (only when you don't have any pending messages, otherwise it processes them as fast as possible).


-Brannon

Edited by - Brannon on December 16, 2001 4:43:07 AM

Share this post


Link to post
Share on other sites