In the third part we handle the levelloop and add the scrolling.
Because some things will seem to be more complex, i can give you following tips:
- make a printout from the code. For me code on paper is more readable than on screen.
- mark the code you already understand respectively what you don't understand.
- try to change a value just to see what happens.
In the attachement you will find again the sample code. New added are the two files
level00.gbas which contains the levelloop and
library.gbas which contains our generally functions and subroutines. Then,
isdo/gfx/level00.png contains back- and foregroundgraphics and also
isdo/level00.dat where the indexlists for the tilemaps (see later) are stored.
Ok. In
isdo.gbas we free our out commented line again.
CASE AS_LEVEL00 ; AppState=RunLevel00()
Then we change following line from
AS_INTRO to
AS_MENU so we can skip the intro.
GLOBAL AppState% = AS_MENU
We will draw our graphics after each visual display in this order:
1. Backgroundgrapchic (LayerBG)
2. Foregroundgraphic (LayerFG)
3. Statusdisplay on top (at the moment only a black filled rectangle)
For this we have to define some new types.
TYPE TLayerFG
Map%[4096]
ENDTYPE
is an indexlist which contain the tilenumbers of the
foregroundgraphic.
TYPE TLayerBG
Map%[2048]
ENDTYPE
is an indexlist which contain the tilenumbers of the
backgroundgraphic.
The tilegraphic is stored in
isdo/gfx/level00.png and consists on 16x16 tiles and every single one of them has a size of 16x16 pixels.
Our tilemap is build as follows:
foreground background
[00][16][..][4080] [00][16][..][2032]
[01][17][..][4081] [01][17][..][2033]
[02][18][..][4082] [02][18][..][2034]
[03][19][..][4083] [03][19][..][2035]
[..][..][..][ .. ] [..][..][..][ .. ]
[..][..][..][ .. ] [..][..][..][ .. ]
[15][31][..][4095] [15][31][..][2047]
For example:
LayerFG[0]=255
with this we put the bottommost right tile at the uppermost position in or tilemap (foreground).
If you still have any questions according the tilemaps, then please ask again.
TYPE TEvent
Map%[4096]
ENDTYPE
This index-list contain special tileattributes. We need them later. Bute since they are icluded in the file
level00.dat we have to define them now.
Now we must define this 3 lists as global variables.
//=============================================================================
// V A R I A B L E S
//=============================================================================
GLOBAL LayerFG AS TLayerFG
GLOBAL LayerBG AS TLayerBG
GLOBAL Event AS TEvent
New added is also the global variable
AppTimer. This is a counter wich will be icreased by 1 on every screen refresh.
GLOBAL AppTimer% = 0
Then we have the two global variables
ScrollX and
ScrollY. We need them for scrolling.
GLOBAL ScrollX = 0
GLOBAL ScrollY = 0
The global constant
COL_TRANSPARENT defines our transparent color value. In or program this means RGB(248,0,248).
GLOBAL COL_TRANSPARENT = 0xF800F8
... ok, let's take a deep breath... and go on...
We look now in the file
level00.gbas.
First we create some local auxiliary variables.
LOCAL done% = FALSE
LOCAL x%,y%
LOCAL ix%,id%,p%
LOCAL val%
Then we set the scrollposition to startpoint.
ScrollX=0
ScrollY=0
With the following lines we load our leveldatas in our 3 indexlists.
// load layers
OPENFILE(1,"./isdo/level00.dat",TRUE)
FOR p=0 TO 4095
READUWORD 1,val
LayerFG.Map[p]=val
NEXT
FOR p=0 TO 2047
READUWORD 1,val
LayerBG.Map[p]=val
NEXT
FOR p=0 TO 4095
READUWORD 1,val
Event.Map[p]=val
NEXT
CLOSEFILE 1
To create leveldatas ether you can make your own leveleditor or you can create them in worst case by a HEX-editor.
Now we set the general transparencycolor to our predefined value.
SETTRANSPARENCY COL_TRANSPARENT
With
LOADBMP ""
we ensure the backgroundbuffer is empty. Otherwise it is possible that the new graphic will overlay an other.
Now we load our tilegraphic and assing to each of the 16x16 pixel size tiles an own spriteID. We start with the number 256 in case we have to expand if we need more tiles later.
LOADSPRITE "./isdo/gfx/level00.png",0
id=256
FOR y=0 TO 15
FOR x=0 TO 15
DRAWRECT 0,0,16,16,COL_TRANSPARENT
DRAWSPRITE 0,-x*16,-y*16
GRABSPRITE id,0,0,16,16
INC id,1
NEXT
NEXT
Then we clear the temporary sprite again.
LOADSPRITE "",0
From now on we are in the mainloop. That means, 60x per second we draw the whole screen in the backbuffer, and draw it just after that on the visual display.
//-----------------------------------------------------------------------------
// M A I N L O O P
//-----------------------------------------------------------------------------
WHILE done=FALSE
IF KEY(BUTTON_START) THEN RETURN AS_MENU
INC AppTimer,1
INC ScrollX,0.5
GOSUB KeyHANDLER
GOSUB LayerBGDRAW
GOSUB LayerFGDRAW
GOSUB StatusDRAW
SHOWSCREEN
WEND
We go on step bei step.
First we program an emergency exit. So we don't always have to turn on and off our device. Through pressing START-button (GP2X) or ENTER-key (WIN) we can return to the startmenu.
IF KEY(BUTTON_START) THEN RETURN AS_MENU
With every screen refresh we increase a general counter by 1.
INC AppTimer,1
Then we determine on how many pixels we will scroll each time (foregrund layer). If you like you can try to change this value.
INC ScrollX,0.5
Afterwards let's do the
KeyHANDLER in
library.gbas his work. His task is to handle all user inputs.
//=============================================================================
SUB KeyHANDLER:
//=============================================================================
// debug options
IF KEY(2) THEN LIMITFPS 2
IF KEY(3) THEN LIMITFPS 30
IF KEY(4) THEN LIMITFPS 60
IF KEY(5) THEN LIMITFPS 80
ENDSUB // KeyHANDLER
These 4 lines are only for test pruposes. Through pressing the keys "1"-"4" we can change the display refresh manually. This has the benefit to slow down everything, so we can see for example, if the collisions occours as accurate as possible.
With
GOSUB LayerBGDRAW
GOSUB LayerFGDRAW
we call 2 subroutines in
library.gbas. These are drawing our two layers.
We calculate the position in LayerBG.Map from where we draw the first tile on top left.
// draw background layer
p=INTEGER(ScrollX / 16 /2)*16
Then we calculate the exact startposition in pixels, where we draw the first column of tiles.
dx=0-(BAND(ScrollX,31) /2)
In opposite to the foreground we have to divide by 2 on the background, so we reach a 2-layer effect.
Now we defin how many columns have to be drawn.
FOR x=0 TO 20
Since our screen resolution is 320x240 pixels this value results in: 320/16=
20. But depending on scrollposition drawing starts outside the visible area on the left side. In this cases on the right side you can see the image-buildup. That's not very aesthetic. So we have to draw one more column.
dy=32
This is the exact startposition in pixels where we draw the first line of tiles (remember status display is 32 pixel heigh).
We draw 13 lines ((240-32)/16=
13.
FOR y=0 TO 12
Here we don't have to add a line because we are not scrolling up or down.
Now we draw the calculated tile to its position (in pixel) on screen.
DRAWSPRITE LayerBG.Map[p],dx,dy
From now on we go line for line downwards until we reach the buttom border of screen.
INC dy,16
INC p,1
NEXT
Then we take the next column.
INC dx,16
INC p,3
NEXT
Since our layermap consit a dimension of 16 lines but we draw only 13 of them, we have to overjump 3 listpositions.
Still remains the statusdisplay.
GOSUB StatusDRAW
At the moment we take it easy and draw only a black filled rectangle with a subroutine in our library.
Library auf.
DRAWRECT 0,0,320,32,RGB(0,0,0)
I hopei have everything explained fairly understandable. If not just go into it.
Next time we take control of our "Hero".
[attachment deleted by admin]