My goal
Render a polygon using the World-Entity-Applicator pattern in the example pong.py.
My understanding of rendering a custom polygon
- Create an instance of a sdl2.ext.World
- Create an applicator that filters for a specific class (componenttype)
- Add applicator as a system to the world
- Create an instance of my polygon object, the sdl2.ext.Entity class will add this in the __ new__ method to the world
- When we call process() on the world object, the custom applicator picks up our polygon, and it can render it however it wants to, for example by calling the following
sdl2.sdlgfx.filledPolygonRGBA(renderer.sdlrenderer, x_array, y_array, vertex_count, *color)
The issue
The Applicator doesn't pick up the custom Entity in its component set.
I have written the following code to reproduce the issue. I am expecting it to print out something after "There should be polygons listed here:", but it prints nothing.
import sdl2.ext
class Polygon(sdl2.ext.Entity):
def __init__(self, world, x, y, vertices):
self.position = (x, y)
self.vertices = vertices
class PolygonApplicator(sdl2.ext.Applicator):
def __init__(self):
super().__init__()
self.componenttypes = (Polygon,)
def process(self, world, componentsets):
print("There should be polygons listed here:")
for polygon in componentsets:
# Draw the polygon entity here
print(polygon)
def run():
sdl2.ext.init()
window = sdl2.ext.Window("Polygon Example", size=(640, 480))
window.show()
world = sdl2.ext.World()
world.add_system(PolygonApplicator())
polygon = Polygon(world, 100, 100, [(0, 0), (50, 0), (50, 50), (0, 50)])
running = True
while running:
events = sdl2.ext.get_events()
for event in events:
if event.type == sdl2.SDL_QUIT:
running = False
break
world.process()
window.refresh()
sdl2.ext.quit()
if __name__ == "__main__":
run()