Here's that code for you to play with:
sync on
sync rate 0
autocam off
backdrop on
color backdrop rgb(0,0,255)
#constant USER32DLL 255
type ObjectInfo_t
Exists as dword
x as float
y as float
z as float
xa as float
ya as float
za as float
endtype
type CameraInfo_t
Exists as dword
x as float
y as float
z as float
xa as float
ya as float
za as float
endtype
global dim ObjectInfo(100) as ObjectInfo_t ` Allow up to 100 objects
global dim CameraInfo(0) as CameraInfo_t ` Allow only 1 camera
CreateGameObjects()
` Wait for things to settle down before starting
WaitForValidDisplay()
` Get the window handle, so we can check if it has been minimised later
Window = OS_GetForegroundWindow()
do
` While the app is minimised, just wait for new windows messages, until we aren't minimised any more
while OS_IsIconic( Window )
OS_WaitMessage()
endwhile
` If the D3D screen surface has been lost ...
if screen invalid()
` Wait until the screen surface has been recovered ...
WaitForValidDisplay()
` then reset the camera and all objects
RestoreGameCamera()
RestoreGameLights()
RestoreGameObjects()
endif
` Do game stuff
RotateCamera( 0, wrapvalue(camera angle x() + mousemovey()), wrapvalue(camera angle y() + mousemovex()), 0.0 )
if upkey() then MoveCamera(0, 0.5)
if downkey() then MoveCamera(0, -0.5)
set cursor 0, 0
position mouse 320, 240
print pick object(320, 240, 1, 100)
print CountGameObjects(1,100)
` Update the display
sync
loop
function WaitForValidDisplay()
while screen invalid()
endwhile
endfunction
function OS_WaitMessage()
local Result as dword
if dll exist(USER32DLL) = 0 then load dll "user32.dll", USER32DLL
Result = call dll(USER32DLL, "WaitMessage")
endfunction Result
function OS_GetForegroundWindow()
local Result as dword
if dll exist(USER32DLL) = 0 then load dll "user32.dll", USER32DLL
Result = call dll(USER32DLL, "GetForegroundWindow")
endfunction Result
function OS_IsIconic(hwnd as dword)
local Result as dword
if dll exist(USER32DLL) = 0 then load dll "user32.dll", USER32DLL
Result = call dll(USER32DLL, "IsIconic", hwnd)
endfunction Result
function CountGameObjects(Start as integer, Finish as integer)
local Counter as integer
local Obj as integer
for Obj = Start to Finish
inc Counter, object exist( Obj )
next
endfunction Counter
function RestoreGameCamera()
set display mode screen width(), screen height(), screen depth()
backdrop on
color backdrop rgb(0,0,255)
position camera CameraInfo(0).x, CameraInfo(0).y, CameraInfo(0).z
rotate camera CameraInfo(0).xa, CameraInfo(0).ya, CameraInfo(0).za
endfunction
function RestoreGameLights()
endfunction
function RestoreGameObjects()
local Obj as integer
local Top as integer
Top = array count( ObjectInfo() )
for Obj = 1 to Top
if object exist(Obj) then delete object Obj
if ObjectInfo(Obj).Exists = 1
make object cube Obj, 1.0
position object Obj, ObjectInfo(Obj).x, ObjectInfo(Obj).y, ObjectInfo(Obj).z
rotate object Obj, ObjectInfo(Obj).xa, ObjectInfo(Obj).ya, ObjectInfo(Obj).za
endif
next
endfunction
function CreateGameObjects()
local Obj as integer
for Obj = 1 to 100
MakeObjectCube( Obj, 1.0 )
PositionObject( Obj, rnd(100)-50, rnd(100)-50, rnd(100)-50 )
PointObject( Obj, rnd(100)-50, rnd(100)-50, rnd(100)-50 )
next Obj
endfunction
` Substitutes for camera commands
function RotateCamera(Cam as integer, xa as float, ya as float, za as float)
rotate camera Cam, xa, ya, za
CameraInfo(Cam).xa = xa
CameraInfo(Cam).ya = ya
CameraInfo(Cam).za = za
endfunction
function PositionCamera(Cam as integer, x as float, y as float, z as float)
position camera Cam, x, y, z
CameraInfo(Cam).x = x
CameraInfo(Cam).y = y
CameraInfo(Cam).z = z
endfunction
function PointCamera(Cam as integer, x as float, y as float, z as float)
point camera Cam, x, y, z
CameraInfo(Cam).xa = camera angle x( Cam )
CameraInfo(Cam).ya = camera angle y( Cam )
CameraInfo(Cam).za = camera angle z( Cam )
endfunction
function MoveCamera(Cam as integer, Distance as float)
move camera Cam, Distance
CameraInfo(Cam).x = camera position x( Cam )
CameraInfo(Cam).y = camera position y( Cam )
CameraInfo(Cam).z = camera position z( Cam )
endfunction
` Substitutes for object commands
function MakeObjectCube(Obj as integer, Size as float)
make object cube Obj, 1.0
CreateObject( Obj )
endfunction
function RotateObject(Obj as integer, xa as float, ya as float, za as float)
rotate object Obj, xa, ya, za
ObjectInfo(Obj).xa = xa
ObjectInfo(Obj).ya = ya
ObjectInfo(Obj).za = za
endfunction
function PositionObject(Obj as integer, x as float, y as float, z as float)
position object Obj, x, y, z
ObjectInfo(Obj).x = x
ObjectInfo(Obj).y = y
ObjectInfo(Obj).z = z
endfunction
function PointObject(Obj as integer, x as float, y as float, z as float)
point object Obj, x, y, z
ObjectInfo(Obj).xa = object angle x(Obj)
ObjectInfo(Obj).ya = object angle y(Obj)
ObjectInfo(Obj).za = object angle z(Obj)
endfunction
function CreateObject(Obj)
ObjectInfo(Obj).Exists = 1
ObjectInfo(Obj).x = 0.0
ObjectInfo(Obj).y = 0.0
ObjectInfo(Obj).z = 0.0
ObjectInfo(Obj).xa = 0.0
ObjectInfo(Obj).ya = 0.0
ObjectInfo(Obj).za = 0.0
endfunction
function DeleteObject(Obj)
ObjectInfo(Obj).Exists = 0
if object exist(Obj) then delete object Obj
endfunction
It contains just the basics - if you need to reload object, reload and reapply textures, create lights etc, then that's stuff you'll need to add in by extending and adding to the UDT's and adding in code to deal with it.