Added Cache and some improvements.

This commit is contained in:
The Wobbler 2024-04-19 17:20:28 +02:00
parent 5c5ed49a9b
commit 8da526f2ca
2 changed files with 26 additions and 9 deletions

View file

@ -1,17 +1,17 @@
#!/usr/bin/python3 #!/usr/bin/python3
import pygame import pygame
from dataclasses import dataclass
from wobbl_tools import pg from wobbl_tools import pg
from wobbl_tools import data_file
class Object: class Model:
def __init__(self, sprite_path: str="sprites/coffee_cup.png", sprite_spacing: int=1, rotation: int=45, size_multiplier: int=16): def __init__(self, sprite_path: str="sprites/coffee_cup.png", size_multiplier: int=16, sprite_spacing: int=1, rotation: int=45):
self.sprite_path = sprite_path self.sprite_path = sprite_path
self.sprite_spacing = sprite_spacing self.sprite_spacing = sprite_spacing
self.rotation = rotation self.rotation = rotation
self.size_multiplier = size_multiplier self.size_multiplier = size_multiplier
self.cache = {}
self.sprite_size = tuple(get_json(get_dir(sprite_path) + "/sprite_properties.json")[get_filename(sprite_path)]["size"]) self.sprite_size = tuple(get_json(get_dir(sprite_path) + "/sprite_properties.json")[get_filename(sprite_path)]["size"])
self.original_sprites = self.get_sprites() self.original_sprites = self.get_sprites()
@ -34,6 +34,9 @@ class Object:
return sprites return sprites
def rotate_sprites(self): def rotate_sprites(self):
if self.rotation in self.cache:
return self.original_sprites
sprites = [] sprites = []
for sprite in self.original_sprites: for sprite in self.original_sprites:
@ -44,6 +47,9 @@ class Object:
return sprites return sprites
def generate_surface(self): def generate_surface(self):
if self.rotation in self.cache:
return self.cache[self.rotation]
sw, sh = self.sprites[0].get_size() sw, sh = self.sprites[0].get_size()
surface = pygame.Surface((sw, sh + self.sprite_spacing * len(self.sprites)), flags=pygame.SRCALPHA) surface = pygame.Surface((sw, sh + self.sprite_spacing * len(self.sprites)), flags=pygame.SRCALPHA)
@ -56,12 +62,27 @@ class Object:
surface = pygame.transform.scale(surface, (surface.get_width() * self.size_multiplier, surface.get_height() * self.size_multiplier)) surface = pygame.transform.scale(surface, (surface.get_width() * self.size_multiplier, surface.get_height() * self.size_multiplier))
self.cache[self.rotation] = surface
return surface return surface
def update(self): def update(self):
return self.rotate_sprites(), self.generate_surface() return self.rotate_sprites(), self.generate_surface()
class Object:
def __init__(self, model: Model, position: tuple, rotation: int=45):
self.model = model
self.position = position
self.rotation = rotation
def update(self):
self.model.update()
def draw(self, surface):
surface.blit(self.model.cache[self.rotation])
def get_dir(path: str): def get_dir(path: str):
return path.rsplit("/", 1)[0] return path.rsplit("/", 1)[0]
@ -76,7 +97,3 @@ def get_json(path: str):
file.close() file.close()
return data return data
if __name__ == "__main__":
bla = Object()

View file

@ -15,7 +15,7 @@ class ModelViewer:
self.window_size = window_size self.window_size = window_size
self.fps = fps self.fps = fps
self.rotating = True self.rotating = True
self.model = engine.Object(size_multiplier=16) self.model = engine.Model(size_multiplier=4, sprite_spacing=1)
self.rotation_cooldown = 5 self.rotation_cooldown = 5
self.clock = pygame.time.Clock() self.clock = pygame.time.Clock()