Jump to content

GDI and sprites


Ramzes
 Share

Recommended Posts

Hi,

I've searched in help files and AutoIt forums and I found nothing useful for me.

I try to make sprite managing using gdi32.dll.

I know ProSpeed.dll, but it has a lot of bugs and it use gdi32.dll too:

Program --> ProSpeed.dll --> gdi32.dll

I want to create my sprite functions:

Program --> gdi32.dll

It's very userful: MSDN GDI

My program:

#include <WinAPI.au3>
HotKeySet("{ESC}", "Quit")
Global Const $Width = 400, $Height = 200, $ScreenSize = $Width * $Height
Global $hWindow = GUICreate("Test", 400, 200)
GUISetState()
Global $hDC = _WinAPI_GetDC($hWindow)
Global $hDCMem =  _WinAPI_CreateCompatibleDC($hDC)
$Bitmap = _WinAPI_CreateCompatibleBitmap($hDC, $Width, $Height)
While 1
Sleep(10)
WEnd
Func Quit()
_WinAPI_ReleaseDC($hWindow, $hDC)
_WinAPI_DeleteDC($hDC)
Exit
EndFunc

Can someone post me some easy example of using sprite managing (i. e. create 2 images and move they somewhere)?

PS: Please don't post link with other graphical library UDFs.

Edited by Ramzes

Sorry for my bad English but nobody is perfect. [font=arial, helvetica, sans-serif]Ramzes[/font]

Link to comment
Share on other sites

Hi Ramzes

I am trying something but IS NOT WORKING. Maybe someone else can take a look at my atempt and correct something, until that i will keep trying to solve it.

#include <WinAPI.au3>
#include <GDIPlus.au3>
#include <WindowsConstants.au3>
 
Global $sSprite = @ScriptDir & "\SpriteTest.gif"
 
If Not FileExists($sSprite) Then InetGet("http://www.aniway.com/examples/character/Sprites.gif", $sSprite)
 
$hGui = GUICreate("GDI+ Bimap Sprites by monoscout999")
GUISetBkColor(0X123456)
$iPic = GUICtrlCreatePic("", 0, 0, 400, 400)
 
_GDIPlus_Startup()
 
$hGraphics = _GDIPlus_GraphicsCreateFromHWND(GUICtrlGetHandle($iPic))
 
$BackBufferBMP = _GDIPlus_BitmapCreateFromGraphics(400, 400, $hGraphics)
$BackBufferGr = _GDIPlus_ImageGetGraphicsContext($BackBufferBMP)
 
_GDIPlus_GraphicsSetSmoothingMode($hGraphics, 2)
_GDIPlus_GraphicsSetSmoothingMode($BackBufferBMP, 2)
 
$hBMP = _GethBMPFromBitmapFileRect($sSprite, 0, 0, 500, 500)        ; My attempt is returning an empty HBITMAP
$hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
$BackBufferGr2 = _GDIPlus_ImageGetGraphicsContext($hBitmap) ; a test
 
AdlibRegister("Drawing", 100)
 
GUISetState()
 
Do
Until GUIGetMsg() = -3
 
AdlibUnRegister("Drawing")
 
_GDIPlus_GraphicsDispose($BackBufferGr2)
_GDIPlus_BitmapDispose($hBitmap)
_WinAPI_DeleteObject($hBMP)
_GDIPlus_GraphicsDispose($BackBufferGr)
_GDIPlus_BitmapDispose($BackBufferBMP)
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_Shutdown()
 
Func Drawing()
    _GDIPlus_GraphicsClear($BackBufferGr2,0xFF123456)
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, 400, 400)
EndFunc   ;==>Drawing
 
;       This Function Will crop a rect from a ImageFile and return a HBITMAP
;       with the data from the crop (IS NOT WORKING), I take as example for
;       this the _ScreenCapture_Capture() code and edited some trying to
;       addapt it.
Func _GethBMPFromBitmapFileRect($sImageFile, $iX, $iY, $iWidht, $iHeight)
    Local $hImage = _GDIPlus_ImageLoadFromFile($sImageFile)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hImage)
    Local $hDC = _GDIPlus_GraphicsGetDC($hGraphics)     ; Maybe this is the error...
    Local $hCDC = _WinAPI_CreateCompatibleDC($hDC)
    Local $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $iWidht, $iHeight)
    _WinAPI_SelectObject($hCDC, $hBMP)
    _WinAPI_BitBlt($hCDC, 0, 0, $iWidht, $iHeight, $hDC, $iX, $iY, $SRCCOPY)
    _GDIPlus_GraphicsReleaseDC($hGraphics, $hDC)
    _WinAPI_DeleteDC($hCDC)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_ImageDispose($hImage)
    Return $hBMP
    ; _WinAPI_DeleteObject to kill hBMP
EndFunc   ;==>_GethBMPFromBitmapFileRect
Edited by monoscout999
Link to comment
Share on other sites

Do you want to do something like this?

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

HotKeySet("{LEFT}", "_GoThrougTheBMP")
HotKeySet("{RIGHT}", "_GoThrougTheBMP")

Global $iBMPWidth = 120
Global $iBMPHeight = 170
Global $iNavX = 5
Global $iNavY = 370
Global $Step = 1
Global $Pos = 1

Global $sSprite = @ScriptDir & "\SpriteTest.gif"

If Not FileExists($sSprite) Then InetGet("http://www.aniway.com/examples/character/Sprites.gif", $sSprite)

$hGui = GUICreate("GDI+ Bimap Sprites by monoscout999")
GUISetBkColor(0XFFFFFF)
$iPic = GUICtrlCreatePic("", 0, 0, 400, 400)

_GDIPlus_Startup()

$hGraphics = _GDIPlus_GraphicsCreateFromHWND(GUICtrlGetHandle($iPic))

$hBackBufferBMP = _GDIPlus_BitmapCreateFromGraphics(400, 400, $hGraphics)
$hBackBufferGraph = _GDIPlus_ImageGetGraphicsContext($hBackBufferBMP)

_GDIPlus_GraphicsSetSmoothingMode($hGraphics, 2)

$hSpriteBitmap = _GDIPlus_ImageLoadFromFile($sSprite)

AdlibRegister("Drawing", 50)

GUISetState()

Do
Until GUIGetMsg() = -3



_GDIPlus_BitmapDispose($hSpriteBitmap)
_GDIPlus_GraphicsDispose($hBackBufferGraph)
_GDIPlus_BitmapDispose($hBackBufferBMP)
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_Shutdown()

Func Drawing()

    $hBitmap = _GethBMPFromBitmapFileRect($hSpriteBitmap, $iNavX, $iNavY, $iBMPWidth, $iBMPHeight)
    _GDIPlus_GraphicsClear($hBackBufferGraph, 0xFFFFFFFF)
    _GDIPlus_GraphicsDrawImage($hBackBufferGraph, $hBitmap, $Pos, 230)
    _GDIPlus_GraphicsDrawImage($hGraphics, $hBackBufferBMP, 0, 0)
    _GDIPlus_BitmapDispose($hBitmap)
EndFunc   ;==>Drawing

Func _GoThrougTheBMP()
    HotKeySet("{LEFT}")
    HotKeySet("{RIGHT}")
    Switch @HotKeyPressed
        Case "{LEFT}"
            $Step -= 1
            If $Step = 0 Then $Step = 6
        Case "{RIGHT}"
            $Step += 1
            If $Step = 7 Then $Step = 1
    EndSwitch
    Switch $Step
        Case 1
            Switch @HotKeyPressed
                Case "{LEFT}"
                    $Pos -= 20

                Case "{RIGHT}"
                    $Pos += 20
            EndSwitch
            $iBMPWidth = 120
            $iBMPHeight = 170
            $iNavX = 5
            $iNavY = 370
        Case 2
            Switch @HotKeyPressed
                Case "{LEFT}"
                    $Pos -= 20

                Case "{RIGHT}"
                    $Pos += 20
            EndSwitch
            $iBMPWidth = 120
            $iBMPHeight = 170
            $iNavX = 145
            $iNavY = 370
        Case 3
            Switch @HotKeyPressed
                Case "{LEFT}"
                    $Pos -= 20

                Case "{RIGHT}"
                    $Pos += 20
            EndSwitch
            $iBMPWidth = 100
            $iBMPHeight = 170
            $iNavX = 275
            $iNavY = 370
        Case 4
            Switch @HotKeyPressed
                Case "{LEFT}"
                    $Pos -= -20

                Case "{RIGHT}"
                    $Pos += -20
            EndSwitch
            $iBMPWidth = 150
            $iBMPHeight = 170
            $iNavX = 380
            $iNavY = 370
        Case 5
            Switch @HotKeyPressed
                Case "{LEFT}"
                    $Pos -= 40

                Case "{RIGHT}"
                    $Pos += 40
            EndSwitch
            $iBMPWidth = 135
            $iBMPHeight = 170
            $iNavX = 525
            $iNavY = 370
        Case 6
            Switch @HotKeyPressed
                Case "{LEFT}"
                    $Pos -= 20

                Case "{RIGHT}"
                    $Pos += 20
            EndSwitch
            $iBMPWidth = 105
            $iBMPHeight = 170
            $iNavX = 650
            $iNavY = 370
        EndSwitch
        HotKeySet("{LEFT}", "_GoThrougTheBMP")
        HotKeySet("{RIGHT}", "_GoThrougTheBMP")
EndFunc   ;==>_GoThrougTheBMP

Func _GethBMPFromBitmapFileRect($hSpriteBitmap, $iX, $iY, $iWidth, $iHeight)
    Local $hDestImg = _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight)
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hDestImg)
    _GDIPlus_GraphicsDrawImage($hGraphics, $hSpriteBitmap, -$iX, -$iY)
    _GDIPlus_GraphicsDispose($hGraphics)
    Return $hDestImg
EndFunc   ;==>_GethBMPFromBitmapFileRect

Func _GDIPlus_BitmapCreateFromScan0($iWidth, $iHeight, $iStride = 0, $iPixelFormat = 0x0026200A, $pScan0 = 0)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[6]
EndFunc   ;==>_GDIPlus_BitmapCreateFromScan0
Link to comment
Share on other sites

Try this:

#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
 
Opt("GUIOnEventMode", 1)
Global $hBmp_Background = _WinAPI_LoadImage(0, @ScriptDir & "\Kaffeetasse.bmp", $IMAGE_BITMAP, 0, 0, $LR_LOADFROMFILE)
Global $aMario[3]
$aMario[0] = _LoadSprite(@ScriptDir & "\Mario1.bmp")
$aMario[1] = _LoadSprite(@ScriptDir & "\Mario2.bmp")
$aMario[2] = _LoadSprite(@ScriptDir & "\Mario3.bmp")
Global $hGui = GUICreate("", 320, 240)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
Global $hDC = _WinAPI_GetDC($hGui)
Global $hDC_Buffer = _WinAPI_CreateCompatibleDC($hDC)
Global $hBmp_Buffer = _WinAPI_CreateCompatibleBitmap($hDC, 320, 240)
_WinAPI_SelectObject($hDC_Buffer, $hBmp_Buffer)
Global $hDC_Draw = _WinAPI_CreateCompatibleDC($hDC)
GUIRegisterMsg($WM_PAINT, "WM_PAINT")
GUIRegisterMsg($WM_ERASEBKGND, "WM_PAINT")
GUISetState()
_Draw()
_DrawSprite($hDC_Buffer, $aMario[0], 0, 0, 10)
_WinAPI_BitBlt($hDC, 0, 0, 320, 240, $hDC_Buffer, 0, 0, $SRCCOPY)
MsgBox(0, "note", "Sprite has transparent pixels! although it was loaded from a normal BMP-file")
While Sleep(40)
_Draw()
WEnd
 
Func _DrawSprite($hDC, $aSprite, $iX, $iY, $fScale = 1)
If UBound($aSprite) <> 3 Then Return SetError(1, 1, False)
Return _WinAPI_DrawIconEx($hDC, $iX, $iY, $aSprite[0], $aSprite[1] * $fScale, $aSprite[2] * $fScale)
EndFunc   ;==>_DrawSprite
 
Func _LoadSprite($sFilename)
Local $hBMP = _WinAPI_LoadImage(0, $sFilename, $IMAGE_BITMAP, 0, 0, $LR_LOADFROMFILE)
If @error Or Not $hBMP Then Return SetError(1, 1, False)
Local $tBitmap = DllStructCreate("long bmType; long bmWidth; long bmHeight; long bmWidthBytes; ushort bmPlanes; ushort bmBitsPixel; ptr bmBits;")
Local $aResult = DllCall("gdi32.dll", "int", "GetObject", "hwnd", $hBMP, "int", DllStructGetSize($tBitmap), "ptr", DllStructGetPtr($tBitmap))
Local $tIcon = DllStructCreate("bool Icon; dword XHotSpot; dword YHotSpot; handle hMask; handle hColor")
DllStructSetData($tIcon, 1, True)
DllStructSetData($tIcon, 2, 0)
DllStructSetData($tIcon, 3, 0)
DllStructSetData($tIcon, 4, $hBMP)
DllStructSetData($tIcon, 5, $hBMP)
$aResult = DllCall("user32.dll", "ptr", "CreateIconIndirect", "ptr", DllStructGetPtr($tIcon))
If @error Or Not $aResult[0] Then
  _WinAPI_DeleteObject($hBMP)
  Return SetError(1, 2, False)
EndIf
_WinAPI_DeleteObject($hBMP)
Local $aReturn[3]
$aReturn[0] = $aResult[0]
$aReturn[1] = DllStructGetData($tBitmap, "bmWidth")
$aReturn[2] = DllStructGetData($tBitmap, "bmHeight")
Return $aReturn
EndFunc   ;==>_LoadSprite
 
Func _DestroySprite(ByRef $aSprite)
If UBound($aSprite) <> 3 Then Return SetError(1, 1, False)
_WinAPI_DestroyIcon($aSprite[0])
$aSprite = 0
EndFunc   ;==>_DestroySprite
 
Func _Draw()
_WinAPI_SelectObject($hDC_Draw, $hBmp_Background)
For $x = 0 To 320 Step 128
  For $y = 0 To 240 Step 128
   _WinAPI_BitBlt($hDC_Buffer, $x, $y, 128, 128, $hDC_Draw, 0, 0, $SRCCOPY)
  Next
Next
Local Static $iStep = 0, $iX = 10
$iStep += 1
If $iStep > 7 Then $iStep = 0
$iX += 2
If $iX > 320 Then $iX = -30
If $iStep < 6 Then
  _DrawSprite($hDC_Buffer, $aMario[Floor($iStep / 2)], $iX, 160, 2)
Else
  _DrawSprite($hDC_Buffer, $aMario[1], $iX, 160, 2)
EndIf
_WinAPI_BitBlt($hDC, 0, 0, 320, 240, $hDC_Buffer, 0, 0, $SRCCOPY)
EndFunc   ;==>_Draw
 
Func WM_PAINT($hWnd, $uMsgm, $wParam, $lParam)
_WinAPI_BitBlt($hDC, 0, 0, 320, 240, $hDC_Buffer, 0, 0, $SRCCOPY)
Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_PAINT
 
Func _Exit()
_DestroySprite($aMario[0])
_DestroySprite($aMario[1])
_DestroySprite($aMario[2])
_WinAPI_DeleteObject($hBmp_Buffer)
_WinAPI_DeleteObject($hBmp_Background)
_WinAPI_DeleteDC($hDC_Draw)
_WinAPI_DeleteDC($hDC_Buffer)
_WinAPI_ReleaseDC($hGui, $hDC)
Exit
EndFunc   ;==>_Exit

Images and Code here:

http://www.autoit.de/index.php?page=Atta...348629663371c46c8f862ad5f36fa5

E

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...