obliterated Draw_TransPic and relatives, also fixed Draw_TransPicTranslate, and resto...
[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 extern unsigned char d_15to8table[65536];
29
30 cvar_t          qsg_version = {"qsg_version", "1"};
31 cvar_t          scr_conalpha = {"scr_conalpha", "1"};
32
33 byte            *draw_chars;                            // 8*8 graphic characters
34 qpic_t          *draw_disc;
35
36 //int                   translate_texture;
37 int                     char_texture;
38
39 typedef struct
40 {
41         int             texnum;
42         float   sl, tl, sh, th;
43 } glpic_t;
44
45 byte            conback_buffer[sizeof(qpic_t) + sizeof(glpic_t)];
46 qpic_t          *conback = (qpic_t *)&conback_buffer;
47
48 /*
49 =============================================================================
50
51   scrap allocation
52
53   Allocate all the little status bar obejcts into a single texture
54   to crutch up stupid hardware / drivers
55
56 =============================================================================
57 */
58
59 #define MAX_SCRAPS              2
60 #define BLOCK_WIDTH             256
61 #define BLOCK_HEIGHT    256
62
63 int                     scrap_allocated[MAX_SCRAPS][BLOCK_WIDTH];
64 byte            scrap_texels[MAX_SCRAPS][BLOCK_WIDTH*BLOCK_HEIGHT*4];
65 qboolean        scrap_dirty;
66
67 // returns a texture number and the position inside it
68 int Scrap_AllocBlock (int w, int h, int *x, int *y)
69 {
70         int             i, j;
71         int             best, best2;
72         int             texnum;
73
74         for (texnum=0 ; texnum<MAX_SCRAPS ; texnum++)
75         {
76                 best = BLOCK_HEIGHT;
77
78                 for (i=0 ; i<BLOCK_WIDTH-w ; i++)
79                 {
80                         best2 = 0;
81
82                         for (j=0 ; j<w ; j++)
83                         {
84                                 if (scrap_allocated[texnum][i+j] >= best)
85                                         break;
86                                 if (scrap_allocated[texnum][i+j] > best2)
87                                         best2 = scrap_allocated[texnum][i+j];
88                         }
89                         if (j == w)
90                         {       // this is a valid spot
91                                 *x = i;
92                                 *y = best = best2;
93                         }
94                 }
95
96                 if (best + h > BLOCK_HEIGHT)
97                         continue;
98
99                 for (i=0 ; i<w ; i++)
100                         scrap_allocated[texnum][*x + i] = best + h;
101
102                 return texnum;
103         }
104
105         Sys_Error ("Scrap_AllocBlock: full");
106         return 0;
107 }
108
109 int     scrap_uploads;
110 int scraptexnum[MAX_SCRAPS];
111
112 void Scrap_Upload (void)
113 {
114         int             texnum;
115
116         scrap_uploads++;
117
118         for (texnum=0 ; texnum<MAX_SCRAPS ; texnum++)
119                 scraptexnum[texnum] = GL_LoadTexture (va("scrapslot%d", texnum), BLOCK_WIDTH, BLOCK_HEIGHT, scrap_texels[texnum], false, true, 1);
120         scrap_dirty = false;
121 }
122
123 //=============================================================================
124 /* Support Routines */
125
126 typedef struct cachepic_s
127 {
128         char            name[MAX_QPATH];
129         qpic_t          pic;
130         byte            padding[32];    // for appended glpic
131 } cachepic_t;
132
133 #define MAX_CACHED_PICS         128
134 cachepic_t      menu_cachepics[MAX_CACHED_PICS];
135 int                     menu_numcachepics;
136
137 byte            menuplyr_pixels[4096];
138
139 int             pic_texels;
140 int             pic_count;
141
142 int GL_LoadPicTexture (qpic_t *pic);
143
144 qpic_t *Draw_PicFromWad (char *name)
145 {
146         qpic_t  *p;
147         glpic_t *gl;
148
149         p = W_GetLumpName (name);
150         gl = (glpic_t *)p->data;
151
152         // load little ones into the scrap
153         if (p->width < 64 && p->height < 64)
154         {
155                 int             x, y;
156                 int             i, j, k;
157                 int             texnum;
158
159                 texnum = Scrap_AllocBlock (p->width, p->height, &x, &y);
160                 scrap_dirty = true;
161                 k = 0;
162                 for (i=0 ; i<p->height ; i++)
163                         for (j=0 ; j<p->width ; j++, k++)
164                                 scrap_texels[texnum][(y+i)*BLOCK_WIDTH + x + j] = p->data[k];
165                 if (!scraptexnum[texnum])
166                         scraptexnum[texnum] = GL_LoadTexture (va("scrapslot%d", texnum), BLOCK_WIDTH, BLOCK_HEIGHT, scrap_texels[texnum], false, true, 1);
167                 gl->texnum = scraptexnum[texnum];
168                 gl->sl = (x+0.01)/(float)BLOCK_WIDTH;
169                 gl->sh = (x+p->width-0.01)/(float)BLOCK_WIDTH;
170                 gl->tl = (y+0.01)/(float)BLOCK_WIDTH;
171                 gl->th = (y+p->height-0.01)/(float)BLOCK_WIDTH;
172
173                 pic_count++;
174                 pic_texels += p->width*p->height;
175         }
176         else
177         {
178                 gl->texnum = GL_LoadPicTexture (p);
179                 gl->sl = 0;
180                 gl->sh = 1;
181                 gl->tl = 0;
182                 gl->th = 1;
183         }
184         return p;
185 }
186
187
188 /*
189 ================
190 Draw_CachePic
191 ================
192 */
193 qpic_t  *Draw_CachePic (char *path)
194 {
195         cachepic_t      *pic;
196         int                     i;
197         qpic_t          *dat;
198         glpic_t         *gl;
199
200         for (pic=menu_cachepics, i=0 ; i<menu_numcachepics ; pic++, i++)
201                 if (!strcmp (path, pic->name))
202                         return &pic->pic;
203
204         if (menu_numcachepics == MAX_CACHED_PICS)
205                 Sys_Error ("menu_numcachepics == MAX_CACHED_PICS");
206         menu_numcachepics++;
207         strcpy (pic->name, path);
208
209 //
210 // load the pic from disk
211 //
212         dat = (qpic_t *)COM_LoadTempFile (path, false);
213         if (!dat)
214                 Sys_Error ("Draw_CachePic: failed to load %s", path);
215         SwapPic (dat);
216
217         // HACK HACK HACK --- we need to keep the bytes for
218         // the translatable player picture just for the menu
219         // configuration dialog
220         if (!strcmp (path, "gfx/menuplyr.lmp"))
221                 memcpy (menuplyr_pixels, dat->data, dat->width*dat->height);
222
223         pic->pic.width = dat->width;
224         pic->pic.height = dat->height;
225
226         gl = (glpic_t *)pic->pic.data;
227         gl->texnum = GL_LoadPicTexture (dat);
228         gl->sl = 0;
229         gl->sh = 1;
230         gl->tl = 0;
231         gl->th = 1;
232
233         return &pic->pic;
234 }
235
236 /*
237 void Draw_CharToConback (int num, byte *dest)
238 {
239         int             row, col;
240         byte    *source;
241         int             drawline;
242         int             x;
243
244         row = num>>4;
245         col = num&15;
246         source = draw_chars + (row<<10) + (col<<3);
247
248         drawline = 8;
249
250         while (drawline--)
251         {
252                 for (x=0 ; x<8 ; x++)
253                         if (source[x] != 255)
254                                 dest[x] = 0x60 + source[x];
255                 source += 128;
256                 dest += 320;
257         }
258
259 }
260 */
261
262 extern void LoadSky_f(void);
263
264 extern char *QSG_EXTENSIONS;
265
266 /*
267 ===============
268 Draw_Init
269 ===============
270 */
271 void rmain_registercvars();
272 extern int buildnumber;
273
274 void gl_draw_start()
275 {
276         int             i;
277         glpic_t *gl;
278
279         // load the console background and the charset
280         // by hand, because we need to write the version
281         // string into the background before turning
282         // it into a texture
283         char_texture = loadtextureimage ("conchars", 0, 0, false, false);
284         if (!char_texture)
285         {
286                 draw_chars = W_GetLumpName ("conchars");
287                 for (i=0 ; i<256*64 ; i++)
288                         if (draw_chars[i] == 0)
289                                 draw_chars[i] = 255;    // proper transparent color
290
291                 // now turn them into textures
292                 char_texture = GL_LoadTexture ("charset", 128, 128, draw_chars, false, true, 1);
293         }
294
295         gl = (glpic_t *)conback->data;
296         gl->texnum = loadtextureimage("gfx/conback", 0, 0, false, false);
297         gl->sl = 0;
298         gl->sh = 1;
299         gl->tl = 0;
300         gl->th = 1;
301         conback->width = vid.width;
302         conback->height = vid.height;
303
304         memset(scraptexnum, 0, sizeof(scraptexnum));
305
306         // get the other pics we need
307         draw_disc = Draw_PicFromWad ("disc");
308 }
309
310 void gl_draw_shutdown()
311 {
312 }
313
314 char engineversion[40];
315 int engineversionx, engineversiony;
316
317 extern void GL_Textures_Init();
318 void GL_Draw_Init (void)
319 {
320         int i;
321         Cvar_RegisterVariable (&qsg_version);
322         Cvar_RegisterVariable (&scr_conalpha);
323
324         Cmd_AddCommand ("loadsky", &LoadSky_f);
325
326 #ifdef NEHAHRA
327 #if defined(__linux__)
328         sprintf (engineversion, "DPNehahra Linux   GL %.2f build %3i", (float) VERSION, buildnumber);
329 #elif defined(WIN32)
330         sprintf (engineversion, "DPNehahra Windows GL %.2f build %3i", (float) VERSION, buildnumber);
331 #else
332         sprintf (engineversion, "DPNehahra Unknown GL %.2f build %3i", (float) VERSION, buildnumber);
333 #endif
334 #else
335 #if defined(__linux__)
336         sprintf (engineversion, "DarkPlaces Linux   GL %.2f build %3i", (float) VERSION, buildnumber);
337 #elif defined(WIN32)
338         sprintf (engineversion, "DarkPlaces Windows GL %.2f build %3i", (float) VERSION, buildnumber);
339 #else
340         sprintf (engineversion, "DarkPlaces Unknown GL %.2f build %3i", (float) VERSION, buildnumber);
341 #endif
342 #endif
343         for (i = 0;i < 40 && engineversion[i];i++)
344                 engineversion[i] += 0x80; // shift to orange
345         engineversionx = vid.width - strlen(engineversion) * 8 - 8;
346         engineversiony = vid.height - 8;
347
348         GL_Textures_Init();
349         R_RegisterModule("GL_Draw", gl_draw_start, gl_draw_shutdown);
350 }
351
352 /*
353 ================
354 Draw_Character
355
356 Draws one 8*8 graphics character with 0 being transparent.
357 It can be clipped to the top of the screen to allow the console to be
358 smoothly scrolled off.
359 ================
360 */
361 void Draw_Character (int x, int y, int num)
362 {
363         int                             row, col;
364         float                   frow, fcol, size;
365
366         if (num == 32)
367                 return;         // space
368
369         num &= 255;
370         
371         if (y <= -8)
372                 return;                 // totally off screen
373
374         row = num>>4;
375         col = num&15;
376
377         frow = row*0.0625;
378         fcol = col*0.0625;
379         size = 0.0625;
380
381         glBindTexture(GL_TEXTURE_2D, char_texture);
382         // LordHavoc: NEAREST mode on text if not scaling up
383         if ((int) vid.width < glwidth)
384         {
385                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
386                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
387         }
388
389         glColor3f(1,1,1);
390         glBegin (GL_QUADS);
391         glTexCoord2f (fcol, frow);
392         glVertex2f (x, y);
393         glTexCoord2f (fcol + size, frow);
394         glVertex2f (x+8, y);
395         glTexCoord2f (fcol + size, frow + size);
396         glVertex2f (x+8, y+8);
397         glTexCoord2f (fcol, frow + size);
398         glVertex2f (x, y+8);
399         glEnd ();
400
401         // LordHavoc: revert to LINEAR mode
402         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
403         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
404 }
405
406 /*
407 ================
408 Draw_String
409 ================
410 */
411 // LordHavoc: sped this up a lot, and added maxlen
412 void Draw_String (int x, int y, char *str, int maxlen)
413 {
414         int num;
415         float frow, fcol;
416         if (y <= -8 || y >= (int) vid.height || x >= (int) vid.width || *str == 0) // completely offscreen or no text to print
417                 return;
418         if (maxlen < 1)
419                 maxlen = strlen(str);
420         else if (maxlen > (int) strlen(str))
421                 maxlen = strlen(str);
422         glBindTexture(GL_TEXTURE_2D, char_texture);
423
424         // LordHavoc: NEAREST mode on text if not scaling up
425         if ((int) vid.width < glwidth)
426         {
427                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
428                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
429         }
430
431         glColor3f(1,1,1);
432         glBegin (GL_QUADS);
433         while (maxlen-- && x < (int) vid.width) // stop rendering when out of characters or room
434         {
435                 if ((num = *str++) != 32) // skip spaces
436                 {
437                         frow = (float) ((int) num >> 4)*0.0625;
438                         fcol = (float) ((int) num & 15)*0.0625;
439                         glTexCoord2f (fcol, frow);
440                         glVertex2f (x, y);
441                         glTexCoord2f (fcol + 0.0625, frow);
442                         glVertex2f (x+8, y);
443                         glTexCoord2f (fcol + 0.0625, frow + 0.0625);
444                         glVertex2f (x+8, y+8);
445                         glTexCoord2f (fcol, frow + 0.0625);
446                         glVertex2f (x, y+8);
447                 }
448                 x += 8;
449         }
450         glEnd ();
451
452         // LordHavoc: revert to LINEAR mode
453         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
454         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
455 }
456
457 void Draw_GenericPic (int texnum, float red, float green, float blue, float alpha, float x, float y, float width, float height)
458 {
459         glDisable(GL_ALPHA_TEST);
460 //      glEnable (GL_BLEND);
461         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
462 //      glCullFace(GL_FRONT);
463         glColor4f(red,green,blue,alpha);
464         glBindTexture(GL_TEXTURE_2D, texnum);
465         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
466         glBegin (GL_QUADS);
467         glTexCoord2f (0, 0);
468         glVertex2f (x, y);
469         glTexCoord2f (1, 0);
470         glVertex2f (x+width, y);
471         glTexCoord2f (1, 1);
472         glVertex2f (x+width, y+height);
473         glTexCoord2f (0, 1);
474         glVertex2f (x, y+height);
475         glEnd ();
476         glColor3f(1,1,1);
477 //      glEnable(GL_ALPHA_TEST);
478 //      glDisable (GL_BLEND);
479 }
480
481 /*
482 =============
483 Draw_AlphaPic
484 =============
485 */
486 void Draw_AlphaPic (int x, int y, qpic_t *pic, float alpha)
487 {
488         glpic_t                 *gl;
489
490         if (scrap_dirty)
491                 Scrap_Upload ();
492         gl = (glpic_t *)pic->data;
493         glColor4f(1,1,1,alpha);
494         glBindTexture(GL_TEXTURE_2D, gl->texnum);
495         glBegin (GL_QUADS);
496         glTexCoord2f (gl->sl, gl->tl);
497         glVertex2f (x, y);
498         glTexCoord2f (gl->sh, gl->tl);
499         glVertex2f (x+pic->width, y);
500         glTexCoord2f (gl->sh, gl->th);
501         glVertex2f (x+pic->width, y+pic->height);
502         glTexCoord2f (gl->sl, gl->th);
503         glVertex2f (x, y+pic->height);
504         glEnd ();
505 }
506
507
508 /*
509 =============
510 Draw_Pic
511 =============
512 */
513 void Draw_Pic (int x, int y, qpic_t *pic)
514 {
515         glpic_t                 *gl;
516
517         if (scrap_dirty)
518                 Scrap_Upload ();
519         gl = (glpic_t *)pic->data;
520         glColor3f(1,1,1);
521         glBindTexture(GL_TEXTURE_2D, gl->texnum);
522         glBegin (GL_QUADS);
523         glTexCoord2f (gl->sl, gl->tl);
524         glVertex2f (x, y);
525         glTexCoord2f (gl->sh, gl->tl);
526         glVertex2f (x+pic->width, y);
527         glTexCoord2f (gl->sh, gl->th);
528         glVertex2f (x+pic->width, y+pic->height);
529         glTexCoord2f (gl->sl, gl->th);
530         glVertex2f (x, y+pic->height);
531         glEnd ();
532 }
533
534
535 /*
536 =============
537 Draw_PicTranslate
538
539 Only used for the player color selection menu
540 =============
541 */
542 void Draw_PicTranslate (int x, int y, qpic_t *pic, byte *translation)
543 {
544         int                             i, c;
545         byte                    *trans, *src, *dest;
546
547         c = pic->width * pic->height;
548         src = menuplyr_pixels;
549         dest = trans = malloc(c);
550         for (i = 0;i < c;i++)
551                 *dest++ = translation[*src++];
552
553         glBindTexture(GL_TEXTURE_2D, GL_LoadTexture ("translatedplayerpic", pic->width, pic->height, trans, false, true, 1));
554         free(trans);
555
556         glColor3f(1,1,1);
557         glBegin (GL_QUADS);
558         glTexCoord2f (0, 0);
559         glVertex2f (x, y);
560         glTexCoord2f (1, 0);
561         glVertex2f (x+pic->width, y);
562         glTexCoord2f (1, 1);
563         glVertex2f (x+pic->width, y+pic->height);
564         glTexCoord2f (0, 1);
565         glVertex2f (x, y+pic->height);
566         glEnd ();
567 }
568
569
570 /*
571 ================
572 Draw_ConsoleBackground
573
574 ================
575 */
576 void Draw_ConsoleBackground (int lines)
577 {
578         // LordHavoc: changed alpha
579         //int y = (vid.height >> 1);
580
581         if (lines >= (int) vid.height)
582                 Draw_Pic(0, lines - vid.height, conback);
583         else
584                 Draw_AlphaPic (0, lines - vid.height, conback, scr_conalpha.value*lines/vid.height);
585         //      Draw_AlphaPic (0, lines - vid.height, conback, (float)(1.2 * lines)/y);
586         // LordHavoc: draw version
587         Draw_String(engineversionx, lines - vid.height + engineversiony, engineversion, 9999);
588 }
589
590 /*
591 =============
592 Draw_Fill
593
594 Fills a box of pixels with a single color
595 =============
596 */
597 void Draw_Fill (int x, int y, int w, int h, int c)
598 {
599         glDisable (GL_TEXTURE_2D);
600         glColor3f (host_basepal[c*3]/255.0, host_basepal[c*3+1]/255.0, host_basepal[c*3+2]/255.0);
601
602         glBegin (GL_QUADS);
603
604         glVertex2f (x,y);
605         glVertex2f (x+w, y);
606         glVertex2f (x+w, y+h);
607         glVertex2f (x, y+h);
608
609         glEnd ();
610         glColor3f(1,1,1);
611         glEnable (GL_TEXTURE_2D);
612 }
613 //=============================================================================
614
615 //=============================================================================
616
617 /*
618 ================
619 GL_Set2D
620
621 Setup as if the screen was 320*200
622 ================
623 */
624 void GL_Set2D (void)
625 {
626         glViewport (glx, gly, glwidth, glheight);
627
628         glMatrixMode(GL_PROJECTION);
629     glLoadIdentity ();
630         glOrtho  (0, vid.width, vid.height, 0, -99999, 99999);
631
632         glMatrixMode(GL_MODELVIEW);
633     glLoadIdentity ();
634
635         glDisable (GL_DEPTH_TEST);
636         glDisable (GL_CULL_FACE);
637         glEnable (GL_BLEND); // was Disable
638 //      glEnable (GL_ALPHA_TEST);
639         glDisable (GL_ALPHA_TEST);
640         glEnable(GL_TEXTURE_2D);
641
642         // LordHavoc: added this
643         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
644         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
645
646         glColor3f(1,1,1);
647 }
648
649 // LordHavoc: SHOWLMP stuff
650 #define SHOWLMP_MAXLABELS 256
651 typedef struct showlmp_s
652 {
653         qboolean        isactive;
654         float           x;
655         float           y;
656         char            label[32];
657         char            pic[128];
658 } showlmp_t;
659
660 showlmp_t showlmp[SHOWLMP_MAXLABELS];
661
662 void SHOWLMP_decodehide()
663 {
664         int i;
665         byte *lmplabel;
666         lmplabel = MSG_ReadString();
667         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
668                 if (showlmp[i].isactive && strcmp(showlmp[i].label, lmplabel) == 0)
669                 {
670                         showlmp[i].isactive = false;
671                         return;
672                 }
673 }
674
675 void SHOWLMP_decodeshow()
676 {
677         int i, k;
678         byte lmplabel[256], picname[256];
679         float x, y;
680         strcpy(lmplabel,MSG_ReadString());
681         strcpy(picname, MSG_ReadString());
682         x = MSG_ReadByte();
683         y = MSG_ReadByte();
684         k = -1;
685         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
686                 if (showlmp[i].isactive)
687                 {
688                         if (strcmp(showlmp[i].label, lmplabel) == 0)
689                         {
690                                 k = i;
691                                 break; // drop out to replace it
692                         }
693                 }
694                 else if (k < 0) // find first empty one to replace
695                         k = i;
696         if (k < 0)
697                 return; // none found to replace
698         // change existing one
699         showlmp[k].isactive = true;
700         strcpy(showlmp[k].label, lmplabel);
701         strcpy(showlmp[k].pic, picname);
702         showlmp[k].x = x;
703         showlmp[k].y = y;
704 }
705
706 void SHOWLMP_drawall()
707 {
708         int i;
709         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
710                 if (showlmp[i].isactive)
711                         Draw_Pic(showlmp[i].x, showlmp[i].y, Draw_CachePic(showlmp[i].pic));
712 }
713
714 void SHOWLMP_clear()
715 {
716         int i;
717         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
718                 showlmp[i].isactive = false;
719 }