Click here to Skip to main content
15,892,298 members
Articles / Multimedia / GDI

A Small GDI+ Sprite Animation Library

Rate me:
Please Sign up or sign in to vote.
4.10/5 (16 votes)
8 Sep 2006CPOL2 min read 64.4K   3.1K   39  
Animate and control sprites using GDI+.
Imports System.Drawing

Public Class winSprite

#Region " Variables "
    Protected m_Loc As System.Drawing.Point
    Protected m_Size As Size
    Protected m_Image() As Bitmap
    Protected m_FrameCount As Integer
    Protected m_CurFrame As Integer
    Protected m_DefaultScreenPtr As IntPtr
    Protected m_DefaultBackColor As Color
    Protected m_DefaultBackImage As Bitmap
    Protected m_Layer As Integer
    Protected m_TransparentColor As Color
    Protected m_MoveDelta As Point
    Protected m_UserData As Object
    Protected m_MoveBounds As Rectangle
    Protected m_SpriteID As Long
    Protected m_IsMovable As Boolean

#Region " Shared "
    Protected Shared m_DeaultClearColor As Color = Color.White
    Protected Shared m_SpriteIDs As Long = 0
    Friend Shared m_LayerList As New ArrayList
#End Region

#End Region

#Region " Propertys "
    Public Property IsMovable() As Boolean
        Get
            Return m_IsMovable
        End Get
        Set(ByVal Value As Boolean)
            m_IsMovable = Value
        End Set
    End Property

    Public Property MoveBounds() As Rectangle
        Get
            Return m_MoveBounds
        End Get
        Set(ByVal Value As Rectangle)
            m_MoveBounds = Value
        End Set
    End Property

    Public Property UserData() As Object
        Get
            Return m_UserData
        End Get
        Set(ByVal Value As Object)
            m_UserData = Value
        End Set
    End Property

    Public Property MoveDelta() As Point
        Get
            Return m_MoveDelta
        End Get
        Set(ByVal Value As Point)
            m_MoveDelta = Value
        End Set
    End Property

    Public Shared Property DeaultClearColor() As Color
        Get
            Return m_DeaultClearColor
        End Get
        Set(ByVal Value As Color)
            m_DeaultClearColor = Value
        End Set
    End Property

    Public Property DefaultBackColor() As Color
        Get
            Return m_DefaultBackColor
        End Get
        Set(ByVal Value As Color)
            m_DefaultBackColor = Value
        End Set
    End Property

    Public Property DefaultScreenPtr() As IntPtr
        Get
            Return m_DefaultScreenPtr
        End Get
        Set(ByVal Value As IntPtr)
            m_DefaultScreenPtr = Value
        End Set
    End Property

    Public Property DefaultBackImage() As Bitmap
        Get
            Return m_DefaultBackImage
        End Get
        Set(ByVal Value As Bitmap)
            m_DefaultBackImage = Value
        End Set
    End Property

    Public Property X() As Integer
        Get
            Return m_Loc.X
        End Get
        Set(ByVal Value As Integer)
            m_Loc.X = Value
        End Set
    End Property

    Public Property Y() As Integer
        Get
            Return m_Loc.Y
        End Get
        Set(ByVal Value As Integer)
            m_Loc.Y = Value
        End Set
    End Property

    Public Property Width() As Integer
        Get
            Return m_Size.Width
        End Get
        Set(ByVal Value As Integer)
            m_Size.Width = Value
        End Set
    End Property

    Public Property Height() As Integer
        Get
            Return m_Size.Height
        End Get
        Set(ByVal Value As Integer)
            m_Size.Height = Value
        End Set
    End Property

    Public Property CurFrame() As Integer
        Get
            Return m_CurFrame
        End Get
        Set(ByVal Value As Integer)
            If Value >= 0 And Value < m_FrameCount Then
                m_CurFrame = Value
            End If
        End Set
    End Property

    Public Property Layer() As Integer
        Get
            Return m_Layer
        End Get
        Set(ByVal Value As Integer)
            m_Layer = Value
            AddLayerToList(m_Layer)
        End Set
    End Property

    Public ReadOnly Property NumFrames() As Integer
        Get
            Return m_FrameCount
        End Get
    End Property
#End Region

#Region " Constructors "

#Region " From Path "
    Public Sub New(ByVal imagePath As String, ByVal w As Integer, ByVal h As Integer, ByVal layer As Integer, ByVal transColor As Color, ByVal direction As winFrameHandler.eFrameDirections, ByVal range As winChipRange)
        m_Loc = New Point(0, 0)
        m_CurFrame = 0
        m_DefaultScreenPtr = IntPtr.Zero
        m_Size = New Size(w, h)
        m_Image = winFrameHandler.GetFrames(imagePath, w, h, range, direction)
        m_FrameCount = m_Image.Length
        m_Layer = layer
        m_TransparentColor = transColor
        m_MoveBounds = Rectangle.Empty
        m_DefaultBackColor = Color.Empty
        m_DefaultBackImage = Nothing
        SetTransparent()
        AddLayerToList(m_Layer)
        SetID()
    End Sub

    Public Sub New(ByVal imagePath As String, ByVal w As Integer, ByVal h As Integer, ByVal layer As Integer, ByVal direction As winFrameHandler.eFrameDirections)
        Me.New(imagePath, w, h, layer, m_DeaultClearColor, direction, Nothing)
    End Sub

    Public Sub New(ByVal imagePath As String, ByVal w As Integer, ByVal h As Integer, ByVal layer As Integer, ByVal direction As winFrameHandler.eFrameDirections, ByVal range As winChipRange)
        Me.New(imagePath, w, h, layer, m_DeaultClearColor, direction, range)
    End Sub

    Public Sub New(ByVal imagePath As String, ByVal w As Integer, ByVal h As Integer, ByVal direction As winFrameHandler.eFrameDirections)
        Me.New(imagePath, w, h, 0, m_DeaultClearColor, direction, Nothing)
    End Sub

    Public Sub New(ByVal imagePath As String, ByVal w As Integer, ByVal h As Integer, ByVal direction As winFrameHandler.eFrameDirections, ByVal range As winChipRange)
        Me.New(imagePath, w, h, 0, m_DeaultClearColor, direction, range)
    End Sub

    Public Sub New(ByVal imagePath As String, ByVal w As Integer, ByVal h As Integer, ByVal layer As Integer)
        Me.New(imagePath, w, h, layer, m_DeaultClearColor, winFrameHandler.eFrameDirections.kSingleFrame, Nothing)
    End Sub

    Public Sub New(ByVal imagePath As String, ByVal w As Integer, ByVal h As Integer, ByVal layer As Integer, ByVal range As winChipRange)
        Me.New(imagePath, w, h, layer, m_DeaultClearColor, winFrameHandler.eFrameDirections.kSingleFrame, range)
    End Sub

    Public Sub New(ByVal imagePath As String, ByVal w As Integer, ByVal h As Integer)
        Me.New(imagePath, w, h, 0, m_DeaultClearColor, winFrameHandler.eFrameDirections.kSingleFrame, Nothing)
    End Sub

    Public Sub New(ByVal imagePath As String, ByVal w As Integer, ByVal h As Integer, ByVal range As winChipRange)
        Me.New(imagePath, w, h, 0, m_DeaultClearColor, winFrameHandler.eFrameDirections.kSingleFrame, range)
    End Sub

    Public Sub New(ByVal imagePath As String, ByVal w As Integer, ByVal h As Integer, ByVal layer As Integer, ByVal transColor As Color)
        Me.New(imagePath, w, h, layer, transColor, winFrameHandler.eFrameDirections.kSingleFrame, Nothing)
    End Sub

    Public Sub New(ByVal imagePath As String, ByVal w As Integer, ByVal h As Integer, ByVal layer As Integer, ByVal transColor As Color, ByVal range As winChipRange)
        Me.New(imagePath, w, h, layer, transColor, winFrameHandler.eFrameDirections.kSingleFrame, range)
    End Sub

    Public Sub New(ByVal imagePath As String, ByVal w As Integer, ByVal h As Integer, ByVal transColor As Color)
        Me.New(imagePath, w, h, 0, transColor, winFrameHandler.eFrameDirections.kSingleFrame, Nothing)
    End Sub

    Public Sub New(ByVal imagePath As String, ByVal w As Integer, ByVal h As Integer, ByVal transColor As Color, ByVal range As winChipRange)
        Me.New(imagePath, w, h, 0, transColor, winFrameHandler.eFrameDirections.kSingleFrame, range)
    End Sub

    Public Sub New(ByVal imagePath As String, ByVal w As Integer, ByVal h As Integer, ByVal transColor As Color, ByVal direction As winFrameHandler.eFrameDirections)
        Me.New(imagePath, w, h, 0, transColor, direction, Nothing)
    End Sub

    Public Sub New(ByVal imagePath As String, ByVal w As Integer, ByVal h As Integer, ByVal transColor As Color, ByVal direction As winFrameHandler.eFrameDirections, ByVal range As winChipRange)
        Me.New(imagePath, w, h, 0, transColor, direction, range)
    End Sub
#End Region

#Region " From Image "
    Public Sub New(ByVal images() As Bitmap, ByVal location As Point, ByVal size As Size, ByVal layer As Integer, ByVal startFrame As Integer, ByVal transColor As Color, ByVal defaultScreenPtr As IntPtr)
        m_Loc = location
        m_CurFrame = startFrame
        m_Size = size
        m_Image = images
        m_FrameCount = images.Length
        m_DefaultScreenPtr = defaultScreenPtr
        m_Layer = layer
        m_TransparentColor = transColor
        m_MoveBounds = Rectangle.Empty
        m_DefaultBackColor = Color.Empty
        m_DefaultBackImage = Nothing
        SetTransparent()
        AddLayerToList(m_Layer)
        SetID()
    End Sub

    Public Sub New(ByVal images() As Bitmap, ByVal defaultScreenPtr As IntPtr, ByVal layer As Integer, ByVal transColor As Color)
        Me.New(images, New Point(0, 0), images(0).Size, layer, 0, transColor, defaultScreenPtr)
    End Sub

    Public Sub New(ByVal images() As Bitmap, ByVal location As Point, ByVal size As Size, ByVal startFrame As Integer, ByVal layer As Integer, ByVal transColor As Color)
        Me.New(images, location, size, layer, startFrame, transColor, IntPtr.Zero)
    End Sub

    Public Sub New(ByVal images() As Bitmap, ByVal x As Integer, ByVal y As Integer, ByVal w As Integer, ByVal h As Integer, ByVal startFrame As Integer, ByVal layer As Integer, ByVal transColor As Color)
        Me.New(images, New Point(x, y), New Size(w, h), layer, startFrame, transColor, IntPtr.Zero)
    End Sub

    Public Sub New(ByVal images() As Bitmap, ByVal location As Point, ByVal startFrame As Integer, ByVal layer As Integer, ByVal transColor As Color)
        Me.New(images, location, images(0).Size, layer, startFrame, transColor, IntPtr.Zero)
    End Sub

    Public Sub New(ByVal images() As Bitmap, ByVal location As Point, ByVal size As Size, ByVal startFrame As Integer, ByVal transColor As Color)
        Me.New(images, location, size, 0, startFrame, transColor, IntPtr.Zero)
    End Sub

    Public Sub New(ByVal images() As Bitmap, ByVal layer As Integer, ByVal transColor As Color)
        Me.New(images, New Point(0, 0), images(0).Size, layer, 0, transColor, IntPtr.Zero)
    End Sub

    Public Sub New(ByVal images() As Bitmap, ByVal transColor As Color)
        Me.New(images, New Point(0, 0), images(0).Size, 0, 0, transColor, IntPtr.Zero)
    End Sub

    Public Sub New(ByVal images() As Bitmap, ByVal defaultScreenPtr As IntPtr, ByVal transColor As Color)
        Me.New(images, New Point(0, 0), images(0).Size, 0, 0, transColor, defaultScreenPtr)
    End Sub

    Public Sub New(ByVal images() As Bitmap, ByVal location As Point, ByVal startFrame As Integer, ByVal transColor As Color)
        Me.New(images, location, images(0).Size, 0, startFrame, transColor, IntPtr.Zero)
    End Sub

    Public Sub New(ByVal images() As Bitmap, ByVal x As Integer, ByVal y As Integer, ByVal w As Integer, ByVal h As Integer, ByVal startFrame As Integer, ByVal transColor As Color)
        Me.New(images, New Point(x, y), New Size(w, h), 0, startFrame, transColor, IntPtr.Zero)
    End Sub

    Public Sub New(ByVal images() As Bitmap)
        Me.New(images, New Point(0, 0), images(0).Size, 0, 0, m_DeaultClearColor, IntPtr.Zero)
    End Sub

    Public Sub New(ByVal images() As Bitmap, ByVal defaultScreenPtr As IntPtr)
        Me.New(images, New Point(0, 0), images(0).Size, 0, 0, m_DeaultClearColor, defaultScreenPtr)
    End Sub

    Public Sub New(ByVal images() As Bitmap, ByVal location As Point, ByVal size As Size, ByVal startFrame As Integer)
        Me.New(images, location, size, 0, startFrame, m_DeaultClearColor, IntPtr.Zero)
    End Sub

    Public Sub New(ByVal images() As Bitmap, ByVal location As Point, ByVal startFrame As Integer)
        Me.New(images, location, images(0).Size, 0, startFrame, m_DeaultClearColor, IntPtr.Zero)
    End Sub

    Public Sub New(ByVal images() As Bitmap, ByVal x As Integer, ByVal y As Integer, ByVal w As Integer, ByVal h As Integer, ByVal startFrame As Integer)
        Me.New(images, New Point(x, y), New Size(w, h), 0, startFrame, m_DeaultClearColor, IntPtr.Zero)
    End Sub

    Public Sub New(ByVal images() As Bitmap, ByVal layer As Integer)
        Me.New(images, New Point(0, 0), images(0).Size, layer, 0, m_DeaultClearColor, IntPtr.Zero)
    End Sub

    Public Sub New(ByVal images() As Bitmap, ByVal defaultScreenPtr As IntPtr, ByVal layer As Integer)
        Me.New(images, New Point(0, 0), images(0).Size, layer, 0, m_DeaultClearColor, defaultScreenPtr)
    End Sub

    Public Sub New(ByVal images() As Bitmap, ByVal location As Point, ByVal size As Size, ByVal startFrame As Integer, ByVal layer As Integer)
        Me.New(images, location, size, layer, startFrame, m_DeaultClearColor, IntPtr.Zero)
    End Sub

    Public Sub New(ByVal images() As Bitmap, ByVal location As Point, ByVal startFrame As Integer, ByVal layer As Integer)
        Me.New(images, location, images(0).Size, layer, startFrame, m_DeaultClearColor, IntPtr.Zero)
    End Sub

    Public Sub New(ByVal images() As Bitmap, ByVal x As Integer, ByVal y As Integer, ByVal w As Integer, ByVal h As Integer, ByVal startFrame As Integer, ByVal layer As Integer)
        Me.New(images, New Point(x, y), New Size(w, h), layer, startFrame, m_DeaultClearColor, IntPtr.Zero)
    End Sub
#End Region

#End Region

#Region " Subs "

#Region " Public "

#Region " Set Frame "

#Region " Fast "
    Public Overridable Sub FastSetCurFrameImediate(ByVal frameNo As Integer)
        CurFrame = frameNo
        FastRender()
    End Sub

    Public Overridable Sub FastSetCurFrameImediate(ByVal frameNo As Integer, ByVal screenPtr As IntPtr)
        CurFrame = frameNo
        FastRender(screenPtr)
    End Sub

#Region " With Color "
    Public Overridable Sub FastSetCurFrameImediate(ByVal frameNo As Integer, ByVal backColor As Color)
        CurFrame = frameNo
        FastRender(backColor)
    End Sub

    Public Overridable Sub FastSetCurFrameImediate(ByVal frameNo As Integer, ByVal screenPtr As IntPtr, ByVal backColor As Color)
        CurFrame = frameNo
        FastRender(screenPtr, backColor)
    End Sub

    Public Overridable Sub FastSetCurFrameImediate(ByVal frameNo As Integer, ByVal gra As Graphics, ByVal backColor As Color, Optional ByVal disposeGraphics As Boolean = False)
        CurFrame = frameNo
        FastRender(gra, backColor, disposeGraphics)
    End Sub
#End Region

#Region " With Image "
    Public Overridable Sub FastSetCurFrameImediate(ByVal frameNo As Integer, ByVal backImg As Bitmap)
        CurFrame = frameNo
        FastRender(backImg)
    End Sub

    Public Overridable Sub FastSetCurFrameImediate(ByVal frameNo As Integer, ByVal screenPtr As IntPtr, ByVal backImg As Bitmap)
        CurFrame = frameNo
        FastRender(screenPtr, backImg)
    End Sub

    Public Overridable Sub FastSetCurFrameImediate(ByVal frameNo As Integer, ByVal gra As Graphics, ByVal backImg As Bitmap, Optional ByVal disposeGraphics As Boolean = False)
        CurFrame = frameNo
        FastRender(gra, backImg, disposeGraphics)
    End Sub
#End Region

#End Region

    Public Overridable Sub SetCurFrameImediate(ByVal frameNo As Integer)
        CurFrame = frameNo
        Render()
    End Sub

    Public Overridable Sub SetCurFrameImediate(ByVal frameNo As Integer, ByVal screenPtr As IntPtr)
        CurFrame = frameNo
        Render(screenPtr)
    End Sub

#Region " With Color "
    Public Overridable Sub SetCurFrameImediate(ByVal frameNo As Integer, ByVal backColor As Color)
        CurFrame = frameNo
        Render(backColor)
    End Sub

    Public Overridable Sub SetCurFrameImediate(ByVal frameNo As Integer, ByVal screenPtr As IntPtr, ByVal backColor As Color)
        CurFrame = frameNo
        Render(screenPtr, backColor)
    End Sub

    Public Overridable Sub SetCurFrameImediate(ByVal frameNo As Integer, ByVal gra As Graphics, ByVal backColor As Color, Optional ByVal disposeGraphics As Boolean = False)
        CurFrame = frameNo
        Render(gra, backColor, disposeGraphics)
    End Sub
#End Region

#Region " With Image "
    Public Overridable Sub SetCurFrameImediate(ByVal frameNo As Integer, ByVal backImg As Bitmap)
        CurFrame = frameNo
        Render(backImg)
    End Sub

    Public Overridable Sub SetCurFrameImediate(ByVal frameNo As Integer, ByVal screenPtr As IntPtr, ByVal backImg As Bitmap)
        CurFrame = frameNo
        Render(screenPtr, backImg)
    End Sub

    Public Overridable Sub SetCurFrameImediate(ByVal frameNo As Integer, ByVal gra As Graphics, ByVal backImg As Bitmap, Optional ByVal disposeGraphics As Boolean = False)
        CurFrame = frameNo
        Render(gra, backImg, disposeGraphics)
    End Sub
#End Region

#End Region

#Region " Render "

#Region " Fast "
    Public Overridable Sub FastRender()
        If m_DefaultBackImage Is Nothing Then
            FastRender(m_DefaultScreenPtr, m_DefaultBackColor)
        Else
            FastRender(m_DefaultScreenPtr, m_DefaultBackImage)
        End If
    End Sub

    Public Overridable Sub FastRender(ByVal screnPtr As IntPtr)
        If m_DefaultBackImage Is Nothing Then
            FastRender(screnPtr, m_DefaultBackColor)
        Else
            FastRender(screnPtr, m_DefaultBackImage)
        End If
    End Sub

#Region " With Color "
    Public Overridable Sub FastRender(ByVal backColor As Color)
        FastRender(m_DefaultScreenPtr, backColor)
    End Sub

    Public Overridable Sub FastRender(ByVal screenPtr As IntPtr, ByVal backColor As Color)
        If screenPtr.Equals(IntPtr.Zero) Then Exit Sub

        Dim theGraphics As Graphics = Graphics.FromHwnd(screenPtr)
        FastRender(theGraphics, backColor, False)
        theGraphics.Dispose()
    End Sub

    Public Overridable Sub FastRender(ByVal gra As Graphics, ByVal backColor As Color, Optional ByVal disposeGraphics As Boolean = False)
        [Erase](gra, backColor, False)
        gra.DrawImageUnscaled(m_Image(m_CurFrame), New Rectangle(m_Loc.X, m_Loc.Y, m_Size.Width, m_Size.Height))
        If disposeGraphics Then gra.Dispose()
    End Sub
#End Region

#Region " With Image "
    Public Overridable Sub FastRender(ByVal backImg As Bitmap)
        FastRender(m_DefaultScreenPtr, backImg)
    End Sub

    Public Overridable Sub FastRender(ByVal screenPtr As IntPtr, ByVal backImg As Bitmap)
        If screenPtr.Equals(IntPtr.Zero) Then Exit Sub

        Dim theGraphics As Graphics = Graphics.FromHwnd(screenPtr)
        FastRender(theGraphics, backImg, False)
        theGraphics.Dispose()
    End Sub

    Public Overridable Sub FastRender(ByVal gra As Graphics, ByVal backImg As Bitmap, Optional ByVal disposeGraphics As Boolean = False)
        [Erase](gra, backImg, False)
        gra.DrawImageUnscaled(m_Image(m_CurFrame), New Rectangle(m_Loc.X, m_Loc.Y, m_Size.Width, m_Size.Height))
        If disposeGraphics Then gra.Dispose()
    End Sub
#End Region

#End Region

    Public Overridable Sub Render()
        If m_DefaultBackImage Is Nothing Then
            Render(m_DefaultScreenPtr, m_DefaultBackColor)
        Else
            Render(m_DefaultScreenPtr, m_DefaultBackImage)
        End If
    End Sub

    Public Overridable Sub Render(ByVal screnPtr As IntPtr)
        If m_DefaultBackImage Is Nothing Then
            Render(screnPtr, m_DefaultBackColor)
        Else
            Render(screnPtr, m_DefaultBackImage)
        End If
    End Sub

#Region " With Color "
    Public Overridable Sub Render(ByVal backColor As Color)
        Render(m_DefaultScreenPtr, backColor)
    End Sub

    Public Overridable Sub Render(ByVal screenPtr As IntPtr, ByVal backColor As Color)
        If screenPtr.Equals(IntPtr.Zero) Then Exit Sub

        Dim theGraphics As Graphics = Graphics.FromHwnd(screenPtr)
        Render(theGraphics, backColor, False)
        theGraphics.Dispose()
    End Sub

    Public Overridable Sub Render(ByVal gra As Graphics, ByVal backColor As Color, Optional ByVal disposeGraphics As Boolean = False)
        [Erase](gra, backColor, False)
        gra.DrawImage(m_Image(m_CurFrame), New Rectangle(m_Loc.X, m_Loc.Y, m_Size.Width, m_Size.Height))
        If disposeGraphics Then gra.Dispose()
    End Sub
#End Region

#Region " With Image "
    Public Overridable Sub Render(ByVal backImg As Bitmap)
        Render(m_DefaultScreenPtr, backImg)
    End Sub

    Public Overridable Sub Render(ByVal screenPtr As IntPtr, ByVal backImg As Bitmap)
        If screenPtr.Equals(IntPtr.Zero) Then Exit Sub

        Dim theGraphics As Graphics = Graphics.FromHwnd(screenPtr)
        Render(theGraphics, backImg, False)
        theGraphics.Dispose()
    End Sub

    Public Overridable Sub Render(ByVal gra As Graphics, ByVal backImg As Bitmap, Optional ByVal disposeGraphics As Boolean = False)
        [Erase](gra, backImg, False)
        gra.DrawImage(m_Image(m_CurFrame), New Rectangle(m_Loc.X, m_Loc.Y, m_Size.Width, m_Size.Height))
        If disposeGraphics Then gra.Dispose()
    End Sub
#End Region

#End Region

#Region " Erase "
    Public Overridable Sub [Erase]()
        If m_DefaultBackImage Is Nothing Then
            [Erase](m_DefaultScreenPtr, m_DefaultBackColor)
        Else
            [Erase](m_DefaultScreenPtr, m_DefaultBackImage)
        End If
    End Sub

    Public Overridable Sub [Erase](ByVal screnPtr As IntPtr)
        If m_DefaultBackImage Is Nothing Then
            [Erase](screnPtr, m_DefaultBackColor)
        Else
            [Erase](screnPtr, m_DefaultBackImage)
        End If
    End Sub

#Region " With Color "
    Public Overridable Sub [Erase](ByVal backColor As Color)
        [Erase](m_DefaultScreenPtr, backColor)
    End Sub

    Public Overridable Sub [Erase](ByVal screenPtr As IntPtr, ByVal backColor As Color)
        If screenPtr.Equals(IntPtr.Zero) Then Exit Sub

        Dim theGraphics As Graphics = Graphics.FromHwnd(screenPtr)
        [Erase](theGraphics, backColor, False)
        theGraphics.Dispose()
    End Sub

    Public Overridable Sub [Erase](ByVal gra As Graphics, ByVal backColor As Color, Optional ByVal disposeGraphics As Boolean = False)
        Dim rect As New Rectangle(m_Loc.X, m_Loc.Y, m_Size.Width, m_Size.Height)

        gra.FillRectangle(New Drawing.SolidBrush(backColor), rect)
        If disposeGraphics Then gra.Dispose()
    End Sub
#End Region

#Region " With Image "
    Public Overridable Sub [Erase](ByVal backImg As Bitmap)
        [Erase](m_DefaultScreenPtr, backImg)
    End Sub

    Public Overridable Sub [Erase](ByVal screenPtr As IntPtr, ByVal backImg As Bitmap)
        If screenPtr.Equals(IntPtr.Zero) Then Exit Sub

        Dim theGraphics As Graphics = Graphics.FromHwnd(screenPtr)
        [Erase](theGraphics, backImg, False)
        theGraphics.Dispose()
    End Sub

    Public Overridable Sub [Erase](ByVal gra As Graphics, ByVal backImg As Bitmap, Optional ByVal disposeGraphics As Boolean = False)
        Dim rect As New Rectangle(m_Loc.X, m_Loc.Y, m_Size.Width, m_Size.Height)
        Dim imgRect As New Rectangle(0, 0, backImg.Width, backImg.Height)

        If imgRect.Contains(rect) Then
            gra.DrawImage(backImg, rect, rect, GraphicsUnit.Pixel)
        Else
            gra.DrawImage(backImg, rect)
        End If

        If disposeGraphics Then gra.Dispose()
    End Sub
#End Region

#End Region

#Region " Move "
    Public Overridable Sub Move()
        If Not m_MoveDelta.Equals(Point.Empty) Then
            MoveBy(m_MoveDelta.X, m_MoveDelta.Y)
        End If
    End Sub

    Public Overridable Sub MoveBy(ByVal dx As Integer, ByVal dy As Integer)
        MoveTo(X + dx, Y + dy)
    End Sub

    Public Overridable Sub MoveTo(ByVal x As Integer, ByVal y As Integer)
        If Not m_MoveBounds.Equals(Rectangle.Empty) Then
            If m_MoveBounds.Contains(New Rectangle(x, y, m_Size.Width, m_Size.Height)) Then
                m_Loc.X = x
                m_Loc.Y = y
            End If
        Else
            m_Loc.X = x
            m_Loc.Y = y
        End If
    End Sub
#End Region

    Public Overridable Sub HandleKeyPress(ByVal theKey As winKeyboardAPI.eKeys, ByVal isDown As Boolean)
        'must override to handle key presses
    End Sub

    Public Overridable Sub Dispose(Optional ByVal doErase As Boolean = True)
        If doErase Then [Erase]()
        If Not m_Image Is Nothing Then
            Array.Clear(m_Image, 0, m_Image.Length)
            m_Image = Nothing
        End If
    End Sub
#End Region

#Region " Private "
    Private Sub SetID()
        m_SpriteID = m_SpriteIDs
        m_SpriteIDs = (m_SpriteIDs + 1) Mod Long.MaxValue
    End Sub

    Private Sub SetTransparent()
        For Each b As Bitmap In m_Image
            b.MakeTransparent(m_TransparentColor)
        Next b
    End Sub

    Private Sub AddLayerToList(ByVal layer As Integer)
        If Not m_LayerList.Contains(layer) Then
            m_LayerList.Add(layer)
            m_LayerList.Sort()
        End If
    End Sub
#End Region

#End Region

#Region " Functions "
    'belive it or not this is the same as saying the average of
    'Me.Width/4 + Me.Height/4...so we move ~1/4 of our size per frame

    '1.) cint((cint(Me.Width/4) + cint(Me.Height/4))/2)
    '2.) ((Me.Width>>2) + (Me.Height>>2))>>1
    '3.) ((Me.Width + Me.Height)>>2)>>1
    '4.) (Me.Width + Me.Height) >> 3
    Public Overridable Function RecomendedSpeed()
        Return (Me.Width + Me.Height) >> 3
    End Function

    Public Overridable Function RecomendedSpeedX()
        Return Me.Width >> 2
    End Function

    Public Overridable Function RecomendedSpeedY()
        Return Me.Height >> 2
    End Function
#End Region

#Region " Overloads "
    Public Overridable Overloads Function Equals(ByVal theSprite As winSprite) As Boolean
        Return m_SpriteID = theSprite.m_SpriteID
    End Function
#End Region

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) www.ruskin.com
United States United States
PC Programmer/Analyst

Comments and Discussions