GDI problems.

Started by
5 comments, last by Omega147 18 years, 1 month ago
Hello, I am currently using GDI to write a simple pacman clone (for a school project). I have run into a slight problem, that I can't seem to figure out. For some reason, my double buffering code is causing my program to slow down, and eventually freeze. It sounds like I am not deleting/releasing objects, and device handles, but as far as I can see I am doing it right. Here is the troublesome code: HDC hdc = GetDC(_hwnd); HDC buffer = CreateCompatibleDC(hdc); HBITMAP memBM = CreateCompatibleBitmap(hdc, _winWidth, _winHeight); SelectObject(buffer, memBM); DeleteObject(memBM); ReleaseDC(_hwnd, hdc); DeleteDC(buffer); Thanks a lot for your help. Let me know if you need anymore information. Edit: Forgot to mention I'm using c++.
'Programming is the only artform that fights back'
Advertisement
Are you making these calls once during initialization/shutdown, or every time you draw? Hopefully not the second one.
I am making the calls during initializing, and shutdown.
'Programming is the only artform that fights back'
You're not deselecting memBM from the buffer. The code should look like this:

HDC hdc = GetDC(_hwnd);HDC buffer = CreateCompatibleDC(hdc);HBITMAP memBM = CreateCompatibleBitmap(hdc, _winWidth, _winHeight);HBITMAP bitmapOld = (HBITMAP)SelectObject(buffer, memBM);SelectObject(buffer, bitmapOld);DeleteObject(memBM);ReleaseDC(_hwnd, hdc);DeleteDC(buffer);


Not sure if that'll fix your issues, though.

Thanks for the replies so far.

I have fixed the deselecting problem. Still hasn't helped though.

I've fooled around a bit, and have gotten just a plain window working (no blitting to it).

As soon as I add:

BitBlt(hdc, 0, 0, 640, 480, buffer, 0, 0, SRCCOPY);

It slows down so much it almost locks up my whole system.
'Programming is the only artform that fights back'
I've figured it out.

Just had to put a small Sleep in my main loop.

Thanks a lot everyone.
'Programming is the only artform that fights back'
Quote:Original post by diablo2_vJust had to put a small Sleep in my main loop.

Umm, from what you described, I don't think just adding a sleep statement into the main loop is going to fix your problem. All that will do is just delay the eventual lock-up you mentioned. I think you still have a problem in the code somewhere... (perhaps with a different piece of code than what was posted earlier?)

This topic is closed to new replies.

Advertisement