Advertisement

Windows GDI sprite drawing (tearing)

Started by June 16, 2001 12:54 PM
5 comments, last by Ridcully 23 years, 7 months ago
I have to code a little game for a small company. It should be fairly easy with really simple artwork but should work on older systems/machines. Additionally I only have about a week to code it. So I decided to go for Windows GDI because it is most compatible. Now I thought that moving a sprite around the screen with BitBlt shouldn''t work too bad, but it does. The sprite (64x64) tears really badly. Is there a way to prevent this? I can post up some sample code, but what I am basically doing is only loading the sprite and drawing it every 10 milliseconds with BitBlt. Thanks for any suggestions Ridcully
Slow it down a bit. Make sure the sprite isn''t being redrawn faster the refresh rate of the display. Being that the GDI has no vsync function, it is best if you keep it proportional, if the display is 120hz, try and draw at 120hz, 60hz, or even 30hz. Otherwise there will be tearing on a different line every frame.
Advertisement
Are you drawing to the main window (that would explain some tearing)?

Try drawing to a backbuffer first and flipping only once the drawing has been completed. That should prevent it from looking like its tearing.
slowing down didn''t help. i am only drawing the sprite 5 times a second, and it still tears like hell...

Nytegard: yes of course i am drawing directly to the window. is there a way that i can get GDI to flip the main window surface? i thought this wouldn''t be possible.


there must be a way to make this work, as many windowed software that doesn''t use dx doesn''t suffer from tearing.
please help me, i have to finish the program until friday...

thanks
ridcully
Are you clearing the whole window between frames?

if so, try to just draw over the sprite you just drew with a tile that is the same size, but matches the background. That is all i can suggest, GDI is slow, and i wish you the best of luck when trying to get it to run fast enough to look nice.
There is no way to use page flipping in GDI, but there is a way around it, emulate it if you want.
I asume you are BitBlt''ing each HBitmap directly to the main window DC rigth?
no good, what you have to do is this:

RECT rect;
GetClientRect(Main_Window_Handle,▭);
HDC MainDC = GetDC(Main_Window_Handle);
HDC BackDC = CreateCompatibleDC(MainDC);
HBITMAP BackBuffer = CreateCompatibleBitmap(BackDC,rect.right,rect.bottom);
HBITMAP OldBMP = SelectObject(BackDC,BackBuffer);

Do_all_your_bliting_drawtos_set_pixel_GDI_drawing_to_BackDC();

BitBlt(MainDC,0,0,rect.right,rect.bottom,BackDC,0,0,SRCCOPY)
//this is what does the sort of page flipping effect.

ReleaseDC(Main_Window_Handle,Main_DC);
BackBuffer = SelectObject(BackDC,OldBMP);
DeleteDC(BackDC);
DeleteObject(BackBuffer);

and thats it, I am not doing a game with GDI, but this is how I repaint when needed on my level editor.

Hope it helps you.
Advertisement
Try this out


HDC hdc, memDC;
GetDC(hdc);
memDC = CreateCompatibleDC(hdc);
//Load the image into an HDC operator class
class BITMAP
{
public:
operator HDC();
// more stuff
}
//in the loop
BitBlt(memDC,0,0,bitWidth,BitHeight,BITMAP,0,0,SRCCOPY);

really you could use directX and it would take a day max if you now the api

==--When the time comes you will realize but no sooner.--==
==--When the time comes you will realize but no sooner.--==

This topic is closed to new replies.

Advertisement