-
Notifications
You must be signed in to change notification settings - Fork 181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch pyglet to programmable pipeline #158
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import absolute_import | ||
import warnings | ||
from distutils.version import LooseVersion | ||
|
||
from pyglet.window import key, mouse | ||
import pyglet | ||
|
||
import imgui | ||
|
||
from . import compute_fb_scale | ||
from .opengl import FixedPipelineRenderer | ||
from .opengl import FixedPipelineRenderer, ProgrammablePipelineRenderer | ||
|
||
|
||
class PygletMixin(object): | ||
|
@@ -33,6 +36,37 @@ class PygletMixin(object): | |
key.Z: imgui.KEY_Z, | ||
} | ||
|
||
def _set_pixel_ratio(self, window): | ||
window_size = window.get_size() | ||
self.io.display_size = window_size | ||
# It is conceivable that the pyglet version will not be solely | ||
# determinant of whether we use the fixed or programmable, so do some | ||
# minor introspection here to check. | ||
if hasattr(window, 'get_viewport_size'): | ||
viewport_size = window.get_viewport_size() | ||
self.io.display_fb_scale = compute_fb_scale(window_size, viewport_size) | ||
elif hasattr(window, 'get_pixel_ratio'): | ||
self.io.display_fb_scale = (window.get_pixel_ratio(), | ||
window.get_pixel_ratio()) | ||
else: | ||
# Default to 1.0 in this unlikely circumstance | ||
self.io.display_fb_scale = (1.0, 1.0) | ||
|
||
|
||
def _attach_callbacks(self, window): | ||
window.push_handlers( | ||
self.on_mouse_motion, | ||
self.on_key_press, | ||
self.on_key_release, | ||
self.on_text, | ||
self.on_mouse_drag, | ||
self.on_mouse_press, | ||
self.on_mouse_release, | ||
self.on_mouse_scroll, | ||
self.on_resize, | ||
) | ||
|
||
|
||
def _map_keys(self): | ||
key_map = self.io.key_map | ||
|
||
|
@@ -100,27 +134,39 @@ def on_mouse_scroll(self, x, y, mods, scroll): | |
def on_resize(self, width, height): | ||
self.io.display_size = width, height | ||
|
||
|
||
class PygletRenderer(PygletMixin, FixedPipelineRenderer): | ||
class PygletFixedPipelineRenderer(PygletMixin, FixedPipelineRenderer): | ||
def __init__(self, window, attach_callbacks=True): | ||
super(PygletRenderer, self).__init__() | ||
window_size = window.get_size() | ||
viewport_size = window.get_viewport_size() | ||
|
||
self.io.display_size = window_size | ||
self.io.display_fb_scale = compute_fb_scale(window_size, viewport_size) | ||
super(PygletFixedPipelineRenderer, self).__init__() | ||
self._set_pixel_ratio(window) | ||
self._map_keys() | ||
if attach_callbacks: self._attach_callbacks(window) | ||
|
||
class PygletProgrammablePipelineRenderer(PygletMixin, ProgrammablePipelineRenderer): | ||
def __init__(self, window, attach_callbacks = True): | ||
super(PygletProgrammablePipelineRenderer, self).__init__() | ||
self._set_pixel_ratio(window) | ||
self._map_keys() | ||
if attach_callbacks: self._attach_callbacks(window) | ||
|
||
if attach_callbacks: | ||
window.push_handlers( | ||
self.on_mouse_motion, | ||
self.on_key_press, | ||
self.on_key_release, | ||
self.on_text, | ||
self.on_mouse_drag, | ||
self.on_mouse_press, | ||
self.on_mouse_release, | ||
self.on_mouse_scroll, | ||
self.on_resize, | ||
) | ||
class PygletRenderer(PygletFixedPipelineRenderer): | ||
def __init__(self, window, attach_callbacks=True): | ||
warnings.warn("PygletRenderer is deprecated; please use either " | ||
"PygletFixedPipelineRenderer (for OpenGL 2.1, pyglet < 2.0) or " | ||
"PygletProgrammablePipelineRenderer (for later versions) or " | ||
"create_renderer(window) to auto-detect.", | ||
DeprecationWarning) | ||
super(PygletRenderer, self).__init__(window, attach_callbacks) | ||
|
||
def create_renderer(window, attach_callbacks=True): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
""" | ||
This is a helper function that wraps the appropriate version of the Pyglet | ||
renderer class, based on the version of pyglet being used. | ||
""" | ||
# Determine the context version | ||
# Pyglet < 2.0 has issues with ProgrammablePipeline even when the context | ||
# is OpenGL 3, so we need to check the pyglet version rather than looking | ||
# at window.config.major_version to see if we want to use programmable. | ||
if LooseVersion(pyglet.version) < LooseVersion('2.0'): | ||
return PygletFixedPipelineRenderer(window, attach_callbacks) | ||
else: | ||
return PygletProgrammablePipelineRenderer(window, attach_callbacks) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍