Author: John McCardle <mccardle.john@gmail.com>
Co-Authored-By: Claude <noreply@anthropic.com>
commit dc47f2474c7b2642d368f9772894aed857527807
the UIEntity rant
commit 673ca8e1b089ea670257fc04ae1a676ed95a40ed
I forget when these tests were written, but I want them in the squash merge
commit 70c71565c684fa96e222179271ecb13a156d80ad
Fix UI object segfault by switching from managed to manual weakref management
The UI types (Frame, Caption, Sprite, Grid, Entity) were using
Py_TPFLAGS_MANAGED_WEAKREF while also trying to manually create weakrefs
for the PythonObjectCache. This is fundamentally incompatible - when
Python manages weakrefs internally, PyWeakref_NewRef() cannot access the
weakref list properly, causing segfaults.
Changed all UI types to use manual weakref management (like PyTimer):
- Restored weakreflist field in all UI type structures
- Removed Py_TPFLAGS_MANAGED_WEAKREF from all UI type flags
- Added tp_weaklistoffset for all UI types in module initialization
- Initialize weakreflist=NULL in tp_new and init methods
- Call PyObject_ClearWeakRefs() in dealloc functions
This allows the PythonObjectCache to continue working correctly,
maintaining Python object identity for C++ objects across the boundary.
Fixes segfault when creating UI objects (e.g., Caption, Grid) that was
preventing tutorial scripts from running.
This is the bulk of the required behavior for Issue #126.
that issure isn't ready for closure yet; several other sub-issues left.
closes #110
mention issue #109 - resolves some __init__ related nuisances
commit 3dce3ec539ae99e32d869007bf3f49d03e4e2f89
Refactor timer system for cleaner architecture and enhanced functionality
Major improvements to the timer system:
- Unified all timer logic in the Timer class (C++)
- Removed PyTimerCallable subclass, now using PyCallable directly
- Timer objects are now passed to callbacks as first argument
- Added 'once' parameter for one-shot timers that auto-stop
- Implemented proper PythonObjectCache integration with weakref support
API enhancements:
- New callback signature: callback(timer, runtime) instead of just (runtime)
- Timer objects expose: name, interval, remaining, paused, active, once properties
- Methods: pause(), resume(), cancel(), restart()
- Comprehensive documentation with examples
- Enhanced repr showing timer state (active/paused/once/remaining time)
This cleanup follows the UIEntity/PyUIEntity pattern and makes the timer
system more Pythonic while maintaining backward compatibility through
the legacy setTimer/delTimer API.
closes #121
commit 145834cfc31b8dabc4cb3591b9cb4ed99fc8b964
Implement Python object cache to preserve derived types in collections
Add a global cache system that maintains weak references to Python objects,
ensuring that derived Python classes maintain their identity when stored in
and retrieved from C++ collections.
Key changes:
- Add PythonObjectCache singleton with serial number system
- Each cacheable object (UIDrawable, UIEntity, Timer, Animation) gets unique ID
- Cache stores weak references to prevent circular reference memory leaks
- Update all UI type definitions to support weak references (Py_TPFLAGS_MANAGED_WEAKREF)
- Enable subclassing for all UI types (Py_TPFLAGS_BASETYPE)
- Collections check cache before creating new Python wrappers
- Register objects in cache during __init__ methods
- Clean up cache entries in C++ destructors
This ensures that Python code like:
```python
class MyFrame(mcrfpy.Frame):
def __init__(self):
super().__init__()
self.custom_data = "preserved"
frame = MyFrame()
scene.ui.append(frame)
retrieved = scene.ui[0] # Same MyFrame instance with custom_data intact
```
Works correctly, with retrieved maintaining the derived type and custom attributes.
Closes #112
commit c5e7e8e298
Update test demos for new Python API and entity system
- Update all text input demos to use new Entity constructor signature
- Fix pathfinding showcase to work with new entity position handling
- Remove entity_waypoints tracking in favor of simplified movement
- Delete obsolete exhaustive_api_demo.py (superseded by newer demos)
- Adjust entity creation calls to match Entity((x, y), texture, sprite_index) pattern
commit 6d29652ae7
Update animation demo suite with crash fixes and improvements
- Add warnings about AnimationManager segfault bug in sizzle_reel_final.py
- Create sizzle_reel_final_fixed.py that works around the crash by hiding objects instead of removing them
- Increase font sizes for better visibility in demos
- Extend demo durations for better showcase of animations
- Remove debug prints from animation_sizzle_reel_working.py
- Minor cleanup and improvements to all animation demos
commit a010e5fa96
Update game scripts for new Python API
- Convert entity position access from tuple to x/y properties
- Update caption size property to font_size
- Fix grid boundary checks to use grid_size instead of exceptions
- Clean up demo timer on menu exit to prevent callbacks
These changes adapt the game scripts to work with the new standardized
Python API constructors and property names.
commit 9c8d6c4591
Fix click event z-order handling in PyScene
Changed click detection to properly respect z-index by:
- Sorting ui_elements in-place when needed (same as render order)
- Using reverse iterators to check highest z-index elements first
- This ensures top-most elements receive clicks before lower ones
commit dcd1b0ca33
Add roguelike tutorial implementation files
Implement Parts 0-2 of the classic roguelike tutorial adapted for McRogueFace:
- Part 0: Basic grid setup and tile rendering
- Part 1: Drawing '@' symbol and basic movement
- Part 1b: Variant with sprite-based player
- Part 2: Entity system and NPC implementation with three movement variants:
- part_2.py: Standard implementation
- part_2-naive.py: Naive movement approach
- part_2-onemovequeued.py: Queued movement system
Includes tutorial assets:
- tutorial2.png: Tileset for dungeon tiles
- tutorial_hero.png: Player sprite sheet
commit 6813fb5129
Standardize Python API constructors and remove PyArgHelpers
- Remove PyArgHelpers.h and all macro-based argument parsing
- Convert all UI class constructors to use PyArg_ParseTupleAndKeywords
- Standardize constructor signatures across UICaption, UIEntity, UIFrame, UIGrid, and UISprite
- Replace PYARGHELPER_SINGLE/MULTI macros with explicit argument parsing
- Improve error messages and argument validation
- Maintain backward compatibility with existing Python code
This change improves code maintainability and consistency across the Python API.
commit 6f67fbb51e
Fix animation callback crashes from iterator invalidation (#119)
Resolved segfaults caused by creating new animations from within
animation callbacks. The issue was iterator invalidation in
AnimationManager::update() when callbacks modified the active
animations vector.
Changes:
- Add deferred animation queue to AnimationManager
- New animations created during update are queued and added after
- Set isUpdating flag to track when in update loop
- Properly handle Animation destructor during callback execution
- Add clearCallback() method for safe cleanup scenarios
This fixes the "free(): invalid pointer" and "malloc(): unaligned
fastbin chunk detected" errors that occurred with rapid animation
creation in callbacks.
commit eb88c7b3aa
Add animation completion callbacks (#119)
Implement callbacks that fire when animations complete, enabling direct
causality between animation end and game state changes. This eliminates
race conditions from parallel timer workarounds.
- Add optional callback parameter to Animation constructor
- Callbacks execute synchronously when animation completes
- Proper Python reference counting with GIL safety
- Callbacks receive (anim, target) parameters (currently None)
- Exception handling prevents crashes from Python errors
Example usage:
```python
def on_complete(anim, target):
player_moving = False
anim = mcrfpy.Animation("x", 300.0, 1.0, "easeOut", callback=on_complete)
anim.start(player)
```
closes #119
commit 9fb428dd01
Update ROADMAP with GitHub issue numbers (#111-#125)
Added issue numbers from GitHub tracker to roadmap items:
- #111: Grid Click Events Broken in Headless
- #112: Object Splitting Bug (Python type preservation)
- #113: Batch Operations for Grid
- #114: CellView API
- #115: SpatialHash Implementation
- #116: Dirty Flag System
- #117: Memory Pool for Entities
- #118: Scene as Drawable
- #119: Animation Completion Callbacks
- #120: Animation Property Locking
- #121: Timer Object System
- #122: Parent-Child UI System
- #123: Grid Subgrid System
- #124: Grid Point Animation
- #125: GitHub Issues Automation
Also updated existing references:
- #101/#110: Constructor standardization
- #109: Vector class indexing
Note: Tutorial-specific items and Python-implementable features
(input queue, collision reservation) are not tracked as engine issues.
commit 062e4dadc4
Fix animation segfaults with RAII weak_ptr implementation
Resolved two critical segmentation faults in AnimationManager:
1. Race condition when creating multiple animations in timer callbacks
2. Exit crash when animations outlive their target objects
Changes:
- Replace raw pointers with std::weak_ptr for automatic target invalidation
- Add Animation::complete() to jump animations to final value
- Add Animation::hasValidTarget() to check if target still exists
- Update AnimationManager to auto-remove invalid animations
- Add AnimationManager::clear() call to GameEngine::cleanup()
- Update Python bindings to pass shared_ptr instead of raw pointers
This ensures animations can never reference destroyed objects, following
proper RAII principles. Tested with sizzle_reel_final.py and stress
tests creating/destroying hundreds of animated objects.
commit 98fc49a978
Directory structure cleanup and organization overhaul
307 lines
No EOL
9.8 KiB
Python
307 lines
No EOL
9.8 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
McRogueFace Animation Sizzle Reel v2
|
|
====================================
|
|
|
|
Fixed version with proper API usage for animations and collections.
|
|
"""
|
|
|
|
import mcrfpy
|
|
|
|
# Configuration
|
|
SCENE_WIDTH = 1280
|
|
SCENE_HEIGHT = 720
|
|
DEMO_DURATION = 5.0 # Duration for each demo section
|
|
|
|
# All available easing functions
|
|
EASING_FUNCTIONS = [
|
|
"linear", "easeIn", "easeOut", "easeInOut",
|
|
"easeInQuad", "easeOutQuad", "easeInOutQuad",
|
|
"easeInCubic", "easeOutCubic", "easeInOutCubic",
|
|
"easeInQuart", "easeOutQuart", "easeInOutQuart",
|
|
"easeInSine", "easeOutSine", "easeInOutSine",
|
|
"easeInExpo", "easeOutExpo", "easeInOutExpo",
|
|
"easeInCirc", "easeOutCirc", "easeInOutCirc",
|
|
"easeInElastic", "easeOutElastic", "easeInOutElastic",
|
|
"easeInBack", "easeOutBack", "easeInOutBack",
|
|
"easeInBounce", "easeOutBounce", "easeInOutBounce"
|
|
]
|
|
|
|
# Track current demo state
|
|
current_demo = 0
|
|
subtitle = None
|
|
demo_objects = [] # Track objects from current demo
|
|
|
|
def create_demo_scene():
|
|
"""Create the main demo scene with title"""
|
|
mcrfpy.createScene("sizzle_reel")
|
|
mcrfpy.setScene("sizzle_reel")
|
|
|
|
ui = mcrfpy.sceneUI("sizzle_reel")
|
|
|
|
# Title caption
|
|
title = mcrfpy.Caption("McRogueFace Animation Sizzle Reel",
|
|
SCENE_WIDTH/2 - 200, 20)
|
|
title.fill_color = mcrfpy.Color(255, 255, 0)
|
|
title.outline = 2
|
|
title.outline_color = mcrfpy.Color(0, 0, 0)
|
|
ui.append(title)
|
|
|
|
# Subtitle showing current demo
|
|
global subtitle
|
|
subtitle = mcrfpy.Caption("Initializing...",
|
|
SCENE_WIDTH/2 - 150, 60)
|
|
subtitle.fill_color = mcrfpy.Color(200, 200, 200)
|
|
ui.append(subtitle)
|
|
|
|
return ui
|
|
|
|
def demo_frame_basic_animations():
|
|
"""Demo 1: Basic frame animations - position, size, colors"""
|
|
global demo_objects
|
|
demo_objects = []
|
|
|
|
ui = mcrfpy.sceneUI("sizzle_reel")
|
|
subtitle.text = "Demo 1: Frame Basic Animations (Position, Size, Colors)"
|
|
|
|
# Create test frame
|
|
frame = mcrfpy.Frame(100, 150, 200, 100)
|
|
frame.fill_color = mcrfpy.Color(50, 50, 150)
|
|
frame.outline = 3
|
|
frame.outline_color = mcrfpy.Color(255, 255, 255)
|
|
ui.append(frame)
|
|
demo_objects.append(frame)
|
|
|
|
# Position animations with different easings
|
|
x_anim = mcrfpy.Animation("x", 800.0, 2.0, "easeInOutBack")
|
|
y_anim = mcrfpy.Animation("y", 400.0, 2.0, "easeInOutElastic")
|
|
x_anim.start(frame)
|
|
y_anim.start(frame)
|
|
|
|
# Size animations
|
|
w_anim = mcrfpy.Animation("w", 400.0, 3.0, "easeInOutCubic")
|
|
h_anim = mcrfpy.Animation("h", 200.0, 3.0, "easeInOutCubic")
|
|
w_anim.start(frame)
|
|
h_anim.start(frame)
|
|
|
|
# Color animations - use tuples instead of Color objects
|
|
fill_anim = mcrfpy.Animation("fill_color", (255, 100, 50, 200), 4.0, "easeInOutSine")
|
|
outline_anim = mcrfpy.Animation("outline_color", (0, 255, 255, 255), 4.0, "easeOutBounce")
|
|
fill_anim.start(frame)
|
|
outline_anim.start(frame)
|
|
|
|
# Outline thickness animation
|
|
thickness_anim = mcrfpy.Animation("outline", 10.0, 4.5, "easeInOutQuad")
|
|
thickness_anim.start(frame)
|
|
|
|
def demo_caption_animations():
|
|
"""Demo 2: Caption text animations and effects"""
|
|
global demo_objects
|
|
demo_objects = []
|
|
|
|
ui = mcrfpy.sceneUI("sizzle_reel")
|
|
subtitle.text = "Demo 2: Caption Animations (Text, Color, Position)"
|
|
|
|
# Basic caption with position animation
|
|
caption1 = mcrfpy.Caption("Moving Text!", 100, 200)
|
|
caption1.fill_color = mcrfpy.Color(255, 255, 255)
|
|
caption1.outline = 1
|
|
ui.append(caption1)
|
|
demo_objects.append(caption1)
|
|
|
|
# Animate across screen with bounce
|
|
x_anim = mcrfpy.Animation("x", 900.0, 3.0, "easeOutBounce")
|
|
x_anim.start(caption1)
|
|
|
|
# Color cycling caption
|
|
caption2 = mcrfpy.Caption("Rainbow Colors", 400, 300)
|
|
caption2.outline = 2
|
|
ui.append(caption2)
|
|
demo_objects.append(caption2)
|
|
|
|
# Cycle through colors using tuples
|
|
color_anim1 = mcrfpy.Animation("fill_color", (255, 0, 0, 255), 1.0, "linear")
|
|
color_anim1.start(caption2)
|
|
|
|
# Schedule color changes
|
|
def change_to_green(rt):
|
|
color_anim2 = mcrfpy.Animation("fill_color", (0, 255, 0, 255), 1.0, "linear")
|
|
color_anim2.start(caption2)
|
|
|
|
def change_to_blue(rt):
|
|
color_anim3 = mcrfpy.Animation("fill_color", (0, 0, 255, 255), 1.0, "linear")
|
|
color_anim3.start(caption2)
|
|
|
|
def change_to_white(rt):
|
|
color_anim4 = mcrfpy.Animation("fill_color", (255, 255, 255, 255), 1.0, "linear")
|
|
color_anim4.start(caption2)
|
|
|
|
mcrfpy.setTimer("color2", change_to_green, 1000)
|
|
mcrfpy.setTimer("color3", change_to_blue, 2000)
|
|
mcrfpy.setTimer("color4", change_to_white, 3000)
|
|
|
|
# Typewriter effect caption
|
|
caption3 = mcrfpy.Caption("", 100, 400)
|
|
caption3.fill_color = mcrfpy.Color(0, 255, 255)
|
|
ui.append(caption3)
|
|
demo_objects.append(caption3)
|
|
|
|
typewriter = mcrfpy.Animation("text", "This text appears one character at a time...", 3.0, "linear")
|
|
typewriter.start(caption3)
|
|
|
|
def demo_easing_showcase():
|
|
"""Demo 3: Showcase different easing functions"""
|
|
global demo_objects
|
|
demo_objects = []
|
|
|
|
ui = mcrfpy.sceneUI("sizzle_reel")
|
|
subtitle.text = "Demo 3: Easing Functions Showcase"
|
|
|
|
# Create small frames for each easing function
|
|
frames_per_row = 6
|
|
frame_width = 180
|
|
spacing = 10
|
|
|
|
# Show first 12 easings
|
|
for i, easing in enumerate(EASING_FUNCTIONS[:12]):
|
|
row = i // frames_per_row
|
|
col = i % frames_per_row
|
|
|
|
x = 50 + col * (frame_width + spacing)
|
|
y = 150 + row * (80 + spacing)
|
|
|
|
# Create indicator frame
|
|
frame = mcrfpy.Frame(x, y, 20, 20)
|
|
frame.fill_color = mcrfpy.Color(100, 200, 255)
|
|
frame.outline = 1
|
|
ui.append(frame)
|
|
demo_objects.append(frame)
|
|
|
|
# Label
|
|
label = mcrfpy.Caption(easing[:8], x, y - 20) # Truncate long names
|
|
label.fill_color = mcrfpy.Color(200, 200, 200)
|
|
ui.append(label)
|
|
demo_objects.append(label)
|
|
|
|
# Animate using this easing
|
|
move_anim = mcrfpy.Animation("x", float(x + frame_width - 20), 3.0, easing)
|
|
move_anim.start(frame)
|
|
|
|
def demo_performance_stress_test():
|
|
"""Demo 4: Performance test with many simultaneous animations"""
|
|
global demo_objects
|
|
demo_objects = []
|
|
|
|
ui = mcrfpy.sceneUI("sizzle_reel")
|
|
subtitle.text = "Demo 4: Performance Test (50+ Simultaneous Animations)"
|
|
|
|
# Create many small objects with different animations
|
|
num_objects = 50
|
|
|
|
for i in range(num_objects):
|
|
# Starting position
|
|
x = 100 + (i % 10) * 100
|
|
y = 150 + (i // 10) * 80
|
|
|
|
# Create small frame
|
|
size = 20 + (i % 3) * 10
|
|
frame = mcrfpy.Frame(x, y, size, size)
|
|
|
|
# Random color
|
|
r = (i * 37) % 256
|
|
g = (i * 73) % 256
|
|
b = (i * 113) % 256
|
|
frame.fill_color = mcrfpy.Color(r, g, b, 200)
|
|
frame.outline = 1
|
|
ui.append(frame)
|
|
demo_objects.append(frame)
|
|
|
|
# Random animation properties
|
|
target_x = 100 + (i % 8) * 120
|
|
target_y = 150 + (i // 8) * 100
|
|
duration = 2.0 + (i % 30) * 0.1
|
|
easing = EASING_FUNCTIONS[i % len(EASING_FUNCTIONS)]
|
|
|
|
# Start multiple animations per object
|
|
x_anim = mcrfpy.Animation("x", float(target_x), duration, easing)
|
|
y_anim = mcrfpy.Animation("y", float(target_y), duration, easing)
|
|
opacity_anim = mcrfpy.Animation("opacity", 0.3 + (i % 7) * 0.1, duration, "easeInOutSine")
|
|
|
|
x_anim.start(frame)
|
|
y_anim.start(frame)
|
|
opacity_anim.start(frame)
|
|
|
|
# Performance counter
|
|
perf_caption = mcrfpy.Caption(f"Animating {num_objects * 3} properties simultaneously", 350, 600)
|
|
perf_caption.fill_color = mcrfpy.Color(255, 255, 0)
|
|
ui.append(perf_caption)
|
|
demo_objects.append(perf_caption)
|
|
|
|
def clear_scene():
|
|
"""Clear the scene except title and subtitle"""
|
|
global demo_objects
|
|
ui = mcrfpy.sceneUI("sizzle_reel")
|
|
|
|
# Remove all demo objects
|
|
for obj in demo_objects:
|
|
try:
|
|
# Find index of object
|
|
for i in range(len(ui)):
|
|
if ui[i] is obj:
|
|
ui.remove(ui[i])
|
|
break
|
|
except:
|
|
pass # Object might already be removed
|
|
|
|
demo_objects = []
|
|
|
|
# Clean up any timers
|
|
for timer_name in ["color2", "color3", "color4"]:
|
|
try:
|
|
mcrfpy.delTimer(timer_name)
|
|
except:
|
|
pass
|
|
|
|
def run_demo_sequence(runtime):
|
|
"""Run through all demos"""
|
|
global current_demo
|
|
|
|
# Clear previous demo
|
|
clear_scene()
|
|
|
|
# Demo list
|
|
demos = [
|
|
demo_frame_basic_animations,
|
|
demo_caption_animations,
|
|
demo_easing_showcase,
|
|
demo_performance_stress_test
|
|
]
|
|
|
|
if current_demo < len(demos):
|
|
# Run current demo
|
|
demos[current_demo]()
|
|
current_demo += 1
|
|
|
|
# Schedule next demo
|
|
if current_demo < len(demos):
|
|
mcrfpy.setTimer("next_demo", run_demo_sequence, int(DEMO_DURATION * 1000))
|
|
else:
|
|
# Final demo completed
|
|
def show_complete(rt):
|
|
subtitle.text = "Animation Showcase Complete!"
|
|
complete = mcrfpy.Caption("All animation types demonstrated!", 400, 350)
|
|
complete.fill_color = mcrfpy.Color(0, 255, 0)
|
|
complete.outline = 2
|
|
ui = mcrfpy.sceneUI("sizzle_reel")
|
|
ui.append(complete)
|
|
|
|
mcrfpy.setTimer("complete", show_complete, 3000)
|
|
|
|
# Initialize scene
|
|
print("Starting McRogueFace Animation Sizzle Reel v2...")
|
|
print("This will demonstrate animation types on various objects.")
|
|
|
|
ui = create_demo_scene()
|
|
|
|
# Start the demo sequence after a short delay
|
|
mcrfpy.setTimer("start_demos", run_demo_sequence, 500) |