@Lost Dragon
The most efficient way to use the sprite statement is to only use one sprite and only to actually draw it's image pointer to the screen. Having a lot of sprites slows down the program greatly. The difference between paste sprite and paste image is that paste image does not cause a slow down no matter how many images are loaded into memory but the usage of alot of sprites will slow down your program greatly.
The solution to this problem is to only use one sprite to draw all your images.
set display mode 1024,768,32,0,0,0
sync on
sync rate 0
LOAD IMAGE "1.png",1 ` grass
LOAD IMAGE "2.png",2 ` boards
LOAD IMAGE "3.png",3 ` ladder with board background
LOAD IMAGE "4.png",4 ` brick flooring
LOAD IMAGE "5.png",5 ` dirt
LOAD IMAGE "6.png",6 ` slate flooring
LOAD IMAGE "7.png",7 ` fancy flooring
LOAD IMAGE "8.png",8 ` ocean
LOAD IMAGE "9.png",9 ` bush
LOAD IMAGE "10.png",10 ` block wall n/s
LOAD IMAGE "11.png",11 ` block wall e/w
LOAD IMAGE "12.png",12 ` pine tree
LOAD IMAGE "13.png",13 ` plaster wall n/s
LOAD IMAGE "14.png",14 ` plaster wall e/w
LOAD IMAGE "15.png",15 ` cobblestone wall n/s
LOAD IMAGE "16.png",16 ` cobblestone wall e/w
LOAD IMAGE "17.png",17 ` mountain bottom left
LOAD IMAGE "18.png",18 ` mountain bottom right
LOAD IMAGE "19.png",19 ` mountain top left
LOAD IMAGE "20.png",20 ` mountain top top right
LOAD IMAGE "21.png",21 ` mountain top
LOAD IMAGE "22.png",22 ` mountain right
LOAD IMAGE "23.png",23 ` mountain left
LOAD IMAGE "24.png",24 ` mountain bottom
LOAD IMAGE "25.png",25 ` mountain peak
LOAD IMAGE "26.png",26 ` mountain filler
LOAD IMAGE "tanku.png",27,0 ` tank facing up <-- handled with a sprite rotation later.
LOAD IMAGE "tankr.png",28,0 ` tank facing right
LOAD IMAGE "tankl.png",29,0 ` tank facing left
LOAD IMAGE "tankd.png",30,0 ` tank facing down
LOAD IMAGE "title.png",31
do
gosub DrawSpriteExample
text 0,0,"FPS:" + str$(screen fps())
sync
loop
DrawSpriteExample:
rem draw the map
for x = 0 to 31
for y = 0 to 23
TileImage = 1+rnd(25)
REM I just put this sprite off screen. You can also just hide it.
sprite 1,-100,-100,TileImage
paste sprite 1,x*32, y*32
next y
next x
return
The reason of using this method in the first place is for the sole purpose of using the sprite modification command that comes with it. Here are some of the useful ones with example.
set display mode 1024,768,32,0,0,0
sync on
sync rate 0
LOAD IMAGE "1.png",1 ` grass
LOAD IMAGE "2.png",2 ` boards
LOAD IMAGE "3.png",3 ` ladder with board background
LOAD IMAGE "4.png",4 ` brick flooring
LOAD IMAGE "5.png",5 ` dirt
LOAD IMAGE "6.png",6 ` slate flooring
LOAD IMAGE "7.png",7 ` fancy flooring
LOAD IMAGE "8.png",8 ` ocean
LOAD IMAGE "9.png",9 ` bush
LOAD IMAGE "10.png",10 ` block wall n/s
LOAD IMAGE "11.png",11 ` block wall e/w
LOAD IMAGE "12.png",12 ` pine tree
LOAD IMAGE "13.png",13 ` plaster wall n/s
LOAD IMAGE "14.png",14 ` plaster wall e/w
LOAD IMAGE "15.png",15 ` cobblestone wall n/s
LOAD IMAGE "16.png",16 ` cobblestone wall e/w
LOAD IMAGE "17.png",17 ` mountain bottom left
LOAD IMAGE "18.png",18 ` mountain bottom right
LOAD IMAGE "19.png",19 ` mountain top left
LOAD IMAGE "20.png",20 ` mountain top top right
LOAD IMAGE "21.png",21 ` mountain top
LOAD IMAGE "22.png",22 ` mountain right
LOAD IMAGE "23.png",23 ` mountain left
LOAD IMAGE "24.png",24 ` mountain bottom
LOAD IMAGE "25.png",25 ` mountain peak
LOAD IMAGE "26.png",26 ` mountain filler
LOAD IMAGE "tanku.png",27,0 ` tank facing up <-- handled with a sprite rotation later.
LOAD IMAGE "tankr.png",28,0 ` tank facing right
LOAD IMAGE "tankl.png",29,0 ` tank facing left
LOAD IMAGE "tankd.png",30,0 ` tank facing down
LOAD IMAGE "title.png",31
do
gosub DrawSpriteExample
text 0,0,"FPS:" + str$(screen fps())
sync
loop
DrawSpriteExample:
rem draw the map
for x = 0 to 31
for y = 0 to 23
TileImage = 1+rnd(25)
sprite 1,-100,-100,TileImage
REM *******************************
REM * Allows sprite modification. *
REM *******************************
REM Values 0~255. The value "127" makes it 50% transparency
set sprite alpha 1,127
REM Flips the sprite horizontaly. Every call to this statement changes it's output.
mirror sprite 1
REM Modify the sprite's color.
set sprite diffuse 1,255,127,127
REM Chages the sprite's x and y size manually.
size sprite 1,32,32
REM Self explanatory
scale sprite 1,100
REM Rotates sprite based off the "center" default is top left corner of image.
rotate sprite 1,0
REM This is best used with rotate sprite. Manually set it's "center" to 16,16 because sprite is size 32.
REM NOTICE: All drawing of this sprite is offseted x-16 and y-16 pixels.
offset sprite 1,16,16
REM *******************************
paste sprite 1,x*32, y*32
next y
next x
return
***************************************************
I dont know if you care for optimization yet but here's a very quick example of using bitmap to greatly speed up the fps rate on your last working posted codes. This is only a small part of the optimization but still creates a big difference. This portion of the speed boost works best when there isn't much going on with the background map.
REM ---------- Global Variables ----------
REM The size of the map as shown on screen.
REM This is how much of the map the player can see at one time.
GLOBAL Screen_Resolution_width = 1024 : REM screen resolution
GLOBAL Screen_Resolution_height = 768
GLOBAL Tile_size_width = 32 : REM width of tiles in pixels
GLOBAL Tile_size_height = 32 : REM height of tiles in pixels
GLOBAL Tiles_on_screen_width : REM declare some globals to hold values calculated below. This value will later tell the engine how many
GLOBAL Tiles_on_screen_height : REM tiles to draw at any given time. You don't want to redraw the entire map every cycle. That is too slow.
GLOBAL Map_size_width = 128 : REM the width of your map in tiles
GLOBAL Map_size_height = 128
GLOBAL MAP_Start_X = 36 : REM Starting offset location for the map. The starting point for your player.
GLOBAL MAP_Start_Y = 60
GLOBAL Map_Name$ = "dbmap128L1.txt" : REM The map file to be loaded
GLOBAL Map_Offset_Boundary_X : REM This is the maximum value your map offset variables can be. It prevents the player from leaving the map array
GLOBAL Map_Offset_Boundary_Y
GLOBAL Player_X : REM the player x,y values (always at the center of the displayed tiles)
GLOBAL Player_Y
REM ---------- Declare Map Array ----------
GLOBAL DIM Map_array(Map_size_width, Map_size_height)
REM ---------- Calculate Global Variables ----------
Tiles_on_screen_width = (Screen_Resolution_width / Tile_size_width) : REM calculate how many tiles across the current screen resolution can show at once.
Tiles_on_screen_height = (Screen_Resolution_height / Tile_size_height)
Map_Offset_Boundary_X = (Map_size_width - Tiles_on_screen_width)-1
Map_Offset_Boundary_Y = (Map_size_height - Tiles_on_screen_height)-1
Player_X = Tiles_on_screen_width / 2
Player_Y = Tiles_on_screen_height / 2
REM ---------- Set up the screen, load images, load map data ----------
GOSUB Init_Display
GOSUB Load_images
GOSUB Load_map
REM ***********************
global DrawMapFlag
create bitmap 1, Screen_Resolution_width, Screen_Resolution_height
set current bitmap 0
REM ***********************
REM ---------- MAIN LOOP ----------
DO
CLS
GOSUB Player_Controls
drawMap(MAP_Start_X, MAP_Start_Y)
drawPlayer(Player_X, Player_Y)
text 0,0,"FPS: "+str$(screen fps()) : REM Show how many FPS we're getting.
text 0,30,"map offset x: "+str$(MAP_Start_X)
text 0,60,"map offset y: "+str$(MAP_Start_Y)
text 0,90,"Player X: "+str$(Player_X)
text 0,120,"Player Y: "+str$(Player_Y)
SYNC
` sync rate 15
LOOP
REM ---------- SUBROUTINES ----------
REM ---------- Initialize Display ----------
Init_Display:
Check_Screen = CHECK DISPLAY MODE(Screen_Resolution_width,Screen_Resolution_height,32) : REM see if the display mode we want can be run on this PC.
IF Check_Screen = 1
SET DISPLAY MODE Screen_Resolution_width, Screen_Resolution_height, 32 : REM All is well, procede with setting up the screen and then on to the game.
SET WINDOW ON
SYNC ON
SYNC RATE 0
BACKDROP OFF
ELSE
SET DISPLAY MODE 320,200,32 : REM something is wrong. Make a very low resolution window and display an error message in it, then close the program.
SET WINDOW ON
PRINT "Unable to create "+STR$(Screen_Resolution_width)," x "+STR$(Screen_Resolution_height)
PRINT "x 32bit window!"
PRINT "Press any key to exit."
WAIT KEY
END
ENDIF
RETURN
REM ---------- Load Images ----------
Load_Images:
LOAD IMAGE "1.png",1 ` grass
LOAD IMAGE "2.png",2 ` boards
LOAD IMAGE "3.png",3 ` ladder with board background
LOAD IMAGE "4.png",4 ` brick flooring
LOAD IMAGE "5.png",5 ` dirt
LOAD IMAGE "6.png",6 ` slate flooring
LOAD IMAGE "7.png",7 ` fancy flooring
LOAD IMAGE "8.png",8 ` ocean
LOAD IMAGE "9.png",9 ` bush
LOAD IMAGE "10.png",10 ` block wall n/s
LOAD IMAGE "11.png",11 ` block wall e/w
LOAD IMAGE "12.png",12 ` pine tree
LOAD IMAGE "13.png",13 ` plaster wall n/s
LOAD IMAGE "14.png",14 ` plaster wall e/w
LOAD IMAGE "15.png",15 ` cobblestone wall n/s
LOAD IMAGE "16.png",16 ` cobblestone wall e/w
LOAD IMAGE "17.png",17 ` mountain bottom left
LOAD IMAGE "18.png",18 ` mountain bottom right
LOAD IMAGE "19.png",19 ` mountain top left
LOAD IMAGE "20.png",20 ` mountain top top right
LOAD IMAGE "21.png",21 ` mountain top
LOAD IMAGE "22.png",22 ` mountain right
LOAD IMAGE "23.png",23 ` mountain left
LOAD IMAGE "24.png",24 ` mountain bottom
LOAD IMAGE "25.png",25 ` mountain peak
LOAD IMAGE "26.png",26 ` mountain filler
LOAD IMAGE "tanku.png",27,0 ` tank facing up <-- handled with a sprite rotation later.
LOAD IMAGE "tankr.png",28,0 ` tank facing right
LOAD IMAGE "tankl.png",29,0 ` tank facing left
LOAD IMAGE "tankd.png",30,0 ` tank facing down
LOAD IMAGE "title.png",31
sprite 1,0,0,1 ` grass
sprite 2,0,0,2
sprite 3,0,0,3
sprite 4,0,0,4
sprite 5,0,0,5
sprite 6,0,0,6
sprite 7,0,0,7
sprite 8,0,0,8
sprite 9,0,0,9
sprite 10,0,0,10
sprite 11,0,0,11
sprite 12,0,0,12
sprite 13,0,0,13
sprite 14,0,0,14
sprite 15,0,0,15
sprite 16,0,0,16
sprite 17,0,0,17
sprite 18,0,0,18
sprite 19,0,0,19
sprite 20,0,0,20
sprite 21,0,0,21
sprite 22,0,0,22
sprite 23,0,0,23
sprite 24,0,0,24
sprite 25,0,0,25
sprite 26,0,0,26
sprite 27,0,0,27
sprite 28,0,0,28
sprite 29,0,0,29
sprite 30,0,0,30
hide all sprites
set sprite 1, 0, 1 ` otherwise you'll have a blue backdrop
RETURN
REM ---------- Load Map File ----------
Load_Map:
open to read 1,Map_Name$ ` Open file name stored in map_name$ to read map values from it.
for row = 0 to Map_size_width - 1
read string 1,s$
for col = 0 to Map_size_height - 1
index = findComma(s$)
value = val(left$(s$, index))
s$ = right$(s$, len(s$)-index)
Map_array(row, col) = value
next col
next row
RETURN
REM ---------- Player Controls ----------
Player_Controls:
if rightkey()=1 and MAP_Start_X < Map_Offset_Boundary_X
inc MAP_Start_X
DrawMapFlag = 0
endif
if leftkey()=1 and MAP_Start_X > 0
dec MAP_Start_X
DrawMapFlag = 0
endif
if upkey()=1 and MAP_Start_Y > 0
dec MAP_Start_Y
DrawMapFlag = 0
endif
if downkey()=1 and MAP_Start_Y < Map_Offset_Boundary_Y
inc MAP_Start_Y
DrawMapFlag = 0
endif
RETURN
REM ---------- FUNCTIONS ----------
REM ---------- Find comma as string ----------
function findComma(s as string)
for i = 1 to len(s)
if mid$(s,i) = "," or i = len(s) then exitfunction i
next i
endfunction -1
REM ---------- Draw the map ----------
function drawMap(offsetX, offsetY)
if DrawMapFlag = 0
DrawMapFlag = 1
set current bitmap 1
rem draw the map
for x = 0 to Tiles_on_screen_width
for y = 0 to Tiles_on_screen_height
rem calculate the map's offset for accessing the map array
mx = offsetX + x
my = offsetY + y
rem set tile color according to map array
TileImage=Map_array(mx,my)
Paste Image TileImage,x*32, y*32
next y
next x
set current bitmap 0
endif
copy bitmap 1,0
endfunction
REM ---------- Draw the player ----------
REM Draw player at tile [x,y]
function drawPlayer(x, y)
` Paste Image 27,x*32,y*32
paste sprite 27,x*32,y*32
endfunction
The three changes made are here so you dont have to go digging for it.
Directly above the MainLoop:
REM ***********************
global DrawMapFlag
create bitmap 1, Screen_Resolution_width, Screen_Resolution_height
set current bitmap 0
REM ***********************
The Player's added "DrawMapFlag = 0" to the keys:
Player_Controls:
if rightkey()=1 and MAP_Start_X < Map_Offset_Boundary_X
inc MAP_Start_X
DrawMapFlag = 0
endif
if leftkey()=1 and MAP_Start_X > 0
dec MAP_Start_X
DrawMapFlag = 0
endif
if upkey()=1 and MAP_Start_Y > 0
dec MAP_Start_Y
DrawMapFlag = 0
endif
if downkey()=1 and MAP_Start_Y < Map_Offset_Boundary_Y
inc MAP_Start_Y
DrawMapFlag = 0
endif
RETURN
Bitmap commands added to the drawmap() function:
function drawMap(offsetX, offsetY)
if DrawMapFlag = 0
DrawMapFlag = 1
set current bitmap 1
rem draw the map
for x = 0 to Tiles_on_screen_width
for y = 0 to Tiles_on_screen_height
rem calculate the map's offset for accessing the map array
mx = offsetX + x
my = offsetY + y
rem set tile color according to map array
TileImage=Map_array(mx,my)
Paste Image TileImage,x*32, y*32
next y
next x
set current bitmap 0
endif
copy bitmap 1,0
endfunction