[英]Why isn't Freetype loading a font in my .exe
我編寫了一個使用FreeType將文本呈現到窗口上的應用程序。 我已經構建了調試和發布版本,都可以從Visual Studio 2015正常運行。我已經設置了運行時庫來鏈接到/ MDd。 我還將字體OCRAEXT.TTF包含在資源文件中。
這是使用FreeType的代碼:
TextRenderer::TextRenderer(GLuint width, GLuint height)
{
//Load and configure shader
this->TextShader = ResourceManager::LoadShader("VertexText.vert", "FragmentText.frag", nullptr, "text");
this->TextShader.SetMatrix4("projection", glm::ortho(0.0f, static_cast<GLfloat>(width), static_cast<GLfloat>(height), 0.0f), GL_TRUE);
this->TextShader.SetInteger("text", 0);
//configure VAO/VBO for texture quads
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &this->VBO);
glBindVertexArray(this->VAO);
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void TextRenderer::Load(std::string font, GLuint fontSize)
{
//first clear the previously loaded characters
this->Characters.clear();
//then initialise and load the Freetype library
FT_Library ft;
if (FT_Init_FreeType(&ft)) //All functions return a value different than 0 whenever an error occurred
std::cout << "ERROR::FREETYPE: Could not init freetype library" << std::endl;
//Load font as face
FT_Face face;
if (FT_New_Face(ft, font.c_str(), 0, &face))
std::cout << "ERROR::FREETYPE: Failed to load font" << std::endl;
//set size to load glyphs as
FT_Set_Pixel_Sizes(face, 0, fontSize);
//disable byte-alignment restriction
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
//then for the first 128 ASCII characters, pre-load/compile their characters and store them
for (GLubyte c = 0; c < 128; c++)
{
//Load character glyph
if (FT_Load_Char(face, c, FT_LOAD_RENDER))
{
std::cout << "ERROR::FREETYPE: Failed to load glyph" << std::endl;
continue;
}
//generate texture
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RED,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
0,
GL_RED,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer
);
//set texture options
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//Now store character for later use
Character character =
{
texture,
glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
face->glyph->advance.x,
};
Characters.insert(std::pair<GLchar, Character>(c, character));
}
glBindTexture(GL_TEXTURE_2D, 0);
//destroy FreeType once we're finished
FT_Done_Face(face);
FT_Done_FreeType(ft);
}
在我的Game類中,我具有一個初始化函數,其中包含以下代碼行:
Text = new TextRenderer(this->Width, this->Height);
Text->Load("fonts/OCRAEXT.TTF", 24);
問題是當我嘗試運行獨立的.exe時,出現消息“ ERROR :: FREETYPE:無法加載字體”和.exe。 停止運行。 我嘗試調試此問題,但收到一條消息,內容為“ Game.exe中0x00730072的未處理異常。0xC0000005:執行位置0x00730072的訪問沖突。”
斷點指向以下代碼行:
FT_Set_Pixel_Sizes(face, 0, fontSize);
我試圖逐步檢查代碼以找出問題所在,但沒有什么意義。 有人可以解釋可能是什么問題嗎?
FT_New_Face
需要文件系統中的路徑。 PE資源不在文件系統中,因此嘗試查找文件失敗,並且出現此錯誤。
您必須改用FT_New_Memory_Face
; 可以使用Resource API(鏈接到MSDN)檢索資源的內存位置。 代替使用LoadLibrary
使用GetModuleHandle(NULL)
來檢索進程的PE映像的句柄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.