Full Screen or Windowed

BlitzPlus Forums/BlitzPlus Programming/Full Screen or Windowed

I wrote a game with an earlier version of BlitzPlus (earlier than 1.35). I wanted to have a fullscreen mode and a true windowed mode (i.e. with Windows menus, etc). The program involves using a canvas in windowed mode, and using a Graphics window in fullscreen mode. When switching between modes, it destroys the previous interface (graphics or canvas) as it creates the new interface.

It worked pretty well then, however when I try it now, it doesn't work. I've discovered that it's due to the changes that Mark put in regarding Canvases and Graphics -- trying to make BlitzPlus code more compatible with Blitz2D. It doesn't allow a canvas to be shown after calling EndGraphics. (I figure it's a bug, but perhaps it's by some design that I don't understand. If so, please tell me. At any rate I file a bug report.)

Here's the code I used. Note that I added in HotKey events just now, and took out most of the code that doesn't apply to this example. Pay most attention to InitDisplay() and SwapDisplay().

Note also that the drawing (client) area of the two screens are exactly the same. This makes it so that you don't have to change any of your drawing code, which is a nice plus.

Comment out the EndGraphics command to get a glimpse of what the action should be like. (It will just create multiple windows.)

;------------------------------
; CONSTS

Const evKeystroke		= $103
Const evWindowClose 	= $803
Const evTimerTick 		= $4001
Const evSwapDisplay		= $99 ; Custom event for Hotkey F11

Const WNDWIDTH	= 800
Const WNDHEIGHT	= 600

;------------------------------
; GLOBALS

Global WndMain, CanMain, TimerDraw
Global bWindowed = True ; True = windowed mode, False = fullscreen mode

;------------------------------
; MAIN_LOOP()
	
InitGame()
InitDisplay() 

While WaitEvent()
	Select EventID()
		Case evTimerTick : Draw()
		Case evWindowClose : End
		Case evSwapDisplay : SwapDisplay() ; Hotkey F11
	End Select
Wend

;------------------------------
; FUNCTIONS

Function InitGame()
	CreateTimer(60)
	HotKeyEvent 87, 0, evSwapDisplay ; Hotkey F11
	HotKeyEvent 1, 0, evWindowClose ; Hotkey ESC
End Function

Function InitDisplay()
	If bWindowed Then 
		; Set up Windowed mode (having the same client area as Fullscreen window)
		Local x% = (ClientWidth(Desktop()) - WNDWIDTH) / 2
		Local y% = (ClientHeight(Desktop()) - WNDHEIGHT) / 2
		WndMain = CreateWindow("Window (windowed mode)", x, y, WNDWIDTH, WNDHEIGHT, 0, 47)
		CanMain = CreateCanvas(0, 0, ClientWidth(WndMain), ClientHeight(WndMain), WndMain)
		SetGadgetLayout(CanMain, 1, 2, 1, 2)
		SetBuffer CanvasBuffer(CanMain)
	Else 
		; Set up Fullscreen mode
		AppTitle("Window (fullscreen mode)")
		Graphics(WNDWIDTH, WNDHEIGHT)
		SetBuffer(BackBuffer())
	EndIf
End Function

Function SwapDisplay()
	If bWindowed Then 
		; Switch to Fullscreen mode
		bWindowed = False 
		FreeGadget WndMain
	Else
		; Switch to Windowed mode
		bWindowed = True
		EndGraphics ; <--- Has problem in v1.35 -- can't create canvas after this call
	End If
	InitDisplay()
End Function

Function Draw()
	Color Rand(255), Rand(255), Rand(255)
	Rect Rand(WNDWIDTH), Rand(WNDHEIGHT), Rand(WNDWIDTH/2), Rand(WNDHEIGHT/2), True
	If bWindowed Then FlipCanvas CanMain Else Flip
End Function