McRogueFace/src/PyTexture.h

44 lines
1.4 KiB
C
Raw Normal View History

Squashed commit of the following: [standardize_texture_handling] closes #18 commit b114ec308518607fad5777c6c1b516381e2a78b9 Author: John McCardle <mccardle.john@gmail.com> Date: Thu Mar 21 22:22:35 2024 -0400 cleaning up for merge commit d7228172c4d5455159cd91782b49304a971bb444 Author: John McCardle <mccardle.john@gmail.com> Date: Thu Mar 21 21:39:15 2024 -0400 Messy, but monumental: PyTexture::pyObject works this also coincidentally fixes a weird bug I encountered while (mis?)using tp_alloc: by using PyType_GenericAlloc, I avoid the segfault that tp_alloc sometimes causes. See the horrible UIDrawable retrieval macro that I use in UI.h for a workaround that can probably be replaced with this technique commit 2cf8f9431025b3eecb67422e98ec95e3f2b4038f Author: John McCardle <mccardle.john@gmail.com> Date: Wed Mar 20 21:16:52 2024 -0400 Radical new example pattern for exposing a C++ class to Python commit 84a8886da2570a8087f83141f0d41fef65073d99 Author: John McCardle <mccardle.john@gmail.com> Date: Sun Mar 17 16:29:33 2024 -0400 Fixed render issue with UIGrid / PyTexture: wasn't positioning or scaling properly after fetching sprite commit 20f80c4114fce029faf28a195ffdbb041181e484 Author: John McCardle <mccardle.john@gmail.com> Date: Sun Mar 17 16:23:52 2024 -0400 Fixed sprite indexing error in PyTexture; needs non-square sprite tests, but feeling confident! commit afd4ff1925382622abf79db132b6123ee33b946b Author: John McCardle <mccardle.john@gmail.com> Date: Sat Mar 16 21:53:24 2024 -0400 good progress, we're building again. Issue with Grid (tile sprite) textures and I think the sprite indexes are being calculated wrong (x and y transposed?) commit bfd33102d13072eb4624cb07dcf5a55a98e93aa0 Author: John McCardle <mccardle.john@gmail.com> Date: Sat Mar 16 14:52:35 2024 -0400 Squashed basically all the compile bugs in UISprite, but UIEntity and UIGrid use textures as well, so they need to be fixed too before the project will build again commit 47d0e34a17eb4aa86f781d0a022180d637cec602 Author: John McCardle <mccardle.john@gmail.com> Date: Sat Mar 16 11:31:39 2024 -0400 Initial PyTexture class no testing done. should enable rectangular (non-square) textures "sprite" method; let's just overwrite sprites with texture coords Hoping to replace awful code like: `self->data->sprite.sprite.setTextureRect(self->data->sprite.itex->spriteCoordinates(val));` with something like: `self->data->sprite = self->data->texture->sprite(val);`
2024-03-21 22:24:42 -04:00
#pragma once
#include "Common.h"
#include "Python.h"
class PyTexture;
typedef struct {
PyObject_HEAD
std::shared_ptr<PyTexture> data;
} PyTextureObject;
class PyTexture : public std::enable_shared_from_this<PyTexture>
{
private:
sf::Texture texture;
std::string source;
int sheet_width, sheet_height;
public:
int sprite_width, sprite_height; // just use them read only, OK?
PyTexture(std::string filename, int sprite_w, int sprite_h);
sf::Sprite sprite(int index, sf::Vector2f pos = sf::Vector2f(0, 0), sf::Vector2f s = sf::Vector2f(1.0, 1.0));
PyObject* pyObject();
static PyObject* repr(PyObject*);
Squashed commit of the following: [standardize_texture_handling] closes #18 commit b114ec308518607fad5777c6c1b516381e2a78b9 Author: John McCardle <mccardle.john@gmail.com> Date: Thu Mar 21 22:22:35 2024 -0400 cleaning up for merge commit d7228172c4d5455159cd91782b49304a971bb444 Author: John McCardle <mccardle.john@gmail.com> Date: Thu Mar 21 21:39:15 2024 -0400 Messy, but monumental: PyTexture::pyObject works this also coincidentally fixes a weird bug I encountered while (mis?)using tp_alloc: by using PyType_GenericAlloc, I avoid the segfault that tp_alloc sometimes causes. See the horrible UIDrawable retrieval macro that I use in UI.h for a workaround that can probably be replaced with this technique commit 2cf8f9431025b3eecb67422e98ec95e3f2b4038f Author: John McCardle <mccardle.john@gmail.com> Date: Wed Mar 20 21:16:52 2024 -0400 Radical new example pattern for exposing a C++ class to Python commit 84a8886da2570a8087f83141f0d41fef65073d99 Author: John McCardle <mccardle.john@gmail.com> Date: Sun Mar 17 16:29:33 2024 -0400 Fixed render issue with UIGrid / PyTexture: wasn't positioning or scaling properly after fetching sprite commit 20f80c4114fce029faf28a195ffdbb041181e484 Author: John McCardle <mccardle.john@gmail.com> Date: Sun Mar 17 16:23:52 2024 -0400 Fixed sprite indexing error in PyTexture; needs non-square sprite tests, but feeling confident! commit afd4ff1925382622abf79db132b6123ee33b946b Author: John McCardle <mccardle.john@gmail.com> Date: Sat Mar 16 21:53:24 2024 -0400 good progress, we're building again. Issue with Grid (tile sprite) textures and I think the sprite indexes are being calculated wrong (x and y transposed?) commit bfd33102d13072eb4624cb07dcf5a55a98e93aa0 Author: John McCardle <mccardle.john@gmail.com> Date: Sat Mar 16 14:52:35 2024 -0400 Squashed basically all the compile bugs in UISprite, but UIEntity and UIGrid use textures as well, so they need to be fixed too before the project will build again commit 47d0e34a17eb4aa86f781d0a022180d637cec602 Author: John McCardle <mccardle.john@gmail.com> Date: Sat Mar 16 11:31:39 2024 -0400 Initial PyTexture class no testing done. should enable rectangular (non-square) textures "sprite" method; let's just overwrite sprites with texture coords Hoping to replace awful code like: `self->data->sprite.sprite.setTextureRect(self->data->sprite.itex->spriteCoordinates(val));` with something like: `self->data->sprite = self->data->texture->sprite(val);`
2024-03-21 22:24:42 -04:00
static Py_hash_t hash(PyObject*);
static int init(PyTextureObject*, PyObject*, PyObject*);
static PyObject* pynew(PyTypeObject* type, PyObject* args=NULL, PyObject* kwds=NULL);
};
namespace mcrfpydef {
static PyTypeObject PyTextureType = {
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
.ob_base = {.ob_base = {.ob_refcnt = 1, .ob_type = NULL}, .ob_size = 0},
Squashed commit of the following: [standardize_texture_handling] closes #18 commit b114ec308518607fad5777c6c1b516381e2a78b9 Author: John McCardle <mccardle.john@gmail.com> Date: Thu Mar 21 22:22:35 2024 -0400 cleaning up for merge commit d7228172c4d5455159cd91782b49304a971bb444 Author: John McCardle <mccardle.john@gmail.com> Date: Thu Mar 21 21:39:15 2024 -0400 Messy, but monumental: PyTexture::pyObject works this also coincidentally fixes a weird bug I encountered while (mis?)using tp_alloc: by using PyType_GenericAlloc, I avoid the segfault that tp_alloc sometimes causes. See the horrible UIDrawable retrieval macro that I use in UI.h for a workaround that can probably be replaced with this technique commit 2cf8f9431025b3eecb67422e98ec95e3f2b4038f Author: John McCardle <mccardle.john@gmail.com> Date: Wed Mar 20 21:16:52 2024 -0400 Radical new example pattern for exposing a C++ class to Python commit 84a8886da2570a8087f83141f0d41fef65073d99 Author: John McCardle <mccardle.john@gmail.com> Date: Sun Mar 17 16:29:33 2024 -0400 Fixed render issue with UIGrid / PyTexture: wasn't positioning or scaling properly after fetching sprite commit 20f80c4114fce029faf28a195ffdbb041181e484 Author: John McCardle <mccardle.john@gmail.com> Date: Sun Mar 17 16:23:52 2024 -0400 Fixed sprite indexing error in PyTexture; needs non-square sprite tests, but feeling confident! commit afd4ff1925382622abf79db132b6123ee33b946b Author: John McCardle <mccardle.john@gmail.com> Date: Sat Mar 16 21:53:24 2024 -0400 good progress, we're building again. Issue with Grid (tile sprite) textures and I think the sprite indexes are being calculated wrong (x and y transposed?) commit bfd33102d13072eb4624cb07dcf5a55a98e93aa0 Author: John McCardle <mccardle.john@gmail.com> Date: Sat Mar 16 14:52:35 2024 -0400 Squashed basically all the compile bugs in UISprite, but UIEntity and UIGrid use textures as well, so they need to be fixed too before the project will build again commit 47d0e34a17eb4aa86f781d0a022180d637cec602 Author: John McCardle <mccardle.john@gmail.com> Date: Sat Mar 16 11:31:39 2024 -0400 Initial PyTexture class no testing done. should enable rectangular (non-square) textures "sprite" method; let's just overwrite sprites with texture coords Hoping to replace awful code like: `self->data->sprite.sprite.setTextureRect(self->data->sprite.itex->spriteCoordinates(val));` with something like: `self->data->sprite = self->data->texture->sprite(val);`
2024-03-21 22:24:42 -04:00
.tp_name = "mcrfpy.Texture",
.tp_basicsize = sizeof(PyTextureObject),
.tp_itemsize = 0,
.tp_repr = PyTexture::repr,
Squashed commit of the following: [standardize_texture_handling] closes #18 commit b114ec308518607fad5777c6c1b516381e2a78b9 Author: John McCardle <mccardle.john@gmail.com> Date: Thu Mar 21 22:22:35 2024 -0400 cleaning up for merge commit d7228172c4d5455159cd91782b49304a971bb444 Author: John McCardle <mccardle.john@gmail.com> Date: Thu Mar 21 21:39:15 2024 -0400 Messy, but monumental: PyTexture::pyObject works this also coincidentally fixes a weird bug I encountered while (mis?)using tp_alloc: by using PyType_GenericAlloc, I avoid the segfault that tp_alloc sometimes causes. See the horrible UIDrawable retrieval macro that I use in UI.h for a workaround that can probably be replaced with this technique commit 2cf8f9431025b3eecb67422e98ec95e3f2b4038f Author: John McCardle <mccardle.john@gmail.com> Date: Wed Mar 20 21:16:52 2024 -0400 Radical new example pattern for exposing a C++ class to Python commit 84a8886da2570a8087f83141f0d41fef65073d99 Author: John McCardle <mccardle.john@gmail.com> Date: Sun Mar 17 16:29:33 2024 -0400 Fixed render issue with UIGrid / PyTexture: wasn't positioning or scaling properly after fetching sprite commit 20f80c4114fce029faf28a195ffdbb041181e484 Author: John McCardle <mccardle.john@gmail.com> Date: Sun Mar 17 16:23:52 2024 -0400 Fixed sprite indexing error in PyTexture; needs non-square sprite tests, but feeling confident! commit afd4ff1925382622abf79db132b6123ee33b946b Author: John McCardle <mccardle.john@gmail.com> Date: Sat Mar 16 21:53:24 2024 -0400 good progress, we're building again. Issue with Grid (tile sprite) textures and I think the sprite indexes are being calculated wrong (x and y transposed?) commit bfd33102d13072eb4624cb07dcf5a55a98e93aa0 Author: John McCardle <mccardle.john@gmail.com> Date: Sat Mar 16 14:52:35 2024 -0400 Squashed basically all the compile bugs in UISprite, but UIEntity and UIGrid use textures as well, so they need to be fixed too before the project will build again commit 47d0e34a17eb4aa86f781d0a022180d637cec602 Author: John McCardle <mccardle.john@gmail.com> Date: Sat Mar 16 11:31:39 2024 -0400 Initial PyTexture class no testing done. should enable rectangular (non-square) textures "sprite" method; let's just overwrite sprites with texture coords Hoping to replace awful code like: `self->data->sprite.sprite.setTextureRect(self->data->sprite.itex->spriteCoordinates(val));` with something like: `self->data->sprite = self->data->texture->sprite(val);`
2024-03-21 22:24:42 -04:00
.tp_hash = PyTexture::hash,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = PyDoc_STR("SFML Texture Object"),
//.tp_base = &PyBaseObject_Type,
Squashed commit of the following: [standardize_texture_handling] closes #18 commit b114ec308518607fad5777c6c1b516381e2a78b9 Author: John McCardle <mccardle.john@gmail.com> Date: Thu Mar 21 22:22:35 2024 -0400 cleaning up for merge commit d7228172c4d5455159cd91782b49304a971bb444 Author: John McCardle <mccardle.john@gmail.com> Date: Thu Mar 21 21:39:15 2024 -0400 Messy, but monumental: PyTexture::pyObject works this also coincidentally fixes a weird bug I encountered while (mis?)using tp_alloc: by using PyType_GenericAlloc, I avoid the segfault that tp_alloc sometimes causes. See the horrible UIDrawable retrieval macro that I use in UI.h for a workaround that can probably be replaced with this technique commit 2cf8f9431025b3eecb67422e98ec95e3f2b4038f Author: John McCardle <mccardle.john@gmail.com> Date: Wed Mar 20 21:16:52 2024 -0400 Radical new example pattern for exposing a C++ class to Python commit 84a8886da2570a8087f83141f0d41fef65073d99 Author: John McCardle <mccardle.john@gmail.com> Date: Sun Mar 17 16:29:33 2024 -0400 Fixed render issue with UIGrid / PyTexture: wasn't positioning or scaling properly after fetching sprite commit 20f80c4114fce029faf28a195ffdbb041181e484 Author: John McCardle <mccardle.john@gmail.com> Date: Sun Mar 17 16:23:52 2024 -0400 Fixed sprite indexing error in PyTexture; needs non-square sprite tests, but feeling confident! commit afd4ff1925382622abf79db132b6123ee33b946b Author: John McCardle <mccardle.john@gmail.com> Date: Sat Mar 16 21:53:24 2024 -0400 good progress, we're building again. Issue with Grid (tile sprite) textures and I think the sprite indexes are being calculated wrong (x and y transposed?) commit bfd33102d13072eb4624cb07dcf5a55a98e93aa0 Author: John McCardle <mccardle.john@gmail.com> Date: Sat Mar 16 14:52:35 2024 -0400 Squashed basically all the compile bugs in UISprite, but UIEntity and UIGrid use textures as well, so they need to be fixed too before the project will build again commit 47d0e34a17eb4aa86f781d0a022180d637cec602 Author: John McCardle <mccardle.john@gmail.com> Date: Sat Mar 16 11:31:39 2024 -0400 Initial PyTexture class no testing done. should enable rectangular (non-square) textures "sprite" method; let's just overwrite sprites with texture coords Hoping to replace awful code like: `self->data->sprite.sprite.setTextureRect(self->data->sprite.itex->spriteCoordinates(val));` with something like: `self->data->sprite = self->data->texture->sprite(val);`
2024-03-21 22:24:42 -04:00
.tp_init = (initproc)PyTexture::init,
.tp_new = PyType_GenericNew, //PyTexture::pynew,
Squashed commit of the following: [standardize_texture_handling] closes #18 commit b114ec308518607fad5777c6c1b516381e2a78b9 Author: John McCardle <mccardle.john@gmail.com> Date: Thu Mar 21 22:22:35 2024 -0400 cleaning up for merge commit d7228172c4d5455159cd91782b49304a971bb444 Author: John McCardle <mccardle.john@gmail.com> Date: Thu Mar 21 21:39:15 2024 -0400 Messy, but monumental: PyTexture::pyObject works this also coincidentally fixes a weird bug I encountered while (mis?)using tp_alloc: by using PyType_GenericAlloc, I avoid the segfault that tp_alloc sometimes causes. See the horrible UIDrawable retrieval macro that I use in UI.h for a workaround that can probably be replaced with this technique commit 2cf8f9431025b3eecb67422e98ec95e3f2b4038f Author: John McCardle <mccardle.john@gmail.com> Date: Wed Mar 20 21:16:52 2024 -0400 Radical new example pattern for exposing a C++ class to Python commit 84a8886da2570a8087f83141f0d41fef65073d99 Author: John McCardle <mccardle.john@gmail.com> Date: Sun Mar 17 16:29:33 2024 -0400 Fixed render issue with UIGrid / PyTexture: wasn't positioning or scaling properly after fetching sprite commit 20f80c4114fce029faf28a195ffdbb041181e484 Author: John McCardle <mccardle.john@gmail.com> Date: Sun Mar 17 16:23:52 2024 -0400 Fixed sprite indexing error in PyTexture; needs non-square sprite tests, but feeling confident! commit afd4ff1925382622abf79db132b6123ee33b946b Author: John McCardle <mccardle.john@gmail.com> Date: Sat Mar 16 21:53:24 2024 -0400 good progress, we're building again. Issue with Grid (tile sprite) textures and I think the sprite indexes are being calculated wrong (x and y transposed?) commit bfd33102d13072eb4624cb07dcf5a55a98e93aa0 Author: John McCardle <mccardle.john@gmail.com> Date: Sat Mar 16 14:52:35 2024 -0400 Squashed basically all the compile bugs in UISprite, but UIEntity and UIGrid use textures as well, so they need to be fixed too before the project will build again commit 47d0e34a17eb4aa86f781d0a022180d637cec602 Author: John McCardle <mccardle.john@gmail.com> Date: Sat Mar 16 11:31:39 2024 -0400 Initial PyTexture class no testing done. should enable rectangular (non-square) textures "sprite" method; let's just overwrite sprites with texture coords Hoping to replace awful code like: `self->data->sprite.sprite.setTextureRect(self->data->sprite.itex->spriteCoordinates(val));` with something like: `self->data->sprite = self->data->texture->sprite(val);`
2024-03-21 22:24:42 -04:00
};
}