March 28, 2024, 04:15:46 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


BOB

Started by aurelCB, October 11, 2011, 05:18:21 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

aurelCB

Hi...
I'm trying to create sprite with GDI BitBlit api function.
I don't know do i choose right aproach but it seems that work.
This program just copy bitmap into DC and show small image on window.
Any suggestion are welcome for moving this 'sprite'... :)
'Sprite by GDI api ./BOB/ ->BlitterOBject
'blitting
DECLARE IMPORT,BitBlt(hDestDC AS INT,x AS INT,y AS INT,nWidth AS INT,nHeight AS INT,hSrcDC AS INT,xSrc AS INT,ySrc AS INT,dwRop AS INT),INT
/* hDestDC - The hDC of the destination surface (this could be a form.hDC if you want to blit to a form, or it could be the address of a backbuffer that we've created).
' x - The X (horizontal position) coordinate of where we want the graphic to appear.
' y - The Y (vertical position) coordinate of where we want the graphic to appear.
' nWidth - The width of our graphic.
' nHeight - The height of our graphic.
' hSrcDC - The hDC of the source graphic, for example the DC address of a sprite that we loaded into memory.
' xSrc - The X (horizontal) offset, 0 if you want to blit from the very left edge of the source graphic,
'       if you want to start the blit from 18 and over then you would make this value 18, etc.
' ySrc - The Y (vertical) offset, same idea as xSrc, except vertically 0
' dwRop - The drawmode we want to use when blitting our graphic, also known as Raster Operations or ROPs.
This parameter is explained below. */
'timer
DECLARE IMPORT, GetTickCount(),INT
'creating buffers / loading sprites
DECLARE IMPORT, CreateCompatibleBitmap(hdc AS INT,nWidth AS INT,nHeight AS INT),INT
DECLARE IMPORT, CreateCompatibleDC(hdc AS INT),INT
DECLARE IMPORT, GetDC(hwnd AS INT),INT
'loading sprites
DECLARE IMPORT, SelectObject(hdc AS INT,hObject AS INT),INT
DECLARE IMPORT, DeleteObject(hObject AS INT),INT
DECLARE IMPORT, _DeleteDC ALIAS DeleteDC(hdc AS INT),INT

'our Buffer's DC
DEF mySprite As INT
'coordinates of our sprite/graphic on the screen
DEF SpriteX As INT
DEF SpriteY As INT
Dim BackHdc   As INT
Dim BackHnd   As INT
Dim xBack     As INT
Dim yBack     As INT
Dim sys_Seed  As INT
Dim xMouse    As INT
Dim yMouse    As INT
Dim sys_clic  As INT
Dim sysHdc   As INT
Dim syshwnd  As INT
Dim bmpHdc as INT
Dim imgH as INT

'Now we have the foundation of our code,
'we have all the API declarations we'll be needing,
'and all the variables we'll be using in this example.
'The next thing we're gunna do is create a function that loads graphics into memory,
'it makes working with the API a bit simpler...
'One thing that is important to understand is that a Device Context alone has no graphical data in it. A device context needs to have a bitmap loaded into it, whether that be a bitmap file, or
'a blank bitmap to use as a canvas to draw on (which is how you create a back buffer).

DEF win as Window
OPENWINDOW win,0,0,400,400,@MINBOX,0,"BlitterOBject",&main

'-----------------------------------------------------
'load small 32x32 bitmap
imgH = LOADIMAGE(GETSTARTPATH + "man.bmp", @IMGBITMAP)
sysHdc=GETHDC(win)
bmpHdc=CreateCompatibleDC(sysHdc)
bmpHnd = CreateCompatibleBitmap(sysHdc, 32, 32)
SelectObject(BmpHdc,imgH)
'blit image from hDC
BitBlt(sysHdc, 0, 0, 32, 32,bmpHdc, 0, 0, 0xCC0020)
RELEASEHDC(win,sysHdc)

'------------------------------------------------------

WAITUNTIL win = 0
END
'########################################
SUB main
'IF @hitwindow=*<window>pw
IF @message= @IDCREATE
CENTERWINDOW #<WINDOW>@HITWINDOW
ENDIF
IF @Message = @IDCLOSEWINDOW
        CLOSEWINDOW win
'IF *<window>pw2<>0 THEN CLOSEWINDOW *<window>pw2
ENDIF
'ENDIF
RETURN
ENDSUB

SUB SetBuffer(x:INT,y:INT),INT
BackHnd = CreateCompatibleBitmap(sysHdc, x, y)
BackHdc = CreateCompatibleDC(sysHdc)
SelectObject(BackHdc, BackHnd)
xBack = x : yBack = y
Return BackHdc
EndSub



aurelCB

Small improvement ,i add moving bob to right by pressing arrow key -> right
'Sprite by GDI api ./BOB/ ->BlitterOBject
'blitting
DECLARE IMPORT,BitBlt(hDestDC AS INT,x AS INT,y AS INT,nWidth AS INT,nHeight AS INT,hSrcDC AS INT,xSrc AS INT,ySrc AS INT,dwRop AS INT),INT
/* hDestDC - The hDC of the destination surface (this could be a form.hDC if you want to blit to a form, or it could be the address of a backbuffer that we've created).
' x - The X (horizontal position) coordinate of where we want the graphic to appear.
' y - The Y (vertical position) coordinate of where we want the graphic to appear.
' nWidth - The width of our graphic.
' nHeight - The height of our graphic.
' hSrcDC - The hDC of the source graphic, for example the DC address of a sprite that we loaded into memory.
' xSrc - The X (horizontal) offset, 0 if you want to blit from the very left edge of the source graphic,
'       if you want to start the blit from 18 and over then you would make this value 18, etc.
' ySrc - The Y (vertical) offset, same idea as xSrc, except vertically 0
' dwRop - The drawmode we want to use when blitting our graphic, also known as Raster Operations or ROPs.
This parameter is explained below. */
'timer
DECLARE IMPORT, GetTickCount(),INT
'creating buffers / loading sprites
DECLARE IMPORT, CreateCompatibleBitmap(hdc AS INT,nWidth AS INT,nHeight AS INT),INT
DECLARE IMPORT, CreateCompatibleDC(hdc AS INT),INT
DECLARE IMPORT, GetDC(hwnd AS INT),INT
'loading sprites
DECLARE IMPORT, SelectObject(hdc AS INT,hObject AS INT),INT
DECLARE IMPORT, DeleteObject(hObject AS INT),INT
DECLARE IMPORT, _DeleteDC ALIAS DeleteDC(hdc AS INT),INT

'our Buffer's DC
DEF mySprite As INT
'coordinates of our sprite/graphic on the screen
DEF SpriteX As INT
DEF SpriteY As INT
Dim BackHdc   As INT
Dim BackHnd   As INT
Dim xBack     As INT
Dim yBack     As INT
Dim sys_Seed  As INT
Dim xMouse    As INT
Dim yMouse    As INT
Dim sys_clic  As INT
Dim sysHdc   As INT
Dim syshwnd  As INT
Dim bmpHdc as INT
Dim imgH as INT
DEF mr :INT

'Now we have the foundation of our code,
'we have all the API declarations we'll be needing,
'and all the variables we'll be using in this example.
'The next thing we're gunna do is create a function that loads graphics into memory,
'it makes working with the API a bit simpler...
'One thing that is important to understand is that a Device Context alone has no graphical data in it. A device context needs to have a bitmap loaded into it, whether that be a bitmap file, or
'a blank bitmap to use as a canvas to draw on (which is how you create a back buffer).

DEF win as Window
OPENWINDOW win,0,0,400,400,@MINBOX,0,"BlitterOBject",&main
SETWINDOWCOLOR win,RGB(0,0,0)
'-----------------------------------------------------
'load small 32x32 bitmap
imgH = LOADIMAGE(GETSTARTPATH + "Alien.bmp", @IMGBITMAP)
sysHdc=GETHDC(win)
bmpHdc=CreateCompatibleDC(sysHdc)
bmpHnd = CreateCompatibleBitmap(sysHdc, 400, 400)
SelectObject(BmpHdc,imgH)
'blit image from hDC
BitBlt(sysHdc, 0, 0, 32, 32,bmpHdc, 0, 0, 0xCC0020)
RELEASEHDC(win,sysHdc)
'------------------------------------------------------
'clear backBuffer(background of window)
SetBuffer(400,400)

WAITUNTIL win = 0
END
'########################################
SUB main
'IF @hitwindow=*<window>pw
IF @message= @IDCREATE
CENTERWINDOW #<WINDOW>@HITWINDOW
ENDIF
IF @Message = @IDCLOSEWINDOW
        CLOSEWINDOW win
'IF *<window>pw2<>0 THEN CLOSEWINDOW *<window>pw2
ENDIF

IF @Message = @IDKEYDOWN
IF @CODE = 0x27 'arrow right
'While mr<200
mr=mr+1
sysHdc=gethdc(win)
'clear background
BitBlt(sysHdc, 0, 0, 400, 400,backHdc, 0, 0, 0xCC0020)
'blit sprite
BitBlt(sysHdc, mr, 0, 32, 32,bmpHdc, 0, 0, 0xCC0020)
RELEASEHDC(win,sysHdc)
'wend
ENDIF
ENDIF



RETURN
ENDSUB

SUB SetBuffer(x:INT,y:INT),INT
INT wHdc
wHdc=GetHdc(win)
BackHnd = CreateCompatibleBitmap(wHdc, x, y)
BackHdc = CreateCompatibleDC(wHdc)
SelectObject(BackHdc, BackHnd)
xBack = x : yBack = y
ReleaseHDC(win,wHdc)
Return BackHdc
EndSub



Heh i'm still not sure that im doing this properly but it looks that work... ;)

LarryMc

Aurel,

You have two memory leaks.

Anytime you SelectObject memory is allocated and you have to give it back.
For every SelectObject there needs to be a DeleteObject.
See here for some examples - http://www.ionicwind.com/forums/index.php?topic=4717.msg36634#msg36634

LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

aurelCB

Yes Larry you right i see that i miss something but dont see what ::)

aurelCB

Small addition to program,you can now move your bob in 4 ways.
Im still not sure why work little bit slow if i may say so.
'Sprite by GDI api ./BOB/ ->BlitterOBject
'blitting
DECLARE IMPORT,BitBlt(hDestDC AS INT,x AS INT,y AS INT,nWidth AS INT,nHeight AS INT,hSrcDC AS INT,xSrc AS INT,ySrc AS INT,dwRop AS INT),INT
/* hDestDC - The hDC of the destination surface (this could be a form.hDC if you want to blit to a form, or it could be the address of a backbuffer that we've created).
' x - The X (horizontal position) coordinate of where we want the graphic to appear.
' y - The Y (vertical position) coordinate of where we want the graphic to appear.
' nWidth - The width of our graphic.
' nHeight - The height of our graphic.
' hSrcDC - The hDC of the source graphic, for example the DC address of a sprite that we loaded into memory.
' xSrc - The X (horizontal) offset, 0 if you want to blit from the very left edge of the source graphic,
'       if you want to start the blit from 18 and over then you would make this value 18, etc.
' ySrc - The Y (vertical) offset, same idea as xSrc, except vertically 0
' dwRop - The drawmode we want to use when blitting our graphic, also known as Raster Operations or ROPs.
This parameter is explained below. */
'timer
DECLARE IMPORT, GetTickCount(),INT
'creating buffers / loading sprites
DECLARE IMPORT, CreateCompatibleBitmap(hdc AS INT,nWidth AS INT,nHeight AS INT),INT
DECLARE IMPORT, CreateCompatibleDC(hdc AS INT),INT
DECLARE IMPORT, GetDC(hwnd AS INT),INT
'loading sprites
DECLARE IMPORT, SelectObject(hdc AS INT,hObject AS INT),INT
DECLARE IMPORT, DeleteObject(hObject AS INT),INT
DECLARE IMPORT, _DeleteDC ALIAS DeleteDC(hdc AS INT),INT

'our Buffer's DC
DEF mySprite As INT
'coordinates of our sprite/graphic on the screen
DEF SpriteX As INT
DEF SpriteY As INT
Dim BackHdc   As INT
Dim BackHnd   As INT
Dim xBack     As INT
Dim yBack     As INT
Dim sys_Seed  As INT
Dim xMouse    As INT
Dim yMouse    As INT
Dim sys_clic  As INT
Dim sysHdc   As INT
Dim syshwnd  As INT
Dim bmpHdc as INT
Dim imgH as INT
DEF mr :INT
DEF my :INT
'Now we have the foundation of our code,
'we have all the API declarations we'll be needing,
'and all the variables we'll be using in this example.
'The next thing we're gunna do is create a function that loads graphics into memory,
'it makes working with the API a bit simpler...
'One thing that is important to understand is that a Device Context alone has no graphical data in it. A device context needs to have a bitmap loaded into it, whether that be a bitmap file, or
'a blank bitmap to use as a canvas to draw on (which is how you create a back buffer).

DEF win as Window
OPENWINDOW win,0,0,400,400,@MINBOX,0,"BlitterOBject",&main
SETWINDOWCOLOR win,RGB(0,0,0)
'-----------------------------------------------------
'load small 32x32 bitmap
imgH = LOADIMAGE(GETSTARTPATH + "Alien.bmp", @IMGBITMAP)
sysHdc=GETHDC(win)
bmpHdc=CreateCompatibleDC(sysHdc)
bmpHnd = CreateCompatibleBitmap(sysHdc, 400, 400)
SelectObject(BmpHdc,imgH)
'blit image from hDC
BitBlt(sysHdc, 0, 0, 32, 32,bmpHdc, 0, 0, 0xCC0020)
'DeleteObject(sysHdc)
RELEASEHDC(win,sysHdc)
'------------------------------------------------------
'clear backBuffer(background of window)
SetBuffer(400,400)

WAITUNTIL win = 0
END
'########################################
SUB main
'IF @hitwindow=*<window>pw
IF @message= @IDCREATE
CENTERWINDOW #<WINDOW>@HITWINDOW
ENDIF
IF @Message = @IDCLOSEWINDOW
        CLOSEWINDOW win:DeleteObject(sysHdc)
'IF *<window>pw2<>0 THEN CLOSEWINDOW *<window>pw2
ENDIF

IF @Message = @IDKEYDOWN
IF @CODE = 0x27 'arrow right
'While mr<200
mr=mr+8
sysHdc=gethdc(win)
'clear background
BitBlt(sysHdc, mr-8, my, 32, 32,backHdc, 0, 0, 0xCC0020)
'blit sprite
BitBlt(sysHdc, mr, my, 32, 32,bmpHdc, 0, 0, 0xCC0020)
RELEASEHDC(win,sysHdc)
'wend
ENDIF
IF @CODE = 0x25 'arrow left
'While mr<200
mr=mr-8
sysHdc=gethdc(win)
'clear background
BitBlt(sysHdc, mr+8, my, 32, 32,backHdc, 0, 0, 0xCC0020)
'blit sprite
BitBlt(sysHdc, mr, my, 32, 32,bmpHdc, 0, 0, 0xCC0020)
RELEASEHDC(win,sysHdc)
'wend
ENDIF
IF @CODE = 0x28 'arrow down
'While mr<200
my=my+8
sysHdc=gethdc(win)
'clear background
BitBlt(sysHdc, mr,my-8, 32, 32,backHdc, 0, 0, 0xCC0020)
'blit sprite
BitBlt(sysHdc, mr,my, 32, 32,bmpHdc, 0, 0, 0xCC0020)
RELEASEHDC(win,sysHdc)
'wend
ENDIF
IF @CODE = 0x26 'arrow up
'While mr<200
my=my-8
sysHdc=gethdc(win)
'clear background
BitBlt(sysHdc, mr,my+8, 32, 32,backHdc, 0, 0, 0xCC0020)
'blit sprite
BitBlt(sysHdc, mr,my, 32, 32,bmpHdc, 0, 0, 0xCC0020)
RELEASEHDC(win,sysHdc)
'wend
ENDIF

ENDIF



RETURN
ENDSUB

SUB SetBuffer(x:INT,y:INT),INT
INT wHdc
wHdc=GetHdc(win)
BackHnd = CreateCompatibleBitmap(wHdc, x, y)
BackHdc = CreateCompatibleDC(wHdc)
SelectObject(BackHdc, BackHnd)
xBack = x : yBack = y
ReleaseHDC(win,wHdc)
Return BackHdc
EndSub