~ubuntu-branches/ubuntu/hardy/sauerbraten/hardy-backports

« back to all changes in this revision

Viewing changes to engine/rendertext.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bruno "Fuddl" Kleinert
  • Date: 2007-01-09 18:19:30 UTC
  • Revision ID: james.westby@ubuntu.com-20070109181930-zy2ulzq3ukfjhrix
Tags: upstream-0.0.20061204.dfsg
ImportĀ upstreamĀ versionĀ 0.0.20061204.dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// rendertext.cpp: based on Don's gl_text.cpp
 
2
 
 
3
#include "pch.h"
 
4
#include "engine.h"
 
5
 
 
6
short char_coords[94][3] = 
 
7
{
 
8
    { 7  , 0  , 12 },  //!
 
9
    { 26 , 0  , 19 },  //"
 
10
    { 52 , 0  , 35 },  //#
 
11
    { 94 , 0  , 27 },  //$
 
12
    { 128, 0  , 50 },  //%
 
13
    { 185, 0  , 38 },  //&
 
14
    { 230, 0  , 9  },  //'
 
15
    { 246, 0  , 19 },  //(
 
16
    { 272, 0  , 18 },  //)
 
17
    { 297, 0  , 27 },  //*
 
18
    { 331, 0  , 35 },  //+
 
19
    { 373, 0  , 13 },  //,
 
20
    { 393, 0  , 17 },  //-
 
21
    { 417, 0  , 11 },  //.
 
22
    { 435, 0  , 20 },  ///
 
23
    { 462, 0  , 27 },  //0
 
24
    { 0  , 71 , 25 },  //1
 
25
    { 32 , 71 , 27 },  //2
 
26
    { 173, 71 , 26 },  //3
 
27
    { 206, 71 , 28 },  //4
 
28
    { 241, 71 , 27 },  //5
 
29
    { 275, 71 , 28 },  //6
 
30
    { 310, 71 , 28 },  //7
 
31
    { 345, 71 , 27 },  //8
 
32
    { 379, 71 , 27 },  //9
 
33
    { 413, 71 , 13 },  //:
 
34
    { 433, 71 , 15 },  //;
 
35
    { 455, 71 , 33 },  //<
 
36
    { 0  , 142, 34 },  //=
 
37
    { 148, 142, 34 },  //>
 
38
    { 189, 142, 24 },  //?
 
39
    { 220, 142, 46 },  //@
 
40
    { 273, 142, 33 },  //A
 
41
    { 313, 142, 30 },  //B
 
42
    { 350, 142, 31 },  //C
 
43
    { 388, 142, 35 },  //D
 
44
    { 430, 142, 28 },  //E
 
45
    { 465, 142, 27 },  //F
 
46
    { 0  , 213, 33 },  //G
 
47
    { 40 , 213, 32 },  //H
 
48
    { 79 , 213, 18 },  //I
 
49
    { 104, 213, 19 },  //J
 
50
    { 130, 213, 32 },  //K
 
51
    { 169, 213, 27 },  //L
 
52
    { 310, 213, 37 },  //M
 
53
    { 354, 213, 32 },  //N
 
54
    { 393, 213, 36 },  //O
 
55
    { 436, 213, 29 },  //P
 
56
    { 472, 213, 36 },  //Q
 
57
    { 0  , 284, 34 },  //R
 
58
    { 41 , 284, 29 },  //S
 
59
    { 77 , 284, 32 },  //T
 
60
    { 116, 284, 32 },  //U
 
61
    { 155, 284, 33 },  //V
 
62
    { 195, 284, 48 },  //W
 
63
    { 250, 284, 31 },  //X
 
64
    { 288, 284, 32 },  //Y
 
65
    { 327, 284, 29 },  //Z
 
66
    { 363, 284, 18 },  //[
 
67
    { 388, 284, 21 },  //"\"
 
68
    { 416, 284, 16 },  //]
 
69
    { 439, 284, 36 },  //^
 
70
    { 0  , 355, 31 },  //_
 
71
    { 203, 213, 18 },  //`      //TODO
 
72
    { 38 , 355, 25 },  //a
 
73
    { 70 , 355, 28 },  //b
 
74
    { 105, 355, 24 },  //c
 
75
    { 136, 355, 26 },  //d
 
76
    { 169, 355, 27 },  //e
 
77
    { 203, 355, 19 },  //f
 
78
    { 229, 355, 26 },  //g
 
79
    { 262, 355, 27 },  //h
 
80
    { 296, 355, 9  },  //i
 
81
    { 312, 355, 15 },  //j
 
82
    { 334, 355, 28 },  //k
 
83
    { 369, 355, 9  },  //l
 
84
    { 385, 355, 42 },  //m
 
85
    { 434, 355, 27 },  //n
 
86
    { 468, 355, 28 },  //o
 
87
    { 0  , 426, 28 },  //p
 
88
    { 35 , 426, 26 },  //q
 
89
    { 68 , 426, 19 },  //r
 
90
    { 94 , 426, 23 },  //s
 
91
    { 124, 426, 18 },  //t
 
92
    { 149, 426, 26 },  //u
 
93
    { 182, 426, 27 },  //v
 
94
    { 216, 426, 39 },  //w
 
95
    { 262, 426, 26 },  //x
 
96
    { 295, 426, 27 },  //y
 
97
    { 329, 426, 23 },  //z
 
98
    { 359, 426, 23 },  //{
 
99
    { 496, 426, 13 },  //|
 
100
    { 389, 426, 23 },  //} // coords
 
101
    { 419, 426, 34 },  //~  // coords
 
102
};                     
 
103
 
 
104
void gettextres(int &w, int &h)
 
105
{
 
106
    if(w < MINRESW || h < MINRESH)
 
107
    {
 
108
        if(MINRESW > w*MINRESH/h)
 
109
        {
 
110
            h = h*MINRESW/w;
 
111
            w = MINRESW;
 
112
        }
 
113
        else
 
114
        {
 
115
            w = w*MINRESH/h;
 
116
            h = MINRESH;
 
117
        };
 
118
    };
 
119
};
 
120
 
 
121
#define PIXELTAB (FONTH*4)
 
122
 
 
123
int char_width(int c, int x)
 
124
{
 
125
    if(c=='\t') x = (x+PIXELTAB)/PIXELTAB*PIXELTAB;
 
126
    else if(c==' ') x += FONTH/2;
 
127
    else if(c>=33 && c<=126)
 
128
    {
 
129
        c -= 33;
 
130
        int in_width = char_coords[c][2];
 
131
        x += in_width + 1;
 
132
    };
 
133
    return x;
 
134
};
 
135
 
 
136
int text_width(const char *str, int limit)
 
137
{
 
138
    int x = 0;
 
139
    for(int i = 0; str[i] && (limit<0 ||i<limit); i++) 
 
140
    {
 
141
        if(str[i]=='\f')
 
142
        {
 
143
            i++;
 
144
            continue;
 
145
        };
 
146
        x = char_width(str[i], x);
 
147
    };
 
148
    return x;
 
149
}
 
150
 
 
151
int text_visible(const char *str, int max)
 
152
{
 
153
    int i = 0, x = 0;
 
154
    while(str[i])
 
155
    {
 
156
        if(str[i]=='\f')
 
157
        {
 
158
            i += 2;
 
159
            continue;
 
160
        };
 
161
        x = char_width(str[i], x);
 
162
        if(x > max) return i;
 
163
        ++i;
 
164
    };
 
165
    return i;
 
166
};
 
167
 
 
168
void draw_textf(const char *fstr, int left, int top, ...)
 
169
{
 
170
    s_sprintfdlv(str, top, fstr);
 
171
    draw_text(str, left, top);
 
172
};
 
173
 
 
174
void draw_text(const char *str, int left, int top, int r, int g, int b, int a)
 
175
{
 
176
    static Texture *charstex = NULL;
 
177
    if(!charstex) charstex = textureload("data/newerchars.png");
 
178
 
 
179
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
180
    glBindTexture(GL_TEXTURE_2D, charstex->gl);
 
181
    glColor4ub(r, g, b, a);
 
182
    //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 
183
 
 
184
    int x = left;
 
185
    int y = top;
 
186
 
 
187
    int i;
 
188
    float in_left, in_top, in_right, in_bottom;
 
189
    int in_width, in_height;
 
190
 
 
191
    glBegin(GL_QUADS);
 
192
    for (i = 0; str[i] != 0; i++)
 
193
    {
 
194
        int c = str[i];
 
195
        if(c=='\t') { x = (x-left+PIXELTAB)/PIXELTAB*PIXELTAB+left; continue; }; 
 
196
        if(c=='\f') switch(str[i+1])
 
197
        {
 
198
            case '0': glColor3ub(64,255,128); i++; continue;    // green: player talk
 
199
            case '1': glColor3ub(96,160,255); i++; continue;    // blue: "echo" command
 
200
            case '2': glColor3ub(255,192,64); i++; continue;    // yellow: gameplay messages 
 
201
            case '3': glColor3ub(255,64,64);  i++; continue;    // red: important errors
 
202
            default: continue;                                  // white: everything else
 
203
        };
 
204
        if(c==' ') { x += FONTH/2; continue; };
 
205
        c -= 33;
 
206
        if(c<0 || c>=95) continue;
 
207
        
 
208
        in_width   = char_coords[c][2];
 
209
        in_height  = 64;
 
210
        in_left    = ((float) char_coords[c][0]+2-2)   / 512.0f;
 
211
        in_top     = ((float) char_coords[c][1]+2-3) / 512.0f;
 
212
        in_right   = ((float) char_coords[c][0]+2+3+in_width)   / 512.0f;
 
213
        in_bottom  = ((float) char_coords[c][1]+2+4+in_height) / 512.0f;
 
214
 
 
215
 
 
216
        glTexCoord2f(in_left,  in_top   ); glVertex2i(x,            y);
 
217
        glTexCoord2f(in_right, in_top   ); glVertex2i(x + in_width, y);
 
218
        glTexCoord2f(in_right, in_bottom); glVertex2i(x + in_width, y + in_height);
 
219
        glTexCoord2f(in_left,  in_bottom); glVertex2i(x,            y + in_height);
 
220
        
 
221
        xtraverts += 4;
 
222
        x += in_width  + 1;
 
223
    };
 
224
    glEnd();
 
225
};
 
226
 
 
227
Texture *sky[6] = { 0, 0, 0, 0, 0, 0 };
 
228
 
 
229
void loadsky(char *basename)
 
230
{
 
231
    static string lastsky = "";
 
232
    if(strcmp(lastsky, basename)==0) return;
 
233
    static char *side[] = { "ft", "bk", "lf", "rt", "dn", "up" };
 
234
    loopi(6)
 
235
    {
 
236
        s_sprintfd(name)("packages/%s_%s.jpg", basename, side[i]);
 
237
        if((sky[i] = textureload(name, true))==crosshair) conoutf("could not load sky textures");
 
238
        // FIXME? now doesn't overwrite old sky any more which uses more memory, but gives faster loadtimes...
 
239
    };
 
240
    s_strcpy(lastsky, basename);
 
241
};
 
242
 
 
243
COMMAND(loadsky, "s");
 
244
 
 
245
void draw_envbox_face(float s0, float t0, int x0, int y0, int z0,
 
246
                      float s1, float t1, int x1, int y1, int z1,
 
247
                      float s2, float t2, int x2, int y2, int z2,
 
248
                      float s3, float t3, int x3, int y3, int z3,
 
249
                      GLuint texture)
 
250
{
 
251
    glBindTexture(GL_TEXTURE_2D, texture);
 
252
    glBegin(GL_QUADS);
 
253
    glTexCoord2f(s3, t3); glVertex3i(x3, y3, z3);
 
254
    glTexCoord2f(s2, t2); glVertex3i(x2, y2, z2);
 
255
    glTexCoord2f(s1, t1); glVertex3i(x1, y1, z1);
 
256
    glTexCoord2f(s0, t0); glVertex3i(x0, y0, z0);
 
257
    glEnd();
 
258
    xtraverts += 4;
 
259
};
 
260
 
 
261
void draw_envbox(int w, float zclip)
 
262
{
 
263
    if(!sky[0]) fatal("no skybox");
 
264
 
 
265
    float vclip = 1-zclip;
 
266
    int z = int(ceil(2*w*(vclip-0.5f)));
 
267
 
 
268
    glDepthMask(GL_FALSE);
 
269
 
 
270
    draw_envbox_face(1.0f, vclip, -w, -w,  z,
 
271
                     0.0f, vclip,  w, -w,  z,
 
272
                     0.0f, 0.0f,  w, -w, -w,
 
273
                     1.0f, 0.0f, -w, -w, -w, sky[0]->gl);
 
274
 
 
275
    draw_envbox_face(1.0f, vclip, +w,  w,  z,
 
276
                     0.0f, vclip, -w,  w,  z,
 
277
                     0.0f, 0.0f, -w,  w, -w,
 
278
                     1.0f, 0.0f, +w,  w, -w, sky[1]->gl);
 
279
 
 
280
    draw_envbox_face(0.0f, 0.0f, -w, -w, -w,
 
281
                     1.0f, 0.0f, -w,  w, -w,
 
282
                     1.0f, vclip, -w,  w,  z,
 
283
                     0.0f, vclip, -w, -w,  z, sky[2]->gl);
 
284
 
 
285
    draw_envbox_face(1.0f, vclip, +w, -w,  z,
 
286
                     0.0f, vclip, +w,  w,  z,
 
287
                     0.0f, 0.0f, +w,  w, -w,
 
288
                     1.0f, 0.0f, +w, -w, -w, sky[3]->gl);
 
289
 
 
290
    if(!zclip)
 
291
        draw_envbox_face(0.0f, 1.0f, -w,  w,  w,
 
292
                         0.0f, 0.0f, +w,  w,  w,
 
293
                         1.0f, 0.0f, +w, -w,  w,
 
294
                         1.0f, 1.0f, -w, -w,  w, sky[4]->gl);
 
295
 
 
296
    draw_envbox_face(0.0f, 1.0f, +w,  w, -w,
 
297
                     0.0f, 0.0f, -w,  w, -w,
 
298
                     1.0f, 0.0f, -w, -w, -w,
 
299
                     1.0f, 1.0f, +w, -w, -w, sky[5]->gl);
 
300
 
 
301
    glDepthMask(GL_TRUE);
 
302
};