GDI functions cause flicker

BlitzPlus Forums/BlitzPlus Programming/GDI functions cause flicker

Recently I have been playing with some of the drawing functions in gdi32.dll. I've found the rect and line functions are at least two times faster than native b+ commands. The only problem I have is that the shapes flicker, especially in fullscreen. Can someone verify they get the same behavior and hopefully offer a solution?

Userlib:
.lib "user32.dll"
GetDC%(hwnd%)
ReleaseDC%(hwnd%, hdc%)
GetActiveWindow%()
RedrawWindow%(hwnd%, lprcUpdate%, hrgnUpdate%, fuRedraw%)
UpdateWindow%(hWnd%)

.lib "gdi32.dll"
CreatePen%(nPenStyle%, nWidth%, crColor%)
DeleteObject%(hObject%)
SelectObject%(hdc%, hObject%)
Rectangle%(hdc%, X1%, Y1%, X2%, Y2%)

(make sure to erase duplicate declarations in your other decls files)

b3d/b+ code:
Graphics 640, 480, 16, 1

; // Get Device Context
hWnd% = GetActiveWindow()
Global DC% = GetDC(hWnd%)
If Not DC% Then End  ; Failed
ReleaseDC hWnd%, DC% ; Release

; // Pen Styles
Const PS_SOLID       = 0
Const PS_DOT         = 2

Const TB_RED   = 255 
Const TB_BLUE  = 16711680
Const TB_GREEN = 65280
 
Local fps_Count = 0, fps_Current = 0, fps_Time# = MilliSecs() + 1  ; FPS Counter
                 
turbo_Pen PS_SOLID, 5, TB_RED

tmpX = 50: tmpY = 50
While Not KeyDown(1)
Cls
fps_Count = fps_Count + 1
If MilliSecs() >= fps_Time Then fps_Current = fps_Count: fps_Count = 0: fps_Time = fps_Time + 1000

Turbo_Rect 50, 50, tmpX, tmpY ; Draw Rect with api

tmpX = tmpX + 1: tmpY = tmpY + 1

If KeyDown(1) Then End

Color 255, 255, 255: Text 1, 1, "FPS: " + fps_Current

;RedrawWindow hWnd, 0, 0, 1
;UpdateWindow hWnd
Flip 1
Wend: End

Function Turbo_Rect(X, Y, W, H)
Rectangle DC%, X, Y, X + W, Y + H
End Function

Function turbo_Pen(Style, Width, CrCol)
DeleteObject tb_Pen
tb_Pen = CreatePen(Style, Width, CrCol)
DeleteObject SelectObject(DC%, tb_Pen)
End Function


I tried Redraw and UpdateWindow but they didnt help with the flicker.

Haven't tested your code but it's most likely the windows background color (white flicker when window classes' hbrBackground set to white pen).

Maybe try it with a panel hwnd?

Tried it with a panel and it still flickers :/

The above example is fullscreen b3d though, I really want it to work in both.

I will investigate the background thing now...

you should also be calling GetDC and ReleaseDC around your rendering code

k, added that. Is the background color of a b3d window really something to worry about? If it still flickered on a panel than I don't know if its worth the trouble. Changing styles is a pain and I hate messing with SetWindowLong...

GDI functions are not double buffered. You can create a virtual DC, draw on it and then copy it to your window.

That would explain it :p

I'll try that then, thanks.