add DrawEngine methods to initialize and render

This commit is contained in:
James Turk 2024-04-23 21:37:25 -05:00
parent 37343739e8
commit 39c9c53bdc
4 changed files with 25 additions and 15 deletions

View File

@ -2,6 +2,14 @@ import abc
class DrawEngine(abc.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 @abc.abstractmethod
def circle_draw(self, screen): def circle_draw(self, screen):
pass pass
@ -13,5 +21,3 @@ class DrawEngine(abc.ABC):
@abc.abstractmethod @abc.abstractmethod
def line_draw(self, screen): def line_draw(self, screen):
pass pass

View File

@ -1,6 +1,5 @@
import math import math
import random import random
import pygame
from typing import Callable from typing import Callable
from .doodles import Doodle from .doodles import Doodle
from .world import world from .world import world

View File

@ -1,5 +1,4 @@
import random import random
import pygame
from .doodles import Doodle from .doodles import Doodle
from .world import world from .world import world

View File

@ -5,8 +5,19 @@ from .draw_engine import DrawEngine
class PygameDrawEngine(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"): 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"): def rect_draw(self, r: "Rectangle"):
# TODO: make accessors # TODO: make accessors
@ -16,11 +27,10 @@ class PygameDrawEngine(DrawEngine):
r._width, r._width,
r._height, r._height,
) )
pygame.draw.rect(world.buffer, r.rgba, rect) pygame.draw.rect(self.buffer, r.rgba, rect)
def line_draw(self, ll: "Line"): def line_draw(self, ll: "Line"):
print("line_draw", ll) pygame.draw.aaline(self.buffer, ll.rgba, ll.world_vec, ll.end_vec)
pygame.draw.aaline(world.buffer, ll.rgba, ll.world_vec, ll.end_vec)
class World: class World:
@ -79,10 +89,9 @@ class World:
if self.screen: if self.screen:
raise ValueError("Can't initialize world twice!") raise ValueError("Can't initialize world twice!")
pygame.init() 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.clock = pygame.time.Clock()
self._elapsed = 0 self._elapsed = 0
self.draw_engine.init()
def clear(self): def clear(self):
self._drawables = [] self._drawables = []
@ -105,11 +114,8 @@ class World:
self.tick() self.tick()
# rendering # rendering
self.buffer.fill((*self.background_color, 255)) self.draw_engine.render(self.background_color, self._drawables)
for d in sorted(self._drawables, key=lambda d: d._z_index):
d.draw()
self.screen.blit(self.buffer, (0, 0))
pygame.display.flip()
# our singleton instance # our singleton instance