These are my general functions that I've made to make my game making times easier, and no, this isn't one of those ones with 10 object distance functions (though I do have a lengthVector() function)...
Basically I decided to start by making my own free[Something]() library... I moved on and make things like 'Obj=makeSphere(Size)', from there I made my 'dLoad()' function, which I will explain later. I then thought to myself, "something that really bothers me is not being able to find a free vector, what a bummer" so, I remade all the vector commands in DBPro (obviously not as fast, but I prefer it none-the-less)... And to finish off I made some Sprite3D functions, which basically do what sprite commands do, but I find are better, and I can put text over them.
Here are all the functions + an example:
`Start
stStart()
`create cubes
cube1=makeCube(100)
sph1=makeSphere(100)
`following cube data
vec=makeVector()
`create a sprite to show fps (lol)
sptObj=freeObject()
spt=sprite3D(0,0,200,50,100,0)
`create world
make matrix 1,10000,10000,50,50
`Main Loop
do
`control object
if leftkey() then ay#=wrapvalue(ay#-3)
if rightkey() then ay#=wrapvalue(ay#+3)
if upkey()
x#=x#+(sin(ay#)*50)
z#=z#+(cos(ay#)*50)
endif
if downkey()
x#=x#-(sin(ay#)*50)
z#=z#-(cos(ay#)*50)
endif
if keystate(17) then y#=y#+50
if keystate(31) then y#=y#-50
position object cube1,x#,y#,z#
yrotate object cube1,ay#
`follow cube (using vectors)
setVector(vec,x#-fx#,y#-fy#,z#-fz#)
acx#=getVectorX(vec)/10
acy#=getVectorY(vec)/10
acz#=getVectorZ(vec)/10
vx#=vx#+acx# : vy#=vy#+acy# : vz#=vz#+acz#
vx#=vx#*0.95 : vy#=vy#*0.95 : vz#=vz#*0.95
fx#=fx#+vx# : fy#=fy#+vy# : fz#=fz#+vz#
position object sph1,fx#,fy#,fz#
yrotate object sph1,ay#
`camera
ca#=curveangle(ay#,ca#,20.0)
cam3RDPerson(x#,y#,z#,ca#,600,300)
`sprite text
ink rgb(255,0,0),0
text 10,10,"text over sprite!"
sync
loop
`Startup Functions
function stStart()
sync on : sync rate 60
set display mode 1024,768,32 : hide mouse
set camera range 1,0x7fffffff : autocam off
backdrop on : color backdrop 0
ldFunc()
endfunction
function ldFunc()
dim objTaken(0) as boolean
dim imgTaken(0) as boolean
dim sndTaken(0) as boolean
dim musTaken(0) as boolean
dim fVec3(0) as tVec3
dim fSprite3D(0) as tSprite3
endfunction
`Free Entity Functions
function freeObject()
local Num as integer
array insert at bottom objTaken() : num=array count(objTaken())
if object exist(num)=0 and objTaken(num)=0
objTaken(num)=1 : exitfunction num
endif
endfunction 0
function freeImage()
local Num as integer
array insert at bottom imgTaken() : num=array count(imgTaken())
if image exist(num)=0 and imgTaken(num)=0
imgTaken(num)=1 : exitfunction num
endif
endfunction 0
function freeSound()
local Num as integer
array insert at bottom sndTaken() : num=array count(sndTaken())
if sound exist(num)=0 and sndTaken(num)=0
sndTaken(num)=1 : exitfunction num
endif
endfunction 0
function freeMusic()
local Num as integer
array insert at bottom musTaken() : num=array count(musTaken())
if music exist(num)=0 and musTaken(num)=0
musTaken(num)=1 : exitfunction num
endif
endfunction 0
function dLoad(file$)
local num as integer
local lFile$=lower$(file$)
local dim oEnd$(1) : oEnd$(0)=".x" : oEnd$(1)=".3ds"
local dim iEnd$(1) : iEnd$(0)=".bmp" : oEnd$(1)=".jpg"
local dim sEnd$(0) : sEnd$(0)=".wav" : sEnd$(1)=".wav"
local dim mEnd$(1) : mEnd$(0)=".mp3" : mEnd$(1)=".mid"
if right$(lFile$,2)=oEnd$(0) then num=freeObject() : load object file$,num
if right$(lFile$,4)=oEnd$(1) then num=freeObject() : load object file$,num
if right$(lFile$,4)=iEnd$(0) then num=freeImage() : load image file$,num
if right$(lFile$,4)=iEnd$(1) then num=freeImage() : load image file$,num
if right$(lFile$,4)=sEnd$(0) then num=freeSound() : load sound file$,num
if right$(lFile$,4)=sEnd$(1) then num=freeSound() : load sound file$,num
if right$(lFile$,4)=mEnd$(0) then num=freeMusic() : load music file$,num
if right$(lFile$,4)=mEnd$(1) then num=freeMusic() : load music file$,num
undim oEnd$() : undim iEnd$() : undim sEnd$() : undim mEnd$()
endfunction
function makeCube(Size as float)
local obj as integer : obj=freeObject()
make object cube obj,size
endfunction obj
function makeSphere(Size as float)
local obj as integer : obj=freeObject()
make object sphere obj,size
endfunction obj
function makeBox(SizeX as float,SizeY as float,SizeZ as float)
local obj as integer : obj=freeObject()
make object box obj,sizex,sizey,sizez
endfunction obj
function makeCone(Size as float)
local obj as integer : obj=freeObject()
make object cone obj,size
endfunction obj
function makeCylinder(Size as float)
local obj as integer : obj=freeObject()
make object cylinder obj,size
endfunction obj
function makePlain(SizeX as float,SizeY as float)
local obj as integer : obj=freeObject()
make object plain obj,sizex,sizey
endfunction obj
function makeTriangle(x1 as float,y1 as float,z1 as float,x2 as float,y2 as float,z2 as float,x3 as float,y3 as float,z3 as float)
local obj as integer : obj=freeObject()
make object triangle obj,x1,y1,z1,x2,y2,z2,x3,y3,z3
endfunction obj
`Control Functions
function msControl(speed as float)
local ax as float : local ay as float
local x as float : local y as float : local z as float
rotate camera wrapvalue(camera angle x()+mousemovey()),wrapvalue(camera angle y()+mousemovex()),0
if mouseclick()=1
move camera speed
endif
if mouseclick()=2
move camera -speed
endif
endfunction
function cam3RDPerson(x#,y#,z#,ca#,oz#,oy#)
cx#=x#-(sin(ca#)*oz#)
cy#=y#+oy#
cz#=z#-(cos(ca#)*oz#)
position camera cx#,cy#,cz#
point camera x#,y#,z#
endfunction
`Vector Functions
function freeVector()
local num as integer
array insert at bottom fVec3() : num=array count(fVec3())
endfunction num
function makeVector()
local num as integer
num=freeVector()
fVec3(num).x=0 : fVec3(num).y=0 : fVec3(num).z=0
endfunction num
function setVector(vec,x#,y#,z#)
fVec3(vec).x=x# : fVec3(vec).y=y# : fVec3(vec).z=z#
endfunction
function addVector(VecRes,VecA,VecB)
fVec3(VecRes).x=fVec3(VecA).x+fVec3(VecB).x
fVec3(VecRes).y=fVec3(VecA).y+fVec3(VecB).y
fVec3(VecRes).z=fVec3(VecA).z+fVec3(VecB).z
endfunction
function subVector(VecRes,VecA,VecB)
fVec3(VecRes).x=fVec3(VecA).x-fVec3(VecB).x
fVec3(VecRes).y=fVec3(VecA).y-fVec3(VecB).y
fVec3(VecRes).z=fVec3(VecA).z-fVec3(VecB).z
endfunction
function lengthVector(vec)
local length as float
length=sqrt((fVec3(vec).x^2)+(fVec3(vec).y^2)+(fVec3(vec).z^2))
endfunction length
function AngleXVector(vec)
local ang as float
ang=atanfull(fVec3(vec).y,fVec3(vec).z)
endfunction ang
function AngleYVector(vec)
local ang as float
ang=atanfull(fVec3(vec).x,fVec3(vec).z)
endfunction ang
function AngleZVector(vec)
local ang as float
ang=atanfull(fVec3(vec).x,fVec3(vec).y)
endfunction ang
function invVector(vec)
fVec3(vec).x=-fVec3(vec).x
fVec3(vec).y=-fVec3(vec).y
fVec3(vec).z=-fVec3(vec).z
endfunction
function multiplyVector(vec,value as float)
fVec3(vec).x=fVec3(vec).x*value
fVec3(vec).y=fVec3(vec).y*value
fVec3(vec).z=fVec3(vec).z*value
endfunction
function divideVector(vec,value as float)
fVec3(vec).x=fVec3(vec).x/value
fVec3(vec).y=fVec3(vec).y/value
fVec3(vec).z=fVec3(vec).z/value
endfunction
function normalizeVector(vec)
local length as float : length=lengthVector(vec)
fVec3(vec).x=fVec3(vec).x/length
fVec3(vec).y=fVec3(vec).y/length
fVec3(vec).z=fVec3(vec).z/length
endfunction
function dotProductVector(VecA,VecB)
local ang as float : local dheight as float
local lA as float : local lB as float
local DotP as float
dHeight=sqrt((fVec3(VecB).x-fVec3(VecA).x)^2+(fVec3(VecB).z-fVec3(VecA).z)^2)
ang=atanfull(dHeight,fVec3(vecA).y)
lA=lengthVector(VecA) : lB=lengthVector(VecB)
DotP=lA*lB*cos(ang)
endfunction DotP
function crossProductVector(VecR,VecA,VecB)
fVec3(VecR).x=(fVec3(VecA).y*fVec3(VecB).z)-(fVec3(VecA).z*fVec3(VecB).y)
fVec3(VecR).y=(fVec3(VecA).z*fVec3(VecB).x)-(fVec3(VecA).x*fVec3(VecB).z)
fVec3(VecR).z=(fVec3(VecA).x*fVec3(VecB).z)-(fVec3(VecA).z*fVec3(VecB).x)
endfunction
function rotateVector(vec,ax as float, ay as float)
local length as float : length=lengthVector(vec)
fVec3(vec).x=sin(ay)*cos(ax)*length
fVec3(vec).y=-sin(ax)*length
fVec3(vec).z=cos(ay)*cos(ax)*length
endfunction
function getVectorX(vec)
local vl as float
vl=fVec3(vec).x
endfunction vl
function getVectorY(vec)
local vl as float
vl=fVec3(vec).y
endfunction vl
function getVectorZ(vec)
local vl as float
vl=fVec3(vec).z
endfunction vl
function equalVector(VecA,VecB)
if fVec3(VecA).x=fVec3(VecB).x and fVec3(VecA).y=fVec3(VecB).y and fVec3(VecA).z=fVec3(VecB).z
exitfunction 1
endif
endfunction 0
function getNormal(ax as float, ay as float, vec)
fVec3(vec).x=sin(ay)*cos(ax)
fVec3(vec).y=-sin(ax)
fVec3(vec).z=cos(ay)*cos(ax)
endfunction
`3D Sprite Functions
function sprite3D(x,y,sx as float,sy as float,alpha,i)
local num as integer
array insert at bottom fSprite3D() : num=array count(fSprite3D())
obj=freeObject()
fSprite3D(num).obj=obj
fSprite3D(num).img=img
fSprite3D(num).pos.x=0-((screen width()/2)-(sx/2))+x
fSprite3D(num).pos.y=((screen height()/2)-(sy/2))-y
fSprite3D(num).pos.z=(screen width()-screen height())*2.5
fSprite3D(num).size.x=sx
fSprite3D(num).size.y=sy
fSprite3D(num).alpha=alpha
if object exist(fSprite3D(num).obj)=0
make object plain fSprite3D(num).obj,fSprite3D(num).size.x,fSprite3D(num).size.y
position object obj,fSprite3D(num).pos.x,fSprite3D(num).pos.y,fSprite3D(num).pos.z
lock object on obj
disable object zwrite obj
set object ambient obj,0 : set object light obj,0
set object fog obj,0 : set object transparency obj,3
set object filter obj,0 : set object smoothing obj,100
set alpha mapping on obj,alpha
endif
endfunction num
function setSprite3D(num,x as float, y as float, sx as float,sy as float,alpha as float,i)
fSprite3D(num).pos.x=0-((screen width()/2)-(sx/2))+x
fSprite3D(num).pos.y=((screen height()/2)-(sy/2))-y
fSprite3D(num).pos.z=(screen width()-screen height())*2.5
fSprite3D(num).size.x=sx
fSprite3D(num).size.y=sy
fSprite3D(num).alpha=alpha
fSprite3D(num).img=i
endfunction
function updateSprite3D(num)
texture object fSprite3D(num).obj,fSprite3D(num).img
position object fSprite3D(num).obj,fSprite3D(num).pos.x,fSprite3D(num).pos.y,fSprite3D(num).pos.z
scale object fSprite3D(num).obj,fSprite3D(num).size.x,fSprite3D(num).size.y,0
set alpha mapping on fSprite3D(num).obj,fSprite3D(num).alpha
if fSprite3D(num).visible then show object fSprite3D(num).obj else hide object fSprite3D(num).obj
endfunction
function setVisibilitySprite3D(num,flag as boolean)
fSprite3D(num).visible=flag
endfunction
function checkObjSprite3D(obj)
for n=1 to array count(fSprite3D())
if fSprite3D(n).obj=obj
exitfunction n
endif
next n
endfunction 0
function checkSpriteObj3D(num)
obj=fSprite3D(num).obj
endfunction obj
`Types
type tVec3
x as float
y as float
z as float
endtype
type tSprite3
obj as integer
img as integer
pos as tVec3
size as tVec3
alpha as integer
visible as boolean
endtype
and now I'll try to explain a few functions:
stStart() - Standard Startup, better typing 1 line than all of those
freeObject() - find free object, the usual
freeImage () - etc etc etc
dLoad(file$) - will load a file, the type is not required (such as object, image), and will place it in an empty slot, and return the number
makeCube(size#) - creates a cube of certain size and returns free object number
makeSphere(size#) - etc etc etc
msControl(speed#) - my standard mouse control, best to function it.
cam3RDPerson(x#,y#,z#,ca#,oz#,oy#) - will make the camera follow a certain point, and the camera moved out slightly, very easy to code, but annoying none-the-less. note: curveangle() doesn't work in functions, so you'll have to provide your own angle..
freeVector() - no use to you..
makeVector() - finds a free vector and creates it.
setVector(x#,y#,z#) - blah blah blah
I added a few vector commands that aren't in DBPro, such as:
invVector() - inverts it
AngleX/Y/Zvector() - gets the x/y/z angle of a vector
rotateVector() - will rotate a vector by a certain x and y angle, z is not needed
equalVector() - will check if two vectors are equal
getNormal(ax#,ay#,vec) - put in an x and y angle, and define a vector, the normal of these angles will be placed into that vector. Just use the getVectorX/Y/Z() commands to retrieve the data.
Note: I hope you noticed, the vector commands are only 3 dimentional.
sprite3D(x,y,size x, size y, alpha, image) - will create a sprite for a plain, just define the screen coordinates, size, alpha and image number, and it will create it. It will also return the number of the sprite, so ofcourse it's created without you having to handle the numbers for it, just like freeObject() (thanks for the help Cu')
setSprite3D(num,x,y,sx,sy,alpha,i) - basically, this has to be used instead of the data on the first command if you want it's data to be changed.
updateSprite3D() - will update it's data.
setVisibilitySprite3D(num,flag) - just like hide/show sprite
checkObjSprite3D(obj) - will check if an object is being used as a sprite or not
checkSpriteObj3D(num) - will return the object used for that sprite.
There's also a usefull type at the end of the code you can use called tVec3, it's used for the vector commands, but you might aswell use it if you need it, saves you remaking it
I doubt the sprite3d type will come in usefull for ya.
Enjoy