Jump to content
Xtreme .Net Talk

DirectDraw-Multiple 'frame' animation from single bmp?


Recommended Posts

Posted

I am wondering how to do this in directx9 for VB.NET..

I have an image with multiple 'frames' of animation in it...

When I draw my .bmp surface I want to 'scroll' through the multiple frames within the .bmp by making DirectX only draw certain widths and heights in my .BMP

I used to do this using GDI..somthing like...

 

Public Sub Timer1_Tick

Dim Graphic As Graphics

Dim bmap As Bitmap

Graphic.DrawImage(bmap, rectangle,BitMapX , BitMapY, 67, 70)

BitmapX = BitmapX + 35

BitMapY = BitMapY + 15

End Sub

..or whatever..just an example...

For my code so far using DX9 I can draw a bitmap to a screen, move it around and such...But I cannot find a way to define what area of the bitmap to draw and how best to 'loop' through a series of defined draw areas within my bitmap, thus creating an animation from only one picture. Thank you for any help in advance.

  • *Experts*
Posted

Are you using Direct3D or DirectDraw? If you're using DirectDraw, the Draw and DrawFast methods on the surface objects take a number of overloads that specify source and destination rectangles or points, or just ints (if I remember right).

 

If you're using Direct3D and you're using the sprite class, you can do pretty much the same thing.

 

If you're using Direct3D and you're NOT using the sprite class, you'll have to calculate the texture coordinates yourself.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Thanks--

I am using directDraw and I found a method to draw certain parts of a bitmap..

I created a rectangle and used it's .top .left .bottom .right properties to specify the source of the image I want to use. Then I passed the DirectDraw 'draw' code to a timer at an interval of 100ms which updates the properties of the rectangle .top .left etc..using If Then...

Thanks for the help.

Posted (edited)

I'll post my code here in case anyone has this question in the future...

 

I made a public variable..

 Dim rmain As Rectangle 

 

Then after loading the bitmap with all predrawn frames into a back surface I put this code into the mousedown event..

 

 Public Sub Form1_MouseDown(ByVal sender As Object, ByVal f As MouseEventArgs) Handles MyBase.MouseDown
       
    If f.Button = MouseButtons.Right Then
           If f.Y < 300 Then 'this would be for determining direction to face the sprite.
               rmain.Width = 35 'rectangles width
               rmain.Height = 95 'height

               rmain.X = 100 'this is where the leftmost x coordinate will pull from the bitmap
               rmain.Y = 0 'same for y coord, with zero being complete top of my image.
               Render()
               Timer1.Enabled = True 'Timer code below
           End If
'then you would put rest of code for determining direction of sprite here
              'If f.Y > 300 then ....etc..

End Sub

The timer interval is 100ms, poor resolution but it works. Every 100ms it updates the source rectangles x,y coords..

 

 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
       If rmain.X = 100 Then
           rmain.X = 130 'sends rectangle over 30 pixels to the next fram in the bitmap
       ElseIf rmain.X = 130 Then
           rmain.X = 160 'and so on..
       ElseIf rmain.X = 160 Then
           rmain.X = 190
       ElseIf rmain.X = 190 Then
           rmain.X = 100
       End If
       Render()
   End Sub

 

Then..

 Public Sub Render()
       BackSurface.ColorFill(Color.Black) 'refresh screen
       BackSurface.DrawFast(500, 356, CharSurface, rmain, DrawFastFlags.Wait) 
       FrontSurface.Draw(BackSurface, DrawFlags.Wait) 'flip backsurface to screen.
End Sub

The 500,356 in the, BackSurface.DrawFast(500, 356, CharSurface, rmain, DrawFastFlags.Wait) is the point you want to draw the sprite on the screen..Supposed to be close to dead center on an 800x600 form but it should be worked out to dynamically position the sprite according to the user size of the form/resolution. The rmain in that tells us where to 'cut' the image from the loaded bitmap using the invisible rectangle.

Then you cant forget to stop the timer from rendering when user releases right mouse button so...

 Public Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp
       If e.Button = MouseButtons.Right Then
           Timer1.Enabled = False
       End If
   End Sub

I dont know if this is the best way to do it, but it gives an idea of how one could accomplish this. Any comments on this from someone who knows what they're doing? =)

-j

Edited by nem33

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...