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