2016-03-30 5 views
0

FreeTypeを使用してテキストをウィンドウにレンダリングするアプリケーションを作成しました。私は両方のVisual Studio 2015からうまく動作するデバッグとリリースのバージョンを構築しました。私は/ MDdにリンクするための私のランタイムライブラリを設定しました。また、リソースファイルにOCRAEXT.TTFというフォントを含めました。私はこれらのコード行が含まれている初期化機能を持っている私のゲームのクラスでFreetypeが私の.exeにフォントをロードしていないのはなぜですか?

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); 
} 

:ここ

はコードの用途FreeTypeのある

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); 

私は問題を把握するために、コードをステップ実行しようとしたが、ほとんど意味をなされました。誰かが何が問題なのか説明してもらえますか?

答えて

2

FT_New_Faceは、ファイルシステム内のパスが必要です。 PEリソースがファイルシステム内に存在しないため、ファイルの検索に失敗し、このエラーが発生します。

代わりにFT_New_Memory_Faceを使用する必要があります。リソースのメモリ位置はResource API (link to MSDN)を使用して取得できます。 LoadLibraryを使用する代わりに、GetModuleHandle(NULL)を使用して、プロセスのPEイメージへのハンドルを取得します。

関連する問題