Been following Scraggle's tutorial from one of the old 08 newsletters, downloaded the source code and ran the example floorlessly, however when copying the functions over into my own project and using the same syntax I end up with a Image Number Illegal error, the only difference from mine to the tutorial is that I'm using FreeImg(), FreeSprite() functions for my media.
My code:
REM Project: Cars
REM Created: 12/03/2010 19:19:15
REM
REM ***** Main Source File *****
REM
Set Window Off
Set Display Mode 1024, 768, 32
Sync On
Backdrop On
Color Backdrop 0
Sync Rate 60
Hide Mouse
Autocam Off
Set Global Shadows On
Set Normalization on
Set Camera Range 1, 0x7fffffff
`Fog On
`Fog Distance -10000000000, -11000000000000000000
Rem Dims
Dim __ObjTaken(0) as boolean ` Global array
Dim __ImgTaken(0) as boolean ` Global array
Dim __SpriteTaken(0) as boolean ` Global array
Dim Cars(4)
Dim OBJ(7)
Dim CarTex(6)
Dim BMleft(127)
Dim BMwidth(127)
global x# as float : x# = 0.0
global increment# as float : increment# = 0.7
global time1 as integer
global time2 as integer
global timeInt as integer : timeInt = 100
InitialiseBMfonts()
Rem
Land = FreeObject()
Cars(1) = FreeObject()
Cars(2) = FreeObject()
Cars(3) = FreeObject()
CarTex(1) = FreeImg()
CarTex(2) = FreeImg()
CarTex(3) = FreeImg()
CarTex(4) = FreeImg()
CarTex(5) = FreeImg()
PlayerTex = FreeImg()
RoadTex = FreeImg()
SkyTex = FreeImg()
SkyBox = FreeObject()
Land2 = FreeObject()
SandTex = FreeImg()
CarTexx=9
CLOUDP = 25
Crash=0
Lifes#=4
PlayerScore#=0
Speed1# = 1.8
Speed2# = 1.4
Level = 1
RequiredScore#=15
PlayerCol = FreeObject()
KiaCol1 = FreeObject()
KiaCol2 = FreeObject()
set ambient light x#
time1 = timer()
LifeSprite = FreeSprite()
ScoreDisp = FreeSprite()
LevelIn = FreeSprite()
FontImg = FreeIMG()
Rem Font stuff
FontImg = LoadBMfont("LED Red.png")
SetSprites=0
Stats = Details
Rem Build Land
Load Image "Media\Models\Textures\road.png", RoadTex, 1
Make object box Land, 75, 5, 400000
Texture Object Land, RoadTex
Scale Object Texture Land, 1.0, 0.1, 0.1
Position Object Land, 0, 0, 0
Rem Build Sky
Make Object Cube SkyBox, -9900
Make Object Plain Land2, 10000, 10000
Scale Object texture Land2, 500.5, 500.5
Rotate Object Land2, 90, 0, 0
Rotate Object SkyBox, -90, 0, 0
Load Image "Media\World\pansky03.jpg", SkyTex, 1
Load Image "Media\World\sand_1.jpg", SandTex
Texture Object Land2, SandTex
scroll object texture Land2, 100, 100
Texture OBject SkyBox, SkyTex
Scale object texture Land2, 125000, 1250000
Load Object "Media\Models\Lamborghini.DBO", Cars(3), 1
Load Image "Media\Models\Textures\LamboTexture.jpeg", PlayerTex, 1
Make Object Box PlayerCol, 15, 7, 20
Position Object Cars(3), 0, 8, 90
Position Object PlayerCol, 0, 8, 90
Scale Object Cars(3), 500, 500, 500
Rotate Object Cars(3), -90, 180, 0
Texture Object Cars(3), PlayerTex
Hide object PlayerCol
Make Light 1
Position Light 1, Object position x(Cars(3)), 0, Object Position Z(Cars(3))
Color Light 1, RGB( 255, 255, 255)
Set Light Range 1, 1000
Show Light 1
Set Ambient Light 65
Rem make cars
Load Image "Media\Models\Textures\rio silver.bmp", CarTex(1), 1
Load Image "Media\Models\Textures\rio red.bmp", CarTex(2), 1
Load Image "Media\Models\Textures\rio white.bmp", CarTex(3), 1
Load Image "Media\Models\Textures\rio yellow.bmp", CarTex(4), 1
Load Image "Media\Models\Textures\rio blue.bmp", CarTex(5), 1
Load Object "Media\Models\kia rio.DBO", Cars(1)
Rotate Object Cars(1), 0, 90, 90
CarTexx = CarTex(rnd(6))
If CarTexx=>2 and CarTexx=<6
Texture object Cars(1), CarTexx
Endif
Scale Object Cars(1), 190, 190, 190
Clone object Cars(2), Cars(1)
Position Object Cars(2), 0, 5, -90
Position Object Cars(1), 0, 5, -90
For X=1 TO 3
Set Object Collision On Cars(X)
Set Object Collision To Boxes Cars(X)
`Set Object Radius Cars(X), 4
Show Object Bounds Cars(X), 1
Next X
Set Object Collision On PlayerCol
Set Object Collision To Boxes PlayerCol
rem
Set Shadow Light 0, 100, 100, 100, 1000
Position Light 0, 100, 100, 100
Set Light Range 0, 1000
For X=1 to 3
Set Shadow Shading On Cars(X), -1, 200, 1
Next X
rem
PScore = DrawBMFont(0, 50, "Player Score: "+str$(PlayerScore#), Stats, 0)
PLife = DrawBMFont(0, 10, "Lifes: "+str$(Lifes#), Stats, 0)
ReqScore = DrawBMFont(0, 100, "Next Level: "+str$(RequiredScore#), Stats, 0)
Do
AlterBMFont(PScore, 0, 50, "Player Score: "+str$(PlayerScore#), Stats, 0)
AlterBMFont(PLife, 0, 10, "Lifes: "+str$(Lifes#), Stats, 0)
AlterBMFont(ReqScore, 0, 100, "Next Level: "+str$(RequiredScore#), Stats, 0)
If SetSprites=0
Set Sprite Priority LevelIn, 1
Set Sprite Priority LifeSprite, 3
Set Sprite Priority ScoreDisp, 2
SetSprites=1
Endif
Inc ScrollY#, 0.1
Position Mouse screen Width()/2, Screen Height()/2
Position Camera 0, 30, 145
Point Camera 0, 10, 95
Scroll Object Texture Land, 0, ScrollY#
If LeftKey()=1 and object position x(Cars(3))=<34.5
Move object Left Cars(3), .9
endif
If RightKey()=1 and object position x(Cars(3))=>-34.5
Move object Right Cars(3), .9
endif
CarTexx = CarTex(rnd(6))
Move object left Cars(1), Speed1#
If Object Position Z(Cars(1))>145
If CarTexx=>1 and CarTexx=<6
Texture object Cars(1), Cartexx
Position Object Cars(1), Rnd(33.5), 3, -140
PlayerScore#=PlayerScore#+1
Endif
Endif
Move object left Cars(2), Speed2#
If Object Position Z(Cars(2))>145
CarTexx = CarTex(rnd(6))
If CarTexx=>1 and CarTexx=<6
Texture object Cars(2), CarTexx
Position Object Cars(2), Rnd(33.5)*-1, 3, -140
PlayerScore#=PlayerScore#+1
Endif
Endif
if PlayerScore#=RequiredScore#
Level=Level+1
inc Speed1#, 0.1
inc Speed2#, 0.4
inc RequiredScore#, 15
endif
If Object Collision(PlayerCol, Cars(1))=1
Text 0, 30, "You Crashed!"
Lifes#=Lifes#-1
Position Object Cars(3), 0, 8, 90
Position OBject Cars(1), rnd(32.5), 1,-140
Position OBject Cars(2), rnd(33.5)*-1, 1,-140
Wait Key
Endif
If Object Collision(PlayerCol, Cars(2))=1
Text 0, 30, "You Crashed!"
Lifes#=Lifes#-1
Position Object Cars(3), 0, 8, 90
Position OBject Cars(2), rnd(33.5)*-1, 1,-90
Position OBject Cars(1), rnd(33.5), 1,-90
Wait Key
Endif
If Lifes#=0
for b=1 to 3
Hide Object Cars(b)
next b
text screen Width()/2, Screen Height()/2, "GAME OVER"
text screen Width()/2, Screen Height()/2-10, "Final Score: "+Str$(PlayerScore#)
Wait Key
End
Endif
Position Light 1, Object position x(Cars(3)), 0, Object Position Z(Cars(3))
Position Object PlayerCol, Object Position X(Cars(3)), 8, Object Position Z(Cars(3))
rem Day and Night
time2 = timer()
if ((time2 - time1) >= timeInt)
if (x# > 100.0)
increment# = -increment#
inc x#, increment#
endif
if (x# < 0.0)
increment# = -increment#
inc x#, increment#
endif
set ambient light x#
inc x#, increment#
time1 = timer()
endif
Sync
Loop
function FreeObject()
array insert at bottom __ObjTaken()
count = array count( __ObjTaken() )
if object exist(count) = 0 and __ObjTaken(count) = 0
__ObjTaken(count) = 1
exitfunction count
endif
endfunction 0
function FreeImg()
array insert at bottom __ImgTaken()
count2 = array count( __ImgTaken() )
if image exist(count2) = 0 and __ImgTaken(count2) = 0
__ImgTaken(count2) = 1
exitfunction count2
endif
endfunction 0
function FreeSprite()
array insert at bottom __SpriteTaken()
count3 = array count( __SpriteTaken() )
if Sprite exist(count3) = 0 and __SpriteTaken(count2) = 0
__SpriteTaken(count3) = 1
exitfunction count3
endif
endfunction 0
`=================================
` INITIALISE BITMAP FONTS
`=================================
Function InitialiseBMfonts()
Dim BMchrLeft( 1, 127 )
Dim BMchrWidth( 1, 127 )
Dim BMstringSpriteNo( 1 )
Dim BMstringLength( 1 )
Global NoOfBMfonts
Global BitmapFontImages
Global BitmapFontSprites
BitmapFontImages = 50000
BitmapFontSprites = 50000
`BitmapFontImages/BitmapFontSprites are references to image/sprite numbers used to store
`and display the Bitmap Fonts in your project.
`They can be any number you like but you must ensure that you dont set any other images or
`sprites to use the same numbers that are set aside for Bitmap fonts.
`
`If you only use one set of fonts, then you will have to set aside 100 image numbers
`But you can have as many different fonts as you like, so setting a high number like 50000
`should hopefully ensure that you have enough image numbers set aside before the fonts and
`enough numbers afterwards to include as many fonts as you need.
`The sprite number will increase by one for every character used because each character is
`a seperate sprite
endfunction
`=================================
` LOAD BITMAP FONT
`=================================
Function LoadBMfont(FileName as string )
`The print commands contained in this function are
`meant as only a temporary measure to demonstrate
`what is happening. It is up to you whether or not
`you keep it in your project.
print
print "Loading Bitmap Font : " + FileName
inc NoOfBMfonts
Dim BMchrLeft( NoOfBMfonts, 127 )
Dim BMchrWidth( NoOfBMfonts, 127 )
load image FileName, BitmapFontImages, 1
Print "Cutting Font File ..."
Size = image Width( BitmapFontImages ) / 10
Chr = 32
For y = 0 to 9
for x = 0 to 9
GetImage( BitmapFontImages, Chr + ( NoOfBMfonts * 255 ), x * Size, y * Size, Size, Size )
inc Chr
next x
next y
Delete Image BitmapFontImages
Print "Trimming Font File ..."
TrimBMfont( NoOfBMfonts, Size )
endfunction NoOfBMfonts
`=================================
` GET IMAGE WITH TRANSPARENCY
`=================================
function GetImage( Image1, Image2, Xstart, Ystart, Xsize as dword, Ysize as dword)
`Set up Local variables
Local Width as Dword
Local Height as Dword
Local Depth as Dword
Local Red as Byte
Local Green as Byte
Local Blue as Byte
Local Alpha as Byte
Local Memblock1 as integer
Local NewMemblock as integer
`Find unused memblocks
Memblock1 = 1
repeat
inc Memblock1
until memblock exist( Memblock1 ) = 0
NewMemblock = 2
repeat
inc NewMemblock
until memblock exist( NewMemblock ) = 0
`Do it!
make memblock from image Memblock1, Image1
Width = memblock dword( Memblock1, 0 )
Height = memblock dword( Memblock1, 4 )
Depth = memblock dword( Memblock1, 8 )
make memblock NewMemblock, ( Width * Height ) + 12
Write memblock Dword NewMemblock, 0, Xsize
Write memblock Dword NewMemblock, 4, Ysize
Write memblock Dword NewMemblock, 8, Depth
Position = ( Width * ( Ystart ) * 4) + ( Xstart * 4 ) + 12
NewPosition = 12
for y = 1 to Ysize
for x = 1 to Xsize
Blue = memblock byte( Memblock1, Position + 0 )
Green = memblock byte( Memblock1, Position + 1 )
Red = memblock byte( Memblock1, Position + 2 )
Alpha = memblock byte( Memblock1, Position + 3 )
write memblock byte NewMemblock, NewPosition + 0, Blue
write memblock byte NewMemblock, NewPosition + 1, Green
write memblock byte NewMemblock, NewPosition + 2, Red
write memblock byte NewMemblock, NewPosition + 3, Alpha
inc Position, 4
Inc NewPosition, 4
next x
inc Position , ( Width * 4 ) - ( Xsize * 4 )
next y
`Make the image from the the memblock we have just manipulated
make image from memblock Image2, NewMemblock
`Clean Up
delete memblock Memblock1
delete memblock NewMemblock
endfunction
`=================================
` TRIM BITMAP FONT
`=================================
Function TrimBMFont( FontNo, Size )
`Set up Local variables
Local BlankColumn as integer
Local Alpha as byte
Local Chr as integer
Local Position as integer
Local Memblock1 as integer
`Find unused memblocks
Memblock1 = 1
repeat
inc Memblock1
until memblock exist( Memblock1 ) = 0
`Find the first column of pixels that
`contains a non-transparent pixel
For Chr = 33 to 126
Make Memblock From Image Memblock1, ( FontNo * 255 ) + Chr
for x = 1 to Size
BlankColumn = 1
for y = 1 to Size - 1
if BMchrLeft( FontNo, Chr ) = 0
Position = ( y * Size * 4 ) + ( x * 4 ) + 15
Alpha = memblock byte( Memblock1, Position )
if Alpha > 0
BlankColumn = 0
y = Size - 1
endif
endif
next y
if BlankColumn = 0
BMchrLeft( FontNo, Chr ) = x
x = Size + 1
endif
next x
`Repeat from the right
for x = Size - 1 to 1 step - 1
BlankColumn = 1
for y = 1 to Size - 1
if BMchrWidth( FontNo, Chr ) = 0
Position = ( y * Size * 4 ) + ( x * 4 ) + 15
Alpha = memblock byte( Memblock1, Position )
if Alpha > 0
BlankColumn = 0
y = Size
endif
endif
next y
if BlankColumn = 0
BMchrWidth( FontNo, Chr ) = x - BMchrLeft( FontNo, Chr )
x = 0
endif
next x
delete memblock Memblock1
next Chr
`Set the Space character to display a gap
`that is half the Width of the other characters.
`You can change this to suit your needs but
`I think it works well
BMchrLeft( FontNo, 32 ) = 0
BMchrWidth( FontNo, 32 ) = Size / 2
endfunction
`=================================
` DISPLAY BITMAP FONT
`=================================
Function DrawBMFont( X, Y, S as string, fontStyle, Kern )
inc BMstringSpriteNo( 0 )
ThisString = BMstringSpriteNo( 0 )
SpriteNo = BitmapFontSprites + BMstringSpriteNo( ThisString - 1 ) + 255
dim BMstringSpriteNo( ThisString )
dim BMstringLength( ThisString )
dim BMstringText( ThisString ) as string
dim BMstringStyle( ThisString )
dim BMstringKern( ThisString )
`re-DIMing like this will retain the contents of the array
`whilst causing it to grow. It is a quirk of DBP and most
`BASICs don't work like this but it is a quirk that we can
`take advantage of. You could change the code to use array
`stacks and/or queues but this method works, and I prefer it.
for k = 1 to len( S )
Chr = asc( mid$( S, k ) )
if k = 1
sprite SpriteNo + k, X - BMchrLeft( FontStyle, Chr ), Y, ( FontStyle * 255 ) + Chr
NewX = X + BMchrWidth( FontStyle, Chr ) + Kern
else
sprite SpriteNo + k, NewX - BMchrLeft( FontStyle, Chr ), Y, ( FontStyle * 255 ) + Chr
NewX = NewX + BMchrWidth( FontStyle, Chr ) + Kern
endif
next k
`Store the values for future referencing
BMstringKern( ThisString ) = Kern
BMstringStyle( ThisString ) = fontStyle
BMstringText( ThisString ) = S
BMstringSpriteNo( ThisString ) = SpriteNo
BMstringLength( ThisString ) = len( S )
endfunction ThisString
`=================================
` ALTER BITMAP FONT
`=================================
Function alterBMFont( ThisString, X, Y, S as string, fontStyle, Kern )
SpriteNo = BitmapFontSprites + BMstringSpriteNo( ThisString - 1 ) + 255
`If the altered font is shorter than the previous one
`it will still show the last few characters. So, adding
`the spaces to the end of the string will fix that:)
if len( S ) <> Len( BMstringText( ThisString ) )
for k = Len( S ) + 1 to Len( BMstringText( ThisString ) )
S = S + " "
next k
endif
for k = 1 to len( S )
Chr = asc( mid$( S, k ) )
if k = 1
sprite SpriteNo + k, X - BMchrLeft( FontStyle, Chr ), Y, ( FontStyle * 255 ) + Chr
NewX = X + BMchrWidth( FontStyle, Chr ) + Kern
else
sprite SpriteNo + k, NewX - BMchrLeft( FontStyle, Chr ), Y, ( FontStyle * 255 ) + Chr
NewX = NewX + BMchrWidth( FontStyle, Chr ) + Kern
endif
next k
`Store the values for future referencing
BMstringKern( ThisString ) = Kern
BMstringStyle( ThisString ) = fontStyle
BMstringText( ThisString ) = S
BMstringSpriteNo( ThisString ) = SpriteNo
BMstringLength( ThisString ) = len( S )
endfunction ThisString
`=================================
` DELETE BITMAP FONT
`=================================
Function deleteBMfont( BMid )
First = BMstringSpriteNo( BMid ) + 1
Last = First + BMstringLength( BMid ) - 1
for k = First to Last
delete sprite k
next k
endfunction
`=================================
` HIDE BITMAP FONT
`=================================
Function hideBMfont( BMid )
First = BMstringSpriteNo( BMid ) + 1
Last = First + BMstringLength( BMid ) - 1
for k = First to Last
hide sprite k
next k
endfunction
`=================================
` SHOW BITMAP FONT
`=================================
Function showBMfont( BMid )
First = BMstringSpriteNo( BMid ) + 1
Last = First + BMstringLength( BMid ) - 1
for k = First to Last
show sprite k
next k
endfunction
`=================================
` SIZE BITMAP FONT
`=================================
Function sizeBMfont( BMid, X, Y )
First = BMstringSpriteNo( BMid ) + 1
Last = First + BMstringLength( BMid ) - 1
for k = First to Last
size sprite k, X, Y
next k
endfunction
`=================================
` SCALE BITMAP FONT
`=================================
Function scaleBMfont( BMid, Scale )
First = BMstringSpriteNo( BMid ) + 1
Last = First + BMstringLength( BMid ) - 1
for k = First to Last
scale sprite k, Scale
next k
endfunction
`=================================
` POSITION BITMAP FONT
`=================================
Function positionBMfont( BMid, x, y )
First = BMstringSpriteNo( BMid ) + 1
Last = BMstringLength( BMid ) - 1 + First
S$ = BMstringText( BMid )
fontStyle = BMstringStyle( BMid )
Kern = BMstringKern( BMid )
t = 1
for k = First to Last
Chr = asc( mid$( S$, t ) )
if k = First
sprite k, X - BMchrLeft( fontStyle, Chr ), Y, ( fontStyle * 255 ) + Chr
NewX = X + BMchrWidth( fontStyle, Chr ) + Kern
inc t
else
sprite k, NewX - BMchrLeft( fontStyle, Chr ), Y, ( fontStyle * 255 ) + Chr
NewX = NewX + BMchrWidth( fontStyle, Chr ) + Kern
inc t
endif
next k
endfunction
The error is on line:
sprite SpriteNo + k, X - BMchrLeft( FontStyle, Chr ), Y, ( FontStyle * 255 ) + Chr
Been having an attempt to fix it but all day, if anyone needs I can attach the media as well.
Thanks in advanced.
Coding.....again, back to the basics xD