Add createFontTexture

This commit is contained in:
Benau 2021-04-17 15:32:33 +08:00
parent b90d4b2d8a
commit 4f9471dcff
10 changed files with 121 additions and 58 deletions

View File

@ -143,7 +143,8 @@ LOCAL_PATH := .
LOCAL_CPP_FEATURES += rtti
LOCAL_SRC_FILES := $(wildcard ../lib/graphics_engine/src/*.c) \
$(wildcard ../lib/graphics_engine/src/*.cpp)
LOCAL_CFLAGS := -I../lib/graphics_engine/include
LOCAL_CFLAGS := -I../lib/graphics_engine/include \
-I../lib/irrlicht/include/
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)

View File

@ -0,0 +1,47 @@
#ifndef HEADER_GE_GL_UTILS_HPP
#define HEADER_GE_GL_UTILS_HPP
#include <glad/gl.h>
#include <set>
#include <sstream>
#include <string>
namespace GE
{
inline bool hasGLExtension(const std::string& extension)
{
if (glGetStringi)
{
int num = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &num);
for (int i = 0; i < num; i++)
{
char* ext = (char*)glGetStringi(GL_EXTENSIONS, i);
if (ext && extension == ext)
return true;
}
return false;
}
static std::set<std::string> extensions;
if (extensions.empty())
{
char* all_ext = (char*)glGetString(GL_EXTENSIONS);
if (all_ext)
{
std::stringstream ss(all_ext);
while (true)
{
std::string ext;
if (ss >> ext)
extensions.insert(ext);
else
break;
}
}
}
return extensions.find(extension) != extensions.end();
} // hasGLExtension
}
#endif

View File

@ -8,7 +8,8 @@
namespace GE
{
irr::video::ITexture* createFontTexture(const std::string& name);
irr::video::ITexture* createFontTexture(const std::string& name,
unsigned size, bool single_channel);
irr::video::ITexture* createTexture(irr::video::IImage* img,
const std::string& name);
irr::video::IImage* getResizedImage(const std::string& path,

View File

@ -1,7 +1,10 @@
#include "ge_gl_texture.hpp"
#include "ge_gl_utils.hpp"
#include "ge_main.hpp"
#include "ge_texture.hpp"
#include <vector>
namespace GE
{
GEGLTexture::GEGLTexture(const std::string& path,
@ -29,6 +32,39 @@ GEGLTexture::GEGLTexture(video::IImage* img, const std::string& name)
img->drop();
} // GEGLTexture
// ----------------------------------------------------------------------------
GEGLTexture::GEGLTexture(const std::string& name, unsigned int size,
bool single_channel)
: video::ITexture(name.c_str()), m_image_mani(nullptr),
m_single_channel(false), m_texture_name(0), m_texture_size(0),
m_driver_type(GE::getDriver()->getDriverType()),
m_disable_reload(true)
{
glGenTextures(1, &m_texture_name);
m_orig_size.Width = size;
m_orig_size.Height = size;
m_size = m_orig_size;
bool texture_swizzle = false;
if (m_driver_type == video::EDT_OGLES2)
{
int gl_major_version = 0;
glGetIntegerv(GL_MAJOR_VERSION, &gl_major_version);
if (gl_major_version >=3)
texture_swizzle = true;
else
texture_swizzle = false;
}
else
texture_swizzle = GE::hasGLExtension("GL_ARB_texture_swizzle");
if (single_channel && texture_swizzle)
m_single_channel = true;
std::vector<uint8_t> data;
data.resize(size * size * (single_channel ? 1 : 4), 0);
upload(data.data());
} // GEGLTexture
// ----------------------------------------------------------------------------
GEGLTexture::~GEGLTexture()
{
@ -97,6 +133,8 @@ void* GEGLTexture::lock(video::E_TEXTURE_LOCK_MODE mode, u32 mipmap_level)
if (m_driver_type == video::EDT_OGLES2 || !glGetTexImage)
{
video::IImage* img = getResizedImage(NamedPath.getPtr());
if (!img)
return NULL;
img->setDeleteMemory(false);
void* data = img->lock();
img->drop();

View File

@ -41,6 +41,9 @@ public:
// ------------------------------------------------------------------------
GEGLTexture(video::IImage* img, const std::string& name);
// ------------------------------------------------------------------------
GEGLTexture(const std::string& name, unsigned int size,
bool single_channel);
// ------------------------------------------------------------------------
virtual ~GEGLTexture();
// ------------------------------------------------------------------------
virtual void* lock(video::E_TEXTURE_LOCK_MODE mode =

View File

@ -74,4 +74,18 @@ irr::video::ITexture* createTexture(video::IImage* img,
}
} // createTexture
// ----------------------------------------------------------------------------
irr::video::ITexture* createFontTexture(const std::string& name,
unsigned size, bool single_channel)
{
switch (GE::getDriver()->getDriverType())
{
case video::EDT_OPENGL:
case video::EDT_OGLES2:
return new GEGLTexture(name, size, single_channel);
default:
return NULL;
}
} // createFontTexture
}

View File

@ -26,7 +26,6 @@
#include "graphics/2dutils.hpp"
#include "graphics/central_settings.hpp"
#include "graphics/irr_driver.hpp"
#include "graphics/stk_texture.hpp"
#include "graphics/stk_tex_manager.hpp"
#include "guiengine/engine.hpp"
#include "guiengine/skin.hpp"
@ -35,6 +34,9 @@
#include "GlyphLayout.h"
#include <array>
#ifndef SERVER_ONLY
#include <ge_texture.hpp>
#endif
#include "../lib/irrlicht/source/Irrlicht/CGUISpriteBank.h"
@ -70,7 +72,7 @@ FontWithFace::~FontWithFace()
for (unsigned int i = 0; i < m_spritebank->getTextureCount(); i++)
{
STKTexManager::getInstance()->removeTexture(
static_cast<STKTexture*>(m_spritebank->getTexture(i)));
m_spritebank->getTexture(i));
}
m_spritebank->drop();
@ -124,7 +126,7 @@ void FontWithFace::reset()
for (unsigned int i = 0; i < m_spritebank->getTextureCount(); i++)
{
STKTexManager::getInstance()->removeTexture(
static_cast<STKTexture*>(m_spritebank->getTexture(i)));
m_spritebank->getTexture(i));
}
m_spritebank->clear();
m_face_ttf->reset();
@ -159,25 +161,15 @@ void FontWithFace::createNewGlyphPage()
#ifndef SERVER_ONLY
if (GUIEngine::isNoGraphics())
return;
uint8_t* data = new uint8_t[getGlyphPageSize() * getGlyphPageSize() *
(CVS->isARBTextureSwizzleUsable() && !useColorGlyphPage() ? 1 : 4)]();
#else
uint8_t* data = NULL;
#endif
m_current_height = 0;
m_used_width = 0;
m_used_height = 0;
STKTexture* stkt = new STKTexture(data, typeid(*this).name() +
video::ITexture* font_texture = GE::createFontTexture(typeid(*this).name() +
StringUtils::toString(m_spritebank->getTextureCount()),
getGlyphPageSize(),
#ifndef SERVER_ONLY
CVS->isARBTextureSwizzleUsable() && !useColorGlyphPage()
#else
false
getGlyphPageSize(), !useColorGlyphPage());
m_spritebank->addTexture(STKTexManager::getInstance()->addTexture(
font_texture));
#endif
);
m_spritebank->addTexture(STKTexManager::getInstance()->addTexture(stkt));
} // createNewGlyphPage
// ----------------------------------------------------------------------------
@ -406,6 +398,8 @@ void FontWithFace::dumpGlyphPage(const std::string& name)
core::dimension2d<u32> size = tex->getSize();
video::ECOLOR_FORMAT col_format = tex->getColorFormat();
void* data = tex->lock();
if (!data)
continue;
video::IImage* image = irr_driver->getVideoDriver()
->createImageFromData(col_format, size, data,
true/*ownForeignMemory*/);

View File

@ -25,7 +25,9 @@
#include "graphics/graphics_restrictions.hpp"
#include "guiengine/engine.hpp"
#include <ge_main.hpp>
#include <ge_gl_utils.hpp>
using namespace GE;
bool CentralVideoSettings::m_supports_sp = true;
CentralVideoSettings *CVS = new CentralVideoSettings();

View File

@ -32,6 +32,7 @@
#include <fstream>
#include <string>
#include <sstream>
#include <ge_gl_utils.hpp>
#ifdef DEBUG
#if !defined(__APPLE__) && !defined(ANDROID)
@ -157,7 +158,7 @@ void initGL()
glDebugMessageCallback((GLDEBUGPROCARB)debugCallback, NULL);
#endif
if (SP::sp_apitrace && hasGLExtension("GL_KHR_debug"))
if (SP::sp_apitrace && GE::hasGLExtension("GL_KHR_debug"))
{
if (glDebugMessageControl)
glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE);
@ -258,43 +259,6 @@ void draw3DLine(const core::vector3df& start,
glGetError();
}
bool hasGLExtension(const char* extension)
{
#if !defined(USE_GLES2)
if (glGetStringi != NULL)
{
GLint numExtensions = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
for (GLint i = 0; i < numExtensions; i++)
{
const char* foundExtension =
(const char*) glGetStringi(GL_EXTENSIONS, i);
if (foundExtension && strcmp(foundExtension, extension) == 0)
{
return true;
}
}
}
else
#endif
{
const char* extensions = (const char*) glGetString(GL_EXTENSIONS);
static std::vector<std::string> all_extensions;
if (all_extensions.empty())
{
all_extensions = StringUtils::split(std::string(extensions), ' ');
}
for (unsigned i = 0; i < all_extensions.size(); i++)
{
if (all_extensions[i] == extension)
{
return true;
}
}
}
return false;
} // hasGLExtension
// ----------------------------------------------------------------------------
/** Returns a space-separated list of all GL extensions. Used for hardware
* reporting.

View File

@ -152,7 +152,6 @@ public:
void draw3DLine(const core::vector3df& start,
const core::vector3df& end, irr::video::SColor color);
bool hasGLExtension(const char* extension);
const std::string getGLExtensions();
void getGLLimits(HardwareStats::Json *json);
bool checkGLError();