From 62cbbb64e181aeca7cd285e3c5dcd2d38f0c298b Mon Sep 17 00:00:00 2001 From: "Tsunami014 (Max)" Date: Tue, 31 Dec 2024 08:32:23 +1100 Subject: [PATCH] =?UTF-8?q?feat(main):=20=E2=9C=A8=20Improved=20both=20the?= =?UTF-8?q?=20wrap=20demo=20and=20the=20tsetcolldemo!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Made the bottom from a surface to an imageviewer in wrap and added the polygon calculations for tsetcoll --- BlazeSudio/graphics/GUI/elements.py | 11 ++++++++-- BlazeSudio/utils/genCollisions.py | 32 ++++++++++++++++++++++++++++ BlazeSudio/utils/wrap/warp.py | 4 +++- demoFiles/sampleTileset.png | Bin 4654 -> 5012 bytes demos.py | 32 ++++++++++++++++++++++------ 5 files changed, 70 insertions(+), 9 deletions(-) diff --git a/BlazeSudio/graphics/GUI/elements.py b/BlazeSudio/graphics/GUI/elements.py index fc7b74b5..2e2c4fbd 100644 --- a/BlazeSudio/graphics/GUI/elements.py +++ b/BlazeSudio/graphics/GUI/elements.py @@ -695,6 +695,13 @@ def sur(self, newsur): self._sur = newsur self.cache = None + def set(self, newsur): + self.sur = newsur + self.centre() + + def get(self): + return self._sur + def reset(self): self.scroll = 1 self.offset = [self.size[0]/2, self.size[1]/2] @@ -735,8 +742,8 @@ def update(self, mousePos, events): scrolling = any(e.type == pygame.MOUSEWHEEL for e in events) for e in events: if e.type == pygame.MOUSEWHEEL: - self.scrollVel += e.y * 0.5 - self.scrollVel += e.x * 0.5 + self.scrollVel += e.y * 0.25 + self.scrollVel += e.x * 0.25 elif not scrolling and e.type == pygame.MOUSEBUTTONDOWN: self.lastMP = mousePos diff --git a/BlazeSudio/utils/genCollisions.py b/BlazeSudio/utils/genCollisions.py index 4a7b8b64..7ce7b3ce 100644 --- a/BlazeSudio/utils/genCollisions.py +++ b/BlazeSudio/utils/genCollisions.py @@ -1,5 +1,37 @@ import BlazeSudio.collisions as colls +__all__ = [ + "bounding_box", + "approximate_polygon" +] + +def bounding_box(surface): + """ + Simplifies a polygon into 4 points. + + Args: + surface (pygame.Surface): The surface to simplify. + """ + width, height = surface.get_size() + points = [] + for x in range(width): + for y in range(height): + if surface.get_at((x, y)).a > 125: + points.append((x, y)) + if not points: + return None + + xs, ys = zip(*points) + x1 = min(xs) + x2 = max(xs) + y1 = min(ys) + y2 = max(ys) + if x2 == width-1: + x2 = width + if y2 == height-1: + y2 = height + return colls.Polygon((x1, y1), (x2, y1), (x2, y2), (x1, y2)) + def approximate_polygon(surface, tolerance=3, ratio=0.1): # TODO: Seriously boost performance """ Returns a concave polygon that approximates the non-transparent area of a Pygame surface. diff --git a/BlazeSudio/utils/wrap/warp.py b/BlazeSudio/utils/wrap/warp.py index 5fb0c490..cffa1bfa 100644 --- a/BlazeSudio/utils/wrap/warp.py +++ b/BlazeSudio/utils/wrap/warp.py @@ -10,6 +10,7 @@ def lerp2d(p1, p2, f): def draw_quad(surface, quad, img): points = [] + pixel_array = pygame.surfarray.array3d(img) wid, hei = img.get_size() for i in range(hei+1): @@ -23,8 +24,9 @@ def draw_quad(surface, quad, img): for x in range(wid): for y in range(hei): + color = pixel_array[x, y] # (r, g, b) pygame.draw.polygon( surface, - img.get_at((x,y)), + color, [points[b][a] for a, b in [(x,y), (x,y+1), (x+1,y+1), (x+1,y)]] ) diff --git a/demoFiles/sampleTileset.png b/demoFiles/sampleTileset.png index 04458ac50c88d7f5fc5e745d592d64e7fd3603d6..6b4cac389112d9c8f75d27bbe00db5620179bc55 100644 GIT binary patch delta 1168 zcmZ3dGDTgnGr-TCmrII^fq{Y7)59f*fq{`3$X4KB1CnC%WS>t|lwx9LoM;=w$TV?+ zGp`3wm=TD<^u!0U>?T%*MpgzUlUW!o>#f84+=0?9na<7up3cq+1x5L3nK`Kp3>9-~ zC)#=(4iIVezucuH%5L2eaw1^vn?*`}Te)0U2xPT}$@;|oV)vDrBqHv9aO;By=aa4; zJQ^9!9_}dchq17%AgE-b(0gw#-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