示例#1
0
    def __init__(self,
                 width: int = 800,
                 height: int = 600,
                 title: str = 'Arcade Window',
                 fullscreen: bool = False,
                 resizable: bool = False,
                 update_rate: Optional[float] = 1 / 60,
                 antialiasing: bool = True,
                 screen: pyglet.canvas.Screen = None):
        """
        Construct a new window

        :param int width: Window width
        :param int height: Window height
        :param str title: Title (appears in title bar)
        :param bool fullscreen: Should this be full screen?
        :param bool resizable: Can the user resize the window?
        :param float update_rate: How frequently to update the window.
        :param bool antialiasing: Should OpenGL's anti-aliasing be enabled?
        """
        if antialiasing:
            config = pyglet.gl.Config(major_version=3,
                                      minor_version=3,
                                      double_buffer=True,
                                      sample_buffers=1,
                                      samples=4)
        else:
            config = pyglet.gl.Config(major_version=3,
                                      minor_version=3,
                                      double_buffer=True)

        try:
            super().__init__(width=width,
                             height=height,
                             caption=title,
                             resizable=resizable,
                             config=config,
                             vsync=False)
            self.register_event_type('update')
            self.register_event_type('on_update')
        except pyglet.window.NoSuchConfigException:
            raise NoOpenGLException(
                "Unable to create an OpenGL 3.3+ context. "
                "Check to make sure your system supports OpenGL 3.3 or higher."
            )

        if antialiasing:
            try:
                gl.glEnable(gl.GL_MULTISAMPLE_ARB)
            except pyglet.gl.GLException:
                print("Warning: Anti-aliasing not supported on this computer.")

        if update_rate:
            from pyglet import compat_platform
            if compat_platform == 'darwin' or compat_platform == 'linux':
                # Set vsync to false, or we'll be limited to a 1/30 sec update rate possibly
                self.context.set_vsync(False)
            self.set_update_rate(update_rate)

        super().set_fullscreen(fullscreen, screen)
        # This used to be necessary on Linux, but no longer appears to be.
        # With Pyglet 2.0+, setting this to false will not allow the screen to
        # update. It does, however, cause flickering if creating a window that
        # isn't derived from the Window class.
        # self.invalid = False
        set_window(self)

        self._current_view: Optional[View] = None
        self.textbox_time = 0.0
        self.key: Optional[int] = None
        self.ui_manager = arcade.experimental.gui.UIManager(self)

        self._ctx: ArcadeContext = ArcadeContext(self)
        set_viewport(0, self.width - 1, 0, self.height - 1)

        self._background_color: Color = (0, 0, 0, 0)

        # Required for transparency
        self._ctx.enable(self.ctx.BLEND)
        self._ctx.blend_func = self.ctx.BLEND_DEFAULT
示例#2
0
    def __init__(
            self,
            width: int = 800,
            height: int = 600,
            title: str = 'Arcade Window',
            fullscreen: bool = False,
            resizable: bool = False,
            update_rate: Optional[float] = 1 / 60,
            antialiasing: bool = True,
            gl_version: Tuple[int, int] = (3, 3),
            screen: pyglet.canvas.Screen = None,
            style: Optional[str] = pyglet.window.Window.WINDOW_STYLE_DEFAULT,
            visible: bool = True,
            vsync: bool = False,
            gc_mode: str = "context_gc",
            center_window: bool = False,
            samples: int = 4,
            enable_polling: bool = True):
        # In certain environments we can't have antialiasing/MSAA enabled.
        # Detect replit environment
        if os.environ.get("REPL_ID"):
            antialiasing = False

        config = None
        # Attempt to make window with antialiasing
        if antialiasing:
            try:
                config = pyglet.gl.Config(
                    major_version=gl_version[0],
                    minor_version=gl_version[1],
                    double_buffer=True,
                    sample_buffers=1,
                    samples=samples,
                )
                display = pyglet.canvas.get_display()
                screen = display.get_default_screen()
                config = screen.get_best_config(config)
            except pyglet.window.NoSuchConfigException:
                LOG.warning(
                    "Skipping antialiasing due missing hardware/driver support"
                )
                config = None
                antialiasing = False
        # If we still don't have a config
        if not config:
            config = pyglet.gl.Config(
                major_version=3,
                minor_version=3,
                double_buffer=True,
            )
        try:
            super().__init__(width=width,
                             height=height,
                             caption=title,
                             resizable=resizable,
                             config=config,
                             vsync=vsync,
                             visible=visible,
                             style=style)
            self.register_event_type('update')
            self.register_event_type('on_update')
        except pyglet.window.NoSuchConfigException:
            raise NoOpenGLException(
                "Unable to create an OpenGL 3.3+ context. "
                "Check to make sure your system supports OpenGL 3.3 or higher."
            )
        if antialiasing:
            try:
                gl.glEnable(gl.GL_MULTISAMPLE_ARB)
            except pyglet.gl.GLException:
                LOG.warning(
                    "Warning: Anti-aliasing not supported on this computer.")

        if update_rate:
            self.set_update_rate(update_rate)

        self.set_vsync(vsync)

        super().set_fullscreen(fullscreen, screen)
        # This used to be necessary on Linux, but no longer appears to be.
        # With Pyglet 2.0+, setting this to false will not allow the screen to
        # update. It does, however, cause flickering if creating a window that
        # isn't derived from the Window class.
        # self.invalid = False
        set_window(self)

        self._current_view: Optional[View] = None
        self.current_camera: Optional[arcade.Camera] = None
        self.textbox_time = 0.0
        self.key: Optional[int] = None
        self.flip_count: int = 0
        self.static_display: bool = False

        self._ctx: ArcadeContext = ArcadeContext(self, gc_mode=gc_mode)
        set_viewport(0, self.width, 0, self.height)
        self._background_color: Color = (0, 0, 0, 0)

        # See if we should center the window
        if center_window:
            self.center_window()

        if enable_polling:
            self.keyboard = pyglet.window.key.KeyStateHandler()
            self.mouse = pyglet.window.mouse.MouseStateHandler()
            self.push_handlers(self.keyboard, self.mouse)
        else:
            self.keyboard = None
            self.mouse = None