Skip to content

Commit ee9f48c

Browse files
vittorioromeoChrisThrasher
authored andcommitted
Update to SFML 3.x vector-based sf::Rect
1 parent 714a758 commit ee9f48c

File tree

1 file changed

+43
-37
lines changed

1 file changed

+43
-37
lines changed

imgui-SFML.cpp

+43-37
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>
@@ -119,9 +117,19 @@ static_assert(sizeof(GLuint) <= sizeof(ImTextureID),
119117
namespace {
120118
// various helper functions
121119
ImColor toImColor(sf::Color c);
120+
ImVec2 toImVec2(const sf::Vector2f& v);
121+
sf::Vector2f toSfVector2f(const ImVec2& v);
122122
ImVec2 getTopLeftAbsolute(const sf::FloatRect& rect);
123123
ImVec2 getDownRightAbsolute(const sf::FloatRect& rect);
124124

125+
struct SpriteTextureData {
126+
ImVec2 uv0;
127+
ImVec2 uv1;
128+
ImTextureID textureID{};
129+
};
130+
131+
SpriteTextureData getSpriteTextureData(const sf::Sprite& sprite);
132+
125133
ImTextureID convertGLTextureHandleToImTextureID(GLuint glTextureHandle);
126134
GLuint convertImTextureIDToGLTextureHandle(ImTextureID textureID);
127135

@@ -240,7 +248,7 @@ bool Init(sf::Window& window, const sf::Vector2f& displaySize, bool loadDefaultF
240248
initDefaultJoystickMapping();
241249

242250
// init rendering
243-
io.DisplaySize = ImVec2(displaySize.x, displaySize.y);
251+
io.DisplaySize = toImVec2(displaySize);
244252

245253
// clipboard
246254
io.SetClipboardTextFn = setClipboardText;
@@ -388,7 +396,7 @@ void Update(const sf::Vector2i& mousePos, const sf::Vector2f& displaySize, sf::T
388396
assert(s_currWindowCtx && "No current window is set - forgot to call ImGui::SFML::Init?");
389397

390398
ImGuiIO& io = ImGui::GetIO();
391-
io.DisplaySize = ImVec2(displaySize.x, displaySize.y);
399+
io.DisplaySize = toImVec2(displaySize);
392400
io.DeltaTime = dt.asSeconds();
393401

394402
if (s_currWindowCtx->windowHasFocus) {
@@ -649,8 +657,8 @@ void Image(const sf::Texture& texture, const sf::Vector2f& size, const sf::Color
649657
const sf::Color& borderColor) {
650658
ImTextureID textureID = convertGLTextureHandleToImTextureID(texture.getNativeHandle());
651659

652-
ImGui::Image(textureID, ImVec2(size.x, size.y), ImVec2(0, 0), ImVec2(1, 1),
653-
toImColor(tintColor), toImColor(borderColor));
660+
ImGui::Image(textureID, toImVec2(size), ImVec2(0, 0), ImVec2(1, 1), toImColor(tintColor),
661+
toImColor(borderColor));
654662
}
655663

656664
/////////////// Image Overloads for sf::RenderTexture
@@ -664,32 +672,23 @@ void Image(const sf::RenderTexture& texture, const sf::Vector2f& size, const sf:
664672
ImTextureID textureID =
665673
convertGLTextureHandleToImTextureID(texture.getTexture().getNativeHandle());
666674

667-
ImGui::Image(textureID, ImVec2(size.x, size.y), ImVec2(0, 1),
668-
ImVec2(1, 0), // flipped vertically, because textures in sf::RenderTexture are
669-
// stored this way
675+
ImGui::Image(textureID, toImVec2(size), ImVec2(0, 1), ImVec2(1, 0), // flipped vertically,
676+
// because textures in
677+
// sf::RenderTexture are
678+
// stored this way
670679
toImColor(tintColor), toImColor(borderColor));
671680
}
672681

673682
/////////////// Image Overloads for sf::Sprite
674683

675684
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);
685+
Image(sprite, sprite.getGlobalBounds().size, tintColor, borderColor);
678686
}
679687

680688
void Image(const sf::Sprite& sprite, const sf::Vector2f& size, const sf::Color& tintColor,
681689
const sf::Color& borderColor) {
682-
const sf::Texture& texture = sprite.getTexture();
683-
const sf::Vector2f textureSize(texture.getSize());
684-
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);
688-
689-
ImTextureID textureID = convertGLTextureHandleToImTextureID(texture.getNativeHandle());
690-
691-
ImGui::Image(textureID, ImVec2(size.x, size.y), uv0, uv1, toImColor(tintColor),
692-
toImColor(borderColor));
690+
auto [uv0, uv1, textureID] = getSpriteTextureData(sprite);
691+
ImGui::Image(textureID, toImVec2(size), uv0, uv1, toImColor(tintColor), toImColor(borderColor));
693692
}
694693

695694
/////////////// Image Button Overloads for sf::Texture
@@ -698,7 +697,7 @@ bool ImageButton(const char* id, const sf::Texture& texture, const sf::Vector2f&
698697
const sf::Color& bgColor, const sf::Color& tintColor) {
699698
ImTextureID textureID = convertGLTextureHandleToImTextureID(texture.getNativeHandle());
700699

701-
return ImGui::ImageButton(id, textureID, ImVec2(size.x, size.y), ImVec2(0, 0), ImVec2(1, 1),
700+
return ImGui::ImageButton(id, textureID, toImVec2(size), ImVec2(0, 0), ImVec2(1, 1),
702701
toImColor(bgColor), toImColor(tintColor));
703702
}
704703

@@ -709,7 +708,7 @@ bool ImageButton(const char* id, const sf::RenderTexture& texture, const sf::Vec
709708
ImTextureID textureID =
710709
convertGLTextureHandleToImTextureID(texture.getTexture().getNativeHandle());
711710

712-
return ImGui::ImageButton(id, textureID, ImVec2(size.x, size.y), ImVec2(0, 1),
711+
return ImGui::ImageButton(id, textureID, toImVec2(size), ImVec2(0, 1),
713712
ImVec2(1, 0), // flipped vertically, because textures in
714713
// sf::RenderTexture are stored this way
715714
toImColor(bgColor), toImColor(tintColor));
@@ -719,15 +718,8 @@ bool ImageButton(const char* id, const sf::RenderTexture& texture, const sf::Vec
719718

720719
bool ImageButton(const char* id, const sf::Sprite& sprite, const sf::Vector2f& size,
721720
const sf::Color& bgColor, const sf::Color& tintColor) {
722-
const sf::Texture& texture = sprite.getTexture();
723-
const sf::Vector2f textureSize(texture.getSize());
724-
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);
728-
729-
ImTextureID textureID = convertGLTextureHandleToImTextureID(texture.getNativeHandle());
730-
return ImGui::ImageButton(id, textureID, ImVec2(size.x, size.y), uv0, uv1, toImColor(bgColor),
721+
auto [uv0, uv1, textureID] = getSpriteTextureData(sprite);
722+
return ImGui::ImageButton(id, textureID, toImVec2(size), uv0, uv1, toImColor(bgColor),
731723
toImColor(tintColor));
732724
}
733725

@@ -763,13 +755,27 @@ ImColor toImColor(sf::Color c) {
763755
return {static_cast<int>(c.r), static_cast<int>(c.g), static_cast<int>(c.b),
764756
static_cast<int>(c.a)};
765757
}
758+
ImVec2 toImVec2(const sf::Vector2f& v) {
759+
return {v.x, v.y};
760+
}
761+
sf::Vector2f toSfVector2f(const ImVec2& v) {
762+
return {v.x, v.y};
763+
}
766764
ImVec2 getTopLeftAbsolute(const sf::FloatRect& rect) {
767-
const ImVec2 pos = ImGui::GetCursorScreenPos();
768-
return {rect.left + pos.x, rect.top + pos.y};
765+
return toImVec2(toSfVector2f(ImGui::GetCursorScreenPos()) + rect.position);
769766
}
770767
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};
768+
return toImVec2(toSfVector2f(ImGui::GetCursorScreenPos()) + rect.position + rect.size);
769+
}
770+
771+
SpriteTextureData getSpriteTextureData(const sf::Sprite& sprite) {
772+
const sf::Texture& texture(sprite.getTexture());
773+
const sf::Vector2f textureSize(texture.getSize());
774+
const sf::FloatRect textureRect(sprite.getTextureRect());
775+
776+
return {toImVec2(textureRect.position.cwiseDiv(textureSize)),
777+
toImVec2((textureRect.position + textureRect.size).cwiseDiv(textureSize)),
778+
convertGLTextureHandleToImTextureID(texture.getNativeHandle())};
773779
}
774780

775781
ImTextureID convertGLTextureHandleToImTextureID(GLuint glTextureHandle) {

0 commit comments

Comments
 (0)