add DrawEngine methods to initialize and render
This commit is contained in:
parent
37343739e8
commit
39c9c53bdc
@ -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
|
||||
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
import math
|
||||
import random
|
||||
import pygame
|
||||
from typing import Callable
|
||||
from .doodles import Doodle
|
||||
from .world import world
|
||||
|
@ -1,5 +1,4 @@
|
||||
import random
|
||||
import pygame
|
||||
from .doodles import Doodle
|
||||
from .world import world
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user