From 47d0e34a17eb4aa86f781d0a022180d637cec602 Mon Sep 17 00:00:00 2001 From: John McCardle Date: Sat, 16 Mar 2024 11:31:39 -0400 Subject: [PATCH] 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);` --- src/PyTexture.cpp | 20 ++++++++++++++++++++ src/PyTexture.h | 14 ++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/PyTexture.cpp create mode 100644 src/PyTexture.h diff --git a/src/PyTexture.cpp b/src/PyTexture.cpp new file mode 100644 index 0000000..0a16ed9 --- /dev/null +++ b/src/PyTexture.cpp @@ -0,0 +1,20 @@ +#include "PyTexture.h" + +PyTexture::PyTexture(std::string filename, int sprite_w, int sprite_h) +: sprite_width(sprite_w), sprite_height(sprite_h) +{ + // TODO - get image resolution and get sheet width and height + sheet_width = 0; + sheet_height = 0; + source = filename; +} + +sf::Sprite PyTexture::sprite(int index, float x = 0.0, float y = 0.0, float s = 1.0) +{ + int tx = index % sprite_width, ty = index / sprite_height; + auto ir = sf::IntRect(tx * sprite_width, ty * sprite_height, sprite_width, sprite_height); + auto sprite = sf::Sprite(texture, ir); + sprite.setPosition(x, y); + sprite.setScale(s, s); + return sprite; +} diff --git a/src/PyTexture.h b/src/PyTexture.h new file mode 100644 index 0000000..288bbfb --- /dev/null +++ b/src/PyTexture.h @@ -0,0 +1,14 @@ +#pragma once +#include "Common.h" +#include "Python.h" + +class PyTexture +{ +private: + sf::Texture texture; + std::string source; + int sprite_width, sprite_height, sheet_width, sheet_height; +public: + PyTexture(std::string filename, int sprite_w, int sprite_h); + sf::Sprite sprite(int index, float x = 0.0, float y = 0.0, float s = 1.0); +};