Of course, game creators are always trying to immerse their customers into the colorful and fantastic worlds they are creating. This is why most games act quite selfish and take up the whole screen to present themselves in all their "awesome glory".
While this is true and understandable, some players just don't like this behavior. Maybe they are reading their email while the game is running. Maybe they want to be able to quickly minimize the game window in case their boss walks up behind them to check if they are busily working. Or maybe a developer just wants to switch from fullscreen to windowed mode to check the debug output window without having to tab through the window choices.
Whatever the reasons might be, PC games should be able to switch between windowed and fullscreen modes to accommodate the needs of players. So this recipe will show you how to achieve this with Panda3D.
Follow the steps of Setting up the game structure found in Chapter 1, Setting Up Panda3D and Configuring Development Tools before you go on with this recipe.
Let's create a sample program for switching between window and fullscreen modes:
Application.py
and insert this code:from direct.showbase.ShowBase import ShowBase from direct.interval.IntervalGlobal import * from direct.gui.OnscreenText import OnscreenText from panda3d.core import * class Application(ShowBase): def __init__(self): ShowBase.__init__(self) self.status = OnscreenText("Window Mode") toggle = Sequence(Wait(3), Func(self.status.setText, "Switching to Fullscreen Mode"), Wait(2), Func(self.toggleFullscreen, 1280, 800, 0, 0, 1), Wait(3), Func(self.status.setText, "Switching to Window Mode"), Wait(2), Func(self.toggleFullscreen, 800, 600, 50, 50, 0)) toggle.start() def toggleFullscreen(self, width, height, posX, posY, full): winProps = WindowProperties() winProps.setOrigin(posX, posY) winProps.setSize(width, height) winProps.setFullscreen(full) self.win.requestProperties(winProps) if full: self.status.setText("Fullscreen Mode") else: self.status.setText("Window Mode");
While in our application class' constructor we set up a sequence that makes our program switch to fullscreen and back again to windowed mode. The interesting part in this code is the toggleFullscreen()
method.
Here, the current properties of the game window are retrieved and modified. We pass the posX
and posY
parameters to setOrigin()
to set the window's position. Then the window size is set using the values stored in width
and height
. For fullscreen mode, this describes the screen resolution that the application will switch to. Finally, we set the fullscreen flag of the window properties object and request our newly created screen mode from Panda3D's window handling system.
Panda3D handles errors quite transparently. If a given resolution is not supported, the engine will try to switch to the closest matching resolution. To check for errors manually we can also use the getRejectedProperties()
of the window object after calling requestProperties()
. This will retrieve a WindowProperties
object containing the properties that could not be changed.