maestro Posted December 27, 2010 Share Posted December 27, 2010 (edited) Hi, I am currently working on a project with GDI+ and am experiencing terrible flickering on upon refreshing the window, I have seen scripts that use GDI+ at high frame rate without flickering. I have looked through these scripts and am still unable to figure out what it is that keeps the GDI from flickering.This is one of the projects I am working on that is having this problem, its not so noticeable at a low score but as the snake gets bigger it flickers really bad.Snake game: Snake.exe DownloadAlso if any one has any suggestions on how I could improve the game let me know.If size is an issue, its a quick fix this is how every thing scales:Global $guisize = 500Global $resolution = 50$resolution is how many squares across and down, and size is in px across and down.Thanks,Ilmaestro. Edited December 27, 2010 by maestro Hello, World!... LAME lol Link to comment Share on other sites More sharing options...
UEZ Posted December 27, 2010 Share Posted December 27, 2010 You can use the backbuffer method to avoid flickering! ... $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI) $hBitmap = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $hGraphic) $hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap) ... <draw everything to $hBackbuffer> _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $iW, $iH) ;copy from backbuffer to visible graphic ... Don't forget to dispose all the GDI+ stuff when closing. Br, UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
maestro Posted December 27, 2010 Author Share Posted December 27, 2010 (edited) Thanks for the Reply UEZ, however could you break down your example so I understand what is going on in it. I have just started playing around with GDI and am still un-sure of what alot of the stuff actually does. Also I have been doing the GDI clean up on exit, however why is it actually necessary? Oh and in what section of the script should i add this back buffer thing into, before of after the graphic gets cleared. Thanks, Ilmaestro. Edited December 27, 2010 by maestro Hello, World!... LAME lol Link to comment Share on other sites More sharing options...
UEZ Posted December 27, 2010 Share Posted December 27, 2010 Here a very simple example: expandcollapse popup#Include <GDIPlus.au3> Opt("GUIOnEventMode", 1) _GDIPlus_Startup() $width = 800 $height = 200 $hGUI = GUICreate("GDI+", $width, $height) $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI) $hBitmap = _GDIPlus_BitmapCreateFromGraphics($width, $height, $hGraphic) $hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap) _GDIPlus_GraphicsSetSmoothingMode($hBackbuffer, 2) _GDIPlus_GraphicsClear($hBackbuffer, 0xFFF0F0F0) GUISetState() GUISetOnEvent(-3, "_Exit") $hBrush = _GDIPlus_BrushCreateSolid(0xFFFF0000) $eSize = 100 $x = $width While Sleep(40) _GDIPlus_GraphicsClear($hBackbuffer, 0xFFF0F0F0) _GDIPlus_GraphicsFillEllipse($hBackbuffer, $x, $height / 2 - $eSize / 2, $eSize, $eSize, $hBrush) If $x = -$eSize Then $x = $width $x -= 2 _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $width, $height) WEnd Func _Exit() _GDIPlus_BrushDispose($hBrush) _GDIPlus_GraphicsDispose($hBackbuffer) _GDIPlus_BitmapDispose ($hBitmap) _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_Shutdown() GUIDelete($hGUI) Exit EndFunc Br, UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
maestro Posted December 27, 2010 Author Share Posted December 27, 2010 (edited) Still getting bad flashing, Here's my GUI creation section: $gdigui = GUICreate("<Snake> Score: "&$GUI_title, $guisize + 1, $guisize + 1, -1, -1,-1, $WS_EX_LAYERED) _GDIPlus_Startup() $hGraphic = _GDIPlus_GraphicsCreateFromHWND($gdigui) $hPen_On = _GDIPlus_PenCreate(0xFFFF0000, 1, 5) $hPen_Off = _GDIPlus_PenCreate(0xFF0000FF, 1, 5) $hPen_Think = _GDIPlus_PenCreate(0xFF00FF00, 1, 5) $Snake_Brush = _GDIPlus_BrushCreateSolid(0xFFFF0000) $Food_Brush = _GDIPlus_BrushCreateSolid(0xFF0000FF) $hBitmap = _GDIPlus_BitmapCreateFromGraphics($guisize, $guisize, $hGraphic) $hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap) _GDIPlus_GraphicsSetSmoothingMode($hBackbuffer, 2) _GDIPlus_GraphicsClear($hBackbuffer, 0xFFF0F0F0) GUISetState(@SW_SHOW) And Here's my redraw section: _GDIPlus_GraphicsClear($hBackbuffer, 0xFFF0F0F0) ;clears it just befor redrawing so it can draw with high accuracy _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $guisize, $guisize) _GDIPlus_GraphicsDrawLine($hGraphic,$iX* ($guisize / $resolution),$iY* ($guisize / $resolution),$FoodX* ($guisize / $resolution) + ($guisize / $resolution)/2,$FoodY* ($guisize / $resolution) + ($guisize / $resolution)/2,$hPen_Think) For $Y = 0 To $resolution-1 For $X = 0 To $resolution-1 If $array[$X][$Y] > 0 And $array[$X][$Y] < $value $array[$X][$Y] += 1 $solid_Cnt += 1 _GDIPlus_GraphicsFillRect($hBackbuffer, $X* ($guisize / $resolution),$Y* ($guisize / $resolution),($guisize / $resolution),($guisize / $resolution),$Snake_Brush) ElseIf $array[$X][$Y] = -100 Then _GDIPlus_GraphicsFillRect($hBackbuffer, $X* ($guisize / $resolution),$Y* ($guisize / $resolution),($guisize / $resolution),($guisize / $resolution),$Food_Brush) Else $array[$X][$Y] = 0 EndIf Next Next _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $guisize, $guisize) Thanks, Ilmaestro. Edited December 27, 2010 by maestro Hello, World!... LAME lol Link to comment Share on other sites More sharing options...
UEZ Posted December 27, 2010 Share Posted December 27, 2010 Try this in your redraw section: _GDIPlus_GraphicsClear($hBackbuffer, 0xFFF0F0F0) ;clears it just befor redrawing so it can draw with high accuracy ;~ _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $guisize, $guisize) _GDIPlus_GraphicsDrawLine($hBackbuffer,$iX* ($guisize / $resolution),$iY* ($guisize / $resolution),$FoodX* ($guisize / $resolution) + ($guisize / $resolution)/2,$FoodY* ($guisize / $resolution) + ($guisize / $resolution)/2,$hPen_Think) For $Y = 0 To $resolution-1 For $X = 0 To $resolution-1 If $array[$X][$Y] > 0 And $array[$X][$Y] < $value $array[$X][$Y] += 1 $solid_Cnt += 1 _GDIPlus_GraphicsFillRect($hBackbuffer, $X* ($guisize / $resolution),$Y* ($guisize / $resolution),($guisize / $resolution),($guisize / $resolution),$Snake_Brush) ElseIf $array[$X][$Y] = -100 Then _GDIPlus_GraphicsFillRect($hBackbuffer, $X* ($guisize / $resolution),$Y* ($guisize / $resolution),($guisize / $resolution),($guisize / $resolution),$Food_Brush) Else $array[$X][$Y] = 0 EndIf Next Next _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $guisize, $guisize) Br, UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
maestro Posted December 27, 2010 Author Share Posted December 27, 2010 (edited) Awesome works perfect Thanks UEZAnd for any one who many be interested, here's my new noflicker(thanks to UEZ) snake game: Snake Download.Ilmaestro. Edited December 27, 2010 by maestro Hello, World!... LAME lol Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now