opensource screensaver code

BlitzMax Forums/BlitzMax Programming/opensource screensaver code

netmaestro(Posted 2005) [#1]
The code in this program will serve as boilerplate to create a fully functional screen saver. It will run in the preview window, full screen or config. All you have to do is put your own presentation in the main loop. I deliberately kept the presentation part of the code to a bare minimum to keep focus on the boilerplate, which is written entirely in Bmax with no reliance on C++ or dlls. I present this sourcecode as a gift to the folks on the forum, and I point out that it is the evolution of a work started by eikon. It was intended at its inception to be open source, and that's what it is.

You can pull this code into your ide, make sure your "create GUI App" is CHECKED, compile it, rename the .exe to .scr and put it in your windows\system32 folder to demo.



'******************** Open Source Screensaver Program ************************
'
' Version 1.0 July 2, 2005
'
' Known issues: None at this time
'
' Contributors: netmaestro, eikon, chris c
'
'
' Use: Compile with "GUI App" CHECKED
' Rename .exe to .scr
' move .scr to c:\windows\system32 (or...)
'
'*****************************************************************************

Framework BRL.GLMax2D
Import BRL.Basic
Import BRL.System
Import BRL.Retro
Import "-lshell32"
Import "-luser32"
Import "-lkernel32"

Extern "win32"
Function FindWindow (x:Byte Ptr, y:Byte Ptr) = "FindWindowA@8"
Function MoveWindow (hWnd,x,y,w,h,d) = "MoveWindow@24"
Function ShowWindow (hWnd,style) = "ShowWindow@8"
Function GetWindowLong (hwnd, nIndex) = "GetWindowLongA@8"
Function SetWindowLong (hWnd,x,y) = "SetWindowLongW@12"
Function SetParent (hWnd1,hWnd2) = "SetParent@8"
Function IsWindowVisible (hWnd) = "IsWindowVisible@4"
Function GetDesktopWindow () = "GetDesktopWindow@0"
Function GetWindowRect (hWnd:Int,r:Byte Ptr) = "GetWindowRect@8"
Function CreateSemaphore (a:Byte Ptr, b:Int, c:Int, d:String) = "CreateSemaphoreW@16"
Function GetLastError () = "GetLastError@0"
EndExtern

Global r:Int[4] '**************** Get Current Desktop Resolution *******************
Global p:Byte Ptr=Varptr(r[0]) '
' This is advisable because it is best not to switch screen
deskWnd=GetDesktopWindow() ' settings when the screen saver starts.
GetWindowRect(deskWnd,p) '
'
Global DeskWidth :Float =r[2] '
Global DeskHeight:Float =r[3] '**************** End Get Current Desktop Resolution ***************

Global width: Float = 0
Global height:Float = 0
Global startmousex:Int = 0
Global startmousey:Int = 0

Const ERROR_ALREADY_EXISTS:Int = 183
Const PREVIEW_ACTIVE = 0
Const CONFIG_ACTIVE = 1
Const SAVER_ACTIVE = 2

Global PrevWnd = Int(AppArgs[2]) 'Handle of the preview window
Global pscale:Float = 1 'scale value to apply depending on screen size
Global bmw:Int = 0 'Handle of the BMax window
Global Status:Int = 0 'Program status, identifies preview, regular, or config modes
Global timemark:Int = 0 'Time marker to use for a makeshift timer

SetGraphicsDriver GLMax2DDriver()

Select AppArgs[1]
Case "/p"
InitPreviewMode()
Case "/s"
InitSaverMode()
Case "/c"
InitConfigMode()
Default
InitConfigMode()
EndSelect

While Not KeyHit(KEY_ESCAPE)

'**************** TODO: Write your own presentation code ***************
'
' Multiply any scaling by pscale (=1 in Saver mode, .15-.25 in Preview)
' You will scale your speeds too (nothing moving in this one)
'
'***********************************************************************
Cls
SetColor 255,255,255
DrawOval 0,0,200,200
SetColor 255,0,0
DrawOval width-(200*pscale),0,200,200
SetColor 0,255,0
DrawOval 0,height-(200*pscale),200,200
SetColor 0,0,255
DrawOval width-(200*pscale),height-(200*pscale),200,200
Flip
FlushMem

Select Status '******* This block is boilerplate ********
Case PREVIEW_ACTIVE '
CheckVisible() '
Case SAVER_ACTIVE '
CheckMovement() '
EndSelect '************* Do Not Remove **************

Wend

End

Function InitSaverMode()
If SetProcessLock("SaverModeLock") <> 0 Then End
Status = SAVER_ACTIVE
width = DeskWidth
height = DeskHeight
Graphics width,height,-1,-1
pscale = 1
startmousex = MouseX()
startmousey = MouseY()
Return
EndFunction

Function InitPreviewMode()
If SetProcessLock("PreviewModeLock") <> 0 Then End
Status = PREVIEW_ACTIVE
width = 152; height = 112
Graphics width,height,0, -1
bmw = FindWindow(Null,"BlitzMax Application")
Local bmx_Style = GetWindowLong(bmw, -16)
bmx_Style = bmx_Style And $40000000 Or $800000 And $10000000
SetWindowLong bmw, -16, bmx_Style;
SetParent(bmw,PrevWnd)
MoveWindow (bmw,0,0,152,112,True)
pscale = 152/DeskWidth
SetScale pscale,pscale
timemark = MilliSecs()
Return
EndFunction

Function InitConfigMode()
Status = CONFIG_ACTIVE
width = 320
height = 200
AppTitle = "Configuration Screen"
Graphics width,height, 0
bmw = FindWindow(Null,"Configuration Screen")
MoveWindow (bmw,(DeskWidth/2-width/2),(DeskHeight/2-height/2),320,200,True)
SetClsColor 192,192,192
SetColor 0,0,0
Cls
DrawText "TODO: Write config code",50,80
DrawText "Press <ANY KEY> to exit",55,130
Flip
WaitKey
End
EndFunction

Function SetProcessLock:Int(LockStr:String)
Global MySem = CreateSemaphore(Null,0,1,LockStr)
If MySem<>0 And GetLastError() = ERROR_ALREADY_EXISTS
Return 1
Else
Return 0
EndIf
EndFunction

Function CheckVisible()
If MilliSecs()-timemark > 2000 'Delay visibility check til window is drawn
If Not IsWindowVisible(PrevWnd)
End
EndIf
EndIf
EndFunction

Function CheckMovement()
If MouseX()<>startmousex Or MouseY()<>startmousey Then End
If GetChar()<>0 Then End
'Probably want to put some more keyhits in here; I'm too lazy
EndFunction


netmaestro(Posted 2005) [#2]
I made a simple implementation of this code by adding some movement to the main loop. Download the example at
http://www.networkmaestro.com/simplesaver.scr
and see it in action. Starting with the boilerplate already made, creating this took about fifteen minutes!


INVENT(Posted 2008) [#3]
any chance you could email that to me.....link is dead...if you still have it..or if anyone has it...