Code archives/Miscellaneous/screensaver code

This code has been declared by its author to be Public Domain code.

Download source code

screensaver code by mindstorms
(Posted 19 years ago)
Uses win api to allow blitz screensavers to have a preview window.

Use this with another file, just import it.

Requirements:
-User32 Decls using api_ naming convention- can be found here http://www.blitzbasic.com/codearcs/codearcs.php?code=1179
; ID: 2093
; Author: mindstorms
; Date: 2007-08-17 02:47:17
; Title: screensaver code
; Description: preview window works!

Global title$ = ""
Const GWL_STYLE	= -16
Const GWL_HWNDPARENT = -8
Const WS_CHILD = $40000000
Const WM_DESTROY                = $0002
Const WM_CLOSE                  = $0010
Const WM_KEYDOWN                = $0100
Const WM_SYSKEYDOWN             = $0104
Const WM_MOUSEMOVE              = $0200
Const WM_LBUTTONDOWN            = $0201
Const WM_RBUTTONDOWN            = $0204
Const WM_MBUTTONDOWN            = $0207
Const WS_VISIBLE          		= $10000000

Global g_width,g_height,hParent
Global w_message.MSG = New MSG
Global orig_pt.POINT = New POINT

Type tRECT
	Field x, y, w, h
End Type

Type MSG
	Field hwnd
	Field message
	Field wParam
	Field lParam
	Field time$
	Field pt.POINT
End Type

Type POINT
	Field x
	Field y
End Type

;call to start the screensaver
Function main(mtitle$="base screensaver")
	title = mtitle 
	AppTitle title
	If CommandLine$() <> "" Then                                ; If Parameter is present then 
		If Upper(Left$(CommandLine$(),2)) = "/S" Then Start()     ;  or Screensaver itself should be started
		If Upper(Left$(CommandLine$(),2)) = "/P" Then Start()		;or preview window  
	EndIf
	;I have noticed that sometimes windows will not give /C...it will give nothing.  
	;It does not give nothing otherwise, so this works (make sure start ends...)
	Configure() 
End Function

;********************************User Overwrites these functions********************************************
;use the function name without the _, these just examples
Function Configure_() ;for settings button
	;put your configure screen here
	End
End Function   

Function ExitScreensaver_()
	End
End Function

Function ScreenSaverMainLoop_()
	Repeat 		;main loop
		ClsColor(255,0,0)
		Cls
		eventHandler()				;must call this at least once each loop...
		;screensaver code here
		Flip 
		Delay 1	
	Forever
End Function
;*************************************************************************************************************



;***************************************internal functions****************************************************
Function Start()	;actual screen saver
	Local c$ = CommandLine()
	hParent = Int(Right(c,Len(c)-Instr(c," ")))
	blitz_hnd = api_FindWindow("Blitz Runtime Class", title$)
	;if there is a parent waiting...
	If api_IsWindow(hParent)
		Local r.tRECT = New tRECT
		api_GetClientRect(hParent,r)
		g_width = r\w
		g_height = r\h
		Graphics3D g_width,g_height,0,2
		api_SetWindowLong(blitz_hnd, GWL_STYLE, WS_CHILD)
		api_SetParent(blitz_hnd,hParent)
		api_MoveWindow(blitz_hnd,r\x,r\y,g_width,g_height,1)
		
	;otherwise regular screensaver stuff
	Else
		hParent = 0
		g_width = api_GetSystemMetrics(0)
		g_height = api_GetSystemMetrics(1)
		Graphics3D g_width, g_height, 0, 2
		api_SetWindowLong(blitz_hnd, GWL_STYLE, WS_VISIBLE)
		api_MoveWindow(blitz_hnd,0, 0, g_width, g_height, 1)
		
		FlushKeys()                   ; clean keyboardbuffer
		FlushMouse()                  ; clean mousebuffer
		HidePointer()  
		api_GetCursorPos(orig_pt)		;set mouse point
	EndIf
	
	ScreenSaverMainLoop()
End Function

Function eventHandler()
	While api_PeekMessage(w_message,0,0,0,1)
		api_TranslateMessage(w_message)
		
		If hParent Then
			Select w_message\message
				Case WM_DESTROY:
					api_PostQuitMessage(0)
				Case WM_CLOSE:
					ExitScreensaver()
				Default:
					api_DefWindowProc(w_message\hwnd,w_message\message,w_message\wParam,w_message\lParam)		
			End Select	
		Else
			Select w_message\message
				Case WM_DESTROY:
					api_PostQuitMessage(0)
				Case WM_CLOSE:
					ExitScreensaver()
				Case WM_MOUSEMOVE:
					pt.POINT = New POINT
					api_GetCursorPos(pt)
					If Abs(pt\x-orig_pt\x)>10 Or Abs(pt\y-orig_pt\y)>10 Then 
						api_PostMessage(w_message\hwnd,WM_CLOSE,0,0)
					EndIf
				Case WM_LBUTTONDOWN, WM_RBUTTONDOWN, WM_MBUTTONDOWN, WM_KEYDOWN, WM_SYSKEYDOWN:
					api_PostMessage(w_message\hwnd,WM_CLOSE,0,0) 
				Default:
					api_DefWindowProc(w_message\hwnd,w_message\message,w_message\lParam,w_message\wParam)						
			End Select	
		EndIf
	Wend
	Return True
End Function

Comments

mindstorms
(Posted 19 years ago)
Here is an example using the flying birds code from Blitz:
Include "base screensaver.bb"

ChangeDir SystemProperty$("appdir")

Include "Textures\KBSplines.bb"

Global gwidth,gheight
Const FPS = 30
Global fstep


Function Configure() ;for settings button
	;put your configure screen here
	api_MessageBox(0, "Here could be the screensaver configuration. But as it is a very simple screensaver, it does not need any config", "Blitz Screensaver Configuaration", 0)
	End
End Function   

Function ExitScreensaver()
	End
End Function

Function ScreenSaverMainLoop()
	;init stuff
	gwidth = g_width
	gheight = g_height
	
	period=1000/FPS
	time=MilliSecs()-period
	
	cmot.Motion = New motion
	b2mot.Motion = New Motion
	b1mot.Motion = New Motion
	
	If Load_Motion( "Textures\Cam.bbm", cmot ) = False Then RuntimeError "Error loading file" : End 
	If Load_Motion( "Textures\Bird1.bbm", b1mot ) = False Then RuntimeError "Error loading file" : End 
	If Load_Motion( "Textures\Bird2.bbm", b2mot ) = False Then RuntimeError "Error loading file" : End 
	
	camera=CreateCamera()
	CameraRange camera,1,3000
	
	AmbientLight 90,90,90
	light_sun = CreateLight(1)
	LightColor light_sun,200,200,100
	RotateEntity light_sun,60,-90,0
	
	mesh_canyon = LoadMesh( "Textures\Canyon.x" )
	mesh_skybox = MakeSkyBox("Textures\sky") 
	mesh_bird = LoadMD2("Textures\Bird.md2")
	tex1 = LoadTexture( "Textures\Bird.bmp" )
	EntityTexture mesh_bird,tex1
	mesh_bird2 = CopyEntity( mesh_bird )
	
	AnimateMD2 mesh_bird,1,2.5,0,31
	AnimateMD2 mesh_bird2,1,2.5,0,31
	
	Apply_Motion(cmot,0,camera)
	Apply_Motion(b1mot,0,mesh_bird)
	Apply_Motion(b2mot,0,mesh_bird2)
	fstep = 1
	
	
	Repeat 		;main loop
	
		Repeat
			elapsed=MilliSecs()-time
		Until elapsed
		
		ticks=elapsed/period
		tween#=Float(elapsed Mod period)/Float(period)
		
		For k=1 To ticks
			time=time+period
			If k=ticks Then CaptureWorld
		
			Apply_Motion(cmot,fstep,camera)
			Apply_Motion(b1mot,fstep,mesh_bird)
			Apply_Motion(b2mot,fstep,mesh_bird2)
			fstep = fstep + 1
			If fstep > cmot\nsteps Then fstep = 1
	
			PositionEntity mesh_skybox,EntityX(camera,1),EntityY(camera,1),EntityZ(camera,1)
	
			UpdateWorld
		
		Next
		RenderWorld tween
		
		eventHandler()				;must call this at least once each loop...
		Flip 
	Forever
End Function

Function MakeSkyBox( file$ )

	m=CreateMesh()
	;front face
	b=LoadBrush( file$+"_FR.bmp",49 )
	s=CreateSurface( m,b )
	AddVertex s,-1,+1,-1,0,0:AddVertex s,+1,+1,-1,1,0
	AddVertex s,+1,-1,-1,1,1:AddVertex s,-1,-1,-1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;right face
	b=LoadBrush( file$+"_LF.bmp",49 )
	s=CreateSurface( m,b )
	AddVertex s,+1,+1,-1,0,0:AddVertex s,+1,+1,+1,1,0
	AddVertex s,+1,-1,+1,1,1:AddVertex s,+1,-1,-1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;back face
	b=LoadBrush( file$+"_BK.bmp",49 )
	s=CreateSurface( m,b )
	AddVertex s,+1,+1,+1,0,0:AddVertex s,-1,+1,+1,1,0
	AddVertex s,-1,-1,+1,1,1:AddVertex s,+1,-1,+1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;left face
	b=LoadBrush( file$+"_RT.bmp",49 )
	s=CreateSurface( m,b )
	AddVertex s,-1,+1,+1,0,0:AddVertex s,-1,+1,-1,1,0
	AddVertex s,-1,-1,-1,1,1:AddVertex s,-1,-1,+1,0,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b
	;top face
	b=LoadBrush( file$+"_UP.bmp",49 )
	s=CreateSurface( m,b )
	AddVertex s,-1,+1,+1,0,1:AddVertex s,+1,+1,+1,0,0
	AddVertex s,+1,+1,-1,1,0:AddVertex s,-1,+1,-1,1,1
	AddTriangle s,0,1,2:AddTriangle s,0,2,3
	FreeBrush b

	ScaleMesh m,1700,1700,1700
	FlipMesh m
	EntityFX m,1
	Return m
	
End Function

main("bird screensaver")


It requires all of the bird's media and includes in a folder named texture, as well as being in the same directory as the above code.

mindstorms
(Posted 19 years ago)
Fixed the problem of mouse and keyboard events quiting the preview mode. See above.

chi
(Posted 19 years ago)
thx mindstorms! working very well here...

cheers, chi

Boiled Sweets
(Posted 19 years ago)
@mindstorm,

very nice, but if you could get rid of the extra windows it would be usable :-)

mindstorms
(Posted 19 years ago)
I have figured out where the extra windows come from, but am unsure as to how to "stop" them. As usual, there is the standard blitz window that opens in the middle, but afterwards there is a small window resulting from the Graphics3d call. It lasts only a few seconds because right after that the api commands get called, which move the window and hide the titlebar. Anyone who knows how to fix these windows are free to fix it :)

@BoiledSweets: I do not think that the windows pose that much of a problem, and most users will not even notice the second one...At the moment I would rather have a small window right on startup than no preview window at all.

I removed a redundant call and constant above, they were not needed.

Boiled Sweets
(Posted 19 years ago)
In fact change this...

If api_IsWindow(hParent)
		Local r.tRECT = New tRECT
		api_GetClientRect(hParent,r)
		g_width = r\w
		g_height = r\h
		Graphics3D g_width,g_height,0,2
		api_SetWindowLong(blitz_hnd, GWL_STYLE, WS_CHILD)
		api_SetParent(blitz_hnd,hParent)
		api_MoveWindow(blitz_hnd,r\x,r\y,g_width,g_height,1)
		
	;otherwise regular screensaver stuff


to this...

If api_IsWindow(hParent)
		Local r.tRECT = New tRECT
		api_GetClientRect(hParent,r)
		g_width = r\w
		g_height = r\h
		Graphics3D g_width,g_height,0,2
		api_SetWindowLong(blitz_hnd, GWL_STYLE, WS_CHILD)
		api_SetParent(blitz_hnd,hParent)
		api_MoveWindow(blitz_hnd,r\x,r\y,g_width,g_height,1)
	        api_ShowWindow( SystemProperty( "AppHWND" ), True )	
	;otherwise regular screensaver stuff


and drop the exe onto the util downloadable from here :

http://www.boiledsweets.com/pub/startupfix.rar

And now you have NO horrid little windows appearing when it runs in the preview window!

This is SO awesome.

Code Archives Forum