Skip to content

Create Render Commands #38

@Brian-Jiang

Description

@Brian-Jiang

The render command class is used to store a single render command entry internally. The render command should be used instead of directly calling pygame API, it can draw a primitive shape, clear the render target, and perform all other operations related to rendering. The render command should work with all types of visual engine, including no visual.
Below is a simple implementation of render command to show how to use it.

from enum import Enum, auto

class OpCode(Enum):
    ClearRenderTarget = auto()
    Draw = auto()

class RenderShape(Enum):
    Triangle = auto()
    Rectangle = auto()
    Circle = auto()

class RenderCommand:
    """
    This represent an instance of a render command
    """
    
    def __init__(self):
        # opCode is the operation code for the command
        self.opCode = None

        # data is a tuple that contain all parameters for this opCode for the command
        self.data: tuple = None

    def validate(self) -> int:
        '''Do some validation on the data'''
        pass

    def __str__(self):
        '''Can used for debugging'''
        return f"RenderCommand({self.opCode}, {self.data})"

    @staticmethod
    def circle(x: int, y: int, radius: int, color: str) -> 'RenderCommand':
        '''Create a circle render command'''
        cmd = RenderCommand()
        cmd.opCode = OpCode.Draw
        cmd.data = (RenderShape.Circle, x, y, radius, color)
        return cmd

    @staticmethod
    def clear(color: str) -> 'RenderCommand':
        '''Create a clear render command'''
        cmd = RenderCommand()
        cmd.opCode = OpCode.ClearRenderTarget
        cmd.data = (color,)
        return cmd
    
    # More static methods for other shapes

# On each frame, create a list of render commands
render_command_list = []
render_command_list.append(RenderCommand.clear('white'))
render_command_list.append(RenderCommand.circle(0, 0, 10, 'red'))
# Add more render commands

# To execute all commands
RenderManager.execute(render_command_list)

# To debug or log the commands
RenderManager.log(render_command_list, 'xxx.log')

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions