Tipp 0116 Bitmap Animation
Autor/Einsender:
Datum:
  Alexander Csadek
21.08.2001
Entwicklungsumgebung:
DirectX-Version:
  VB 6
DirectX 7
Mit DirectDraw lassen sich ganz hervorragende und flüssige Animationen erstellen.
Zu diesem Tipp finden Sie eine ausführliche Beschreibung in unserer DirectX-Rubrik zu DirectDraw.
Code im Codebereich des Moduls
 
Option Explicit

Type strcBild
  Width    As Single
  Height   As Single
  AniCount As Single
  AniMax   As Single
End Type

Public Animation1 As strcBild
Public Animation2 As strcBild

Public Const SCREENWIDTH As Single = 800
Public Const SCREENHEIGHT As Single = 600

Public FrameTime As Long
Public Const FrameRate = 50
 
Code im Codebereich der Form
 
Option Explicit

Dim DX7 As New DirectX7
Dim DD7 As DirectDraw7
Dim SurfaceDesc As DDSURFACEDESC2
Dim PrimarySurface As DirectDrawSurface7
Dim BackBuffer As DirectDrawSurface7
Dim bmpAnimation1 As DirectDrawSurface7
Dim bmpAnimation2 As DirectDrawSurface7

Dim running As Boolean

Private Sub Form_Load()
  Dim Destrect As RECT
  Dim SrcRect As RECT
  Dim hlpY As Single

  Me.Show
  Me.Refresh

  Initialization
  BitmapLaden

  running = True

  Do
    If GetTime - FrameTime < FrameRate Then
      DoEvents
    Else
      With SrcRect
        .Left = (Animation1.Width * Animation1.AniCount): _
              .Right = (.Left + Animation1.Width)
        .Top = 0: .Bottom = Animation1.Height
      End With
      BackBuffer.BltFast _
            100, 100, bmpAnimation1, SrcRect, DDBLTFAST_WAIT

      With Animation1
        .AniCount = .AniCount + 1
        If .AniCount > .AniMax Then .AniCount = 0
      End With

      With SrcRect
        hlpY = Int(Animation2.AniCount / 11)
        .Left = (Animation2.AniCount - (hlpY * 11)) * _
            Animation2.Width: .Right = (.Left + Animation2.Width)
        .Top = (hlpY * Animation2.Height): _
            .Bottom = (.Top + Animation2.Height)
      End With
      BackBuffer.BltFast _
            300, 100, bmpAnimation2, SrcRect, DDBLTFAST_WAIT

      With Animation2
        .AniCount = .AniCount + 1
        If .AniCount > .AniMax Then .AniCount = 0
      End With

      BackBuffer.SetForeColor vbRed
      BackBuffer.SetFont Me.Font
      BackBuffer.DrawText _
            10, 10, "DirectDraw und Bitmaps-Animation", False
      BackBuffer.DrawText _
            10, 30, "<Esc> beendet das Programm", False

      PrimarySurface.Flip Nothing, DDFLIP_WAIT
      FrameTime = GetTime

      ClearBuffer vbBlack

      DoEvents
    End If
  Loop While running

  Terminate
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
  If (KeyAscii = vbKeyEscape) Then
    running = False
  End If
End Sub

Sub Initialization()
  Set DD7 = DX7.DirectDrawCreate("")
  DD7.SetCooperativeLevel Me.hWnd, DDSCL_EXCLUSIVE Or _
        DDSCL_FULLSCREEN Or DDSCL_ALLOWREBOOT
  DD7.SetDisplayMode SCREENWIDTH, SCREENHEIGHT, 16, 0, _
        DDSDM_DEFAULT

  With SurfaceDesc
    .lFlags = DDSD_CAPS Or DDSD_BACKBUFFERCOUNT
    .ddsCaps.lCaps = _
        DDSCAPS_PRIMARYSURFACE Or DDSCAPS_FLIP Or DDSCAPS_COMPLEX
    .lBackBufferCount = 1
  End With
  Set PrimarySurface = DD7.CreateSurface(SurfaceDesc)
  SurfaceDesc.ddsCaps.lCaps = DDSCAPS_BACKBUFFER

  Set BackBuffer = _
        PrimarySurface.GetAttachedSurface(SurfaceDesc.ddsCaps)
End Sub

Sub BitmapLaden()
  Dim BmpDesc As DDSURFACEDESC2

  BmpDesc.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
  BmpDesc.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN

  Animation1.Width = 49: Animation1.Height = 104
  Animation1.AniCount = 0: Animation1.AniMax = 31
  BmpDesc.lWidth = 1568: BmpDesc.lHeight = 104

  Set bmpAnimation1 = DD7.CreateSurfaceFromFile _
        (App.Path & "\Animation1.bmp", BmpDesc)

  Animation2.Width = 35: Animation2.Height = 35
  Animation2.AniCount = 0: Animation2.AniMax = 43
  BmpDesc.lWidth = 385: BmpDesc.lHeight = 140

  Set bmpAnimation2 = DD7.CreateSurfaceFromFile _
        (App.Path & "\Animation2.bmp", BmpDesc)
End Sub

Sub Terminate()
  Set bmpAnimation1 = Nothing
  Set bmpAnimation2 = Nothing
  DD7.RestoreDisplayMode
  DD7.SetCooperativeLevel Me.hWnd, DDSCL_NORMAL
  Set PrimarySurface = Nothing
  Set DD7 = Nothing
  Set DX7 = Nothing
  End
End Sub

Sub ClearBuffer(Color As Long)
  Dim Destrect As RECT

  With Destrect
    .Bottom = SCREENHEIGHT
    .Left = 0
    .Right = SCREENWIDTH
    .Top = 0
  End With
  BackBuffer.BltColorFill Destrect, Color
End Sub

Function GetTime() As Long
  GetTime = DX7.TickCount
End Function
 
Weitere Links zum Thema
Animation in PictureBox
Eigener, animierter Mauszeiger
Hinweis
Um dieses Beispiel ausführen zu können, wird die DirectX 7 for Visual Basic Type Library benötigt (siehe dazu die Erläuterungen in der DirectX-Rubrik).

Windows-Version
95
98/SE
ME
NT
2000
XP
Vista
Win 7
VB-Version
VBA 5
VBA 6
VB 4/16
VB 4/32
VB 5
VB 6


Download  (122 kB) Downloads bisher: [ 3498 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Projekte | Tutorials | API-Referenz | VB-/VBA-Tipps | Komponenten | Bücherecke | VB/VBA-Forum | VB.Net-Forum | DirectX-Forum | Foren-Archiv | DirectX | VB.Net-Tipps | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Sonntag, 18. September 2011