]> icculus.org git repositories - btb/d2x.git/blob - main/hud.c
whitespace
[btb/d2x.git] / main / hud.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 /*
15  * $Source: /cvs/cvsroot/d2x/main/hud.c,v $
16  * $Revision: 1.4 $
17  * $Author: btb $
18  * $Date: 2002-10-10 19:08:15 $
19  *
20  * Routines for displaying HUD messages...
21  *
22  * $Log: not supported by cvs2svn $
23  * Revision 1.3  2001/11/04 09:00:25  bradleyb
24  * Enable d1x-style hud_message
25  *
26  *
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include <conf.h>
31 #endif
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <ctype.h>
37
38 #include "hudmsg.h"
39
40 #include "pstypes.h"
41 #include "u_mem.h"
42 #include "strutil.h"
43 #include "console.h"
44 #include "inferno.h"
45 #include "game.h"
46 #include "screens.h"
47 #include "gauges.h"
48 #include "physics.h"
49 #include "error.h"
50
51 #include "menu.h"                       // For the font.
52 #include "mono.h"
53 #include "collide.h"
54 #include "newdemo.h"
55 #include "player.h"
56 #include "gamefont.h"
57
58 #include "wall.h"
59 #include "screens.h"
60 #include "text.h"
61 #include "laser.h"
62 #include "args.h"
63 #include "pa_enabl.h"
64
65 int hud_first = 0;
66 int hud_last = 0;
67
68 int     HUD_nmessages = 0;
69 fix     HUD_message_timer = 0;          // Time, relative to Players[Player_num].time (int.frac seconds.frac), at which to erase gauge message
70 char  HUD_messages[HUD_MAX_NUM][HUD_MESSAGE_LENGTH+5];
71
72 extern void copy_background_rect(int left,int top,int right,int bot);
73 char Displayed_background_message[2][HUD_MESSAGE_LENGTH] = {"",""};
74 int     Last_msg_ycrd = -1;
75 int     Last_msg_height = 6;
76 int     HUD_color = -1;
77
78 int     MSG_Playermessages = 0;
79 int     MSG_Noredundancy = 0;
80
81 int     Modex_hud_msg_count;
82
83 #define LHX(x)          ((x)*(FontHires?2:1))
84 #define LHY(y)          ((y)*(FontHires?2.4:1))
85
86 #ifdef WINDOWS
87 int extra_clear=0;
88 #endif
89
90 //      -----------------------------------------------------------------------------
91 void clear_background_messages(void)
92 {
93         #ifdef WINDOWS
94         if (extra_clear == FrameCount)          //don't do extra clear on same frame
95                 return;
96         #endif
97
98 #ifdef WINDOWS
99         if (((Cockpit_mode == CM_STATUS_BAR) || (Cockpit_mode == CM_FULL_SCREEN)) && (Last_msg_ycrd != -1) && (dd_VR_render_sub_buffer[0].yoff >= 6)) {
100                 dd_grs_canvas *canv_save = dd_grd_curcanv;
101 #else
102         if (((Cockpit_mode == CM_STATUS_BAR) || (Cockpit_mode == CM_FULL_SCREEN)) && (Last_msg_ycrd != -1) && (VR_render_sub_buffer[0].cv_bitmap.bm_y >= 6)) {
103                 grs_canvas      *canv_save = grd_curcanv;
104 #endif    
105
106                 WINDOS( dd_gr_set_current_canvas(get_current_game_screen()),
107                                 gr_set_current_canvas(get_current_game_screen())
108                 );
109
110                 PA_DFX (pa_set_frontbuffer_current());
111                 PA_DFX (copy_background_rect(0, Last_msg_ycrd, grd_curcanv->cv_bitmap.bm_w, Last_msg_ycrd+Last_msg_height-1));
112                 PA_DFX (pa_set_backbuffer_current());
113                 copy_background_rect(0, Last_msg_ycrd, grd_curcanv->cv_bitmap.bm_w, Last_msg_ycrd+Last_msg_height-1);
114           
115                 WINDOS( 
116                         dd_gr_set_current_canvas(canv_save),
117                         gr_set_current_canvas(canv_save)
118                 );
119
120                 #ifdef WINDOWS
121                 if (extra_clear || !GRMODEINFO(modex)) {
122                         extra_clear = 0;
123                         Last_msg_ycrd = -1;
124                 }
125                 else
126                         extra_clear = FrameCount;
127                 #else
128                 Last_msg_ycrd = -1;
129                 #endif
130         }
131
132         Displayed_background_message[VR_current_page][0] = 0;
133
134 }
135
136 void HUD_clear_messages()
137 {
138         int i;
139         HUD_nmessages = 0;
140         hud_first = hud_last = 0;
141         HUD_message_timer = 0;
142         clear_background_messages();
143         for (i = 0; i < HUD_MAX_NUM; i++)
144                 sprintf(HUD_messages[i], "SlagelSlagel!!");
145 }
146
147
148 extern int Guided_in_big_window;
149 extern int max_window_h;
150
151 extern grs_canvas *print_to_canvas(char *s,grs_font *font, int fc, int bc, int double_flag);
152
153 //      -----------------------------------------------------------------------------
154 //      print to buffer, double heights, and blit bitmap to screen
155 void modex_hud_message(int x, int y, char *s, grs_font *font, int color)
156 {
157         grs_canvas *temp_canv;
158
159         temp_canv = print_to_canvas(s, font, color, -1, 1);
160
161         gr_bitmapm(x,y,&temp_canv->cv_bitmap);
162
163         gr_free_canvas(temp_canv);
164 }
165
166 extern int max_window_w;
167
168 //      -----------------------------------------------------------------------------
169 //      Writes a message on the HUD and checks its timer.
170 void HUD_render_message_frame()
171 {
172         int i, y,n;
173         int h,w,aw;
174
175         if (( HUD_nmessages < 0 ) || (HUD_nmessages > HUD_MAX_NUM))
176                 Int3(); // Get Rob!
177
178         if ( (HUD_nmessages < 1 ) && (Modex_hud_msg_count == 0))
179                 return;
180
181         HUD_message_timer -= FrameTime;
182
183         #ifdef WINDOWS
184         if (extra_clear)
185                 clear_background_messages();                    //      If in status bar mode and no messages, then erase.
186         #endif
187
188         if ( HUD_message_timer < 0 )    {
189                 // Timer expired... get rid of oldest message...
190                 if (hud_last!=hud_first)        {
191                         int     temp;
192
193                         //&HUD_messages[hud_first][0] is deing deleted...;
194                         hud_first = (hud_first+1) % HUD_MAX_NUM;
195                         HUD_message_timer = F1_0*2;
196                         HUD_nmessages--;
197                         if (HUD_nmessages == 0)
198                                 Modex_hud_msg_count = 2;
199                         temp = Last_msg_ycrd;
200                         clear_background_messages();                    //      If in status bar mode and no messages, then erase.
201                         if (Modex_hud_msg_count)
202                                 Last_msg_ycrd = temp;
203                 }
204         }
205
206         if (HUD_nmessages > 0 ) {
207
208                 if (HUD_color == -1)
209                         HUD_color = BM_XRGB(0,28,0);
210
211         #ifdef WINDOWS
212                 if ( (VR_render_mode==VR_NONE) && ((Cockpit_mode == CM_STATUS_BAR) || (Cockpit_mode == CM_FULL_SCREEN)) && (dd_VR_render_sub_buffer[0].yoff >= (max_window_h/8))) {
213         #else
214                 if ( (VR_render_mode==VR_NONE) && ((Cockpit_mode == CM_STATUS_BAR) || (Cockpit_mode == CM_FULL_SCREEN)) && (VR_render_sub_buffer[0].cv_bitmap.bm_y >= (max_window_h/8))) {
215         #endif
216                         // Only display the most recent message in this mode
217                         char    *message = HUD_messages[(hud_first+HUD_nmessages-1) % HUD_MAX_NUM];
218
219                         if (strcmp(Displayed_background_message[VR_current_page], message)) {
220                                 int     ycrd;
221                                 WINDOS(
222                                         dd_grs_canvas *canv_save = dd_grd_curcanv,
223                                         grs_canvas      *canv_save = grd_curcanv
224                                 );
225
226                                 #ifdef MACINTOSH
227                                 if (Scanline_double)
228                                         FontHires=1;    // always display hires font outside of display
229                                 #endif
230
231                                 WINDOS(
232                                         ycrd = dd_grd_curcanv->yoff - (SMALL_FONT->ft_h+2),
233                                         ycrd = grd_curcanv->cv_bitmap.bm_y - (SMALL_FONT->ft_h+2)
234                                 );
235
236                                 if (ycrd < 0)
237                                         ycrd = 0;
238
239                                 WINDOS(
240                                         dd_gr_set_current_canvas(get_current_game_screen()),
241                                         gr_set_current_canvas(get_current_game_screen())
242                                 );
243
244                                 gr_set_curfont( SMALL_FONT );
245                                 gr_get_string_size(message, &w, &h, &aw );
246                                 clear_background_messages();
247
248
249                                 if (grd_curcanv->cv_bitmap.bm_type == BM_MODEX) {
250                                         WIN(Int3());    // No no no no ....
251                                         ycrd -= h;
252                                         h *= 2;
253                                         modex_hud_message((grd_curcanv->cv_bitmap.bm_w-w)/2, ycrd, message, SMALL_FONT, HUD_color);
254                                         if (Modex_hud_msg_count > 0) {
255                                                 Modex_hud_msg_count--;
256                                                 Displayed_background_message[VR_current_page][0] = '!';
257                                         } else
258                                                 strcpy(Displayed_background_message[VR_current_page], message);
259                                 } else {
260                                 WIN(DDGRLOCK(dd_grd_curcanv));
261                                         gr_set_fontcolor( HUD_color, -1);
262                                         PA_DFX (pa_set_frontbuffer_current());
263                                         PA_DFX (gr_printf((grd_curcanv->cv_bitmap.bm_w-w)/2, ycrd, message ));
264                                         PA_DFX (pa_set_backbuffer_current());
265                                         gr_printf((grd_curcanv->cv_bitmap.bm_w-w)/2, ycrd, message );
266                                         strcpy(Displayed_background_message[VR_current_page], message);
267                                 WIN(DDGRUNLOCK(dd_grd_curcanv));
268                                 }
269
270                         WINDOS(
271                                 dd_gr_set_current_canvas(canv_save),
272                                 gr_set_current_canvas(canv_save)
273                         );
274
275                                 Last_msg_ycrd = ycrd;
276                                 Last_msg_height = h;
277
278                                 #ifdef MACINTOSH
279                                 if (Scanline_double)
280                                         FontHires=0;    // always display hires font outside of display
281                                 #endif
282
283                         }
284                 } else {
285
286                         gr_set_curfont( SMALL_FONT );
287
288                         if ( (Cockpit_mode == CM_FULL_SCREEN) || (Cockpit_mode == CM_LETTERBOX) ) {
289                                 if (Game_window_w == max_window_w)
290                                         y = SMALL_FONT->ft_h/2;
291                                 else
292                                         y= SMALL_FONT->ft_h * 2;
293                         } else
294                                 y = SMALL_FONT->ft_h/2;
295
296                   if (Guided_missile[Player_num] && Guided_missile[Player_num]->type==OBJ_WEAPON && Guided_missile[Player_num]->id==GUIDEDMISS_ID &&
297                       Guided_missile[Player_num]->signature==Guided_missile_sig[Player_num] && Guided_in_big_window)
298                               y+=SMALL_FONT->ft_h+3; 
299
300                 WIN(DDGRLOCK(dd_grd_curcanv));
301                         for (i=0; i<HUD_nmessages; i++ )        {       
302                                 n = (hud_first+i) % HUD_MAX_NUM;
303                                 if ((n < 0) || (n >= HUD_MAX_NUM))
304                                         Int3(); // Get Rob!!
305                                 if (!strcmp(HUD_messages[n], "This is a bug."))
306                                         Int3(); // Get Rob!!
307                                 gr_get_string_size(&HUD_messages[n][0], &w, &h, &aw );
308                                 gr_set_fontcolor( HUD_color, -1);
309                         
310                                 PA_DFX (pa_set_frontbuffer_current());
311                                 PA_DFX(gr_string((grd_curcanv->cv_bitmap.bm_w-w)/2,y, &HUD_messages[n][0] ));
312                                 PA_DFX (pa_set_backbuffer_current());
313                                 gr_string((grd_curcanv->cv_bitmap.bm_w-w)/2,y, &HUD_messages[n][0] );
314                                 y += h+1;
315                         }
316                 WIN(DDGRUNLOCK(dd_grd_curcanv));
317                 }
318         }
319         #ifndef WINDOWS
320         else if (get_current_game_screen()->cv_bitmap.bm_type == BM_MODEX) {
321                 if (Modex_hud_msg_count) {
322                         int     temp = Last_msg_ycrd;
323                         Modex_hud_msg_count--;
324                         clear_background_messages();                    //      If in status bar mode and no messages, then erase.
325                         Last_msg_ycrd = temp;
326                 }
327         }
328         #endif
329
330         gr_set_curfont( GAME_FONT );
331 }
332
333 int PlayerMessage=1;
334
335 // Call to flash a message on the HUD.  Returns true if message drawn.
336 //  (message might not be drawn if previous message was same)
337 int HUD_init_message(char * format, ... )
338 {
339         va_list args;
340         int temp, temp2;
341         char *message = NULL;
342         char *last_message=NULL;
343         char *cleanmessage;
344
345         Modex_hud_msg_count = 2;
346
347         if ( (hud_last < 0) || (hud_last >= HUD_MAX_NUM))
348                 Int3(); // Get Rob!!
349
350         // -- mprintf((0, "message timer: %7.3f\n", f2fl(HUD_message_timer)));
351         va_start(args, format );
352         message = &HUD_messages[hud_last][0];
353         vsprintf(message,format,args);
354         va_end(args);
355         
356         /* Produce a sanitised version and send it to the console */
357         cleanmessage = d_strdup(message);
358         for (temp=0,temp2=0; message[temp]!=0; temp++)
359         {
360                 if (isprint(message[temp])) cleanmessage[temp2++] = message[temp];
361                 else temp++; /* Skip next character as well */
362         }
363         cleanmessage[temp2] = 0;
364         con_printf(CON_NORMAL, "%s\n", message);
365         d_free(cleanmessage);
366
367         // Added by Leighton 
368    
369    if ((Game_mode & GM_MULTI) && FindArg("-noredundancy"))
370          if (!strnicmp ("You already",message,11))
371                 return 0;
372
373    if ((Game_mode & GM_MULTI) && FindArg("-PlayerMessages") && PlayerMessage==0)
374                 return 0;
375   
376         if (HUD_nmessages > 0)  {
377                 if (hud_last==0)
378                         last_message = &HUD_messages[HUD_MAX_NUM-1][0];
379                 else
380                         last_message = &HUD_messages[hud_last-1][0];
381         }
382
383         temp = (hud_last+1) % HUD_MAX_NUM;
384
385         if ( temp==hud_first )  {
386                 // If too many messages, remove oldest message to make room
387                 hud_first = (hud_first+1) % HUD_MAX_NUM;
388                 HUD_nmessages--;
389         }
390
391         if (last_message && (!strcmp(last_message, message))) {
392                 HUD_message_timer = F1_0*3;             // 1 second per 5 characters
393                 return 0;       // ignore since it is the same as the last one
394         }
395
396         hud_last = temp;
397         // Check if memory has been overwritten at this point.
398         if (strlen(message) >= HUD_MESSAGE_LENGTH)
399                 Error( "Your message to HUD is too long.  Limit is %i characters.\n", HUD_MESSAGE_LENGTH);
400         #ifdef NEWDEMO
401         if (Newdemo_state == ND_STATE_RECORDING )
402                 newdemo_record_hud_message( message );
403         #endif
404         HUD_message_timer = F1_0*3;             // 1 second per 5 characters
405         HUD_nmessages++;
406
407         return 1;
408 }
409
410
411 //@@void player_dead_message(void)
412 //@@{
413 //@@    if (!Arcade_mode && Player_exploded) { //(ConsoleObject->flags & OF_EXPLODING)) {
414 //@@            gr_set_curfont( SMALL_FONT );    
415 //@@            if (HUD_color == -1)
416 //@@                    HUD_color = BM_XRGB(0,28,0);
417 //@@            gr_set_fontcolor( HUD_color, -1);
418 //@@
419 //@@            gr_printf(0x8000, grd_curcanv->cv_bitmap.bm_h-8, TXT_PRESS_ANY_KEY);
420 //@@            gr_set_curfont( GAME_FONT );    
421 //@@    }
422 //@@
423 //@@}
424
425 void player_dead_message(void)
426 {
427     if (Player_exploded) {      
428         if ( Players[Player_num].lives < 2 )    {
429             int x, y, w, h, aw;
430             gr_set_curfont( HUGE_FONT );    
431             gr_get_string_size( TXT_GAME_OVER, &w, &h, &aw );
432             w += 20;
433             h += 8;
434             x = (grd_curcanv->cv_w - w ) / 2;
435             y = (grd_curcanv->cv_h - h ) / 2;
436             
437             NO_DFX (Gr_scanline_darkening_level = 2*7);
438             NO_DFX (gr_setcolor( BM_XRGB(0,0,0) ));
439             NO_DFX (gr_rect( x, y, x+w, y+h ));
440             Gr_scanline_darkening_level = GR_FADE_LEVELS;
441
442             gr_string(0x8000, (grd_curcanv->cv_h - grd_curcanv->cv_font->ft_h)/2 + h/8, TXT_GAME_OVER );
443
444 #if 0
445             // Automatically exit death after 10 secs
446             if ( GameTime > Player_time_of_death + F1_0*10 ) {
447                     Function_mode = FMODE_MENU;
448                     Game_mode = GM_GAME_OVER;
449                     longjmp( LeaveGame, 1 );        // Exit out of game loop
450             }
451 #endif
452
453         } 
454         gr_set_curfont( GAME_FONT );    
455         if (HUD_color == -1)
456             HUD_color = BM_XRGB(0,28,0);
457         gr_set_fontcolor( HUD_color, -1);
458         gr_string(0x8000, grd_curcanv->cv_h-(grd_curcanv->cv_font->ft_h+3), TXT_PRESS_ANY_KEY);
459     }
460 }
461
462 // void say_afterburner_status(void)
463 // {
464 //      if (Players[Player_num].flags & PLAYER_FLAGS_AFTERBURNER)
465 //              HUD_init_message("Afterburner engaged.");
466 //      else
467 //              HUD_init_message("Afterburner disengaged.");
468 // }
469
470 void hud_message(int class, char *format, ...)
471 {
472  va_list vp;
473  va_start(vp, format);
474   if ((!MSG_Noredundancy || (class & MSGC_NOREDUNDANCY)) &&
475       (!MSG_Playermessages || !(Game_mode & GM_MULTI) ||
476       (class & MSGC_PLAYERMESSAGES)))
477    HUD_init_message(format, vp);
478  va_end(vp);
479 }
480