Jump to content

[Solved] Making 2D sprites UDF


Ramzes
 Share

Recommended Posts

Hi,

I want to create the fastest 2D sprites UDF for AutoIt but I need your help.

The best known me library is ProSpeed.dll but there are lots of bugs and we don't need additional library. We've to know how it works and make the same better.

Here is a list of ProSpeed functions:

AddWindowStyle
AlphaTrans
Antialiasing
AttachSprite
AttachSpriteAnim
BitBltArray
BlackWhite
Blur
BringSpriteToBottom
BringSpriteToTop
ChangeBytesToWords
ChangeDisplay
ChangeLongSequence
ChangeSpritePara
ChangeWordsToLongs
CleanUp
ClearWith
CloneFX
Collide
CollideAll
CollideMore
CollideUnknown
Collision
ColorFill
ColorFillImage
ColorMove
CompareBytes
Compute
CopyFX
CopyMemory
CopyMemoryFromBack
CopySprite
CountAllSprites
CountBytes
CountFirstBytes
CountJoystickButtons
CountLongs
CountSprites
CountStrings
CountWords
CreateExtFX
CreateImage
Crypt
Curve
Darken
DataBytes
DataMem
DataPos
DeInitPixelEffects
DeleteAllSprites
DeleteAllXSprites
DeleteAllYSprites
DeleteSprite
DeleteSpritesIfAnimReady
DeleteSpritesMark
DeleteTags
DePack
DownloadHttpFile
DSoundDeInit
DSoundGetInterfaces
DSoundGetNextBuffer
DSoundGetStatus
DSoundInit
DSoundLoad
DSoundLoadMemory
DSoundLoadResource
DSoundPlay
DSoundSetGlobalPan
DSoundSetGlobalVolume
DSoundSetPan
DSoundSetVolume
DSoundStop
DSoundUnLoad
Examine
ExchangeColor
ExchangeRgb
ExistSprite
ExportPng
FindBytes
FindBytesCount
FindJoystick
FindPath
FlipX
FlipY
Fog
Frame
FreeAllImages
FreeFX
FreeImage
FreePng
FXToPng
GenerateChecksum
GetAByte
GetBackgroundInfos
GetHeightImage
GetInfosImage
GetMemHandle
GetOriginalSize
GetSpriteAnimModeStatus
GetSpriteFixMode
GetSpriteInfos
GetSpriteLayer
GetSpriteMark
GetSpriteMoveX
GetSpriteMoveXY
GetSpriteMoveY
GetSpritesFps
GetSpriteX
GetSpriteY
GetSystemKey
GetVersion
GetWidthImage
Grey
HasSpriteArrived
ImportPng
InitFX
InitPixelEffects
InitSprite
InitSprite5
InitSpriteBackground
InvertChannelPng
IsInternet
JoystickButton
JoystickR
JoystickU
JoystickV
JoystickX
JoystickY
JoystickZ
Lighten
LoadFileImage
LoadMemoryImage
LoadResourceImage
LongToAddrVar4
MarkSprite
Merge
MergeChannelPng
MergeImagePng
MouseButton
MouseOverSprite
MoveSprite
MoveSpriteWithTable
NoStars
OemBytes
OptionStars
Pack
PaintImage
PaintPng
PixelToMemory
PMouse
PngToFX
PSRegister
Random
ReadFileFast
ReadFilePieceFast
ReadWatch
Replace
ReplaceTabs
Rotate
Rotate180
RotateImage
RotateMem
Rustle
SaveFX
SaveImage
SaveToMemoryFX
SemiTrans
SetAByte
SetBackAutoCollision
SetBytes
SetCollideUnknownSprite
SetLongs
SetMouseRect
SetMouseXY
SetPixelEffect
SetSpriteAnim
SetSpriteAnimMode
SetSpriteAnimMove
SetSpriteCage
SetSpriteFixMode
SetSpriteLayer
SetSpriteMovingMode
SetSpritePos
SetSpriteSpeed
SetTiles
SetTiles5
SetWords
Sharpen
SizeImage
SlowDownSprite
Smooth
SmoothPath
SpriteMovingRect
SpriteScrollMode
SpriteTableMode
SpriteThreadBrake
SpriteToHDC
Stars
StartWatch
StatusStars
StopAllSprites
SubWindowStyle
SwapSpriteLayers
SwitchTaskbar
SystemThreadBrake
TestSprite
Tooltip
VarToLong10
VarToLong4
Version
VisualTableFix
WaitWatch
Water
WriteFileFast

ProSpeed imports:

GDI32.DLL:

BitBlt
CreateBitmap
CreateCompatibleBitmap
CreateCompatibleDC
CreateDIBSection
CreateDIBitmap
CreatePen
CreateSolidBrush
DeleteDC
DeleteObject
GdiFlush
GetDIBits
GetDeviceCaps
GetPixel
LineTo
MoveToEx
PolyBezier
SelectObject
SetBkColor
SetDIBitsToDevice
SetPixel
SetStretchBltMode
StretchBlt

USER32.DLL:

ChangeDisplaySettingsA
ClipCursor
CreateWindowExA
EnumDisplaySettingsA
FillRect
FindWindowA
GetActiveWindow
GetAsyncKeyState
GetCursorPos
GetDC
GetKeyState
GetWindowLongA
LoadBitmapA
MessageBoxA
PtInRect
ReleaseDC
ScreenToClient
SendMessageA
SetCursorPos
SetWindowLongA
SetWindowPos
ShowWindow

KERNEL32.DLL

DSOUND.DLL

OLE32.DLL

OLEAUT32.DLL

COMCTL32.DLL

WININET.DLL

URLMON.DLL

To display sprite we must call "InitSprite". But what does this function call in GDI32 or other MS libraries? I don't know.

ProSpeed is in attachment.

If someone can help me with making this UDF - please post.

/Ramzes

ProSpeed.dll

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

ProSpeed uses DIB functions:

CreateDIBitmap

Creates a device-dependent bitmap (DDB) from a DIB.

CreateDIBSection

Creates a DIB that applications can write to directly.

GetDIBits

Copies a bitmap into a buffer.

SetDIBitsToDevice

Sets the pixels in a rectangle using color data from a DIB.

Now we need to know how use these functions.

My code:

#include <WinAPI.au3>
#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
 
Opt("GUIOnEventMode", 1)
 
HotKeySet("{ESC}", "Quit")
Global Const $Width = 1200, $Height = 800, $Size = $Width * $Height
$hWnd = _WinAPI_CreateWindowEx(0, "AutoIt v3 GUI", "Test", 0, 0, 0, $Width, $Height, 0)
WinSetState($hWnd, "", @SW_SHOW)
$hDC = _WinAPI_GetDC($hWnd)
ConsoleWrite("hDC: " & $hDC & @CR)
; Please draw random image in Paint and save as "1.bmp"
$hBitmap = _WinAPI_LoadImage(0, "1.bmp", $IMAGE_BITMAP, 0, 0, $LR_LOADFROMFILE)
ConsoleWrite("hBitmap: " & $hBitmap & @CR)
$hdCMem = _WinAPI_CreateCompatibleDC($hDC)
$hCmpBitmap = _WinAPI_CreateCompatibleBitmap($hDC, 32, 32)
ConsoleWrite("hCmpBitmap: " & $hBitmap & @CR)
_WinAPI_SelectObject($hdCMem, $hCmpBitmap)
Global $tBits = DllStructCreate("int[" & 1024 & "]")
Global $pBits = DllStructGetPtr($tBits)
$tBitmap = DllStructCreate("long bmType; long bmWidth; long bmHeight; long bmWidthBytes; ushort bmPlanes; ushort bmBitsPixel; ptr bmBits;")
$pBitmap = DllStructGetPtr($tBitmap)
$Return = _WinAPI_GetDIBits($hDC, $hCmpBitmap, 5, 32, $pBits, $pBitmap, 0)
ConsoleWrite("Return: " & $Return & @CR)
 
While 1
Sleep(10)
WEnd
 
Func Quit()
Exit
EndFunc

I need little help with BITMAPINFO.

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

@Ramzes you are not disposing any element in your example... and i don´t know why you use _Winapi_CreateWindowEx() instead of a regular GUI.

I dont know what you mean with fastest 2d Sprites.. fastest compared with what(?)

BTW this is a good idea... I was thinking doing some Sprites Manangment UDF(but using GDIPLUS.DLL) by myself a while ago.

Edited by monoscout999
Link to comment
Share on other sites

@taietel

This UDF is good but only 336 of 728 functions have examples (I've bad luck because only CreateDIBSection has example...).

@monoscout999

GUICreate() is almost the same _Winapi_CreateWindowEx() . I want to create ProSpeed UDF without DLL so it will faster.

@smashly

It's good but ProSpeed uses only GDI32 so GDIPlus is unnecessary.

Now I try to use SetDIBitsToDevice.

My code (some functions are from ):

Global Const $Width = 32, $Height = 32
$tBIHDR = DllStructCreate($tagBITMAPINFOHEADER)
DllStructSetData($tBIHDR, 'biSize', DllStructGetSize($tBIHDR))
DllStructSetData($tBIHDR, 'biWidth', $Width)
DllStructSetData($tBIHDR, 'biHeight', $Height)
DllStructSetData($tBIHDR, 'biPlanes', 1)
DllStructSetData($tBIHDR, 'biBitCount', 32)
DllStructSetData($tBIHDR, 'biCompression', $BI_RGB)
$hBitmap = _WinAPI_CreateDIBSection(0, $tBIHDR, $DIB_RGB_COLORS, $pBits)
$tBITMAPINFO = DllStructCreate($tagBITMAPINFO)
$tBits = DllStructCreate("int[" & $Width * $Height & "]")
_WinAPI_SetDIBitsToDevice($hDC, 0, 0, 32, 32, $Width, $Height, 0, 32, DllStructGetPtr($tBITMAPINFO)), $DIB_RGB_COLORS, DllCallbackGetPtr($tBits))

If someone know how to use this function please tell me.

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

_Winapi_windowcreateex can not support AutoIt GUI functions like GuiGetMsg(), and i have my doubts about that a no DLL version will be more faster thatn a DLL.. but dont listen to me, a more experienced coder can give you a hint about this... and i find this a nice proyect to do it by your self in pure autoit.

Link to comment
Share on other sites

You can create a compatible DC, compatible Bitmap, select the Bitmap into the DC, then use SetDIBits (rather than SetDIBitsDevice). Alternatively, if you want to write directly to a given DC's DDB, just use StretchDIBits. I've done some messing around in graphics lately, and there's a number of neat ways to Blit graphics to screen, including Rotation, semi-3D effects, and a Blit with transparency (AlphaBlend) - all done without touching GDI+. If you don't like sprites looking terribly aliased however, GDI+ is the way to go - plus you get an alpha channel with most op's. There's a performance hit for the extra GDI+ effects, but I've not yet compared it for myself. (antialiasing and alpha blending require reading pixels from the destination - assuming GDI+ doesn't get hardware acceleration, that is)

I've also done work with DirectDraw (utilizing AutoItObject), which is pretty fun, but has limited usefulness just as GDI32 does. DirectDraw gets you hardware acceleration and allows transparent colors, but you suffer again with aliasing and a lack of an alpha channel. However - you can at least use it to synchronize blits to the vertical refresh. But that's old school now - Direct3D with 'textured quads', or Direct2D (WinVista+) are the way to go for 2D with the benefits of aliasing + alpha channel and other transforms (all hardware accelerated).

The reason for my own interest in this area is porting an old side-scroll game demo I created back in the 90's to Windows.. ahh, memories..

*edit: A better article on textured quads: GameDev.net -- 2D in Direct3D using Textured Quads (archive.org)

Edited by Ascend4nt
Link to comment
Share on other sites

@Ascend4nt

Thank you.

Your post is very useful for me. I'll test speed of DirectDraw and Direct3D. Maybe after I'll create 2D sprites UDF using one of them or GDI32.

Please close this topic.

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

I used ProSpeed and tryed to create my own Map Editor for a small game...

I didn't finished my editor cose many bugs that I can't solve...

My unfinished map-editor example (using prospeed): http://nfk.pro2d.ru/files/MAP-EDITOR-AU3.rar

To test just run TEEEEESTTT-----Trx-Designer.au3 then open map from folder REAL-MAPS

Then you can add eny sprite (BMP) only (!?)... And without eny transparency (!?)... Also everything is blinking ;D

I hope this unfinished example will help to someone...

Sorry for my english...

Edited by Enforcer
[RU] Zone
Link to comment
Share on other sites

@Enforcer

I've download and check it. It's not bad (but you have a mess in your code).

Answering your questions about ProSpeed:

Sometimes ago I was creating little MMORPG with this library. You can use BMP, PNG, GIF... with transparency and it isn't blinking. But you must use sprite functions to do it.

Here is little example of using sprites (you need ProSpeed.au3):

Global Const $HDC = GetHDC()
Background("", 0, 0, 200, 100)
$LoadSprite = LoadSprite(@ScriptDir & "\sprite1.png")
$LoadSpriteResize = LoadSpriteResize(@ScriptDir & "\sprite1.png", 256, 128)
Sprite($LoadSprite, $HDC,  0, 0, 32, 32, 1, 1, 0, 0, 0)

But it isn't good way to making game. Now I'm writing my own graphical library.

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

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...