Jump to content
  • Advertisement
Sign in to follow this  

freetype2 opengl access violation error...need help

This topic is 3164 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been trying to get truetype fonts to show up in my opengl project using the freetype2 lib. The project compiles ok but I keep getting an access violation error on the line with FT_GlyphSlot slot = face->glyph; in the function drawText; When i turn off the parts resposible for fonts all renders ok so I'm only posting the truetype stuff. Am I messing up something with the order of defining FT_Face face that its causing the violation? ////////////////////////// ////// includes #include <windows.h> #include <gl/gl3.h> #include <gl/glu.h> #include <stdio.h> #include <iostream> #include <vector> #include <fstream> #include <string> #include <soil/SOIL.h> #include <math.h> #include "render.h" #include "component.h" #include "inputs.h" #include <ft2build.h> #include <ft2build.h> #include FT_FREETYPE_H /////////////////////////////// // defined before main loop // FT_Library library; FT_Face face; GLuint texture; int nextpoweroftwo(int x) { double logbase2 = log(float(x)) / log(float(2)); //return round(pow(2,ceil(logbase2))); return (int)((pow(2,ceil(logbase2)))+0.5); } void drawFillRectTextured (float x, float y, float width, float height) { glBegin (GL_QUADS); glTexCoord2f (0.0f,0.0f); glVertex2f (x,y); glTexCoord2f (1.0f, 0.0f); glVertex2f (x+width, y); glTexCoord2f (1.0f, 1.0f); glVertex2f (x+width, y+height); glTexCoord2f (0.0f, 1.0f); glVertex2f (x, y+height); glEnd (); } void drawBitmap (FT_Bitmap * bitmap, FT_Int left, FT_Int top) { int width = nextpoweroftwo( bitmap->width ); int height = nextpoweroftwo( bitmap->rows ); GLubyte* buffer = new GLubyte[ 2 * width * height]; for(int j=0; j <height;j++) { for(int i=0; i < width; i++){ buffer[2*(i+j*width)]= buffer[2*(i+j*width)+1] = (i>=bitmap->width || j>=bitmap->rows) ? 0 : bitmap->buffer[i + bitmap->width*j]; } } glBindTexture( GL_TEXTURE_2D, texture ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, buffer ); delete [] buffer; //setColor(GREEN); glEnable (GL_TEXTURE_2D); drawFillRectTextured(left, top, bitmap->width, bitmap->rows); glDisable (GL_TEXTURE_2D); } void drawText (string text, FT_Face face, int pen_x, int pen_y) { int error; FT_GlyphSlot slot = face->glyph; for (unsigned int n=0;n<text.length();n++) { error = FT_Load_Char( face, 't', FT_LOAD_RENDER ); if ( error ) continue; drawBitmap (&slot->bitmap, slot->bitmap_left + pen_x, slot->bitmap_top + pen_y); pen_x += slot->advance.x >> 6; pen_y += slot->advance.y >> 6; } } void loadFTFonts () { int error; error = FT_New_Face ( library, "./arial.ttf", 0, &face ); if (error == FT_Err_Unknown_File_Format) { // cerr << "Font isn't in the correct format." << endl; //exit (1); } else if (error) { //cerr << "Error opening font." << endl; //exit (1); } error = FT_Set_Pixel_Sizes( face, /* handle to face object */ 0, /* pixel_width */ 18 ); if (error) { // cerr << "FT_Set_Pixel_Sizes failed to set the pixel sizes!" << endl; // exit (1); } } //////////////////////////////////////// // inside my initiliazation function void init() { loadFTFonts (); .... setting up threads & opengl window } ///////////////////////////////////////// /// main engine drawing loop void mainloop(){ drawText ("test the text", face, 10, 20); draw(); }; /////////////////////////////////////////////////////////////// //// setting up winmain and initializing init() the main loop #ifdef _DEBUG int WINAPI WinMain(HINSTANCE i, HINSTANCE p, LPSTR cmd, int n) #else int WinMainCRTStartup() #endif { PIXELFORMATDESCRIPTOR pfd; HWND hwnd; HDC hdc; int pixelFormat; #ifdef FULLSCREEN DEVMODE dmScreenSettings; // Device Mode #endif #ifndef _DEBUG HINSTANCE i = GetModuleHandle(NULL); #endif /* init the pixelformat descriptor */ RtlZeroMemory(&pfd,sizeof(PIXELFORMATDESCRIPTOR)); // Clear the struct pfd.nSize=sizeof(PIXELFORMATDESCRIPTOR); // Size pfd.nVersion=1; // Version pfd.dwFlags=PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER; // Selected flags pfd.iPixelType=PFD_TYPE_RGBA; // Pixelformat pfd.cColorBits=32; // Pixel depth pfd.cDepthBits=16; // Zbuffer depth pfd.iLayerType=PFD_MAIN_PLANE; // Place the pixelformat on the main plane #ifdef FULLSCREEN /* init the resolution change settings */ RtlZeroMemory(&dmScreenSettings,sizeof(dmScreenSettings)); // Clear the struct dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure dmScreenSettings.dmPelsWidth = 640; // Selected Screen Width dmScreenSettings.dmPelsHeight = 480; // Selected Screen Height dmScreenSettings.dmBitsPerPel = 32; // Selected Bits Per Pixel dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT; // Notify which fields we have used if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL) return 0; #endif /* setup the window handle */ hwnd=CreateWindowEx(WS_EX_TOOLWINDOW, L"STATIC", L"blipblop", WS_POPUP|WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN, 0, 0, 800, 600, 0, 0, i, 0); /* select the pixel format for the current window */ hdc = GetDC(hwnd); pixelFormat = ChoosePixelFormat(hdc, &pfd); SetPixelFormat(hdc, pixelFormat, &pfd); wglMakeCurrent(hdc, wglCreateContext(hdc)); /* cursor be gone */ SetCursor(0); init(); /* loop until escape is pressed all normal windows messages are ignored */ while (!GetAsyncKeyState(VK_ESCAPE)) { mainloop(); // nifty effects here SwapBuffers(hdc); // display the cool stuff } return 0; // exit to OS } // clean up I've been stuck trying to get my head around implementing freetype for the past 2 days. The include paths and libs for freetype are setup so they don't give off any errors.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!