[3455] | 1 | /*************************************************************************** |
---|
| 2 | cone3dfont.cpp - description |
---|
| 3 | ------------------- |
---|
| 4 | copyright : (C) 2001 by Marius Andra aka Cone3D |
---|
| 5 | email : marius@hot.ee |
---|
| 6 | ICQ : 43999510 |
---|
| 7 | ***************************************************************************/ |
---|
| 8 | |
---|
| 9 | /*************************************************************************** |
---|
| 10 | * * |
---|
| 11 | * This program is free software; you can redistribute it and/or modify * |
---|
| 12 | * it under the terms of the GNU General Public License as published by * |
---|
| 13 | * the Free Software Foundation; either version 2 of the License, or * |
---|
| 14 | * (at your option) any later version. * |
---|
| 15 | * * |
---|
| 16 | ***************************************************************************/ |
---|
| 17 | |
---|
| 18 | #include "cone3dfont.h" |
---|
| 19 | #include <stdarg.h> |
---|
| 20 | |
---|
| 21 | #include "../importer/texture.h" |
---|
| 22 | |
---|
| 23 | int Cone3DFont::SetSize(float x, float y) // Set the size of the font |
---|
| 24 | { |
---|
| 25 | sizex=x; |
---|
| 26 | sizey=y; |
---|
| 27 | |
---|
| 28 | return 1; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | int Cone3DFont::BuildFont(char *file) // Build Our Font Display List |
---|
| 32 | { |
---|
| 33 | int loop1; |
---|
| 34 | float cx, cy; |
---|
| 35 | |
---|
| 36 | font = new Texture(); |
---|
| 37 | font->loadImage(file); |
---|
| 38 | |
---|
| 39 | base=glGenLists(256); // Creating 256 Display Lists |
---|
| 40 | glBindTexture(GL_TEXTURE_2D, font->getTexture()); // Select Our Font Texture |
---|
| 41 | for (loop1=0; loop1<256; loop1++) // Loop Through All 256 Lists |
---|
| 42 | { |
---|
| 43 | cx=(float)(loop1%16)/16.0f; // X Position Of Current Character |
---|
| 44 | cy=(float)(loop1/16)/16.0f; // Y Position Of Current Character |
---|
| 45 | |
---|
| 46 | glNewList(base+loop1,GL_COMPILE); // Start Building A List |
---|
| 47 | glBegin(GL_QUADS); // Use A Quad For Each Character |
---|
| 48 | glTexCoord2f(cx,1.0f-cy-0.0625f); // Texture Coord (Bottom Left) |
---|
| 49 | glVertex2d(0,16); // Vertex Coord (Bottom Left) |
---|
| 50 | glTexCoord2f(cx+0.0625f,1.0f-cy-0.0625f); // Texture Coord (Bottom Right) |
---|
| 51 | glVertex2i(16,16); // Vertex Coord (Bottom Right) |
---|
| 52 | glTexCoord2f(cx+0.0625f,1.0f-cy-0.001f); // Texture Coord (Top Right) |
---|
| 53 | glVertex2i(16,0); // Vertex Coord (Top Right) |
---|
| 54 | glTexCoord2f(cx,1.0f-cy-0.001f); // Texture Coord (Top Left) |
---|
| 55 | glVertex2i(0,0); // Vertex Coord (Top Left) |
---|
| 56 | glEnd(); // Done Building Our Quad (Character) |
---|
| 57 | glTranslated(12,0,0); // Move To The Right Of The Character |
---|
| 58 | glEndList(); // Done Building The Display List |
---|
| 59 | } // Loop Until All 256 Are Built |
---|
| 60 | return 1; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | int Cone3DFont::KillFont(GLvoid) // Delete The Font From Memory |
---|
| 64 | { |
---|
| 65 | glDeleteLists(base,256); // Delete All 256 Display Lists |
---|
| 66 | return 1; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | int Cone3DFont::PrintText(GLint x, GLint y, char type, char *fmt,...)// Where The Printing Happens |
---|
| 70 | { |
---|
| 71 | char text[1024]; // Holds Our String |
---|
| 72 | int blendOn,scissorOn,textureOn,lightOn; // Holds the previous GL settings |
---|
| 73 | int depthOn,matrixMode,screenStats[4],blendSrc,blendDst; |
---|
| 74 | char typ=type; |
---|
| 75 | |
---|
| 76 | va_list ap; // Pointer To List Of Arguments |
---|
| 77 | |
---|
| 78 | if (fmt == NULL) // If There's No Text |
---|
| 79 | return 1; // Do Nothing |
---|
| 80 | |
---|
| 81 | va_start(ap, fmt); // Parses The String For Variables |
---|
| 82 | vsprintf(text, fmt, ap); // And Converts Symbols To Actual Numbers |
---|
| 83 | va_end(ap); // Results Are Stored In Text |
---|
| 84 | |
---|
| 85 | if (type>3) // Did User Choose An Invalid Character Set? |
---|
| 86 | type=3; // If So, Select Set 2 (Italic) |
---|
| 87 | |
---|
| 88 | textureOn = glIsEnabled(GL_TEXTURE_2D); // Were textures enabled? |
---|
| 89 | depthOn = glIsEnabled(GL_DEPTH_TEST); // Was GL_DEPTH_TEST enabled? |
---|
| 90 | lightOn = glIsEnabled(GL_LIGHTING); // Was GL_LIGHTING enabled? |
---|
| 91 | scissorOn = glIsEnabled(GL_SCISSOR_TEST); // etc. |
---|
| 92 | glGetIntegerv(GL_MATRIX_MODE, &matrixMode); |
---|
| 93 | glGetIntegerv(GL_VIEWPORT, screenStats); |
---|
| 94 | blendOn= glIsEnabled(GL_BLEND); |
---|
| 95 | glGetIntegerv(GL_BLEND_SRC, &blendSrc); |
---|
| 96 | glGetIntegerv(GL_BLEND_DST, &blendDst); |
---|
| 97 | |
---|
| 98 | if (depthOn) glDisable(GL_DEPTH_TEST); // If they were enabled/disabled |
---|
| 99 | if (!textureOn) glEnable(GL_TEXTURE_2D); // then enable/disable them |
---|
| 100 | if (!blendOn) glEnable(GL_BLEND); |
---|
| 101 | if (!scissorOn) glEnable(GL_SCISSOR_TEST); |
---|
| 102 | if (lightOn) glDisable(GL_LIGHTING); |
---|
| 103 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Set the correct blending mode |
---|
| 104 | |
---|
| 105 | glMatrixMode(GL_PROJECTION); // and initalize Ortho mode |
---|
| 106 | glPushMatrix(); |
---|
| 107 | glLoadIdentity(); |
---|
| 108 | glOrtho(0.0f,screenStats[2],screenStats[3],0.0f,-1.0f,1.0f); |
---|
| 109 | glMatrixMode(GL_MODELVIEW); |
---|
| 110 | glPushMatrix(); |
---|
| 111 | glBindTexture(GL_TEXTURE_2D, font->getTexture()); |
---|
| 112 | |
---|
| 113 | if(type>1) typ=typ-2; |
---|
| 114 | glLoadIdentity(); |
---|
| 115 | glTranslated(x,y,0); |
---|
| 116 | glListBase(base-32+(128*typ)); |
---|
| 117 | glScalef(sizex,sizey,1.0f); |
---|
| 118 | glCallLists(strlen(text),GL_UNSIGNED_BYTE, text); // Write The Text To The Screen |
---|
| 119 | |
---|
| 120 | if(type>1) |
---|
| 121 | { |
---|
| 122 | glLoadIdentity(); |
---|
| 123 | glTranslated(x+1,y,0); |
---|
| 124 | glListBase(base-32+(128*(type-2))); |
---|
| 125 | glScalef(sizex,sizey,1.0f); |
---|
| 126 | glCallLists(strlen(text),GL_UNSIGNED_BYTE, text); // Write The Text To The Screen |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | glBindTexture(GL_TEXTURE_2D, 0); // Fix everything up |
---|
| 130 | glMatrixMode(GL_PROJECTION); |
---|
| 131 | glPopMatrix(); |
---|
| 132 | glMatrixMode(GL_MODELVIEW); |
---|
| 133 | glPopMatrix(); |
---|
| 134 | |
---|
| 135 | if (depthOn) glEnable(GL_DEPTH_TEST); |
---|
| 136 | if (!textureOn) glDisable(GL_TEXTURE_2D); |
---|
| 137 | if (!blendOn) glDisable(GL_BLEND); |
---|
| 138 | if (!scissorOn) glDisable(GL_SCISSOR_TEST); |
---|
| 139 | if (lightOn) glEnable(GL_LIGHTING); |
---|
| 140 | |
---|
| 141 | glBlendFunc(blendSrc, blendDst); |
---|
| 142 | glMatrixMode(matrixMode); |
---|
| 143 | |
---|
| 144 | return 1; |
---|
| 145 | } |
---|