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 =============================================================================
57 #define BLOCK_WIDTH 256
58 #define BLOCK_HEIGHT 256
60 int scrap_allocated[MAX_SCRAPS][BLOCK_WIDTH];
61 byte scrap_texels[MAX_SCRAPS][BLOCK_WIDTH*BLOCK_HEIGHT*4];
64 // returns a texture number and the position inside it
65 int Scrap_AllocBlock (int w, int h, int *x, int *y)
71 for (texnum=0 ; texnum<MAX_SCRAPS ; texnum++)
75 for (i=0 ; i<BLOCK_WIDTH-w ; i++)
81 if (scrap_allocated[texnum][i+j] >= best)
83 if (scrap_allocated[texnum][i+j] > best2)
84 best2 = scrap_allocated[texnum][i+j];
87 { // this is a valid spot
93 if (best + h > BLOCK_HEIGHT)
97 scrap_allocated[texnum][*x + i] = best + h;
102 Sys_Error ("Scrap_AllocBlock: full");
107 int scraptexnum[MAX_SCRAPS];
109 void Scrap_Upload (void)
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);
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 extern 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
152 if (p->width < 64 && p->height < 64)
158 texnum = Scrap_AllocBlock (p->width, p->height, &x, &y);
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;
173 pic_texels += p->width*p->height;
178 gl->texnum = GL_LoadPicTexture (p);
193 qpic_t *Draw_CachePic (char *path)
200 for (pic=menu_cachepics, i=0 ; i<menu_numcachepics ; pic++, i++)
201 if (!strcmp (path, pic->name))
204 if (menu_numcachepics == MAX_CACHED_PICS)
205 Sys_Error ("menu_numcachepics == MAX_CACHED_PICS");
207 strcpy (pic->name, path);
210 // load the pic from disk
212 dat = (qpic_t *)COM_LoadTempFile (path, false);
214 Sys_Error ("Draw_CachePic: failed to load %s", path);
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);
223 pic->pic.width = dat->width;
224 pic->pic.height = dat->height;
226 gl = (glpic_t *)pic->pic.data;
227 gl->texnum = loadtextureimage(path, 0, 0, false, false);
229 gl->texnum = GL_LoadPicTexture (dat);
238 extern void LoadSky_f(void);
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);
288 #if defined(__linux__)
289 sprintf (engineversion, "DarkPlaces Linux GL %.2f build %3i", (float) VERSION, buildnumber);
291 sprintf (engineversion, "DarkPlaces Windows GL %.2f build %3i", (float) VERSION, buildnumber);
293 sprintf (engineversion, "DarkPlaces Unknown GL %.2f build %3i", (float) VERSION, buildnumber);
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;
301 R_RegisterModule("GL_Draw", gl_draw_start, gl_draw_shutdown);
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.
313 void Draw_Character (int x, int y, int num)
316 float frow, fcol, size;
324 return; // totally off screen
335 glBindTexture(GL_TEXTURE_2D, char_texture);
336 // LordHavoc: NEAREST mode on text if not scaling up
337 if (glwidth < (int) vid.width)
339 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
340 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
345 glTexCoord2f (fcol, frow);
347 glTexCoord2f (fcol + size, frow);
349 glTexCoord2f (fcol + size, frow + size);
350 glVertex2f (x+8, y+8);
351 glTexCoord2f (fcol, frow + size);
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);
365 // LordHavoc: sped this up a lot, and added maxlen
366 void Draw_String (int x, int y, char *str, int maxlen)
372 if (y <= -8 || y >= (int) vid.height || x >= (int) vid.width || *str == 0) // completely offscreen or no text to print
375 maxlen = strlen(str);
376 else if (maxlen > (int) strlen(str))
377 maxlen = strlen(str);
378 glBindTexture(GL_TEXTURE_2D, char_texture);
380 // LordHavoc: NEAREST mode on text if not scaling up
381 if (glwidth < (int) vid.width)
383 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
384 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
389 while (maxlen-- && x < (int) vid.width) // stop rendering when out of characters or room
391 if ((num = *str++) != 32) // skip spaces
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);
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);
409 void Draw_GenericPic (int texnum, float red, float green, float blue, float alpha, int x, int y, int width, int height)
413 glColor4f(red,green,blue,alpha);
414 glBindTexture(GL_TEXTURE_2D, texnum);
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);
428 void Draw_AlphaPic (int x, int y, qpic_t *pic, float alpha)
434 gl = (glpic_t *)pic->data;
437 glColor4f(1,1,1,alpha);
438 glBindTexture(GL_TEXTURE_2D, gl->texnum);
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);
453 void Draw_Pic (int x, int y, qpic_t *pic)
459 gl = (glpic_t *)pic->data;
463 glBindTexture(GL_TEXTURE_2D, gl->texnum);
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);
477 Only used for the player color selection menu
480 void Draw_PicTranslate (int x, int y, qpic_t *pic, byte *translation)
483 byte *trans, *src, *dest;
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++];
491 c = GL_LoadTexture ("translatedplayerpic", pic->width, pic->height, trans, false, true, 1);
496 Draw_GenericPic (c, 1,1,1,1, x, y, pic->width, pic->height);
498 glBindTexture(GL_TEXTURE_2D, c);
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);
512 Draw_ConsoleBackground
516 void Draw_ConsoleBackground (int lines)
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);
527 Fills a box of pixels with a single color
530 void Draw_Fill (int x, int y, int w, int h, int c)
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);
541 glVertex2f (x+w, y+h);
546 glEnable (GL_TEXTURE_2D);
548 //=============================================================================
550 //=============================================================================
556 Setup as if the screen was 320*200
563 glViewport (glx, gly, glwidth, glheight);
565 glMatrixMode(GL_PROJECTION);
567 glOrtho (0, vid.width, vid.height, 0, -99999, 99999);
569 glMatrixMode(GL_MODELVIEW);
572 glDisable (GL_DEPTH_TEST);
573 glDisable (GL_CULL_FACE);
575 glDisable (GL_ALPHA_TEST);
576 glEnable(GL_TEXTURE_2D);
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);
585 // LordHavoc: SHOWLMP stuff
586 #define SHOWLMP_MAXLABELS 256
587 typedef struct showlmp_s
596 showlmp_t showlmp[SHOWLMP_MAXLABELS];
598 void SHOWLMP_decodehide()
602 lmplabel = MSG_ReadString();
603 for (i = 0;i < SHOWLMP_MAXLABELS;i++)
604 if (showlmp[i].isactive && strcmp(showlmp[i].label, lmplabel) == 0)
606 showlmp[i].isactive = false;
611 void SHOWLMP_decodeshow()
614 byte lmplabel[256], picname[256];
616 strcpy(lmplabel,MSG_ReadString());
617 strcpy(picname, MSG_ReadString());
621 for (i = 0;i < SHOWLMP_MAXLABELS;i++)
622 if (showlmp[i].isactive)
624 if (strcmp(showlmp[i].label, lmplabel) == 0)
627 break; // drop out to replace it
630 else if (k < 0) // find first empty one to replace
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);
642 void SHOWLMP_drawall()
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));
653 for (i = 0;i < SHOWLMP_MAXLABELS;i++)
654 showlmp[i].isactive = false;