2024-03-30 21:20:40 -04:00
# include "PyColor.h"
2024-04-20 13:37:19 -04:00
# include "McRFPy_API.h"
Iterators, other Python C API improvements
closes #72
ref #69 - this resolves the "UICollection" (not "UIEntityCollection", perhaps renamed since the issue opened) and "UIEntityCollection" portion. The Grid point based iterators were not updated.
**RPATH updates**
Will this RPATH setting allow McRogueFace to execute using its included "lib" subdirectory after being unzipped on a new computer?
The change from "./lib" to "$ORIGIN/./lib" improves portability. The $ORIGIN token is a special Linux/Unix convention that refers to the directory containing the executable itself. This makes the path relative to the executable's location rather than the current working directory, which means McRogueFace will correctly find its libraries in the lib subdirectory regardless of where it's run from after being unzipped on a new computer.
**New standard object initialization**
PyColor, PyVector
- Fixed all 15 PyTypeObject definitions to use proper designated initializer syntax
- Replaced PyType_GenericAlloc usage in PyColor.cpp and PyVector.cpp
- Updated PyObject_New usage in UIEntity.cpp
- All object creation now uses module-based type lookups instead of static references
- Created centralized utilities in PyObjectUtils.h
**RAII Wrappers**
automatic reference counting via C++ object lifecycle
- Created PyRAII.h with PyObjectRef and PyTypeRef classes
- These provide automatic reference counting management
- Updated PyColor::from_arg() to demonstrate RAII usage
- Prevents memory leaks and reference counting errors
**Python object base in type defs:**
`.ob_base = {.ob_base = {.ob_refcnt = 1, .ob_type = NULL}, .ob_size = 0}`
PyColor, PyTexture, PyVector, UICaption, UICollection, UIEntity, UIFrame, UIGrid
**convertDrawableToPython**
replace crazy macro to detect the correct Python type of a UIDrawable instance
- Removed the problematic macro from UIDrawable.h
- Created template-based functions in PyObjectUtils.h
- Updated UICollection.cpp to use local helper function
- The new approach is cleaner, more debuggable, and avoids static type references
**Iterator fixes**
tp_iter on UICollection, UIGrid, UIGridPoint, UISprite
UIGrid logic improved, standard
**List vs Vector usage analysis**
there are different use cases that weren't standardized:
- UICollection (for Frame children) uses std::vector<std::shared_ptr<UIDrawable>>
- UIEntityCollection (for Grid entities) uses std::list<std::shared_ptr<UIEntity>>
The rationale is currently connected to frequency of expected changes.
* A "UICollection" is likely either all visible or not; it's also likely to be created once and have a static set of contents. They should be contiguous in memory in hopes that this helps rendering speed.
* A "UIEntityCollection" is expected to be rendered as a subset within the visible rectangle of the UIGrid. Scrolling the grid or gameplay logic is likely to frequently create and destroy entities. In general I expect Entity collections to have a much higher common size than UICollections. For these reasons I've made them Lists in hopes that they never have to be reallocated or moved during a frame.
2025-05-31 08:58:52 -04:00
# include "PyObjectUtils.h"
# include "PyRAII.h"
2024-03-30 21:20:40 -04:00
PyGetSetDef PyColor : : getsetters [ ] = {
{ " r " , ( getter ) PyColor : : get_member , ( setter ) PyColor : : set_member , " Red component " , ( void * ) 0 } ,
{ " g " , ( getter ) PyColor : : get_member , ( setter ) PyColor : : set_member , " Green component " , ( void * ) 1 } ,
{ " b " , ( getter ) PyColor : : get_member , ( setter ) PyColor : : set_member , " Blue component " , ( void * ) 2 } ,
{ " a " , ( getter ) PyColor : : get_member , ( setter ) PyColor : : set_member , " Alpha component " , ( void * ) 3 } ,
{ NULL }
} ;
PyColor : : PyColor ( sf : : Color target )
: data ( target ) { }
PyObject * PyColor : : pyObject ( )
{
Iterators, other Python C API improvements
closes #72
ref #69 - this resolves the "UICollection" (not "UIEntityCollection", perhaps renamed since the issue opened) and "UIEntityCollection" portion. The Grid point based iterators were not updated.
**RPATH updates**
Will this RPATH setting allow McRogueFace to execute using its included "lib" subdirectory after being unzipped on a new computer?
The change from "./lib" to "$ORIGIN/./lib" improves portability. The $ORIGIN token is a special Linux/Unix convention that refers to the directory containing the executable itself. This makes the path relative to the executable's location rather than the current working directory, which means McRogueFace will correctly find its libraries in the lib subdirectory regardless of where it's run from after being unzipped on a new computer.
**New standard object initialization**
PyColor, PyVector
- Fixed all 15 PyTypeObject definitions to use proper designated initializer syntax
- Replaced PyType_GenericAlloc usage in PyColor.cpp and PyVector.cpp
- Updated PyObject_New usage in UIEntity.cpp
- All object creation now uses module-based type lookups instead of static references
- Created centralized utilities in PyObjectUtils.h
**RAII Wrappers**
automatic reference counting via C++ object lifecycle
- Created PyRAII.h with PyObjectRef and PyTypeRef classes
- These provide automatic reference counting management
- Updated PyColor::from_arg() to demonstrate RAII usage
- Prevents memory leaks and reference counting errors
**Python object base in type defs:**
`.ob_base = {.ob_base = {.ob_refcnt = 1, .ob_type = NULL}, .ob_size = 0}`
PyColor, PyTexture, PyVector, UICaption, UICollection, UIEntity, UIFrame, UIGrid
**convertDrawableToPython**
replace crazy macro to detect the correct Python type of a UIDrawable instance
- Removed the problematic macro from UIDrawable.h
- Created template-based functions in PyObjectUtils.h
- Updated UICollection.cpp to use local helper function
- The new approach is cleaner, more debuggable, and avoids static type references
**Iterator fixes**
tp_iter on UICollection, UIGrid, UIGridPoint, UISprite
UIGrid logic improved, standard
**List vs Vector usage analysis**
there are different use cases that weren't standardized:
- UICollection (for Frame children) uses std::vector<std::shared_ptr<UIDrawable>>
- UIEntityCollection (for Grid entities) uses std::list<std::shared_ptr<UIEntity>>
The rationale is currently connected to frequency of expected changes.
* A "UICollection" is likely either all visible or not; it's also likely to be created once and have a static set of contents. They should be contiguous in memory in hopes that this helps rendering speed.
* A "UIEntityCollection" is expected to be rendered as a subset within the visible rectangle of the UIGrid. Scrolling the grid or gameplay logic is likely to frequently create and destroy entities. In general I expect Entity collections to have a much higher common size than UICollections. For these reasons I've made them Lists in hopes that they never have to be reallocated or moved during a frame.
2025-05-31 08:58:52 -04:00
PyTypeObject * type = ( PyTypeObject * ) PyObject_GetAttrString ( McRFPy_API : : mcrf_module , " Color " ) ;
if ( ! type ) return nullptr ;
PyColorObject * obj = ( PyColorObject * ) type - > tp_alloc ( type , 0 ) ;
Py_DECREF ( type ) ;
if ( obj ) {
obj - > data = data ;
}
return ( PyObject * ) obj ;
2024-03-30 21:20:40 -04:00
}
sf : : Color PyColor : : fromPy ( PyObject * obj )
{
PyColorObject * self = ( PyColorObject * ) obj ;
return self - > data ;
}
sf : : Color PyColor : : fromPy ( PyColorObject * self )
{
return self - > data ;
}
void PyColor : : set ( sf : : Color color )
{
data = color ;
}
sf : : Color PyColor : : get ( )
{
return data ;
}
Py_hash_t PyColor : : hash ( PyObject * obj )
{
auto self = ( PyColorObject * ) obj ;
Py_hash_t value = 0 ;
value + = self - > data . r ;
value < < 8 ; value + = self - > data . g ;
value < < 8 ; value + = self - > data . b ;
value < < 8 ; value + = self - > data . a ;
return value ;
}
PyObject * PyColor : : repr ( PyObject * obj )
{
PyColorObject * self = ( PyColorObject * ) obj ;
std : : ostringstream ss ;
sf : : Color c = self - > data ;
ss < < " <Color ( " < < int ( c . r ) < < " , " < < int ( c . g ) < < " , " < < int ( c . b ) < < " , " < < int ( c . a ) < < " )> " ;
std : : string repr_str = ss . str ( ) ;
return PyUnicode_DecodeUTF8 ( repr_str . c_str ( ) , repr_str . size ( ) , " replace " ) ;
}
2024-03-31 18:00:19 -04:00
int PyColor : : init ( PyColorObject * self , PyObject * args , PyObject * kwds ) {
//using namespace mcrfpydef;
2024-03-30 21:20:40 -04:00
static const char * keywords [ ] = { " r " , " g " , " b " , " a " , nullptr } ;
PyObject * leader ;
int r = - 1 , g = - 1 , b = - 1 , a = 255 ;
2024-03-31 18:00:19 -04:00
if ( ! PyArg_ParseTupleAndKeywords ( args , kwds , " O|iii " , const_cast < char * * > ( keywords ) , & leader , & g , & b , & a ) ) {
PyErr_SetString ( PyExc_TypeError , " mcrfpy.Color requires a 3-tuple, 4-tuple, color name, or integer values within 0-255 (r, g, b, optionally a) " ) ;
2024-03-30 21:20:40 -04:00
return - 1 ;
}
2024-03-31 18:00:19 -04:00
//std::cout << "Arg parsing succeeded. Values: " << r << " " << g << " " << b << " " << a <<std::endl;
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(leader)) << std::endl;
// Tuple cases
if ( PyTuple_Check ( leader ) ) {
Py_ssize_t tupleSize = PyTuple_Size ( leader ) ;
if ( tupleSize < 3 | | tupleSize > 4 ) {
PyErr_SetString ( PyExc_TypeError , " Invalid tuple length: mcrfpy.Color requires a 3-tuple, 4-tuple, color name, or integer values within 0-255 (r, g, b, optionally a) " ) ;
2024-03-30 21:20:40 -04:00
return - 1 ;
}
r = PyLong_AsLong ( PyTuple_GetItem ( leader , 0 ) ) ;
g = PyLong_AsLong ( PyTuple_GetItem ( leader , 1 ) ) ;
b = PyLong_AsLong ( PyTuple_GetItem ( leader , 2 ) ) ;
2024-03-31 18:00:19 -04:00
if ( tupleSize = = 4 ) {
2024-03-30 21:20:40 -04:00
a = PyLong_AsLong ( PyTuple_GetItem ( leader , 3 ) ) ;
}
}
2024-03-31 18:00:19 -04:00
// Color name (not implemented yet)
else if ( PyUnicode_Check ( leader ) ) {
PyErr_SetString ( PyExc_NotImplementedError , " Color names aren't ready yet " ) ;
2024-03-30 21:20:40 -04:00
return - 1 ;
}
2024-03-31 18:00:19 -04:00
// Check if the leader is actually an integer for the r value
else if ( PyLong_Check ( leader ) ) {
r = PyLong_AsLong ( leader ) ;
// Additional validation not shown; g, b are required to be parsed
} else {
PyErr_SetString ( PyExc_TypeError , " mcrfpy.Color requires a 3-tuple, 4-tuple, color name, or integer values within 0-255 (r, g, b, optionally a) " ) ;
2024-03-30 21:20:40 -04:00
return - 1 ;
}
2024-03-31 18:00:19 -04:00
// Validate color values
if ( r < 0 | | r > 255 | | g < 0 | | g > 255 | | b < 0 | | b > 255 | | a < 0 | | a > 255 ) {
PyErr_SetString ( PyExc_ValueError , " Color values must be between 0 and 255. " ) ;
2024-03-30 21:20:40 -04:00
return - 1 ;
}
2024-03-31 18:00:19 -04:00
2024-03-30 21:20:40 -04:00
self - > data = sf : : Color ( r , g , b , a ) ;
return 0 ;
}
PyObject * PyColor : : pynew ( PyTypeObject * type , PyObject * args , PyObject * kwds )
{
2024-03-31 18:00:19 -04:00
auto obj = ( PyObject * ) type - > tp_alloc ( type , 0 ) ;
//Py_INCREF(obj);
return obj ;
2024-03-30 21:20:40 -04:00
}
PyObject * PyColor : : get_member ( PyObject * obj , void * closure )
{
// TODO
return Py_None ;
}
int PyColor : : set_member ( PyObject * obj , PyObject * value , void * closure )
{
// TODO
return 0 ;
}
2024-04-20 13:37:19 -04:00
PyColorObject * PyColor : : from_arg ( PyObject * args )
{
Iterators, other Python C API improvements
closes #72
ref #69 - this resolves the "UICollection" (not "UIEntityCollection", perhaps renamed since the issue opened) and "UIEntityCollection" portion. The Grid point based iterators were not updated.
**RPATH updates**
Will this RPATH setting allow McRogueFace to execute using its included "lib" subdirectory after being unzipped on a new computer?
The change from "./lib" to "$ORIGIN/./lib" improves portability. The $ORIGIN token is a special Linux/Unix convention that refers to the directory containing the executable itself. This makes the path relative to the executable's location rather than the current working directory, which means McRogueFace will correctly find its libraries in the lib subdirectory regardless of where it's run from after being unzipped on a new computer.
**New standard object initialization**
PyColor, PyVector
- Fixed all 15 PyTypeObject definitions to use proper designated initializer syntax
- Replaced PyType_GenericAlloc usage in PyColor.cpp and PyVector.cpp
- Updated PyObject_New usage in UIEntity.cpp
- All object creation now uses module-based type lookups instead of static references
- Created centralized utilities in PyObjectUtils.h
**RAII Wrappers**
automatic reference counting via C++ object lifecycle
- Created PyRAII.h with PyObjectRef and PyTypeRef classes
- These provide automatic reference counting management
- Updated PyColor::from_arg() to demonstrate RAII usage
- Prevents memory leaks and reference counting errors
**Python object base in type defs:**
`.ob_base = {.ob_base = {.ob_refcnt = 1, .ob_type = NULL}, .ob_size = 0}`
PyColor, PyTexture, PyVector, UICaption, UICollection, UIEntity, UIFrame, UIGrid
**convertDrawableToPython**
replace crazy macro to detect the correct Python type of a UIDrawable instance
- Removed the problematic macro from UIDrawable.h
- Created template-based functions in PyObjectUtils.h
- Updated UICollection.cpp to use local helper function
- The new approach is cleaner, more debuggable, and avoids static type references
**Iterator fixes**
tp_iter on UICollection, UIGrid, UIGridPoint, UISprite
UIGrid logic improved, standard
**List vs Vector usage analysis**
there are different use cases that weren't standardized:
- UICollection (for Frame children) uses std::vector<std::shared_ptr<UIDrawable>>
- UIEntityCollection (for Grid entities) uses std::list<std::shared_ptr<UIEntity>>
The rationale is currently connected to frequency of expected changes.
* A "UICollection" is likely either all visible or not; it's also likely to be created once and have a static set of contents. They should be contiguous in memory in hopes that this helps rendering speed.
* A "UIEntityCollection" is expected to be rendered as a subset within the visible rectangle of the UIGrid. Scrolling the grid or gameplay logic is likely to frequently create and destroy entities. In general I expect Entity collections to have a much higher common size than UICollections. For these reasons I've made them Lists in hopes that they never have to be reallocated or moved during a frame.
2025-05-31 08:58:52 -04:00
// Use RAII for type reference management
PyRAII : : PyTypeRef type ( " Color " , McRFPy_API : : mcrf_module ) ;
if ( ! type ) {
return NULL ;
}
// Check if args is already a Color instance
if ( PyObject_IsInstance ( args , ( PyObject * ) type . get ( ) ) ) {
return ( PyColorObject * ) args ;
}
// Create new Color object using RAII
PyRAII : : PyObjectRef obj ( type - > tp_alloc ( type . get ( ) , 0 ) , true ) ;
if ( ! obj ) {
return NULL ;
}
// Initialize the object
int err = init ( ( PyColorObject * ) obj . get ( ) , args , NULL ) ;
2024-04-20 13:37:19 -04:00
if ( err ) {
Iterators, other Python C API improvements
closes #72
ref #69 - this resolves the "UICollection" (not "UIEntityCollection", perhaps renamed since the issue opened) and "UIEntityCollection" portion. The Grid point based iterators were not updated.
**RPATH updates**
Will this RPATH setting allow McRogueFace to execute using its included "lib" subdirectory after being unzipped on a new computer?
The change from "./lib" to "$ORIGIN/./lib" improves portability. The $ORIGIN token is a special Linux/Unix convention that refers to the directory containing the executable itself. This makes the path relative to the executable's location rather than the current working directory, which means McRogueFace will correctly find its libraries in the lib subdirectory regardless of where it's run from after being unzipped on a new computer.
**New standard object initialization**
PyColor, PyVector
- Fixed all 15 PyTypeObject definitions to use proper designated initializer syntax
- Replaced PyType_GenericAlloc usage in PyColor.cpp and PyVector.cpp
- Updated PyObject_New usage in UIEntity.cpp
- All object creation now uses module-based type lookups instead of static references
- Created centralized utilities in PyObjectUtils.h
**RAII Wrappers**
automatic reference counting via C++ object lifecycle
- Created PyRAII.h with PyObjectRef and PyTypeRef classes
- These provide automatic reference counting management
- Updated PyColor::from_arg() to demonstrate RAII usage
- Prevents memory leaks and reference counting errors
**Python object base in type defs:**
`.ob_base = {.ob_base = {.ob_refcnt = 1, .ob_type = NULL}, .ob_size = 0}`
PyColor, PyTexture, PyVector, UICaption, UICollection, UIEntity, UIFrame, UIGrid
**convertDrawableToPython**
replace crazy macro to detect the correct Python type of a UIDrawable instance
- Removed the problematic macro from UIDrawable.h
- Created template-based functions in PyObjectUtils.h
- Updated UICollection.cpp to use local helper function
- The new approach is cleaner, more debuggable, and avoids static type references
**Iterator fixes**
tp_iter on UICollection, UIGrid, UIGridPoint, UISprite
UIGrid logic improved, standard
**List vs Vector usage analysis**
there are different use cases that weren't standardized:
- UICollection (for Frame children) uses std::vector<std::shared_ptr<UIDrawable>>
- UIEntityCollection (for Grid entities) uses std::list<std::shared_ptr<UIEntity>>
The rationale is currently connected to frequency of expected changes.
* A "UICollection" is likely either all visible or not; it's also likely to be created once and have a static set of contents. They should be contiguous in memory in hopes that this helps rendering speed.
* A "UIEntityCollection" is expected to be rendered as a subset within the visible rectangle of the UIGrid. Scrolling the grid or gameplay logic is likely to frequently create and destroy entities. In general I expect Entity collections to have a much higher common size than UICollections. For these reasons I've made them Lists in hopes that they never have to be reallocated or moved during a frame.
2025-05-31 08:58:52 -04:00
// obj will be automatically cleaned up when it goes out of scope
2024-04-20 13:37:19 -04:00
return NULL ;
}
Iterators, other Python C API improvements
closes #72
ref #69 - this resolves the "UICollection" (not "UIEntityCollection", perhaps renamed since the issue opened) and "UIEntityCollection" portion. The Grid point based iterators were not updated.
**RPATH updates**
Will this RPATH setting allow McRogueFace to execute using its included "lib" subdirectory after being unzipped on a new computer?
The change from "./lib" to "$ORIGIN/./lib" improves portability. The $ORIGIN token is a special Linux/Unix convention that refers to the directory containing the executable itself. This makes the path relative to the executable's location rather than the current working directory, which means McRogueFace will correctly find its libraries in the lib subdirectory regardless of where it's run from after being unzipped on a new computer.
**New standard object initialization**
PyColor, PyVector
- Fixed all 15 PyTypeObject definitions to use proper designated initializer syntax
- Replaced PyType_GenericAlloc usage in PyColor.cpp and PyVector.cpp
- Updated PyObject_New usage in UIEntity.cpp
- All object creation now uses module-based type lookups instead of static references
- Created centralized utilities in PyObjectUtils.h
**RAII Wrappers**
automatic reference counting via C++ object lifecycle
- Created PyRAII.h with PyObjectRef and PyTypeRef classes
- These provide automatic reference counting management
- Updated PyColor::from_arg() to demonstrate RAII usage
- Prevents memory leaks and reference counting errors
**Python object base in type defs:**
`.ob_base = {.ob_base = {.ob_refcnt = 1, .ob_type = NULL}, .ob_size = 0}`
PyColor, PyTexture, PyVector, UICaption, UICollection, UIEntity, UIFrame, UIGrid
**convertDrawableToPython**
replace crazy macro to detect the correct Python type of a UIDrawable instance
- Removed the problematic macro from UIDrawable.h
- Created template-based functions in PyObjectUtils.h
- Updated UICollection.cpp to use local helper function
- The new approach is cleaner, more debuggable, and avoids static type references
**Iterator fixes**
tp_iter on UICollection, UIGrid, UIGridPoint, UISprite
UIGrid logic improved, standard
**List vs Vector usage analysis**
there are different use cases that weren't standardized:
- UICollection (for Frame children) uses std::vector<std::shared_ptr<UIDrawable>>
- UIEntityCollection (for Grid entities) uses std::list<std::shared_ptr<UIEntity>>
The rationale is currently connected to frequency of expected changes.
* A "UICollection" is likely either all visible or not; it's also likely to be created once and have a static set of contents. They should be contiguous in memory in hopes that this helps rendering speed.
* A "UIEntityCollection" is expected to be rendered as a subset within the visible rectangle of the UIGrid. Scrolling the grid or gameplay logic is likely to frequently create and destroy entities. In general I expect Entity collections to have a much higher common size than UICollections. For these reasons I've made them Lists in hopes that they never have to be reallocated or moved during a frame.
2025-05-31 08:58:52 -04:00
// Release ownership and return
return ( PyColorObject * ) obj . release ( ) ;
2024-04-20 13:37:19 -04:00
}