Talus123
09-06-2004, 07:55 AM
Hello again everybody,
I am posting this time because well I have another question. You see Im experimenting with sprites and maps together (sort of combining a few tutorials from vbprogramming.8k.com) However when ever I run this program it goes very slow and laggy. My question is if anyone knows why this is happening, what I can do to speed it up, and how to fix it. If someone could help me out, I d be very happy hehe. Anyway heres a copy of the code if you want to take a look at it
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Dim SR As StreamReader
Dim SW As StreamWriter
Dim NumColumns As Integer
Dim NumRows As Integer
Dim Tiles(,) As String
Dim Pos As Point
Dim Loc As String
Dim Dir As String
Dim Frame As Integer
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
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
Public Sub Read()
SR = New StreamReader("test.map")
Dim ln As String
ln = SR.ReadLine() 'This is where the "22,16" is
Dim str() As String
str = ln.Split(",")
NumColumns = str(0) - 1
NumRows = str(1) - 1
ReDim Tiles(NumColumns, NumRows)
Dim CurrentRow As Integer
Dim CurrentColumn As Integer
For CurrentRow = 0 To NumRows
'Read each Line
ln = SR.ReadLine()
Dim Line = ln.Split(",")
For CurrentColumn = 0 To NumColumns
'Read each character (Go across the file)
Tiles(CurrentColumn, CurrentRow) = ("tiles\" & Line(CurrentColumn) & ".jpg")
Next
Next
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()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(656, 478)
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Read()
Loc = "link/down1.bmp"
SetStyle(ControlStyles.DoubleBuffer, True)
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
End Sub
Private Sub Form1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim x, y As Integer
For x = 0 To numcolumns
For y = 0 To numrows
e.Graphics.DrawImage(New Bitmap(Tiles(x, y)), x * 30, y * 30)
Next
Next
Dim Bmp As New Bitmap(Loc)
Bmp.MakeTransparent(Color.White)
e.Graphics.DrawImage(New Bitmap(Bmp), Pos)
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles MyBase.KeyDown
Me.Invalidate()
If e.KeyCode = Keys.Left Then
Pos.X -= 5
Dir = "left"
Frame += 1
If Frame = 10 Then Frame = 1
Loc = "link/" & Dir & Frame & ".bmp"
End If
If e.KeyCode = Keys.Right Then
Pos.X += 5
Dir = "right"
Frame += 1
If Frame = 10 Then Frame = 1
Loc = "link/" & Dir & Frame & ".bmp"
End If
If e.KeyCode = Keys.Up Then
Pos.Y -= 5
Dir = "Up"
Frame += 1
If Frame = 10 Then Frame = 1
Loc = "link/" & Dir & Frame & ".bmp"
End If
If e.KeyCode = Keys.Down Then
Pos.Y += 5
Dir = "Down"
Frame += 1
If Frame = 10 Then Frame = 1
Loc = "link/" & Dir & Frame & ".bmp"
End If
End Sub
End Class
I am posting this time because well I have another question. You see Im experimenting with sprites and maps together (sort of combining a few tutorials from vbprogramming.8k.com) However when ever I run this program it goes very slow and laggy. My question is if anyone knows why this is happening, what I can do to speed it up, and how to fix it. If someone could help me out, I d be very happy hehe. Anyway heres a copy of the code if you want to take a look at it
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Dim SR As StreamReader
Dim SW As StreamWriter
Dim NumColumns As Integer
Dim NumRows As Integer
Dim Tiles(,) As String
Dim Pos As Point
Dim Loc As String
Dim Dir As String
Dim Frame As Integer
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
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
Public Sub Read()
SR = New StreamReader("test.map")
Dim ln As String
ln = SR.ReadLine() 'This is where the "22,16" is
Dim str() As String
str = ln.Split(",")
NumColumns = str(0) - 1
NumRows = str(1) - 1
ReDim Tiles(NumColumns, NumRows)
Dim CurrentRow As Integer
Dim CurrentColumn As Integer
For CurrentRow = 0 To NumRows
'Read each Line
ln = SR.ReadLine()
Dim Line = ln.Split(",")
For CurrentColumn = 0 To NumColumns
'Read each character (Go across the file)
Tiles(CurrentColumn, CurrentRow) = ("tiles\" & Line(CurrentColumn) & ".jpg")
Next
Next
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()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(656, 478)
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Read()
Loc = "link/down1.bmp"
SetStyle(ControlStyles.DoubleBuffer, True)
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
End Sub
Private Sub Form1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim x, y As Integer
For x = 0 To numcolumns
For y = 0 To numrows
e.Graphics.DrawImage(New Bitmap(Tiles(x, y)), x * 30, y * 30)
Next
Next
Dim Bmp As New Bitmap(Loc)
Bmp.MakeTransparent(Color.White)
e.Graphics.DrawImage(New Bitmap(Bmp), Pos)
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles MyBase.KeyDown
Me.Invalidate()
If e.KeyCode = Keys.Left Then
Pos.X -= 5
Dir = "left"
Frame += 1
If Frame = 10 Then Frame = 1
Loc = "link/" & Dir & Frame & ".bmp"
End If
If e.KeyCode = Keys.Right Then
Pos.X += 5
Dir = "right"
Frame += 1
If Frame = 10 Then Frame = 1
Loc = "link/" & Dir & Frame & ".bmp"
End If
If e.KeyCode = Keys.Up Then
Pos.Y -= 5
Dir = "Up"
Frame += 1
If Frame = 10 Then Frame = 1
Loc = "link/" & Dir & Frame & ".bmp"
End If
If e.KeyCode = Keys.Down Then
Pos.Y += 5
Dir = "Down"
Frame += 1
If Frame = 10 Then Frame = 1
Loc = "link/" & Dir & Frame & ".bmp"
End If
End Sub
End Class