Skip to content

Commit c1bd82b

Browse files
committed
LoadTextureBin
1 parent d16c814 commit c1bd82b

File tree

6 files changed

+111
-4
lines changed

6 files changed

+111
-4
lines changed

LuaSTG/Core/Graphics/Device.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ namespace Core::Graphics
128128
virtual void* getNativeRendererHandle() = 0;
129129

130130
virtual bool createTextureFromFile(StringView path, bool mipmap, ITexture2D** pp_texutre) = 0;
131-
//virtual bool createTextureFromMemory(void const* data, size_t size, bool mipmap, ITexture2D** pp_texutre) = 0;
131+
virtual bool createTextureFromMemory(void const* data, size_t size, bool mipmap, ITexture2D** pp_texutre) = 0;
132132
virtual bool createTexture(Vector2U size, ITexture2D** pp_texutre) = 0;
133133

134134
virtual bool createRenderTarget(Vector2U size, IRenderTarget** pp_rt) = 0;

LuaSTG/Core/Graphics/Device_OpenGL.cpp

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
#include "Core/Graphics/Device_OpenGL.hpp"
22
#include "Core/FileManager.hpp"
3+
#include "Core/Object.hpp"
34
#include "Core/Type.hpp"
45
#include "Core/i18n.hpp"
56

7+
#include <cstddef>
68
#include <cstdint>
9+
#include <cstring>
710
#include <memory>
11+
#include <string>
12+
#include <string_view>
813

914
#include "glad/gl.h"
1015
#include "spdlog/spdlog.h"
@@ -112,7 +117,18 @@ namespace Core::Graphics
112117
return false;
113118
}
114119
}
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+
}
116132
bool Device_OpenGL::createTexture(Vector2U size, ITexture2D** pp_texture)
117133
{
118134
try
@@ -238,14 +254,27 @@ namespace Core::Graphics
238254
{
239255
if (m_data)
240256
{
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+
241268
glGenTextures(1, &opengl_texture2d);
242269
if (opengl_texture2d == 0) {
243270
i18n_core_system_call_report_error("glGenTextures");
244271
return false;
245272
}
246273
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);
248275
glGenerateMipmap(GL_TEXTURE_2D);
276+
277+
stbi_image_free(data);
249278
}
250279
else if (!source_path.empty())
251280
{
@@ -313,6 +342,22 @@ namespace Core::Graphics
313342
throw std::runtime_error("Texture2D::Texture2D(2)");
314343
m_device->addEventListener(this);
315344
}
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+
}
316361
Texture2D_OpenGL::Texture2D_OpenGL(Device_OpenGL* device, Vector2U size, bool rendertarget)
317362
: m_device(device)
318363
, m_size(size)

LuaSTG/Core/Graphics/Device_OpenGL.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Core/Type.hpp"
55
#include "glad/gl.h"
66
#include <SDL_video.h>
7+
#include <cstddef>
78
#include <cstdint>
89
#include <optional>
910
// #include "Platform/RuntimeLoader/DXGI.hpp"
@@ -36,7 +37,7 @@ namespace Core::Graphics
3637
void* getNativeRendererHandle() { return SDL_GL_GetCurrentContext(); }
3738

3839
bool createTextureFromFile(StringView path, bool mipmap, ITexture2D** pp_texutre);
39-
//bool createTextureFromMemory(void const* data, size_t size, bool mipmap, ITexture2D** pp_texutre);
40+
bool createTextureFromMemory(void const* data, size_t size, bool mipmap, ITexture2D** pp_texutre);
4041
bool createTexture(Vector2U size, ITexture2D** pp_texutre);
4142

4243
bool createRenderTarget(Vector2U size, IRenderTarget** pp_rt);
@@ -95,6 +96,7 @@ namespace Core::Graphics
9596

9697
public:
9798
Texture2D_OpenGL(Device_OpenGL* device, StringView path, bool mipmap);
99+
Texture2D_OpenGL(Device_OpenGL* device, void const* data, size_t size, bool mipmap);
98100
Texture2D_OpenGL(Device_OpenGL* device, Vector2U size, bool rendertarget); // if rendertarget, then hand over control to RenderTarget_OpenGL
99101
~Texture2D_OpenGL();
100102
};

LuaSTG/LuaSTG/GameResource/ResourceManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ namespace LuaSTGPlus
9696

9797
// 纹理
9898
bool LoadTexture(const char* name, const char* path, bool mipmaps = true) noexcept;
99+
bool LoadTextureBin(const char* name, std::vector<uint8_t> data, bool mipmaps = true) noexcept;
99100
bool CreateTexture(const char* name, int width, int height) noexcept;
100101
// 渲染目标
101102
bool CreateRenderTarget(const char* name, int width = 0, int height = 0, bool depth_buffer = false) noexcept;

LuaSTG/LuaSTG/GameResource/ResourcePool.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
#include "Core/FileManager.hpp"
1313
#include "AppFrame.h"
1414
#include "LuaBinding/lua_utility.hpp"
15+
#include <cstdint>
1516
#include <spdlog/spdlog.h>
17+
#include <sys/types.h>
18+
#include <vector>
1619

1720
namespace LuaSTGPlus
1821
{
@@ -230,6 +233,44 @@ namespace LuaSTGPlus
230233
return true;
231234
}
232235

236+
bool ResourcePool::LoadTextureBin(const char* name, std::vector<uint8_t> data, bool mipmaps) noexcept
237+
{
238+
if (m_TexturePool.find(std::string_view(name)) != m_TexturePool.end())
239+
{
240+
if (ResourceMgr::GetResourceLoadingLog())
241+
{
242+
spdlog::warn("[luastg] LoadTexture: Texture '{}' already exists, loading cancelled.", name);
243+
}
244+
return true;
245+
}
246+
247+
Core::ScopeObject<Core::Graphics::ITexture2D> p_texture;
248+
if (!LAPP.GetAppModel()->getDevice()->createTextureFromMemory(data.data(), data.size(), mipmaps, ~p_texture))
249+
{
250+
spdlog::error("[luastg] Failed to create texture '{}' from binary data", name);
251+
return false;
252+
}
253+
254+
try
255+
{
256+
Core::ScopeObject<IResourceTexture> tRes;
257+
tRes.attach(new ResourceTextureImpl(name, p_texture.get()));
258+
m_TexturePool.emplace(name, tRes);
259+
}
260+
catch (std::exception const& e)
261+
{
262+
spdlog::error("[luastg] LoadTexture: Failed to load texture '{}' ({})", name, e.what());
263+
return false;
264+
}
265+
266+
if (ResourceMgr::GetResourceLoadingLog())
267+
{
268+
spdlog::info("[luastg] LoadTexture: <binary data>, name '{}' ({})", name, getResourcePoolTypeName());
269+
}
270+
271+
return true;
272+
}
273+
233274
bool ResourcePool::CreateTexture(const char* name, int width, int height) noexcept
234275
{
235276
if (m_TexturePool.find(std::string_view(name)) != m_TexturePool.end())

LuaSTG/LuaSTG/LuaBinding/LW_ResourceMgr.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#include "LuaBinding/lua_utility.hpp"
33
#include "Core/FileManager.hpp"
44
#include "AppFrame.h"
5+
#include <cstdint>
6+
#include <string_view>
7+
#include <vector>
58

69
void LuaSTGPlus::LuaWrapper::ResourceMgrWrapper::Register(lua_State* L) noexcept
710
{
@@ -53,6 +56,20 @@ void LuaSTGPlus::LuaWrapper::ResourceMgrWrapper::Register(lua_State* L) noexcept
5356
return luaL_error(L, "can't load texture from file '%s'.", path);
5457
return 0;
5558
}
59+
static int LoadTextureBin(lua_State* L) noexcept
60+
{
61+
const char* name = luaL_checkstring(L, 1);
62+
// const char* path = luaL_checkstring(L, 2);
63+
std::string_view bin_init = luaL_check_string_view(L, 2);
64+
std::vector<uint8_t> bin(bin_init.begin(), bin_init.end());
65+
66+
ResourcePool* pActivedPool = LRES.GetActivedPool();
67+
if (!pActivedPool)
68+
return luaL_error(L, "can't load resource at this time.");
69+
if (!pActivedPool->LoadTextureBin(name, bin, lua_toboolean(L, 3) == 0 ? false : true))
70+
return luaL_error(L, "can't load texture '%s' from binary data.", name);
71+
return 0;
72+
}
5673
static int LoadSprite(lua_State* L) noexcept
5774
{
5875
const char* name = luaL_checkstring(L, 1);
@@ -651,6 +668,7 @@ void LuaSTGPlus::LuaWrapper::ResourceMgrWrapper::Register(lua_State* L) noexcept
651668
{ "SetResourceStatus", &Wrapper::SetResourceStatus },
652669
{ "GetResourceStatus", &Wrapper::GetResourceStatus },
653670
{ "LoadTexture", &Wrapper::LoadTexture },
671+
{ "LoadTextureBin", &Wrapper::LoadTextureBin },
654672
{ "LoadImage", &Wrapper::LoadSprite },
655673
{ "LoadAnimation", &Wrapper::LoadAnimation },
656674
{ "LoadPS", &Wrapper::LoadPS },

0 commit comments

Comments
 (0)