17
17
18
18
#include < cassert>
19
19
#include < cmath> // abs
20
- #include < cstddef> // offsetof, nullptr, size_t
21
- #include < cstdint> // uint8_t
22
20
#include < cstring> // memcpy
23
21
24
22
#include < algorithm>
@@ -119,9 +117,19 @@ static_assert(sizeof(GLuint) <= sizeof(ImTextureID),
119
117
namespace {
120
118
// various helper functions
121
119
ImColor toImColor (sf::Color c);
120
+ ImVec2 toImVec2 (const sf::Vector2f& v);
121
+ sf::Vector2f toSfVector2f (const ImVec2& v);
122
122
ImVec2 getTopLeftAbsolute (const sf::FloatRect& rect);
123
123
ImVec2 getDownRightAbsolute (const sf::FloatRect& rect);
124
124
125
+ struct SpriteTextureData {
126
+ ImVec2 uv0;
127
+ ImVec2 uv1;
128
+ ImTextureID textureID{};
129
+ };
130
+
131
+ SpriteTextureData getSpriteTextureData (const sf::Sprite& sprite);
132
+
125
133
ImTextureID convertGLTextureHandleToImTextureID (GLuint glTextureHandle);
126
134
GLuint convertImTextureIDToGLTextureHandle (ImTextureID textureID);
127
135
@@ -240,7 +248,7 @@ bool Init(sf::Window& window, const sf::Vector2f& displaySize, bool loadDefaultF
240
248
initDefaultJoystickMapping ();
241
249
242
250
// init rendering
243
- io.DisplaySize = ImVec2 (displaySize. x , displaySize. y );
251
+ io.DisplaySize = toImVec2 (displaySize);
244
252
245
253
// clipboard
246
254
io.SetClipboardTextFn = setClipboardText;
@@ -388,7 +396,7 @@ void Update(const sf::Vector2i& mousePos, const sf::Vector2f& displaySize, sf::T
388
396
assert (s_currWindowCtx && " No current window is set - forgot to call ImGui::SFML::Init?" );
389
397
390
398
ImGuiIO& io = ImGui::GetIO ();
391
- io.DisplaySize = ImVec2 (displaySize. x , displaySize. y );
399
+ io.DisplaySize = toImVec2 (displaySize);
392
400
io.DeltaTime = dt.asSeconds ();
393
401
394
402
if (s_currWindowCtx->windowHasFocus ) {
@@ -649,8 +657,8 @@ void Image(const sf::Texture& texture, const sf::Vector2f& size, const sf::Color
649
657
const sf::Color& borderColor) {
650
658
ImTextureID textureID = convertGLTextureHandleToImTextureID (texture.getNativeHandle ());
651
659
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));
654
662
}
655
663
656
664
// ///////////// Image Overloads for sf::RenderTexture
@@ -664,32 +672,23 @@ void Image(const sf::RenderTexture& texture, const sf::Vector2f& size, const sf:
664
672
ImTextureID textureID =
665
673
convertGLTextureHandleToImTextureID (texture.getTexture ().getNativeHandle ());
666
674
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
670
679
toImColor (tintColor), toImColor (borderColor));
671
680
}
672
681
673
682
// ///////////// Image Overloads for sf::Sprite
674
683
675
684
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);
678
686
}
679
687
680
688
void Image (const sf::Sprite& sprite, const sf::Vector2f& size, const sf::Color& tintColor,
681
689
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));
693
692
}
694
693
695
694
// ///////////// Image Button Overloads for sf::Texture
@@ -698,7 +697,7 @@ bool ImageButton(const char* id, const sf::Texture& texture, const sf::Vector2f&
698
697
const sf::Color& bgColor, const sf::Color& tintColor) {
699
698
ImTextureID textureID = convertGLTextureHandleToImTextureID (texture.getNativeHandle ());
700
699
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 ),
702
701
toImColor (bgColor), toImColor (tintColor));
703
702
}
704
703
@@ -709,7 +708,7 @@ bool ImageButton(const char* id, const sf::RenderTexture& texture, const sf::Vec
709
708
ImTextureID textureID =
710
709
convertGLTextureHandleToImTextureID (texture.getTexture ().getNativeHandle ());
711
710
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 ),
713
712
ImVec2 (1 , 0 ), // flipped vertically, because textures in
714
713
// sf::RenderTexture are stored this way
715
714
toImColor (bgColor), toImColor (tintColor));
@@ -719,15 +718,8 @@ bool ImageButton(const char* id, const sf::RenderTexture& texture, const sf::Vec
719
718
720
719
bool ImageButton (const char * id, const sf::Sprite& sprite, const sf::Vector2f& size,
721
720
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),
731
723
toImColor (tintColor));
732
724
}
733
725
@@ -763,13 +755,27 @@ ImColor toImColor(sf::Color c) {
763
755
return {static_cast <int >(c.r ), static_cast <int >(c.g ), static_cast <int >(c.b ),
764
756
static_cast <int >(c.a )};
765
757
}
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
+ }
766
764
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 );
769
766
}
770
767
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 ())};
773
779
}
774
780
775
781
ImTextureID convertGLTextureHandleToImTextureID (GLuint glTextureHandle) {
0 commit comments