improved performances + added zoom slider

This commit is contained in:
Louis Heredero 2024-06-28 18:41:47 +02:00
parent c7d84246b4
commit 9346637462
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
2 changed files with 105 additions and 78 deletions

View File

@ -1,68 +1,37 @@
from math import floor from math import floor
import os
from typing import Optional from typing import Optional
import pygame import pygame
from src.image_handler import ImageHandler
class Editor: class Editor:
WIDTH = 800 WIDTH: int = 800
HEIGHT = 600 HEIGHT: int = 600
MAP_SIZE = 1024 MAP_SIZE: int = 1024
MAPS_DIR = "/tmp/lycacraft_maps" MAPS_DIR: str = "/tmp/lycacraft_maps"
MIN_ZOOM = 0.25 MIN_ZOOM: float = 0.25
MAX_ZOOM = 4 MAX_ZOOM: float = 4
def __init__(self): def __init__(self):
pygame.init()
self.width: int = self.WIDTH self.width: int = self.WIDTH
self.height: int = self.HEIGHT self.height: int = self.HEIGHT
self.win: pygame.Surface = pygame.display.set_mode([self.width, self.height], pygame.RESIZABLE) self.win: pygame.Surface = pygame.display.set_mode([self.width, self.height], pygame.RESIZABLE)
pygame.display.set_caption("Lycacraft Map Editor")
self.center: list[int] = [0, 0] self.center: list[int] = [0, 0]
self.zoom: float = 1 self.zoom: float = 1
self.map_size: int = self.MAP_SIZE
self.running: bool = False self.running: bool = False
self.cache: dict[float, dict[tuple[int, int], pygame.Surface]] = {} self.image_handler: ImageHandler = ImageHandler(self.MAPS_DIR, self.MAP_SIZE)
self.load_maps()
self.clock: pygame.time.Clock = pygame.time.Clock() self.clock: pygame.time.Clock = pygame.time.Clock()
self.visible_maps_count: int = 0
self.drag_pos: Optional[tuple[int, int]] = None self.drag_pos: Optional[tuple[int, int]] = None
self.font: pygame.font.Font = pygame.font.SysFont("Ubuntu", 20)
def load_maps(self) -> None:
cache = {}
for path in os.listdir(self.MAPS_DIR):
fullpath = os.path.join(self.MAPS_DIR, path)
name, x, y = path.split(".")[0].split("_")
cache[(int(x), int(y))] = pygame.image.load(fullpath)
self.cache = {
self.zoom: cache
}
def resize_maps(self) -> None:
if self.zoom in self.cache:
return
cache = {}
for pos, img in self.cache[1].items():
cache[pos] = pygame.transform.scale_by(img, self.zoom)
self.cache[self.zoom] = cache
def get_map(self, x: int, y: int) -> Optional[pygame.Surface]:
cache = self.cache.get(self.zoom, {})
pos = (x, y)
if pos not in cache:
if pos not in self.cache[1]:
return None
img = self.cache[1][pos]
cache[pos] = pygame.transform.scale_by(img, self.zoom)
return cache[pos]
def mainloop(self) -> None: def mainloop(self) -> None:
self.running = True self.running = True
while self.running: while self.running:
pygame.display.set_caption(f"Lycacraft Map Editor - {self.clock.get_fps():.2f}fps - {self.visible_maps_count} maps rendered") pygame.display.set_caption(f"Lycacraft Map Editor - {self.clock.get_fps():.2f}fps")
self.process_events() self.process_events()
self.render() self.render()
self.clock.tick(30) self.clock.tick(30)
@ -111,63 +80,61 @@ class Editor:
def render(self) -> None: def render(self) -> None:
self.win.fill((0, 0, 0)) self.win.fill((0, 0, 0))
off_x = self.center[0] % self.MAP_SIZE off_x = (self.center[0] * self.zoom) % self.MAP_SIZE
off_y = self.center[1] % self.MAP_SIZE off_y = (self.center[1] * self.zoom) % self.MAP_SIZE
w2 = self.width / 2 w2 = self.width / 2
h2 = self.height / 2 h2 = self.height / 2
# In game top-left / bottom-right corners
x0 = floor(self.center[0] - w2 / self.zoom) x0 = floor(self.center[0] - w2 / self.zoom)
y0 = floor(self.center[1] - h2 / self.zoom) y0 = floor(self.center[1] - h2 / self.zoom)
x1 = floor(self.center[0] + w2 / self.zoom) x1 = floor(self.center[0] + w2 / self.zoom)
y1 = floor(self.center[1] + h2 / self.zoom) y1 = floor(self.center[1] + h2 / self.zoom)
mx0 = floor(x0 / self.MAP_SIZE) # Top-left / bottom-right maps
my0 = floor(y0 / self.MAP_SIZE) mx0 = floor(x0 * self.zoom / self.MAP_SIZE)
mx1 = floor(x1 / self.MAP_SIZE) my0 = floor(y0 * self.zoom / self.MAP_SIZE)
my1 = floor(y1 / self.MAP_SIZE) mx1 = floor(x1 * self.zoom / self.MAP_SIZE)
my1 = floor(y1 * self.zoom / self.MAP_SIZE)
h_maps = mx1 - mx0 + 1 h_maps = mx1 - mx0 + 1
v_maps = my1 - my0 + 1 v_maps = my1 - my0 + 1
cx = floor(self.center[0] / self.MAP_SIZE) cx = floor(self.center[0] * self.zoom / self.MAP_SIZE)
cy = floor(self.center[1] / self.MAP_SIZE) cy = floor(self.center[1] * self.zoom / self.MAP_SIZE)
self.visible_maps_count = h_maps * v_maps ox = w2 + (mx0 - cx) * self.MAP_SIZE - off_x
ox = w2 + (mx0 - cx) * self.map_size - off_x * self.zoom oy = h2 + (my0 - cy) * self.MAP_SIZE - off_y
oy = h2 + (my0 - cy) * self.map_size - off_y * self.zoom
# cache = self.cache[self.zoom]
for y in range(v_maps): for y in range(v_maps):
for x in range(h_maps): for x in range(h_maps):
# map_pos = (mx0 + x, my0 + y) map_image = self.image_handler.get_for_pos(mx0 + x, my0 + y, self.zoom)
map_image = self.get_map(mx0 + x, my0 + y)
# if map_pos not in cache:
if map_image is None: if map_image is None:
continue continue
self.win.blit(map_image, [
ox + x * self.MAP_SIZE,
oy + y * self.MAP_SIZE
])
# map_image = cache[map_pos] zoom_height = self.height * 0.2
zoom_h_margin = self.width * 0.02
sx = ox + x * self.map_size zoom_v_margin = self.height * 0.05
sy = oy + y * self.map_size zoom_x = self.width - zoom_h_margin
ex = sx + self.map_size zoom_y = self.height - zoom_v_margin - zoom_height
ey = sy + self.map_size zoom_space = zoom_height / 4
sx2, ex2 = min(self.width, max(0, sx)), min(self.width, max(0, ex)) zoom_r = zoom_space / 4
sy2, ey2 = min(self.height, max(0, sy)), min(self.height, max(0, ey)) pygame.draw.line(self.win, (255, 255, 255), [zoom_x, zoom_y], [zoom_x, zoom_y + zoom_height])
"""self.win.blit(map_image, [ for i in range(5):
ox + x * self.map_size, y = zoom_y + zoom_height - i * zoom_space
oy + y * self.map_size, zoom = 2 ** (i - 2)
])""" col = (255, 0, 0) if zoom == self.zoom else (255, 255, 255)
self.win.blit(map_image, [sx2, sy2], [sx2 - sx, sy2 - sy, ex2 - sx2, ey2 - sy2]) pygame.draw.circle(self.win, col, [zoom_x, y], zoom_r)
txt = self.font.render(str(zoom), True, (255, 255, 255))
for y in range(v_maps): self.win.blit(txt, [zoom_x - txt.get_width() - zoom_r - 5, y - txt.get_height() / 2])
for x in range(h_maps):
pygame.draw.rect(self.win, (255, 0, 0), [ox + x * self.map_size, oy + y * self.map_size, self.map_size, self.map_size], 1)
pygame.display.flip() pygame.display.flip()
def set_zoom(self, zoom: float) -> None: def set_zoom(self, zoom: float) -> None:
self.zoom = max(self.MIN_ZOOM, min(self.MAX_ZOOM, zoom)) self.zoom = max(self.MIN_ZOOM, min(self.MAX_ZOOM, zoom))
self.map_size = self.MAP_SIZE * self.zoom
# self.resize_maps()
def zoom_in(self) -> None: def zoom_in(self) -> None:
self.set_zoom(self.zoom * 2) self.set_zoom(self.zoom * 2)

60
src/image_handler.py Normal file
View File

@ -0,0 +1,60 @@
import os
from math import floor
from typing import Optional
import pygame
class ImageHandler:
def __init__(self, maps_dir: str, base_size: int):
self.maps_dir: str = maps_dir
self.base_size: int = base_size
self.cache: dict = {}
self.load_base_images()
def load_base_images(self) -> None:
cache = {}
for path in os.listdir(self.maps_dir):
fullpath = os.path.join(self.maps_dir, path)
name, x, y = path.split(".")[0].split("_")
cache[(int(x), int(y))] = pygame.image.load(fullpath).convert_alpha()
self.cache = {
1: cache
}
def get_for_pos(self, x: int, y: int, zoom: float = 1) -> Optional[pygame.Surface]:
cache = self.cache.get(zoom, {})
pos = (x, y)
if pos not in cache:
if zoom == 1:
return None
# Multiple maps per zone
elif zoom < 1:
img = pygame.Surface((self.base_size, self.base_size), pygame.SRCALPHA)
n_maps = int(1 / zoom)
sub_size = self.base_size * zoom
for sub_y in range(n_maps):
for sub_x in range(n_maps):
sub_img = self.get_for_pos(x * n_maps + sub_x, y * n_maps + sub_y, 1)
if sub_img is not None:
sub_img = pygame.transform.scale_by(sub_img, zoom)
img.blit(sub_img, [sub_x * sub_size, sub_y * sub_size])
# Zone is part of a map
else:
img = self.get_for_pos(floor(x / zoom), floor(y / zoom), 1)
if img is not None:
sub_x = x % zoom
sub_y = y % zoom
sub_size = self.base_size / zoom
img = img.subsurface([sub_x * sub_size, sub_y * sub_size, sub_size, sub_size])
img = pygame.transform.scale_by(img, zoom)
print("Generating new image")
cache[pos] = img
self.cache[zoom] = cache
return cache[pos]