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 cvar_t qsg_version = {"qsg_version", "1"};
29 cvar_t scr_conalpha = {"scr_conalpha", "1"};
31 byte *draw_chars; // 8*8 graphic characters
45 =============================================================================
49 Allocate all the little status bar obejcts into a single texture
50 to crutch up stupid hardware / drivers
52 =============================================================================
56 #define BLOCK_WIDTH 256
57 #define BLOCK_HEIGHT 256
59 int scrap_allocated[MAX_SCRAPS][BLOCK_WIDTH];
60 byte scrap_texels[MAX_SCRAPS][BLOCK_WIDTH*BLOCK_HEIGHT*4];
63 // returns a texture number and the position inside it
64 int Scrap_AllocBlock (int w, int h, int *x, int *y)
70 for (texnum=0 ; texnum<MAX_SCRAPS ; texnum++)
74 for (i=0 ; i<BLOCK_WIDTH-w ; i++)
80 if (scrap_allocated[texnum][i+j] >= best)
82 if (scrap_allocated[texnum][i+j] > best2)
83 best2 = scrap_allocated[texnum][i+j];
86 { // this is a valid spot
92 if (best + h > BLOCK_HEIGHT)
96 scrap_allocated[texnum][*x + i] = best + h;
101 Sys_Error ("Scrap_AllocBlock: full");
106 int scraptexnum[MAX_SCRAPS];
108 void Scrap_Upload (void)
114 for (texnum=0 ; texnum<MAX_SCRAPS ; texnum++)
115 scraptexnum[texnum] = GL_LoadTexture (va("scrapslot%d", texnum), BLOCK_WIDTH, BLOCK_HEIGHT, scrap_texels[texnum], false, true, 1);
119 //=============================================================================
120 /* Support Routines */
122 typedef struct cachepic_s
124 char name[MAX_QPATH];
126 byte padding[32]; // for appended glpic
129 #define MAX_CACHED_PICS 128
130 cachepic_t menu_cachepics[MAX_CACHED_PICS];
131 int menu_numcachepics;
133 byte menuplyr_pixels[4096];
138 extern int GL_LoadPicTexture (qpic_t *pic);
140 qpic_t *Draw_PicFromWad (char *name)
145 p = W_GetLumpName (name);
146 gl = (glpic_t *)p->data;
148 // load little ones into the scrap
149 if (p->width < 64 && p->height < 64)
155 texnum = Scrap_AllocBlock (p->width, p->height, &x, &y);
158 for (i=0 ; i<p->height ; i++)
159 for (j=0 ; j<p->width ; j++, k++)
160 scrap_texels[texnum][(y+i)*BLOCK_WIDTH + x + j] = p->data[k];
161 if (!scraptexnum[texnum])
162 scraptexnum[texnum] = GL_LoadTexture (va("scrapslot%d", texnum), BLOCK_WIDTH, BLOCK_HEIGHT, scrap_texels[texnum], false, true, 1);
163 gl->texnum = scraptexnum[texnum];
164 gl->sl = (x+0.01)/(float)BLOCK_WIDTH;
165 gl->sh = (x+p->width-0.01)/(float)BLOCK_WIDTH;
166 gl->tl = (y+0.01)/(float)BLOCK_WIDTH;
167 gl->th = (y+p->height-0.01)/(float)BLOCK_WIDTH;
170 pic_texels += p->width*p->height;
174 gl->texnum = GL_LoadPicTexture (p);
189 qpic_t *Draw_CachePic (char *path)
196 for (pic=menu_cachepics, i=0 ; i<menu_numcachepics ; pic++, i++)
197 if (!strcmp (path, pic->name))
200 if (menu_numcachepics == MAX_CACHED_PICS)
201 Sys_Error ("menu_numcachepics == MAX_CACHED_PICS");
203 strcpy (pic->name, path);
206 // load the pic from disk
208 dat = (qpic_t *)COM_LoadTempFile (path, false);
210 Sys_Error ("Draw_CachePic: failed to load %s", path);
213 // HACK HACK HACK --- we need to keep the bytes for
214 // the translatable player picture just for the menu
215 // configuration dialog
216 if (!strcmp (path, "gfx/menuplyr.lmp"))
217 memcpy (menuplyr_pixels, dat->data, dat->width*dat->height);
219 pic->pic.width = dat->width;
220 pic->pic.height = dat->height;
222 gl = (glpic_t *)pic->pic.data;
223 gl->texnum = loadtextureimage(path, 0, 0, false, false);
225 gl->texnum = GL_LoadPicTexture (dat);
234 extern void LoadSky_f(void);
241 void rmain_registercvars();
242 extern int buildnumber;
248 char_texture = loadtextureimage ("conchars", 0, 0, false, false);
251 draw_chars = W_GetLumpName ("conchars");
252 for (i=0 ; i<128*128 ; i++)
253 if (draw_chars[i] == 0)
254 draw_chars[i] = 255; // proper transparent color
256 // now turn them into textures
257 char_texture = GL_LoadTexture ("charset", 128, 128, draw_chars, false, true, 1);
260 conbacktexnum = loadtextureimage("gfx/conback", 0, 0, false, false);
262 memset(scraptexnum, 0, sizeof(scraptexnum));
264 // get the other pics we need
265 draw_disc = Draw_PicFromWad ("disc");
268 void gl_draw_shutdown()
272 char engineversion[40];
273 int engineversionx, engineversiony;
275 extern void GL_Textures_Init();
276 void GL_Draw_Init (void)
279 Cvar_RegisterVariable (&qsg_version);
280 Cvar_RegisterVariable (&scr_conalpha);
282 Cmd_AddCommand ("loadsky", &LoadSky_f);
284 #if defined(__linux__)
285 sprintf (engineversion, "DarkPlaces Linux GL %.2f build %3i", (float) VERSION, buildnumber);
287 sprintf (engineversion, "DarkPlaces Windows GL %.2f build %3i", (float) VERSION, buildnumber);
289 sprintf (engineversion, "DarkPlaces Unknown GL %.2f build %3i", (float) VERSION, buildnumber);
291 for (i = 0;i < 40 && engineversion[i];i++)
292 engineversion[i] += 0x80; // shift to orange
293 engineversionx = vid.width - strlen(engineversion) * 8 - 8;
294 engineversiony = vid.height - 8;
297 R_RegisterModule("GL_Draw", gl_draw_start, gl_draw_shutdown);
304 Draws one 8*8 graphics character with 0 being transparent.
305 It can be clipped to the top of the screen to allow the console to be
306 smoothly scrolled off.
309 void Draw_Character (int x, int y, int num)
312 float frow, fcol, size;
320 return; // totally off screen
331 glBindTexture(GL_TEXTURE_2D, char_texture);
332 // LordHavoc: NEAREST mode on text if not scaling up
333 if ((int) vid.width < glwidth)
335 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
336 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
341 glTexCoord2f (fcol, frow);
343 glTexCoord2f (fcol + size, frow);
345 glTexCoord2f (fcol + size, frow + size);
346 glVertex2f (x+8, y+8);
347 glTexCoord2f (fcol, frow + size);
351 // LordHavoc: revert to LINEAR mode
352 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
353 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
361 // LordHavoc: sped this up a lot, and added maxlen
362 void Draw_String (int x, int y, char *str, int maxlen)
368 if (y <= -8 || y >= (int) vid.height || x >= (int) vid.width || *str == 0) // completely offscreen or no text to print
371 maxlen = strlen(str);
372 else if (maxlen > (int) strlen(str))
373 maxlen = strlen(str);
374 glBindTexture(GL_TEXTURE_2D, char_texture);
376 // LordHavoc: NEAREST mode on text if not scaling up
377 if ((int) vid.width < glwidth)
379 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
380 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
385 while (maxlen-- && x < (int) vid.width) // stop rendering when out of characters or room
387 if ((num = *str++) != 32) // skip spaces
389 frow = (float) ((int) num >> 4)*0.0625;
390 fcol = (float) ((int) num & 15)*0.0625;
391 glTexCoord2f (fcol , frow );glVertex2f (x, y);
392 glTexCoord2f (fcol + 0.0625, frow );glVertex2f (x+8, y);
393 glTexCoord2f (fcol + 0.0625, frow + 0.0625);glVertex2f (x+8, y+8);
394 glTexCoord2f (fcol , frow + 0.0625);glVertex2f (x, y+8);
400 // LordHavoc: revert to LINEAR mode
401 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
402 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
405 void Draw_GenericPic (int texnum, float red, float green, float blue, float alpha, int x, int y, int width, int height)
409 glColor4f(red,green,blue,alpha);
410 glBindTexture(GL_TEXTURE_2D, texnum);
412 glTexCoord2f (0, 0);glVertex2f (x, y);
413 glTexCoord2f (1, 0);glVertex2f (x+width, y);
414 glTexCoord2f (1, 1);glVertex2f (x+width, y+height);
415 glTexCoord2f (0, 1);glVertex2f (x, y+height);
424 void Draw_AlphaPic (int x, int y, qpic_t *pic, float alpha)
430 gl = (glpic_t *)pic->data;
433 glColor4f(1,1,1,alpha);
434 glBindTexture(GL_TEXTURE_2D, gl->texnum);
436 glTexCoord2f (gl->sl, gl->tl);glVertex2f (x, y);
437 glTexCoord2f (gl->sh, gl->tl);glVertex2f (x+pic->width, y);
438 glTexCoord2f (gl->sh, gl->th);glVertex2f (x+pic->width, y+pic->height);
439 glTexCoord2f (gl->sl, gl->th);glVertex2f (x, y+pic->height);
449 void Draw_Pic (int x, int y, qpic_t *pic)
455 gl = (glpic_t *)pic->data;
459 glBindTexture(GL_TEXTURE_2D, gl->texnum);
461 glTexCoord2f (gl->sl, gl->tl);glVertex2f (x, y);
462 glTexCoord2f (gl->sh, gl->tl);glVertex2f (x+pic->width, y);
463 glTexCoord2f (gl->sh, gl->th);glVertex2f (x+pic->width, y+pic->height);
464 glTexCoord2f (gl->sl, gl->th);glVertex2f (x, y+pic->height);
473 Only used for the player color selection menu
476 void Draw_PicTranslate (int x, int y, qpic_t *pic, byte *translation)
479 byte *trans, *src, *dest;
481 c = pic->width * pic->height;
482 src = menuplyr_pixels;
483 dest = trans = malloc(c);
484 for (i = 0;i < c;i++)
485 *dest++ = translation[*src++];
487 c = GL_LoadTexture ("translatedplayerpic", pic->width, pic->height, trans, false, true, 1);
492 Draw_GenericPic (c, 1,1,1,1, x, y, pic->width, pic->height);
494 glBindTexture(GL_TEXTURE_2D, c);
497 glTexCoord2f (0, 0);glVertex2f (x, y);
498 glTexCoord2f (1, 0);glVertex2f (x+pic->width, y);
499 glTexCoord2f (1, 1);glVertex2f (x+pic->width, y+pic->height);
500 glTexCoord2f (0, 1);glVertex2f (x, y+pic->height);
508 Draw_ConsoleBackground
512 void Draw_ConsoleBackground (int lines)
514 Draw_GenericPic (conbacktexnum, 1,1,1,scr_conalpha.value*lines/vid.height, 0, lines - vid.height, vid.width, vid.height);
515 // LordHavoc: draw version
516 Draw_String(engineversionx, lines - vid.height + engineversiony, engineversion, 9999);
523 Fills a box of pixels with a single color
526 void Draw_Fill (int x, int y, int w, int h, int c)
530 glDisable (GL_TEXTURE_2D);
531 glColor3f (host_basepal[c*3]/255.0, host_basepal[c*3+1]/255.0, host_basepal[c*3+2]/255.0);
537 glVertex2f (x+w, y+h);
542 glEnable (GL_TEXTURE_2D);
544 //=============================================================================
546 //=============================================================================
552 Setup as if the screen was 320*200
559 glViewport (glx, gly, glwidth, glheight);
561 glMatrixMode(GL_PROJECTION);
563 glOrtho (0, vid.width, vid.height, 0, -99999, 99999);
565 glMatrixMode(GL_MODELVIEW);
568 glDisable (GL_DEPTH_TEST);
569 glDisable (GL_CULL_FACE);
571 glDisable (GL_ALPHA_TEST);
572 glEnable(GL_TEXTURE_2D);
574 // LordHavoc: added this
575 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
576 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
581 // LordHavoc: SHOWLMP stuff
582 #define SHOWLMP_MAXLABELS 256
583 typedef struct showlmp_s
592 showlmp_t showlmp[SHOWLMP_MAXLABELS];
594 void SHOWLMP_decodehide()
598 lmplabel = MSG_ReadString();
599 for (i = 0;i < SHOWLMP_MAXLABELS;i++)
600 if (showlmp[i].isactive && strcmp(showlmp[i].label, lmplabel) == 0)
602 showlmp[i].isactive = false;
607 void SHOWLMP_decodeshow()
610 byte lmplabel[256], picname[256];
612 strcpy(lmplabel,MSG_ReadString());
613 strcpy(picname, MSG_ReadString());
617 for (i = 0;i < SHOWLMP_MAXLABELS;i++)
618 if (showlmp[i].isactive)
620 if (strcmp(showlmp[i].label, lmplabel) == 0)
623 break; // drop out to replace it
626 else if (k < 0) // find first empty one to replace
629 return; // none found to replace
630 // change existing one
631 showlmp[k].isactive = true;
632 strcpy(showlmp[k].label, lmplabel);
633 strcpy(showlmp[k].pic, picname);
638 void SHOWLMP_drawall()
641 for (i = 0;i < SHOWLMP_MAXLABELS;i++)
642 if (showlmp[i].isactive)
643 Draw_Pic(showlmp[i].x, showlmp[i].y, Draw_CachePic(showlmp[i].pic));
649 for (i = 0;i < SHOWLMP_MAXLABELS;i++)
650 showlmp[i].isactive = false;