diff --git a/Library/PAX_GRAPHICA/Rect.hpp b/Library/PAX_GRAPHICA/Rect.hpp index 49e2f7738..ff0b44538 100644 --- a/Library/PAX_GRAPHICA/Rect.hpp +++ b/Library/PAX_GRAPHICA/Rect.hpp @@ -253,7 +253,19 @@ namespace paxg { return false; #elif defined(PAXS_USING_SFML) - return rect.getGlobalBounds().contains(sf::Mouse::getPosition(Window::window).x, sf::Mouse::getPosition(Window::window).y); + if (old_left_mouse) { + // 1 フレーム前にタッチされている + if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) { + int mx = sf::Mouse::getPosition(Window::window).x, my = sf::Mouse::getPosition(Window::window).y; + return (mx >= rect.getPosition().x && + my >= rect.getPosition().y && + mx < rect.getPosition().x + rect.getSize().x && + my < rect.getPosition().y + rect.getSize().y); + } + } + + return false; + // return rect.getGlobalBounds().contains(sf::Mouse::getPosition(Window::window).x, sf::Mouse::getPosition(Window::window).y); #else return false; diff --git a/Library/PAX_GRAPHICA/String.hpp b/Library/PAX_GRAPHICA/String.hpp index b100efb64..08cdcd4b4 100644 --- a/Library/PAX_GRAPHICA/String.hpp +++ b/Library/PAX_GRAPHICA/String.hpp @@ -24,6 +24,7 @@ #include #elif defined(PAXS_USING_SFML) #include +#include #endif #include @@ -273,27 +274,33 @@ namespace paxg { void drawBottomLeft(const std::string& str, const paxg::Vec2i& pos, const paxg::Color& color) const { sf::Text text; text.setFont(font); - text.setString(str); + std::wstring wstr; + sf::Utf<8>::toWide(str.begin(), str.end(), std::back_inserter(wstr)); + text.setString(wstr); text.setCharacterSize(size); text.setFillColor(color.color); - text.setPosition(pos.x(), pos.y() - 10); + text.setPosition(pos.x(), pos.y() - size / 2); paxg::Window::window.draw(text); } void drawTopRight(const std::string& str, const paxg::Vec2i& pos, const paxg::Color& color) const { sf::Text text; text.setFont(font); - text.setString(str); + std::wstring wstr; + sf::Utf<8>::toWide(str.begin(), str.end(), std::back_inserter(wstr)); + text.setString(wstr); text.setCharacterSize(size); text.setFillColor(color.color); - text.setPosition(pos.x(), pos.y() + 10); + text.setPosition(pos.x() - text.getGlobalBounds().width, pos.y() + size / 2); paxg::Window::window.draw(text); } void draw(const std::string& str, const paxg::Vec2i& pos, const paxg::Color& color) const { sf::Text text; text.setFont(font); - text.setString(str); + std::wstring wstr; + sf::Utf<8>::toWide(str.begin(), str.end(), std::back_inserter(wstr)); + text.setString(wstr); text.setCharacterSize(size); text.setFillColor(color.color); text.setPosition(pos.x(), pos.y()); @@ -303,30 +310,36 @@ namespace paxg { void drawBottomCenter(const std::string& str, const paxg::Vec2i& pos, const paxg::Color& color) const { sf::Text text; text.setFont(font); - text.setString(str); + std::wstring wstr; + sf::Utf<8>::toWide(str.begin(), str.end(), std::back_inserter(wstr)); + text.setString(wstr); text.setCharacterSize(size); text.setFillColor(color.color); - text.setPosition(pos.x(), pos.y() - 10); + text.setPosition(pos.x() - text.getGlobalBounds().width / 2, pos.y() - size / 2); paxg::Window::window.draw(text); } void drawTopCenter(const std::string& str, const paxg::Vec2i& pos, const paxg::Color& color) const { sf::Text text; text.setFont(font); - text.setString(str); + std::wstring wstr; + sf::Utf<8>::toWide(str.begin(), str.end(), std::back_inserter(wstr)); + text.setString(wstr); text.setCharacterSize(size); text.setFillColor(color.color); - text.setPosition(pos.x(), pos.y() + 10); + text.setPosition(pos.x() - text.getGlobalBounds().width / 2, pos.y() + size / 2); paxg::Window::window.draw(text); } void drawAt(const std::string& str, const paxg::Vec2i& pos, const paxg::Color& color) const { sf::Text text; text.setFont(font); - text.setString(str); + std::wstring wstr; + sf::Utf<8>::toWide(str.begin(), str.end(), std::back_inserter(wstr)); + text.setString(wstr); text.setCharacterSize(size); text.setFillColor(color.color); - text.setPosition(pos.x(), pos.y()); + text.setPosition(pos.x() - text.getGlobalBounds().width / 2, pos.y()); paxg::Window::window.draw(text); } @@ -335,7 +348,15 @@ namespace paxg { } int width(const std::string& str_) { - return str_.size() * size * 0.5; + sf::Text text; + text.setFont(font); + std::wstring wstr; + sf::Utf<8>::toWide(str_.begin(), str_.end(), std::back_inserter(wstr)); + text.setString(wstr); + text.setCharacterSize(size); + + return text.getGlobalBounds().width; + // return str_.size() * size * 0.5; } #else diff --git a/Library/PAX_GRAPHICA/Texture.hpp b/Library/PAX_GRAPHICA/Texture.hpp index c35b4be7c..e320892a7 100644 --- a/Library/PAX_GRAPHICA/Texture.hpp +++ b/Library/PAX_GRAPHICA/Texture.hpp @@ -169,7 +169,7 @@ namespace paxg { #elif defined(PAXS_USING_SFML) void drawAt(const paxg::Vec2f& pos) const override { sf::Sprite sprite(texture); - sprite.setPosition(pos); + sprite.setPosition(static_cast(pos.x() - (width() / 2)), static_cast(pos.y() - (height() / 2))); paxg::Window::window.draw(sprite); } #else @@ -187,7 +187,7 @@ namespace paxg { #elif defined(PAXS_USING_SFML) void drawAt(const paxg::Vec2i& pos) const override { sf::Sprite sprite(texture); - sprite.setPosition(static_cast(pos.x()), static_cast(pos.y())); + sprite.setPosition(static_cast(pos.x() - (width() / 2)), static_cast(pos.y() - (height() / 2))); paxg::Window::window.draw(sprite); } #else @@ -209,7 +209,7 @@ namespace paxg { void resizedDrawAt(const paxg::Vec2i& resize, const paxg::Vec2i& pos) const { sf::Sprite sprite(texture); sprite.setScale(static_cast(resize.x()) / texture.getSize().x, static_cast(resize.y()) / texture.getSize().y); - sprite.setPosition(pos.x(), pos.y()); + sprite.setPosition(pos.x() - (resize.x() / 2), pos.y() - (resize.y() / 2)); paxg::Window::window.draw(sprite); } #else @@ -231,7 +231,7 @@ namespace paxg { void resizedDrawAt(const int resize, const paxg::Vec2i & pos) const { sf::Sprite sprite(texture); sprite.setScale(static_cast(resize) / texture.getSize().x, static_cast(resize) / texture.getSize().y); - sprite.setPosition(pos.x(), pos.y()); + sprite.setPosition(pos.x() - (resize / 2), pos.y() - (resize / 2)); paxg::Window::window.draw(sprite); } #else @@ -253,7 +253,7 @@ namespace paxg { void resizedDrawAt(const paxg::Vec2f & resize, const paxg::Vec2f & pos) const { sf::Sprite sprite(texture); sprite.setScale(resize.x() / texture.getSize().x, resize.y() / texture.getSize().y); - sprite.setPosition(pos.x(), pos.y()); + sprite.setPosition(pos.x() - (resize.x() / 2), pos.y() - (resize.y() / 2)); paxg::Window::window.draw(sprite); } #else @@ -275,7 +275,7 @@ namespace paxg { void resizedDrawAt(const int resize, const paxg::Vec2f & pos) const { sf::Sprite sprite(texture); sprite.setScale(static_cast(resize) / texture.getSize().x, static_cast(resize) / texture.getSize().y); - sprite.setPosition(pos.x(), pos.y()); + sprite.setPosition(pos.x() - (resize / 2), pos.y() - (resize / 2)); paxg::Window::window.draw(sprite); } #else diff --git a/Library/PAX_MAHOROBA/Main.hpp b/Library/PAX_MAHOROBA/Main.hpp index 38d1bc324..de0966ae5 100644 --- a/Library/PAX_MAHOROBA/Main.hpp +++ b/Library/PAX_MAHOROBA/Main.hpp @@ -18,7 +18,7 @@ #include -#ifdef PAXS_USING_DXLIB +#if defined(PAXS_USING_DXLIB) || defined(PAXS_USING_SFML) static bool old_left_mouse = false; static int old_left_touch = 0; static paxg::Vec2i old_left_touch_pos = paxg::Vec2i{ 0,0 }; @@ -109,7 +109,7 @@ namespace paxs { std::size_t pop_num = 0; // 人口数 std::size_t sat_num = 0; // 集落数 - std::ofstream pop_ofs("pop.txt"); + //std::ofstream pop_ofs("pop.txt"); #ifdef PAXS_USING_SFML paxg::Window::window.setFramerateLimit(60); @@ -204,7 +204,7 @@ namespace paxs { sat_num // 集落数 ); if (is_sim) { - pop_ofs << pop_num << '\t' << sat_num << '\n'; + //pop_ofs << pop_num << '\t' << sat_num << '\n'; } #ifdef PAXS_USING_DXLIB @@ -217,6 +217,7 @@ namespace paxs { } #endif #ifdef PAXS_USING_SFML + old_left_mouse = !sf::Mouse::isButtonPressed(sf::Mouse::Button::Left); paxg::Window::display(); #endif }