Hi, programming sprites commands for my language i played a little with Direct3D and DirectDraw, here is a code that use API functions for the window, create a directdraw screen, initialise Direct3D, create a sprite, and draw it to screen, require a image called "image.bmp". I hope that it will interest to someone.
Code: Select all
; DirectDraw and Direct3D Example by Pedro Gil (Balrog Software)
Global Quit
;* DDCOLORKEY
Structure DDCOLORKEY
dwColorSpaceLowValue.l ;// low boundary of color space that is to
;// be treated as Color Key, inclusive
dwColorSpaceHighValue.l ;// high boundary of color space that is
;// to be treated as Color Key, inclusive
EndStructure
;* DDSCAPS2
Structure DDSCAPS2
dwCaps.l ;// capabilities of surface wanted
dwCaps2.l
dwCaps3.l
dwCaps4.l
EndStructure
;* DDPIXELFORMAT
Structure DDPIXELFORMAT
dwSize.l; // size of structure
dwFlags.l; // pixel format flags
dwFourCC.l; // (FOURCC code)
StructureUnion
dwRGBBitCount.l; // how many bits per pixel
dwYUVBitCount.l; // how many bits per pixel
dwZBufferBitDepth.l; // how many total bits/pixel in z buffer (including any stencil bits)
dwAlphaBitDepth.l; // how many bits for alpha channels
dwLuminanceBitCount.l; // how many bits per pixel
dwBumpBitCount.l; // how many bits per "buxel", total
EndStructureUnion
StructureUnion
dwRBitMask.l; // mask for red bit
dwYBitMask.l; // mask for Y bits
dwStencilBitDepth.l; // how many stencil bits (note: dwZBufferBitDepth-dwStencilBitDepth is total Z-only bits)
dwLuminanceBitMask.l; // mask for luminance bits
dwBumpDuBitMask.l; // mask for bump map U delta bits
EndStructureUnion
StructureUnion
dwGBitMask.l; // mask for green bits
dwUBitMask.l; // mask for U bits
dwZBitMask.l; // mask for Z bits
dwBumpDvBitMask.l; // mask for bump map V delta bits
EndStructureUnion
StructureUnion
dwBBitMask.l; // mask for blue bits
dwVBitMask.l; // mask for V bits
dwStencilBitMask.l; // mask for stencil bits
dwBumpLuminanceBitMask.l; // mask for luminance in bump map
EndStructureUnion
StructureUnion
dwRGBAlphaBitMask.l; // mask for alpha channel
dwYUVAlphaBitMask.l; // mask for alpha channel
dwLuminanceAlphaBitMask.l; // mask for alpha channel
dwRGBZBitMask.l; // mask for Z channel
dwYUVZBitMask.l; // mask for Z channel
EndStructureUnion
EndStructure
;* DDSURFACEDESC2
Structure DDSURFACEDESC2
dwSize.l; // size of the DDSURFACEDESC structure
dwFlags.l; // determines what fields are valid
dwHeight.l; // height of surface to be created
dwWidth.l; // width of input surface
StructureUnion
lPitch.l; // distance to start of next line (return value only)
dwLinearSize.l; // Formless late-allocated optimized surface size
EndStructureUnion
dwBackBufferCount.l; // number of back buffers requested
StructureUnion
dwMipMapCount.l; // number of mip-map levels requestde
; // dwZBufferBitDepth removed, use ddpfPixelFormat one instead
dwRefreshRate.l; // refresh rate (used when display mode is described)
dwSrcVBHandle.l; // The source used in VB::Optimize
EndStructureUnion
dwAlphaBitDepth.l; // depth of alpha buffer requested
dwReserved.l; // reserved
lpSurface.l; // pointer to the associated surface memory
StructureUnion
ddckCKDestOverlay.DDCOLORKEY; // color key for destination overlay use
dwEmptyFaceColor.l; // Physical color for empty cubemap faces
EndStructureUnion
ddckCKDestBlt.DDCOLORKEY; // color key for destination blt use
ddckCKSrcOverlay.DDCOLORKEY; // color key for source overlay use
ddckCKSrcBlt.DDCOLORKEY; // color key for source blt use
StructureUnion
ddpfPixelFormat.DDPIXELFORMAT; // pixel format description of the surface
dwFVF.l; // vertex format description of vertex buffers
EndStructureUnion
ddsCaps.DDSCAPS2; // direct draw surface capabilities
dwTextureStage.l; // stage in multitexture cascade
EndStructure
Structure DDBLTFX
dwSize.l; // size of structure
dwDDFX.l; // FX operations
dwROP.l; // Win32 raster operations
dwDDROP.l; // Raster operations new for DirectDraw
dwRotationAngle.l; // Rotation angle for blt
dwZBufferOpCode.l; // ZBuffer compares
dwZBufferLow.l; // Low limit of Z buffer
dwZBufferHigh.l; // High limit of Z buffer
dwZBufferBaseDest.l; // Destination base value
dwZDestConstBitDepth.l; // Bit depth used to specify Z constant for destination
StructureUnion
dwZDestConst.l; // Constant to use as Z buffer for dest
lpDDSZBufferDest.l; // Surface to use as Z buffer for dest
EndStructureUnion
dwZSrcConstBitDepth.l; // Bit depth used to specify Z constant for source
StructureUnion
dwZSrcConst.l; // Constant to use as Z buffer for src
lpDDSZBufferSrc.l; // Surface to use as Z buffer for src
EndStructureUnion
dwAlphaEdgeBlendBitDepth.l; // Bit depth used to specify constant for alpha edge blend
dwAlphaEdgeBlend.l; // Alpha for edge blending
dwReserved.l;
dwAlphaDestConstBitDepth.l; // Bit depth used to specify alpha constant for destination
StructureUnion
dwAlphaDestConst.l; // Constant to use as Alpha Channel
lpDDSAlphaDest.l; // Surface to use as Alpha Channel
EndStructureUnion
dwAlphaSrcConstBitDepth.l; // Bit depth used to specify alpha constant for source
StructureUnion
dwAlphaSrcConst.l; // Constant to use as Alpha Channel
lpDDSAlphaSrc.l; // Surface to use as Alpha Channel
EndStructureUnion
StructureUnion
dwFillColor.l; // color in RGB or Palettized
dwFillDepth.l; // depth value for z-buffer
dwFillPixel.l; // pixel value for RGBA or RGBZ
lpDDSPattern.l; // Surface to use as pattern
EndStructureUnion
ddckDestColorkey.DDCOLORKEY; // DestColorkey override
ddckSrcColorkey.DDCOLORKEY; // SrcColorkey override
EndStructure
Structure D3DTLVERTEX
StructureUnion ; Screen coordinates
sx.f
dvSX.f
EndStructureUnion
StructureUnion
sy.f
dvSY.f
EndStructureUnion
StructureUnion
sz.f
dvSZ.f
EndStructureUnion
StructureUnion ; Reciprocal of homogeneous w
rhw.f
dvRHW.f
EndStructureUnion
StructureUnion ; Vertex color
color.l
dcColor.l
EndStructureUnion
StructureUnion ; Specular component of vertex
specular.l
dcSpecular.l
EndStructureUnion
StructureUnion ; Texture coordinates
tu.f
dvTU.f
EndStructureUnion
StructureUnion
tv.f
dvTV.f
EndStructureUnion
EndStructure
Dim Point3D.D3DTLVERTEX(3)
#D3DTSS_COLOROP = 1
#D3DTSS_COLORARG1 = 2
#D3DTSS_COLORARG2 = 3
#D3DTSS_ALPHAOP = 4
#D3DTSS_ALPHAARG1 = 5
#D3DTSS_ALPHAARG2 = 6
#D3DTSS_BUMPENVMAT00 = 7
#D3DTSS_BUMPENVMAT01 = 8
#D3DTSS_BUMPENVMAT10 = 9
#D3DTSS_BUMPENVMAT11 = 10
#D3DTSS_TEXCOORDINDEX = 11
#D3DTSS_ADDRESS = 12
#D3DTSS_ADDRESSU = 13
#D3DTSS_ADDRESSV = 14
#D3DTSS_BORDERCOLOR = 15
#D3DTSS_MAGFILTER = 16
#D3DTSS_MINFILTER = 17
#D3DTSS_MIPFILTER = 18
#D3DTSS_MIPMAPLODBIAS = 19
#D3DTSS_MAXMIPLEVEL = 20
#D3DTSS_MAXANISOTROPY = 21
#D3DTSS_BUMPENVLSCALE = 22
#D3DTSS_BUMPENVLOFFSET = 23
#D3DTSS_TEXTURETRANSFORMFLAGS = 24
#D3DTSS_FORCE_DWORD = $7fffffff
#D3DTFG_LINEAR = 2
#D3DTOP_DISABLE = 1; // disables stage
#D3DTOP_SELECTARG1 = 2; // the Default
#D3DTOP_SELECTARG2 = 3
#D3DTOP_MODULATE = 4; // multiply args together
#D3DRENDERSTATE_SRCBLEND=19
#D3DRENDERSTATE_DESTBLEND=20
#D3DRENDERSTATE_ALPHABLENDENABLE=27
#D3DBLEND_SRCALPHA = 5
#D3DBLEND_INVSRCALPHA = 6
#D3DTA_SELECTMASK = $0000000 ; // mask For arg selector
#D3DTA_DIFFUSE = $00000000 ; // Select diffuse color
#D3DTA_CURRENT = $00000001 ; // Select result of previous stage
#D3DTA_TEXTURE = $00000002 ; // Select texture color
#D3DTA_TFACTOR = $00000003
#D3DRENDERSTATE_DESTBLEND = 20
#D3DRENDERSTATE_COLORKEYENABLE = 41
#DDSD_PIXELFORMAT = $00001000
#DDSD_TEXTURESTAGE = $00100000
#D3DRENDERSTATE_DITHERENABLE = 26
#D3DRENDERSTATE_ANTIALIAS=2
#D3DTRANSFORMSTATE_PROJECTION=3
#D3DTRANSFORMSTATE_VIEW=2
#D3DTRANSFORMSTATE_WORLD=1
#D3DRENDERSTATE_CULLMODE=22
#D3DCULL_NONE=1
#D3DRENDERSTATE_LIGHTING=137
#DDSCAPS_3DDEVICE = $00002000
#DD_OK = 0
#DD_FALSE = 1
#DDBLT_WAIT = $01000000
#DDBLTFAST_DONOTWAIT = $20
#DDSCL_FULLSCREEN = $00000001
#DDSCL_EXCLUSIVE = $00000010
#DDSCL_NORMAL = $00000081
#DDSCL_NOWINDOWCHANGES = $00000002
#DDSD_CAPS = $00000001
#DDSD_HEIGHT = $00000002
#DDSD_WIDTH = $00000004
#DDSD_BACKBUFFERCOUNT = $00000020
#DDSCAPS_BACKBUFFER = $00000004
#DDSCAPS_OFFSCREENPLAIN = $00000040
#DDSCAPS_PRIMARYSURFACE = $00000200
#DDSCAPS_COMPLEX = $00000008
#DDSCAPS_FLIP = $00000010
#DDSCAPS_TEXTURE = $00001000
#DDSCAPS_SYSTEMMEMORY = $00000800
#DDCKEY_SRCBLT = $00000008
#DDFLIP_WAIT = $00000001
#DDBLTFAST_NOCOLORKEY = $00000000
#DDBLTFAST_SRCCOLORKEY = $00000001
#DDBLTFAST_WAIT = $00000010
#DDBLT_KEYSRC = $00008000
#DDBLT_COLORFILL = $00000400
#DDERR_SURFACELOST = $450
#D3DTFN_LINEAR = 2
#D3DCLEAR_TARGET = $1
#D3DDP_WAIT = $00000001
#D3DPT_POINTLIST=1
#D3DPT_LINELIST=2
#D3DPT_LINESTRIP=3
#D3DPT_TRIANGLELIST=4
#D3DPT_TRIANGLESTRIP=5
#D3DFVF_RESERVED0 = $001
#D3DFVF_POSITION_MASK = $00E
#D3DFVF_XYZ = $002
#D3DFVF_XYZRHW = $004
#D3DFVF_XYZB1 = $006
#D3DFVF_XYZB2 = $008
#D3DFVF_XYZB3 = $00a
#D3DFVF_XYZB4 = $00c
#D3DFVF_XYZB5 = $00e
#D3DFVF_NORMAL = $010
#D3DFVF_RESERVED1 = $020
#D3DFVF_DIFFUSE = $040
#D3DFVF_SPECULAR = $080
#D3DFVF_TEXCOUNT_MASK = $f00
#D3DFVF_TEXCOUNT_SHIFT = 8
#D3DFVF_TEX0 = $000
#D3DFVF_TEX1 = $100
#D3DFVF_TEX2 = $200
#D3DFVF_TEX3 = $300
#D3DFVF_TEX4 = $400
#D3DFVF_TEX5 = $500
#D3DFVF_TEX6 = $600
#D3DFVF_TEX7 = $700
#D3DFVF_TEX8 = $800
#D3DFVF_RESERVED2 = $f000 ; // 4 reserved bits
#D3DFVF_VERTEX =( #D3DFVF_XYZ | #D3DFVF_NORMAL | #D3DFVF_TEX1 )
#D3DFVF_LVERTEX =( #D3DFVF_XYZ | #D3DFVF_RESERVED1 | #D3DFVF_DIFFUSE | #D3DFVF_SPECULAR | #D3DFVF_TEX1 )
#D3DFVF_TLVERTEX =( #D3DFVF_XYZRHW | #D3DFVF_DIFFUSE | #D3DFVF_SPECULAR | #D3DFVF_TEX1 )
DD.IDirectDraw7 ;Direct Draw
Primary.IDirectDrawSurface7 ;Primary surface
BackBuffer.IDirectDrawSurface7 ;Backbuffer
Direct3D.IDirect3D7 ;Direct 3D
Device.IDirect3DDevice7 ;Direct 3D Device
ddsd.DDSURFACEDESC2
caps.DDSCAPS2
DEnum.IDirect3DDevice7
Procedure WindowCallback(Window, Message, wParam, lParam)
Select Message
Case #WM_CLOSE
Quit=1
DestroyWindow_(Window)
; Result = DefWindowProc_(Window, Message, wParam, lParam)
Case #WM_DESTROY
PostQuitMessage_(0)
Result = 0
Default
Result = DefWindowProc_(Window, Message, wParam, lParam)
EndSelect
ProcedureReturn Result
EndProcedure
#StyleEx = 0
WindowClass.s = "DirectDraw"
wc.WNDCLASSEX
wc\cbSize = SizeOf(WNDCLASSEX)
wc\lpfnWndProc = @WindowCallback()
wc\hCursor = LoadCursor_(0, #IDC_ARROW)
wc\hbrBackground = #COLOR_BTNFACE+1
wc\lpszClassName = @WindowClass
RegisterClassEx_(@wc)
hWnd = CreateWindowEx_(#StyleEx, WindowClass, "DirectX", #WS_VISIBLE | #WS_BORDER | #WS_SYSMENU, 100, 100, 400, 300, 0, 0, 0, 0)
ShowWindow_(hWnd, #SW_SHOWDEFAULT)
UpdateWindow_(hWnd)
SetFocus_(hWnd)
; Open DirectDraw library and create the object.
If OpenLibrary(0,"ddraw.dll")
If CallFunction(0,"DirectDrawCreateEx",0,@DD.IDirectDraw7,?IID_IDirectDraw7,0) <> #DD_OK
MessageRequester("Warning:","Couldn't init DirectDraw",0)
End
EndIf
Else
MessageRequester("Warning:","Couldn't init DirectDraw",0)
End
EndIf
SurfaceDescriptor.DDSURFACEDESC2
; Make a fullscreen, exclusive application
DD\SetCooperativeLevel(hWnd, #DDSCL_EXCLUSIVE|#DDSCL_FULLSCREEN)
DD\SetDisplayMode(640, 480, 16, 0, 0)
; Prepare and create the primary surface.
ddsd\dwSize = SizeOf(DDSURFACEDESC2)
ddsd\dwFlags = #DDSD_BACKBUFFERCOUNT|#DDSD_CAPS
ddsd\ddsCaps\dwCaps = #DDSCAPS_COMPLEX|#DDSCAPS_FLIP|#DDSCAPS_PRIMARYSURFACE|#DDSCAPS_3DDEVICE
ddsd\dwBackBufferCount = 1
DD\CreateSurface(ddsd,@Primary,0)
caps\dwCaps = #DDSCAPS_BACKBUFFER
; Create the secondary surface.
Primary\GetAttachedSurface(caps, @BackBuffer)
; Initialise direct3d object.
DD\QueryInterface(?IID_IDirect3D7, @Direct3D)
; Create direct 3d device with hardware optimization
If Direct3D\CreateDevice(?IID_IDirect3DTnLHalDevice, backbuffer, @Device)<>0
Debug "tnlhal not supported"
; Create direct 3d device with partial hardware optimization
If Direct3D\CreateDevice(?IID_IDirect3DHalDevice, backbuffer, @Device)<>0
Debug "hal not supported"
; Create direct 3d device emulated by software
If Direct3D\CreateDevice(?IID_IDirect3DRGBDevice, backbuffer, @Device)<>0
Debug "RGB not supported"
EndIf
EndIf
EndIf
; Set the options for Direct3D Device.
Device\SetRenderState(#D3DRENDERSTATE_COLORKEYENABLE,1);
Device\SetRenderState(#D3DRENDERSTATE_COLORKEYENABLE, 1)
Device\SetRenderState(#D3DRENDERSTATE_LIGHTING,0)
Device\SetRenderState(#D3DRENDERSTATE_CULLMODE,#D3DCULL_NONE)
Device\SetRenderState(#D3DRENDERSTATE_DITHERENABLE,1)
Device\SetRenderState(#D3DRENDERSTATE_ANTIALIAS,1)
Device\SetTextureStageState(0, #D3DTSS_COLORARG1, #D3DTA_TEXTURE);
Device\SetTextureStageState(0, #D3DTSS_ALPHAARG1, #D3DTA_TEXTURE);
Device\SetTextureStageState(0, #D3DTSS_ALPHAARG2, #D3DTA_DIFFUSE);
Device\SetTextureStageState(0, #D3DTSS_ALPHAOP, #D3DTOP_MODULATE);
Device\SetTextureStageState(0, #D3DTSS_MINFILTER, #D3DTFN_LINEAR);
Device\SetTextureStageState(0, #D3DTSS_MAGFILTER, #D3DTFG_LINEAR);
Device\SetRenderState(#D3DRENDERSTATE_ALPHABLENDENABLE, #TRUE);
Device\SetRenderState(#D3DRENDERSTATE_SRCBLEND, #D3DBLEND_SRCALPHA)
Device\SetRenderState(#D3DRENDERSTATE_DESTBLEND, #D3DBLEND_INVSRCALPHA);;//
; Create the sprite
hbm.l : bm.BITMAP : pdds.l : hdcImage.l : hdc.l : bm2.BITMAP : ddsd2.DDSURFACEDESC2 : hr.l
hbm = LoadImage_(0, "image.bmp", #IMAGE_BITMAP, 0, 0, #LR_LOADFROMFILE | #LR_CREATEDIBSECTION);
If hbm = 0
MessageRequester("INFO","Couldn't load the Sprite",0)
End
EndIf
GetObject_(hbm, SizeOf(BITMAP), bm)
SurfaceDescriptor\dwSize = SizeOf(DDSURFACEDESC2)
SurfaceDescriptor\dwFlags = #DDSD_CAPS | #DDSD_HEIGHT | #DDSD_WIDTH | #DDSD_TEXTURESTAGE
SurfaceDescriptor\ddsCaps\dwCaps = #DDSCAPS_TEXTURE;OFFSCREENPLAIN
SurfaceDescriptor\dwWidth = bm\bmWidth
SurfaceDescriptor\dwHeight = bm\bmHeight
; Create sprite surface
If DD\CreateSurface(SurfaceDescriptor, @Sprite.IDirectDrawSurface7, 0) <> #DD_OK
MessageRequester("Error","Couldn't create Sprite surface",0)
End
EndIf
hdcImage = CreateCompatibleDC_(0)
If hdcImage = 0
MessageRequester("INFO","createcompatible dc failed",0)
End
EndIf
SelectObject_(hdcImage, hbm)
GetObject_(hbm, SizeOf(BITMAP), bm2)
dx = bm2\bmWidth
dy = bm2\bmHeight
ddsd2\dwSize = SizeOf(DDSURFACEDESC2)
ddsd2\dwFlags = #DDSD_CAPS|#DDSD_HEIGHT | #DDSD_WIDTH;
ddsd2\ddsCaps\dwCaps = #DDSCAPS_TEXTURE
Sprite\GetSurfaceDesc(@ddsd2)
hr = Sprite\GetDC(@hdc)
If hr = #DD_OK
StretchBlt_(hdc, 0, 0, ddsd2\dwWidth, ddsd2\dwHeight, hdcImage, 0, 0, dx, dy, #SRCCOPY);
Sprite\ReleaseDC(hdc)
Else
End
EndIf
DeleteDC_(hdcImage)
DeleteObject_(hbm)
If Sprite = 0
MessageRequester("INFO","LoadBitmap failed",0)
End
EndIf
ddck.DDCOLORKEY;
ddck\dwColorSpaceLowValue = 0
ddck\dwColorSpaceHighValue = 0
Sprite\SetColorKey(#DDCKEY_SRCBLT, ddck)
Trans=128
msg.MSG
Repeat
If PeekMessage_(@msg, 0, 0, 0, #PM_REMOVE)
TranslateMessage_(@msg)
DispatchMessage_(@msg)
EndIf
Point3D(0)\sX=0
Point3D(0)\sY=0
Point3D(0)\sZ=0
Point3D(0)\tu=0
Point3D(0)\tv=0
Point3D(0)\rhw=1.0
Point3D(0)\color=RGB(255,0,0)+Trans<<24
Point3D(1)\sX=100
Point3D(1)\sY=0
Point3D(1)\sZ=0
Point3D(1)\tu=1
Point3D(1)\tv=0
Point3D(1)\rhw=1.0
Point3D(1)\color=RGB(0,255,0)+Trans<<24
Point3D(2)\sX=0
Point3D(2)\sY=100
Point3D(2)\sZ=0
Point3D(2)\tu=0
Point3D(2)\tv=1
Point3D(2)\rhw=1.0
Point3D(2)\color=RGB(0,0,255)+Trans<<24
Point3D(3)\sX=100
Point3D(3)\sY=100
Point3D(3)\sZ=0
Point3D(3)\tu=1
Point3D(3)\tv=1
Point3D(3)\rhw=1.0
Point3D(3)\color=RGB(0,255,255)+Trans<<24
BackBuffer\GetDC(@DC.l)
Ellipse_(DC,40,40,255,255)
BackBuffer\ReleaseDC(DC)
Device\BeginScene()
Device\SetTexture(0,Sprite)
Device\DrawPrimitive(#D3DPT_TRIANGLESTRIP,#D3DFVF_XYZRHW|#D3DFVF_TEX1|#D3DFVF_SPECULAR |#D3DFVF_DIFFUSE ,@Point3D(0),4,0)
Device\EndScene()
Primary\Flip(0, #DDFLIP_WAIT)
BlitFX.DDBLTFX\dwSize = SizeOf(DDBLTFX)
BlitFX\dwFillColor = RGB(255, 0, 0)
BackBuffer\Blt(0, 0, 0, #DDBLT_COLORFILL, BlitFX)
Until Quit=1
;Restore the display mode.
DD\RestoreDisplayMode()
DD\SetCooperativeLevel(hWnd, #DDSCL_NORMAL)
End
DataSection
IID_IDirectDraw7:
Data.l $15e65ec0
Data.w $3b9c, $11d2
Data.b $b9, $2f, $00, $60, $97, $97, $ea, $5b
IID_IDirect3DRGBDevice:
Data.l $A4665C60
Data.w $2673,$11CF
Data.b $A3,$1A,$0,$AA,$0,$B9,$33,$56
IID_IDirect3DHALDevice:
Data.l $84E63DE0
Data.w $46AA,$11CF
Data.b $81,$6F,$0,$0,$C0,$20,$15,$6E
IID_IDirect3DTnLHalDevice:
Data.l $F5049E78
Data.w $4861,$11D2
Data.b $A4,$7,$0,$A0,$C9,$6,$29,$A8
IID_IDirect3D7:
Data.l $F5049E77
Data.w $4861,$11D2
Data.b $A4,$7,$0,$A0,$C9,$6,$29,$A8
EndDataSection