Quick Question {sprites and maps - slow}

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

Iceplug
09-06-2004, 09:02 AM
Perhaps it is because all of the pictures are constantly being loaded from file. This is not efficient. It would be much faster to preload all of the images into an image array and instead of reading filenames from the map file, you would just read in a number. Then, use this number to access a specific element of the array.
This way, all of the pictures are held in a bitmap array.

You'd have to create the array like this:

Dim Bmps() As Bitmap

Bmps = New Bitmap() {New Bitmap("tiles\picture1.bmp"), New Bitmap("tiles\picture2.bmp"), New Bitmap(...), New Bitmap(...), ... }

:)

Talus123
09-06-2004, 09:08 AM
So you mean changing the map background?
Right now I have a text document that has numbers like 1, 2, 1, 2, 3, 1, 1, 3
1 = light green grass
2 = dark green grass
3 = mountin

So you mean rather than having it like that just have it preloaded as one bitmap? Rather than the text document?

Iceplug
09-06-2004, 09:12 AM
No, I mean having all of the tiles that you need preloaded into the array.
Then, instead of creating a New Bitmap for your DrawImage, you just pick a picture from the tile array--like, Bmps(1) for light green grass, Bmps(2) for dark green grass.
And then, just draw that. :)

Talus123
09-06-2004, 09:19 AM
Ok, Im sort of getting you but Im still a little confused so that code you posted before where does it go? Is the Dim Bmp... global?






Maybe if you could post a sample code of what you mean, I learn best that way. Ill understand if you dont have the time, but it would be very helpfull

Iceplug
09-06-2004, 10:16 AM
Yes, Bmps is global. And a main part of development with .NET involves logically figuring out where code goes, I guess.
But, naturally, a global declaration such as Dim Bmps() As Bitmap goes into the declarations section of the form. A procedure declaration goes inside the procedure, but we are not using that.
For instantiating the declarations that you make globally, you will probably want to do that in Form_Load.
Bmps = New Bitmap() {New Bitmap("tiles\picture1.bmp"), New Bitmap("tiles\picture2.bmp"), New Bitmap(...), New Bitmap(...), ... }

Talus123
09-06-2004, 10:19 AM
ok thx i got it thanks for the help

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum