Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

drawing sprites with Direct X 8 2

Status
Not open for further replies.

slowATthought

Programmer
Dec 2, 2003
56
US
I am going to start a game that uses DirectX 8 (mainly because I know how to do music and sound in DX8). I know that DX8 doesn't have DirectDraw, because it is mixed in with Direct3D. That's just as well, though, because I never learned DirectDraw. I'm trying to find where to begin on D3D 8 for 2D graphics. I couldn't find a tutorial on the Internet, so maybe you all could help me.
I just need some of the basic variables (or surfaces or whatever) and loading the images in and drawing routines, etc. This would be a whole lot of help.
Thank you
 
I have the book, but it doesn't cover 2D graphics in DX8. But thank you for the reply.
 

Ok, if you have the book, then check out chapter 10 "Diving Into DirectDraw" and also look up the sprite class. In fact you may want to check out chapters 7 through 10, and while you are up to reading you may want to look at the quote about DirectDraw on pg 179.

Then check your references in VB and you should be able to set references to either 7 or 8 (could be wrong I think I only installed the version 8 SDK).

Good Luck

 
I looked through those sections of the book, and they explain DirectDraw, and that 2D graphics can be done in D3D, but they don't tell how. I started by trying to use DirectDraw commands, but they don't exist in D3D.
Like I said, I have looked to find a solution already, and I am stumped. Maybe a few of the commands would help...
 
<ahem> DirectX 8 includes DirectDraw 7...

In which case, all you need to do is add a reference to the DirectX 7 for Visual Basic Type Library (and to the DirectX 8 for Visual Basic Type Library, since it contains some useful declarations that I use in the example) and then something like the following shpould help to get you started (it's a simple DirectDraw blit of a bitmap). You just need a form with a command button:
[tt]
Option Explicit

Private Declare Sub Sleep Lib &quot;kernel32&quot; (ByVal dwMilliseconds As Long)
Private Declare Function GetClientRect Lib &quot;user32&quot; (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function SetRect Lib &quot;user32&quot; (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function ClientToScreen Lib &quot;user32&quot; (ByVal hwnd As Long, lpPoint As Point) As Long ' Point is defined in the DirectX 8 library

Private Sub Command1_Click()
Dim DX7 As DirectX7

Dim srcRECT As RECT
Dim dstRect As RECT

Dim DFactory As DirectDraw7
Dim Primary As DirectDrawSurface7
Dim Backbuffer As DirectDrawSurface7
Dim ddsdPrimary As DDSURFACEDESC2
Dim ddsd2 As DDSURFACEDESC2


Dim hwCAPS As DDCAPS
Dim helCAPS As DDCAPS

Dim lp As Single
Dim oldright As Single
Dim myPointTL As Point
Dim myPointBR As Point

Set DX7 = New DirectX7
Set DFactory = DX7.DirectDrawCreate(&quot;&quot;)
DFactory.SetCooperativeLevel Form1.hwnd, DDSCL_NORMAL

DFactory.GetCaps hwCAPS, helCAPS


GetClientRect Form1.hwnd, dstRect

myPointTL.x = dstRect.Left
myPointTL.y = dstRect.Top
ClientToScreen Form1.hwnd, myPointTL
myPointBR.x = dstRect.Right
myPointBR.y = dstRect.Bottom
ClientToScreen Form1.hwnd, myPointBR
SetRect dstRect, myPointTL.x, myPointTL.y, myPointBR.x, myPointBR.y

ddsdPrimary.lFlags = DDSD_CAPS
ddsdPrimary.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE
Set Primary = DFactory.CreateSurface(ddsdPrimary)

ddsd2.lFlags = DDSD_CAPS
ddsd2.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN

Set Backbuffer = DFactory.CreateSurfaceFromFile(&quot;c:\sunset.bmp&quot;, ddsd2) ' Use your own bitmap here

' stretch the image into the client area step by step
oldright = dstRect.Right
For lp = 0# To 1 Step 0.02
dstRect.Right = oldright * lp
Primary.Blt dstRect, Backbuffer, srcRECT, DDBLT_DONOTWAIT
Sleep 25 ' slow the 'animation' down
Next

End Sub
 

Just looking in the index you did not then find this information?
[tt]
double buffer 385
blitting 386
creating on 385-386
drawing sprites using 387
encapsulating
clsDDSprit7 class 402-410
clsDDSurface7 class 399-402
clsDirectDraw7 class 369-399
overview 179, 374-376
surfaces, assigning color key to, 384-385
surfaces, blitting
Blt function 382
BltColorFill function, 383
BltFast function, 383
BltFx function, 383-384
BltToDc function, 384
surfaces, creating
CreateSurface function, 380-381
CreateSurfaceFromFile function 381
CreateSurfaceFromResource function 381-382
DirectDrawCreate function 379
surfaces, scaling, 420
testing
in full-screen mode 410-411
using DDWindow program 387-388
[/tt]
Or you did not find it useful?

Then perhaps, while I am still looking at the index you may find the following useful...
[tt]
DirectDraw 7.0
support
clsDDSprite7 class 1000-1001
clsDDSurface7 class 1001-1002
clsDirectDraw7 class 999
DirectDrawCreate function 379, 1015
[/tt]

Good Luck

 
I did see those, but I was hoping to use D3D so I would use only one version of DirectX, but if it is too much trouble, I think I will go with DirectDraw.

As for the code, thank you a lot, this is what I wanted. I am getting and error, however. Upon hitting the command button, everything performs until the Blt command, where it highlights dstRect and displays &quot;ByRef arguement type mismatch&quot;. Do you know what is wrong?
 
I realise it is probably silly of me to ask, but you have

a) copied the code in it's entirety, including the Option Explicit;
b) made sure you have references to both the Direct 7 and DirectX 8 libraries?

(RECT and POINT are types that would normally need to be declared seperately if you don't have a reference to the DirectX 8 library)
 
hmmm... I didn't have the Option Explicit part, but i added it and it still encounters the error. Any ideas?
 
Not off the top of my head. The code is cut and pasted from a working demo - and I then copied it back from TekTips into a brand new VB project, and it worked fine.
 
I retried it on a new project, and it works. Maybe it has to do with my project having more than one module. Is this possible?
 
Well, I guess whatever the problem was doesn't matter. I just redid the old project in the one that works. One more question, though. Is there a faster way to clear the screen (set to black) other than BltColorFill (Black)?
 
I guess the question to ask is why do you think BltColorFill is too slow (it seems pretty quick here...), and then to consider if there are alternative approaches to what you are trying to achieve
 
Well, my old way of animation was to clear the screen, then draw out all my new images, wait for a few milliseconds, and repeat the process.

I get the feeling that in most cases for DirextX games, people draw over their old images, move them, and then redraw certain stuff. However, I'm not used to this, and am rather stubborn.

That's why I want to clear the entire screen, and then redraw my images, and I figured that because DirectX is so fast, I could get away with it. However, when I tried using BltColorFill with the color set to RGB(0, 0, 0), everything slowed down dramatically, flickered a lot.

I'm guessing that it is because I am blting onto the entire screen, (even though it is just black) and I'm wondering if there is a faster way to clear the screen.

Thanks for any help.
 

strongm,

Your code is similar to some of the examples in the book that I was pointing slowATthought to, although all in one place.

slowATthought,

The reason that it flickers is because you are introducing a blank frame every other frame. Take some movies for example at 24 frames per second. Now if you introduced a black frame in between each frame or blacked out every other frame you would get a slowed down flickered image on the screen or you would get jerky movement with the flicker.

There is no reason to &quot;erase&quot; the current displayed image because when you apply the new image (if double buffered) or draw over the current image you are simply/basically &quot;erasing&quot; the old image.

Good Luck

 
>similar to some of the examples

I'd guess that, for the basic DirectDraw stuff (and I guess there really can't be anything much more basic than blitting from one DD surface to another, apart from perhaps drawing a primitive direct to the Primary), there's only one way of doing it. So the similarity doesn't surprise me.

The real pain with DD is all the setting up you have to do (eg, to draw a circle takes about 7 or 8 lines of code, 6-7 of which are setup, one of which actually draws the circle). And it gets worse with DX8...
 

strongm,

That's why I got the book to begin with. When the MSDN subscription came with the SDK I just had to look at the sample projects and with the amount of plain language commenting MS does with their samples ... well you know what I mean and 3 books (1/2 read) later these little side curiosities of mine will just have to wait until there is some down time.

 
When go with out erasing, it just layers al the old images so everything is a blur... I guess I'm not using the backbuffer right. I'll try and figure it out and I'll get back with you. I might have an idea of what to do. Thanks for all the help.
 
I fixed it! My problem with the back buffer had been that I really didn't know how to use it, so I was using as the surface for my background image. Now that I am using the buffer the way I should, everything is working prefectly! Thanks for all the help, I couldn't have used DirectDraw at all had it not been for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top