Back to index

extremetuxracer  0.5beta
callbacks.cpp
Go to the documentation of this file.
00001 /* 
00002  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017  */
00018 
00019 //for the code that specifies an outline font's width
00020 #include <GL/gl.h> 
00021 #include "ppgltk/FT/FTGLOutlineFont.h"
00022  
00023 #include "callbacks.h"
00024 
00025 #include "tcl_util.h"
00026 #include "ppgltk/font.h"
00027 #include "ppgltk/alg/color.h"
00028 
00029 static int
00030 pp_register_font_cb ( ClientData cd, Tcl_Interp *ip, 
00031                        int argc, CONST84 char *argv[])
00032 {
00033        bool error = false;
00034        double tmp_dbl;
00035 
00036     CONST84 char *binding = NULL;
00037     CONST84 char *fileName = NULL;
00038     bool isOutline = false;
00039     bool width_defined = false;
00040     GLfloat width;
00041     pp::Color color = pp::Color::white;
00042     unsigned int size = 30;
00043 
00044        if ( argc < 7 ) {
00045               Tcl_AppendResult(ip, argv[0], ": invalid number of arguments\n", 
00046                       "Usage: ", argv[0], " -binding <binding> -font <ttf file> -size <fontsize> [-color <color>]",
00047                       (char *)0 );
00048         return TCL_ERROR;
00049     } 
00050            
00051     NEXT_ARG;
00052 
00053     while ( !error && argc > 0 ) {
00054 
00055        if ( strcmp( "-binding", *argv ) == 0 ) {
00056            NEXT_ARG;
00057            if ( argc == 0 ) {
00058               error = true;
00059               break;
00060            }
00061            binding = *argv;
00062        } else if ( strcmp( "-font", *argv ) == 0 ) {
00063            NEXT_ARG;
00064            if ( argc == 0 ) {
00065               error = true;
00066               break;
00067            }
00068            fileName = *argv;
00069        } else if ( strcmp( "-size", *argv ) == 0 ) {
00070            NEXT_ARG;
00071            if ( argc == 0 ) {
00072               error = true;
00073               break;
00074            }
00075            if ( Tcl_GetDouble ( ip, *argv, &tmp_dbl ) == TCL_ERROR ) {
00076               error = true;
00077               break;
00078            }
00079            size = int(tmp_dbl);
00080        } else if ( strcmp( "-color", *argv ) == 0 ) {
00081            NEXT_ARG;
00082            if ( argc == 0 ) {
00083               error = true;
00084               break;
00085            }
00086            if ( get_tcl_tuple( ip, *argv, (double*)&color, 4 ) == 
00087                TCL_ERROR ) 
00088            {
00089               error = true;
00090               break;
00091            }
00092     } else if ( strcmp( "-outline", *argv ) == 0 ) {
00093         isOutline = true;
00094        } else if ( strcmp( "-width", *argv ) == 0 ) {
00095         NEXT_ARG;
00096            if ( argc == 0 ) {
00097             error = true;
00098             break;
00099            }
00100         if(isOutline) {
00101             if ( Tcl_GetDouble ( ip, *argv, &tmp_dbl ) == TCL_ERROR ) {
00102                 error = true;
00103                 break;
00104            }
00105             width_defined = true;
00106            width = GLfloat(tmp_dbl);
00107         } else {
00108             print_warning( TCL_WARNING, "pp_load_font: -width parameter only needed for outline fonts !");
00109         }
00110        } else {
00111            print_warning( TCL_WARNING, "pp_load_font: unrecognized "
00112                         "parameter `%s'", *argv );
00113        }
00114 
00115        NEXT_ARG;
00116     }
00117 
00118     if ( binding == NULL || fileName == NULL ){
00119               error = true;
00120     }
00121 
00122     if ( error ) {
00123               return TCL_ERROR;
00124     }
00125 
00126        pp::Font::registerFont(binding, fileName, size, color, isOutline);
00127     
00128     if(width_defined) {
00129         FTGLOutlineFont* outlineFont = (FTGLOutlineFont*)(pp::Font::get(binding)->getFTFont());
00130         outlineFont->setWidth(width);
00131     }
00132 
00133     return TCL_OK;
00134 }
00135 
00136 static int
00137 pp_bind_font_cb ( ClientData cd, Tcl_Interp *ip, 
00138                        int argc, CONST84 char *argv[])
00139 {
00140        bool error = false;
00141 
00142     CONST84 char *binding = NULL;
00143     CONST84 char *fontName = NULL;
00144     pp::Color *color = NULL;
00145 
00146        NEXT_ARG;
00147 
00148     while ( !error && argc > 0 ) {
00149 
00150        if ( strcmp( "-binding", *argv ) == 0 ) {
00151            NEXT_ARG;
00152            if ( argc == 0 ) {
00153               error = true;
00154               break;
00155            }
00156            binding = *argv;
00157        } else if ( strcmp( "-font", *argv ) == 0 ) {
00158            NEXT_ARG;
00159               if ( argc == 0 ) {
00160                      error = true;
00161                      break;
00162            }
00163            fontName = *argv;
00164        } else if ( strcmp( "-color", *argv ) == 0 ) {
00165            NEXT_ARG;
00166            if ( argc == 0 ) {
00167                      error = true;
00168                      break;
00169            }
00170               
00171               color = new pp::Color;
00172               
00173            if ( get_tcl_tuple( ip, *argv, (double*)color, 4 ) == 
00174                TCL_ERROR ) 
00175            {
00176                      error = true;
00177                      break;
00178            }
00179        } else {
00180            print_warning( TCL_WARNING, "pp_bind_font: unrecognized "
00181                         "parameter `%s'", *argv );
00182        }
00183 
00184        NEXT_ARG;
00185     }
00186 
00187     if ( binding == NULL || fontName == NULL ){
00188               error = true;
00189     }
00190 
00191     if ( error ) {
00192               return TCL_ERROR;
00193     }
00194 
00195        if(color){
00196               pp::Font::bindFont(binding, fontName, *color);
00197               delete color;
00198        }else{
00199               pp::Font::bindFont(binding, fontName);
00200        }   
00201     return TCL_OK;
00202 }
00203 
00204 
00205 void
00206 register_common_callbacks( Tcl_Interp *ip )
00207 {
00208     Tcl_CreateCommand (ip, "pp_load_font", pp_register_font_cb,   0,0);
00209     Tcl_CreateCommand (ip, "pp_bind_font", pp_bind_font_cb,   0,0);
00210 }