Skip to main content
gamedev.net
🔒 Locked

Problem with animation

Started by BlacknRedGT 3 replies 450+ views
Original Post
BlacknRedGT
BlacknRedGT
ok I am just putting a pic up of a guy running(one bmp with 4 animations on it) when I run the prog, it will lockup after the second frame is drawn.. however if I change the CreateFrames() to say 2 instead of 3(it will only animate the 3 images) it will work fine..... the error Im getting it this after the lockup: (says it is happening in the INIT fuction) An unhandled exception of type 'Microsoft.DirectX.DirectDraw.PrimarySurfaceAlreadyExistsException' occurred in microsoft.directx.directdraw.dll Additional information: Error in the application.

Imports Microsoft.DirectX
Imports Microsoft.DirectX.DirectDraw
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()
        Me.Show()
        AddHandler Me.GotFocus, AddressOf Gfocus
        dddevice = Nothing
        dddevice = New Device
        dddevice.SetCooperativeLevel(Me, CooperativeLevelFlags.FullscreenExclusive) 'create a new instance of our form1 class and pass the form
        'into it
        

        'Add any initialization after the InitializeComponent() call
        Init()
        GameLoop()

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container
        Me.Text = "Form1"
    End Sub

#End Region
    Public dddevice As Device = Nothing
    Public psurface As Surface 'Primary Surface (user visable)
    Public ssurface As Surface 'Secondary Surface (back buffer)
    Dim charsurface As Surface 'image surface
    Private Clip As Clipper = Nothing
    Private Frames(4) As Rectangle
    Private frm4act As Integer = 0

    Public Shared Sub Main()
        Dim GameForm As New Form1   ' making instance of form1
        Application.Exit() 'exiting the application
    End Sub
    Public Sub Init()
        psurface = Nothing
        ssurface = Nothing
        Dim sdesc As New SurfaceDescription
        Clip = New Clipper(dddevice)
        Clip.Window = Me
        sdesc.SurfaceCaps.PrimarySurface = True
        sdesc.SurfaceCaps.Flip = True
        sdesc.SurfaceCaps.Complex = True
        sdesc.BackBufferCount = 1
        psurface = New Surface(sdesc, dddevice)
        psurface.Clipper = Clip
        sdesc.Clear()
        sdesc.SurfaceCaps.BackBuffer = True
        ssurface = psurface.GetAttachedSurface(sdesc.SurfaceCaps)
        sdesc.Clear()
        sdesc.SurfaceCaps.OffScreenPlain = True
        charsurface = New Surface("C:\Documents and Settings\ROBERT\My Documents\Visual Studio Projects\DirectXProg\character.bmp", sdesc, dddevice)
        CreateFrames()

    End Sub

    Private Sub GameLoop()
        Do While Me.Created
            If Not Me.Focused Then
                Application.DoEvents()
            Else
                ShowSurface()
                Application.DoEvents()
            End If
        Loop
        ending()
    End Sub

    Public Sub ShowSurface()
        Try
            ssurface.ColorFill(Color.Red)
            ssurface.DrawFast(100, 300, charsurface, Frames(frm4act), DrawFastFlags.DoNotWait)

        Catch e As Exception
            Init()
        End Try
        psurface.Flip(ssurface, FlipFlags.Wait)
    End Sub

    Public Sub ending()
        dddevice.Dispose()
        psurface.Dispose()
        ssurface.Dispose()
        Clip.Dispose()
    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Right Then
            frm4act = frm4act + 1
            If frm4act > 3 Then
                frm4act = 0
            End If
            ShowSurface()
        End If
        If e.KeyCode = Keys.Escape Then
            ending()
            End
        End If
    End Sub

    Public Sub GFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        ending()
        Dim Newform As New Form1
    End Sub

    Public Sub CreateFrames()
        Dim x As Integer
        For x = 0 To 3
            Frames(x) = New Rectangle(141 * x, 0, 141, 250)
        Next
    End Sub
End Class

matthughson
matthughson
Try taking out that init() call in your show surface function. It sounds like you are trying to allocate a surface over top of another that hasn't been freed yet.

Matt Hughson
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
BlacknRedGT
BlacknRedGT
I took out the INIT() call and no more locking up but for some reason the 3rd frame shows up as the background image(which I have set to red right now.... Im gonna try using a differnt pic with a diff amout of frames and see what happens.
BlacknRedGT
BlacknRedGT
any suggestions on the 3rd frame not showing up in the mean time?
BlacknRedGT
BlacknRedGT
nevermind, switched out the image with one of my own and seems to work fine... thanks for the help again..

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.