File: fonts.c/*
fonts.c
A basic test of font drawing using cpwDrawFont
and listing fonts.
*/
#define CPWDLL_EXTERN
#include <cpw.h>
/* font face and list */
static CpwFontFace font;
static CpwFontList flist;
/* handy window characteristics holder */
static CpwWindowInfo windowInfo = { 0,100,100,400,200 }; /* id,posx,posy,w,h */
/****************************************************/
/* 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 )
{
uint_32 y;
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
set2DMatrix();
for ( y = 0; y <= 15; y++ ) {
glRasterPos2d( 1, y * (5+y) );
/* draw the same string using cpwDrawFont */
cpwFontMode( cpw, CPW_FONTOPT_SIZE, y*2 );
cpwDrawFont( cpw, font, "The Quick Brown Fox", 1 );
}
}
/****************************************************/
/* 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 );
}
void setupFontMenu( pCpw cpw, uint_32 submenu )
{
char buf[100];
int_32 count = 0;
while ( count < flist.size ) {
sprintf( buf, "%s", flist.list[count] );
cpwAddMenuEntry( cpw, submenu, buf, count+1, false );
count++;
/* not the whole list.. */
if ( count > CPW_MAX_FONTS ) break;
}
return;
}
/****************************************************/
/* Menu Select callback */
/****************************************************/
void menuselect( pCpw cpw, uint_32 winid, uint_32 menuid, uint_32 entryid )
{
bool res = false;
if ( entryid == 0 ) cpwBreakMainLoop( cpw );
cpwUnloadFont( cpw, font );
font = cpwLoadFont( cpw, flist.list[entryid-1], CPW_FONTLOC_HOST, "", "" );
cpwPostRedisplay( cpw );
}
/****************************************************/
/* Main */
/****************************************************/
int main(int argc, char* argv[])
{
pCpw cpw = null;
uint_32 topmenu, submenu1, submenu2;
/****************************************************/
/* Init */
/****************************************************/
cpwInitContext( &cpw );
/****************************************************/
/* Creaing Windows */
/****************************************************/
windowInfo.id =
cpwCreateWindowEx( cpw, "Basic Template", windowInfo.x, windowInfo.y,
windowInfo.width, windowInfo.height );
/****************************************************/
/* Event Callbacks */
/****************************************************/
cpwCreateCallback( cpw, window );
cpwDisplayCallback( cpw, draw );
cpwReshapeCallback( cpw, reshape );
/****************************************************/
/* Fonts */
/****************************************************/
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_LAYOUTDEBUG, 0 );
cpwFontMode( cpw, CPW_FONTOPT_SPACING, 1 );
cpwFontMode( cpw, CPW_FONTOPT_PIXELMAP_GLFORMAT, GL_LUMINANCE );
cpwListFonts( cpw, &flist, "*.ttf", CPW_FONTLOC_HOST );
font = cpwLoadFont( cpw, flist.list[0], CPW_FONTLOC_HOST, "", "" );
/****************************************************/
/* Window Menus */
/****************************************************/
topmenu = cpwCreateMenu( cpw, menuselect );
submenu1 = cpwCreateSubMenu( cpw );
submenu2 = cpwCreateSubMenu( cpw );
cpwAddMenuEntry( cpw, submenu1, "Exit", 0, false );
setupFontMenu( cpw, submenu2 );
cpwAddSubMenu( cpw, topmenu, "Control", submenu1, 100 );
cpwAddSubMenu( cpw, topmenu, "Fonts", submenu2, 102 );
cpwAssignMenuToWindow( cpw, topmenu, windowInfo.id );
/****************************************************/
/* MainLoop */
/****************************************************/
cpwMainLoop( cpw );
/****************************************************/
/* Exit and Free */
/****************************************************/
cpwFreeFontList( cpw, &flist );
cpwFreeContext( &cpw );
return 0;
}