Code: Select all
Declare rnd2(num)
Procedure ErrorHandler()
description.s=GetErrorDescription()
module.s=GetErrorModuleName()
string.s+dbGetErrorDescription(@description.s)+#CRLF$
string.s+"Line: "+Str(GetErrorLineNR())+#CRLF$
string.s+"AFile: "+dbGetErrorFilename(@module.s)+#CRLF$
MessageRequester("Custom Error",string.s)
End
EndProcedure
OnErrorGosub(@ErrorHandler())
hDBWnd=OpenDBWnd(0,0,640,480,32,#GDK_Window_SystemMenu|#GDK_Window_ScreenCentered)
; Advanced Terrain System - Example 1
; sky textures from - SkyMatter
; heightmap created by - David Smith ( infiniteloop@optushome.com.au )
; terrain base texture - generated from Advanced Terrain System Plus
; demo by - TGC
; set up display And camera
dbBackdropOn()
dbAutoCamOff()
dbSetCameraRange(0.5, 30000)
; movement
g_fSpeed.f = 0.05
g_fTurn.f = 0.3
; set the directory To media
dbSetDir("Media")
; load base And detail texture
dbLoadImage("texture.bmp",1)
dbLoadImage("detail.tga",2)
dbMakeObjectTerrain(1) ; create the terrain object
dbSetTerrainHeightMap(1,"map.bmp") ; set the heightmap
dbSetTerrainScale(1,3,0.6,3) ; set the scale
dbSetTerrainSplit(1,16) ; split value by 16 * 16
dbSetTerrainTiling(1,4) ; detail map tiling
dbSetTerrainLight(1,1,-0.25,0,1,1,0.78,0.5) ; light - xdir, ydir, zdir, red, green, blue, intensity
dbSetTerrainTexture(1,1,2) ; base And detail texture
dbBuildTerrain(1) ; finally build the terrain
; load our skybox
dbLoadObject("skybox2.x",200)
dbSetObjectLight(200,0)
dbSetObjectTexture(200,3,1)
dbPositionObject(200,1000,2000,4000)
dbScaleObject(200,30000,30000,30000)
; reset the directory
dbSetDir("..")
; position the camera
dbPositionCamera(385,23,100)
; main program loop
Repeat
; handle user input And show some stats
Gosub userInput
Gosub information
; get the height of the terrain at the current camera position
a.f=dbGetTerrainGroundHeight(1,dbCameraPositionX(),dbCameraPositionZ())
; now position the camera slightly above the terrain
dbPositionCamera(dbCameraPositionX(),a.f+3,dbCameraPositionZ())
; let the terrain handle some internal work
dbUpdateTerrain()
; final screen update
dbSync()
ForEver
userInput:
; simple mouse And keyboard movement
; move around With arrow keys
dbControlCameraUsingArrowKeys(0,g_fSpeed.f,g_fTurn.f)
; store old camera angle
OldCamAngleY.f=CameraAngleY.f
OldCamAngleX.f=CameraAngleX.f
; store new camera angle
CameraAngleY.f=dbWrapValue(CameraAngleY.f+dbMouseMoveX()*0.4)
CameraAngleX.f=dbWrapValue(CameraAngleX.f+dbMouseMoveY()*0.4)
; rotate camera
dbYRotateCamera(dbCurveAngle(CameraAngleY.f,OldCamAngleY.f,24))
dbXRotateCamera(dbCurveAngle(CameraAngleX.f,OldCamAngleX.f,24))
; speed up movement
If dbInKey()="+"
If g_fSpeed.f<1000
g_fSpeed.f=g_fSpeed.f+0.01
EndIf
EndIf
; slow down movement
If dbInKey()="-"
If g_fSpeed.f>0.002
g_fSpeed.f=g_fSpeed.f-0.001
EndIf
EndIf
Return
information:
; show some information
; start printing at top of screen
dbSetCursor(0,0)
; show frame rate
dbPrint("fps = "+Str(dbScreenFPS()))
dbPrint("")
; current camera position
dbPrint("")
dbPrint("x = "+StrF(dbCameraPositionX()))
dbPrint("y = "+StrF(dbCameraPositionY()))
dbPrint("z = "+StrF(dbCameraPositionZ()))
dbPrint("")
dbPrint("FPS "+Str(dbScreenFPS()))
; finally the polygon count
dbPrint("polygon count = "+Str(dbStatistic(1)))
dbPrint("")
Return
Here is an example for Dark Physics using the buoyancy demo. It uses some PureGDK 1.1 syntax so you'll have to tweak it to make it work. Specifically the OpenDBWnd parameters have changed, the OpenWindow superfluous, and dbGetActiveFunctions isn't implemented.
Code: Select all
; demonstrates a simple bouyancy effect
Procedure Thread(Null)
ExamineDesktops()
OpenWindow(1,DesktopWidth(0)-186,0,180,50,"")
TextGadget(0,8,8,200,200,"dbGetActiveFunctions 0")
Repeat
WaitWindowEvent(10)
SetGadgetText(0,"dbGetActiveFunctions "+Str(dbGetActiveFunctions()))
ForEver
EndProcedure
; ThreadID=CreateThread(@Thread(),0)
; show the PureGDK render window
OpenWindow(0,0,0,640,480,"DarkBasic Professional - PureGDK 1.1.0 Beta",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
hDBWnd=OpenDBWnd(WindowID(0),0,0,640,480,32)
; SetParent_(hDBWnd,0)
; SetWindowLongPtr_(hDBWnd,#GWL_STYLE,GetWindowLongPtr_(hDBWnd,#GWL_STYLE)|#WS_POPUP&(~#WS_CHILD)&(~#WS_DISABLED))
; sync rate on
dbSetDisplayMode(640,480,32,1)
dbSetDir("D:\Program Files\Dark Basic Professional\Projects\Dark Physics\Demos\Rigid Body\Water")
; SetClassLong_(hDBWnd,#GCL_HICON,Str(#IDI_APPLICATION))
; Init
width.f=640: height.f=480
dbAutoCamOff()
dbSetTextFont("Verdana")
dbInk(dbRGB(255,255,255),0)
; prepare counters
Define tmlast.q
Define tmcode.q
Define tmphysics.q
Define tmvisuals.q
; Light
dbSetNormalizationOn(): dbSetAmbientLight(5)
dbSetDirectionalLight(0,0.2,-0.7,0.1)
; Camera
dbPositionCamera(-40,12,-55)
dbSetCameraRange(1,15000)
dbSetCameraAspect(width.f/height.f)
dbSetCameraFOV(70)
dbBackdropOff()
; settings
hardwaremode=1
; capability
dbGetD3DCapabilities(@Result.D3DCaps)
clipvalid=Result\ClipTLVerts
pixelshader.f=Result\MaxPixelShaderVersion.f
; Logo sprite
dbLoadImage("logo.png",100000)
dbSprite(1,0,dbScreenHeight()-60,100000)
dbSizeSprite(1,dbScreenWidth(),60)
; SW/HW loop
Repeat
; Setup physics
Gosub _physics_init
Gosub _physics_setup
; Setup water
Gosub _water_setup
; Main
While dbSpaceKey()=0
dbSetCurrentCamera(0)
; camera movement
If dbUpKey()=1
dbMoveCamera(1)
EndIf
If dbDownKey()=1
dbMoveCamera(-1)
EndIf
angy.f=dbCameraAngleY()
angx.f=dbCameraAngleX()
dbXRotateCamera(0)
If dbLeftKey()=1
dbYRotateCamera(angy.f-90): dbMoveCamera(1): dbYRotateCamera(angy.f)
EndIf
If dbRightKey()=1
dbYRotateCamera(angy.f+90): dbMoveCamera(1): dbYRotateCamera(angy.f)
EndIf
dbYRotateCamera(angy.f+dbMouseMoveX()/2.0)
dbXRotateCamera(angx.f+dbMouseMoveY()/2.0)
dbPointCamera(0,0,0)
dbPositionListener(dbCameraPositionX(),dbCameraPositionY(),dbCameraPositionZ())
dbRotateListener(90,0,0)
; prompts
swhw.s="HARDWARE"
If hardwaremode=0
swhw.s="SOFTWARE"
dbCenterText(dbScreenWidth()/2,60,"")
EndIf
dbCenterText(dbScreenWidth()/2,20,"PHYSX IN "+swhw.s+" OBJECTS:"+Str(objid-1)+" FPS:"+Str(dbScreenFPS())+" CODE:"+Str(tmcode/1000)+" PHYSICS:"+Str(tmphysics/1000)+" VISUALS:"+Str(tmvisuals/1000))
If dbPhysx()=1
dbCenterText(dbScreenWidth()/2,40,"PRESS SPACE TO TOGGLE BETWEEN SOFTWARE AND HARDWARE")
Else
dbCenterText(dbScreenWidth()/2,40,"PHYSX SOFTWARE SUPPORT ONLY")
EndIf
If pixelshader.f<2.0
dbCenterText(dbScreenWidth()/2,60,"YOU NEED A GPU WITH PIXEL SHADERS 2.0 OR ABOVE")
EndIf
; water camera and physics boyancy
Gosub _physics_handle
Gosub _water_handle
; update simulation
tmlast=ElapsedMilliseconds()
dbPhyUpdate()
If tmdelay=0
tmphysics=ElapsedMilliseconds()-tmlast
EndIf
; visual update
tmlast=ElapsedMilliseconds()
Gosub _sync
If tmdelay=0
tmvisuals=ElapsedMilliseconds()-tmlast
EndIf
; timer handler
tmdelay+1
If tmdelay>30
tmdelay=0
EndIf
If WindowEvent()=#WM_CLOSE
; KillThread(ThreadID)
End
EndIf
Wend
While dbSpaceKey()<>0 : Wend
; Prompt
dbSetTextSize(30)
dbCenterText(dbScreenWidth()/2,dbScreenHeight()/2,"RESETTING SCENE")
dbSync()
dbSetTextSize(12)
; toggle hardware flag
hardwaremode=1-hardwaremode
; free usages
dbPhyUpdate()
dbDeleteEffect(1): dbDeleteCamera(1): dbDeleteCamera(2)
For s=10 To 10+splashmax
dbDeleteSound(s)
Next s
dbDeleteObjects(1,objidmax)
dbPhyEnd()
; SW/HW endloop
Until WindowEvent()=#WM_CLOSE
; KillThread(ThreadID)
End
_sync:
; Hide water
dbHideObject(2)
; Quick render the RTTs first
dbSyncMask($00000006): dbSync()
; Show water
dbShowObject(2)
; Debug keys
If dbShiftKey()=1
dbPasteImage(3,0,0)
EndIf
If dbControlKey()=1
dbPasteImage(4,0,0)
EndIf
; Sync/end loop
dbSyncMask($00000001): dbSync()
Return
_physics_init:
; Start the Physics Engine
If hardwaremode=1
If dbPhysx()=1
dbPhyStart(1,0,1)
Else
hardwaremode=0
dbPhyStart()
EndIf
Else
dbPhyStart()
EndIf
dbPhySetSkinWidth(0.001)
Return
_water_setup:
; make a refraction camera
dbMakeCamera(1)
dbSetCameraRange(1,15000,1)
dbSetCameraFOV(70,1)
dbSetCameraAspect(width.f/height.f,1)
dbBackdropOff(1)
dbSetCameraToImage(1,3,512,512)
; make reflection camera
dbMakeCamera(2)
dbSetCameraRange(1,15000,2)
dbSetCameraFOV(70,2)
dbSetCameraAspect(width.f/height.f,2)
dbBackdropOff(2)
dbSetCameraToImage(2,4,512,512)
dbSetCameraClip(2,2,0,0,0,0,1,0)
; make water
dbSetDir("Water")
dbLoadImage("waves2.dds",2)
dbMakeObjectPlain(2,2000,2000)
dbXRotateObject(2,90)
dbTextureObject(2,2,0)
dbTextureObject(2,3,1)
dbTextureObject(2,4,2)
dbLoadEffect("Fresnel Water.fx",1,0)
dbSetObjectEffect(2,1)
dbSetObjectTransparency(2,1)
; make floor for water
dbLoadImage("f_g_01_d2.dds",5)
dbMakeObjectPlain(5,2000,2000)
dbXRotateObject(5,270)
dbPositionObject(5,0,-20,0)
dbScaleObjectTexture(5,20,20)
dbTextureObject(5,5)
dbSetDir("..")
; make skybox
dbSetDir("Sky")
dbLoadObject("skybox.x",3)
dbSetObjectLight(3,0)
dbScaleObject(3,20000,20000,20000)
dbSetObjectTexture(3,2,1)
dbSetObjectCull(3,0)
dbSetDir("..")
Return
_physics_setup:
; create original object
dbLoadObject("Wood\physXcrate.X",1): dbHideObject(1): dbSetObjectCull(1,0)
; make small box boat
objid=10: dbCloneObject(1,objid): dbScaleObject(objid,300,300,300): dbPositionObject(objid,0,0,0): dbRotateObject(objid,-5,0,0): dbPhyMakeRigidBodyDynamicBox(objid)
objid=11: dbCloneObject(1,objid): dbScaleObject(objid,200,200,200): dbPositionObject(objid,0,20,0): dbRotateObject(objid,-5,0,0): dbPhyMakeRigidBodyDynamicBox(objid)
; create many
Dim crate(2000)
For m=0 To 299
; crate
objid+1: dbInstanceObject(1,objid)
dbPositionObject(objid,dbCos(m*22)*50,50+(m*10),dbSin(m*22)*50)
dbRotateObject(objid,Random(360),Random(360),Random(360))
dbPhyMakeRigidBodyDynamicBox(objid)
dbPhyAddRigidBodyForce(objid,0,Random(1000),0,1)
crate(objid)=0
Next m
; Water splash
splashmax=50
dbSetDir("Water")
Dim splash(splashmax)
If splashmax>0
dbLoad3DSound("splash.wav",10)
For s=11 To 10+splashmax
dbCloneSound(s,10)
Next s
EndIf
; Make splash objects (ripple & decal effect)
dbLoadImage("ripple16.png",11)
dbLoadImage("bigsplash161.dds",12)
For ss=1 To splashmax
objid+1: dbMakeObjectPlain(objid,10,10)
dbXRotateObject(objid,270)
dbTextureObject(objid,11)
dbSetObjectLight(objid,0)
dbSetObjectTransparency(objid,2)
dbHideObject(objid)
If ss=1
splashobj=objid
EndIf
objid+1: dbMakeObjectPlain(objid,10,10)
dbTextureObject(objid,12)
dbSetObjectLight(objid,0)
dbSetObjectTransparency(objid,2)
dbHideObject(objid)
Next ss
objidmax=objid
dbSetDir("..")
; Scale listener for correct sound scape
dbScaleListener(0.2)
Return
_physics_handle:
; create a boyancy force effect below waterline
waterlevelangle.f=dbWrapValue(waterlevelangle.f+1.0)
waterlevel.f=dbCos(waterlevelangle.f)
; For objid=10 To objidmax
For objid=10 To 299
; boyancy force
forcex.f=0.0
forcey.f=0.0
forcez.f=0.0
mass.f=dbPhyGetRigidBodyMass(objid)
If dbObjectPositionY(objid)<0
; hit water line
forcey.f=(Abs(dbObjectPositionY(objid))*mass.f)/2.0
dbPhySetRigidBodyAngularDamping(objid,0.1+Abs(dbObjectPositionY(objid)/2.0))
dbPhySetRigidBodyLinearDamping(objid,0.1+Abs(dbObjectPositionY(objid)*4))
EndIf
If splashmax>0
If dbObjectPositionY(objid)<0 And objid>11
; splash
If crate(objid)=0
For s=1 To splashmax
If splash(s)=0
splash(s)=32
posx.f=dbObjectPositionX(objid)
posz.f=dbObjectPositionZ(objid)
dbPositionObject(splashobj+((s-1)*2)+0,posx.f,0.1,posz.f): dbShowObject(splashobj+((s-1)*2)+0)
dbPositionObject(splashobj+((s-1)*2)+1,posx.f,6.0,posz.f): dbShowObject(splashobj+((s-1)*2)+1)
dbPlaySound(10+s): dbPositionSound(10+s,posx.f,waterlevel.f,posz.f)
dbSetSoundSpeed(10+s,18000+Random(3000))
Break
EndIf
Next s
crate(objid)=1
EndIf
EndIf
EndIf
; small water/wave influences
If dbObjectPositionY(objid)<waterlevel.f
forcey.f=forcey.f+(Random(500)/100.0)*(mass.f/200.0)
EndIf
dbPhyAddRigidBodyForce(objid,forcex.f,forcey.f,forcez.f,1)
Next objid
; handle splashes
If splashmax>0
For s=1 To splashmax
If splash(s)>0
n.f=50+(100-((splash(s)/32.0)*100.0)): nn.f=n.f/2.0
splobj1=splashobj+((s-1)*2)+0: dbScaleObject(splobj1,n.f,n.f,n.f)
splobj2=splashobj+((s-1)*2)+1: dbScaleObject(splobj2,100+nn.f,100+nn.f,100+nn.f)
dbPointObject(splobj2,dbCameraPositionX(0),dbCameraPositionY(0),dbCameraPositionZ(0))
dbRotateObject(splobj2,0,dbObjectAngleY(splobj2),0)
splash(s)=splash(s)-1
If splash(s)>0
; animation UV plain
objid=splobj1: frame=15-(splash(s)/2): Gosub _animate_uv_plain
objid=splobj2: frame=15-(splash(s)/2): Gosub _animate_uv_plain
Else
dbHideObject(splobj1): dbHideObject(splobj2)
EndIf
EndIf
Next s
EndIf
Return
_water_handle:
; move camera 2 to camera 1 pos
dbPositionCamera(dbCameraPositionX(),dbCameraPositionY(),dbCameraPositionZ(),1)
dbRotateCamera(dbCameraAngleX(),dbCameraAngleY(),dbCameraAngleZ(),1)
dbPositionCamera(dbCameraPositionX(),dbCameraPositionY(),dbCameraPositionZ(),2)
dbRotateCamera(dbCameraAngleX(),dbCameraAngleY(),dbCameraAngleZ(),2)
; sky position for camera
dbPositionObject(3,dbCameraPositionX(0),dbCameraPositionY(0)+200,dbCameraPositionZ(0))
Return
_animate_uv_plain:
dbLockVertexDataForLimb(objid,0)
vi=0 : vv.f=Int(frame/4)/4.0 : uu.f=(frame-(Int(vv.f)*4))/4.0
u0.f=uu.f : u1.f=uu.f+0.25 : v0.f=vv.f : v1.f=vv.f+0.25
dbSetVertexDataUV(vi+0,u1.f,v0.f)
dbSetVertexDataUV(vi+1,u0.f,v0.f)
dbSetVertexDataUV(vi+2,u1.f,v1.f)
dbSetVertexDataUV(vi+3,u0.f,v0.f)
dbSetVertexDataUV(vi+4,u0.f,v1.f)
dbSetVertexDataUV(vi+5,u1.f,v1.f)
dbUnlockVertexData()
Return
There is another Dark Physics example someone posted here of the vehicle demo. I think it's in this thread or the beta one.