File: bufferedfont.c/*
bufferedfont.c
A basic test for buffered font rendering. Buffered font
rendering allows the developer to pre-render a string of
text or character set into bitmaps which can then be
drawn into the scene through various methods. (texture
maps, direct writes, extrusion, etc.)
*/
#define CPWDLL_EXTERN
#include <cpw.h>
/* handy window characteristics holder */
static CpwWindowInfo windowInfo = { 0,100,100,400,200 }; /* id,posx,posy,w,h */
static char * testString = "AbcJji!@#$%^*;[1]{1}|";
static CpwFontFace font1;
static CpwFontFace font2;
static CpwFontFace font3;
static CpwFontBuffer cpwbuffer;
/****************************************************/
/* OpenGL 2D Matrix Setup */
/****************************************************/
void set2DMatrix( void )
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D( 0, windowInfo.width, 0, windowInfo.height );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
/****************************************************/
/* OpenGL 3D Matrix Setup */
/****************************************************/
void set3DMatrix( void )
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 60, windowInfo.width / windowInfo.height, 1, 100 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
/****************************************************/
/* Draw Window One */
/****************************************************/
void drawWindowOne( pCpw cpw )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
set2DMatrix();
/* draw something in the window with OpenGL */
glPushMatrix();
glRasterPos2d( 1, 1 );
/* draw the bufferd font into the frame buffer */
glDrawPixels( cpwbuffer.w, cpwbuffer.h, cpwbuffer.glformat, GL_UNSIGNED_BYTE, cpwbuffer.data );
glRasterPos2d( 1, 40 );
/* draw the same string using cpwDrawFont */
cpwDrawFont( cpw, font1, testString, 1 );
glRasterPos2d( 1, 80 );
/* draw the same string using cpwDrawFont */
cpwDrawFont( cpw, font2, testString, 1 );
glRasterPos2d( 1, 120 );
/* draw the same string using cpwDrawFont */
cpwDrawFont( cpw, font3, testString, 1 );
glPopMatrix();
}
/****************************************************/
/* Window Draw Event callback */
/****************************************************/
void draw( pCpw cpw, uint_32 winid )
{
drawWindowOne( cpw );
cpwSwapWindowBuffers( cpw, winid );
}
/****************************************************/
/* Window Create / Destroy Event callback */
/****************************************************/
void window( pCpw cpw, uint_32 winid, bool flag )
{
/* creation event */
if ( flag == true ) {
glShadeModel( GL_SMOOTH );
glDepthFunc( GL_LEQUAL );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
glEnable( GL_LINE_SMOOTH );
glEnable( GL_POLYGON_SMOOTH );
glClearColor( 0.0, 0.0, 0.0, 1.0 );
return;
}
/* window close event */
if ( flag == false ) {
cpwDestroyWindow( cpw, winid );
return;
}
}
/****************************************************/
/* Window Resize Event callback */
/****************************************************/
void reshape( pCpw cpw, uint_32 winid, uint_32 width, uint_32 height )
{
if ( height == 0 ) { height = 1; }
if ( width == 0 ) { width = 1; }
windowInfo.width = width;
windowInfo.height = height;
set2DMatrix();
glViewport( 0, 0, width, height );
}
/****************************************************/
/* Main */
/****************************************************/
int main(int argc, char* argv[])
{
pCpw cpw = null;
/****************************************************/
/* Init */
/****************************************************/
cpwInitContext( &cpw );
/****************************************************/
/* Creaing Windows */
/****************************************************/
windowInfo.id =
cpwCreateWindowEx( cpw, "Buffered Font", windowInfo.x, windowInfo.y,
windowInfo.width, windowInfo.height );
/****************************************************/
/* Event Callbacks */
/****************************************************/
cpwCreateCallback( cpw, window );
cpwDisplayCallback( cpw, draw );
cpwReshapeCallback( cpw, reshape );
/****************************************************/
/* Fonts */
/****************************************************/
glPixelStorei( GL_UNPACK_LSB_FIRST, GL_FALSE );
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
cpwFontMode( cpw, CPW_FONTOPT_PIXELMAP, 0 );
cpwFontMode( cpw, CPW_FONTOPT_ALIGNLEFT, 0 );
cpwFontMode( cpw, CPW_FONTOPT_SIZE, 30 );
cpwFontMode( cpw, CPW_FONTOPT_KERN, 1 );
cpwFontMode( cpw, CPW_FONTOPT_PIXELMAP_GLFORMAT, GL_RGB );
//cpwFontMode( cpw, CPW_FONTOPT_PIXELMAP_GLFORMAT, GL_RGBA );
//cpwFontMode( cpw, CPW_FONTOPT_PIXELMAP_GLFORMAT, GL_LUMINANCE );
cpwFontMode( cpw, CPW_FONTOPT_LAYOUTDEBUG, 0 );
cpwFontMode( cpw, CPW_FONTOPT_SPACING, 1 );
glColor3f( 0.0, 1.0, 0.0 );
font1 = cpwLoadFont( cpw, "times.ttf", CPW_FONTLOC_HOST, "", "" );
font2 = cpwLoadFont( cpw, "arial.ttf", CPW_FONTLOC_HOST, "", "" );
font3 = cpwLoadFont( cpw, "cour.ttf", CPW_FONTLOC_HOST, "", "" );
/* pre-render our font string into a buffer */
cpwDrawBufferedFont( cpw, font1, testString, &cpwbuffer );
/****************************************************/
/* MainLoop */
/****************************************************/
cpwMainLoop( cpw );
/****************************************************/
/* Exit and Free */
/****************************************************/
/* free the font buffer */
cpwFreeBufferedFont( cpw, &cpwbuffer );
cpwFreeContext( &cpw );
return 0;
}