Skip to content

Commit

Permalink
fix: SFML bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
AsPJT committed Jan 1, 2024
1 parent 4d3e9f0 commit 13500a7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 22 deletions.
14 changes: 13 additions & 1 deletion Library/PAX_GRAPHICA/Rect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 33 additions & 12 deletions Library/PAX_GRAPHICA/String.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <DxLib.h>
#elif defined(PAXS_USING_SFML)
#include <SFML/Graphics.hpp>
#include <SFML/System/Utf.hpp>
#endif

#include <PAX_GRAPHICA/Color.hpp>
Expand Down Expand Up @@ -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());
Expand All @@ -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);
}

Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions Library/PAX_GRAPHICA/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>(pos.x() - (width() / 2)), static_cast<float>(pos.y() - (height() / 2)));
paxg::Window::window.draw(sprite);
}
#else
Expand All @@ -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<float>(pos.x()), static_cast<float>(pos.y()));
sprite.setPosition(static_cast<float>(pos.x() - (width() / 2)), static_cast<float>(pos.y() - (height() / 2)));
paxg::Window::window.draw(sprite);
}
#else
Expand All @@ -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<float>(resize.x()) / texture.getSize().x, static_cast<float>(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
Expand All @@ -231,7 +231,7 @@ namespace paxg {
void resizedDrawAt(const int resize, const paxg::Vec2i & pos) const {
sf::Sprite sprite(texture);
sprite.setScale(static_cast<float>(resize) / texture.getSize().x, static_cast<float>(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
Expand All @@ -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
Expand All @@ -275,7 +275,7 @@ namespace paxg {
void resizedDrawAt(const int resize, const paxg::Vec2f & pos) const {
sf::Sprite sprite(texture);
sprite.setScale(static_cast<float>(resize) / texture.getSize().x, static_cast<float>(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
Expand Down
7 changes: 4 additions & 3 deletions Library/PAX_MAHOROBA/Main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include <PAX_GRAPHICA/Vec2.hpp>

#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 };
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 13500a7

Please sign in to comment.