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