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):
@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

View File

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

View File

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

View File

@ -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