I took the SetupWindow_LoadMedia() function out from the above code and made it so it wouldn't call "set window" again. This helps cut down the screen flickering.
Also this example doesn't need an image file to be loaded. It creates it's own image. I did that to make it easier for you to test this code out without having to find your own image and making sure it calls it. I should of done that the first time.
* So far this hasn't failed on me yet. I need to test this out on a bigger program. Please share your experience with us if you try this method. Thanks.
REM Device Lost State Example - By: Todd Riggins - Updated: June 27, 2012 - ExoDev.Com
REM Delete everything / Reload what you need
REM This code shows how you can recover from a device lost state. A device will
REM reset itself from a device lost state. When this happens, media resources like images, sprites, etc...
REM will be lost and will result in your program crashing if you trying use them.
REM In windowed or exclusive fullscreen mode, the ctrl-alt-del key combo will
REM cause a device lost state.
REM In exclusive fullscreen mode, the alt-tab and windows keys will cause a device lost state also.
sync on
sync rate 60
autocam off
GLOBAL MyLostDeviceCheck_Image as INTEGER
MyLostDeviceCheck_Image = 1
GLOBAL My3DObject_01 as INTEGER
My3DObject_01 = 1
GLOBAL MyMainCamera as INTEGER
MyMainCamera = 1
GLOBAL App_WindowModeOn as INTEGER
App_WindowModeOn=1 ` 0=off, 1=on
GLOBAL User_Notice as INTEGER
User_Notice=0
GLOBAL SpaceBar_Key as INTEGER
SpaceBar_Key=0
GLOBAL ExampleStage as INTEGER
ExampleStage = 0
rem Setup Window
if App_WindowModeOn=0
Set Display Mode DESKTOP WIDTH(),DESKTOP HEIGHT(),0
set window off
else
Set Display Mode 800,600,0
endif
rem Load/Create Needed Media
Load_Needed_Media()
DO
REM ### Force Reload If Lost Device ###
CheckForDeviceLost()
REM Do Stage 0 stuff here
if ExampleStage = 0
REM Rotate Sphere
Rotate Object My3DObject_01, 0, RotateOnY#, 0
inc RotateOnY#
RotateOnY#=wrapvalue(RotateOnY#)
text 0,0,"Device Lost Test | FPS:"+str$(screen fps())
paste image MyLostDeviceCheck_Image, 10, screen height()-270
endif
SYNC
LOOP
Function CheckForDeviceLost()
REM Use just one image throughout your program's life to test for device lost.
REM Dont need to use it for anything else.
if image exist(MyLostDeviceCheck_Image)=0
if App_WindowModeOn=0 ` just for FULLSCREEN MODE
SYNC SLEEP 1 ` Sync sleep to be nice to the cpu
MINIMIZE WINDOW
While Screen Invalid()=0
sync
EndWhile
RESTORE WINDOW
SYNC SLEEP 0
endif
While User_Notice=0
Text 0,0,"NOTICE: A Device Reset has occurred. Press the SPACEBAR to conitinue..."
if SPACEKEY()=1 and SpaceBar_Key=0
SpaceBar_Key=1
endif
if SPACEKEY()=0 and SpaceBar_Key=1
SpaceBar_Key=0
User_Notice=1
endif
sync
EndWhile
User_Notice=0
` Reload/recreate all
Load_Needed_Media()
endif
EndFunction
REM Load_Needed_Media() can also be use when you need new media like when you change from level to level for an example.
REM Ofcoarse when you need to load new media, don't forget to delete the media you no longer need! = Memory Management.
Function Load_Needed_Media()
REM Remake Camera
if camera exist(MyMainCamera)=1
Delete Camera MyMainCamera
endif
make camera MyMainCamera
move camera MyMainCamera,-30
color backdrop MyMainCamera,rgb(0,68,128)
REM For example, ExampleStage = 0 could represent a menu stage where you
REM could just load in menu media (ie: GFX, sounds, etc... )
if ExampleStage = 0
REM Load/Create All IMAGES
if image exist(MyLostDeviceCheck_Image)
delete image MyLostDeviceCheck_Image
endif
Create Bitmap 1,256,256
cls
box 0,0,255,255,rgb(255,0,0),rgb(0,0,255),rgb(255,255,0),rgb(0,255,0)
ink 0,0
line 0,0,255,0
line 0,255,255,255
line 0,0,0,255
line 255,0,255,255
ink rgb(255,255,255),0 ` Set Color Back to White
get image MyLostDeviceCheck_Image,0,0,256,256,1
Set Current Bitmap 0 ` Set Drawing operations back to Screen
REM Load/Create ALL 3D OBJECTS
if object exist(My3DObject_01)=1
Delete Object My3DObject_01
endif
make object sphere My3DObject_01,5,8,8
texture object My3DObject_01,MyLostDeviceCheck_Image
endif
REM If ExampleStage = 1
REM Could load level scene to start playing in
REM ie:
REM If LEVEL=1
REM ... Load level 1 media
REM IF LEVEL=2
REM ... Load level 2 media
REM and so on...
REM Endif
EndFunction