*** empty log message ***
git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@1049 e016b1c5-ed68-44aa-a2f7-31cac88cffde
This commit is contained in:
+97
-124
@@ -2,113 +2,76 @@
|
||||
|
||||
#include "cube.h"
|
||||
|
||||
short char_coords[96][4] =
|
||||
struct font
|
||||
{
|
||||
{0,0,25,64}, //!
|
||||
{25,0,54,64}, //"
|
||||
{54,0,107,64}, //#
|
||||
{107,0,148,64}, //$
|
||||
{148,0,217,64}, //%
|
||||
{217,0,263,64}, //&
|
||||
{263,0,280,64}, //'
|
||||
{280,0,309,64}, //(
|
||||
{309,0,338,64}, //)
|
||||
{338,0,379,64}, //*
|
||||
{379,0,432,64}, //+
|
||||
{432,0,455,64}, //,
|
||||
{455,0,484,64}, //-
|
||||
{0,64,21,128}, //.
|
||||
{23,64,52,128}, ///
|
||||
{52,64,93,128}, //0
|
||||
{93,64,133,128}, //1
|
||||
{133,64,174,128}, //2
|
||||
{174,64,215,128}, //3
|
||||
{215,64,256,128}, //4
|
||||
{256,64,296,128}, //5
|
||||
{296,64,337,128}, //6
|
||||
{337,64,378,128}, //7
|
||||
{378,64,419,128}, //8
|
||||
{419,64,459,128}, //9
|
||||
{459,64,488,128}, //:
|
||||
{0,128,29,192}, //;
|
||||
{29,128,81,192}, //<
|
||||
{81,128,134,192}, //=
|
||||
{134,128,186,192}, //>
|
||||
{186,128,221,192}, //?
|
||||
{221,128,285,192}, //@
|
||||
{285,128,329,192}, //A
|
||||
{329,128,373,192}, //B
|
||||
{373,128,418,192}, //C
|
||||
{418,128,467,192}, //D
|
||||
{0,192,40,256}, //E
|
||||
{40,192,77,256}, //F
|
||||
{77,192,127,256}, //G
|
||||
{127,192,175,256}, //H
|
||||
{175,192,202,256}, //I
|
||||
{202,192,231,256}, //J
|
||||
{231,192,275,256}, //K
|
||||
{275,192,311,256}, //L
|
||||
{311,192,365,256}, //M
|
||||
{365,192,413,256}, //N
|
||||
{413,192,463,256}, //O
|
||||
{1,256,38,320}, //P
|
||||
{38,256,89,320}, //Q
|
||||
{89,256,133,320}, //R
|
||||
{133,256,176,320}, //S
|
||||
{177,256,216,320}, //T
|
||||
{217,256,263,320}, //U
|
||||
{263,256,307,320}, //V
|
||||
{307,256,370,320}, //W
|
||||
{370,256,414,320}, //X
|
||||
{414,256,453,320}, //Y
|
||||
{453,256,497,320}, //Z
|
||||
{0,320,29,384}, //[
|
||||
{29,320,58,384}, //"\"
|
||||
{59,320,87,384}, //]
|
||||
{87,320,139,384}, //^
|
||||
{139,320,180,384}, //_
|
||||
{180,320,221,384}, //`
|
||||
{221,320,259,384}, //a
|
||||
{259,320,299,384}, //b
|
||||
{299,320,332,384}, //c
|
||||
{332,320,372,384}, //d
|
||||
{372,320,411,384}, //e
|
||||
{411,320,433,384}, //f
|
||||
{435,320,473,384}, //g
|
||||
{0,384,40,448}, //h
|
||||
{40,384,56,448}, //i
|
||||
{58,384,80,448}, //j
|
||||
{80,384,118,448}, //k
|
||||
{118,384,135,448}, //l
|
||||
{135,384,197,448}, //m
|
||||
{197,384,238,448}, //n
|
||||
{238,384,277,448}, //o
|
||||
{277,384,317,448}, //p
|
||||
{317,384,356,448}, //q
|
||||
{357,384,384,448}, //r
|
||||
{385,384,417,448}, //s
|
||||
{417,384,442,448}, //t
|
||||
{443,384,483,448}, //u
|
||||
{0,448,38,512}, //v
|
||||
{38,448,90,512}, //w
|
||||
{90,448,128,512}, //x
|
||||
{128,448,166,512}, //y
|
||||
{166,448,200,512}, //z
|
||||
{200,448,241,512}, //{
|
||||
{241,448,270,512}, //|
|
||||
{270,448,310,512}, //}
|
||||
{310,448,363,512}, //~
|
||||
struct charinfo
|
||||
{
|
||||
short x, y, w, h;
|
||||
};
|
||||
|
||||
char *name;
|
||||
Texture *tex;
|
||||
vector<charinfo> chars;
|
||||
short defaultw, defaulth;
|
||||
short offsetx, offsety, offsetw, offseth;
|
||||
};
|
||||
|
||||
static hashtable<char *, font> fonts;
|
||||
static font *fontdef = NULL, *curfont = NULL;
|
||||
|
||||
void newfont(char *name, char *tex, char *defaultw, char *defaulth, char *offsetx, char *offsety, char *offsetw, char *offseth)
|
||||
{
|
||||
font *f = fonts.access(name);
|
||||
if(!f)
|
||||
{
|
||||
name = newstring(name);
|
||||
f = &fonts[name];
|
||||
f->name = name;
|
||||
}
|
||||
|
||||
f->tex = textureload(tex);
|
||||
f->chars.setsize(0);
|
||||
f->defaultw = ATOI(defaultw);
|
||||
f->defaulth = ATOI(defaulth);
|
||||
f->offsetx = ATOI(offsetx);
|
||||
f->offsety = ATOI(offsety);
|
||||
f->offsetw = ATOI(offsetw);
|
||||
f->offseth = ATOI(offseth);
|
||||
|
||||
fontdef = f;
|
||||
}
|
||||
|
||||
void fontchar(int x, int y, int w, int h)
|
||||
{
|
||||
if(!fontdef) return;
|
||||
|
||||
font::charinfo &c = fontdef->chars.add();
|
||||
c.x = x;
|
||||
c.y = y;
|
||||
c.w = w ? w : fontdef->defaultw;
|
||||
c.h = h ? h : fontdef->defaulth;
|
||||
}
|
||||
|
||||
COMMANDN(font, newfont, ARG_8STR);
|
||||
COMMANDN(fontchar, fontchar, ARG_4INT);
|
||||
|
||||
bool setfont(char *name)
|
||||
{
|
||||
font *f = fonts.access(name);
|
||||
if(!f) return false;
|
||||
curfont = f;
|
||||
return true;
|
||||
}
|
||||
|
||||
int char_width(int c, int x)
|
||||
{
|
||||
if(c=='\t') x = (x+PIXELTAB)/PIXELTAB*PIXELTAB;
|
||||
else if(c==' ') x += FONTH/2;
|
||||
else if(c>=33 && c<=126)
|
||||
if(!curfont) return x;
|
||||
else if(c=='\t') x = (x+PIXELTAB)/PIXELTAB*PIXELTAB;
|
||||
else if(c==' ') x += curfont->defaultw;
|
||||
else if(curfont->chars.inrange(c-33))
|
||||
{
|
||||
c -= 33;
|
||||
int in_width = char_coords[c][2] - char_coords[c][0];
|
||||
x += in_width + 1;
|
||||
x += curfont->chars[c].w + 1;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
@@ -201,19 +164,17 @@ void draw_textf(const char *fstr, int left, int top, ...)
|
||||
|
||||
void draw_text(const char *str, int left, int top)
|
||||
{
|
||||
static Texture *charstex = NULL;
|
||||
if(!charstex) charstex = textureload("packages/misc/chars.png");
|
||||
if(!curfont) return;
|
||||
|
||||
glBlendFunc(GL_ONE, GL_ONE);
|
||||
glBindTexture(GL_TEXTURE_2D, charstex->id);
|
||||
glBindTexture(GL_TEXTURE_2D, curfont->tex->id);
|
||||
glColor3ub(255, 255, 255);
|
||||
|
||||
int x = left, y = top, col = 0;
|
||||
|
||||
float in_left, in_top, in_right, in_bottom;
|
||||
int in_width, in_height;
|
||||
static float colorstack[8][4];
|
||||
int colorpos = 0, x = left, y = top, col = 0;
|
||||
|
||||
for (int i = 0; str[i]; i++)
|
||||
glBegin(GL_QUADS);
|
||||
for(int i = 0; str[i]; i++)
|
||||
{
|
||||
int c = str[i];
|
||||
switch(c)
|
||||
@@ -232,33 +193,45 @@ void draw_text(const char *str, int left, int top)
|
||||
case '3': glColor3ub(255, 64, 64); i++; continue; // red: important errors
|
||||
case '4': glColor3ub(128, 128, 128); i++; continue; // gray
|
||||
case '5': glColor3ub(255, 255, 255); i++; continue; // white: everything else
|
||||
case 's': // save color
|
||||
if((size_t)colorpos<sizeof(colorstack)/sizeof(colorstack[0]))
|
||||
{
|
||||
glEnd();
|
||||
glGetFloatv(GL_CURRENT_COLOR, colorstack[colorpos++]);
|
||||
glBegin(GL_QUADS);
|
||||
}
|
||||
i++;
|
||||
continue;
|
||||
case 'r': // restore color
|
||||
if(colorpos>0)
|
||||
glColor4fv(colorstack[--colorpos]);
|
||||
i++;
|
||||
continue;
|
||||
default: i++; continue;
|
||||
}
|
||||
|
||||
case ' ':
|
||||
x += FONTH/2;
|
||||
x += curfont->defaultw;
|
||||
continue;
|
||||
}
|
||||
|
||||
c -= 33;
|
||||
if(c<0 || c>=95) continue;
|
||||
if(!curfont->chars.inrange(c)) continue;
|
||||
|
||||
in_left = ((float) char_coords[c][0]) / 512.0f;
|
||||
in_top = ((float) char_coords[c][1]+2) / 512.0f;
|
||||
in_right = ((float) char_coords[c][2]) / 512.0f;
|
||||
in_bottom = ((float) char_coords[c][3]-2) / 512.0f;
|
||||
font::charinfo &info = curfont->chars[c];
|
||||
float tc_left = (info.x + curfont->offsetx) / float(curfont->tex->xs);
|
||||
float tc_top = (info.y + curfont->offsety) / float(curfont->tex->ys);
|
||||
float tc_right = (info.x + info.w + curfont->offsetw) / float(curfont->tex->xs);
|
||||
float tc_bottom = (info.y + info.h + curfont->offseth) / float(curfont->tex->ys);
|
||||
|
||||
in_width = char_coords[c][2] - char_coords[c][0];
|
||||
in_height = char_coords[c][3] - char_coords[c][1];
|
||||
glTexCoord2f(tc_left, tc_top ); glVertex2i(x, y);
|
||||
glTexCoord2f(tc_right, tc_top ); glVertex2i(x + info.w, y);
|
||||
glTexCoord2f(tc_right, tc_bottom); glVertex2i(x + info.w, y + info.h);
|
||||
glTexCoord2f(tc_left, tc_bottom); glVertex2i(x, y + info.h);
|
||||
|
||||
glBegin(GL_QUADS);
|
||||
glTexCoord2f(in_left, in_top ); glVertex2i(x, y);
|
||||
glTexCoord2f(in_right, in_top ); glVertex2i(x + in_width, y);
|
||||
glTexCoord2f(in_right, in_bottom); glVertex2i(x + in_width, y + in_height);
|
||||
glTexCoord2f(in_left, in_bottom); glVertex2i(x, y + in_height);
|
||||
glEnd();
|
||||
|
||||
xtraverts += 4;
|
||||
x += in_width + 1;
|
||||
x += info.w + 1;
|
||||
}
|
||||
glEnd();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user