Sign in to follow this  
Followers 0
zwierzak

GDI+ buffering problem

5 posts in this topic

#1 ·  Posted (edited)

Hello. I've got a problem with buffering. I have read something about it, but all the examples are so complicated and long, so I don't really understand them. I'd like to optimize the refresh rate of my animation.I wonder if you could do it for me in the most simple way so I can fully get it. Please have a look:

#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <GuiConstants.au3>
#include <WindowsConstants.au3>
#include <Timers.au3>
Global $GuiSizeX = 500, $GuiSizeY = 500, $var = True

$hGui = GUICreate("GDIPlus Example", $GuiSizeX, $GuiSizeY)
$hGui2 = GUICreate("GDIPlus Example", $GuiSizeX, $GuiSizeY)
GUISetState(@SW_SHOW, $hGui)

_GDIPlus_Startup()

; Create Double Buffer
$hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hGui)
$hBMPBuff = _GDIPlus_BitmapCreateFromGraphics($GuiSizeX, $GuiSizeY, $hGraphicGUI)
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hBMPBuff)
;End Double Buffer

;Graphics here
_GDIPlus_GraphicsClear($hGraphic, 0xFFE8FFE8)
$hPen = _GDIPlus_PenCreate(0xFFFF0000, 2)
;End of graphics

_GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)

$iTimer = _Timer_SetTimer($hGui, 30, "MY_PAINT"); create timer

While 1

    $hGraphicGUI2 = _GDIPlus_GraphicsCreateFromHWND($hGui2)
$hBMPBuff2 = _GDIPlus_BitmapCreateFromGraphics($GuiSizeX, $GuiSizeY, $hGraphicGUI2)
$hGraphic2 = _GDIPlus_ImageGetGraphicsContext($hBMPBuff2)
draw1()
_GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff2, 0, 0)
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
        ;_GDIPlus_BrushDispose($hBrush)
            _GDIPlus_PenDispose($hPen)
            _Timer_KillTimer($hGui, $iTimer)
            _GDIPlus_GraphicsDispose($hGraphic)
            _GDIPlus_GraphicsDispose($hGraphicGUI)
            _WinAPI_DeleteObject($hBMPBuff)
            _GDIPlus_Shutdown()

            Exit
    EndSwitch
WEnd

Func draw1()
        $pos = MouseGetPos()
    _GDIPlus_GraphicsDrawEllipse ($hGraphic2, 100, 70, 50, 50, $hPen) ;head
    _GDIPlus_GraphicsDrawLine ($hGraphic2, 125, 120, 125, 200, $hPen) ;body

    _GDIPlus_GraphicsDrawLine ($hGraphic2, 125, 135, 100, 160, $hPen) ;left arm
    _GDIPlus_GraphicsDrawLine ($hGraphic2, 100, 160, 170, $pos[1], $hPen);left forearm

    _GDIPlus_GraphicsDrawLine ($hGraphic2, 125, 120, 125, 200, $hPen) ;right arm
    _GDIPlus_GraphicsDrawLine ($hGraphic2, 125, 120, 125, 200, $hPen);right forearm

    _GDIPlus_GraphicsDrawLine ($hGraphic2, 125, 200, 90, 280, $hPen) ;left leg
    _GDIPlus_GraphicsDrawLine ($hGraphic2, 125, 200, 160, 280, $hPen) ;right leg
EndFunc

Func MY_PAINT($hWnd, $msg, $wParam, $lParam)
    _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
    _WinAPI_RedrawWindow($hGui, "", "", BitOR($RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_FRAME)); , $RDW_ALLCHILDREN
EndFunc  ;==>MY_PAINT
Edited by zwierzak

Share this post


Link to post
Share on other sites



Try this:

#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <GuiConstants.au3>
#include <WindowsConstants.au3>
#include <Timers.au3>
Global $GuiSizeX = 500, $GuiSizeY = 500, $var = True

$hGui = GUICreate("GDIPlus Example", $GuiSizeX, $GuiSizeY)
$hGui2 = GUICreate("GDIPlus Example", $GuiSizeX, $GuiSizeY)
GUISetState(@SW_SHOW, $hGui)

_GDIPlus_Startup()

; Create Double Buffer
$hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hGui)
$hBMPBuff = _GDIPlus_BitmapCreateFromGraphics($GuiSizeX, $GuiSizeY, $hGraphicGUI)
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hBMPBuff)
;End Double Buffer

;Graphics here
$hPen = _GDIPlus_PenCreate(0xFFFF0000, 2)
;End of graphics

GUIRegisterMsg($WM_ERASEBKGND, "WM_ERASEBKGND")

While 1
draw1()
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            GUIRegisterMsg($WM_ERASEBKGND, "")
        ;_GDIPlus_BrushDispose($hBrush)
            _GDIPlus_PenDispose($hPen)
            _GDIPlus_GraphicsDispose($hGraphic)
            _GDIPlus_GraphicsDispose($hGraphicGUI)
            _WinAPI_DeleteObject($hBMPBuff)
            _GDIPlus_Shutdown()
            Exit
    EndSwitch
WEnd

Func draw1()
     _GDIPlus_GraphicsClear($hGraphic, 0xFFE8FFE8)
        $pos = MouseGetPos()
    _GDIPlus_GraphicsDrawEllipse ($hGraphic, 100, 70, 50, 50, $hPen) ;head
    _GDIPlus_GraphicsDrawLine ($hGraphic, 125, 120, 125, 200, $hPen) ;body

    _GDIPlus_GraphicsDrawLine ($hGraphic, 125, 135, 100, 160, $hPen) ;left arm
    _GDIPlus_GraphicsDrawLine ($hGraphic, 100, 160, 170, $pos[1], $hPen);left forearm

    _GDIPlus_GraphicsDrawLine ($hGraphic, 125, 120, 125, 200, $hPen) ;right arm
    _GDIPlus_GraphicsDrawLine ($hGraphic, 125, 120, 125, 200, $hPen);right forearm

    _GDIPlus_GraphicsDrawLine ($hGraphic, 125, 200, 90, 280, $hPen) ;left leg
    _GDIPlus_GraphicsDrawLine ($hGraphic, 125, 200, 160, 280, $hPen) ;right leg
    _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
EndFunc

Func WM_ERASEBKGND($hWnd, $msg, $wParam, $lParam)
    _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
    Return 1
EndFunc  ;==>MY_PAINT

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thank you very much. That was exactly what I was looking for!

@EDIT

Hmm it uses about 20% of my processor. Is it normal for this kind of procedure?

Edited by zwierzak

Share this post


Link to post
Share on other sites

If you don't reduce CPU usage then it is "normal".

Here with sleep()

#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <GuiConstants.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)
Global $GuiSizeX = 500, $GuiSizeY = 500, $var = True

$hGui = GUICreate("GDIPlus Example", $GuiSizeX, $GuiSizeY)
$hGui2 = GUICreate("GDIPlus Example", $GuiSizeX, $GuiSizeY)
GUISetState(@SW_SHOW, $hGui)

_GDIPlus_Startup()

; Create Double Buffer
$hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hGui)
$hBMPBuff = _GDIPlus_BitmapCreateFromGraphics($GuiSizeX, $GuiSizeY, $hGraphicGUI)
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hBMPBuff)
;End Double Buffer

;Graphics here
$hPen = _GDIPlus_PenCreate(0xFFFF0000, 2)
;End of graphics
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

GUIRegisterMsg($WM_ERASEBKGND, "WM_ERASEBKGND")

While Sleep(50)
    draw1()
WEnd

Func draw1()
     _GDIPlus_GraphicsClear($hGraphic, 0xFFE8FFE8)
        $pos = MouseGetPos()
    _GDIPlus_GraphicsDrawEllipse ($hGraphic, 100, 70, 50, 50, $hPen) ;head
    _GDIPlus_GraphicsDrawLine ($hGraphic, 125, 120, 125, 200, $hPen) ;body

    _GDIPlus_GraphicsDrawLine ($hGraphic, 125, 135, 100, 160, $hPen) ;left arm
    _GDIPlus_GraphicsDrawLine ($hGraphic, 100, 160, 170, $pos[1], $hPen);left forearm

    _GDIPlus_GraphicsDrawLine ($hGraphic, 125, 120, 125, 200, $hPen) ;right arm
    _GDIPlus_GraphicsDrawLine ($hGraphic, 125, 120, 125, 200, $hPen);right forearm

    _GDIPlus_GraphicsDrawLine ($hGraphic, 125, 200, 90, 280, $hPen) ;left leg
    _GDIPlus_GraphicsDrawLine ($hGraphic, 125, 200, 160, 280, $hPen) ;right leg
    _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
EndFunc

Func WM_ERASEBKGND($hWnd, $msg, $wParam, $lParam)
    _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
    Return 1
EndFunc  ;==>MY_PAINT

Func _Exit()
    GUIRegisterMsg($WM_ERASEBKGND, "")
    ;_GDIPlus_BrushDispose($hBrush)
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_GraphicsDispose($hGraphicGUI)
    _WinAPI_DeleteObject($hBMPBuff)
    _GDIPlus_Shutdown()
    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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thank you again! Now it works just perfect for me. I really appreciate your help :graduated:

Btw. Now I understand how does it work :(

Edited by zwierzak

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0