1d6cb5a1ab3003ea90acd94417cbb426bde7bc74
[btb/d2x.git] / main / gamerend.c
1 /* $Id: gamerend.c,v 1.20 2005-08-07 09:58:12 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  * Stuff for rendering the HUD
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <conf.h>
23 #endif
24
25 #ifdef RCS
26 static char rcsid[] = "$Id: gamerend.c,v 1.20 2005-08-07 09:58:12 chris Exp $";
27 #endif
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32
33 #include "pstypes.h"
34 #include "console.h"
35 #include "inferno.h"
36 #include "error.h"
37 #include "mono.h"
38 #include "gr.h"
39 #include "palette.h"
40 #include "ibitblt.h"
41 #include "bm.h"
42 #include "player.h"
43 #include "render.h"
44 #include "menu.h"
45 #include "newmenu.h"
46 #include "screens.h"
47 #include "fix.h"
48 #include "robot.h"
49 #include "game.h"
50 #include "gauges.h"
51 #include "gamefont.h"
52 #include "newdemo.h"
53 #include "text.h"
54 #include "multi.h"
55 #include "endlevel.h"
56 #include "cntrlcen.h"
57 #include "powerup.h"
58 #include "laser.h"
59 #include "playsave.h"
60 #include "automap.h"
61 #include "mission.h"
62 #include "gameseq.h"
63
64 #ifdef OGL
65 #include "ogl_init.h"
66 #endif
67
68 extern fix Cruise_speed;
69 extern int LinearSVGABuffer;
70 extern int Current_display_mode;
71 extern cvar_t r_framerate;
72
73
74 #ifndef NDEBUG
75 extern int Debug_pause;                         //John's debugging pause system
76 #endif
77
78 #ifndef RELEASE
79 extern int Saving_movie_frames;
80 #else
81 #define Saving_movie_frames 0
82 #endif
83
84 // Returns the length of the first 'n' characters of a string.
85 int string_width( char * s, int n )
86 {
87         int w,h,aw;
88         char p;
89         p = s[n];
90         s[n] = 0;
91         gr_get_string_size( s, &w, &h, &aw );
92         s[n] = p;
93         return w;
94 }
95
96 // Draw string 's' centered on a canvas... if wider than
97 // canvas, then wrap it.
98 void draw_centered_text( int y, char * s )
99 {
100         int i, l;
101         char p;
102
103         l = strlen(s);
104
105         if ( string_width( s, l ) < grd_curcanv->cv_bitmap.bm_w )       {
106                 gr_string( 0x8000, y, s );
107                 return;
108         }
109
110         for (i=0; i<l; i++ )    {
111                 if ( string_width(s,i) > (grd_curcanv->cv_bitmap.bm_w - 16) )   {
112                         p = s[i];
113                         s[i] = 0;
114                         gr_string( 0x8000, y, s );
115                         s[i] = p;
116                         gr_string( 0x8000, y+grd_curcanv->cv_font->ft_h+1, &s[i] );
117                         return;
118                 }
119         }
120 }
121
122 extern ubyte DefiningMarkerMessage;
123 extern char Marker_input[];
124
125 #define MAX_MARKER_MESSAGE_LEN 120
126 void game_draw_marker_message()
127 {
128         char temp_string[MAX_MARKER_MESSAGE_LEN+25];
129
130         if ( DefiningMarkerMessage)
131           {
132                 gr_set_curfont( GAME_FONT );    //GAME_FONT 
133                 gr_set_fontcolor(gr_getcolor(0,63,0), -1 );
134                 sprintf( temp_string, "Marker: %s_", Marker_input );
135                 draw_centered_text(grd_curcanv->cv_bitmap.bm_h/2-16, temp_string );
136           }
137
138 }
139
140 #ifdef NETWORK
141 void game_draw_multi_message()
142 {
143         char temp_string[MAX_MULTI_MESSAGE_LEN+25];
144
145         if ( (Game_mode&GM_MULTI) && (multi_sending_message))   {
146                 gr_set_curfont( GAME_FONT );    //GAME_FONT );
147                 gr_set_fontcolor(gr_getcolor(0,63,0), -1 );
148                 sprintf( temp_string, "%s: %s_", TXT_MESSAGE, Network_message );
149                 draw_centered_text(grd_curcanv->cv_bitmap.bm_h/2-16, temp_string );
150
151         }
152
153         if ( (Game_mode&GM_MULTI) && (multi_defining_message))  {
154                 gr_set_curfont( GAME_FONT );    //GAME_FONT );
155                 gr_set_fontcolor(gr_getcolor(0,63,0), -1 );
156                 sprintf( temp_string, "%s #%d: %s_", TXT_MACRO, multi_defining_message, Network_message );
157                 draw_centered_text(grd_curcanv->cv_bitmap.bm_h/2-16, temp_string );
158         }
159 }
160 #endif
161
162 //these should be in gr.h 
163 #define cv_w  cv_bitmap.bm_w
164 #define cv_h  cv_bitmap.bm_h
165
166 fix frame_time_list[8] = {0,0,0,0,0,0,0,0};
167 fix frame_time_total=0;
168 int frame_time_cntr=0;
169
170 void ftoa(char *string, fix f)
171 {
172         int decimal, fractional;
173         
174         decimal = f2i(f);
175         fractional = ((f & 0xffff)*100)/65536;
176         if (fractional < 0 )
177                 fractional *= -1;
178         if (fractional > 99 ) fractional = 99;
179         sprintf( string, "%d.%02d", decimal, fractional );
180 }
181
182 void show_framerate()
183 {
184         char temp[50];
185    //static int q;
186
187         fix rate;
188         int x = 8, y = 5; // position measured from lower right corner
189
190         frame_time_total += RealFrameTime - frame_time_list[frame_time_cntr];
191         frame_time_list[frame_time_cntr] = RealFrameTime;
192         frame_time_cntr = (frame_time_cntr+1)%8;
193
194         rate = fixdiv(f1_0*8,frame_time_total);
195
196         gr_set_curfont( GAME_FONT );    
197         gr_set_fontcolor(gr_getcolor(0,31,0),-1 );
198
199         ftoa( temp, rate );     // Convert fixed to string
200         if (Game_mode & GM_MULTI)
201                 y = 7;
202         gr_printf(grd_curcanv->cv_w-(x*GAME_FONT->ft_w),grd_curcanv->cv_h-y*(GAME_FONT->ft_h+GAME_FONT->ft_h/4),"FPS: %s ", temp );
203 //   if ( !( q++ % 30 ) )
204 //      mprintf( (0,"fps: %s\n", temp ) );
205 }
206
207 #ifndef NDEBUG
208
209 fix Show_view_text_timer = -1;
210
211 void draw_window_label()
212 {
213         if ( Show_view_text_timer > 0 )
214         {
215                 char *viewer_name,*control_name;
216                 char    *viewer_id;
217                 Show_view_text_timer -= FrameTime;
218                 gr_set_curfont( GAME_FONT );
219
220                 viewer_id = "";
221                 switch( Viewer->type )
222                 {
223                         case OBJ_FIREBALL:      viewer_name = "Fireball"; break;
224                         case OBJ_ROBOT:         viewer_name = "Robot";
225 #ifdef EDITOR
226                                                                                 viewer_id = Robot_names[Viewer->id];
227 #endif
228                                 break;
229                         case OBJ_HOSTAGE:               viewer_name = "Hostage"; break;
230                         case OBJ_PLAYER:                viewer_name = "Player"; break;
231                         case OBJ_WEAPON:                viewer_name = "Weapon"; break;
232                         case OBJ_CAMERA:                viewer_name = "Camera"; break;
233                         case OBJ_POWERUP:               viewer_name = "Powerup";
234 #ifdef EDITOR
235                                                                                 viewer_id = Powerup_names[Viewer->id];
236 #endif
237                                 break;
238                         case OBJ_DEBRIS:                viewer_name = "Debris"; break;
239                         case OBJ_CNTRLCEN:      viewer_name = "Reactor"; break;
240                         default:                                        viewer_name = "Unknown"; break;
241                 }
242
243                 switch ( Viewer->control_type) {
244                         case CT_NONE:                   control_name = "Stopped"; break;
245                         case CT_AI:                             control_name = "AI"; break;
246                         case CT_FLYING:         control_name = "Flying"; break;
247                         case CT_SLEW:                   control_name = "Slew"; break;
248                         case CT_FLYTHROUGH:     control_name = "Flythrough"; break;
249                         case CT_MORPH:                  control_name = "Morphing"; break;
250                         default:                                        control_name = "Unknown"; break;
251                 }
252
253                 gr_set_fontcolor( gr_getcolor(31, 0, 0), -1 );
254                 gr_printf( 0x8000, 45, "%i: %s [%s] View - %s",Viewer-Objects, viewer_name, viewer_id, control_name );
255
256         }
257 }
258 #endif
259
260 extern int Game_window_x;
261 extern int Game_window_y;
262 extern int Game_window_w;
263 extern int Game_window_h;
264 extern int max_window_w;
265 extern int max_window_h;
266
267 void render_countdown_gauge()
268 {
269         if (!Endlevel_sequence && Control_center_destroyed  && (Countdown_seconds_left>-1)) { // && (Countdown_seconds_left<127))       {
270                 int     y;
271
272                 if (!is_D2_OEM && !is_MAC_SHARE && !is_SHAREWARE)    // no countdown on registered only
273                 {
274                         //      On last level, we don't want a countdown.
275                         if (PLAYING_BUILTIN_MISSION && Current_level_num == Last_level)
276                         {
277                                 if (!(Game_mode & GM_MULTI))
278                                         return;
279                                 if (Game_mode & GM_MULTI_ROBOTS)
280                                         return;
281                         }
282                 }
283
284                 gr_set_curfont( SMALL_FONT );
285                 gr_set_fontcolor(gr_getcolor(0,63,0), -1 );
286                 y = SMALL_FONT->ft_h*4;
287                 if (Cockpit_mode == CM_FULL_SCREEN)
288                         y += SMALL_FONT->ft_h*2;
289
290                 if (Player_is_dead)
291                         y += SMALL_FONT->ft_h*2;
292
293                 //if (!((Cockpit_mode == CM_STATUS_BAR) && (Game_window_y >= 19)))
294                 //      y += 5;
295                 gr_printf(0x8000, y, "T-%d s", Countdown_seconds_left );
296         }
297 }
298
299 void game_draw_hud_stuff()
300 {
301         //mprintf ((0,"Linear is %d!\n",LinearSVGABuffer));
302         
303         #ifndef NDEBUG
304         if (Debug_pause) {
305                 gr_set_curfont( MEDIUM1_FONT );
306                 gr_set_fontcolor( gr_getcolor(31, 31, 31), -1 ); // gr_getcolor(31,0,0));
307                 gr_ustring( 0x8000, 85/2, "Debug Pause - Press P to exit" );
308         }
309         #endif
310
311         #ifndef NDEBUG
312         draw_window_label();
313         #endif
314
315 #ifdef NETWORK
316         game_draw_multi_message();
317 #endif
318
319    game_draw_marker_message();
320
321 //   if (Game_mode & GM_MULTI)
322 //    {
323 //     if (Netgame.PlayTimeAllowed)
324 //       game_draw_time_left ();
325 //  }
326
327         if ((Newdemo_state == ND_STATE_PLAYBACK) || (Newdemo_state == ND_STATE_RECORDING)) {
328                 char message[128];
329                 int h,w,aw;
330
331                 if (Newdemo_state == ND_STATE_PLAYBACK) {
332                         if (Newdemo_vcr_state != ND_STATE_PRINTSCREEN) {
333                                 sprintf(message, "%s (%d%%%% %s)", TXT_DEMO_PLAYBACK, newdemo_get_percent_done(), TXT_DONE);
334                         } else {
335                                 sprintf (message, " ");
336                         }
337                 } else 
338                         sprintf (message, TXT_DEMO_RECORDING);
339
340                 gr_set_curfont( GAME_FONT );    //GAME_FONT );
341                 gr_set_fontcolor(gr_getcolor(27,0,0), -1 );
342
343                 gr_get_string_size(message, &w, &h, &aw );
344                 if (Cockpit_mode == CM_FULL_COCKPIT) {
345                         if (grd_curcanv->cv_bitmap.bm_h > 240)
346                                 h += 40;
347                         else
348                                 h += 15;
349                 } else if ( Cockpit_mode == CM_LETTERBOX )
350                         h += 7;
351                 if (Cockpit_mode != CM_REAR_VIEW && !Saving_movie_frames)
352                         gr_printf((grd_curcanv->cv_bitmap.bm_w-w)/2, grd_curcanv->cv_bitmap.bm_h - h - 2, message );
353         }
354
355         render_countdown_gauge();
356
357         if ( Player_num > -1 && Viewer->type==OBJ_PLAYER && Viewer->id==Player_num )    {
358                 int     x = 3;
359                 int     y = grd_curcanv->cv_bitmap.bm_h;
360
361                 gr_set_curfont( GAME_FONT );
362                 gr_set_fontcolor( gr_getcolor(0, 31, 0), -1 );
363                 if (Cruise_speed > 0) {
364                         int line_spacing = GAME_FONT->ft_h + GAME_FONT->ft_h/4;
365
366 mprintf((0,"line_spacing=%d ",line_spacing));
367
368                         if (Cockpit_mode==CM_FULL_SCREEN) {
369                                 if (Game_mode & GM_MULTI)
370                                         y -= line_spacing * 11; //64
371                                 else
372                                         y -= line_spacing * 6;  //32
373                         } else if (Cockpit_mode == CM_STATUS_BAR) {
374                                 if (Game_mode & GM_MULTI)
375                                         y -= line_spacing * 8;  //48
376                                 else
377                                         y -= line_spacing * 4;  //24
378                         } else {
379                                 y = line_spacing * 2;   //12
380                                 x = 20+2;
381                         }
382
383                         gr_printf( x, y, "%s %2d%%", TXT_CRUISE, f2i(Cruise_speed) );
384                 }
385         }
386
387         if (r_framerate.value)
388                 show_framerate();
389
390         if ( (Newdemo_state == ND_STATE_PLAYBACK) )
391                 Game_mode = Newdemo_game_mode;
392
393         draw_hud();
394
395         if ( (Newdemo_state == ND_STATE_PLAYBACK) )
396                 Game_mode = GM_NORMAL;
397
398         if ( Player_is_dead )
399                 player_dead_message();
400 }
401
402 extern int gr_bitblt_dest_step_shift;
403 extern int gr_wait_for_retrace;
404 extern int gr_bitblt_double;
405
406 #if 0
407 void expand_row(ubyte * dest, ubyte * src, int num_src_pixels );
408 #pragma aux expand_row parm [edi] [esi] [ecx] modify exact [ecx esi edi eax ebx] = \
409         "add    esi, ecx"                       \
410         "dec    esi"                                    \
411         "add    edi, ecx"                       \
412         "add    edi, ecx"                       \
413         "dec    edi"                                    \
414         "dec    edi"                                    \
415 "nextpixel:"                                    \
416         "mov    al,[esi]"                       \
417         "mov    ah, al"                         \
418         "dec    esi"                                    \
419         "mov    [edi], ax"                      \
420         "dec    edi"                                    \
421         "dec    edi"                                    \
422         "dec    ecx"                                    \
423         "jnz    nextpixel"                      \
424 "done:"
425 #else
426 void expand_row(ubyte * dest, ubyte * src, int num_src_pixels )
427 {
428         int i;
429         
430         for (i = 0; i < num_src_pixels; i++) {
431                 *dest++ = *src;
432                 *dest++ = *src++;
433         }
434 }
435 #endif
436
437 // doubles the size in x or y of a bitmap in place.
438 void game_expand_bitmap( grs_bitmap * bmp, uint flags )
439 {
440         int i;
441         ubyte * dptr, * sptr;
442
443         switch(flags & 3)       {
444         case 2: // expand x
445                 Assert( bmp->bm_rowsize == bmp->bm_w*2 );
446                 dptr = &bmp->bm_data[(bmp->bm_h-1)*bmp->bm_rowsize];
447                 for (i=bmp->bm_h-1; i>=0; i-- ) {
448                         expand_row( dptr, dptr, bmp->bm_w );    
449                         dptr -= bmp->bm_rowsize;
450                 }
451                 bmp->bm_w *= 2;
452                 break;
453         case 1: // expand y
454                 dptr = &bmp->bm_data[(2*(bmp->bm_h-1)+1)*bmp->bm_rowsize];
455                 sptr = &bmp->bm_data[(bmp->bm_h-1)*bmp->bm_rowsize];
456                 for (i=bmp->bm_h-1; i>=0; i-- ) {
457                         memcpy( dptr, sptr, bmp->bm_w );        
458                         dptr -= bmp->bm_rowsize;
459                         memcpy( dptr, sptr, bmp->bm_w );        
460                         dptr -= bmp->bm_rowsize;
461                         sptr -= bmp->bm_rowsize;
462                 }
463                 bmp->bm_h *= 2;
464                 break;
465         case 3: // expand x & y
466                 Assert( bmp->bm_rowsize == bmp->bm_w*2 );
467                 dptr = &bmp->bm_data[(2*(bmp->bm_h-1)+1)*bmp->bm_rowsize];
468                 sptr = &bmp->bm_data[(bmp->bm_h-1)*bmp->bm_rowsize];
469                 for (i=bmp->bm_h-1; i>=0; i-- ) {
470                         expand_row( dptr, sptr, bmp->bm_w );    
471                         dptr -= bmp->bm_rowsize;
472                         expand_row( dptr, sptr, bmp->bm_w );    
473                         dptr -= bmp->bm_rowsize;
474                         sptr -= bmp->bm_rowsize;
475                 }
476                 bmp->bm_w *= 2;
477                 bmp->bm_h *= 2;
478                 break;
479         }
480 }
481
482 extern int SW_drawn[2], SW_x[2], SW_y[2], SW_w[2], SW_h[2];
483
484 extern int Guided_in_big_window;
485
486 #if 0
487 //render a frame for the game in stereo
488 void game_render_frame_stereo()
489 {
490         int dw,dh,sw,sh;
491         fix save_aspect;
492         fix actual_eye_width;
493         int actual_eye_offset;
494         grs_canvas RenderCanvas[2];
495         int no_draw_hud=0;
496
497         save_aspect = grd_curscreen->sc_aspect;
498         grd_curscreen->sc_aspect *= 2;  //Muck with aspect ratio
499
500         sw = dw = VR_render_buffer[0].cv_bitmap.bm_w;
501         sh = dh = VR_render_buffer[0].cv_bitmap.bm_h;
502
503         if (VR_low_res & 1)     {
504                 sh /= 2;                                
505                 grd_curscreen->sc_aspect *= 2;  //Muck with aspect ratio                                
506         }
507         if (VR_low_res & 2)     {
508                 sw /= 2;                                
509                 grd_curscreen->sc_aspect /= 2;  //Muck with aspect ratio                                
510         }
511
512         gr_init_sub_canvas( &RenderCanvas[0], &VR_render_buffer[0], 0, 0, sw, sh );
513         gr_init_sub_canvas( &RenderCanvas[1], &VR_render_buffer[1], 0, 0, sw, sh );
514
515         // Draw the left eye's view
516         if (VR_eye_switch)      {
517                 actual_eye_width = -VR_eye_width;
518                 actual_eye_offset = -VR_eye_offset;
519         } else {
520                 actual_eye_width = VR_eye_width;
521                 actual_eye_offset = VR_eye_offset;
522         }
523
524         if (Guided_missile[Player_num] && Guided_missile[Player_num]->type==OBJ_WEAPON && Guided_missile[Player_num]->id==GUIDEDMISS_ID && Guided_missile[Player_num]->signature==Guided_missile_sig[Player_num] && Guided_in_big_window)
525                 actual_eye_offset = 0;
526
527         gr_set_current_canvas(&RenderCanvas[0]);
528
529         if (Guided_missile[Player_num] && Guided_missile[Player_num]->type==OBJ_WEAPON && Guided_missile[Player_num]->id==GUIDEDMISS_ID && Guided_missile[Player_num]->signature==Guided_missile_sig[Player_num] && Guided_in_big_window) {
530                 char *msg = "Guided Missile View";
531                 object *viewer_save = Viewer;
532                 int w,h,aw;
533
534                 Viewer = Guided_missile[Player_num];
535
536                 {
537                         update_rendered_data(0, Viewer, 0, 0);
538                         render_frame(0, 0);
539   
540                         wake_up_rendered_objects(Viewer, 0);
541                         Viewer = viewer_save;
542
543                         gr_set_curfont( GAME_FONT );    //GAME_FONT );
544                         gr_set_fontcolor(gr_getcolor(27,0,0), -1 );
545                         gr_get_string_size(msg, &w, &h, &aw );
546
547                         gr_printf((grd_curcanv->cv_bitmap.bm_w-w)/2, 3, msg );
548
549                         draw_guided_crosshair();
550                 }
551
552                 HUD_render_message_frame();
553
554                 no_draw_hud=1;
555         }
556         else if (Rear_view)
557                 render_frame(actual_eye_width, 0);      // switch eye positions for rear view
558         else
559                 render_frame(-actual_eye_width, 0);             // Left eye
560
561         if ( VR_low_res )
562                 game_expand_bitmap( &RenderCanvas[0].cv_bitmap, VR_low_res );
563
564         {       //render small window into left eye's canvas
565                 grs_canvas *save=grd_curcanv;
566                 fix save_aspect2 = grd_curscreen->sc_aspect;
567                 grd_curscreen->sc_aspect = save_aspect*2;
568                 SW_drawn[0] = SW_drawn[1] = 0;
569                 show_extra_views();
570                 gr_set_current_canvas(save);
571                 grd_curscreen->sc_aspect = save_aspect2;
572         }
573
574 //NEWVR
575         if (actual_eye_offset > 0 ) {
576                 gr_setcolor( gr_getcolor(0,0,0) );
577                 gr_rect( grd_curcanv->cv_bitmap.bm_w-labs(actual_eye_offset)*2, 0, 
578                grd_curcanv->cv_bitmap.bm_w-1, grd_curcanv->cv_bitmap.bm_h );
579         } else if (actual_eye_offset < 0 ) {
580                 gr_setcolor( gr_getcolor(0,0,0) );
581                 gr_rect( 0, 0, labs(actual_eye_offset)*2-1, grd_curcanv->cv_bitmap.bm_h );
582         }
583
584         if ( VR_show_hud && !no_draw_hud )      {
585                 grs_canvas tmp;
586                 if (actual_eye_offset < 0 ) {
587                         gr_init_sub_canvas( &tmp, grd_curcanv, labs(actual_eye_offset*2), 0, grd_curcanv->cv_bitmap.bm_w-(labs(actual_eye_offset)*2), grd_curcanv->cv_bitmap.bm_h );
588                 } else {
589                         gr_init_sub_canvas( &tmp, grd_curcanv, 0, 0, grd_curcanv->cv_bitmap.bm_w-(labs(actual_eye_offset)*2), grd_curcanv->cv_bitmap.bm_h );
590                 }
591                 gr_set_current_canvas( &tmp );
592                 game_draw_hud_stuff();
593         }
594
595
596         // Draw the right eye's view
597         gr_set_current_canvas(&RenderCanvas[1]);
598
599         if (Guided_missile[Player_num] && Guided_missile[Player_num]->type==OBJ_WEAPON && Guided_missile[Player_num]->id==GUIDEDMISS_ID && Guided_missile[Player_num]->signature==Guided_missile_sig[Player_num] && Guided_in_big_window)
600                 gr_bitmap(0,0,&RenderCanvas[0].cv_bitmap);
601         else {
602                 if (Rear_view)
603                         render_frame(-actual_eye_width, 0);     // switch eye positions for rear view
604                 else
605                         render_frame(actual_eye_width, 0);              // Right eye
606
607                 if ( VR_low_res )
608                         game_expand_bitmap( &RenderCanvas[1].cv_bitmap, VR_low_res );
609         }
610
611
612         {       //copy small window from left eye
613                 grs_canvas temp;
614                 int w;
615                 for (w=0;w<2;w++) {
616                         if (SW_drawn[w]) {
617                                 gr_init_sub_canvas(&temp,&RenderCanvas[0],SW_x[w],SW_y[w],SW_w[w],SW_h[w]);
618                                 gr_bitmap(SW_x[w]+actual_eye_offset*2,SW_y[w],&temp.cv_bitmap);
619                         }
620                 }
621         }
622
623 //NEWVR
624         if (actual_eye_offset>0) {
625                 gr_setcolor( gr_getcolor(0,0,0) );
626                 gr_rect( 0, 0, labs(actual_eye_offset)*2-1, grd_curcanv->cv_bitmap.bm_h );
627         } else if ( actual_eye_offset < 0 )     {
628                 gr_setcolor( gr_getcolor(0,0,0) );
629                 gr_rect( grd_curcanv->cv_bitmap.bm_w-labs(actual_eye_offset)*2, 0, 
630                grd_curcanv->cv_bitmap.bm_w-1, grd_curcanv->cv_bitmap.bm_h );
631         }
632
633 //NEWVR (Add the next 2 lines)
634         if ( VR_show_hud && !no_draw_hud )      {
635                 grs_canvas tmp;
636                 if (actual_eye_offset > 0 ) {
637                         gr_init_sub_canvas( &tmp, grd_curcanv, labs(actual_eye_offset*2), 0, grd_curcanv->cv_bitmap.bm_w-(labs(actual_eye_offset)*2), grd_curcanv->cv_bitmap.bm_h );
638                 } else {
639                         gr_init_sub_canvas( &tmp, grd_curcanv, 0, 0, grd_curcanv->cv_bitmap.bm_w-(labs(actual_eye_offset)*2), grd_curcanv->cv_bitmap.bm_h );
640                 }
641                 gr_set_current_canvas( &tmp );
642                 game_draw_hud_stuff();
643         }
644
645
646         // Draws white and black registration encoding lines
647         // and Accounts for pixel-shift adjustment in upcoming bitblts
648         if (VR_use_reg_code)    {
649                 int width, height, quarter;
650
651                 width = RenderCanvas[0].cv_bitmap.bm_w;
652                 height = RenderCanvas[0].cv_bitmap.bm_h;
653                 quarter = width / 4;
654
655                 // black out left-hand side of left page
656
657                 // draw registration code for left eye
658                 if ( VR_eye_switch )
659                         gr_set_current_canvas( &RenderCanvas[1] );
660                 else
661                         gr_set_current_canvas( &RenderCanvas[0] );
662                 gr_setcolor( VR_WHITE_INDEX );
663                 gr_scanline( 0, quarter, height-1 );
664                 gr_setcolor( VR_BLACK_INDEX );
665                 gr_scanline( quarter, width-1, height-1 );
666
667                 if ( VR_eye_switch )
668                         gr_set_current_canvas( &RenderCanvas[0] );
669                 else
670                         gr_set_current_canvas( &RenderCanvas[1] );
671                 gr_setcolor( VR_WHITE_INDEX );
672                 gr_scanline( 0, quarter*3, height-1 );
673                 gr_setcolor( VR_BLACK_INDEX );
674                 gr_scanline( quarter*3, width-1, height-1 );
675    }
676
677                 // Copy left eye, then right eye
678         if ( VR_screen_flags&VRF_USE_PAGING )
679                 VR_current_page = !VR_current_page;
680         else 
681                 VR_current_page = 0;
682         gr_set_current_canvas( &VR_screen_pages[VR_current_page] );
683
684 //NEWVR
685
686         if ( VR_eye_offset_changed > 0 )        {
687                 VR_eye_offset_changed--;
688                 gr_clear_canvas(0);
689         }
690
691         sw = dw = VR_render_buffer[0].cv_bitmap.bm_w;
692         sh = dh = VR_render_buffer[0].cv_bitmap.bm_h;
693
694         // Copy left eye, then right eye
695         gr_bitblt_dest_step_shift = 1;          // Skip every other scanline.
696
697         if (VR_render_mode == VR_INTERLACED )   {
698                 if ( actual_eye_offset > 0 )    {
699                         int xoff = labs(actual_eye_offset);
700                         gr_bm_ubitblt( dw-xoff, dh, xoff, 0, 0, 0, &RenderCanvas[0].cv_bitmap, &VR_screen_pages[VR_current_page].cv_bitmap);
701                         gr_bm_ubitblt( dw-xoff, dh, 0, 1, xoff, 0, &RenderCanvas[1].cv_bitmap, &VR_screen_pages[VR_current_page].cv_bitmap);
702                 } else if ( actual_eye_offset < 0 )     {
703                         int xoff = labs(actual_eye_offset);
704                         gr_bm_ubitblt( dw-xoff, dh, 0, 0, xoff, 0, &RenderCanvas[0].cv_bitmap, &VR_screen_pages[VR_current_page].cv_bitmap);
705                         gr_bm_ubitblt( dw-xoff, dh, xoff, 1, 0, 0, &RenderCanvas[1].cv_bitmap, &VR_screen_pages[VR_current_page].cv_bitmap);
706                 } else {
707                         gr_bm_ubitblt( dw, dh, 0, 0, 0, 0, &RenderCanvas[0].cv_bitmap, &VR_screen_pages[VR_current_page].cv_bitmap);
708                         gr_bm_ubitblt( dw, dh, 0, 1, 0, 0, &RenderCanvas[1].cv_bitmap, &VR_screen_pages[VR_current_page].cv_bitmap);
709                 }
710         } else if (VR_render_mode == VR_AREA_DET) {
711                 // VFX copy
712                 gr_bm_ubitblt( dw, dh, 0,  VR_current_page, 0, 0, &RenderCanvas[0].cv_bitmap, &VR_screen_pages[0].cv_bitmap);
713                 gr_bm_ubitblt( dw, dh, dw, VR_current_page, 0, 0, &RenderCanvas[1].cv_bitmap, &VR_screen_pages[0].cv_bitmap);
714         } else {
715                 Int3();         // Huh?
716         }
717
718         gr_bitblt_dest_step_shift = 0;
719
720         //if ( Game_vfx_flag )
721         //      vfx_set_page(VR_current_page);          // 0 or 1
722         //else 
723                 if ( VR_screen_flags&VRF_USE_PAGING )   {
724                         gr_wait_for_retrace = 0;
725
726 //      Added by Samir from John's code
727                 if ( (VR_screen_pages[VR_current_page].cv_bitmap.bm_type == BM_MODEX) && (Game_3dmax_flag==3) ) {
728                         int old_x, old_y, new_x;
729                         old_x = VR_screen_pages[VR_current_page].cv_bitmap.bm_x;
730                         old_y = VR_screen_pages[VR_current_page].cv_bitmap.bm_y;
731                         new_x = old_y*VR_screen_pages[VR_current_page].cv_bitmap.bm_rowsize;
732                         new_x += old_x/4;
733                         VR_screen_pages[VR_current_page].cv_bitmap.bm_x = new_x;
734                         VR_screen_pages[VR_current_page].cv_bitmap.bm_y = 0;
735                         VR_screen_pages[VR_current_page].cv_bitmap.bm_type = BM_SVGA;
736                         gr_show_canvas( &VR_screen_pages[VR_current_page] );
737                         VR_screen_pages[VR_current_page].cv_bitmap.bm_type = BM_MODEX;
738                         VR_screen_pages[VR_current_page].cv_bitmap.bm_x = old_x;
739                         VR_screen_pages[VR_current_page].cv_bitmap.bm_y = old_y;
740                 } else {
741                         gr_show_canvas( &VR_screen_pages[VR_current_page] );
742                 }
743                 gr_wait_for_retrace = 1;
744         }
745         grd_curscreen->sc_aspect=save_aspect;
746 }
747 #endif
748 ubyte RenderingType=0;
749 ubyte DemoDoingRight=0,DemoDoingLeft=0;
750 extern ubyte DemoDoRight,DemoDoLeft;
751 extern object DemoRightExtra,DemoLeftExtra;
752
753 char DemoWBUType[]={0,WBU_MISSILE,WBU_MISSILE,WBU_REAR,WBU_ESCORT,WBU_MARKER,WBU_MISSILE};
754 char DemoRearCheck[]={0,0,0,1,0,0,0};
755 char *DemoExtraMessage[]={"PLAYER","GUIDED","MISSILE","REAR","GUIDE-BOT","MARKER","SHIP"};
756
757 extern char guidebot_name[];
758
759 void show_extra_views()
760 {
761         int did_missile_view=0;
762         int save_newdemo_state = Newdemo_state;
763         int w;
764
765    if (Newdemo_state==ND_STATE_PLAYBACK)
766     {
767      if (DemoDoLeft)
768       { 
769                  DemoDoingLeft=DemoDoLeft;
770                 
771        if (DemoDoLeft==3)
772                         do_cockpit_window_view(0,ConsoleObject,1,WBU_REAR,"REAR");
773        else
774               do_cockpit_window_view(0,&DemoLeftExtra,DemoRearCheck[DemoDoLeft],DemoWBUType[DemoDoLeft],DemoExtraMessage[DemoDoLeft]);
775                 }
776      else
777                 do_cockpit_window_view(0,NULL,0,WBU_WEAPON,NULL);
778
779           if (DemoDoRight)
780                 {
781                  DemoDoingRight=DemoDoRight;
782                 
783        if (DemoDoRight==3)
784                         do_cockpit_window_view(1,ConsoleObject,1,WBU_REAR,"REAR");
785        else
786            do_cockpit_window_view(1,&DemoRightExtra,DemoRearCheck[DemoDoRight],DemoWBUType[DemoDoRight],DemoExtraMessage[DemoDoRight]);
787                 } 
788      else
789         do_cockpit_window_view(1,NULL,0,WBU_WEAPON,NULL);
790           
791       DemoDoLeft=DemoDoRight=0;
792                 DemoDoingLeft=DemoDoingRight=0;
793     
794         return;
795     } 
796
797         if (Guided_missile[Player_num] && Guided_missile[Player_num]->type==OBJ_WEAPON && Guided_missile[Player_num]->id==GUIDEDMISS_ID && Guided_missile[Player_num]->signature==Guided_missile_sig[Player_num]) {
798                 if (Guided_in_big_window)
799                  {
800                         RenderingType=6+(1<<4);
801                         do_cockpit_window_view(1,Viewer,0,WBU_MISSILE,"SHIP");
802                  }
803                 else
804                  {
805                         RenderingType=1+(1<<4);
806                         do_cockpit_window_view(1,Guided_missile[Player_num],0,WBU_GUIDED,"GUIDED");
807             }
808                         
809                 did_missile_view=1;
810         }
811         else {
812
813                 if (Guided_missile[Player_num]) {               //used to be active
814                         if (!Guided_in_big_window)
815                                 do_cockpit_window_view(1,NULL,0,WBU_STATIC,NULL);
816                         Guided_missile[Player_num] = NULL;
817                 }
818
819                 if (Missile_viewer) {           //do missile view
820                         static int mv_sig=-1;
821                         if (mv_sig == -1)
822                                 mv_sig = Missile_viewer->signature;
823                         if (Missile_view_enabled && Missile_viewer->type!=OBJ_NONE && Missile_viewer->signature == mv_sig) {
824                                 RenderingType=2+(1<<4);
825                                 do_cockpit_window_view(1,Missile_viewer,0,WBU_MISSILE,"MISSILE");
826                                 did_missile_view=1;
827                         }
828                         else {
829                                 Missile_viewer = NULL;
830                                 mv_sig = -1;
831                                 RenderingType=255;
832                                 do_cockpit_window_view(1,NULL,0,WBU_STATIC,NULL);
833                         }
834                 }
835         }
836
837         for (w=0;w<2;w++) {
838
839                 if (w==1 && did_missile_view)
840                         continue;               //if showing missile view in right window, can't show anything else
841
842                 //show special views if selected
843                 switch (Cockpit_3d_view[w]) {
844                         case CV_NONE:
845                                 RenderingType=255;
846                                 do_cockpit_window_view(w,NULL,0,WBU_WEAPON,NULL);
847                                 break;
848                         case CV_REAR:
849                                 if (Rear_view) {                //if big window is rear view, show front here
850                                         RenderingType=3+(w<<4);                         
851                                         do_cockpit_window_view(w,ConsoleObject,0,WBU_REAR,"FRONT");
852                                 }
853                                 else {                                  //show normal rear view
854                                         RenderingType=3+(w<<4);                         
855                                         do_cockpit_window_view(w,ConsoleObject,1,WBU_REAR,"REAR");
856                                 }
857                                 break;
858                         case CV_ESCORT: {
859                                 object *buddy;
860                                 buddy = find_escort();
861                                 if (buddy == NULL) {
862                                         do_cockpit_window_view(w,NULL,0,WBU_WEAPON,NULL);
863                                         Cockpit_3d_view[w] = CV_NONE;
864                                 }
865                                 else {
866                                         RenderingType=4+(w<<4);
867                                         do_cockpit_window_view(w,buddy,0,WBU_ESCORT,guidebot_name);
868                                 }
869                                 break;
870                         }
871 #ifdef NETWORK
872                         case CV_COOP: {
873                                 int player = Coop_view_player[w];
874
875                  RenderingType=255; // don't handle coop stuff                  
876                                 
877                                 if (player!=-1 && Players[player].connected && ((Game_mode & GM_MULTI_COOP) || ((Game_mode & GM_TEAM) && (get_team(player) == get_team(Player_num)))))
878                                         do_cockpit_window_view(w,&Objects[Players[Coop_view_player[w]].objnum],0,WBU_COOP,Players[Coop_view_player[w]].callsign);
879                                 else {
880                                         do_cockpit_window_view(w,NULL,0,WBU_WEAPON,NULL);
881                                         Cockpit_3d_view[w] = CV_NONE;
882                                 }
883                                 break;
884                         }
885 #endif
886                         case CV_MARKER: {
887                                 char label[10];
888                                 RenderingType=5+(w<<4);
889                                 if (Marker_viewer_num[w] == -1 || MarkerObject[Marker_viewer_num[w]] == -1) {
890                                         Cockpit_3d_view[w] = CV_NONE;
891                                         break;
892                                 }
893                                 sprintf(label,"Marker %d",Marker_viewer_num[w]+1);
894                                 do_cockpit_window_view(w,&Objects[MarkerObject[Marker_viewer_num[w]]],0,WBU_MARKER,label);
895                                 break;
896                         }
897                         default:
898                                 Int3();         //invalid window type
899                 }
900         }
901         RenderingType=0;
902         Newdemo_state = save_newdemo_state;
903 }
904
905 int BigWindowSwitch=0;
906 extern int force_cockpit_redraw;
907
908 void draw_guided_crosshair(void);
909 void update_cockpits(int force_redraw);
910
911
912 //render a frame for the game
913 void game_render_frame_mono(void)
914 {
915         grs_canvas Screen_3d_window;
916         int no_draw_hud=0;
917         
918
919         gr_init_sub_canvas( &Screen_3d_window, &VR_screen_pages[0], 
920                 VR_render_sub_buffer[0].cv_bitmap.bm_x, 
921                 VR_render_sub_buffer[0].cv_bitmap.bm_y, 
922                 VR_render_sub_buffer[0].cv_bitmap.bm_w, 
923                 VR_render_sub_buffer[0].cv_bitmap.bm_h);
924
925         if ( Game_double_buffer )
926                 gr_set_current_canvas(&VR_render_sub_buffer[0]);
927         else
928                 gr_set_current_canvas(&Screen_3d_window);
929         
930         if (Guided_missile[Player_num] && Guided_missile[Player_num]->type==OBJ_WEAPON && Guided_missile[Player_num]->id==GUIDEDMISS_ID && Guided_missile[Player_num]->signature==Guided_missile_sig[Player_num] && Guided_in_big_window) {
931                 char *msg = "Guided Missile View";
932                 object *viewer_save = Viewer;
933                 int w,h,aw;
934
935       if (Cockpit_mode==CM_FULL_COCKPIT)
936                         {
937                          BigWindowSwitch=1;
938                          force_cockpit_redraw=1;
939                          Cockpit_mode=CM_STATUS_BAR;
940                          return;
941                    }
942   
943                 Viewer = Guided_missile[Player_num];
944
945                 {
946                         update_rendered_data(0, Viewer, 0, 0);
947                         render_frame(0, 0);
948   
949                         wake_up_rendered_objects(Viewer, 0);
950                         Viewer = viewer_save;
951
952                         gr_set_curfont( GAME_FONT );    //GAME_FONT );
953                         gr_set_fontcolor(gr_getcolor(27,0,0), -1 );
954                         gr_get_string_size(msg, &w, &h, &aw );
955
956                         gr_printf((grd_curcanv->cv_bitmap.bm_w-w)/2, 3, msg );
957
958                         draw_guided_crosshair();
959                 }
960
961                 HUD_render_message_frame();
962
963                 no_draw_hud=1;
964         }
965         else
966          {      
967                 if (BigWindowSwitch)
968                  {
969                    force_cockpit_redraw=1;
970                         Cockpit_mode=CM_FULL_COCKPIT;
971                    BigWindowSwitch=0;
972                         return;
973                  }
974                 update_rendered_data(0, Viewer, Rear_view, 0);
975                 render_frame(0, 0);
976          }
977
978         if ( Game_double_buffer )
979                 gr_set_current_canvas(&VR_render_sub_buffer[0]);
980         else
981                 gr_set_current_canvas(&Screen_3d_window);
982
983         if (!no_draw_hud)
984                 game_draw_hud_stuff();
985
986         if (Game_paused) {              //render pause message over off-screen 3d (to minimize flicker)
987                 extern char *Pause_msg;
988                 ubyte *save_data = VR_screen_pages[VR_current_page].cv_bitmap.bm_data;
989
990                 VR_screen_pages[VR_current_page].cv_bitmap.bm_data=VR_render_buffer[VR_current_page].cv_bitmap.bm_data;
991                 show_boxed_message(Pause_msg);
992                 VR_screen_pages[VR_current_page].cv_bitmap.bm_data=save_data;
993         }
994
995         if ( Game_double_buffer ) {             //copy to visible screen
996 //              if ( !Game_cockpit_copy_code )  {
997                         if ( VR_screen_flags&VRF_USE_PAGING )   {       
998                                 VR_current_page = !VR_current_page;
999                                 gr_set_current_canvas( &VR_screen_pages[VR_current_page] );
1000                                 gr_bm_ubitblt( VR_render_sub_buffer[0].cv_w, VR_render_sub_buffer[0].cv_h, VR_render_sub_buffer[0].cv_bitmap.bm_x, VR_render_sub_buffer[0].cv_bitmap.bm_y, 0, 0, &VR_render_sub_buffer[0].cv_bitmap, &VR_screen_pages[VR_current_page].cv_bitmap );
1001                                 gr_wait_for_retrace = 0;
1002                                 gr_show_canvas( &VR_screen_pages[VR_current_page] );
1003                                 gr_wait_for_retrace = 1;
1004                         } else {
1005                                 gr_bm_ubitblt( VR_render_sub_buffer[0].cv_w, 
1006                                                 VR_render_sub_buffer[0].cv_h, 
1007                                                 VR_render_sub_buffer[0].cv_bitmap.bm_x, 
1008                                                 VR_render_sub_buffer[0].cv_bitmap.bm_y, 
1009                                                 0, 0, 
1010                                                 &VR_render_sub_buffer[0].cv_bitmap, 
1011                                                 &VR_screen_pages[0].cv_bitmap );
1012                         }
1013 #if 0
1014                 } else  {
1015                         #ifdef __MSDOS__
1016                                 gr_ibitblt( &VR_render_buffer[0].cv_bitmap, &VR_screen_pages[0].cv_bitmap, Game_cockpit_copy_code );
1017                         #else //def MACINTOSH
1018                                 gr_ibitblt( &VR_render_sub_buffer[0].cv_bitmap, &VR_screen_pages[0].cv_bitmap );
1019                         #endif
1020                 }
1021 #endif
1022         }
1023
1024         update_cockpits(0);
1025
1026         show_extra_views();             //missile view, buddy bot, etc.
1027
1028         if (Cockpit_mode==CM_FULL_COCKPIT || Cockpit_mode==CM_STATUS_BAR) {
1029
1030                 if ( (Newdemo_state == ND_STATE_PLAYBACK) )
1031                         Game_mode = Newdemo_game_mode;
1032
1033                 render_gauges();
1034
1035                 if ( (Newdemo_state == ND_STATE_PLAYBACK) )
1036                         Game_mode = GM_NORMAL;
1037         }
1038
1039         con_update();
1040
1041         gr_update();
1042 #ifdef OGL
1043         ogl_swap_buffers();
1044 #endif
1045 }
1046
1047 void toggle_cockpit()
1048 {
1049         int new_mode;
1050
1051         switch (Cockpit_mode) {
1052
1053                 case CM_FULL_COCKPIT: {
1054                         int max_h = grd_curscreen->sc_h - GameBitmaps[cockpit_bitmap[CM_STATUS_BAR+(Current_display_mode?(Num_cockpits/2):0)].index].bm_h;
1055                         if (Game_window_h > max_h)              //too big for statusbar
1056                                 new_mode = CM_FULL_SCREEN;
1057                         else
1058                                 new_mode = CM_STATUS_BAR;
1059                         break;
1060                 }
1061
1062                 case CM_STATUS_BAR:
1063                 case CM_FULL_SCREEN:
1064                         if (Rear_view)
1065                                 return;
1066                         new_mode = CM_FULL_COCKPIT;
1067                         break;
1068
1069                 case CM_REAR_VIEW:
1070                 case CM_LETTERBOX:
1071                 default:
1072                         return;                 //do nothing
1073                         break;
1074
1075         }
1076
1077         select_cockpit(new_mode);
1078         HUD_clear_messages();
1079         write_player_file();
1080 }
1081
1082 #ifndef MACINTOSH
1083 #define WINDOW_W_DELTA  ((max_window_w / 16)&~1)        //24    //20
1084 #define WINDOW_H_DELTA  ((max_window_h / 16)&~1)        //12    //10
1085
1086 #define WINDOW_MIN_W            ((max_window_w * 10) / 22)      //160
1087 #define WINDOW_MIN_H            ((max_window_h * 10) / 22)
1088 #else
1089 //#define WINDOW_W_DELTA        32
1090 //#define WINDOW_H_DELTA        16
1091
1092 #define WINDOW_W_DELTA  ((max_window_w / 16) & ~15)             // double word aligned
1093 #define WINDOW_H_DELTA  ((max_window_h / 16) & ~15)             // double word aligned
1094
1095 #define WINDOW_MIN_W            (max_window_w-(WINDOW_W_DELTA*11))
1096 #define WINDOW_MIN_H            (max_window_h-(WINDOW_H_DELTA*11))
1097 #endif
1098
1099 void grow_window()
1100 {
1101         if (Cockpit_mode == CM_FULL_COCKPIT) {
1102                 Game_window_h = max_window_h;
1103                 Game_window_w = max_window_w;
1104                 toggle_cockpit();
1105                 HUD_init_message("Press F3 to return to Cockpit mode");
1106                 return;
1107         }
1108
1109         if (Cockpit_mode != CM_STATUS_BAR && (VR_screen_flags & VRF_ALLOW_COCKPIT))
1110                 return;
1111
1112         if (Game_window_h>=max_window_h || Game_window_w>=max_window_w) {
1113                 //Game_window_w = max_window_w;
1114                 //Game_window_h = max_window_h;
1115                 select_cockpit(CM_FULL_SCREEN);
1116         } else {
1117                 //int x,y;
1118
1119                 Game_window_w += WINDOW_W_DELTA;
1120                 Game_window_h += WINDOW_H_DELTA;
1121
1122                 if (Game_window_h > max_window_h)
1123                         Game_window_h = max_window_h;
1124
1125                 if (Game_window_w > max_window_w)
1126                         Game_window_w = max_window_w;
1127
1128                 Game_window_x = (max_window_w - Game_window_w)/2;
1129                 Game_window_y = (max_window_h - Game_window_h)/2;
1130
1131                 game_init_render_sub_buffers( Game_window_x, Game_window_y, Game_window_w, Game_window_h );
1132         }
1133
1134         HUD_clear_messages();   //      @mk, 11/11/94
1135
1136         write_player_file();
1137 }
1138
1139 // grs_bitmap background_bitmap;        already declared in line 434 (samir 4/10/94)
1140
1141 extern grs_bitmap background_bitmap;
1142
1143 void copy_background_rect(int left,int top,int right,int bot)
1144 {
1145         grs_bitmap *bm = &background_bitmap;
1146         int x,y;
1147         int tile_left,tile_right,tile_top,tile_bot;
1148         int ofs_x,ofs_y;
1149         int dest_x,dest_y;
1150
1151         if (right < left || bot < top)
1152                 return;
1153
1154         tile_left = left / bm->bm_w;
1155         tile_right = right / bm->bm_w;
1156         tile_top = top / bm->bm_h;
1157         tile_bot = bot / bm->bm_h;
1158
1159         ofs_y = top % bm->bm_h;
1160         dest_y = top;
1161
1162         for (y=tile_top;y<=tile_bot;y++) {
1163                 int w,h;
1164
1165                 ofs_x = left % bm->bm_w;
1166                 dest_x = left;
1167
1168                 //h = (bot < dest_y+bm->bm_h)?(bot-dest_y+1):(bm->bm_h-ofs_y);
1169                 h = min(bot-dest_y+1,bm->bm_h-ofs_y);
1170
1171                 for (x=tile_left;x<=tile_right;x++) {
1172
1173                         //w = (right < dest_x+bm->bm_w)?(right-dest_x+1):(bm->bm_w-ofs_x);
1174                         w = min(right-dest_x+1,bm->bm_w-ofs_x);
1175                 
1176                         gr_bm_ubitblt(w,h,dest_x,dest_y,ofs_x,ofs_y,
1177                                         &background_bitmap,&grd_curcanv->cv_bitmap);
1178
1179                         ofs_x = 0;
1180                         dest_x += w;
1181                 }
1182
1183                 ofs_y = 0;
1184                 dest_y += h;
1185         }
1186 }
1187
1188 //fills int the background surrounding the 3d window
1189 void fill_background()
1190 {
1191         int x,y,w,h,dx,dy;
1192
1193         x = Game_window_x;
1194         y = Game_window_y;
1195         w = Game_window_w;
1196         h = Game_window_h;
1197
1198         dx = x;
1199         dy = y;
1200
1201         gr_set_current_canvas(&VR_screen_pages[VR_current_page]);
1202         copy_background_rect(x-dx,y-dy,x-1,y+h+dy-1);
1203         copy_background_rect(x+w,y-dy,grd_curcanv->cv_w-1,y+h+dy-1);
1204         copy_background_rect(x,y-dy,x+w-1,y-1);
1205         copy_background_rect(x,y+h,x+w-1,y+h+dy-1);
1206
1207         if (VR_screen_flags & VRF_USE_PAGING) {
1208                 gr_set_current_canvas(&VR_screen_pages[!VR_current_page]);
1209                 copy_background_rect(x-dx,y-dy,x-1,y+h+dy-1);
1210                 copy_background_rect(x+w,y-dy,x+w+dx-1,y+h+dy-1);
1211                 copy_background_rect(x,y-dy,x+w-1,y-1);
1212                 copy_background_rect(x,y+h,x+w-1,y+h+dy-1);
1213         }
1214 }
1215
1216 void shrink_window()
1217 {
1218         mprintf((0,"%d ",FrameCount));
1219
1220 //  mprintf ((0,"W=%d H=%d\n",Game_window_w,Game_window_h));
1221  
1222         if (Cockpit_mode == CM_FULL_COCKPIT && (VR_screen_flags & VRF_ALLOW_COCKPIT)) {
1223                 Game_window_h = max_window_h;
1224                 Game_window_w = max_window_w;
1225                 //!!toggle_cockpit();
1226                 select_cockpit(CM_STATUS_BAR);
1227 //              shrink_window();
1228 //              shrink_window();
1229                 HUD_init_message("Press F3 to return to Cockpit mode");
1230                 write_player_file();
1231                 return;
1232         }
1233
1234         if (Cockpit_mode == CM_FULL_SCREEN && (VR_screen_flags & VRF_ALLOW_COCKPIT))
1235         {
1236                 //Game_window_w = max_window_w;
1237                 //Game_window_h = max_window_h;
1238                 select_cockpit(CM_STATUS_BAR);
1239                 write_player_file();
1240                 return;
1241         }
1242
1243         if (Cockpit_mode != CM_STATUS_BAR && (VR_screen_flags & VRF_ALLOW_COCKPIT))
1244                 return;
1245
1246    mprintf ((0,"Cockpit mode=%d\n",Cockpit_mode));
1247
1248         if (Game_window_w > WINDOW_MIN_W) {
1249                 //int x,y;
1250
1251       Game_window_w -= WINDOW_W_DELTA;
1252                 Game_window_h -= WINDOW_H_DELTA;
1253
1254
1255   mprintf ((0,"NewW=%d NewH=%d VW=%d maxH=%d\n",Game_window_w,Game_window_h,max_window_w,max_window_h));
1256                   
1257                 if ( Game_window_w < WINDOW_MIN_W )
1258                         Game_window_w = WINDOW_MIN_W;
1259
1260                 if ( Game_window_h < WINDOW_MIN_H )
1261                         Game_window_h = WINDOW_MIN_H;
1262                         
1263                 Game_window_x = (max_window_w - Game_window_w)/2;
1264                 Game_window_y = (max_window_h - Game_window_h)/2;
1265
1266                 fill_background();
1267
1268                 game_init_render_sub_buffers( Game_window_x, Game_window_y, Game_window_w, Game_window_h );
1269                 HUD_clear_messages();
1270                 write_player_file();
1271         }
1272
1273 }
1274
1275 int last_drawn_cockpit[2] = { -1, -1 };
1276 extern void ogl_loadbmtexture(grs_bitmap *bm); 
1277
1278 // This actually renders the new cockpit onto the screen.
1279 void update_cockpits(int force_redraw)
1280 {
1281         //int x, y, w, h;
1282
1283         //Redraw the on-screen cockpit bitmaps
1284         if (VR_render_mode != VR_NONE ) return;
1285
1286         switch( Cockpit_mode )  {
1287         case CM_FULL_COCKPIT:
1288         case CM_REAR_VIEW:
1289                 gr_set_current_canvas(&VR_screen_pages[VR_current_page]);
1290                 PIGGY_PAGE_IN(cockpit_bitmap[Cockpit_mode+(Current_display_mode?(Num_cockpits/2):0)]);
1291                 gr_ubitmapm(0,0, &GameBitmaps[cockpit_bitmap[Cockpit_mode+(Current_display_mode?(Num_cockpits/2):0)].index]);   
1292                 break;
1293
1294         case CM_FULL_SCREEN:
1295                 Game_window_x = (max_window_w - Game_window_w)/2;
1296                 Game_window_y = (max_window_h - Game_window_h)/2;
1297                 fill_background();
1298                 break;
1299
1300         case CM_STATUS_BAR:
1301
1302                 gr_set_current_canvas(&VR_screen_pages[VR_current_page]);
1303
1304                 PIGGY_PAGE_IN(cockpit_bitmap[Cockpit_mode+(Current_display_mode?(Num_cockpits/2):0)]);
1305                 gr_ubitmapm(0,max_window_h,&GameBitmaps[cockpit_bitmap[Cockpit_mode+(Current_display_mode?(Num_cockpits/2):0)].index]);
1306         
1307                 Game_window_x = (max_window_w - Game_window_w)/2;
1308                 Game_window_y = (max_window_h - Game_window_h)/2;
1309                 fill_background();
1310                 break;
1311
1312         case CM_LETTERBOX:
1313                 gr_set_current_canvas(&VR_screen_pages[VR_current_page]);
1314                 gr_clear_canvas( BM_XRGB(0,0,0) );
1315
1316                 // In a modex mode, clear the other buffer.
1317                 if (grd_curcanv->cv_bitmap.bm_type == BM_MODEX) {
1318                         gr_set_current_canvas(&VR_screen_pages[VR_current_page^1]);
1319                         gr_clear_canvas( BM_XRGB(0,0,0) );
1320                         gr_set_current_canvas(&VR_screen_pages[VR_current_page]);
1321                 }
1322                 break;
1323
1324         }
1325
1326         gr_set_current_canvas(&VR_screen_pages[VR_current_page]);
1327
1328         if (Cockpit_mode != last_drawn_cockpit[VR_current_page] || force_redraw )
1329                 last_drawn_cockpit[VR_current_page] = Cockpit_mode;
1330         else
1331                 return;
1332
1333         if (Cockpit_mode==CM_FULL_COCKPIT || Cockpit_mode==CM_STATUS_BAR)
1334                 init_gauges();
1335
1336 }
1337
1338
1339 void game_render_frame()
1340 {
1341         set_screen_mode( SCREEN_GAME );
1342
1343 //      update_cockpits(0);
1344
1345         play_homing_warning();
1346
1347         if (VR_render_mode == VR_NONE )
1348                 game_render_frame_mono();        
1349 /*      else
1350                 game_render_frame_stereo();      
1351                 */
1352
1353         // Make sure palette is faded in
1354         stop_time();
1355         gr_palette_fade_in( gr_palette, 32, 0 );
1356         start_time();
1357
1358         FrameCount++;
1359 }
1360
1361 extern int Color_0_31_0;
1362
1363 //draw a crosshair for the guided missile
1364 void draw_guided_crosshair(void)
1365 {
1366         int x,y,w,h;
1367
1368         gr_setcolor(Color_0_31_0);
1369
1370         w = grd_curcanv->cv_w>>5;
1371         if (w < 5)
1372                 w = 5;
1373
1374         h = i2f(w) / grd_curscreen->sc_aspect;
1375
1376         x = grd_curcanv->cv_w / 2;
1377         y = grd_curcanv->cv_h / 2;
1378
1379         gr_scanline(x-w/2,x+w/2,y);
1380         gr_uline(i2f(x),i2f(y-h/2),i2f(x),i2f(y+h/2));
1381
1382 }
1383
1384 typedef struct bkg {
1385         short x, y, w, h;                       // The location of the menu.
1386         grs_bitmap * bmp;                       // The background under the menu.
1387 } bkg;
1388
1389 bkg bg = {0,0,0,0,NULL};
1390
1391 #define BOX_BORDER (MenuHires?60:30)
1392
1393 //show a message in a nice little box
1394 void show_boxed_message(char *msg)
1395 {       
1396         int w,h,aw;
1397         int x,y;
1398
1399         gr_set_current_canvas(&VR_screen_pages[VR_current_page]);
1400         gr_set_curfont( MEDIUM1_FONT );
1401
1402         gr_get_string_size(msg,&w,&h,&aw);
1403
1404         x = (grd_curscreen->sc_w-w)/2;
1405         y = (grd_curscreen->sc_h-h)/2;
1406
1407         if (bg.bmp) {
1408                 gr_free_bitmap(bg.bmp);
1409                 bg.bmp = NULL;
1410         }
1411
1412         // Save the background of the display
1413         bg.x=x; bg.y=y; bg.w=w; bg.h=h;
1414
1415         bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
1416
1417         gr_bm_ubitblt(w+BOX_BORDER, h+BOX_BORDER, 0, 0, x-BOX_BORDER/2, y-BOX_BORDER/2, &(grd_curcanv->cv_bitmap), bg.bmp );
1418
1419         nm_draw_background(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
1420
1421         gr_set_fontcolor( gr_getcolor(31, 31, 31), -1 );
1422
1423         gr_ustring( 0x8000, y, msg );
1424 }
1425
1426 void clear_boxed_message()
1427 {
1428
1429         if (bg.bmp) {
1430
1431                 gr_bitmap(bg.x-BOX_BORDER/2, bg.y-BOX_BORDER/2, bg.bmp);
1432         
1433                 gr_free_bitmap(bg.bmp);
1434                 bg.bmp = NULL;
1435         }
1436 }