Screen Saver example

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Screen Saver example

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

Code: Select all

; (c) 2001 - Franco's template - absolutely freeware
; PureBasic screensaver
;

Procedure OnlyOneSCRStart(Name$)
  Shared OnlyOneStartMutex.l
  OnlyOneStartMutex=CreateMutex_(0,1,Name$)
  OnlyOneStartError.l=GetLastError_()
  If OnlyOneStartMutex0 And OnlyOneStartError=0 : ProcedureReturn 1
  Else : CloseHandle_(OnlyOneStartMutex) : End
  EndIf
EndProcedure

Procedure OnlyOneSCRStop()
  CloseHandle_(OnlyOneStartMutex)
EndProcedure


Procedure RunScreenSaver(ScreenSaverName$)
  If InitSprite() = 0
    MessageRequester("Error", "Can't open DirectX 7 Or later", 0)
    End
  EndIf
  If InitKeyboard() = 0
    MessageRequester("Error", "Can't open DirectX 7 Or later",0)
    End
  EndIf
  
  If OpenScreen(GetSystemMetrics_(#SM_CXSCREEN), GetSystemMetrics_(#SM_CYSCREEN), 32, ScreenSaverName$)
    
    If LoadSprite(0, "PureBasic.bmp", 0) = 0
      MessageRequester("Error", "Can't open a sprite !", 0)
    EndIf
  Else
    MessageRequester("Error", "Can't open a screen !", 0)
  EndIf
EndProcedure


; MAIN PROGRAM
ScreenSaverName$="PureBasic ScreenSaver"
OnlyOneSCRStart(ScreenSaverName$)

;only the first 2 chars are interesting sometimes you have more as you see below
Parameter$ = Left(ProgramParameter(),2)


; normal start of a screensaver
If Parameter$ = "/s"
  RunScreenSaver(ScreenSaverName$)
  Run = 10
  XMax = (GetSystemMetrics_(#SM_CXSCREEN)/2)-(SpriteWidth(0)/2)
  YMax = (GetSystemMetrics_(#SM_CYSCREEN)/2)-(SpriteHeight(0)/2)
  
  While Run > 0
    FlipBuffers()
    ClearScreen(0,0,0)
    ; all sprites moves syncron
    DisplaySprite(0, XMax+x, YMax+x)
    DisplaySprite(0, XMax-x, YMax-x)
    DisplaySprite(0, XMax+x, YMax-x)
    DisplaySprite(0, XMax-x, YMax+x)
    DisplaySprite(0, XMax+x, YMax)
    DisplaySprite(0, XMax-x, YMax)
    DisplaySprite(0, XMax, YMax+x)
    DisplaySprite(0, XMax, YMax-x)
    
    ; with the following code the srites will return to start and restart new
    If x < XMax And Pos = 0 : x+1 : Pos = 0 : EndIf
    If x = XMax And Pos = 0 : x-1 : Pos = 1 : EndIf
    If x < XMax And Pos = 1 : x-1 : Pos = 1 : EndIf
    If x = 0 And Pos = 1 : x+1 : Pos = 0 : EndIf
    ;-----------------------------------------------
    
    If GetInputState_() : Run = Run-10 : EndIf
    If GetQueueStatus_(#QS_MOUSEMOVE) : Run = Run-1 : EndIf ;because of WinXp works under Win98 as well
    Delay(10)
  Wend
EndIf 

; "/c:1340" under w98 don't know if it is always: ":1340" <- what is this?
; wenn you press the Settings button
; this is the settings window...
If Parameter$ = "/c"
  If OpenWindow(0, 200, 200, 320,240, #PB_Window_SystemMenu ,ScreenSaverName$)
    Repeat
      EventID.l = WaitWindowEvent()
    Until EventID = #PB_EventCloseWindow
  EndIf
EndIf

; "/p" when you mark the screensaver in the screensaver list
; and you have to show up something in the preview screen
; or if you leave the settings or the preview mode
If Parameter$ = "/p"
  
EndIf


OnlyOneSCRStop()
End

Have a nice day...
Franco


Edited by - franco on 10 December 2001 16:46:52