Skip to content

Commit

Permalink
make backend more flexible, deal with weird macosx backend
Browse files Browse the repository at this point in the history
  • Loading branch information
misko committed Oct 8, 2024
1 parent d06e0a4 commit 0bb124a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
1 change: 0 additions & 1 deletion hudes/hudes_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def main():
if args.input == "keyboard":
controller = KeyboardClient(seed=args.seed)
view = View()
controller.attach_view(View())
elif args.input == "keyboardGL":
controller = KeyboardClientGL(
seed=args.seed,
Expand Down
61 changes: 48 additions & 13 deletions hudes/view.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import logging
import math
from typing import List

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pygame as pg
import pygame.midi
import torch
from OpenGL.GL import *
from OpenGL.GLU import * # Import GLU for perspective functions
from pygame.locals import *

from hudes.opengl_func import (
create_grid_indices,
Expand All @@ -18,16 +26,24 @@
update_grid_vbo,
)

matplotlib.use("Agg")
# backend = "Agg"
# backend='cairo'
# matplotlib.use(backend)


import matplotlib.pyplot as plt
import numpy as np
import pygame as pg
import torch
from OpenGL.GL import *
from OpenGL.GLU import * # Import GLU for perspective functions
from pygame.locals import *
def surface_to_npim(surface):
"""Transforms a Cairo surface into a numpy array."""
im = +np.frombuffer(surface.get_data(), np.uint8)
H, W = surface.get_height(), surface.get_width()
im.shape = (H, W, 4) # for RGBA
return im[:, :, :3]


def svg_to_npim(svg_bytestring, dpi):
"""Renders a svg bytestring as a RGB image in a numpy array"""
tree = cairosvg.parser.Tree(bytestring=svg_bytestring)
surf = cairosvg.surface.PNGSurface(tree, None, dpi).cairo
return surface_to_npim(surf)


# Shader creation helper functions
Expand Down Expand Up @@ -150,12 +166,25 @@ def __init__(self, use_midi=False):
print(f"using input_id :{self.midi_input_id}:")
self.midi_input = pygame.midi.Input(self.midi_input_id)

dpi = 200 # plt.rcParams["figure.dpi"]
logging.info(f"Matplotlib backend: {plt.get_backend()}")

self.window_size = (1200, 800)
self.window = pg.display.set_mode(self.window_size)
self.fig = plt.figure(
figsize=(12, 8),
figsize=(self.window_size[0] / dpi, self.window_size[1] / dpi),
)

if self.fig.dpi != dpi:
logging.warning(
f"DPI flag not respected by matplotlib backend ({plt.get_backend()})! Should be {dpi} but is {self.fig.dpi} "
)
self.window_size = (
int(self.fig.get_figwidth() * self.fig.dpi),
int(self.fig.get_figheight() * self.fig.dpi),
)

self.window = pg.display.set_mode(self.window_size)

# self.canvas = agg.FigureCanvasAgg(self.fig)
self.canvas = self.fig.canvas
self.renderer = self.canvas.get_renderer()
Expand Down Expand Up @@ -290,10 +319,16 @@ def plot_train_and_val(
self.axd["D"].set_xlabel("Step")

def draw(self):
if True: # backend.lower()=='agg':
self.canvas.draw()
surf = pg.image.frombytes(
self.renderer.tostring_rgb(),
self.window_size,
"RGB",
)
self.screen.blit(surf, (0, 0))
# else:

self.canvas.draw()
surf = pg.image.frombytes(self.renderer.tostring_rgb(), self.window_size, "RGB")
self.screen.blit(surf, (0, 0))
pg.display.flip() # draws whole screen vs update that draws a parts


Expand Down

0 comments on commit 0bb124a

Please sign in to comment.