cocos.director.director is the singleton that creates and handles the main Window and manages the logic behind the Scenes.
The first thing to do, is to initialize the director:
from cocos.director import director
director.init( parameters )
This will initialize the director, and will create a display area (a 640x480 window by default). The parameters that are supported by director.init() are the same parameters that are supported by pyglet.window.Window(), plus a few cocos exclusive ones. They are all named parameters (kwargs). See Director.init() for details.
Example:
director.init( width=800, height=600, caption="Hello World", fullscreen=True )
Once you have initialized the director, you can run your first Scene:
director.run( Scene( MyLayer() ) )
This will run a scene that has only 1 layer: MyLayer(). You can run a scene that has multiple layers. For more information about Layers and Scenes refer to the Layers and Scene documentation.
Once a scene is running you can do the following actions:
- director.replace( new_scene ):
Replaces the running scene with the new_scene You could also use a transition. For example: director.replace( SplitRowsTransition( new_scene, duration=2 ) )
- director.push( new_scene ):
The running scene will be pushed to a queue of scenes to run, and new_scene will be executed.
- director.pop():
Will pop out a scene from the queue, and it will replace the running scene.
- director.scene.end( end_value ):
Finishes the current scene with an end value of end_value. The next scene to be run will be popped from the queue.
Other functions you can use are:
- director.get_window_size(): Returns an (x,y) pair with the _logical_ dimensions of the display. The display might have been resized, but coordinates are always relative to this size. If you need the _physical_ dimensions, check the dimensions of director.window
- get_virtual_coordinates(self, x, y): Transforms coordinates that belongs the real (physical) window size, to the coordinates that belongs to the virtual (logical) window. Returns an x,y pair in logical coordinates.
The director also has some useful attributes:
- director.return_value: The value returned by the last scene that called director.scene.end. This is useful to use scenes somewhat like function calls: you push a scene to call it, and check the return value when the director returns control to you.
- director.window: This is the pyglet window handled by this director, if you happen to need low level access to it.
- self.show_FPS: You can set this to a boolean value to enable, disable the framerate indicator.
- self.scene: The scene currently active
Bases: pyglet.event.EventDispatcher
Class that creates and handle the main Window and manages how and when to execute the Scenes
You should not directly instantiate the class, instead you do:
from cocos.director import director
to access the only one Director instance.
Transforms coordinates that belongs the real window size, to the coordinates that belongs to the virtual window.
For example, if you created a window of 640x480, and it was resized to 640x1000, then if you move your mouse over that window, it will return the coordinates that belongs to the newly resized window. Probably you are not interested in those coordinates, but in the coordinates that belongs to your virtual window.
Return type: | (x,y) |
---|---|
Returns: | Transformed coordinates from the real window to the virtual window |
Returns the size of the window when it was created, and not the actual size of the window.
Usually you don’t want to know the current window size, because the Director() hides the complexity of rescaling your objects when the Window is resized or if the window is made fullscreen.
If you created a window of 640x480, the you should continue to place your objects in a 640x480 world, no matter if your window is resized or not. Director will do the magic for you.
Return type: | (x,y) |
---|---|
Returns: | The size of the window when it was created |
Initializes the Director creating the main window.
There are a few cocos exclusive parameters, the rest are the standard pyglet parameters for pyglet.window.Window.__init__ This docstring only partially list the pyglet parameteres; a full list is available at pyglet Window API Reference at http://pyglet.org/doc/api/pyglet.window.Window-class.html
Parameters: |
|
---|---|
Return type: | pyglet.window.Window |
Returns: | The main window, an instance of pyglet.window.Window class. |
Handles the event ‘on_draw’ from the pyglet.window.Window
Realizes switch to other scene and app termination if needed Clears the window area The windows is painted as:
- Render the current scene by calling it’s visit method
- Eventualy draw the fps metter
- Eventually draw the interpreter
When the window is minimized any pending switch to scene will be delayed to the next de-minimizing time.
If the scene stack is empty the appication is terminated. Else pops out a scene from the stack and sets as the running one.
Suspends the execution of the running scene, pushing it on the stack of suspended scenes. The new scene will be executed.
Parameters: |
|
---|
Replaces the running scene with a new one. The running scene is terminated.
Parameters: |
|
---|
Runs a scene, entering in the Director’s main loop.
Parameters: |
|
---|
One of two possible methods that are called when the main window is resized.
This implementation scales the display such that the initial resolution requested by the programmer (the “logical” resolution) is always retained and the content scaled to fit the physical display.
This implementation also sets up a 3D projection for compatibility with the largest set of Cocos transforms.
The other implementation is unscaled_resize_window.
Parameters: |
|
---|
Enables/Disables alpha blending in OpenGL using the GL_ONE_MINUS_SRC_ALPHA algorithm. On by default.
Enables z test. On by default
placeholder, will be set to one of set_projection2D or set_projection3D when director.init is called
Sets a 2D projection (ortho) covering all the window
Sets a 3D projection mantaining the aspect ratio of the original window size
Will replace the app clock so that now we can ensure a steady frame rate and save one image per frame
One of two possible methods that are called when the main window is resized.
This implementation does not scale the display but rather forces the logical resolution to match the physical one.
This implementation sets up a 2D projection, resulting in the best pixel alignment possible. This is good for text and other detailed 2d graphics rendering.
The other implementation is scaled_resize_window.
Parameters: |
|
---|
a dict with locals for the interactive python interpreter (fill with what you need)