Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Optimizing Graphic Output (Finished)
#1
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 Smile Tell me if it dont.

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

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]
So that list is pretty big. A few things needs to be changed and a lot of things need to be added. This tutorial will only cover the parts in MSE, it will not include the other layers (from GSD's tutorial), so no Mask1, Fringe2 and such.

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
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.
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
To the code below. This part is pretty much as the above one, but this does it for the fringe layer instead.
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
Now, above this line:
Code:
' Blit out tiles layers ground/anim1/anim2
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.
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
Add the code below. This will blit the tiles around the enemy, similar to the technique used for the player before.
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
So now below this:
Code:
' Blit out the npcs
        For i = 1 To MAX_MAP_NPCS
            Call BltNpc(i)
        Next i
Add this code:
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
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.
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
Now add these in a module (modGlobal):
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)
Now find:
Code:
' Play music
        Call StopMidi
        If Map.Music > 0 Then
            Call PlayMidi("music" & Trim(STR(Map.Music)) & ".mid")
        End If
Below that, add the code below:
Code:
GraphicInput1 = 0
GraphicInput2 = 0
Now, below this:
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
Add this code:
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 Smile Should be working fine.


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)