Skip to content

Commit

Permalink
update ImageViewer to detect threading usage and fail gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
Kautenja committed Jun 16, 2020
1 parent b3b7d17 commit fe25ac6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
32 changes: 18 additions & 14 deletions nes_py/_image_viewer.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
"""A simple class for viewing images using pyglet."""
from pyglet.window import key
from pyglet.window import Window
from pyglet.image import ImageData


# a mapping from pyglet key identifiers to native identifiers
KEY_MAP = {
key.ENTER: ord('\r'),
key.SPACE: ord(' '),
}


class ImageViewer(object):
Expand All @@ -31,6 +21,20 @@ def __init__(self, caption, height, width,
Returns:
None
"""
# detect if rendering from python threads and fail
import threading
if threading.current_thread() is not threading.main_thread():
msg = 'rendering from python threads is not supported'
raise RuntimeError(msg)
# import pyglet within class scope to resolve issues with how pyglet
# interacts with OpenGL while using multiprocessing
import pyglet
self.pyglet = pyglet
# a mapping from pyglet key identifiers to native identifiers
self.KEY_MAP = {
self.pyglet.window.key.ENTER: ord('\r'),
self.pyglet.window.key.SPACE: ord(' '),
}
self.caption = caption
self.height = height
self.width = width
Expand Down Expand Up @@ -68,9 +72,9 @@ def _handle_key_event(self, symbol, is_press):
"""
# remap the key to the expected domain
symbol = KEY_MAP.get(symbol, symbol)
symbol = self.KEY_MAP.get(symbol, symbol)
# check if the symbol is the escape key
if symbol == key.ESCAPE:
if symbol == self.pyglet.window.key.ESCAPE:
self._is_escape_pressed = is_press
return
# make sure the symbol is relevant
Expand All @@ -93,7 +97,7 @@ def on_key_release(self, symbol, modifiers):
def open(self):
"""Open the window."""
# create a window for this image viewer instance
self._window = Window(
self._window = self.pyglet.window.Window(
caption=self.caption,
height=self.height,
width=self.width,
Expand Down Expand Up @@ -133,7 +137,7 @@ def show(self, frame):
self._window.switch_to()
self._window.dispatch_events()
# create an image data object
image = ImageData(
image = self.pyglet.image.ImageData(
frame.shape[1],
frame.shape[0],
'RGB',
Expand Down
2 changes: 1 addition & 1 deletion nes_py/nes_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from gym.spaces import Discrete
import numpy as np
from ._rom import ROM
from ._image_viewer import ImageViewer


# the path to the directory this file is in
Expand Down Expand Up @@ -361,7 +362,6 @@ def render(self, mode='human'):
if mode == 'human':
# if the viewer isn't setup, import it and create one
if self.viewer is None:
from ._image_viewer import ImageViewer
# get the caption for the ImageViewer
if self.spec is None:
# if there is no spec, just use the .nes filename
Expand Down

0 comments on commit fe25ac6

Please sign in to comment.