1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
// -----------------------------------------------
// Simple 2D font
// -----------------------------------------------
#include "GLFont.h"
#include <CImage.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
extern char *LID(char *fileName);
// -------------------------------------------
GLFont2D::GLFont2D() {
}
// -------------------------------------------
int GLFont2D::RestoreDeviceObjects(int scrWidth,int scrHeight) {
// Load the image
CImage img;
if( !img.LoadImage(LID("images/font.png")) ) {
printf("Failed to load %s\n",LID("images/font.png"));
return 0;
}
// Make 32 Bit RGBA buffer
fWidth = img.Width();
fHeight = img.Height();
BYTE *buff32 = (BYTE *)malloc(fWidth*fHeight*4);
BYTE *data = img.GetData();
for(int y=0;y<fHeight;y++) {
for(int x=0;x<fWidth;x++) {
buff32[x*4 + 0 + y*4*fWidth] = data[x*3+2 + y*3*fWidth];
buff32[x*4 + 1 + y*4*fWidth] = data[x*3+1 + y*3*fWidth];
buff32[x*4 + 2 + y*4*fWidth] = data[x*3+0 + y*3*fWidth];
buff32[x*4 + 3 + y*4*fWidth] = data[x*3+1 + y*3*fWidth]; // Green as alpha
}
}
glGenTextures(1,&texId);
glBindTexture(GL_TEXTURE_2D,texId);
glTexImage2D (
GL_TEXTURE_2D, // Type
0, // No Mipmap
4, // Format RGBA
fWidth, // Width
fHeight, // Height
0, // Border
GL_RGBA, // Format RGBA
GL_UNSIGNED_BYTE, // 8 Bit/color
buff32 // Data
);
free(buff32);
img.Release();
if( glGetError() != GL_NO_ERROR )
{
printf("Failed to create font texture: glcode=%d\n",glGetError());
return 0;
}
// Compute othographic matrix (for Transfomed Lit vertex)
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, scrWidth, scrHeight, 0, -1, 1 );
glGetFloatv( GL_PROJECTION_MATRIX , pMatrix );
return 1;
}
// -------------------------------------------
void GLFont2D::InvalidateDeviceObjects() {
if(texId) glDeleteTextures(1, &texId);
texId = 0;
}
// -------------------------------------------
void GLFont2D::DrawText(int x,int y,char *text) {
int lgth = (int)strlen(text);
glDisable(GL_CULL_FACE);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glColor3f(1.0f,1.0f,0.0f);
glMatrixMode( GL_PROJECTION );
glLoadMatrixf(pMatrix);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
for(int i=0;i<lgth;i++ ) {
char c = text[i];
float xPos = (float)((c % 16) * 16 + 1)/ (float)fWidth;
float yPos = (float)((c / 16) * 16 )/ (float)fHeight;
float cW = 9.0f / (float)fWidth;
float cH = 15.0f / (float)fWidth;
glBegin(GL_QUADS);
glTexCoord2f(xPos ,yPos );glVertex2i(x+9*i ,y );
glTexCoord2f(xPos+cW,yPos );glVertex2i(x+9*(i+1),y );
glTexCoord2f(xPos+cW,yPos+cH);glVertex2i(x+9*(i+1),y+15);
glTexCoord2f(xPos ,yPos+cH);glVertex2i(x+9*i ,y+15);
glEnd();
}
}
|