Class cocos.director.Director

pyglet.event.EventDispatcher --+
                               |
                              Director

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.

Methods

pyglet.window.Window init(self, *args, **kwargs)
Initializes the Director creating the main window.
  set_show_FPS(self, value)
  run(self, scene)
Runs a scene, entering in the Director's main loop.
  set_recorder(self, framerate, template='frame-%d.png', duration=None)
Will replace the system clock so that now we can ensure a steady frame rate and save one image per frame
  on_draw(self)
Handles the event 'on_draw' from the pyglet.window.Window
  push(self, scene)
Suspends the execution of the running scene, pushing it on the stack of suspended scenes.
  on_push(self, scene)
  pop(self)
If the scene stack is empty the appication is terminated.
  on_pop(self)
  replace(self, scene)
Replaces the running scene with a new one.
(x,y) get_window_size(self)
Returns the size of the window when it was created, and not the actual size of the window.
(x,y) get_virtual_coordinates(self, x, y)
Transforms coordinates that belongs the real window size, to the coordinates that belongs to the virtual window.
  scaled_resize_window(self, width, height)
One of two possible methods that are called when the main window is resized.
  unscaled_resize_window(self, width, height)
One of two possible methods that are called when the main window is resized.
  set_projection(self)
Sets a 3D projection mantaining the aspect ratio of the original window size
  set_alpha_blending(self, on=True)
Enables/Disables alpha blending in OpenGL using the GL_ONE_MINUS_SRC_ALPHA algorithm.
  set_depth_test(sefl, on=True)
Enables z test.
Inherited from pyglet.event.EventDispatcher: dispatch_event, event, pop_handlers, push_handlers, register_event_type, remove_handler, remove_handlers, set_handler, set_handlers

Properties

  show_FPS
whether or not the FPS are displayed

Instance Variables

  scene_stack
stack of scenes
  scene
scene that is being run
  next_scene
this is the next scene that will be shown
  show_interpreter
whether or not to show the python interpreter
  terminate_app
flag requesting app termination
  window
pyglet's window object

Class Variables

  interpreter_locals = {'cocos': <module 'cocos' from 'D:\cocos_...
a dict with locals for the interactive python interpreter (fill with what you need)
  fps_display = None
  event_types = ['on_push', 'on_pop', 'on_resize', 'on_cocos_res...

Method Details

init

init(self, *args, **kwargs)

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:
do_not_scale : bool
False: on window resizes, cocos will scale the view so that your app don't need to handle resizes. True: your app must include logic to deal with diferent window sizes along the session. Defaults to False
audio_backend : string
one in ['pyglet','sdl']. Defaults to 'pyglet' for legacy support.
audio : dict or None
None or a dict providing parameters for the sdl audio backend. None: in this case a "null" audio system will be used, where all the sdl sound operations will be no-ops. This may be useful if you do not want to depend on SDL_mixer A dictionary with string keys; these are the arguments for setting up the audio output (sample rate and bit-width, channels, buffer size). The key names/values should match the positional arguments of http://www.pygame.org/docs/ref/mixer.html#pygame.mixer.init The default value is {}, which means sound enabled with default settings
fullscreen : bool
Window is created in fullscreen. Default is False
resizable : bool
Window is resizable. Default is False
vsync : bool
Sync with the vertical retrace. Default is True
width : int
Window width size. Default is 640
height : int
Window height size. Default is 480
caption : string
Window title.
visible : bool
Window is visible or not. Default is True.
Returns:
pyglet.window.Window: The main window, an instance of pyglet.window.Window class.

run

run(self, scene)
Runs a scene, entering in the Director's main loop.
Parameters:
scene : Scene
The scene that will be run.

set_recorder

set_recorder(self, framerate, template='frame-%d.png', duration=None)

Will replace the system clock so that now we can ensure a steady frame rate and save one image per frame

:Parameters
framerate: int
the number of frames per second
template: str
the template that will be completed with an in for the name of the files
duration: float
the amount of seconds to record, or 0 for infinite

on_draw

on_draw(self)

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.

push

push(self, scene)
Suspends the execution of the running scene, pushing it on the stack of suspended scenes. The new scene will be executed.
Parameters:
scene : Scene
It is the scene that will be run.

pop

pop(self)
If the scene stack is empty the appication is terminated. Else pops out a scene from the stack and sets as the running one.

replace

replace(self, scene)
Replaces the running scene with a new one. The running scene is terminated.
Parameters:
scene : Scene
It is the scene that will be run.

get_window_size

get_window_size(self)

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.

Returns:
(x,y): The size of the window when it was created

get_virtual_coordinates

get_virtual_coordinates(self, x, y)

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.

Returns:
(x,y): Transformed coordinates from the real window to the virtual window

scaled_resize_window

scaled_resize_window(self, width, height)

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:
width : Integer
New width
height : Integer
New height

unscaled_resize_window

unscaled_resize_window(self, width, height)

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:
width : Integer
New width
height : Integer
New height

set_alpha_blending

set_alpha_blending(self, on=True)
Enables/Disables alpha blending in OpenGL using the GL_ONE_MINUS_SRC_ALPHA algorithm. On by default.

set_depth_test

set_depth_test(sefl, on=True)
Enables z test. On by default

Property Details

show_FPS

whether or not the FPS are displayed

Class Variable Details

interpreter_locals

a dict with locals for the interactive python interpreter (fill with what you need)
Value:
{'cocos': <module 'cocos' from 'D:\cocos_pristine\cocos_trunk\cocos\__\
init__.py'>,
 'director': <cocos.director.Director object at 0x01C26790>}

event_types

Value:
['on_push', 'on_pop', 'on_resize', 'on_cocos_resize']