Skip to content

Add RenderWindow class for Pygame window management - #10

Draft
dmccoystephenson with Copilot wants to merge 5 commits into
mainfrom
copilot/fix-b699f03e-5c89-442f-80a1-38253693307d
Draft

Add RenderWindow class for Pygame window management#10
dmccoystephenson with Copilot wants to merge 5 commits into
mainfrom
copilot/fix-b699f03e-5c89-442f-80a1-38253693307d

Conversation

Copilot AI commented Oct 2, 2025

Copy link
Copy Markdown

Overview

This PR adds a new RenderWindow class that encapsulates Pygame initialization and window management, providing a clean and modular interface for rendering applications.

What's New

RenderWindow Class

A new render_window.py module provides the RenderWindow class with the following features:

  • Automatic Pygame Setup: Handles pygame.init(), window creation, and display configuration
  • Simple API: Clean methods for common operations:
    • get_surface() - Access the display surface for rendering
    • should_continue() - Process events and check if the window should remain open
    • tick(fps) - Control frame rate
    • register_event_handler(handler) - Add custom event handlers
  • Built-in QUIT Handling: Automatically handles window close events internally
  • Composition-Friendly: Designed to be used as a component rather than requiring inheritance

Usage Examples and Documentation

Added render_window_example.py with three complete working examples demonstrating:

  • Simple Example: Basic RenderWindow initialization and usage
  • Event Handler Example: Custom event registration and handling
  • Integration Example: Shows how to refactor main.py to use RenderWindow with the existing Graphik class

Updated README.md with a dedicated RenderWindow section including feature overview, basic usage example, and reference to the example file.

Usage Example

from render_window import RenderWindow
import pygame

# Create window with title and dimensions
window = RenderWindow("My Application", 800, 600)
surface = window.get_surface()

# Register custom event handlers
def handle_keyboard(event):
    if event.type == pygame.KEYDOWN:
        print(f"Key pressed: {event.key}")

window.register_event_handler(handle_keyboard)

# Main application loop
while window.should_continue():
    surface.fill((50, 50, 100))
    # ... render your content ...
    pygame.display.update()
    window.tick(60)  # 60 FPS

pygame.quit()

Composition Pattern

The class is designed for composition, making it easy to integrate into existing applications:

class MyGame:
    def __init__(self):
        self.window = RenderWindow("My Game", 640, 480)
        self.surface = self.window.get_surface()
        self.window.register_event_handler(self.on_input)
    
    def run(self):
        while self.window.should_continue():
            # ... game logic ...
            self.window.tick(60)

Benefits

  • Reduced Boilerplate: No need to manually initialize Pygame or handle window setup
  • Modular: Clean separation between window management and application logic
  • Flexible: Multiple event handlers can be registered and managed independently
  • Testable: Easy to mock and test without requiring a display
  • Ready for Apex: Provides the foundation requested for use in the Apex project
  • Well-Documented: Includes comprehensive examples and documentation

Additional Changes

  • Updated .gitignore to exclude __pycache__/ directories
  • Added render_window_example.py with three usage examples
  • Updated README.md with RenderWindow documentation

Testing

The implementation has been thoroughly tested with:

  • Window initialization with various sizes
  • Surface access and rendering operations
  • Event handling (keyboard, mouse, custom events)
  • Multiple event handler registration
  • Frame rate control
  • QUIT event handling
  • Composition pattern verification
  • Integration with existing Graphik class

Resolves the issue requesting a RenderWindow class for managing Pygame initialization and enables modular, flexible use across multiple projects.

Original prompt

This section details on the original issue you should resolve

<issue_title>Create a RenderWindow class to manage Pygame initialization</issue_title>
<issue_description>Patchwork should provide a RenderWindow class that encapsulates Pygame setup logic and manages the window, surface, event loop, and frame rate.

The class should be designed for composition, not inheritance, so that clients like Apex can use it as a container for rendering rather than subclassing it.

Key Features

  • Initialize Pygame with title and window size
  • Expose get_surface(), tick(fps), and should_continue() methods
  • Allow registration of custom event handlers with register_event_handler(handler)
  • Call each handler on events, while handling QUIT internally

This enables modular, flexible use in multiple projects.</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #6

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits October 2, 2025 05:12
Co-authored-by: dmccoystephenson <21204351+dmccoystephenson@users.noreply.github.com>
Co-authored-by: dmccoystephenson <21204351+dmccoystephenson@users.noreply.github.com>
Copilot AI changed the title [WIP] Create a RenderWindow class to manage Pygame initialization Add RenderWindow class for Pygame window management Oct 2, 2025
Copilot AI requested a review from dmccoystephenson October 2, 2025 05:18

@dmccoystephenson dmccoystephenson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot The new class does not appear to be used anywhere

Co-authored-by: dmccoystephenson <21204351+dmccoystephenson@users.noreply.github.com>

Copilot AI commented Oct 2, 2025

Copy link
Copy Markdown
Author

@copilot The new class does not appear to be used anywhere

Added usage examples in render_window_example.py demonstrating integration with the existing Graphik class, and updated README with documentation. The example shows how to use RenderWindow in the Patchwork context, including how main.py could be refactored. Commit: ee5ffb0

Introduces this repo's first tests/ directory. Covers init, tick,
event dispatch, and QUIT handling for the RenderWindow class added
in this PR, without opening a real display.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@dmccoystephenson

Copy link
Copy Markdown
Member

Self-review rubric (dev-loop adoption of this draft PR):

  • Scope: PASS — all changed files (.gitignore, README.md, render_window.py, render_window_example.py, tests/test_render_window.py) are necessary for RenderWindow (Closes Create a RenderWindow class to manage Pygame initialization #6); no unrelated churn.
  • Tests-new: PASS — added tests/test_render_window.py (this repo's first tests/ dir), covering __init__'s pygame setup, tick, should_continue (event dispatch + internal QUIT handling), and that QUIT events don't reach registered handlers. pygame is fully mocked via unittest.mock.patch("render_window.pygame") — no real display opened.
  • Tests-fix: n/a — this is new functionality, not a bug fix.
  • Sibling structure: PASS — render_window.py follows the existing single-class-per-file convention seen in graphik.py.
  • Docs: PASS — README's new "RenderWindow" section accurately reflects the shipped API (get_surface, tick, should_continue, register_event_handler); example code in the README matches render_window_example.py.
  • Issue resolution: PASS — issue Create a RenderWindow class to manage Pygame initialization #6 asked for composition-friendly Pygame init/window/event-loop/frame-rate management with get_surface(), tick(fps), should_continue(), and register_event_handler(handler) with internal QUIT handling — all present and verified.
  • Manual validation: PASS —
    • python3 -m py_compile main.py graphik.py __init__.py render_window.py render_window_example.py tests/test_render_window.py
    • python3 -m unittest discover -s tests → 5/5 passed
    • Functional smoke test with SDL_VIDEODRIVER=dummy: constructed a real RenderWindow, posted a KEYDOWN event (confirmed it reaches a registered handler) and a QUIT event (confirmed should_continue() then returns False).

No functional changes were made to the copilot-authored implementation — I rebased onto current main (already a fast-forward, no conflicts) and added the missing unit test coverage before review.

One blocker for merge: this PR is still marked Draft. gh pr ready was not permitted in this run's tooling, so I could not undraft it myself. @dmccoystephenson — marking it ready for review (or merging directly once ready) is the only remaining step.

@dmccoystephenson

Copy link
Copy Markdown
Member

A status update is provided on the two outstanding blockers for this PR.

The CHANGES_REQUESTED review ("The new class does not appear to be used anywhere") was left on commit 647eb64. That concern was addressed in a later commit (ee5ffb0), which added render_window_example.py with usage examples, including one showing how main.py could be refactored to use RenderWindow alongside Graphik. Re-review of that commit, and either dismissal or re-approval of the stale review, is left to a maintainer, since automated re-approval of a human-authored review is out of scope for this run.

The PR also remains marked as a Draft. gh pr ready was attempted in this run and was denied by the harness's permission gate, with no interactive approval available in this headless dispatch. Marking the PR ready for review (or merging directly once ready) is left as a manual step.

No code changes were made in this pass; prior test coverage and self-review (see above) are believed to still hold, since no new commits landed on this PR since they were posted.

This PR comment was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create a RenderWindow class to manage Pygame initialization

2 participants