Esempio n. 1
0
    def 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 parameters; a full
        list is available at pyglet Window API Reference at
        http://pyglet.org/doc/api/pyglet.window.Window-class.html

        :Parameters:
            `autoscale` : bool
                True: on window resizes, cocos will scale the view so that your
                app don't need to handle resizes.
                False: your app must include logic to deal with different window
                sizes along the session.
                Defaults to False
            `do_not_scale` : bool
                Deprecated. The logical negation of autoscale
            `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.

        :rtype: pyglet.window.Window
        :returns: The main window, an instance of pyglet.window.Window class.
        """
        #: callable that would provide the object to calculate and eventually draw fps stats
        self.fps_display_provider = cocos.fps.get_default_fpsdisplay

        #: whether or not the FPS are displayed
        self.show_FPS = False

        #: stack of scenes
        self.scene_stack = []

        #: scene that is being run
        self.scene = None

        #: this is the next scene that will be shown
        self.next_scene = None

        # python interpreter
        self.python_interpreter = None

        #: whether or not to show the python interpreter
        self.show_interpreter = False

        #: flag requesting app termination
        self.terminate_app = False

        # pop out the Cocos-specific flags

        # 'autoscale' / 'do_not_scale' - Scheduled for cleanup at v0.7
        if 'do_not_scale' in kwargs:
            warnings.warn("'do_not_scale' kw-param in director.init is deprecated, use 'autoscale'")
            if 'autoscale' in kwargs:
                warnings.warn("Conflict between deprecated 'do_not_scale' and 'autoscale', " +
                              "'autoscale' wins")
                self.autoscale = kwargs.pop('autoscale')
            else:
                self.autoscale = not kwargs.pop('do_not_scale')
        else:
            self.autoscale = kwargs.pop('autoscale', True)

        def _get_do_not_scale_window():
            warnings.warn('Access to deprecated director.do_not_scale_window')
            return not self.autoscale

        def _set_do_not_scale_window(v):
            warnings.warn('Access to deprecated director.do_not_scale_window')
            self.autoscale = not v
        do_not_scale_window = property(_get_do_not_scale_window, _set_do_not_scale_window)

        audio_backend = kwargs.pop('audio_backend', 'pyglet')
        audio_settings = kwargs.pop('audio', {})

        # handle pyglet 1.1.x vs 1.2dev differences in fullscreen
        self._window_virtual_width = kwargs.get('width', None)
        self._window_virtual_height = kwargs.get('height', None)
        if pyglet.version.startswith('1.1') and kwargs.get('fullscreen', False):
            # pyglet 1.1.x dont allow fullscreen with explicit width or height
            kwargs.pop('width', 0)
            kwargs.pop('height', 0)

        #: pyglet's window object
        self.window = window.Window(*args, **kwargs)

        # complete the viewport geometry info, both virtual and real,
        # also set the appropriate on_resize handler
        if self._window_virtual_width is None:
            self._window_virtual_width = self.window.width
        if self._window_virtual_height is None:
            self._window_virtual_height = self.window.height

        self._window_virtual_aspect = (
            self._window_virtual_width / float(self._window_virtual_height))

        self._offset_x = 0
        self._offset_y = 0

        if self.autoscale:
            resize_handler = self.scaled_resize_window
            self.set_projection = self.set_projection3D
        else:
            resize_handler = self.unscaled_resize_window
            self.set_projection = self.set_projection2D
        # the offsets and size for the viewport will be proper after this
        self._resize_no_events = True
        resize_handler(self.window.width, self.window.height)
        self._resize_no_events = False

        self.window.push_handlers(on_resize=resize_handler)

        self.window.push_handlers(self.on_draw)

        # opengl settings
        self.set_alpha_blending()

        # default handler
        self.window.push_handlers(DefaultHandler())

        # Environment variable COCOS2d_NOSOUND=1 overrides audio settings
        if getenv('COCOS2D_NOSOUND', None) == '1' or audio_backend == 'pyglet':
            audio_settings = None
        # if audio is not working, better to not work at all. Except if
        # explicitly instructed to continue
        if not cocos.audio._working and audio_settings is not None:
            from cocos.audio.exceptions import NoAudioError
            msg = "cocos.audio isn't able to work without needed dependencies. " \
                  "Try installing pygame for fixing it, or forcing no audio " \
                  "mode by calling director.init with audio=None, or setting the " \
                  "COCOS2D_NOSOUND=1 variable in your env."
            raise NoAudioError(msg)

        # Audio setup:
        # TODO: reshape audio to not screw unittests
        import os
        if not os.environ.get('cocos_utest', False):
            cocos.audio.initialize(audio_settings)

        return self.window
Esempio n. 2
0
    def init(self, *args, **kwargs):
        """Initializes the Director creating the main window.
        Keyword arguments are passed to pyglet.window.Window().

        All the valid arguments can be found here:

            - http://www.pyglet.org/doc/1.1/api/pyglet.window.Window-class.html

        :rtype: pyglet.window.Window
        :returns: The main window, an instance of pyglet.window.Window class.
        """

        # pop out the Cocos-specific flags
        do_not_scale_window = kwargs.pop('do_not_scale', False)
        audio_settings = kwargs.pop('audio', {})

        # Environment variable COCOS2d_NOSOUND=1 overrides audio settings
        if getenv('COCOS2D_NOSOUND', None) == '1':
            audio_settings = None
        # if audio is not working, better to not work at all. Except if
        # explicitely instructed to continue
        if not cocos.audio._working and audio_settings is not None:
            from cocos.audio.exceptions import NoAudioError
            msg = "cocos.audio isn't able to work without needed dependencies. " \
                  "Try installing pygame for fixing it, or forcing no audio " \
                  "mode by calling director.init with audio=None, or setting the " \
                  "COCOS2D_NOSOUND=1 variable in your env."
            raise NoAudioError(msg)

        #: pyglet's window object
        self.window = window.Window(*args, **kwargs)

        #: whether or not the FPS are displayed
        self.show_FPS = False

        #: stack of scenes
        self.scene_stack = []

        #: scene that is being run
        self.scene = None

        #: this is the next scene that will be shown
        self.next_scene = None

        # save resolution and aspect for resize / fullscreen
        if do_not_scale_window:
            self.window.push_handlers(on_resize=self.unscaled_resize_window)
        else:
            self.window.push_handlers(on_resize=self.scaled_resize_window)
        self.window.push_handlers(self.on_draw)
        self._window_original_width = self.window.width
        self._window_original_height = self.window.height
        self._window_aspect = self.window.width / float(self.window.height)
        self._offset_x = 0
        self._offset_y = 0

        # opengl settings
        self.set_alpha_blending()

        # python interpreter
        self.python_interpreter = None

        #: whether or not to show the python interpreter
        self.show_interpreter = False

        # default handler
        self.window.push_handlers(DefaultHandler())

        # Audio setup:
        cocos.audio.initialize(audio_settings)

        return self.window