]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_draw.c
added default case for particles to report the error and remove the particle
[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 cvar_t          scr_conalpha = {"scr_conalpha", "1"};
29
30 byte            *draw_chars;                            // 8*8 graphic characters
31 qpic_t          *draw_disc;
32
33 rtexture_t      *char_texture;
34
35 typedef struct
36 {
37         rtexture_t      *tex;
38 } glpic_t;
39
40 rtexture_t      *conbacktex;
41
42 //=============================================================================
43 /* Support Routines */
44
45 typedef struct cachepic_s
46 {
47         char            name[MAX_QPATH];
48         qpic_t          pic;
49         byte            padding[32];    // for appended glpic
50 } cachepic_t;
51
52 #define MAX_CACHED_PICS         128
53 cachepic_t      menu_cachepics[MAX_CACHED_PICS];
54 int                     menu_numcachepics;
55
56 byte            menuplyr_pixels[4096];
57
58 int             pic_texels;
59 int             pic_count;
60
61 qpic_t *Draw_PicFromWad (char *name)
62 {
63         qpic_t  *p;
64         glpic_t *gl;
65
66         p = W_GetLumpName (name);
67         gl = (glpic_t *)p->data;
68
69         gl->tex = R_LoadTexture (name, p->width, p->height, p->data, TEXF_ALPHA | TEXF_PRECACHE);
70         return p;
71 }
72
73
74 /*
75 ================
76 Draw_CachePic
77 ================
78 */
79 qpic_t  *Draw_CachePic (char *path)
80 {
81         cachepic_t      *pic;
82         int                     i;
83         qpic_t          *dat;
84         glpic_t         *gl;
85
86         for (pic=menu_cachepics, i=0 ; i<menu_numcachepics ; pic++, i++)
87                 if (!strcmp (path, pic->name))
88                         return &pic->pic;
89
90         if (menu_numcachepics == MAX_CACHED_PICS)
91                 Sys_Error ("menu_numcachepics == MAX_CACHED_PICS");
92         menu_numcachepics++;
93         strcpy (pic->name, path);
94
95 //
96 // load the pic from disk
97 //
98         dat = (qpic_t *)COM_LoadMallocFile (path, false);
99         if (!dat)
100                 Sys_Error ("Draw_CachePic: failed to load %s", path);
101         SwapPic (dat);
102
103         // HACK HACK HACK --- we need to keep the bytes for
104         // the translatable player picture just for the menu
105         // configuration dialog
106         if (!strcmp (path, "gfx/menuplyr.lmp"))
107                 memcpy (menuplyr_pixels, dat->data, dat->width*dat->height);
108
109         pic->pic.width = dat->width;
110         pic->pic.height = dat->height;
111
112         gl = (glpic_t *)pic->pic.data;
113         gl->tex = loadtextureimage(path, 0, 0, false, false, true);
114         if (!gl->tex)
115                 gl->tex = R_LoadTexture (path, dat->width, dat->height, dat->data, TEXF_ALPHA | TEXF_PRECACHE);
116
117         qfree(dat);
118
119         return &pic->pic;
120 }
121
122 extern void LoadSky_f(void);
123
124 /*
125 ===============
126 Draw_Init
127 ===============
128 */
129 void rmain_registercvars();
130
131 void gl_draw_start()
132 {
133         int             i;
134
135         char_texture = loadtextureimage ("conchars", 0, 0, false, false, true);
136         if (!char_texture)
137         {
138                 draw_chars = W_GetLumpName ("conchars");
139                 for (i=0 ; i<128*128 ; i++)
140                         if (draw_chars[i] == 0)
141                                 draw_chars[i] = 255;    // proper transparent color
142
143                 // now turn them into textures
144                 char_texture = R_LoadTexture ("charset", 128, 128, draw_chars, TEXF_ALPHA | TEXF_PRECACHE);
145         }
146
147         conbacktex = loadtextureimage("gfx/conback", 0, 0, false, false, true);
148
149         // get the other pics we need
150         draw_disc = Draw_PicFromWad ("disc");
151 }
152
153 void gl_draw_shutdown()
154 {
155 }
156
157 char engineversion[40];
158 int engineversionx, engineversiony;
159
160 extern void R_Textures_Init();
161 void GL_Draw_Init (void)
162 {
163         int i;
164         Cvar_RegisterVariable (&scr_conalpha);
165
166         Cmd_AddCommand ("loadsky", &LoadSky_f);
167
168 #if defined(__linux__)
169         sprintf (engineversion, "DarkPlaces Linux   GL %.2f build %3i", (float) VERSION, buildnumber);
170 #elif defined(WIN32)
171         sprintf (engineversion, "DarkPlaces Windows GL %.2f build %3i", (float) VERSION, buildnumber);
172 #else
173         sprintf (engineversion, "DarkPlaces Unknown GL %.2f build %3i", (float) VERSION, buildnumber);
174 #endif
175         for (i = 0;i < 40 && engineversion[i];i++)
176                 engineversion[i] += 0x80; // shift to orange
177         engineversionx = vid.width - strlen(engineversion) * 8 - 8;
178         engineversiony = vid.height - 8;
179
180         R_Textures_Init();
181         R_RegisterModule("GL_Draw", gl_draw_start, gl_draw_shutdown);
182 }
183
184 /*
185 ================
186 Draw_Character
187
188 Draws one 8*8 graphics character with 0 being transparent.
189 It can be clipped to the top of the screen to allow the console to be
190 smoothly scrolled off.
191 ================
192 */
193 void Draw_Character (int x, int y, int num)
194 {
195         int                             row, col;
196         float                   frow, fcol, size;
197
198         if (num == 32)
199                 return;         // space
200
201         num &= 255;
202         
203         if (y <= -8)
204                 return;                 // totally off screen
205
206         row = num>>4;
207         col = num&15;
208
209         frow = row*0.0625;
210         fcol = col*0.0625;
211         size = 0.0625;
212
213         if (!r_render.value)
214                 return;
215         glBindTexture(GL_TEXTURE_2D, R_GetTexture(char_texture));
216         // LordHavoc: NEAREST mode on text if not scaling up
217         if (glwidth <= (int) vid.width)
218         {
219                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
220                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
221         }
222         else
223         {
224                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
225                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
226         }
227
228         glColor3f(1,1,1);
229         glBegin (GL_QUADS);
230         glTexCoord2f (fcol, frow);
231         glVertex2f (x, y);
232         glTexCoord2f (fcol + size, frow);
233         glVertex2f (x+8, y);
234         glTexCoord2f (fcol + size, frow + size);
235         glVertex2f (x+8, y+8);
236         glTexCoord2f (fcol, frow + size);
237         glVertex2f (x, y+8);
238         glEnd ();
239
240         // LordHavoc: revert to LINEAR mode
241 //      if (glwidth < (int) vid.width)
242 //      {
243 //              glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
244 //              glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
245 //      }
246 }
247
248 /*
249 ================
250 Draw_String
251 ================
252 */
253 // LordHavoc: sped this up a lot, and added maxlen
254 void Draw_String (int x, int y, char *str, int maxlen)
255 {
256         int num;
257         float frow, fcol;
258         if (!r_render.value)
259                 return;
260         if (y <= -8 || y >= (int) vid.height || x >= (int) vid.width || *str == 0) // completely offscreen or no text to print
261                 return;
262         if (maxlen < 1)
263                 maxlen = strlen(str);
264         else if (maxlen > (int) strlen(str))
265                 maxlen = strlen(str);
266         glBindTexture(GL_TEXTURE_2D, R_GetTexture(char_texture));
267
268         // LordHavoc: NEAREST mode on text if not scaling up
269         if (glwidth <= (int) vid.width)
270         {
271                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
272                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
273         }
274         else
275         {
276                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
277                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
278         }
279
280         glColor3f(1,1,1);
281         glBegin (GL_QUADS);
282         while (maxlen-- && x < (int) vid.width) // stop rendering when out of characters or room
283         {
284                 if ((num = *str++) != 32) // skip spaces
285                 {
286                         frow = (float) ((int) num >> 4)*0.0625;
287                         fcol = (float) ((int) num & 15)*0.0625;
288                         glTexCoord2f (fcol         , frow         );glVertex2f (x, y);
289                         glTexCoord2f (fcol + 0.0625, frow         );glVertex2f (x+8, y);
290                         glTexCoord2f (fcol + 0.0625, frow + 0.0625);glVertex2f (x+8, y+8);
291                         glTexCoord2f (fcol         , frow + 0.0625);glVertex2f (x, y+8);
292                 }
293                 x += 8;
294         }
295         glEnd ();
296
297         // LordHavoc: revert to LINEAR mode
298 //      if (glwidth < (int) vid.width)
299 //      {
300 //              glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
301 //              glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
302 //      }
303 }
304
305 void Draw_GenericPic (rtexture_t *tex, float red, float green, float blue, float alpha, int x, int y, int width, int height)
306 {
307         if (!r_render.value)
308                 return;
309         glColor4f(red,green,blue,alpha);
310         glBindTexture(GL_TEXTURE_2D, R_GetTexture(tex));
311         glBegin (GL_QUADS);
312         glTexCoord2f (0, 0);glVertex2f (x, y);
313         glTexCoord2f (1, 0);glVertex2f (x+width, y);
314         glTexCoord2f (1, 1);glVertex2f (x+width, y+height);
315         glTexCoord2f (0, 1);glVertex2f (x, y+height);
316         glEnd ();
317 }
318
319 /*
320 =============
321 Draw_AlphaPic
322 =============
323 */
324 void Draw_AlphaPic (int x, int y, qpic_t *pic, float alpha)
325 {
326         Draw_GenericPic(((glpic_t *)pic->data)->tex, 1,1,1,alpha, x,y,pic->width, pic->height);
327 }
328
329
330 /*
331 =============
332 Draw_Pic
333 =============
334 */
335 void Draw_Pic (int x, int y, qpic_t *pic)
336 {
337         Draw_GenericPic(((glpic_t *)pic->data)->tex, 1,1,1,1, x,y,pic->width, pic->height);
338 }
339
340
341 /*
342 =============
343 Draw_PicTranslate
344
345 Only used for the player color selection menu
346 =============
347 */
348 void Draw_PicTranslate (int x, int y, qpic_t *pic, byte *translation)
349 {
350         int                             i, c;
351         byte                    *trans, *src, *dest;
352         rtexture_t              *rt;
353
354         c = pic->width * pic->height;
355         src = menuplyr_pixels;
356         dest = trans = qmalloc(c);
357         for (i = 0;i < c;i++)
358                 *dest++ = translation[*src++];
359
360         rt = R_LoadTexture ("translatedplayerpic", pic->width, pic->height, trans, TEXF_ALPHA | TEXF_PRECACHE);
361         qfree(trans);
362
363         if (!r_render.value)
364                 return;
365         Draw_GenericPic (rt, 1,1,1,1, x, y, pic->width, pic->height);
366 }
367
368
369 /*
370 ================
371 Draw_ConsoleBackground
372
373 ================
374 */
375 void Draw_ConsoleBackground (int lines)
376 {
377         Draw_GenericPic (conbacktex, 1,1,1,scr_conalpha.value*lines/vid.height, 0, lines - vid.height, vid.width, vid.height);
378         // LordHavoc: draw version
379         Draw_String(engineversionx, lines - vid.height + engineversiony, engineversion, 9999);
380 }
381
382 /*
383 =============
384 Draw_Fill
385
386 Fills a box of pixels with a single color
387 =============
388 */
389 void Draw_Fill (int x, int y, int w, int h, int c)
390 {
391         if (!r_render.value)
392                 return;
393         glDisable (GL_TEXTURE_2D);
394         glColor3f (host_basepal[c*3]/255.0, host_basepal[c*3+1]/255.0, host_basepal[c*3+2]/255.0);
395
396         glBegin (GL_QUADS);
397
398         glVertex2f (x,y);
399         glVertex2f (x+w, y);
400         glVertex2f (x+w, y+h);
401         glVertex2f (x, y+h);
402
403         glEnd ();
404         glColor3f(1,1,1);
405         glEnable (GL_TEXTURE_2D);
406 }
407 //=============================================================================
408
409 //=============================================================================
410
411 /*
412 ================
413 GL_Set2D
414
415 Setup as if the screen was 320*200
416 ================
417 */
418 void GL_Set2D (void)
419 {
420         if (!r_render.value)
421                 return;
422         glViewport (glx, gly, glwidth, glheight);
423
424         glMatrixMode(GL_PROJECTION);
425     glLoadIdentity ();
426         glOrtho  (0, vid.width, vid.height, 0, -99999, 99999);
427
428         glMatrixMode(GL_MODELVIEW);
429     glLoadIdentity ();
430
431         glDisable (GL_DEPTH_TEST);
432         glDisable (GL_CULL_FACE);
433         glEnable (GL_BLEND);
434         glDisable (GL_ALPHA_TEST);
435         glEnable(GL_TEXTURE_2D);
436
437         // LordHavoc: added this
438         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
439         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
440
441         glColor3f(1,1,1);
442 }
443
444 // LordHavoc: SHOWLMP stuff
445 #define SHOWLMP_MAXLABELS 256
446 typedef struct showlmp_s
447 {
448         qboolean        isactive;
449         float           x;
450         float           y;
451         char            label[32];
452         char            pic[128];
453 } showlmp_t;
454
455 showlmp_t showlmp[SHOWLMP_MAXLABELS];
456
457 void SHOWLMP_decodehide()
458 {
459         int i;
460         byte *lmplabel;
461         lmplabel = MSG_ReadString();
462         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
463                 if (showlmp[i].isactive && strcmp(showlmp[i].label, lmplabel) == 0)
464                 {
465                         showlmp[i].isactive = false;
466                         return;
467                 }
468 }
469
470 void SHOWLMP_decodeshow()
471 {
472         int i, k;
473         byte lmplabel[256], picname[256];
474         float x, y;
475         strcpy(lmplabel,MSG_ReadString());
476         strcpy(picname, MSG_ReadString());
477         if (nehahra) // LordHavoc: nasty old legacy junk
478         {
479                 x = MSG_ReadByte();
480                 y = MSG_ReadByte();
481         }
482         else
483         {
484                 x = MSG_ReadShort();
485                 y = MSG_ReadShort();
486         }
487         k = -1;
488         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
489                 if (showlmp[i].isactive)
490                 {
491                         if (strcmp(showlmp[i].label, lmplabel) == 0)
492                         {
493                                 k = i;
494                                 break; // drop out to replace it
495                         }
496                 }
497                 else if (k < 0) // find first empty one to replace
498                         k = i;
499         if (k < 0)
500                 return; // none found to replace
501         // change existing one
502         showlmp[k].isactive = true;
503         strcpy(showlmp[k].label, lmplabel);
504         strcpy(showlmp[k].pic, picname);
505         showlmp[k].x = x;
506         showlmp[k].y = y;
507 }
508
509 void SHOWLMP_drawall()
510 {
511         int i;
512         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
513                 if (showlmp[i].isactive)
514                         Draw_Pic(showlmp[i].x, showlmp[i].y, Draw_CachePic(showlmp[i].pic));
515 }
516
517 void SHOWLMP_clear()
518 {
519         int i;
520         for (i = 0;i < SHOWLMP_MAXLABELS;i++)
521                 showlmp[i].isactive = false;
522 }