diff --git a/src/doodles/draw_engine.py b/src/doodles/draw_engine.py index a362f3f..b7908cc 100644 --- a/src/doodles/draw_engine.py +++ b/src/doodles/draw_engine.py @@ -2,6 +2,14 @@ import abc class DrawEngine(abc.ABC): + @abc.abstractmethod + def init(self): + pass + + @abc.abstractmethod + def render(self, background_color: "Color", drawables: list["Doodle"]): + pass + @abc.abstractmethod def circle_draw(self, screen): pass @@ -13,5 +21,3 @@ class DrawEngine(abc.ABC): @abc.abstractmethod def line_draw(self, screen): pass - - diff --git a/src/doodles/lines.py b/src/doodles/lines.py index 8df0f96..42ea444 100644 --- a/src/doodles/lines.py +++ b/src/doodles/lines.py @@ -1,6 +1,5 @@ import math import random -import pygame from typing import Callable from .doodles import Doodle from .world import world diff --git a/src/doodles/shapes.py b/src/doodles/shapes.py index 8cdae99..426c908 100644 --- a/src/doodles/shapes.py +++ b/src/doodles/shapes.py @@ -1,5 +1,4 @@ import random -import pygame from .doodles import Doodle from .world import world diff --git a/src/doodles/world.py b/src/doodles/world.py index 9a10f57..9d8c12e 100644 --- a/src/doodles/world.py +++ b/src/doodles/world.py @@ -5,8 +5,19 @@ from .draw_engine import DrawEngine class PygameDrawEngine(DrawEngine): + def init(self): + self.screen = pygame.display.set_mode((world.WIDTH, world.HEIGHT)) + self.buffer = pygame.Surface((world.WIDTH, world.HEIGHT), pygame.SRCALPHA) + + def render(self, background_color: Color, drawables: list["Doodle"]): + self.buffer.fill((*background_color, 255)) + for d in sorted(drawables, key=lambda d: d._z_index): + d.draw() + self.screen.blit(self.buffer, (0, 0)) + pygame.display.flip() + def circle_draw(self, c: "Circle"): - pygame.draw.circle(world.buffer, c.rgba, c.world_vec, c.radius_val) + pygame.draw.circle(self.buffer, c.rgba, c.world_vec, c.radius_val) def rect_draw(self, r: "Rectangle"): # TODO: make accessors @@ -16,11 +27,10 @@ class PygameDrawEngine(DrawEngine): r._width, r._height, ) - pygame.draw.rect(world.buffer, r.rgba, rect) + pygame.draw.rect(self.buffer, r.rgba, rect) def line_draw(self, ll: "Line"): - print("line_draw", ll) - pygame.draw.aaline(world.buffer, ll.rgba, ll.world_vec, ll.end_vec) + pygame.draw.aaline(self.buffer, ll.rgba, ll.world_vec, ll.end_vec) class World: @@ -79,10 +89,9 @@ class World: if self.screen: raise ValueError("Can't initialize world twice!") pygame.init() - self.screen = pygame.display.set_mode((world.WIDTH, world.HEIGHT)) - self.buffer = pygame.Surface((world.WIDTH, world.HEIGHT), pygame.SRCALPHA) self.clock = pygame.time.Clock() self._elapsed = 0 + self.draw_engine.init() def clear(self): self._drawables = [] @@ -105,11 +114,8 @@ class World: self.tick() # rendering - self.buffer.fill((*self.background_color, 255)) - for d in sorted(self._drawables, key=lambda d: d._z_index): - d.draw() - self.screen.blit(self.buffer, (0, 0)) - pygame.display.flip() + self.draw_engine.render(self.background_color, self._drawables) + # our singleton instance