Skip to content

Commit 311be60

Browse files
committed
feat: change way of create ImTexture in an agnostic one
1 parent ee62d5e commit 311be60

3 files changed

Lines changed: 25 additions & 13 deletions

File tree

‎src/ILayer.hpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include "imgui.h"
34
#include "types/RenderDataBuffer.hpp"
45

56
namespace ui {
@@ -15,5 +16,8 @@ namespace ui {
1516

1617
// Shutdown / cleanup
1718
virtual void shutdown() = 0;
19+
20+
// Return ImTexture allowing your Renderer API to create ImGUI Font (depending on your Renderer)
21+
virtual ImTextureID uploadFontTexture(unsigned char *pixels, int width, int height) = 0;
1822
};
1923
} // namespace ui

‎src/ImGUILayer.cpp‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#include "ImGUILayer.hpp"
22

33
#include <iostream>
4+
#include "imgui.h"
45

56
ui::ImGUILayer::ImGUILayer()
67
{
8+
this->_isShutdown = false;
79
IMGUI_CHECKVERSION();
810
ImGui::CreateContext();
911
ImGui::StyleColorsDark();
@@ -16,18 +18,7 @@ ui::ImGUILayer::ImGUILayer()
1618

1719
unsigned char* pixels = nullptr;
1820
int width = 0, height = 0;
19-
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
20-
21-
Image fontImage = {};
22-
fontImage.data = pixels;
23-
fontImage.width = width;
24-
fontImage.height = height;
25-
fontImage.mipmaps = 1;
26-
fontImage.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
27-
28-
this->_fontTexture = LoadTextureFromImage(fontImage);
29-
30-
io.Fonts->SetTexID((ImTextureID)(intptr_t)this->_fontTexture.id);
21+
io.Fonts->SetTexID(this->uploadFontTexture(pixels, width, height));
3122

3223
io.Fonts->ClearTexData();
3324

@@ -76,6 +67,20 @@ void ui::ImGUILayer::shutdown()
7667
UnloadTexture(this->_fontTexture);
7768
if (ImGui::GetCurrentContext() != nullptr)
7869
ImGui::DestroyContext();
70+
this->_isShutdown = true;
71+
}
72+
73+
ImTextureID ui::ImGUILayer::uploadFontTexture(unsigned char *pixels, int width, int height)
74+
{
75+
Image fontImage = {};
76+
fontImage.data = pixels;
77+
fontImage.width = width;
78+
fontImage.height = height;
79+
fontImage.mipmaps = 1;
80+
fontImage.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
81+
82+
this->_fontTexture = LoadTextureFromImage(fontImage);
83+
return (ImTextureID)(intptr_t)this->_fontTexture.id;
7984
}
8085

8186
void ui::ImGUILayer::_recoverVertex(ImDrawList& cmdList)

‎src/ImGUILayer.hpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace ui {
1010
class ImGUILayer : public ui::ILayer {
1111
public:
1212
ImGUILayer();
13-
~ImGUILayer() override { this->shutdown(); };
13+
~ImGUILayer() override { if (!this->_isShutdown) { this->shutdown(); } };
1414

1515
void beginFrame(float delta_time, float width, float height) override;
1616

@@ -20,6 +20,8 @@ namespace ui {
2020

2121
void shutdown() override;
2222

23+
ImTextureID uploadFontTexture(unsigned char *pixels, int width, int height) override;
24+
2325
private:
2426

2527
// Convert Methods
@@ -34,6 +36,7 @@ namespace ui {
3436
// Private Attributs
3537
common::RenderDataBuffer _buffer;
3638
Texture2D _fontTexture;
39+
bool _isShutdown;
3740
};
3841

3942
} // namespace ui

0 commit comments

Comments
 (0)