SPRITE IN A BOX

© 2003, Alyce Watson

by Alyce Watson [Alyce's Restaurant]

Home

Collision Simulator

Foon's Tips

Encryption

Lesson Browser

Cookie DLL

LB NoteBoard

LB Clipboard Commands

Clipboard API Demos

Compiled HTML Help

Tabstrip

Tabstrip Demo

Scrolling Controls

Why API?

Sprite in a Box

Newsletter Help

Index

This is not a sprite tutorial! For a good sprite tutorial online, see the [Visual Basic Explorer Sprite Tutorial.] http://www.vbexplorer.com/VBExplorer/gdi1.asp


This little demo does two things.

Sprite Complexity

It demonstrates some of the complexity involved in drawing sprites. This is a simplified version that takes a shortcut and draws the sprite on the screen, rather than building the frame in memory, but it shows quite a lot of the necessary coding.

Draw Sprites in Any Graphicbox

The demo is highly commented. If you follow the procedures outlined, you can use this method to draw sprites in graphicboxes yourself. This might be helpful because Liberty BASIC permits you to have only one sprite graphicbox or graphics window per program.


'run this program from the LB root directory
nomainwin

'load a sprite
loadbmp "smile", "sprites\smiley1.bmp"

'get handle of bmp
hSprite=hbmp("smile")

'get dimensions of sprite bitmap
'first build a struct to contain info
struct BITMAP,_ 
bmType As Long,_
bmWidth As Long,_
bmHeight As Long,_
bmWidthBytes As Long,_
bmPlanes As Word,_
bmBitsPixel As Word,_
bmBits As Long

'get size of struct
nSize=Len(BITMAP.struct)

'fill struct
CallDLL #gdi32, "GetObjectA",_
hSprite As Long,_   'handle of sprite bitmap
nSize As Long,_     'size of struct
BITMAP As struct,_  'struct
results As Long

'retrieve info
BitmapWidth=BITMAP.bmWidth.struct
BitmapHeight=BITMAP.bmHeight.struct
SpriteHeight=BitmapHeight/2  ''height of sprite is half bmp height

'load a background
loadbmp "bg", "sprites\bg1.bmp"

'open a window that contains a graphicbox
WindowWidth=230:WindowHeight=160
graphicbox #1.g, 10,10,200,100
open "Sprite in a Box" for window_nf as #1
#1 "trapclose [quit]"

'draw background:
#1.g "down; drawbmp bg 0 0"

'get graphicbox handle
hBox=hwnd(#1.g)

'get a device context for graphicbox
CallDLL #user32, "GetDC",_
hBox As Long,_  'handle of graphicbox
hDC As Long     'returns handle of device context

'create memory device context
CallDLL #gdi32, "CreateCompatibleDC",_   
hDC As Long,_   'graphicbox device context
hDCmem As Long  'returns memory device context

'select our sprite bitmap into memory DC
CallDLL #gdi32, "SelectObject",_
hDCmem As Long,_    'memory device context
hSprite As Long,_   'bitmap handle
oldObject As Long   'returns previous object of same type

x=60                'x location to draw sprite
y=40                'y location to draw sprite
w=BitmapWidth       'width of sprite
h=SpriteHeight      'height of sprite is half bmp height
x2=0                'x location to get image from
y2=0                'y location to get image from

'draw mask first
CallDLL #gdi32, "BitBlt",_
hDC As Long,_       'graphicbox hDC
x As Long,_         'x location to put sprite
y As Long,_         'y location to put sprite
w As Long,_         'width of sprite
h As Long,_         'height of sprite
hDCmem As Long,_    'memory DC
x2 As Long,_        'x location of source sprite on bmp
y2 As Long,_        'y location of source sprite on bmp
_SRCAND As Ulong,_  'dw Raster Operation for mask
RESULT As Boolean
    
'y location to get sprite from is the
'same as the height of the sprite -
'remember the mask is on the bitmap
'above the sprite!
y2=SpriteHeight     'y location to get image from

'now draw sprite:
CallDLL #gdi32, "BitBlt",_
hDC As Long,_       'graphicbox hDC
x As Long,_         'x location to put sprite
y As Long,_         'y location to put sprite
w As Long,_         'width of sprite
h As Long,_         'height of sprite
hDCmem As Long,_    'memory DC
x2 As Long,_        'x location of source sprite on bmp
y2 As Long,_        'y location of source sprite on bmp
_SRCPAINT As Ulong,_'dw Raster Operation for sprite
RESULT As Boolean
    
'release device context for graphicbox
CallDLL#user32, "ReleaseDC",_
hBox As Long,_  'handle of graphicbox
hDC As Long,_   'handle of device context
result As Long

'delete memory device context
CallDLL #gdi32, "DeleteDC",_
hDC As Long,_   'graphicbox dc
r As Boolean

'use this method to make graphics persist
#1.g "getbmp SaveMe 0 0 200 100"
#1.g "drawbmp SaveMe 0 0; flush"


wait

[quit] close #1:end

Home

Collision Simulator

Foon's Tips

Encryption

Lesson Browser

Cookie DLL

LB NoteBoard

LB Clipboard Commands

Clipboard API Demos

Compiled HTML Help

Tabstrip

Tabstrip Demo

Scrolling Controls

Why API?

Sprite in a Box

Newsletter Help

Index