|
1 | 1 | #include "Core/Graphics/Device_OpenGL.hpp" |
2 | 2 | #include "Core/FileManager.hpp" |
| 3 | +#include "Core/Object.hpp" |
3 | 4 | #include "Core/Type.hpp" |
4 | 5 | #include "Core/i18n.hpp" |
5 | 6 |
|
| 7 | +#include <cstddef> |
6 | 8 | #include <cstdint> |
| 9 | +#include <cstring> |
7 | 10 | #include <memory> |
| 11 | +#include <string> |
| 12 | +#include <string_view> |
8 | 13 |
|
9 | 14 | #include "glad/gl.h" |
10 | 15 | #include "spdlog/spdlog.h" |
@@ -112,7 +117,18 @@ namespace Core::Graphics |
112 | 117 | return false; |
113 | 118 | } |
114 | 119 | } |
115 | | - //bool createTextureFromMemory(void const* data, size_t size, bool mipmap, ITexture2D** pp_texture); |
| 120 | + bool Device_OpenGL::createTextureFromMemory(void const* data, size_t size, bool mipmap, ITexture2D** pp_texture) { |
| 121 | + try |
| 122 | + { |
| 123 | + *pp_texture = new Texture2D_OpenGL(this, data, size, mipmap); |
| 124 | + return true; |
| 125 | + } |
| 126 | + catch (...) |
| 127 | + { |
| 128 | + *pp_texture = nullptr; |
| 129 | + return false; |
| 130 | + } |
| 131 | + } |
116 | 132 | bool Device_OpenGL::createTexture(Vector2U size, ITexture2D** pp_texture) |
117 | 133 | { |
118 | 134 | try |
@@ -238,14 +254,27 @@ namespace Core::Graphics |
238 | 254 | { |
239 | 255 | if (m_data) |
240 | 256 | { |
| 257 | + Vector2I size; |
| 258 | + uint8_t* data = stbi_load_from_memory((uint8_t*)m_data->data(), m_data->size(), &size.x, &size.y, NULL, 4); |
| 259 | + if (data == NULL) |
| 260 | + { |
| 261 | + spdlog::error("[core] Unable to parse binary data"); |
| 262 | + return false; |
| 263 | + } |
| 264 | + // image size will never be negative |
| 265 | + m_size.x = size.x; |
| 266 | + m_size.y = size.y; |
| 267 | + |
241 | 268 | glGenTextures(1, &opengl_texture2d); |
242 | 269 | if (opengl_texture2d == 0) { |
243 | 270 | i18n_core_system_call_report_error("glGenTextures"); |
244 | 271 | return false; |
245 | 272 | } |
246 | 273 | glBindTexture(GL_TEXTURE_2D, opengl_texture2d); |
247 | | - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_size.x, m_size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_data->data()); |
| 274 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_size.x, m_size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); |
248 | 275 | glGenerateMipmap(GL_TEXTURE_2D); |
| 276 | + |
| 277 | + stbi_image_free(data); |
249 | 278 | } |
250 | 279 | else if (!source_path.empty()) |
251 | 280 | { |
@@ -313,6 +342,22 @@ namespace Core::Graphics |
313 | 342 | throw std::runtime_error("Texture2D::Texture2D(2)"); |
314 | 343 | m_device->addEventListener(this); |
315 | 344 | } |
| 345 | + Texture2D_OpenGL::Texture2D_OpenGL(Device_OpenGL* device, void const* data, size_t size, bool mipmap) |
| 346 | + : m_device(device) |
| 347 | + , m_dynamic(false) |
| 348 | + , m_premul(false) |
| 349 | + , m_mipmap(mipmap) |
| 350 | + , m_isrt(false) |
| 351 | + { |
| 352 | + if (!IData::create(size, ~m_data)) |
| 353 | + throw std::runtime_error("Texture2D::Texture2D(1)"); |
| 354 | + |
| 355 | + std::memcpy(m_data->data(), data, size); |
| 356 | + |
| 357 | + if (!createResource()) |
| 358 | + throw std::runtime_error("Texture2D::Texture2D(2)"); |
| 359 | + m_device->addEventListener(this); |
| 360 | + } |
316 | 361 | Texture2D_OpenGL::Texture2D_OpenGL(Device_OpenGL* device, Vector2U size, bool rendertarget) |
317 | 362 | : m_device(device) |
318 | 363 | , m_size(size) |
|
0 commit comments