Skip to content

Commit

Permalink
Propagate possible texture creation failure to Init
Browse files Browse the repository at this point in the history
  • Loading branch information
vittorioromeo committed Dec 22, 2021
1 parent 3dd9b4d commit c233af2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
23 changes: 15 additions & 8 deletions imgui-SFML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,15 @@ WindowContext* s_currWindowCtx = NULL;

namespace ImGui {
namespace SFML {
void Init(sf::RenderWindow& window, bool loadDefaultFont) {
Init(window, window, loadDefaultFont);
bool Init(sf::RenderWindow& window, bool loadDefaultFont) {
return Init(window, window, loadDefaultFont);
}

void Init(sf::Window& window, sf::RenderTarget& target, bool loadDefaultFont) {
Init(window, static_cast<sf::Vector2f>(target.getSize()), loadDefaultFont);
bool Init(sf::Window& window, sf::RenderTarget& target, bool loadDefaultFont) {
return Init(window, static_cast<sf::Vector2f>(target.getSize()), loadDefaultFont);
}

void Init(sf::Window& window, const sf::Vector2f& displaySize, bool loadDefaultFont) {
bool Init(sf::Window& window, const sf::Vector2f& displaySize, bool loadDefaultFont) {
#if __cplusplus < 201103L // runtime assert when using earlier than C++11 as no
// static_assert support
assert(sizeof(GLuint) <= sizeof(ImTextureID)); // ImTextureID is not large enough to fit
Expand Down Expand Up @@ -314,8 +314,10 @@ void Init(sf::Window& window, const sf::Vector2f& displaySize, bool loadDefaultF
if (loadDefaultFont) {
// this will load default font automatically
// No need to call AddDefaultFont
UpdateFontTexture();
return UpdateFontTexture();
}

return true;
}

void SetCurrentWindow(const sf::Window& window) {
Expand Down Expand Up @@ -572,7 +574,7 @@ void Shutdown() {
s_windowContexts.clear();
}

void UpdateFontTexture() {
bool UpdateFontTexture() {
assert(s_currWindowCtx);

ImGuiIO& io = ImGui::GetIO();
Expand All @@ -582,11 +584,16 @@ void UpdateFontTexture() {
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);

sf::Texture& texture = *s_currWindowCtx->fontTexture;
texture.create(width, height);
if (!texture.create(width, height)) {
return false;
}

texture.update(pixels);

ImTextureID texID = convertGLTextureHandleToImTextureID(texture.getNativeHandle());
io.Fonts->SetTexID(texID);

return true;
}

sf::Texture& GetFontTexture() {
Expand Down
18 changes: 13 additions & 5 deletions imgui-SFML.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@

#include "imgui-SFML_export.h"

#if __cplusplus >= 201703L // C++17 and above
#define IMGUI_SFML_NODISCARD [[nodiscard]]
#else
#define IMGUI_SFML_NODISCARD
#endif

namespace sf {
class Event;
class RenderTarget;
Expand All @@ -21,10 +27,12 @@ class Window;

namespace ImGui {
namespace SFML {
IMGUI_SFML_API void Init(sf::RenderWindow& window, bool loadDefaultFont = true);
IMGUI_SFML_API void Init(sf::Window& window, sf::RenderTarget& target, bool loadDefaultFont = true);
IMGUI_SFML_API void Init(sf::Window& window, const sf::Vector2f& displaySize,
bool loadDefaultFont = true);
IMGUI_SFML_NODISCARD IMGUI_SFML_API bool Init(sf::RenderWindow& window,
bool loadDefaultFont = true);
IMGUI_SFML_NODISCARD IMGUI_SFML_API bool Init(sf::Window& window, sf::RenderTarget& target,
bool loadDefaultFont = true);
IMGUI_SFML_NODISCARD IMGUI_SFML_API bool Init(sf::Window& window, const sf::Vector2f& displaySize,
bool loadDefaultFont = true);

IMGUI_SFML_API void SetCurrentWindow(const sf::Window& window);
IMGUI_SFML_API void ProcessEvent(const sf::Event& event); // DEPRECATED: use (window,
Expand All @@ -44,7 +52,7 @@ IMGUI_SFML_API void Shutdown(const sf::Window& window);
// Shuts down all ImGui contexts
IMGUI_SFML_API void Shutdown();

IMGUI_SFML_API void UpdateFontTexture();
IMGUI_SFML_NODISCARD IMGUI_SFML_API bool UpdateFontTexture();
IMGUI_SFML_API sf::Texture& GetFontTexture();

// joystick functions
Expand Down

0 comments on commit c233af2

Please sign in to comment.