Hey all,
I'm super new to DB programming so my apologies for any stupidity in these matters, but I can't figure out exactly WHY this code is running so slowly.
As soon as I disable the update reflection camera code I'm back to full speed.
I've tried disabling pretty much every combination of code to find the source, but i'm stumped.
I've attached all my media and code as well as the code below.
REM Date 27/10/2010
REM Author: Tim and Callum
// Setup screen
screenSetup()
randomize timer()
// Restart Game
RESTART:
for i = 1 to 3000
if object exist(i) then delete object i
if effect exist(i) then delete effect i
if camera exist(i) then delete camera i
NEXT
// Display Menu
`menu()
// Constant object numbers
#constant frog = 1
#constant worldObj = 2
#constant worldFinish = 3
// Minimum and maximum object number for lilypads
minPad as integer = 20
maxPad as integer = 30
// Minimum and maximum object number for powerups
minPow as integer = 7
maxPow as integer = 12
// Minimum and maximum object number for enemies
minEnemy as integer = 50
maxEnemy as integer = 100
// Random Lily variable for pickup position calculation
randomLily as integer = 1
// Frog property variables
collided as string = "none"
playermassState as string = "normal"
// Current lilypad frog is colliding with
currentObj as integer
// Set player gravity
playergrav#=2.0
// Set first run status
firstRun as string = "true"
// Set lilyspacing variables
lilyXSpacing as integer = 0
lilyYSpacing as integer = 0
lilyZSpacing as integer = 0
// ##ARRAY FOR LILYPAD##
// UDT for lilypad variables
type lilypad
startXLily as float
startYLily as float
startZLily as float
xLily as float
yLily as float
zLily as float
sizeXLily as integer
sizeYLily as integer
sizeZLily as Integer
objNo as integer
yDirection as string
xDirection as string
endType
if firstRun = "false"
empty array lilypad()
endif
// Populate lilypads with size, position and direction variables
for i=minPad to maxPad
dim lilypad(i) as lilypad
// Lilypad spacing
lilypad(i).xLily=lilyXSpacing
lilypad(i).yLily=lilyYSpacing
lilypad(i).zLily=lilyZSpacing
// Lilypad start position
lilypad(i).startXLily=lilypad(i).xLily
lilypad(i).startYLily=lilypad(i).yLily
lilypad(i).startZLily=lilypad(i).zLily
// Lilypad size
lilypad(i).sizeXLily=12
lilypad(i).sizeYLily=2
lilypad(i).sizeZLily=12
// Lilypad object number
lilypad(i).objNo=i
// Randomise lilypad direction
if rnd(2) = 2 then lilypad(i).yDirection="up" else lilypad(i).yDirection="down"
if rnd(2) = 2 then lilypad(i).xDirection="left" else lilypad(i).xDirection="right"
// Randomize spacing of lilypads
lilyXSpacing = rnd(200)-100
lilyYSpacing = rnd(20)
inc lilyZSpacing, rnd(70) + 70
// Add current item to array
Array Insert At bottom lilypad(), i
next i
// Create and place lilypads from array, create collision box and color.
for i=minPad to maxPad
load object "media/lilypad.x", lilypad(i).objNo
load image "media/flikk_tex.jpg", 2
texture object i, 2
if effect exist(2) <> 1 then load Effect "shaders\Gloss.fx", 2, 2
Set Object Effect i, 2
scale object lilypad(i).objNo, 600, 200, 600
make object collision box lilypad(i).objNo, -lilypad(i).sizexLily/2, -lilypad(i).sizeYLily/2, -lilypad(i).sizeZLily/2, lilypad(i).sizexLily/2, lilypad(i).sizeYLily/2, lilypad(i).sizeZLily/2, 0
color object lilypad(i).objNo, rgb(20,255,150)
set shadow shading on i,-1,1000,0
next i
// ##CREATE SKYBOX##
skyBox()
// ##CREATE PLAYER##
createFrog()
// ##CREATE WORLD##
createWorld(lilyZSpacing)
// Make pickups
for i=minPow to maxPow
make object cube i, 6
make object collision box i,-5,-5,-5,5,5,5,0
randomLily = (rnd(maxPad)+minPad)
position object i, lilypad(randomLily).xLily, lilypad(randomLily).yLily+40, lilypad(randomLily).zLily
next i
// Create enemies, set initial positions, set collision boxes and object emissivity
for i =minEnemy to maxEnemy
make object cube i, 6
SET OBJECT emissive i, rgb(20, 255, 100)
make object collision box i,-4,-4,-4,4,4,4,0
position object i, rnd(1000)-500, rnd(1500), rnd(1000)
set shadow shading on i,-1,1000,0
NEXT
// ##LIGHTING##
if light exist(1) then delete light 1
make light 1
set directional light 1,100,50,100
position light 1, -1000, 500, -1000
`SET LIGHT TO OBJECT ORIENTATION 1, frog
set light range 1, 500
set shadow position 1,0,0,0
color light 1, 1, 100, 160
jumpForward as string = "false"
// Make water
makeWater()
// ###MAIN LOOP###
do
// User prompts
ink rgb(255,255,0),0
center text 320 , 20, "CONTROLS: WASD for movement. Left/Right arrow keys for rotation. Shift for EXTRA SPEEED! Space to Jump"
center text 320 , 40, "screen fps "+str$(screen fps())
// Store old frog positions positions
oldposx# = object position x(frog)
oldposy# = object position y(frog)
oldposz# = object position z(frog)
// #CONTROL PLAYER MOVEMENT#
// control player momentum using shift key
if shiftkey() = 1
fmomentum = 2.3
else
fmomentum = 2.0
endif
// key commands
if keystate(17) = 1 and collided = "true" then move object frog, fmomentum
if keystate(17) = 1 and collided <> "true" then jumpForward = "true"
if jumpForward = "true" then move object frog, fmomentum + 1.0
if keystate(31) = 1 then move object frog, -fmomentum
if keystate(30) = 1 and playergrav# = 0 then move object left frog, fmomentum
if keystate(32) = 1 and playergrav# = 0 then move object left frog, -fmomentum
if leftkey() = 1 then yrotate object frog, wrapvalue(object angle y(frog)-3)
if rightkey()= 1 then yrotate object frog, wrapvalue(object angle y(frog)+3)
// jump command and players jumping height determined by players mass
if spacekey() = 1 and playergrav#=0.0 and playermassState = "normal" then playergrav#=2.0
if spacekey() = 1 and playergrav#=0.0 and playermassState = "light" then playergrav#=2.5
if spacekey() = 1 and playergrav#=0.0 and playermassState = "heavy" then playergrav#=1.7
//Get current frog position
fposx# = object position x(frog)
fposy# = object position y(frog)
fposz# = object position z(frog)
// Gravity application
playergrav# = playergrav# - 0.1
fposy# = fposy# + playergrav#
// Handle sliding collision for player object with other objects
position object frog, fposx#, fposy#, fposz#
if object collision(frog, 0) > 0
dec fposx#, get object collision x()
dec fposy#, get object collision y()
dec fposz#, get object collision z()
if get object collision y() <> 0 then playergrav# = 0.0
jumpForward = "false"
endif
`collided as string = "no"
for i=minPad to maxPad
if object collision(frog, i) > 0 then collided = "collided" : currentObj = i
next i
if posy# > (lilypad(currentObj).yLily + 30) then collided = "none"
if lilypad(currentObj).xDirection ="left" and collided = "collided" then fposx# = fposx# + 0.20
if lilypad(currentObj).xDirection ="right" and collided = "collided" then fposx# = fposx# - 0.20
// Platforms fall when player lands on them.
`for i=minPad to maxPad
`if object collision(frog,0) then dec lilypad(i).yLily, 0.3 : currentObj = i
`if lilyPad(i).yDirection = "falling" then dec lilypad(i).yLily, 0.3
`if object collision(frog,i) = 0 then lilyPad(i).yDirection = "up"
`next i
// Move platforms vertically
for i=minPad to maxPad step 2
// Inc direction based on direction.
if lilyPad(i).yDirection = "up" then inc lilypad(i).yLily, 0.2
if lilyPad(i).yDirection = "down" then dec lilypad(i).yLily, 0.2
// Send lilypad in the other direction if their position exceeds their start posiion by 30 units.
if lilyPad(i).yLily > lilypad(i).startYLily + 30 then lilyPad(i).yDirection = "down"
if lilyPad(i).yLily < lilypad(i).startYLily - 30 and lilyPad(i).yDirection <> "falling" then lilyPad(i).yDirection = "up"
next i
// Move platforms horizontally
for i=minPad to maxPad step 1
// Inc direction based on direction.
if lilyPad(i).xDirection = "left" then inc lilypad(i).xLily, 0.2
if lilyPad(i).xDirection = "right" then dec lilypad(i).xLily, 0.2
// Send lilypad in the other direction if their position exceeds their start posiion by 30 units.
if lilyPad(i).xLily > lilypad(i).startxLily + 30 then lilyPad(i).xDirection = "right"
if lilyPad(i).xLily < lilypad(i).startxLily - 30 then lilyPad(i).xDirection = "left"
next i
// Player mass state logic
// If player collides with a powerup or enemy change mass state
for i=minPow to maxPow
if object exist(i)
if object collision(frog, i) > 0 then playermassState = "light" : delete object i
endif
next i
for i=minPad to maxPad
if object collision(frog, i) < 0
if lilyPad(i).xDirection = "right"
move object left frog, -1
endif
endif
NEXT
// Position lilypads
for i=minPad to maxPad
position object lilypad(i).objNo, lilypad(i).xLily, lilypad(i).yLily, lilypad(i).zLily
next i
// Update with new frog position
position object frog, fposx#, fposy#, fposz#
// ##ENEMY##
// Delete and repopulate enemies and place them.
for i=minEnemy to maxEnemy
if object exist(i)
// If the enemies have passed off screen delete them.
if object position Y(i) < -400
delete object i
endif
a = 1
else
// Create replacements in random positions
make object cube i, 6
make object collision box i,-4,-4,-4,4,4,4,0
set object emissive i, rgb(200, 255, 20)
position object i, rnd(250)-125, rnd(1500), rnd(250)
`set shadow shading on i,-1,1000,0
endif
next i
// Enemy position update and
for i=minEnemy to maxEnemy
if object exist(i)
move object down i, 1.0
endif
next i
// Change player mass if hit by enemy
for i=minEnemy to maxEnemy
if object exist(i)
if object collision(frog, i) > 0 then delete object i
endif
next i
// ##CAMERA##
cameraTrack(fposx#, fposy#, fposz#)
// ##DEBUG##
// Update Water
updateWater()
// Restart game
if returnkey() = 1
goto restart:
endif
// Update screen
sync
// End loop
loop
// ##SCREEN SETUP##
function ScreenSetUp()
sync on
sync rate 60
set display mode 1024, 768, 32
autocam off
`position camera 0, 100, -300
`point camera 0, 0, 0
endFunction
// ##CAMERA##
function cameraTrack(fposx#, fposy#, fposz#)
// Use camera tracker to follow player object
angle#=object angle y(frog)
camdist#=150.0 : camhigh#=100.0 : camfade#=5.0
if fposy# > -200
set camera to follow fposx#,fposy#,fposz#,angle#,camdist#,camhigh#,camfade#,1
else
pitch camera down 0, 20
endif
// Tilt camera down a little
xrotate camera 20
SET CAMERA RANGE 1, 20000
ENDFUNCTION
// ##SKYBOX##
function skyBox()
boxNum as integer = 1000
texNum as integer = 1000
// Load Skybox images
load image "media/1.bmp", texNum
load image "media/2.bmp", texNum + 1
load image "media/3.bmp", texNum + 2
load image "media/4.bmp", texNum + 3
load image "media/6.bmp", texNum + 4
load image "media/5.bmp", texNum + 5
// Create Skybox Plains
make object plain boxNum, 5120, 5120
make object plain boxNum + 1, 5120, 5120
make object plain boxNum + 2, 5120, 5120
make object plain boxNum + 3, 5120, 5120
make object plain boxNum + 4, 5120, 5120
make object plain boxNum + 5, 5120, 5120
// Texture skybox plains
texture object boxNum, texNum
texture object boxNum + 1, texNum + 1
texture object boxNum + 2, texNum + 2
texture object boxNum + 3, texNum + 3
texture object boxNum + 4, texNum + 4
texture object boxNum + 5, texNum + 5
// Position Skybox plains
position object boxNum, -2560, 0, 0
position object boxNum + 1, 0, 0, 2560
position object boxNum + 2, 2560, 0, 0
position object boxNum + 3, 0, 0, -2560
position object boxNum + 4, 0, 2560, 0
position object boxNum + 5, 0, -2560, 0
// Set ambient reflectivity to off determine texture type and set plain direction
for i=boxNum to boxNum + 5
set object ambient i,0
set object texture i,2,0
point object i,0,0,0
next i
endFunction
// ##FROG##
// Create frog, set collisions and shadowing
function createFrog
load object "media/flikk_frog.x", frog
load image "media/flikk_tex.jpg", frog
Load Effect "shaders\Gloss.fx", frog, frog
Set Object Effect frog, 1
texture object frog, 1, 1
scale object frog, 120, 120, 120
position object frog, 0, 50, -130
`set object collision to polygons frog
make object collision box frog, -8, 2, -5, 8, 20, 5, 0
set shadow shading on frog,-1,1000,0
endFunction
// ##WORLD##
// Create world, set position, collisions and shadows
function createWorld(lilyZSpacing)
load object "media/world.x", worldObj
load image "media/flikk_tex.jpg", worldObj
texture object worldObj, 1
`Load Effect "shaders\Gloss.fx", 3, worldObj
`Set Object Effect worldObj, 1
scale object worldObj, 2000, 2000, 2000
rotate object worldObj, 0, 0, 0
position object worldObj, 0, -60, -150
set object collision to polygons worldObj
//Create second platform
clone object worldObj + 1, worldObj
position object worldObj + 1 , 0, -60, lilyZSpacing + 80
`make object collision box worldObj, -10, -10, -10, 10, 2, 0,0
`set shadow shading on worldObj, -1, 1000, 0
endFunction
// ##WATER##
function MakeWater()
// Globals
global ObjNum=500
global ImgNum=500
global FXNum=500
global WaterHeight#=-50.0
global WaterCamY#
// Setup Refraction Camera
make camera 30
set camera range 30,1,20000
set camera aspect 30,1.325
backdrop off 30
set camera to image 30, ImgNum, 1024, 1024
set camera fov 30,65
// Setup Reflection Camera
make camera 31
set camera range 31,1,20000
set camera aspect 31,1.325
backdrop off 31
set camera to image 31,ImgNum+1,1024,1024
set camera fov 31,65
// Make Water plain
load image "media/Waves2.dds",ImgNum+2
load image "media/WaterMask.bmp",ImgNum+3
make object plain ObjNum,15000,15000
position object objnum, -5000, 0, -5000
texture object ObjNum, 0, ImgNum+2
texture object ObjNum, 1, ImgNum
texture object ObjNum, 2, ImgNum+1
texture object ObjNum, 3, ImgNum+3
load effect "shaders/Water.fx", FXNum, 0
set object effect ObjNum, FXNum
xrotate object ObjNum, 270
set object transparency ObjNum, 1
endfunction
// Update Water
function updateWater()
// update Water plain
position object ObjNum, object position x(ObjNum), WaterHeight#, object position z(ObjNum)
WaterCamY# = camera position y() - WaterHeight#
Hide object ObjNum
// Upade Reflections cameras
updateReflection()
// Show Water
show object ObjNum
// Mask
sync mask 0x3fffffff
endfunction
// Update Reflections
function updateReflection()
// Update Reflection camera
position camera 31, camera position x(), WaterHeight#-WaterCamY#, camera position z()
rotate camera 31, -camera angle x(), camera angle y(), camera angle z()
// Clip Camera
ClipY# = WaterCamY#
if ClipY# > 100 then ClipY# = 100
set camera clip 31, 1, 0, WaterHeight#-(1+(ClipY#/25)), 0, 0, 1, 0
set camera clip 30, 0, 0, 0, 0, 0, 0, 0
// Only sync Camera 31
sync mask 0x80000000
fastsync
endfunction