]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_draw.c
corrected a typo in a comment referring to the e4 hall in start as e1m4
[divverent/darkplaces.git] / gl_draw.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
12
13 See the 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, USA.
18
19 */
20
21 // draw.c -- this is the only file outside the refresh that touches the
22 // vid buffer
23
24 #include "quakedef.h"
25
26 //#define GL_COLOR_INDEX8_EXT     0x80E5
27
28 cvar_t          scr_conalpha = {CVAR_SAVE, "scr_conalpha", "1"};
29
30 byte            *draw_chars;                            // 8*8 graphic characters
31 qpic_t          *draw_disc;
32
33 rtexture_t      *char_texture;
34
35 typedef struct
36 {
37         rtexture_t      *tex;
38 } glpic_t;
39
40 rtexture_t      *conbacktex;
41
42 //=============================================================================
43 /* Support Routines */
44
45 typedef struct cachepic_s
46 {
47         char            name[MAX_QPATH];
48         qpic_t          pic;
49         byte            padding[32];    // for appended glpic
50 } cachepic_t;
51
52 #define MAX_CACHED_PICS         128
53 cachepic_t      menu_cachepics[MAX_CACHED_PICS];
54 int                     menu_numcachepics;
55
56 byte            menuplyr_pixels[4096];
57
58 int             pic_texels;
59 int             pic_count;
60
61 qpic_t *Draw_PicFromWad (char *name)
62 {
63         qpic_t  *p;
64         glpic_t *gl;
65
66         p = W_GetLumpName (name);
67         gl = (glpic_t *)p->data;
68
69         gl->tex = R_LoadTexture (name, p->width, p->height, p->data, TEXF_ALPHA | TEXF_PRECACHE);
70         return p;
71 }
72
73
74 /*
75 ================
76 Draw_CachePic
77 ================
78 */
79 qpic_t  *Draw_CachePic (char *path)
80 {
81         cachepic_t      *pic;
82         int                     i;
83         qpic_t          *dat;
84         glpic_t         *gl;
85
86         for (pic=menu_cachepics, i=0 ; i<menu_numcachepics ; pic++, i++)
87                 if (!strcmp (path, pic->name))
88                         return &pic->pic;
89
90         if (menu_numcachepics == MAX_CACHED_PICS)
91                 Sys_Error ("menu_numcachepics == MAX_CACHED_PICS");
92         menu_numcachepics++;
93         strcpy (pic->name, path);
94
95 //
96 // load the pic from disk
97 //
98         dat = (qpic_t *)COM_LoadMallocFile (path, false);
99         if (!dat)
100                 Sys_Error ("Draw_CachePic: failed to load %s", path);
101         SwapPic (dat);
102
103         // HACK HACK HACK --- we need to keep the bytes for
104         // the translatable player picture just for the menu
105         // configuration dialog
106         if (!strcmp (path, "gfx/menuplyr.lmp"))
107                 memcpy (menuplyr_pixels, dat->data, dat->width*dat->height);
108
109         pic->pic.width = dat->width;
110         pic->pic.height = dat->height;
111
112         gl = (glpic_t *)pic->pic.data;
113         gl->tex = loadtextureimage(path, 0, 0, false, false, true);
114         if (!gl->tex)
115                 gl->tex = R_LoadTexture (path, dat->width, dat->height, dat->data, TEXF_ALPHA | TEXF_PRECACHE);
116
117         qfree(dat);
118
119         return &pic->pic;
120 }
121
122 /*
123 ===============
124 Draw_Init
125 ===============
126 */
127 void gl_draw_start(void)
128 {
129         int             i;
130
131         char_texture = loadtextureimage ("conchars", 0, 0, false, false, true);
132         if (!char_texture)
133         {
134                 draw_chars = W_GetLumpName ("conchars");
135                 for (i=0 ; i<128*128 ; i++)
136                         if (draw_chars[i] == 0)
137                                 draw_chars[i] = 255;    // proper transparent color
138
139                 // now turn them into textures
140                 char_texture = R_LoadTexture ("charset", 128, 128, draw_chars, TEXF_ALPHA | TEXF_PRECACHE);
141         }
142
143         conbacktex = loadtextureimage("gfx/conback", 0, 0, false, false, true);
144
145         // get the other pics we need
146         draw_disc = Draw_PicFromWad ("disc");
147 }
148
149 void gl_draw_shutdown(void)
150 {
151 }
152
153 void gl_draw_newmap(void)
154 {
155 }
156
157 char engineversion[40];
158 int engineversionx, engineversiony;
159
160 extern void R_Textures_Init();
161 void GL_Draw_Init (void)
162 {
163         int i;
164         Cvar_RegisterVariable (&scr_conalpha);
165
166 #if defined(__linux__)
167         sprintf (engineversion, "DarkPlaces Linux   GL %.2f build %3i", (float) VERSION, buildnumber);
168 #elif defined(WIN32)
169         sprintf (engineversion, "DarkPlaces Windows GL %.2f build %3i", (float) VERSION, buildnumber);
170 #else
171         sprintf (engineversion, "DarkPlaces Unknown GL %.2f build %3i", (float) VERSION, buildnumber);
172 #endif
173         for (i = 0;i < 40 && engineversion[i];i++)
174                 engineversion[i] += 0x80; // shift to orange
175         engineversionx = vid.width - strlen(engineversion) * 8 - 8;
176         engineversiony = vid.height - 8;
177
178         R_Textures_Init();
179         R_RegisterModule("GL_Draw", gl_draw_start, gl_draw_shutdown, gl_draw_newmap);
180 }
181
182 /*
183 ================
184 Draw_Character
185
186 Draws one 8*8 graphics character with 0 being transparent.
187 It can be clipped to the top of the screen to allow the console to be
188 smoothly scrolled off.
189 ================
190 */
191 void Draw_Character (int x, int y, int num)
192 {
193         int                             row, col;
194         float                   frow, fcol, size;
195
196         if (num == 32)
197                 return;         // space
198
199         num &= 255;
200         
201         if (y <= -8)
202                 return;                 // totally off screen
203
204         row = num>>4;
205         col = num&15;
206
207         frow = row*0.0625;
208         fcol = col*0.0625;
209         size = 0.0625;
210
211         if (!r_render.value)
212                 return;
213         glBindTexture(GL_TEXTURE_2D, R_GetTexture(char_texture));
214         // LordHavoc: NEAREST mode on text if not scaling up
215         if (glwidth <= (int) vid.width)
216         {
217                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
218                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
219         }
220         else
221         {
222                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
223                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
224         }
225
226         if (lighthalf)
227                 glColor3f(0.5f,0.5f,0.5f);
228         else
229                 glColor3f(1.0f,1.0f,1.0f);
230         glBegin (GL_QUADS);
231         glTexCoord2f (fcol, frow);
232         glVertex2f (x, y);
233         glTexCoord2f (fcol + size, frow);
234         glVertex2f (x+8, y);
235         glTexCoord2f (fcol + size, frow + size);
236         glVertex2f (x+8, y+8);
237         glTexCoord2f (fcol, frow + size);
238         glVertex2f (x, y+8);
239         glEnd ();
240
241         // LordHavoc: revert to LINEAR mode
242 //      if (glwidth < (int) vid.width)
243 //      {
244 //              glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
245 //              glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
246 //      }
247 }
248
249 /*
250 ================
251 Draw_String
252 ================
253 */
254 // LordHavoc: sped this up a lot, and added maxlen
255 void Draw_String (int x, int y, char *str, int maxlen)
256 {
257         int num;
258         float frow, fcol;
259         if (!r_render.value)
260                 return;
261         if (y <= -8 || y >= (int) vid.height || x >= (int) vid.width || *str == 0) // completely offscreen or no text to print
262                 return;
263         if (maxlen < 1)
264                 maxlen = strlen(str);
265         else if (maxlen > (int) strlen(str))
266                 maxlen = strlen(str);
267         glBindTexture(GL_TEXTURE_2D, R_GetTexture(char_texture));
268
269         // LordHavoc: NEAREST mode on text if not scaling up
270         if (glwidth <= (int) vid.width)
271         {
272                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
273                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
274         }
275         else
276         {
277                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
278                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
279         }
280
281         if (lighthalf)
282                 glColor3f(0.5f,0.5f,0.5f);
283         else
284                 glColor3f(1.0f,1.0f,1.0f);
285         glBegin (GL_QUADS);
286         while (maxlen-- && x < (int) vid.width) // stop rendering when out of characters or room
287         {
288                 if ((num = *str++) != 32) // skip spaces
289                 {
290                         frow = (float) ((int) num >> 4)*0.0625;
291                         fcol = (float) ((int) num & 15)*0.0625;
292                         glTexCoord2f (fcol         , frow         );glVertex2f (x, y);
293                         glTexCoord2f (fcol + 0.0625, frow         );glVertex2f (x+8, y);
294                         glTexCoord2f (fcol + 0.0625, frow + 0.0625);glVertex2f (x+8, y+8);
295                         glTexCoord2f (fcol         , frow + 0.0625);glVertex2f (x, y+8);
296                 }
297                 x += 8;
298         }
299         glEnd ();
300
301         // LordHavoc: revert to LINEAR mode
302 //      if (glwidth < (int) vid.width)
303 //      {
304 //              glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
305 //              glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
306 //      }
307 }
308
309 void Draw_GenericPic (rtexture_t *tex, float red, float green, float blue, float alpha, int x, int y, int width, int height)
310 {
311         if (!r_render.value)
312                 return;
313         if (lighthalf)
314                 glColor4f(red * 0.5f, green * 0.5f, blue * 0.5f, alpha);
315         else
316                 glColor4f(red, green, blue, alpha);
317         glBindTexture(GL_TEXTURE_2D, R_GetTexture(tex));
318         glBegin (GL_QUADS);
319         glTexCoord2f (0, 0);glVertex2f (x, y);
320         glTexCoord2f (1, 0);glVertex2f (x+width, y);
321         glTexCoord2f (1, 1);glVertex2f (x+width, y+height);
322         glTexCoord2f (0, 1);glVertex2f (x, y+height);
323         glEnd ();
324 }
325
326 /*
327 =============
328 Draw_AlphaPic
329 =============
330 */
331 void Draw_AlphaPic (int x, int y, qpic_t *pic, float alpha)
332 {
333         if (pic)
334                 Draw_GenericPic(((glpic_t *)pic->data)->tex, 1,1,1,alpha, x,y,pic->width, pic->height);
335 }
336
337
338 /*
339 =============
340 Draw_Pic
341 =============
342 */
343 void Draw_Pic (int x, int y, qpic_t *pic)
344 {
345         if (pic)
346                 Draw_GenericPic(((glpic_t *)pic->data)->tex, 1,1,1,1, x,y,pic->width, pic->height);
347 }
348
349
350 void Draw_AdditivePic (int x, int y, qpic_t *pic)
351 {
352         if (pic)
353         {
354                 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
355                 Draw_GenericPic(((glpic_t *)pic->data)->tex, 1,1,1,1, x,y,pic->width, pic->height);
356                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
357         }
358 }
359
360
361 /*
362 =============
363 Draw_PicTranslate
364
365 Only used for the player color selection menu
366 =============
367 */
368 void Draw_PicTranslate (int x, int y, qpic_t *pic, byte *translation)
369 {
370         int                             i, c;
371         byte                    *trans, *src, *dest;
372         rtexture_t              *rt;
373
374         if (pic == NULL)
375                 return;
376
377         c = pic->width * pic->height;
378         src = menuplyr_pixels;
379         dest = trans = qmalloc(c);
380         for (i = 0;i < c;i++)
381                 *dest++ = translation[*src++];
382
383         rt = R_LoadTexture ("translatedplayerpic", pic->width, pic->height, trans, TEXF_ALPHA | TEXF_PRECACHE);
384         qfree(trans);
385
386         if (!r_render.value)
387                 return;
388         Draw_GenericPic (rt, 1,1,1,1, x, y, pic->width, pic->height);
389 }
390
391
392 /*
393 ================
394 Draw_ConsoleBackground
395
396 ================
397 */
398 void Draw_ConsoleBackground (int lines)
399 {
400         Draw_GenericPic (conbacktex, 1,1,1,scr_conalpha.value*lines/vid.height, 0, lines - vid.height, vid.width, vid.height);
401         // LordHavoc: draw version
402         Draw_String(engineversionx, lines - vid.height + engineversiony, engineversion, 9999);
403 }
404
405 /*
406 =============
407 Draw_Fill
408
409 Fills a box of pixels with a single color
410 =============
411 */
412 void Draw_Fill (int x, int y, int w, int h, int c)
413 {
414         if (!r_render.value)
415                 return;
416         glDisable (GL_TEXTURE_2D);
417         if (lighthalf)
418         {
419                 byte *tempcolor = (byte *)&d_8to24table[c];
420                 glColor4ub ((byte) (tempcolor[0] >> 1), (byte) (tempcolor[1] >> 1), (byte) (tempcolor[2] >> 1), tempcolor[3]);
421         }
422         else
423                 glColor4ubv ((byte *)&d_8to24table[c]);
424
425         glBegin (GL_QUADS);
426
427         glVertex2f (x,y);
428         glVertex2f (x+w, y);
429         glVertex2f (x+w, y+h);
430         glVertex2f (x, y+h);
431
432         glEnd ();
433         glColor3f(1,1,1);
434         glEnable (GL_TEXTURE_2D);
435 }
436 //=============================================================================
437
438 //=============================================================================
439
440 /*
441 ================
442 GL_Set2D
443
444 Setup as if the screen was 320*200
445 ================
446 */
447 void GL_Set2D (void)
448 {
449         if (!r_render.value)
450                 return;
451         glViewport (glx, gly, glwidth, glheight);
452
453         glMatrixMode(GL_PROJECTION);
454     glLoadIdentity ();
455         glOrtho  (0, vid.width, vid.height, 0, -99999, 99999);
456
457         glMatrixMode(GL_MODELVIEW);
458     glLoadIdentity ();
459
460         glDisable (GL_DEPTH_TEST);
461         glDisable (GL_CULL_FACE);
462         glEnable (GL_BLEND);
463         glDisable (GL_ALPHA_TEST);
464         glEnable(GL_TEXTURE_2D);
465
466         // LordHavoc: added this
467         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
468         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
469
470         glColor3f(1,1,1);
471 }
472
473 // LordHavoc: SHOWLMP stuff
474 #define SHOWLMP_MAXLABELS 256
475 typedef struct showlmp_s
476 {
477         qboolean        isactive;
478         float           x;
479         float           y;
480         char            label[32];
481         char            pic[128];
482 } showlmp_t;
483
484 showlmp_t showlmp[SHOWLMP_MAXLABELS];
485
486 void SHOWLMP_decodehide(void)
487 {
488         int i;
489         byte *lmplabel;
490         lmplabel = MSG_ReadString();
491         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
492                 if (showlmp[i].isactive && strcmp(showlmp[i].label, lmplabel) == 0)
493                 {
494                         showlmp[i].isactive = false;
495                         return;
496                 }
497 }
498
499 void SHOWLMP_decodeshow(void)
500 {
501         int i, k;
502         byte lmplabel[256], picname[256];
503         float x, y;
504         strcpy(lmplabel,MSG_ReadString());
505         strcpy(picname, MSG_ReadString());
506         if (nehahra) // LordHavoc: nasty old legacy junk
507         {
508                 x = MSG_ReadByte();
509                 y = MSG_ReadByte();
510         }
511         else
512         {
513                 x = MSG_ReadShort();
514                 y = MSG_ReadShort();
515         }
516         k = -1;
517         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
518                 if (showlmp[i].isactive)
519                 {
520                         if (strcmp(showlmp[i].label, lmplabel) == 0)
521                         {
522                                 k = i;
523                                 break; // drop out to replace it
524                         }
525                 }
526                 else if (k < 0) // find first empty one to replace
527                         k = i;
528         if (k < 0)
529                 return; // none found to replace
530         // change existing one
531         showlmp[k].isactive = true;
532         strcpy(showlmp[k].label, lmplabel);
533         strcpy(showlmp[k].pic, picname);
534         showlmp[k].x = x;
535         showlmp[k].y = y;
536 }
537
538 void SHOWLMP_drawall(void)
539 {
540         int i;
541         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
542                 if (showlmp[i].isactive)
543                         Draw_Pic(showlmp[i].x, showlmp[i].y, Draw_CachePic(showlmp[i].pic));
544 }
545
546 void SHOWLMP_clear(void)
547 {
548         int i;
549         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
550                 showlmp[i].isactive = false;
551 }