2024-03-21 22:24:42 -04:00
# include "PyTexture.h"
2024-04-07 22:51:31 -04:00
# include "McRFPy_API.h"
2024-03-21 22:24:42 -04:00
PyTexture : : PyTexture ( std : : string filename , int sprite_w , int sprite_h )
: source ( filename ) , sprite_width ( sprite_w ) , sprite_height ( sprite_h )
{
texture = sf : : Texture ( ) ;
texture . loadFromFile ( source ) ;
auto size = texture . getSize ( ) ;
sheet_width = ( size . x / sprite_width ) ;
sheet_height = ( size . y / sprite_height ) ;
if ( size . x % sprite_width ! = 0 | | size . y % sprite_height ! = 0 )
{
std : : cout < < " Warning: Texture ` " < < source < < " ` is not an even number of sprite widths or heights across. " < < std : : endl
< < " Sprite size given was " < < sprite_w < < " x " < < sprite_h < < " px but the file has a resolution of " < < sheet_width < < " x " < < sheet_height < < " px. " < < std : : endl ;
}
}
sf : : Sprite PyTexture : : sprite ( int index , sf : : Vector2f pos , sf : : Vector2f s )
{
int tx = index % sheet_width , ty = index / sheet_width ;
auto ir = sf : : IntRect ( tx * sprite_width , ty * sprite_height , sprite_width , sprite_height ) ;
auto sprite = sf : : Sprite ( texture , ir ) ;
sprite . setPosition ( pos ) ;
sprite . setScale ( s ) ;
return sprite ;
}
PyObject * PyTexture : : pyObject ( )
{
2024-04-07 22:51:31 -04:00
auto type = ( PyTypeObject * ) PyObject_GetAttrString ( McRFPy_API : : mcrf_module , " Texture " ) ;
2024-04-20 10:32:04 -04:00
PyObject * obj = PyTexture : : pynew ( type , Py_None , Py_None ) ;
2024-04-07 22:51:31 -04:00
2024-03-21 22:24:42 -04:00
try {
( ( PyTextureObject * ) obj ) - > data = shared_from_this ( ) ;
}
catch ( std : : bad_weak_ptr & e )
{
std : : cout < < " Bad weak ptr: shared_from_this() failed in PyTexture::pyObject(); did you create a PyTexture outside of std::make_shared? enjoy your segfault, soon! " < < std : : endl ;
}
// TODO - shared_from_this will raise an exception if the object does not have a shared pointer. Constructor should be made private; write a factory function
return obj ;
}
2024-04-07 22:51:31 -04:00
PyObject * PyTexture : : repr ( PyObject * obj )
{
PyTextureObject * self = ( PyTextureObject * ) obj ;
std : : ostringstream ss ;
if ( ! self - > data )
{
ss < < " <Texture [invalid internal object]> " ;
std : : string repr_str = ss . str ( ) ;
return PyUnicode_DecodeUTF8 ( repr_str . c_str ( ) , repr_str . size ( ) , " replace " ) ;
}
auto & ptex = * ( self - > data ) ;
ss < < " <Texture " < < ptex . sheet_height < < " rows, " < < ptex . sheet_width < < " columns; " < < ptex . sprite_width < < " x " < < ptex . sprite_height < < " px sprites. source=' " < < ptex . source < < " '> " ;
std : : string repr_str = ss . str ( ) ;
return PyUnicode_DecodeUTF8 ( repr_str . c_str ( ) , repr_str . size ( ) , " replace " ) ;
}
2024-03-21 22:24:42 -04:00
Py_hash_t PyTexture : : hash ( PyObject * obj )
{
auto self = ( PyTextureObject * ) obj ;
return reinterpret_cast < Py_hash_t > ( self - > data . get ( ) ) ;
}
int PyTexture : : init ( PyTextureObject * self , PyObject * args , PyObject * kwds )
{
static const char * keywords [ ] = { " filename " , " sprite_width " , " sprite_height " , nullptr } ;
char * filename ;
int sprite_width , sprite_height ;
if ( ! PyArg_ParseTupleAndKeywords ( args , kwds , " sii " , const_cast < char * * > ( keywords ) , & filename , & sprite_width , & sprite_height ) )
return - 1 ;
self - > data = std : : make_shared < PyTexture > ( filename , sprite_width , sprite_height ) ;
return 0 ;
}
PyObject * PyTexture : : pynew ( PyTypeObject * type , PyObject * args , PyObject * kwds )
{
return ( PyObject * ) type - > tp_alloc ( type , 0 ) ;
}
Squashed commit of the following: [alpha_streamline_1]
the low-hanging fruit of pre-existing issues and standardizing the
Python interfaces
Special thanks to Claude Code, ~100k output tokens for this merge
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
commit 99f301e3a0e9e81ad28c9e1d410390c32dfd933c
Author: John McCardle <mccardle.john@gmail.com>
Date: Sat Jul 5 16:25:32 2025 -0400
Add position tuple support and pos property to UI elements
closes #83, closes #84
- Issue #83: Add position tuple support to constructors
- Frame and Sprite now accept both (x, y) and ((x, y)) forms
- Also accept Vector objects as position arguments
- Caption and Entity already supported tuple/Vector forms
- Uses PyVector::from_arg for flexible position parsing
- Issue #84: Add pos property to Frame and Sprite
- Added pos getter that returns a Vector
- Added pos setter that accepts Vector or tuple
- Provides consistency with Caption and Entity which already had pos properties
- All UI elements now have a uniform way to get/set positions as Vectors
Both features improve API consistency and make it easier to work with positions.
commit 2f2b488fb54da12c39c0010dbd83cb9f6c429b01
Author: John McCardle <mccardle.john@gmail.com>
Date: Sat Jul 5 16:18:10 2025 -0400
Standardize sprite_index property and add scale_x/scale_y to UISprite
closes #81, closes #82
- Issue #81: Standardized property name to sprite_index across UISprite and UIEntity
- Added sprite_index as the primary property name
- Kept sprite_number as a deprecated alias for backward compatibility
- Updated repr() methods to use sprite_index
- Updated animation system to recognize both names
- Issue #82: Added scale_x and scale_y properties to UISprite
- Enables non-uniform scaling of sprites
- scale property still works for uniform scaling
- Both properties work with the animation system
All existing code using sprite_number continues to work due to backward compatibility.
commit 5a003a9aa587eb8ee4b79ac67ca8f342ab62e2d2
Author: John McCardle <mccardle.john@gmail.com>
Date: Sat Jul 5 16:09:52 2025 -0400
Fix multiple low priority issues
closes #12, closes #80, closes #95, closes #96, closes #99
- Issue #12: Set tp_new to NULL for GridPoint and GridPointState to prevent instantiation from Python
- Issue #80: Renamed Caption.size to Caption.font_size for semantic clarity
- Issue #95: Fixed UICollection repr to show actual derived types instead of generic UIDrawable
- Issue #96: Added extend() method to UICollection for API consistency with UIEntityCollection
- Issue #99: Exposed read-only properties for Texture (sprite_width, sprite_height, sheet_width, sheet_height, sprite_count, source) and Font (family, source)
All issues have corresponding tests that verify the fixes work correctly.
commit e5affaf317665395135c936bc4a6b840ae321765
Author: John McCardle <mccardle.john@gmail.com>
Date: Sat Jul 5 15:50:09 2025 -0400
Fix critical issues: script loading, entity types, and color properties
- Issue #37: Fix Windows scripts subdirectory not checked
- Updated executeScript() to use executable_path() from platform.h
- Scripts now load correctly when working directory differs from executable
- Issue #76: Fix UIEntityCollection returns wrong type
- Updated UIEntityCollectionIter::next() to check for stored Python object
- Derived Entity classes now preserve their type when retrieved from collections
- Issue #9: Recreate RenderTexture when resized (already fixed)
- Confirmed RenderTexture recreation already implemented in set_size() and set_float_member()
- Uses 1.5x padding and 4096 max size limit
- Issue #79: Fix Color r, g, b, a properties return None
- Implemented get_member() and set_member() in PyColor.cpp
- Color component properties now work correctly with proper validation
- Additional fix: Grid.at() method signature
- Changed from METH_O to METH_VARARGS to accept two arguments
All fixes include comprehensive tests to verify functionality.
closes #37, closes #76, closes #9, closes #79
2025-07-05 17:30:49 -04:00
PyObject * PyTexture : : get_sprite_width ( PyTextureObject * self , void * closure )
{
return PyLong_FromLong ( self - > data - > sprite_width ) ;
}
PyObject * PyTexture : : get_sprite_height ( PyTextureObject * self , void * closure )
{
return PyLong_FromLong ( self - > data - > sprite_height ) ;
}
PyObject * PyTexture : : get_sheet_width ( PyTextureObject * self , void * closure )
{
return PyLong_FromLong ( self - > data - > sheet_width ) ;
}
PyObject * PyTexture : : get_sheet_height ( PyTextureObject * self , void * closure )
{
return PyLong_FromLong ( self - > data - > sheet_height ) ;
}
PyObject * PyTexture : : get_sprite_count ( PyTextureObject * self , void * closure )
{
return PyLong_FromLong ( self - > data - > getSpriteCount ( ) ) ;
}
PyObject * PyTexture : : get_source ( PyTextureObject * self , void * closure )
{
return PyUnicode_FromString ( self - > data - > source . c_str ( ) ) ;
}
PyGetSetDef PyTexture : : getsetters [ ] = {
{ " sprite_width " , ( getter ) PyTexture : : get_sprite_width , NULL , " Width of each sprite in pixels " , NULL } ,
{ " sprite_height " , ( getter ) PyTexture : : get_sprite_height , NULL , " Height of each sprite in pixels " , NULL } ,
{ " sheet_width " , ( getter ) PyTexture : : get_sheet_width , NULL , " Number of sprite columns in the texture " , NULL } ,
{ " sheet_height " , ( getter ) PyTexture : : get_sheet_height , NULL , " Number of sprite rows in the texture " , NULL } ,
{ " sprite_count " , ( getter ) PyTexture : : get_sprite_count , NULL , " Total number of sprites in the texture " , NULL } ,
{ " source " , ( getter ) PyTexture : : get_source , NULL , " Source filename of the texture " , NULL } ,
{ NULL } // Sentinel
} ;