OpenScreen() Alt-Tab/Windows Key Demo

Share your advanced PureBasic knowledge/code with the community.
AndyMK
Enthusiast
Enthusiast
Posts: 582
Joined: Wed Jul 12, 2006 4:38 pm
Location: UK

OpenScreen() Alt-Tab/Windows Key Demo

Post by AndyMK »

Nothing fancy and works with the debugger on! Also closes from the taskbar.

Code: Select all

;******************************************************************************************************
;* ALT-TAB IsScreenActive() demo
;* By djes (djes@free.fr) 03/24/2009
;* Modified by AndyMK 14/09/2010
;* PB 4.51
;******************************************************************************************************

#STARS_NB = 100

Structure pos
  x.l
  y.l
  speed.l
EndStructure

Dim stars.pos(#STARS_NB)

Desktop = ExamineDesktops()
Global Width = DesktopWidth(0)
Global Height = DesktopHeight(0)

Procedure create_sprites()
  
  CreateSprite(0, 64, 64)
  StartDrawing(SpriteOutput(0))
  Circle(32, 32, 31, RGB($FF, $FF, $FF))
  StopDrawing()
  
EndProcedure

Procedure init_display()
  
  OpenScreen(Width, Height, 32, "", #PB_Screen_SmartSynchronization)
  ClearScreen(0)
  
EndProcedure

Procedure check_lostfocus()
  
  If IsScreenActive() = 0
    FreeSprite(0)
    OpenWindow (0,1,1,1,1, "Alt-Tab Demo" , #PB_Window_Minimize|#PB_Window_BorderLess )
    CloseScreen()
    Repeat
      Event=WaitWindowEvent () 
      If Event = #WM_CLOSE
        ExitProg = 1
        Break
      EndIf
    Until Event = #WM_PAINT
    
    If ExitProg = 0
      CloseWindow (0)
      init_display()
      create_sprites()
    Else
      End
    EndIf
  EndIf
  
EndProcedure

InitSprite()
init_display()
create_sprites()

For i=0 To #STARS_NB-1
  stars(i)\x = Random(Width-1)
  stars(i)\y = Random(767)
  stars(i)\speed = Random(16) + 1
Next i

Repeat
  
  FlipBuffers()
  ClearScreen(0)
  check_lostfocus()
  
  For i = 0 To #STARS_NB-1
    stars(i)\x + stars(i)\speed
    If stars(i)\x > Width-1
      stars(i)\x - Width - 64
    EndIf
  Next i
  
  For i = 0 To #STARS_NB-1
    DisplayTranslucentSprite(0, stars(i)\x, stars(i)\y, stars(i)\speed<<4)
  Next i
  
Until GetAsyncKeyState_(#VK_ESCAPE)

CloseScreen()

End
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: OpenScreen() Alt-Tab/Windows Key Demo

Post by Olliv »

Hello AndyMK,

Your idea is very good. My wish is Xplatform. So, there is a performance mistake in your source:
you nest the test insured by IsScreenActive() within a procedure, what is a useless waste of time if it not necessary. And it is not necessary. The coder must keep the availabilities adding or removed such a short and quick test.

This type...

Code: Select all

If IsScreenActive()
; Back processus
EndIf
...inside the main graphic loop is a quicker and simpler way.

I suggest what it follows below. This has to be crossplatform.

Code: Select all

 ;______________________________________________________
; Plein ecran : Gerer le changement de tache
;______________________________________________________
Declare OnFullScreen(ScreenTitle.S,
ScreenFlipMode.I = #PB_Screen_WaitSynchronization)
;______________________________________________________
Declare FullScreenResume(ScreenTitle.S,
ScreenFlipMode.I = #PB_Screen_WaitSynchronization,
*StandBy = 0,
DelayDuration.I = 16)
;______________________________________________________
;______________________________________________________
; TEST
InitSprite()
OnFullScreen("test") ; <<<< ICI
InitKeyboard()
Repeat
ClearScreen(0)
Delay(16)
FlipBuffers()
If IsScreenActive() = 0
FullScreenResume("TEST") ; <<<< ICI
EndIf
ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)
End
;______________________________________________________
;______________________________________________________
Procedure OnFullScreen(ScreenTitle.S,
ScreenFlipMode.I = #PB_Screen_WaitSynchronization)
ExamineDesktops()
OpenScreen(DesktopWidth(0),
DesktopHeight(0),
DesktopDepth(0),
ScreenTitle,
ScreenFlipMode,
DesktopFrequency(0) )
EndProcedure
;______________________________________________________
Procedure FullScreenResume(ScreenTitle.S,
ScreenFlipMode.I = #PB_Screen_WaitSynchronization,
*StandBy = 0,
DelayDuration.I = 16)
Protected BackgroundWindow.I
CloseScreen()
BackgroundWindow = OpenWindow(#PB_Any,
0,
0,
0,
0,
ScreenTitle,
#PB_Window_BorderLess |
#PB_Window_NoActivate)
Repeat
Delay(DelayDuration)
If *StandBy
CallFunctionFast(*StandBy)
EndIf
Until WindowEvent() = #PB_Event_ActivateWindow
CloseWindow(BackgroundWindow)
OnFullScreen(ScreenTitle, ScreenFlipMode)
EndProcedure
;______________________________________________________ 
Post Reply