Option Explicit On
Public Class frmMain
Inherits System.Windows.Forms.Form
Private Structure RECT
Dim Left As Integer
Dim Right As Integer
Dim Top As Integer
Dim Bottom As Integer
End Structure
Private Structure Vector
Dim X As Single
Dim Y As Single
End Structure
Private Structure Map_Type
<VBFixedString(50)> Dim Name As String
Dim Initial_Position As Vector 'Used for if you click to move the map with the mouse youll use a new set of map position coordinates.
Dim Position As Vector 'The map position on screen which is typically negative as it goes more right.
Dim Tile(,) As Integer 'The tile image number used in Tile(X,Y) 'ex. Tile(0,0) = 0 "grass" Tile(2,5) = 1 "Rock"
Dim Width As Integer 'Width of map in tiles
Dim Height As Integer 'Height of map in tiles
<VBFixedString(256)> Dim Texture_List() As String 'File path of textures
Dim Number_of_Textures As Integer
Dim Screen_Tile_Width As Integer 'Number of tiles possible to display on screen horizontally
Dim Screen_Tile_Height As Integer 'Number of tiles possible to display on screen vrtically
End Structure
Private Const TILE_WIDTH As Integer = 50
Private Const TILE_HEIGHT As Integer = 50
Dim Map As New Map_Type()
Dim Running As Boolean
Dim Backbuffer As Bitmap
Dim G As Graphics
Private Sub Setup_Map()
Map.Width = 64
Map.Height = 64
Map.Screen_Tile_Width = 32
Map.Screen_Tile_Height = 16
Map.Number_of_Textures = 2
Map.Tile = New Integer(Map.Width, Map.Height) {}
Map.Texture_List = New String(Map.Number_of_Textures) {}
Map.Texture_List(0) = Application.StartupPath() & "\Graphics\grass.bmp"
Map.Texture_List(1) = Application.StartupPath() & "\Graphics\rock.bmp"
'Normally its loaded from file but you can manually put where the images go here
Dim X As Integer, Y As Integer
For Y = 0 To Map.Height
For X = 0 To Map.Width
Map.Tile(X, Y) = 0
Next X
Next Y
End Sub
Private Sub Draw_Map()
Dim imageFile As Image
Dim g As Graphics
Dim newGraphics As Graphics
Dim Coordinates As Vector 'Tile Coordinates of the position on map such as 5,10 or 24, 8
Dim X1 As Integer, Y1 As Integer, X2 As Integer, Y2 As Integer
Dim X As Integer, Y As Integer
Dim R As RECT
R.Left = 0
R.Top = 0
R.Right = Me.Width
R.Bottom = Me.Height
Coordinates.X = Int(-(Map.Position.X) / TILE_WIDTH)
Coordinates.Y = Int(-(Map.Position.Y) / TILE_HEIGHT) 'converts your position into tile coordinates
'This here allows the world to be as gigantic as you want with zero slow down like 5000x5000 for example. Only draws what is on screen.
X1 = Coordinates.X
Y1 = Coordinates.Y
X2 = Coordinates.X + Map.Screen_Tile_Width
Y2 = Coordinates.Y + Map.Screen_Tile_Height
If X2 <= 0 Then X2 = 0
If Y2 <= 0 Then Y2 = 0
If Y2 >= Map.Height - 1 Then Y2 = Map.Height - 1
If X2 >= Map.Width - 1 Then X2 = Map.Width - 1
If X1 <= 0 Then X1 = 0
If Y1 <= 0 Then Y1 = 0
If X1 >= X2 Then X1 = X2
If Y1 >= Y2 Then Y1 = Y2
For Y = Y1 To Y2
For X = X1 To X2
If Running = True Then
If Not ((Map.Position.X + ((TILE_WIDTH * X) + TILE_WIDTH) < R.Left) Or (Map.Position.X + ((TILE_WIDTH * X)) > R.Right) Or (Map.Position.Y + ((TILE_HEIGHT * Y) + TILE_HEIGHT) < R.Top) Or (Map.Position.Y + ((TILE_HEIGHT * Y)) > R.Bottom)) Then
imageFile = Image.FromFile(Map.Texture_List(Map.Tile(X, Y)))
g = Me.CreateGraphics()
newGraphics = Graphics.FromImage(imageFile)
g.DrawImage(imageFile, New RectangleF(Map.Position.X + (TILE_WIDTH * X), Map.Position.Y + (TILE_HEIGHT * Y), TILE_WIDTH + 3, TILE_HEIGHT + 3))
newGraphics.Dispose()
g.Dispose()
End If
End If
Next X
Next Y
End Sub
Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Left Then Map.Position.X += 10
If e.KeyCode = Keys.Right Then Map.Position.X -= 10
If e.KeyCode = Keys.Up Then Map.Position.Y += 10
If e.KeyCode = Keys.Down Then Map.Position.Y -= 10
End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Show()
Me.DoubleBuffered = True
Setup_Map()
Running = True
Backbuffer = New Bitmap(Width, Height, Me.CreateGraphics())
G = Graphics.FromImage(Backbuffer)
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
Running = False
Application.Exit()
End Sub
Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.NearestNeighbor
e.Graphics.PixelOffsetMode = Drawing2D.PixelOffsetMode.None
Do While Running = True
e.Graphics.Clear(Color.Black)
Draw_Map()
System.Windows.Forms.Application.DoEvents()
Loop
End Sub
End Class