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