Public Class RenderController
    Dim ghostlocation As String = Application.StartupPath & "\ghost\" & ReadConfig("ghostfolder") & "\"
    Dim eyelayer As Integer = 0

    Public Function LoadBitMap(ByVal name As String)


        If name.Contains(".png") Then
            name = name.Replace(".png", Nothing)
        End If
        Dim BMP As Bitmap

        If My.Computer.FileSystem.FileExists(ghostlocation & name & ".png") Then
            BMP = Image.FromFile(ghostlocation & name & ".png")
        ElseIf My.Computer.FileSystem.FileExists(ghostlocation & "\inf\" & name & ".png") Then
            BMP = Image.FromFile(ghostlocation & "\inf\" & name & ".png")
        Else
            'MsgBox(ghostlocation & name & ".png")
            BMP = Image.FromFile(Application.StartupPath & "\ghost\TR.png")
        End If
        Return BMP
    End Function

    Private Sub refreshimage(ByVal background As Bitmap, ByVal layer1 As Bitmap, ByVal layer2 As Bitmap, ByVal layer3 As Bitmap, ByVal l1x As Integer, ByVal l1y As Integer, ByVal l2x As Integer, ByVal l2y As Integer, ByVal l3x As Integer, ByVal l3y As Integer)
        Dim BMP As New Bitmap(228, 355)

        'create a graphic base on that
        Dim GR As Graphics = Graphics.FromImage(BMP)

        'draw onto your bmp starting from the background
        GR.DrawImage(My.Resources.TR, 0, 0)

        'set X,y to the coordinate you want your girl to appear
        GR.DrawImage(background, 0, 0)
        GR.DrawImage(layer1, l1x, l1y)
        GR.DrawImage(layer2, l2x, l2y)
        GR.DrawImage(layer3, l3x, l3y)
        'clear the picturebox
        ghost.BackgroundImage = Nothing

        'now that we have draw all our image onto the same bitmap, assign it to your picturebox element
        ghost.BackgroundImage = BMP
        ghost.Refresh()
    End Sub

    Function ResizeBitmap(ByVal bitmapToResize As Bitmap)
        Dim bm_dest As New Bitmap(228, 355)

        ' Make a Graphics object for the result Bitmap.
        Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

        ' Copy the source image into the destination bitmap.
        gr_dest.DrawImage(bitmapToResize, 0, 0, 228, 355)

        ' Display the result.
        Return bm_dest
    End Function

    Public Sub UpdateRender(clothesid As Integer, clothesidl2 As Integer, expressionid As Integer)
        ' DebugWindow.TextBox1.AppendText("clothid=" & clothesid & vbNewLine & "expressionid=" & expressionid & vbNewLine)
        Dim bg As Bitmap
        bg = LoadBitMap("surface0")
        bg = ResizeBitmap(bg)

        Dim l1 As Bitmap
        If clothesid = 0 Then
            l1 = LoadBitMap("surface" & ghost.clothid)
            l1 = ResizeBitmap(l1)
        Else
            l1 = LoadBitMap("surface" & clothesid)
            l1 = ResizeBitmap(l1)
            ghost.clothid = clothesid
        End If
        Dim l2 As Bitmap
        If clothesidl2 = 0 Then
            l2 = LoadBitMap("TR")
            l2 = ResizeBitmap(l2)
        Else
            l2 = LoadBitMap("surface" & clothesidl2)
            l2 = ResizeBitmap(l2)
        End If
        Dim l3 As Bitmap
        If expressionid = 0 Then
            l3 = LoadBitMap("TR")
            l3 = ResizeBitmap(l3)
        Else
            l3 = LoadBitMap("surface" & expressionid)
            l3 = ResizeBitmap(l3)

            If expressionid = 101 Or expressionid = 100 Then
                'These two are exceptional case that will not be stored as "Motion Before"
            Else
                ghost.expressid = expressionid
            End If

        End If

        refreshimage(bg, l1, l2, l3, 0, 0, 0, 0, 0, 0)


    End Sub
    Public Function ReadConfig(item As String)
        Try
            Dim arc As New ArConfig
            Dim al As String = Application.StartupPath & "\config\"
            Return arc.ReadConfig(al, item).ToString.Trim
        Catch ex As Exception
            Return Nothing
        End Try

    End Function
    Public Sub delay(second As String)
        Dim i As Integer = 0
        While i < second * 1000
            i += 10
            Threading.Thread.Sleep(10)
            Application.DoEvents()
        End While
    End Sub
End Class
