Skip to content
Snippets Groups Projects
Commit 481c52d9 authored by Fabian Heinrich's avatar Fabian Heinrich
Browse files

Pygame images are stored in a dict to avoid loading multiple times

parent 103df462
No related branches found
No related tags found
1 merge request!14Resolve "Cashe loaded gui images"
Pipeline #42214 passed
...@@ -19,7 +19,7 @@ from overcooked_simulator.pygame_gui.game_colors import BLUE ...@@ -19,7 +19,7 @@ from overcooked_simulator.pygame_gui.game_colors import BLUE
from overcooked_simulator.pygame_gui.game_colors import colors, Color from overcooked_simulator.pygame_gui.game_colors import colors, Color
from overcooked_simulator.simulation_runner import Simulator from overcooked_simulator.simulation_runner import Simulator
PLAYER_DEBUG_VIZ = True USE_PLAYER_COOK_SPRITES = False
SHOW_INTERACTION_RANGE = False SHOW_INTERACTION_RANGE = False
...@@ -104,6 +104,8 @@ class PyGameGUI: ...@@ -104,6 +104,8 @@ class PyGameGUI:
self.player_colors = self.create_player_colors() self.player_colors = self.create_player_colors()
self.image_cache_dict = {}
def create_player_colors(self) -> list[Color]: def create_player_colors(self) -> list[Color]:
number_player = len(self.simulator.env.players) number_player = len(self.simulator.env.players)
hue_values = np.linspace(0, 1, number_player + 1) hue_values = np.linspace(0, 1, number_player + 1)
...@@ -183,6 +185,24 @@ class PyGameGUI: ...@@ -183,6 +185,24 @@ class PyGameGUI:
1, 1,
) )
def draw_image(self, img_path, size, pos, rot_angle=0):
cache_entry = f"{img_path}_{size}_{rot_angle}"
if cache_entry in self.image_cache_dict.keys():
image = self.image_cache_dict[cache_entry]
else:
image = pygame.image.load(
ROOT_DIR / "pygame_gui" / img_path
).convert_alpha()
image = pygame.transform.scale(image, (size, size))
if rot_angle != 0:
image = pygame.transform.rotate(image, rot_angle)
self.image_cache_dict[cache_entry] = image
rect = image.get_rect()
rect.center = pos
self.screen.blit(image, rect)
def draw_players(self, state): def draw_players(self, state):
"""Visualizes the players as circles with a triangle for the facing direction. """Visualizes the players as circles with a triangle for the facing direction.
If the player holds something in their hands, it is displayed If the player holds something in their hands, it is displayed
...@@ -191,7 +211,7 @@ class PyGameGUI: ...@@ -191,7 +211,7 @@ class PyGameGUI:
state: The game state returned by the environment. state: The game state returned by the environment.
""" """
for p_idx, player in enumerate(state["players"].values()): for p_idx, player in enumerate(state["players"].values()):
if PLAYER_DEBUG_VIZ: if USE_PLAYER_COOK_SPRITES:
pos = player.pos pos = player.pos
size = player.radius size = player.radius
color1 = self.player_colors[p_idx] color1 = self.player_colors[p_idx]
...@@ -214,18 +234,10 @@ class PyGameGUI: ...@@ -214,18 +234,10 @@ class PyGameGUI:
) )
else: else:
img_path = self.visualization_config["Cook"]["parts"][0]["path"] img_path = self.visualization_config["Cook"]["parts"][0]["path"]
image = pygame.image.load(
ROOT_DIR / "pygame_gui" / img_path
).convert_alpha()
rel_x, rel_y = player.facing_direction rel_x, rel_y = player.facing_direction
angle = -np.rad2deg(math.atan2(rel_y, rel_x)) + 90 angle = -np.rad2deg(math.atan2(rel_y, rel_x)) + 90
size = self.visualization_config["Cook"]["parts"][0]["size"] size = self.visualization_config["Cook"]["parts"][0]["size"]
image = pygame.transform.scale(image, (size, size)) self.draw_image(img_path, size, player.pos, angle)
image = pygame.transform.rotate(image, angle)
rect = image.get_rect()
rect.center = player.pos
self.screen.blit(image, rect)
if SHOW_INTERACTION_RANGE: if SHOW_INTERACTION_RANGE:
pygame.draw.circle( pygame.draw.circle(
...@@ -246,16 +258,7 @@ class PyGameGUI: ...@@ -246,16 +258,7 @@ class PyGameGUI:
for part in parts: for part in parts:
part_type = part["type"] part_type = part["type"]
if part_type == "image": if part_type == "image":
image = pygame.image.load( self.draw_image(parts[0]["path"], parts[0]["size"], pos)
ROOT_DIR / "pygame_gui" / parts[0]["path"]
).convert_alpha()
size = parts[0]["size"]
image = pygame.transform.scale(image, (size * scale, size * scale))
rect = image.get_rect()
rect.center = pos
self.screen.blit(image, rect)
elif part_type == "rect": elif part_type == "rect":
height = part["height"] height = part["height"]
width = part["width"] width = part["width"]
...@@ -285,7 +288,7 @@ class PyGameGUI: ...@@ -285,7 +288,7 @@ class PyGameGUI:
else: else:
pygame.draw.circle(self.screen, color, pos, radius) pygame.draw.circle(self.screen, color, pos, radius)
def draw_item(self, pos: npt.NDArray[float], item: Item, scale=1.0): def draw_item(self, pos: npt.NDArray[float], item: Item, scale: float = 1.0):
"""Visualization of an item at the specified position. On a counter or in the hands of the player. """Visualization of an item at the specified position. On a counter or in the hands of the player.
The visual composition of the item is read in from visualization.yaml file, where it is specified as The visual composition of the item is read in from visualization.yaml file, where it is specified as
different parts to be drawn. different parts to be drawn.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment