1 /*
2  * Label.cpp
3  * Copyright (C) 2007 by Bryan Duff <duff0097@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA
19  */
20 #include "Label.h"
21 #include "Config.h"
22 
23 extern Config config;
24 
Label()25 Label::Label()
26 {
27   xpos = ypos = size = 0;
28   color = Color(0.0, 0.0, 0.0, 1.0);    //black
29 }
30 
setLabel(GLfloat _xpos,GLfloat _ypos,float _size)31 void Label::setLabel(GLfloat _xpos, GLfloat _ypos, float _size)
32 {
33   xpos = _xpos;
34   ypos = _ypos;
35   size = _size;
36 }
37 
setFont(int set,GLuint _base,GLuint _texture)38 void Label::setFont(int set, GLuint _base, GLuint _texture)
39 {
40   if(set > 1) {
41     set = 1;
42   }
43 
44   fontSet = set;
45   base = _base;
46   texture = _texture;
47 }
48 
setText(const char * string)49 void Label::setText(const char *string)
50 {
51   text = string;
52 }
53 
setColor(Color _color)54 void Label::setColor(Color _color)
55 {
56   color = _color;
57 }
58 
59 //FIXME: base, FontTexture
draw()60 void Label::draw()              // Where The Printing Happens
61 {
62   Window::draw();               //draw children
63 
64   float width = config.screenwidth;
65   float height = config.screenheight;
66 
67   GLint x = (int)(xpos * width);
68   GLint y = (int)(ypos * height);
69 
70   glEnable(GL_TEXTURE_2D);
71   glColor4f(color.r, color.g, color.b, color.a);
72   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
73   glBindTexture(GL_TEXTURE_2D, texture);        // Select Our Font Texture
74   glDisable(GL_DEPTH_TEST);     // Disables Depth Testing
75   glDisable(GL_LIGHTING);
76   glEnable(GL_BLEND);
77   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
78   glMatrixMode(GL_PROJECTION);  // Select The Projection Matrix
79   glPushMatrix();               // Store The Projection Matrix
80   glLoadIdentity();             // Reset The Projection Matrix
81   glOrtho(0, width, 0, height, -100, 100);      // Set Up An Ortho Screen
82   glMatrixMode(GL_MODELVIEW);   // Select The Modelview Matrix
83   glPushMatrix();               // Store The Modelview Matrix
84   glLoadIdentity();
85   glScalef(size, size, 1);      // Reset The Modelview Matrix
86   glTranslated(x, y, 0);        // Position The Text (0,0 - Bottom Left)
87   glListBase(base - 32 + (128 * fontSet));      // Choose The Font Set (0 or 1)
88   glCallLists(text.length(), GL_BYTE, text.c_str());    // Write The Text To The Screen
89   glMatrixMode(GL_PROJECTION);  // Select The Projection Matrix
90   glPopMatrix();                // Restore The Old Projection Matrix
91   glMatrixMode(GL_MODELVIEW);   // Select The Modelview Matrix
92   glPopMatrix();                // Restore The Old Projection Matrix
93   glEnable(GL_DEPTH_TEST);      // Enables Depth Testing
94   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
95 }
96