• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Katz

Problem with lesson 43

1 post in this topic

Hello !

I'm having troubles with lesson 43. I can't get FreeType to work and display text. I made a test program to try to make it work.
I've changed some things from lesson 43, taken out some things I wouldn't need, changed some variables names to respect my own conventions, and mixed it up a little with the tutorial on FreeType's website.

Problem is I don't get any text. OpenGL works fine, as I get a red rectangle. But no text.
The main problem is I don't really understand all that was done in the tutorial, so I might have changed some stuff I wasn't supposed to... I especially cut a whole lot in the "print()" function (renamed "write()" in my code).

Can someone help me get this code to work? Or point out at least what vital part I'm missing, so I can look it up in the tutorial?

Thanks !

Code follows:
[code]#include <SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <vector>
#include <string>
#include <iostream>

using namespace std;

struct font_data
{
int h;
GLuint *textures;
GLuint listBase;

void init(string fontName, unsigned int h); //create font of required height
void clean(); //clean font data
};

void write(const font_data &ft_font, int x, int y, string text);

//get power of two right above given number
inline int nextP2(int nb)
{
int ret=1;
while(ret < nb)
ret *= 2;
return ret;
}

//create a display lit corresponding to the given character
void makeDList(FT_Face font, char ch, GLuint listBase, GLuint *texBase)
{
if(FT_Load_Char(font, ch, FT_LOAD_RENDER))
cout << "FT_Load_Char failed in makeDList." << endl;
FT_Bitmap &bitmap = font->glyph->bitmap;

//get required with and height to get correct texture size
int width = nextP2(bitmap.width);
int height = nextP2(bitmap.rows);

GLubyte *expandedData = new GLubyte [2*width*height];

for(int j=0;j<height;j++)
{
for(int i=0;i<width;i++)
expandedData[2*(i+j*width)] = expandedData[2*(i+j*width)+1] = (i>=bitmap.width || j>=bitmap.rows) ? 0 : bitmap.buffer[i+bitmap.width*j];
}

glBindTexture(GL_TEXTURE_2D, texBase[ch]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, expandedData);

delete [] expandedData;

glNewList(listBase + ch, GL_COMPILE);

glBindTexture(GL_TEXTURE_2D, texBase[ch]);

glPushMatrix();

//glTranslatef(font->glyph->left,0,0); //shift character to have right amount of space before
//glTranslatef(0,font->glyph->top - bitmap.rows,0); //move down a little if previous line has lower character
int x = bitmap.width / width; //take empty space around char into account
int y = bitmap.rows / height;

glBegin(GL_QUADS);
glTexCoord2d(0,0);
glVertex2f(0,bitmap.rows);
glTexCoord2d(0,y);
glVertex2f(0,0);
glTexCoord2d(x,y);
glVertex2f(bitmap.width,0);
glTexCoord2d(x,0);
glVertex2f(bitmap.width,bitmap.rows);
glEnd();
glPopMatrix();
//glTranslatef(font->glyph->advance.x * 64,0,0);

glEndList();
}

void font_data::init(string fontName, unsigned int h)
{
textures = new GLuint[128];
this->h=h;

FT_Library library;
if(FT_Init_FreeType(&library))
cout << "Could not initialize Freetype library" << endl;

FT_Face font;
if(FT_New_Face(library, fontName.c_str(), 0, &font))
cout << "Could not open font \"" <<fontName<<"\"."<<endl;
FT_Set_Char_Size(font, h*64,h*64,96,96); //set font size

//allocate resources for textures and display lists
listBase=glGenLists(128);
glGenTextures(128,textures);

//create each of the fonts display lists
for(unsigned char i=0;i<128;i++)
makeDList(font,i,listBase,textures);

FT_Done_Face(font);
FT_Done_FreeType(library);
}

void font_data::clean()
{
glDeleteLists(listBase,128);
glDeleteTextures(128,textures);
delete [] textures;
}

void write(const font_data &ft_font, int x, int y, string text)
{
GLuint font = ft_font.listBase;
glListBase(font);

glPushMatrix();
glLoadIdentity();
glTranslatef(x,y,0); //move to text
glCallLists(text.length(), GL_UNSIGNED_BYTE,text.c_str());
glPopMatrix();
}

int main ( int argc, char** argv )
{
SDL_Init( SDL_INIT_VIDEO );
SDL_SetVideoMode(800, 600, 32, SDL_OPENGL);
//initialize freetype
/*FT_Library freetype;
if(FT_Init_FreeType(&freetype) != 0)
{
cout << "Unable to initialize Freetype library." <<endl;
exit(EXIT_FAILURE);
}*/
//set openGL parameters
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,800,600,0);
glDisable(GL_DEPTH_TEST); //depth buffering
glEnable(GL_TEXTURE_2D); //texture using
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

font_data myFont;
myFont.init("../TD/fonts/jester.ttf", 40);

SDL_Event event;
bool quit = false;
while(!quit)
{
glClear(GL_COLOR_BUFFER_BIT);

glColor3ub(255,0,0);
glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glVertex2d(180,280);
glVertex2d(480,280);
glVertex2d(480,480);
glVertex2d(180,480);
glEnd();
glColor3ub(0,255,0);
glEnable(GL_TEXTURE_2D);
write(myFont,200,300, "Hello World!");

glFlush();
SDL_GL_SwapBuffers();

while (SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_RETURN)))
quit=true;
}
}

myFont.clean();
SDL_Quit();
}[/code]
0

Share this post


Link to post
Share on other sites
OK, I solved my own problem, after much looking around.
I had two main problems: One was that my texture coordinates were entered into integers, although they are supposed to by smaller than 1. (Two days for this problem... *facepalm*)
The other was that I am using a different coordinate system: My ordinate axis runs from top to bottom and not bottom to top.

Anyway, working code, if it may help anybody:
[code]
#include <SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <vector>
#include <string>
#include <iostream>

using namespace std;

struct font_data
{
int h;
GLuint *textures;
GLuint listBase;

void init(string fontName, unsigned int h); //create font of required height
void clean(); //clean font data
};

void write(const font_data &ft_font, int x, int y, string text);

//get power of two right above given number
inline int nextP2(int nb)
{
int ret=1;
while(ret < nb)
ret *= 2;
return ret;
}

//create a display lit corresponding to the given character
void makeDList(FT_Face font, unsigned char ch, GLuint listBase, GLuint *texBase)
{
if(FT_Load_Char(font, ch, FT_LOAD_RENDER))
cout << "FT_Load_Char failed in makeDList." << endl;
FT_GlyphSlot glyph = font->glyph;
FT_Bitmap &bitmap = glyph->bitmap;

//get required width and height to get correct texture size
int width = nextP2(bitmap.width);
int height = nextP2(bitmap.rows);

GLubyte *expandedData = new GLubyte [2*width*height];

for(int j=0;j<height;j++)
{
for(int i=0;i<width;i++)
{
expandedData[2*(i+j*width)] = 255;
expandedData[2*(i+j*width)+1] = (i>=bitmap.width || j>=bitmap.rows) ? 0 : bitmap.buffer[i+bitmap.width*j];
}
}

glBindTexture(GL_TEXTURE_2D, texBase[ch]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, expandedData);

delete [] expandedData;

glNewList(listBase + ch, GL_COMPILE);

glBindTexture(GL_TEXTURE_2D, texBase[ch]);

glPushMatrix();
glTranslatef(glyph->bitmap_left,0,0); //shift character to have right amount of space before
glTranslatef(0, (font->bbox.yMax - glyph->metrics.horiBearingY)/64,0); //move down a little if previous line has lower character
double x = (double)bitmap.width /(double)width; //take empty space around char into account
double y = (double)bitmap.rows / (double)height;

glBegin(GL_QUADS);
glTexCoord2d(0,y);
glVertex2f(0,bitmap.rows);
glTexCoord2d(0,0);
glVertex2f(0,0);
glTexCoord2d(x,0);
glVertex2f(bitmap.width,0);
glTexCoord2d(x,y);
glVertex2f(bitmap.width,bitmap.rows);
glEnd();
glPopMatrix();
glTranslatef(glyph->metrics.horiAdvance/64,0,0);

glEndList();
}

void font_data::init(string fontName, unsigned int h)
{
textures = new GLuint[128];
this->h=h;

FT_Library library;
if(FT_Init_FreeType(&library))
cout << "Could not initialize Freetype library" << endl;

FT_Face font;
if(FT_New_Face(library, fontName.c_str(), 0, &font))
cout << "Could not open font \"" <<fontName<<"\"."<<endl;
FT_Set_Char_Size(font, h*64,h*64,96,96); //set font size

//allocate resources for textures and display lists
listBase=glGenLists(128);
glGenTextures(128,textures);

//create each of the fonts display lists
for(unsigned char i=0;i<128;i++)
makeDList(font,i,listBase,textures);

FT_Done_Face(font);
FT_Done_FreeType(library);
}

void font_data::clean()
{
glDeleteLists(listBase,128);
glDeleteTextures(128,textures);
delete [] textures;
}

void write(const font_data &ft_font, int x, int y, string text)
{
GLuint font = ft_font.listBase;
glListBase(font);

glPushMatrix();
glTranslatef(x,y,0); //move to text
glCallLists(text.length(), GL_UNSIGNED_BYTE,text.c_str());
glPopMatrix();
}

int main ( int argc, char** argv )
{
SDL_Init( SDL_INIT_VIDEO );
SDL_SetVideoMode(800, 600, 32, SDL_OPENGL);
//set openGL parameters
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,800,600,0);
glDisable(GL_DEPTH_TEST); //depth buffering
glEnable(GL_TEXTURE_2D); //texture using
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

font_data myFont;
myFont.init("../TD/fonts/jester.ttf", 20);

SDL_Event event;
bool quit = false;
while(!quit)
{
glClear(GL_COLOR_BUFFER_BIT);

glColor3ub(255,255,255);
glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glVertex2d(0,0);
glVertex2d(800,0);
glVertex2d(800,600);
glVertex2d(0,600);
glEnd();
glEnable(GL_TEXTURE_2D);
glColor3ub(180,180,0);
write(myFont,200,300, "Hello World!");

glFlush();
SDL_GL_SwapBuffers();

while (SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_RETURN)))
quit=true;
}
}

myFont.clean();
SDL_Quit();
}
[/code]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0