bugfix: use float instead of int for modMenu/listMenus API calls, as this was corrupting the coordinates of sprites on uimenus

This commit is contained in:
John McCardle 2023-03-11 17:15:06 -05:00
parent b4daac6e0c
commit 29ac89b489
5 changed files with 22 additions and 10 deletions

View File

@ -12,9 +12,13 @@ sf::Sprite IndexSprite::drawable()
s.setTexture(tex.texture); s.setTexture(tex.texture);
s.setScale(sf::Vector2f(scale, scale)); s.setScale(sf::Vector2f(scale, scale));
s.setPosition(sf::Vector2f(x, y)); s.setPosition(sf::Vector2f(x, y));
//std::cout << "Drawable position: " << x << ", " << y << " -> " << s.getPosition().x << ", " << s.getPosition().y << std::endl;
s.setTextureRect(tex.spriteCoordinates(sprite_index)); s.setTextureRect(tex.spriteCoordinates(sprite_index));
return s; return s;
} }
IndexSprite::IndexSprite(int _ti, int _si, int _x, int _y, float _s): IndexSprite::IndexSprite(int _ti, int _si, float _x, float _y, float _s):
texture_index(_ti), sprite_index(_si), x(_x), y(_y), scale(_s) {} texture_index(_ti), sprite_index(_si), x(_x), y(_y), scale(_s) {
//std::cout << "IndexSprite constructed with x, y " << _x << ", " << _y << std::endl;
//std::cout << " * Stored x, y " << x << ", " << y << std::endl;
}

View File

@ -9,5 +9,5 @@ public:
float scale; float scale;
static GameEngine* game; static GameEngine* game;
sf::Sprite drawable(); sf::Sprite drawable();
IndexSprite(int, int, int, int, float); IndexSprite(int, int, float, float, float);
}; };

View File

@ -333,7 +333,7 @@ PyObject* McRFPy_API::_listMenus(PyObject*, PyObject*) {
// Loop: Convert Sprite objects to Python Objects // Loop: Convert Sprite objects to Python Objects
PyObject* sprite_list = PyObject_GetAttrString(menuobj, "sprites"); PyObject* sprite_list = PyObject_GetAttrString(menuobj, "sprites");
for (auto& s : menu->sprites) { for (auto& s : menu->sprites) {
PyObject* spr_args = Py_BuildValue("(iiii)", PyObject* spr_args = Py_BuildValue("(iiff)",
s.texture_index, s.sprite_index, s.x, s.y); s.texture_index, s.sprite_index, s.x, s.y);
PyObject* sprobj = PyObject_CallObject((PyObject*) spr_type, spr_args); PyObject* sprobj = PyObject_CallObject((PyObject*) spr_type, spr_args);
@ -436,9 +436,9 @@ PyObject* McRFPy_API::_modMenu(PyObject* self, PyObject* args) {
menu->sprites[i].sprite_index = menu->sprites[i].sprite_index =
PyLong_AsLong(PyObject_GetAttrString(spriteobj, "sprite_index")); PyLong_AsLong(PyObject_GetAttrString(spriteobj, "sprite_index"));
menu->sprites[i].x = menu->sprites[i].x =
PyLong_AsLong(PyObject_GetAttrString(spriteobj, "x")); PyFloat_AsDouble(PyObject_GetAttrString(spriteobj, "x"));
menu->sprites[i].y = menu->sprites[i].y =
PyLong_AsLong(PyObject_GetAttrString(spriteobj, "y")); PyFloat_AsDouble(PyObject_GetAttrString(spriteobj, "y"));
} }
Py_INCREF(Py_None); Py_INCREF(Py_None);
@ -485,9 +485,11 @@ PyObject* McRFPy_API::_listTextures(PyObject*, PyObject*) {
PyObject* McRFPy_API::_createSprite(PyObject* self, PyObject* args) { PyObject* McRFPy_API::_createSprite(PyObject* self, PyObject* args) {
const char * menu_cstr; const char * menu_cstr;
int ti, si, x, y; int ti, si;
float x, y;
float s; float s;
if (!PyArg_ParseTuple(args, "siiiif", &menu_cstr, &ti, &si, &x, &y, &s)) return NULL; if (!PyArg_ParseTuple(args, "siifff", &menu_cstr, &ti, &si, &x, &y, &s)) return NULL;
//std::cout << "Creating uisprite " << ti << " " << si << " " << x << " " << y << " " << s << " " << std::endl;
createSprite(std::string(menu_cstr), ti, si, x, y, s); createSprite(std::string(menu_cstr), ti, si, x, y, s);
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
@ -511,10 +513,11 @@ void McRFPy_API::createButton(std::string menukey, int x, int y, int w, int h, s
menu->add_button(b); menu->add_button(b);
} }
void McRFPy_API::createSprite(std::string menukey, int ti, int si, int x, int y, float scale) { void McRFPy_API::createSprite(std::string menukey, int ti, int si, float x, float y, float scale) {
auto menu = menus[menukey]; auto menu = menus[menukey];
auto s = IndexSprite(ti, si, x, y, scale); auto s = IndexSprite(ti, si, x, y, scale);
menu->add_sprite(s); menu->add_sprite(s);
//std::cout << "indexsprite just created has values x,y " << s.x << ", " << s.y << std::endl;
} }
int McRFPy_API::createTexture(std::string filename, int grid_size, int grid_width, int grid_height) { int McRFPy_API::createTexture(std::string filename, int grid_size, int grid_width, int grid_height) {

View File

@ -122,7 +122,7 @@ public:
static UIMenu* createMenu(int posx, int posy, int sizex, int sizey); static UIMenu* createMenu(int posx, int posy, int sizex, int sizey);
static void createCaption(std::string menukey, std::string text, int fontsize, sf::Color textcolor); static void createCaption(std::string menukey, std::string text, int fontsize, sf::Color textcolor);
static void createButton(std::string menukey, int x, int y, int w, int h, sf::Color bgcolor, sf::Color textcolor, std::string caption, std::string action); static void createButton(std::string menukey, int x, int y, int w, int h, sf::Color bgcolor, sf::Color textcolor, std::string caption, std::string action);
static void createSprite(std::string menukey, int ti, int si, int x, int y, float scale); static void createSprite(std::string menukey, int ti, int si, float x, float y, float scale);
static int createTexture(std::string filename, int grid_size, int grid_width, int grid_height); static int createTexture(std::string filename, int grid_size, int grid_width, int grid_height);
//static void playSound(const char * filename); //static void playSound(const char * filename);
//static void playMusic(const char * filename); //static void playMusic(const char * filename);

View File

@ -15,7 +15,11 @@ void UIMenu::render(sf::RenderWindow & window)
window.draw(box); window.draw(box);
for (auto& s: sprites) { for (auto& s: sprites) {
auto _s = s.drawable(); auto _s = s.drawable();
//std::cout << "Sprite has values " << s.x << ", " << s.y << std::endl;
//std::cout << "Drawable generated @ " << _s.getPosition().x << ", " << _s.getPosition().y << std::endl;
_s.move(box.getPosition()); _s.move(box.getPosition());
//std::cout << "Moved by " << box.getPosition().x << ", " << box.getPosition().y << std::endl;
//std::cout << "Render UIMenu Sprite @ " << _s.getPosition().x << ", " << _s.getPosition().y << std::endl;
window.draw(_s); window.draw(_s);
} }
for (auto& c : captions) { for (auto& c : captions) {
@ -64,6 +68,7 @@ void UIMenu::add_button(Button b)
void UIMenu::add_sprite(IndexSprite s) void UIMenu::add_sprite(IndexSprite s)
{ {
//std::cout << "Adding sprite to UIMenu x,y " << s.x << ", " << s.y << std::endl;
sprites.push_back(s); sprites.push_back(s);
} }