]> icculus.org git repositories - taylor/freespace2.git/blob - src/pofview/pofview.cpp
get rid of some platform specific stuff
[taylor/freespace2.git] / src / pofview / pofview.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 // PofView.cpp : Defines the class behaviors for the application.
10 //
11
12 #include "stdafx.h"
13 #include "pofview.h"
14
15 #include "mainfrm.h"
16 #include "childfrm.h"
17 #include "pofviewdoc.h"
18 #include "pofviewview.h"
19
20 #include "pstypes.h"
21 #include "cfile.h"
22 #include "palman.h"
23 #include "2d.h"
24 #include "3d.h"
25 #include "model.h"
26 #include "vecmat.h"
27 #include "floating.h"
28 #include "key.h"
29 #include "timer.h"
30 #include "mouse.h"
31 #include "bmpman.h"
32 #include "systemvars.h"
33
34
35 #ifdef _DEBUG
36 #define new DEBUG_NEW
37 #undef THIS_FILE
38 static char THIS_FILE[] = __FILE__;
39 #endif
40
41 int Pofview_running = 1;
42
43 /////////////////////////////////////////////////////////////////////////////
44 // CPofViewApp
45
46 BEGIN_MESSAGE_MAP(CPofViewApp, CWinApp)
47         //{{AFX_MSG_MAP(CPofViewApp)
48         ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
49                 // NOTE - the ClassWizard will add and remove mapping macros here.
50                 //    DO NOT EDIT what you see in these blocks of generated code!
51         //}}AFX_MSG_MAP
52         // Standard file based document commands
53         ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
54         ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
55         // Standard print setup command
56         ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
57 END_MESSAGE_MAP()
58
59 /////////////////////////////////////////////////////////////////////////////
60 // CPofViewApp construction
61
62 CPofViewApp::CPofViewApp()
63 {
64         // TODO: add construction code here,
65         // Place all significant initialization in InitInstance
66 }
67
68 /////////////////////////////////////////////////////////////////////////////
69 // The one and only CPofViewApp object
70
71 CPofViewApp theApp;
72
73 /////////////////////////////////////////////////////////////////////////////
74 // CPofViewApp initialization
75
76 BOOL CPofViewApp::InitInstance()
77 {
78         // Standard initialization
79         // If you are not using these features and wish to reduce the size
80         //  of your final executable, you should remove from the following
81         //  the specific initialization routines you do not need.
82
83 #ifdef _AFXDLL
84         Enable3dControls();                     // Call this when using MFC in a shared DLL
85 #else
86         Enable3dControlsStatic();       // Call this when linking to MFC statically
87 #endif
88
89         LoadStdProfileSettings();  // Load standard INI file options (including MRU)
90
91         // Register the application's document templates.  Document templates
92         //  serve as the connection between documents, frame windows and views.
93
94         CMultiDocTemplate* pDocTemplate;
95         pDocTemplate = new CMultiDocTemplate(
96                 IDR_POFTYPE,
97                 RUNTIME_CLASS(CPofViewDoc),
98                 RUNTIME_CLASS(CChildFrame), // custom MDI child frame
99                 RUNTIME_CLASS(CPofViewView));
100         AddDocTemplate(pDocTemplate);
101
102         // create main MDI Frame window
103         CMainFrame* pMainFrame = new CMainFrame;
104         if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
105                 return FALSE;
106         m_pMainWnd = pMainFrame;
107
108         // Enable drag/drop open
109         m_pMainWnd->DragAcceptFiles();
110
111         // setup the fred exe directory so CFILE can init properly
112         /*
113         char *c = GetCommandLine();
114         SDL_assert(c != NULL);
115         char *tok = strtok(c, " ");
116         SDL_assert(tok != NULL);        
117         */
118
119         outwnd_init();
120         timer_init();
121         cfile_init(__argv[0]);
122         palette_load_table( "gamepalette1-01" );
123         gr_init(GR_640, GR_SOFTWARE, 8, 640, 480);
124         gr_set_gamma(2.0f);
125         key_init();
126         mouse_init();
127         gr_init_font( "font01.vf" );
128         m_timer = timer_get_milliseconds();
129         model_init();
130
131         // Enable DDE Execute open
132         EnableShellOpen();
133         RegisterShellFileTypes(TRUE);
134
135         // Parse command line for standard shell commands, DDE, file open
136         CCommandLineInfo cmdInfo;
137         ParseCommandLine(cmdInfo);
138
139         // DON'T display a new MDI child window during startup!!!
140         if ( cmdInfo.m_nShellCommand == CCommandLineInfo::FileNew )     {
141                 cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
142         }
143
144         // Dispatch commands specified on the command line
145         if (!ProcessShellCommand(cmdInfo))
146                 return FALSE;
147
148         // The main window has been initialized, so show and update it.
149         pMainFrame->ShowWindow(m_nCmdShow);
150         pMainFrame->UpdateWindow();
151
152         return TRUE;
153 }
154
155 /////////////////////////////////////////////////////////////////////////////
156 // CAboutDlg dialog used for App About
157
158 class CAboutDlg : public CDialog
159 {
160 public:
161         CAboutDlg();
162
163 // Dialog Data
164         //{{AFX_DATA(CAboutDlg)
165         enum { IDD = IDD_ABOUTBOX };
166         //}}AFX_DATA
167
168         // ClassWizard generated virtual function overrides
169         //{{AFX_VIRTUAL(CAboutDlg)
170         protected:
171         virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
172         //}}AFX_VIRTUAL
173
174 // Implementation
175 protected:
176         //{{AFX_MSG(CAboutDlg)
177                 // No message handlers
178         //}}AFX_MSG
179         DECLARE_MESSAGE_MAP()
180 };
181
182 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
183 {
184         //{{AFX_DATA_INIT(CAboutDlg)
185         //}}AFX_DATA_INIT
186 }
187
188 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
189 {
190         CDialog::DoDataExchange(pDX);
191         //{{AFX_DATA_MAP(CAboutDlg)
192         //}}AFX_DATA_MAP
193 }
194
195 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
196         //{{AFX_MSG_MAP(CAboutDlg)
197                 // No message handlers
198         //}}AFX_MSG_MAP
199 END_MESSAGE_MAP()
200
201 // App command to run the dialog
202 void CPofViewApp::OnAppAbout()
203 {
204         CAboutDlg aboutDlg;
205         aboutDlg.DoModal();
206 }
207
208 /////////////////////////////////////////////////////////////////////////////
209 // CPofViewApp commands
210
211
212 CPofViewView * GetView()
213 {
214   CMDIChildWnd * pChild =
215        ((CMDIFrameWnd*)(AfxGetApp()->m_pMainWnd))->MDIGetActive();
216
217   if ( !pChild )
218      return NULL;
219
220   CView * pView = pChild->GetActiveView();
221
222   if ( !pView )
223      return NULL;
224
225   // Fail if view is of wrong kind
226   // (this could occur with splitter windows, or additional
227   // views on a single document
228   if ( ! pView->IsKindOf( RUNTIME_CLASS(CPofViewView) ) )
229      return NULL;
230
231   return (CPofViewView *) pView;
232 }
233
234
235 // Stuff for showing ship thrusters. 
236 typedef struct thrust_anim {
237         int     num_frames;
238         int     first_frame;
239         float time;                             // in seconds
240 } thrust_anim;
241
242 #define NUM_THRUST_ANIMS                                        6
243 #define THRUST_ANIM_NORMAL                                      0
244 #define THRUST_ANIM_AFTERBURNER                 1
245 #define THRUST_ANIM_GLOW_NORMAL                 2
246 #define THRUST_ANIM_GLOW_AFTERBURNER    3
247
248 static thrust_anim      Thrust_anims[NUM_THRUST_ANIMS];
249 extern char Thrust_anim_names[NUM_THRUST_ANIMS][MAX_FILENAME_LEN];
250
251 static int Thrust_anim_inited = 0;
252
253 int     shipp_thruster_bitmap = -1;                                     // What frame the current thruster bitmap is at for this ship
254 float   shipp_thruster_frame = 0.0f;                                    // Used to keep track of which frame the animation should be on.
255
256 int     shipp_thruster_glow_bitmap = -1;                                        // What frame the current thruster bitmap is at for this ship
257 float   shipp_thruster_glow_frame = 0.0f;                                       // Used to keep track of which frame the animation should be on.
258 float   shipp_thruster_glow_noise = 1.0f;
259
260 // loads the animations for ship's afterburners
261 void init_thrusters()
262 {
263         int                     fps, i;
264         thrust_anim     *ta;
265
266         if ( Thrust_anim_inited == 1 )
267                 return;
268
269         for ( i = 0; i < NUM_THRUST_ANIMS; i++ ) {
270                 ta = &Thrust_anims[i];
271                 //if ( i < 2 )  {
272                         ta->first_frame = bm_load_animation(Thrust_anim_names[i],  &ta->num_frames, &fps);
273                         if ( ta->first_frame == -1 ) {
274                                 Error(LOCATION,"Error loading animation file: %s\n",Thrust_anim_names[i]);
275                                 return;
276                         }
277                         SDL_assert(fps != 0);
278                         ta->time = i2fl(ta->num_frames)/fps;
279                 //} else {
280                 //      ta->first_frame = bm_load(Thrust_anim_names[i] );
281                 //      ta->num_frames = NOISE_NUM_FRAMES;
282                 //      fps = 15;
283                 //      if ( ta->first_frame == -1 ) {
284                 //              Error(LOCATION,"Error loading bitmap file: %s\n",Thrust_anim_names[i]);
285                 //              return;
286                 //      }
287                 //      SDL_assert(fps != 0);
288                 //      ta->time = i2fl(ta->num_frames)/fps;
289                 //}
290         }
291
292         Thrust_anim_inited = 1;
293 }
294
295         // JAS - figure out which thruster bitmap will get rendered next
296         // time around.  ship_render needs to have shipp_thruster_bitmap set to
297         // a valid bitmap number, or -1 if we shouldn't render thrusters.
298 void do_thruster_frame( float thrust, int afterburner, float frametime )
299 {
300         float rate;
301         int framenum;
302         thrust_anim *the_anim;
303
304         if ( !Thrust_anim_inited )      init_thrusters();
305
306         if ( afterburner )      {
307                 the_anim = &Thrust_anims[THRUST_ANIM_AFTERBURNER];
308                 rate = 1.2f;            // go at 1.2x faster when afterburners on
309         } else {
310                 the_anim = &Thrust_anims[THRUST_ANIM_NORMAL];
311                 // If thrust at 0, go at half as fast, full thrust; full framerate
312                 // so set rate from 0.5 to 1.0, depending on thrust from 0 to 1
313                 rate = 0.5f + thrust / 2.0f;
314         }
315
316         shipp_thruster_frame += frametime * rate;
317
318         // Sanity checks
319         if ( shipp_thruster_frame < 0.0f )      shipp_thruster_frame = 0.0f;
320         if ( shipp_thruster_frame > 100.0f ) shipp_thruster_frame = 0.0f;
321
322         if ( shipp_thruster_frame > the_anim->time )    {
323                 shipp_thruster_frame -= the_anim->time;
324         }
325         framenum = fl2i( (shipp_thruster_frame*the_anim->num_frames) / the_anim->time );
326         if ( framenum < 0 ) framenum = 0;
327         if ( framenum >= the_anim->num_frames ) framenum = the_anim->num_frames-1;
328         
329         // Get the bitmap for this frame
330         shipp_thruster_bitmap = the_anim->first_frame + framenum;
331
332 //      mprintf(( "TF: %.2f\n", shipp_thruster_frame ));
333
334
335         // Do for glows
336
337         if ( afterburner )      {
338                 the_anim = &Thrust_anims[THRUST_ANIM_GLOW_AFTERBURNER];
339                 rate = 1.2f;            // go at 1.2x faster when afterburners on
340         } else {
341                 the_anim = &Thrust_anims[THRUST_ANIM_GLOW_NORMAL];
342                 // If thrust at 0, go at half as fast, full thrust; full framerate
343                 // so set rate from 0.5 to 1.0, depending on thrust from 0 to 1
344                 rate = 0.5f + thrust / 2.0f;
345         }
346
347         shipp_thruster_glow_frame += frametime * rate;
348
349         // Sanity checks
350         if ( shipp_thruster_glow_frame < 0.0f ) shipp_thruster_glow_frame = 0.0f;
351         if ( shipp_thruster_glow_frame > 100.0f ) shipp_thruster_glow_frame = 0.0f;
352
353         if ( shipp_thruster_glow_frame > the_anim->time )       {
354                 shipp_thruster_glow_frame -= the_anim->time;
355         }
356         framenum = fl2i( (shipp_thruster_glow_frame*the_anim->num_frames) / the_anim->time );
357         if ( framenum < 0 ) framenum = 0;
358         if ( framenum >= the_anim->num_frames ) framenum = the_anim->num_frames-1;
359         
360         // Get the bitmap for this frame
361         shipp_thruster_glow_bitmap = the_anim->first_frame;
362         shipp_thruster_glow_noise = Noise[framenum];
363
364 //      mprintf(( "TF: %.2f\n", shipp_thruster_frame ));
365
366 }
367
368
369 int pofview_focus=0;
370 float flFrametime;
371
372 float model_thrust = 0.0f;
373 int model_afterburner = 0;
374
375 BOOL CPofViewApp::OnIdle(LONG lCount)
376 {
377         CWinApp::OnIdle(lCount);
378
379         CPofViewView* CurrentView;
380         CDC* pDC;
381         int tmp_time = timer_get_milliseconds();
382         float frame_time;
383         int k = key_inkey();
384
385         frame_time = ((float)(tmp_time-m_timer)) / 1000.0f;
386         flFrametime = frame_time;
387
388         m_timer = tmp_time;
389
390         if (pofview_focus<1) return FALSE;
391
392         CurrentView = GetView();
393         if ( !CurrentView ) return TRUE;
394
395         do_thruster_frame( model_thrust, model_afterburner, flFrametime );
396
397         CurrentView->MoveViewer(frame_time,TRUE,k);
398
399         pDC = CurrentView->GetDC();
400         if ( !pDC ) return TRUE;
401
402         CurrentView->OnDraw(pDC);
403
404         CurrentView->ReleaseDC(pDC);
405
406         return TRUE;
407 }
408
409 int CPofViewApp::ExitInstance() 
410 {
411         // TODO: Add your specialized code here and/or call the base class
412         
413         return CWinApp::ExitInstance();
414 }
415