From 29ac89b489eefcac10031eb4423d3665110613bc Mon Sep 17 00:00:00 2001 From: John McCardle Date: Sat, 11 Mar 2023 17:15:06 -0500 Subject: [PATCH] bugfix: use float instead of int for modMenu/listMenus API calls, as this was corrupting the coordinates of sprites on uimenus --- src/IndexSprite.cpp | 8 ++++++-- src/IndexSprite.h | 2 +- src/McRFPy_API.cpp | 15 +++++++++------ src/McRFPy_API.h | 2 +- src/UIMenu.cpp | 5 +++++ 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/IndexSprite.cpp b/src/IndexSprite.cpp index 16d78db..526c1e2 100644 --- a/src/IndexSprite.cpp +++ b/src/IndexSprite.cpp @@ -12,9 +12,13 @@ sf::Sprite IndexSprite::drawable() s.setTexture(tex.texture); s.setScale(sf::Vector2f(scale, scale)); 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)); return s; } -IndexSprite::IndexSprite(int _ti, int _si, int _x, int _y, float _s): - texture_index(_ti), sprite_index(_si), x(_x), y(_y), scale(_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) { + //std::cout << "IndexSprite constructed with x, y " << _x << ", " << _y << std::endl; + //std::cout << " * Stored x, y " << x << ", " << y << std::endl; +} diff --git a/src/IndexSprite.h b/src/IndexSprite.h index a5b4d39..76b053b 100644 --- a/src/IndexSprite.h +++ b/src/IndexSprite.h @@ -9,5 +9,5 @@ public: float scale; static GameEngine* game; sf::Sprite drawable(); - IndexSprite(int, int, int, int, float); + IndexSprite(int, int, float, float, float); }; diff --git a/src/McRFPy_API.cpp b/src/McRFPy_API.cpp index 1ef7ad9..8c8891d 100644 --- a/src/McRFPy_API.cpp +++ b/src/McRFPy_API.cpp @@ -333,7 +333,7 @@ PyObject* McRFPy_API::_listMenus(PyObject*, PyObject*) { // Loop: Convert Sprite objects to Python Objects PyObject* sprite_list = PyObject_GetAttrString(menuobj, "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); 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 = PyLong_AsLong(PyObject_GetAttrString(spriteobj, "sprite_index")); menu->sprites[i].x = - PyLong_AsLong(PyObject_GetAttrString(spriteobj, "x")); + PyFloat_AsDouble(PyObject_GetAttrString(spriteobj, "x")); menu->sprites[i].y = - PyLong_AsLong(PyObject_GetAttrString(spriteobj, "y")); + PyFloat_AsDouble(PyObject_GetAttrString(spriteobj, "y")); } Py_INCREF(Py_None); @@ -485,9 +485,11 @@ PyObject* McRFPy_API::_listTextures(PyObject*, PyObject*) { PyObject* McRFPy_API::_createSprite(PyObject* self, PyObject* args) { const char * menu_cstr; - int ti, si, x, y; + int ti, si; + float x, y; 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); Py_INCREF(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); } -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 s = IndexSprite(ti, si, x, y, scale); 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) { diff --git a/src/McRFPy_API.h b/src/McRFPy_API.h index 240a80f..6e7669a 100644 --- a/src/McRFPy_API.h +++ b/src/McRFPy_API.h @@ -122,7 +122,7 @@ public: 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 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 void playSound(const char * filename); //static void playMusic(const char * filename); diff --git a/src/UIMenu.cpp b/src/UIMenu.cpp index 61f09c0..34cda11 100644 --- a/src/UIMenu.cpp +++ b/src/UIMenu.cpp @@ -15,7 +15,11 @@ void UIMenu::render(sf::RenderWindow & window) window.draw(box); for (auto& s: sprites) { 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()); + //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); } for (auto& c : captions) { @@ -64,6 +68,7 @@ void UIMenu::add_button(Button b) void UIMenu::add_sprite(IndexSprite s) { + //std::cout << "Adding sprite to UIMenu x,y " << s.x << ", " << s.y << std::endl; sprites.push_back(s); }