Skip to content

Commit abec5d1

Browse files
committed
Update to SFML 3.x vector-based sf::Rect
1 parent 714a758 commit abec5d1

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

imgui-SFML.cpp

+7-14
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
#include <cassert>
1919
#include <cmath> // abs
20-
#include <cstddef> // offsetof, nullptr, size_t
21-
#include <cstdint> // uint8_t
2220
#include <cstring> // memcpy
2321

2422
#include <algorithm>
@@ -673,18 +671,16 @@ void Image(const sf::RenderTexture& texture, const sf::Vector2f& size, const sf:
673671
/////////////// Image Overloads for sf::Sprite
674672

675673
void Image(const sf::Sprite& sprite, const sf::Color& tintColor, const sf::Color& borderColor) {
676-
const sf::FloatRect bounds = sprite.getGlobalBounds();
677-
Image(sprite, sf::Vector2f(bounds.width, bounds.height), tintColor, borderColor);
674+
Image(sprite, sprite.getGlobalBounds().size, tintColor, borderColor);
678675
}
679676

680677
void Image(const sf::Sprite& sprite, const sf::Vector2f& size, const sf::Color& tintColor,
681678
const sf::Color& borderColor) {
682679
const sf::Texture& texture = sprite.getTexture();
683680
const sf::Vector2f textureSize(texture.getSize());
684681
const sf::FloatRect textureRect(sprite.getTextureRect());
685-
const ImVec2 uv0(textureRect.left / textureSize.x, textureRect.top / textureSize.y);
686-
const ImVec2 uv1((textureRect.left + textureRect.width) / textureSize.x,
687-
(textureRect.top + textureRect.height) / textureSize.y);
682+
const ImVec2 uv0(textureRect.position.cwiseDiv(textureSize));
683+
const ImVec2 uv1((textureRect.position + textureRect.size).cwiseDiv(textureSize));
688684

689685
ImTextureID textureID = convertGLTextureHandleToImTextureID(texture.getNativeHandle());
690686

@@ -722,9 +718,8 @@ bool ImageButton(const char* id, const sf::Sprite& sprite, const sf::Vector2f& s
722718
const sf::Texture& texture = sprite.getTexture();
723719
const sf::Vector2f textureSize(texture.getSize());
724720
const sf::FloatRect textureRect(sprite.getTextureRect());
725-
const ImVec2 uv0(textureRect.left / textureSize.x, textureRect.top / textureSize.y);
726-
const ImVec2 uv1((textureRect.left + textureRect.width) / textureSize.x,
727-
(textureRect.top + textureRect.height) / textureSize.y);
721+
const ImVec2 uv0(textureRect.position.cwiseDiv(textureSize));
722+
const ImVec2 uv1((textureRect.position + textureRect.size).cwiseDiv(textureSize));
728723

729724
ImTextureID textureID = convertGLTextureHandleToImTextureID(texture.getNativeHandle());
730725
return ImGui::ImageButton(id, textureID, ImVec2(size.x, size.y), uv0, uv1, toImColor(bgColor),
@@ -764,12 +759,10 @@ ImColor toImColor(sf::Color c) {
764759
static_cast<int>(c.a)};
765760
}
766761
ImVec2 getTopLeftAbsolute(const sf::FloatRect& rect) {
767-
const ImVec2 pos = ImGui::GetCursorScreenPos();
768-
return {rect.left + pos.x, rect.top + pos.y};
762+
return sf::Vector2f(ImGui::GetCursorScreenPos()) + rect.position;
769763
}
770764
ImVec2 getDownRightAbsolute(const sf::FloatRect& rect) {
771-
const ImVec2 pos = ImGui::GetCursorScreenPos();
772-
return {rect.left + rect.width + pos.x, rect.top + rect.height + pos.y};
765+
return sf::Vector2f(ImGui::GetCursorScreenPos()) + rect.position + rect.size;
773766
}
774767

775768
ImTextureID convertGLTextureHandleToImTextureID(GLuint glTextureHandle) {

0 commit comments

Comments
 (0)