예제 #1
0
    def show(self, frame):
        """
        Show an array of pixels on the window.

        Args:
            frame (numpy.ndarray): the frame to show on the window

        Returns:
            None
        """
        # check that the frame has the correct dimensions
        if len(frame.shape) != 3:
            raise ValueError('frame should have shape with only 3 dimensions')
        # open the window if it isn't open already
        if not self.is_open:
            self.open()
        # prepare the window for the next frame
        self._window.clear()
        self._window.switch_to()
        self._window.dispatch_events()
        # create an image data object
        image = ImageData(frame.shape[1],
                          frame.shape[0],
                          'RGB',
                          frame.tobytes(),
                          pitch=frame.shape[1] * -3)
        # send the image to the window
        image.blit(0, 0, width=self._window.width, height=self._window.height)
        self._window.flip()
예제 #2
0
 def draw(self):
     data = ImageData(
         *self.__image.size,
         'RGBA', self.__image.tobytes(),
         pitch=-self.__image.size[0]*4
     )
     data.blit(0, 0)
예제 #3
0
 def render(self, img_array):
     from pyglet.image import ImageData
     from pyglet.gl import glClearColor
     glClearColor(1,1,1,1)
     img_array = np.ascontiguousarray(img_array[::-1, :])
     img_c_array = img_array.ctypes.data_as(ctypes.POINTER(ctypes.c_float * img_array.size))
     img_data = ImageData(img_array.shape[0], img_array.shape[1], 'L', img_c_array)
     self.window.clear()
     self.window.switch_to()
     self.window.dispatch_events()
     img_data.blit(0, 0)
     self.window.flip()
예제 #4
0
    def _render(self, mode='human', close=False):
        import pyglet
        from pyglet.image import ImageData
        from pyglet.gl import glPushMatrix, glPopMatrix, glScalef, glTranslatef

        if mode == 'rgb_array':
            return self.img

        if close:
            if self.window:
                self.window.close()
            return

        if self.window is None:
            # For displaying text
            self.textLabel = pyglet.text.Label(font_name="Arial",
                                               font_size=14,
                                               x=5,
                                               y=WINDOW_SIZE - 19)
            self.window = pyglet.window.Window(width=WINDOW_SIZE,
                                               height=WINDOW_SIZE)

        self.window.clear()
        self.window.switch_to()
        self.window.dispatch_events()

        if self.stateData is None:
            return

        # Draw the image to the rendering window
        width = self.img.shape[0]
        height = self.img.shape[1]
        imgData = ImageData(
            width,
            height,
            'RGB',
            self.img.tobytes(),
            pitch=width * 3,
        )
        glPushMatrix()
        glTranslatef(0, WINDOW_SIZE, 0)
        glScalef(1, -1, 1)
        imgData.blit(0, 0, 0, WINDOW_SIZE, WINDOW_SIZE)
        glPopMatrix()

        # Display position/state information
        pos = self.stateData['position']
        self.textLabel.text = "(%.2f, %.2f, %.2f)" % (pos[0], pos[1], pos[2])
        self.textLabel.draw()

        self.window.flip()
예제 #5
0
    def update(self, dt):
        received = 0
        img_buf = b""
        size_data = self.sock.recv(4)
        size = struct.unpack(">I", size_data)[0]

        while True:
            data = self.sock.recv(size - received)
            received += len(data)
            img_buf += data
            if size <= received:
                break

        img = Image.open(io.BytesIO(img_buf))
        #img = img.transpose(Image.FLIP_TOP_BOTTOM)
        img = img.transpose(Image.FLIP_LEFT_RIGHT)
        img = ImageData(self.width, self.height, "RGB",
                        img.tobytes("raw", "RGB"))

        img.blit(0, 0)
예제 #6
0
class Renderer(object):
    def __init__(self, client, map_size, minimap_size) -> None:
        self._client = client

        self._window = None
        self._map_size = map_size
        self._map_image = None
        self._minimap_size = minimap_size
        self._minimap_image = None
        self._mouse_x, self._mouse_y = None, None
        self._text_supply = None
        self._text_vespene = None
        self._text_minerals = None
        self._text_score = None
        self._text_time = None

    async def render(self, observation):
        render_data = observation.observation.render_data

        map_size = render_data.map.size
        map_data = render_data.map.data
        minimap_size = render_data.minimap.size
        minimap_data = render_data.minimap.data

        map_width, map_height = map_size.x, map_size.y
        map_pitch = -map_width * 3

        minimap_width, minimap_height = minimap_size.x, minimap_size.y
        minimap_pitch = -minimap_width * 3

        if not self._window:
            from pyglet.window import Window
            from pyglet.image import ImageData
            from pyglet.text import Label
            self._window = Window(width=map_width, height=map_height)
            self._window.on_mouse_press = self._on_mouse_press
            self._window.on_mouse_release = self._on_mouse_release
            self._window.on_mouse_drag = self._on_mouse_drag
            self._map_image = ImageData(map_width, map_height, 'RGB', map_data,
                                        map_pitch)
            self._minimap_image = ImageData(minimap_width, minimap_height,
                                            'RGB', minimap_data, minimap_pitch)
            self._text_supply = Label('',
                                      font_name='Arial',
                                      font_size=16,
                                      anchor_x='right',
                                      anchor_y='top',
                                      x=self._map_size[0] - 10,
                                      y=self._map_size[1] - 10,
                                      color=(200, 200, 200, 255))
            self._text_vespene = Label('',
                                       font_name='Arial',
                                       font_size=16,
                                       anchor_x='right',
                                       anchor_y='top',
                                       x=self._map_size[0] - 130,
                                       y=self._map_size[1] - 10,
                                       color=(28, 160, 16, 255))
            self._text_minerals = Label('',
                                        font_name='Arial',
                                        font_size=16,
                                        anchor_x='right',
                                        anchor_y='top',
                                        x=self._map_size[0] - 200,
                                        y=self._map_size[1] - 10,
                                        color=(68, 140, 255, 255))
            self._text_score = Label('',
                                     font_name='Arial',
                                     font_size=16,
                                     anchor_x='left',
                                     anchor_y='top',
                                     x=10,
                                     y=self._map_size[1] - 10,
                                     color=(219, 30, 30, 255))
            self._text_time = Label('',
                                    font_name='Arial',
                                    font_size=16,
                                    anchor_x='right',
                                    anchor_y='bottom',
                                    x=self._minimap_size[0] - 10,
                                    y=self._minimap_size[1] + 10,
                                    color=(255, 255, 255, 255))
        else:
            self._map_image.set_data('RGB', map_pitch, map_data)
            self._minimap_image.set_data('RGB', minimap_pitch, minimap_data)
            self._text_time.text = str(
                datetime.timedelta(
                    seconds=(observation.observation.game_loop * 0.725) // 16))
            if observation.observation.HasField('player_common'):
                self._text_supply.text = "{} / {}".format(
                    observation.observation.player_common.food_used,
                    observation.observation.player_common.food_cap)
                self._text_vespene.text = str(
                    observation.observation.player_common.vespene)
                self._text_minerals.text = str(
                    observation.observation.player_common.minerals)
            if observation.observation.HasField('score'):
                self._text_score.text = "{} score: {}".format(
                    score_pb._SCORE_SCORETYPE.values_by_number[
                        observation.observation.score.score_type].name,
                    observation.observation.score.score)

        await self._update_window()

        if self._client.in_game and (not observation.player_result
                                     ) and self._mouse_x and self._mouse_y:
            await self._client.move_camera_spatial(
                Point2((self._mouse_x, self._minimap_size[0] - self._mouse_y)))
            self._mouse_x, self._mouse_y = None, None

    async def _update_window(self):
        self._window.switch_to()
        self._window.dispatch_events()

        self._window.clear()

        self._map_image.blit(0, 0)
        self._minimap_image.blit(0, 0)
        self._text_time.draw()
        self._text_score.draw()
        self._text_minerals.draw()
        self._text_vespene.draw()
        self._text_supply.draw()

        self._window.flip()

    def _on_mouse_press(self, x, y, button, modifiers):
        if button != 1:  # 1: mouse.LEFT
            return
        if x > self._minimap_size[0] or y > self._minimap_size[1]:
            return
        self._mouse_x, self._mouse_y = x, y

    def _on_mouse_release(self, x, y, button, modifiers):
        if button != 1:  # 1: mouse.LEFT
            return
        if x > self._minimap_size[0] or y > self._minimap_size[1]:
            return
        self._mouse_x, self._mouse_y = x, y

    def _on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
        if not buttons & 1:  # 1: mouse.LEFT
            return
        if x > self._minimap_size[0] or y > self._minimap_size[1]:
            return
        self._mouse_x, self._mouse_y = x, y
예제 #7
0
class Renderer:
    def __init__(self, client, map_size, minimap_size):
        self._client = client

        self._window = None
        self._map_size = map_size
        self._map_image = None
        self._minimap_size = minimap_size
        self._minimap_image = None
        self._mouse_x, self._mouse_y = None, None
        self._text_supply = None
        self._text_vespene = None
        self._text_minerals = None
        self._text_score = None
        self._text_time = None

    async def render(self, observation):
        render_data = observation.observation.render_data

        map_size = render_data.map.size
        map_data = render_data.map.data
        minimap_size = render_data.minimap.size
        minimap_data = render_data.minimap.data

        map_width, map_height = map_size.x, map_size.y
        map_pitch = -map_width * 3

        minimap_width, minimap_height = minimap_size.x, minimap_size.y
        minimap_pitch = -minimap_width * 3

        if not self._window:
            # pylint: disable=C0415
            from pyglet.image import ImageData
            from pyglet.text import Label
            from pyglet.window import Window

            self._window = Window(width=map_width, height=map_height)
            self._window.on_mouse_press = self._on_mouse_press
            self._window.on_mouse_release = self._on_mouse_release
            self._window.on_mouse_drag = self._on_mouse_drag
            self._map_image = ImageData(map_width, map_height, "RGB", map_data,
                                        map_pitch)
            self._minimap_image = ImageData(minimap_width, minimap_height,
                                            "RGB", minimap_data, minimap_pitch)
            self._text_supply = Label(
                "",
                font_name="Arial",
                font_size=16,
                anchor_x="right",
                anchor_y="top",
                x=self._map_size[0] - 10,
                y=self._map_size[1] - 10,
                color=(200, 200, 200, 255),
            )
            self._text_vespene = Label(
                "",
                font_name="Arial",
                font_size=16,
                anchor_x="right",
                anchor_y="top",
                x=self._map_size[0] - 130,
                y=self._map_size[1] - 10,
                color=(28, 160, 16, 255),
            )
            self._text_minerals = Label(
                "",
                font_name="Arial",
                font_size=16,
                anchor_x="right",
                anchor_y="top",
                x=self._map_size[0] - 200,
                y=self._map_size[1] - 10,
                color=(68, 140, 255, 255),
            )
            self._text_score = Label(
                "",
                font_name="Arial",
                font_size=16,
                anchor_x="left",
                anchor_y="top",
                x=10,
                y=self._map_size[1] - 10,
                color=(219, 30, 30, 255),
            )
            self._text_time = Label(
                "",
                font_name="Arial",
                font_size=16,
                anchor_x="right",
                anchor_y="bottom",
                x=self._minimap_size[0] - 10,
                y=self._minimap_size[1] + 10,
                color=(255, 255, 255, 255),
            )
        else:
            self._map_image.set_data("RGB", map_pitch, map_data)
            self._minimap_image.set_data("RGB", minimap_pitch, minimap_data)
            self._text_time.text = str(
                datetime.timedelta(
                    seconds=(observation.observation.game_loop * 0.725) // 16))
            if observation.observation.HasField("player_common"):
                self._text_supply.text = f"{observation.observation.player_common.food_used} / {observation.observation.player_common.food_cap}"
                self._text_vespene.text = str(
                    observation.observation.player_common.vespene)
                self._text_minerals.text = str(
                    observation.observation.player_common.minerals)
            if observation.observation.HasField("score"):
                # pylint: disable=W0212
                self._text_score.text = f"{score_pb._SCORE_SCORETYPE.values_by_number[observation.observation.score.score_type].name} score: {observation.observation.score.score}"

        await self._update_window()

        if self._client.in_game and (not observation.player_result
                                     ) and self._mouse_x and self._mouse_y:
            await self._client.move_camera_spatial(
                Point2((self._mouse_x, self._minimap_size[0] - self._mouse_y)))
            self._mouse_x, self._mouse_y = None, None

    async def _update_window(self):
        self._window.switch_to()
        self._window.dispatch_events()

        self._window.clear()

        self._map_image.blit(0, 0)
        self._minimap_image.blit(0, 0)
        self._text_time.draw()
        self._text_score.draw()
        self._text_minerals.draw()
        self._text_vespene.draw()
        self._text_supply.draw()

        self._window.flip()

    def _on_mouse_press(self, x, y, button, _modifiers):
        if button != 1:  # 1: mouse.LEFT
            return
        if x > self._minimap_size[0] or y > self._minimap_size[1]:
            return
        self._mouse_x, self._mouse_y = x, y

    def _on_mouse_release(self, x, y, button, _modifiers):
        if button != 1:  # 1: mouse.LEFT
            return
        if x > self._minimap_size[0] or y > self._minimap_size[1]:
            return
        self._mouse_x, self._mouse_y = x, y

    def _on_mouse_drag(self, x, y, _dx, _dy, buttons, _modifiers):
        if not buttons & 1:  # 1: mouse.LEFT
            return
        if x > self._minimap_size[0] or y > self._minimap_size[1]:
            return
        self._mouse_x, self._mouse_y = x, y