11-10-2006, 03:28 PM
By: William
Difficulty: 3/5 (Medium)
Improvement: Increases the speed in your game. The bigger maps and the more layers you have, the more this code will speed the game up.
Introduction
As it is now, the whole game screen is being covered with tiles every second. So I thought, wouldn't it be better if only the tiles that needs to be updated are redrawn. Thats basicly what this tutorial will do. It will blit the tiles around you, nothing else. If your game has a larger player/enemy size. Then you need to edit the xChange1-2 and yChange1-2. That might be a bit tricky if you dont understand the code. But just ask and I help you out.
Hopefully this will speed up the game
Tell me if it dont.
Backup your source before adding this, you never know what things can do
But in order for this system to work, these things need to be changed/added:
Start with adding this at the top of Public Sub GameLoop():
Replace:
With this code below. This basicly checks were your player is and we now blit the tiles around him instead of on the whole map.
Now, change this:
To the code below. This part is pretty much as the above one, but this does it for the fringe layer instead.
Now, above this line:
Add the code below. This will make you able to write to the chat. If your using textboxes for your chat (both the writing part and the text part) then you dont need to add this.
Now remove:
So now above this:
Add the code below. This will blit the tiles around the enemy, similar to the technique used for the player before.
So now below this:
Add this code:
Find this:
In the end of that (before exit sub) add the code below. This will make it so that the map is updated when the enemies is set to be respawned.
Now add these in a module (modGlobal):
Now find:
Below that, add the code below:
Now, below this:
Add this code:
That covers 8 of the 8 points in the begining of the topic. Thats it
Should be working fine.
Difficulty: 3/5 (Medium)
Improvement: Increases the speed in your game. The bigger maps and the more layers you have, the more this code will speed the game up.
Introduction
As it is now, the whole game screen is being covered with tiles every second. So I thought, wouldn't it be better if only the tiles that needs to be updated are redrawn. Thats basicly what this tutorial will do. It will blit the tiles around you, nothing else. If your game has a larger player/enemy size. Then you need to edit the xChange1-2 and yChange1-2. That might be a bit tricky if you dont understand the code. But just ask and I help you out.
Hopefully this will speed up the game

Backup your source before adding this, you never know what things can do

But in order for this system to work, these things need to be changed/added:
- BltTile Change [Completed]
- BltFringe Change [Completed]
- Blt tile to cover the text (if your using the inbuilt system with the text on the screen. You dont need to change this if you use a textbox instead). Add [Completed]
- Blt tiles around the enemies when they move. Add [Completed]
- When saving the map - refill the screen. Add [Completed]
- Need to blit around the other players when they move.Add
- Refresh the screen when you enter a new map Add [Completed]
- /Respawn command needs to have bltTile and bltFringe. Add [Completed]
Start with adding this at the top of Public Sub GameLoop():
Code:
Dim xChange1 As Byte, xChange2 As Byte, yChange1 As Byte, yChange2 As Byte
Dim x1 As Long, y1 As Long
Replace:
Code:
' Blit out tiles layers ground/anim1/anim2
For y = 0 To MAX_MAPY
For x = 0 To MAX_MAPX
Call BltTile(x, y)
Next x
Next y
Code:
If GraphicInput1 = 0 Then
GraphicInput1 = 1
For y = 0 To MAX_MAPY
For x = 0 To MAX_MAPX
Call BltTile(x, y)
Next x
Next y
Else
If InEditor Then
For y = 0 To MAX_MAPY
For x = 0 To MAX_MAPX
Call BltTile(x, y)
Next x
Next y
Else
yChange1 = 2
yChange2 = 2
xChange1 = 2
xChange2 = 2
If GetPlayerY(MyIndex) - 2 < 0 Then yChange1 = 1
If GetPlayerY(MyIndex) + 2 > MAX_MAPY Then yChange2 = 1
If GetPlayerX(MyIndex) - 2 < 1 Then xChange1 = 1
If GetPlayerX(MyIndex) + 2 > MAX_MAPX Then xChange2 = 1
If GetPlayerY(MyIndex) - 1 < 0 Then yChange1 = 0
If GetPlayerY(MyIndex) + 1 > MAX_MAPY Then yChange2 = 0
If GetPlayerX(MyIndex) - 1 < 0 Then xChange1 = 0
If GetPlayerX(MyIndex) + 1 > MAX_MAPX Then xChange2 = 0
For y = GetPlayerY(MyIndex) - yChange1 To GetPlayerY(MyIndex) + yChange2
For x = GetPlayerX(MyIndex) - xChange1 To GetPlayerX(MyIndex) + xChange2
Call BltTile(x, y)
Next x
Next y
End If
End If
Now, change this:
Code:
' Blit out tile layer fringe
For y = 0 To MAX_MAPY
For x = 0 To MAX_MAPX
Call BltFringeTile(x, y)
Next x
Next y
Code:
' Blit out tile layer fringe
If GraphicInput2 = 0 Then
GraphicInput2 = 1
For y = 0 To MAX_MAPY
For x = 0 To MAX_MAPX
Call BltFringeTile(x, y)
Next x
Next y
Else
If InEditor Then
For y = 0 To MAX_MAPY
For x = 0 To MAX_MAPX
Call BltFringeTile(x, y)
Next x
Next y
Else
yChange1 = 2
yChange2 = 2
xChange1 = 2
xChange2 = 2
If GetPlayerY(MyIndex) - 2 < 0 Then yChange1 = 1
If GetPlayerY(MyIndex) + 2 > MAX_MAPY Then yChange2 = 1
If GetPlayerX(MyIndex) - 2 < 1 Then xChange1 = 1
If GetPlayerX(MyIndex) + 2 > MAX_MAPX Then xChange2 = 1
If GetPlayerY(MyIndex) - 1 < 0 Then yChange1 = 0
If GetPlayerY(MyIndex) + 1 > MAX_MAPY Then yChange2 = 0
If GetPlayerX(MyIndex) - 1 < 0 Then xChange1 = 0
If GetPlayerX(MyIndex) + 1 > MAX_MAPX Then xChange2 = 0
For y = GetPlayerY(MyIndex) - yChange1 To GetPlayerY(MyIndex) + yChange2
For x = GetPlayerX(MyIndex) - xChange1 To GetPlayerX(MyIndex) + xChange2
Call BltFringeTile(x, y)
Next x
Next y
End If
End If
Code:
' Blit out tiles layers ground/anim1/anim2
Code:
' Blit Tile to cover text
For x = 0 To MAX_MAPX
Call BltTile(x, MAX_MAPY)
Next x
Now remove:
Code:
' Blit out the items
For i = 1 To MAX_MAP_ITEMS
If MapItem(i).Num > 0 Then
Call BltItem(i)
End If
Next i
So now above this:
Code:
' Blit out the npcs
For i = 1 To MAX_MAP_NPCS
Call BltNpc(i)
Next i
Code:
' Blit out the tiles around the npc
For i = 1 To MAX_MAP_NPCS
' Make sure that theres an npc there, and if not exit the sub
If MapNpc(i).Num > 0 Then
' Blit tiles around the npc
yChange1 = 2
yChange2 = 1
xChange1 = 1
xChange2 = 1
If MapNpc(i).y - 2 < 0 Then yChange1 = 1
If MapNpc(i).y + 2 > MAX_MAPY Then yChange2 = 1
If MapNpc(i).x - 2 < 1 Then xChange1 = 1
If MapNpc(i).x + 2 > MAX_MAPX Then xChange2 = 1
If MapNpc(i).y - 1 < 0 Then yChange1 = 0
If MapNpc(i).y + 1 > MAX_MAPY Then yChange2 = 0
If MapNpc(i).x - 1 < 0 Then xChange1 = 0
If MapNpc(i).x + 1 > MAX_MAPX Then xChange2 = 0
For y1 = MapNpc(i).y - yChange1 To MapNpc(i).y + yChange2
For x1 = (MapNpc(i).x - xChange1) To (MapNpc(i).x + xChange2)
Call BltTile(x1, y1)
Next x1
Next y1
End If
Next i
' Blit out the tiles around the player
For i = 1 To MAX_PLAYERS
' Make sure that theres a player here
If IsPlaying(i) And GetPlayerMap(i) = GetPlayerMap(MyIndex) Then
' Blit tiles around the player
yChange1 = 2
yChange2 = 1
xChange1 = 1
xChange2 = 1
If GetPlayerY(i) - 2 < 0 Then yChange1 = 1
If GetPlayerY(i) + 2 > MAX_MAPY Then yChange2 = 1
If GetPlayerX(i) - 2 < 1 Then xChange1 = 1
If GetPlayerX(i) + 2 > MAX_MAPX Then xChange2 = 1
If GetPlayerY(i) - 1 < 0 Then yChange1 = 0
If GetPlayerY(i) + 1 > MAX_MAPY Then yChange2 = 0
If GetPlayerX(i) - 1 < 0 Then xChange1 = 0
If GetPlayerX(i) + 1 > MAX_MAPX Then xChange2 = 0
For y1 = GetPlayerY(i) - yChange1 To GetPlayerY(i) + yChange2
For x1 = (GetPlayerX(i) - xChange1) To (GetPlayerX(i) + xChange2)
Call BltTile(x1, y1)
Next x1
Next y1
End If
Next i
' Blit out the items
For i = 1 To MAX_MAP_ITEMS
If MapItem(i).Num > 0 Then
Call BltItem(i)
End If
Next i
Code:
' Blit out the npcs
For i = 1 To MAX_MAP_NPCS
Call BltNpc(i)
Next i
Code:
' Blit out the tiles around the npc
For i = 1 To MAX_MAP_NPCS
' Make sure that theres an npc there, and if not exit the sub
If MapNpc(i).Num > 0 Then
' Blit tiles around the npc
yChange1 = 2
yChange2 = 1
xChange1 = 1
xChange2 = 1
If MapNpc(i).y - 2 < 0 Then yChange1 = 1
If MapNpc(i).y + 2 > MAX_MAPY Then yChange2 = 1
If MapNpc(i).x - 2 < 1 Then xChange1 = 1
If MapNpc(i).x + 2 > MAX_MAPX Then xChange2 = 1
If MapNpc(i).y - 1 < 0 Then yChange1 = 0
If MapNpc(i).y + 1 > MAX_MAPY Then yChange2 = 0
If MapNpc(i).x - 1 < 0 Then xChange1 = 0
If MapNpc(i).x + 1 > MAX_MAPX Then xChange2 = 0
For y1 = MapNpc(i).y - yChange1 To MapNpc(i).y + yChange2
For x1 = (MapNpc(i).x - xChange1) To (MapNpc(i).x + xChange2)
Call BltFringeTile(x1, y1)
Next x1
Next y1
End If
Next i
Find this:
Code:
' ::::::::::::::::::::::
' :: Npc spawn packet ::
' ::::::::::::::::::::::
If LCase(Parse(0)) = "spawnnpc" Then
Code:
' Blit Tiles
For x = 0 To MAX_MAPX
For y = 0 To MAX_MAPY
Call BltTile(x, y)
Call BltFringeTile(x, y)
Next y
Next x
Code:
' Checks if the map has been updated
Public GraphicInput1 As Byte ' For the first layer (bltTile)
Public GraphicInput2 As Byte ' for the other layer (bltFringeTile)
Code:
' Play music
Call StopMidi
If Map.Music > 0 Then
Call PlayMidi("music" & Trim(STR(Map.Music)) & ".mid")
End If
Code:
GraphicInput1 = 0
GraphicInput2 = 0
Code:
' Blit out players
For i = 1 To MAX_PLAYERS
If IsPlaying(i) And GetPlayerMap(i) = GetPlayerMap(MyIndex) Then
Call BltPlayer(i)
End If
Next i
Code:
' Blit out the tiles around the player
For i = 1 To MAX_PLAYERS
' Make sure that theres an player there
If IsPlaying(i) And GetPlayerMap(i) = GetPlayerMap(MyIndex) Then
' Blit tiles around the player
yChange1 = 2
yChange2 = 1
xChange1 = 1
xChange2 = 1
If GetPlayerY(i) - 2 < 0 Then yChange1 = 1
If GetPlayerY(i) + 2 > MAX_MAPY Then yChange2 = 1
If GetPlayerX(i) - 2 < 1 Then xChange1 = 1
If GetPlayerX(i) + 2 > MAX_MAPX Then xChange2 = 1
If GetPlayerY(i) - 1 < 0 Then yChange1 = 0
If GetPlayerY(i) + 1 > MAX_MAPY Then yChange2 = 0
If GetPlayerX(i) - 1 < 0 Then xChange1 = 0
If GetPlayerX(i) + 1 > MAX_MAPX Then xChange2 = 0
For y1 = GetPlayerY(i) - yChange1 To GetPlayerY(i) + yChange2
For x1 = (GetPlayerX(i) - xChange1) To (GetPlayerX(i) + xChange2)
Call BltFringeTile(x1, y1)
Next x1
Next y1
End If
Next i
That covers 8 of the 8 points in the begining of the topic. Thats it
