2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
21 // draw.c -- this is the only file outside the refresh that touches the
26 #define GL_COLOR_INDEX8_EXT 0x80E5
28 extern unsigned char d_15to8table[65536];
30 cvar_t qsg_version = {"qsg_version", "1"};
31 cvar_t scr_conalpha = {"scr_conalpha", "1"};
33 byte *draw_chars; // 8*8 graphic characters
47 =============================================================================
51 Allocate all the little status bar obejcts into a single texture
52 to crutch up stupid hardware / drivers
54 =============================================================================
58 #define BLOCK_WIDTH 256
59 #define BLOCK_HEIGHT 256
61 int scrap_allocated[MAX_SCRAPS][BLOCK_WIDTH];
62 byte scrap_texels[MAX_SCRAPS][BLOCK_WIDTH*BLOCK_HEIGHT*4];
65 // returns a texture number and the position inside it
66 int Scrap_AllocBlock (int w, int h, int *x, int *y)
72 for (texnum=0 ; texnum<MAX_SCRAPS ; texnum++)
76 for (i=0 ; i<BLOCK_WIDTH-w ; i++)
82 if (scrap_allocated[texnum][i+j] >= best)
84 if (scrap_allocated[texnum][i+j] > best2)
85 best2 = scrap_allocated[texnum][i+j];
88 { // this is a valid spot
94 if (best + h > BLOCK_HEIGHT)
98 scrap_allocated[texnum][*x + i] = best + h;
103 Sys_Error ("Scrap_AllocBlock: full");
108 int scraptexnum[MAX_SCRAPS];
110 void Scrap_Upload (void)
116 for (texnum=0 ; texnum<MAX_SCRAPS ; texnum++)
117 scraptexnum[texnum] = GL_LoadTexture (va("scrapslot%d", texnum), BLOCK_WIDTH, BLOCK_HEIGHT, scrap_texels[texnum], false, true, 1);
121 //=============================================================================
122 /* Support Routines */
124 typedef struct cachepic_s
126 char name[MAX_QPATH];
128 byte padding[32]; // for appended glpic
131 #define MAX_CACHED_PICS 128
132 cachepic_t menu_cachepics[MAX_CACHED_PICS];
133 int menu_numcachepics;
135 byte menuplyr_pixels[4096];
140 int GL_LoadPicTexture (qpic_t *pic);
142 qpic_t *Draw_PicFromWad (char *name)
147 p = W_GetLumpName (name);
148 gl = (glpic_t *)p->data;
150 // load little ones into the scrap
151 if (p->width < 64 && p->height < 64)
157 texnum = Scrap_AllocBlock (p->width, p->height, &x, &y);
160 for (i=0 ; i<p->height ; i++)
161 for (j=0 ; j<p->width ; j++, k++)
162 scrap_texels[texnum][(y+i)*BLOCK_WIDTH + x + j] = p->data[k];
163 if (!scraptexnum[texnum])
164 scraptexnum[texnum] = GL_LoadTexture (va("scrapslot%d", texnum), BLOCK_WIDTH, BLOCK_HEIGHT, scrap_texels[texnum], false, true, 1);
165 gl->texnum = scraptexnum[texnum];
166 gl->sl = (x+0.01)/(float)BLOCK_WIDTH;
167 gl->sh = (x+p->width-0.01)/(float)BLOCK_WIDTH;
168 gl->tl = (y+0.01)/(float)BLOCK_WIDTH;
169 gl->th = (y+p->height-0.01)/(float)BLOCK_WIDTH;
172 pic_texels += p->width*p->height;
176 gl->texnum = GL_LoadPicTexture (p);
191 qpic_t *Draw_CachePic (char *path)
198 for (pic=menu_cachepics, i=0 ; i<menu_numcachepics ; pic++, i++)
199 if (!strcmp (path, pic->name))
202 if (menu_numcachepics == MAX_CACHED_PICS)
203 Sys_Error ("menu_numcachepics == MAX_CACHED_PICS");
205 strcpy (pic->name, path);
208 // load the pic from disk
210 dat = (qpic_t *)COM_LoadTempFile (path, false);
212 Sys_Error ("Draw_CachePic: failed to load %s", path);
215 // HACK HACK HACK --- we need to keep the bytes for
216 // the translatable player picture just for the menu
217 // configuration dialog
218 if (!strcmp (path, "gfx/menuplyr.lmp"))
219 memcpy (menuplyr_pixels, dat->data, dat->width*dat->height);
221 pic->pic.width = dat->width;
222 pic->pic.height = dat->height;
224 gl = (glpic_t *)pic->pic.data;
225 gl->texnum = loadtextureimage(path, 0, 0, false, false);
227 gl->texnum = GL_LoadPicTexture (dat);
236 extern void LoadSky_f(void);
238 extern char *QSG_EXTENSIONS;
245 void rmain_registercvars();
246 extern int buildnumber;
252 char_texture = loadtextureimage ("conchars", 0, 0, false, false);
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
260 // now turn them into textures
261 char_texture = GL_LoadTexture ("charset", 128, 128, draw_chars, false, true, 1);
264 conbacktexnum = loadtextureimage("gfx/conback", 0, 0, false, false);
266 memset(scraptexnum, 0, sizeof(scraptexnum));
268 // get the other pics we need
269 draw_disc = Draw_PicFromWad ("disc");
272 void gl_draw_shutdown()
276 char engineversion[40];
277 int engineversionx, engineversiony;
279 extern void GL_Textures_Init();
280 void GL_Draw_Init (void)
283 Cvar_RegisterVariable (&qsg_version);
284 Cvar_RegisterVariable (&scr_conalpha);
286 Cmd_AddCommand ("loadsky", &LoadSky_f);
289 #if defined(__linux__)
290 sprintf (engineversion, "DPNehahra Linux GL %.2f build %3i", (float) VERSION, buildnumber);
292 sprintf (engineversion, "DPNehahra Windows GL %.2f build %3i", (float) VERSION, buildnumber);
294 sprintf (engineversion, "DPNehahra Unknown GL %.2f build %3i", (float) VERSION, buildnumber);
297 #if defined(__linux__)
298 sprintf (engineversion, "DarkPlaces Linux GL %.2f build %3i", (float) VERSION, buildnumber);
300 sprintf (engineversion, "DarkPlaces Windows GL %.2f build %3i", (float) VERSION, buildnumber);
302 sprintf (engineversion, "DarkPlaces Unknown GL %.2f build %3i", (float) VERSION, buildnumber);
305 for (i = 0;i < 40 && engineversion[i];i++)
306 engineversion[i] += 0x80; // shift to orange
307 engineversionx = vid.width - strlen(engineversion) * 8 - 8;
308 engineversiony = vid.height - 8;
311 R_RegisterModule("GL_Draw", gl_draw_start, gl_draw_shutdown);
318 Draws one 8*8 graphics character with 0 being transparent.
319 It can be clipped to the top of the screen to allow the console to be
320 smoothly scrolled off.
323 void Draw_Character (int x, int y, int num)
326 float frow, fcol, size;
334 return; // totally off screen
345 glBindTexture(GL_TEXTURE_2D, char_texture);
346 // LordHavoc: NEAREST mode on text if not scaling up
347 if ((int) vid.width < glwidth)
349 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
350 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
355 glTexCoord2f (fcol, frow);
357 glTexCoord2f (fcol + size, frow);
359 glTexCoord2f (fcol + size, frow + size);
360 glVertex2f (x+8, y+8);
361 glTexCoord2f (fcol, frow + size);
365 // LordHavoc: revert to LINEAR mode
366 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
367 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
375 // LordHavoc: sped this up a lot, and added maxlen
376 void Draw_String (int x, int y, char *str, int maxlen)
382 if (y <= -8 || y >= (int) vid.height || x >= (int) vid.width || *str == 0) // completely offscreen or no text to print
385 maxlen = strlen(str);
386 else if (maxlen > (int) strlen(str))
387 maxlen = strlen(str);
388 glBindTexture(GL_TEXTURE_2D, char_texture);
390 // LordHavoc: NEAREST mode on text if not scaling up
391 if ((int) vid.width < glwidth)
393 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
394 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
399 while (maxlen-- && x < (int) vid.width) // stop rendering when out of characters or room
401 if ((num = *str++) != 32) // skip spaces
403 frow = (float) ((int) num >> 4)*0.0625;
404 fcol = (float) ((int) num & 15)*0.0625;
405 glTexCoord2f (fcol , frow );glVertex2f (x, y);
406 glTexCoord2f (fcol + 0.0625, frow );glVertex2f (x+8, y);
407 glTexCoord2f (fcol + 0.0625, frow + 0.0625);glVertex2f (x+8, y+8);
408 glTexCoord2f (fcol , frow + 0.0625);glVertex2f (x, y+8);
414 // LordHavoc: revert to LINEAR mode
415 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
416 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
419 void Draw_GenericPic (int texnum, float red, float green, float blue, float alpha, int x, int y, int width, int height)
423 glColor4f(red,green,blue,alpha);
424 glBindTexture(GL_TEXTURE_2D, texnum);
426 glTexCoord2f (0, 0);glVertex2f (x, y);
427 glTexCoord2f (1, 0);glVertex2f (x+width, y);
428 glTexCoord2f (1, 1);glVertex2f (x+width, y+height);
429 glTexCoord2f (0, 1);glVertex2f (x, y+height);
438 void Draw_AlphaPic (int x, int y, qpic_t *pic, float alpha)
444 gl = (glpic_t *)pic->data;
447 glColor4f(1,1,1,alpha);
448 glBindTexture(GL_TEXTURE_2D, gl->texnum);
450 glTexCoord2f (gl->sl, gl->tl);glVertex2f (x, y);
451 glTexCoord2f (gl->sh, gl->tl);glVertex2f (x+pic->width, y);
452 glTexCoord2f (gl->sh, gl->th);glVertex2f (x+pic->width, y+pic->height);
453 glTexCoord2f (gl->sl, gl->th);glVertex2f (x, y+pic->height);
463 void Draw_Pic (int x, int y, qpic_t *pic)
469 gl = (glpic_t *)pic->data;
473 glBindTexture(GL_TEXTURE_2D, gl->texnum);
475 glTexCoord2f (gl->sl, gl->tl);glVertex2f (x, y);
476 glTexCoord2f (gl->sh, gl->tl);glVertex2f (x+pic->width, y);
477 glTexCoord2f (gl->sh, gl->th);glVertex2f (x+pic->width, y+pic->height);
478 glTexCoord2f (gl->sl, gl->th);glVertex2f (x, y+pic->height);
487 Only used for the player color selection menu
490 void Draw_PicTranslate (int x, int y, qpic_t *pic, byte *translation)
493 byte *trans, *src, *dest;
495 c = pic->width * pic->height;
496 src = menuplyr_pixels;
497 dest = trans = malloc(c);
498 for (i = 0;i < c;i++)
499 *dest++ = translation[*src++];
501 c = GL_LoadTexture ("translatedplayerpic", pic->width, pic->height, trans, false, true, 1);
506 Draw_GenericPic (c, 1,1,1,1, x, y, pic->width, pic->height);
508 glBindTexture(GL_TEXTURE_2D, c);
511 glTexCoord2f (0, 0);glVertex2f (x, y);
512 glTexCoord2f (1, 0);glVertex2f (x+pic->width, y);
513 glTexCoord2f (1, 1);glVertex2f (x+pic->width, y+pic->height);
514 glTexCoord2f (0, 1);glVertex2f (x, y+pic->height);
522 Draw_ConsoleBackground
526 void Draw_ConsoleBackground (int lines)
528 Draw_GenericPic (conbacktexnum, 1,1,1,scr_conalpha.value*lines/vid.height, 0, lines - vid.height, vid.width, vid.height);
529 // LordHavoc: draw version
530 Draw_String(engineversionx, lines - vid.height + engineversiony, engineversion, 9999);
537 Fills a box of pixels with a single color
540 void Draw_Fill (int x, int y, int w, int h, int c)
544 glDisable (GL_TEXTURE_2D);
545 glColor3f (host_basepal[c*3]/255.0, host_basepal[c*3+1]/255.0, host_basepal[c*3+2]/255.0);
551 glVertex2f (x+w, y+h);
556 glEnable (GL_TEXTURE_2D);
558 //=============================================================================
560 //=============================================================================
566 Setup as if the screen was 320*200
573 glViewport (glx, gly, glwidth, glheight);
575 glMatrixMode(GL_PROJECTION);
577 glOrtho (0, vid.width, vid.height, 0, -99999, 99999);
579 glMatrixMode(GL_MODELVIEW);
582 glDisable (GL_DEPTH_TEST);
583 glDisable (GL_CULL_FACE);
585 glDisable (GL_ALPHA_TEST);
586 glEnable(GL_TEXTURE_2D);
588 // LordHavoc: added this
589 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
590 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
595 // LordHavoc: SHOWLMP stuff
596 #define SHOWLMP_MAXLABELS 256
597 typedef struct showlmp_s
606 showlmp_t showlmp[SHOWLMP_MAXLABELS];
608 void SHOWLMP_decodehide()
612 lmplabel = MSG_ReadString();
613 for (i = 0;i < SHOWLMP_MAXLABELS;i++)
614 if (showlmp[i].isactive && strcmp(showlmp[i].label, lmplabel) == 0)
616 showlmp[i].isactive = false;
621 void SHOWLMP_decodeshow()
624 byte lmplabel[256], picname[256];
626 strcpy(lmplabel,MSG_ReadString());
627 strcpy(picname, MSG_ReadString());
631 for (i = 0;i < SHOWLMP_MAXLABELS;i++)
632 if (showlmp[i].isactive)
634 if (strcmp(showlmp[i].label, lmplabel) == 0)
637 break; // drop out to replace it
640 else if (k < 0) // find first empty one to replace
643 return; // none found to replace
644 // change existing one
645 showlmp[k].isactive = true;
646 strcpy(showlmp[k].label, lmplabel);
647 strcpy(showlmp[k].pic, picname);
652 void SHOWLMP_drawall()
655 for (i = 0;i < SHOWLMP_MAXLABELS;i++)
656 if (showlmp[i].isactive)
657 Draw_Pic(showlmp[i].x, showlmp[i].y, Draw_CachePic(showlmp[i].pic));
663 for (i = 0;i < SHOWLMP_MAXLABELS;i++)
664 showlmp[i].isactive = false;