Skip to content

Commit 4e9fab9

Browse files
committed
Consolidate declarations and definitions
1 parent ee40740 commit 4e9fab9

File tree

1 file changed

+73
-83
lines changed

1 file changed

+73
-83
lines changed

imgui-SFML.cpp

Lines changed: 73 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,44 @@ static_assert(sizeof(GLuint) <= sizeof(ImTextureID), "ImTextureID is not large e
118118
namespace
119119
{
120120
// various helper functions
121-
[[nodiscard]] ImColor toImColor(sf::Color c);
122-
[[nodiscard]] ImVec2 toImVec2(const sf::Vector2f& v);
123-
[[nodiscard]] sf::Vector2f toSfVector2f(const ImVec2& v);
124-
[[nodiscard]] ImVec2 getTopLeftAbsolute(const sf::FloatRect& rect);
125-
[[nodiscard]] ImVec2 getDownRightAbsolute(const sf::FloatRect& rect);
121+
[[nodiscard]] ImColor toImColor(sf::Color c)
122+
{
123+
return {int{c.r}, int{c.g}, int{c.b}, int{c.a}};
124+
}
125+
126+
[[nodiscard]] ImVec2 toImVec2(const sf::Vector2f& v)
127+
{
128+
return {v.x, v.y};
129+
}
130+
131+
[[nodiscard]] sf::Vector2f toSfVector2f(const ImVec2& v)
132+
{
133+
return {v.x, v.y};
134+
}
135+
136+
[[nodiscard]] ImVec2 getTopLeftAbsolute(const sf::FloatRect& rect)
137+
{
138+
return toImVec2(toSfVector2f(ImGui::GetCursorScreenPos()) + rect.position);
139+
}
140+
141+
[[nodiscard]] ImVec2 getDownRightAbsolute(const sf::FloatRect& rect)
142+
{
143+
return toImVec2(toSfVector2f(ImGui::GetCursorScreenPos()) + rect.position + rect.size);
144+
}
145+
146+
[[nodiscard]] ImTextureID convertGLTextureHandleToImTextureID(GLuint glTextureHandle)
147+
{
148+
ImTextureID textureID{};
149+
std::memcpy(&textureID, &glTextureHandle, sizeof(GLuint));
150+
return textureID;
151+
}
152+
153+
[[nodiscard]] GLuint convertImTextureIDToGLTextureHandle(ImTextureID textureID)
154+
{
155+
GLuint glTextureHandle = 0;
156+
std::memcpy(&glTextureHandle, &textureID, sizeof(GLuint));
157+
return glTextureHandle;
158+
}
126159

127160
struct SpriteTextureData
128161
{
@@ -131,27 +164,55 @@ struct SpriteTextureData
131164
ImTextureID textureID{};
132165
};
133166

134-
[[nodiscard]] SpriteTextureData getSpriteTextureData(const sf::Sprite& sprite);
167+
[[nodiscard]] SpriteTextureData getSpriteTextureData(const sf::Sprite& sprite)
168+
{
169+
const sf::Texture& texture(sprite.getTexture());
170+
const sf::Vector2f textureSize(texture.getSize());
171+
const sf::FloatRect textureRect(sprite.getTextureRect());
135172

136-
[[nodiscard]] ImTextureID convertGLTextureHandleToImTextureID(GLuint glTextureHandle);
137-
[[nodiscard]] GLuint convertImTextureIDToGLTextureHandle(ImTextureID textureID);
173+
return {toImVec2(textureRect.position.componentWiseDiv(textureSize)),
174+
toImVec2((textureRect.position + textureRect.size).componentWiseDiv(textureSize)),
175+
convertGLTextureHandleToImTextureID(texture.getNativeHandle())};
176+
}
138177

139178
void RenderDrawLists(ImDrawData* draw_data); // rendering callback function prototype
140179

141180
// Default mapping is XInput gamepad mapping
142181
void initDefaultJoystickMapping();
143182

183+
// data
184+
constexpr unsigned int NULL_JOYSTICK_ID = sf::Joystick::Count;
185+
144186
// Returns first id of connected joystick
145-
[[nodiscard]] unsigned int getConnectedJoystickId();
187+
[[nodiscard]] unsigned int getConnectedJoystickId()
188+
{
189+
for (unsigned int i = 0; i < sf::Joystick::Count; ++i)
190+
{
191+
if (sf::Joystick::isConnected(i))
192+
return i;
193+
}
194+
195+
return NULL_JOYSTICK_ID;
196+
}
146197

147198
void updateJoystickButtonState(ImGuiIO& io);
148199
void updateJoystickDPadState(ImGuiIO& io);
149200
void updateJoystickAxisState(ImGuiIO& io);
150201

151202
// clipboard functions
152-
void setClipboardText(void* userData, const char* text);
153-
[[nodiscard]] const char* getClipboardText(void* userData);
154-
std::string s_clipboardText;
203+
void setClipboardText(void* /*userData*/, const char* text)
204+
{
205+
sf::Clipboard::setString(sf::String::fromUtf8(text, text + std::strlen(text)));
206+
}
207+
208+
[[nodiscard]] const char* getClipboardText(void* /*userData*/)
209+
{
210+
static std::string s_clipboardText;
211+
212+
auto tmp = sf::Clipboard::getString().toUtf8();
213+
s_clipboardText.assign(tmp.begin(), tmp.end());
214+
return s_clipboardText.c_str();
215+
}
155216

156217
// mouse cursors
157218
void loadMouseCursor(ImGuiMouseCursor imguiCursorType, sf::Cursor::Type sfmlCursorType);
@@ -161,9 +222,6 @@ void updateMouseCursor(sf::Window& window);
161222
[[nodiscard]] ImGuiKey keycodeToImGuiKey(sf::Keyboard::Key code);
162223
[[nodiscard]] ImGuiKey keycodeToImGuiMod(sf::Keyboard::Key code);
163224

164-
// data
165-
constexpr unsigned int NULL_JOYSTICK_ID = sf::Joystick::Count;
166-
167225
struct StickInfo
168226
{
169227
sf::Joystick::Axis xAxis{sf::Joystick::Axis::X};
@@ -858,51 +916,6 @@ void DrawRectFilled(const sf::FloatRect& rect, const sf::Color& color, float rou
858916

859917
namespace
860918
{
861-
ImColor toImColor(sf::Color c)
862-
{
863-
return {int{c.r}, int{c.g}, int{c.b}, int{c.a}};
864-
}
865-
ImVec2 toImVec2(const sf::Vector2f& v)
866-
{
867-
return {v.x, v.y};
868-
}
869-
sf::Vector2f toSfVector2f(const ImVec2& v)
870-
{
871-
return {v.x, v.y};
872-
}
873-
ImVec2 getTopLeftAbsolute(const sf::FloatRect& rect)
874-
{
875-
return toImVec2(toSfVector2f(ImGui::GetCursorScreenPos()) + rect.position);
876-
}
877-
ImVec2 getDownRightAbsolute(const sf::FloatRect& rect)
878-
{
879-
return toImVec2(toSfVector2f(ImGui::GetCursorScreenPos()) + rect.position + rect.size);
880-
}
881-
882-
SpriteTextureData getSpriteTextureData(const sf::Sprite& sprite)
883-
{
884-
const sf::Texture& texture(sprite.getTexture());
885-
const sf::Vector2f textureSize(texture.getSize());
886-
const sf::FloatRect textureRect(sprite.getTextureRect());
887-
888-
return {toImVec2(textureRect.position.componentWiseDiv(textureSize)),
889-
toImVec2((textureRect.position + textureRect.size).componentWiseDiv(textureSize)),
890-
convertGLTextureHandleToImTextureID(texture.getNativeHandle())};
891-
}
892-
893-
ImTextureID convertGLTextureHandleToImTextureID(GLuint glTextureHandle)
894-
{
895-
ImTextureID textureID{};
896-
std::memcpy(&textureID, &glTextureHandle, sizeof(GLuint));
897-
return textureID;
898-
}
899-
GLuint convertImTextureIDToGLTextureHandle(ImTextureID textureID)
900-
{
901-
GLuint glTextureHandle = 0;
902-
std::memcpy(&glTextureHandle, &textureID, sizeof(GLuint));
903-
return glTextureHandle;
904-
}
905-
906919
// copied from imgui/backends/imgui_impl_opengl2.cpp
907920
void SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height)
908921
{
@@ -1093,17 +1106,6 @@ void RenderDrawLists(ImDrawData* draw_data)
10931106
#endif
10941107
}
10951108

1096-
unsigned int getConnectedJoystickId()
1097-
{
1098-
for (unsigned int i = 0; i < sf::Joystick::Count; ++i)
1099-
{
1100-
if (sf::Joystick::isConnected(i))
1101-
return i;
1102-
}
1103-
1104-
return NULL_JOYSTICK_ID;
1105-
}
1106-
11071109
void initDefaultJoystickMapping()
11081110
{
11091111
ImGui::SFML::SetJoystickMapping(ImGuiKey_GamepadFaceDown, 0);
@@ -1237,18 +1239,6 @@ void updateJoystickAxisState(ImGuiIO& io)
12371239
false);
12381240
}
12391241

1240-
void setClipboardText(void* /*userData*/, const char* text)
1241-
{
1242-
sf::Clipboard::setString(sf::String::fromUtf8(text, text + std::strlen(text)));
1243-
}
1244-
1245-
const char* getClipboardText(void* /*userData*/)
1246-
{
1247-
auto tmp = sf::Clipboard::getString().toUtf8();
1248-
s_clipboardText.assign(tmp.begin(), tmp.end());
1249-
return s_clipboardText.c_str();
1250-
}
1251-
12521242
void loadMouseCursor(ImGuiMouseCursor imguiCursorType, sf::Cursor::Type sfmlCursorType)
12531243
{
12541244
s_currWindowCtx->mouseCursors[imguiCursorType] = sf::Cursor::createFromSystem(sfmlCursorType);

0 commit comments

Comments
 (0)