]> icculus.org git repositories - taylor/freespace2.git/blob - src/launcher/launcher.cpp
add Volition copyright notice
[taylor/freespace2.git] / src / launcher / launcher.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 #include "launcher.h"
10 #include "launchersetup.h"
11
12 #include "wx/filename.h"
13 #include "wx/stdpaths.h"
14
15 #include "pstypes.h"
16 #include "osregistry.h"
17 #include "cfile.h"
18
19 #ifndef MAKE_FS1
20 #include "res/fs2_background.xpm"
21 #include "res/fs2_btn_help.xpm"
22 #include "res/fs2_btn_help-hover.xpm"
23 #include "res/fs2_btn_help-click.xpm"
24 #include "res/fs2_btn_play.xpm"
25 #include "res/fs2_btn_play-hover.xpm"
26 #include "res/fs2_btn_play-click.xpm"
27 #include "res/fs2_btn_pxo.xpm"
28 #include "res/fs2_btn_pxo-hover.xpm"
29 #include "res/fs2_btn_pxo-click.xpm"
30 #include "res/fs2_btn_quit.xpm"
31 #include "res/fs2_btn_quit-hover.xpm"
32 #include "res/fs2_btn_quit-click.xpm"
33 #include "res/fs2_btn_readme.xpm"
34 #include "res/fs2_btn_readme-hover.xpm"
35 #include "res/fs2_btn_readme-click.xpm"
36 #include "res/fs2_btn_setup.xpm"
37 #include "res/fs2_btn_setup-hover.xpm"
38 #include "res/fs2_btn_setup-click.xpm"
39 #include "res/fs2_btn_uninstall.xpm"
40 #include "res/fs2_btn_uninstall-hover.xpm"
41 #include "res/fs2_btn_uninstall-click.xpm"
42 #include "res/fs2_btn_update.xpm"
43 #include "res/fs2_btn_update-hover.xpm"
44 #include "res/fs2_btn_update-click.xpm"
45 #include "res/fs2_btn_volition.xpm"
46 #include "res/fs2_btn_volition-hover.xpm"
47 #include "res/fs2_btn_volition-click.xpm"
48
49 #include "res/fs2_snd_hover_wav.inc"
50 #include "res/fs2_snd_click_wav.inc"
51 #else
52 #include "res/freespace_img.xpm"
53 #include "res/volition_img.xpm"
54 #endif
55
56
57
58 class LauncherApp: public wxApp
59 {
60         public:
61                 virtual bool OnInit();
62 };
63
64
65 IMPLEMENT_APP(LauncherApp)
66
67 bool LauncherApp::OnInit()
68 {
69         Launcher *frame = new Launcher(NULL);
70
71         frame->Show();
72         SetTopWindow(frame);
73
74         frame->JumpToSetup();
75
76         return true;
77 }
78
79 bool wxBackgroundBitmap::ProcessEvent(wxEvent &Event)
80 {
81         if (Event.GetEventType() == wxEVT_ERASE_BACKGROUND) {
82                 wxEraseEvent &EraseEvent = dynamic_cast<wxEraseEvent &>(Event);
83                 wxDC *DC = EraseEvent.GetDC();
84                 DC->DrawBitmap(Bitmap, 0, 0, false);
85
86                 return true;
87         } else {
88                 return Inherited::ProcessEvent(Event);
89         }
90 }
91
92
93 wxBEGIN_EVENT_TABLE(wxLauncherButton, wxStaticBitmap)
94         EVT_LEFT_DOWN(wxLauncherButton::onMouseDown)
95         EVT_LEFT_UP(wxLauncherButton::onMouseUp)
96         EVT_MOTION(wxLauncherButton::onMouseEnter)
97 wxEND_EVENT_TABLE()
98
99
100 wxLauncherButton::wxLauncherButton(wxWindow *parent, wxWindowID id, const wxBitmap& label, const wxPoint& pos, const wxSize& size)
101 {
102         this->Create(parent, id, label, pos, size);
103
104         parent->Connect(wxEVT_MOTION, wxMouseEventHandler(wxLauncherButton::onMouseLeave), NULL, this);
105
106         m_bitmap = label;
107
108         m_in_hover = false;
109 }
110
111 wxLauncherButton::~wxLauncherButton()
112 {
113 }
114
115 void wxLauncherButton::onMouseEnter(wxMouseEvent& event)
116 {
117         if ( !m_in_hover ) {
118                 this->SetBitmap(m_bitmap_hover);
119
120                 ((Launcher*)GetGrandParent())->SndPlayHover();
121
122                 m_in_hover = true;
123
124                 // hack to toggle off certain buttons that don't get the onMouseLeave
125                 // call due to placement/overlap
126                 {
127                         wxMouseEvent mv;
128
129                         const wxWindowList wl = GetParent()->GetChildren();
130
131                         wxWindowList::compatibility_iterator node = wl.GetFirst();
132
133                         while (node) {
134                                 wxLauncherButton *cur = (wxLauncherButton*)node->GetData();
135
136                                 if ( cur->GetId() != event.GetId() ) {
137                                         cur->onMouseLeave(mv);
138                                 }
139
140                                 node = node->GetNext();
141                         }
142                 }
143         }
144
145         event.Skip();
146 }
147
148 void wxLauncherButton::onMouseLeave(wxMouseEvent& event)
149 {
150         if (m_in_hover) {
151                 this->SetBitmap(m_bitmap);
152
153                 m_in_hover = false;
154         }
155
156         event.Skip();
157 }
158
159 void wxLauncherButton::onMouseDown(wxMouseEvent& event)
160 {
161         this->SetBitmap(m_bitmap_pressed);
162
163         ((Launcher*)GetGrandParent())->SndPlayPressed();
164
165         event.Skip();
166 }
167
168 void wxLauncherButton::onMouseUp(wxMouseEvent& event)
169 {
170         this->SetBitmap(m_bitmap_hover);
171
172         wxCommandEvent ev(wxEVT_COMMAND_BUTTON_CLICKED, event.GetId());
173         GetGrandParent()->GetEventHandler()->ProcessEvent(ev);
174
175         event.Skip();
176 }
177
178
179 wxBEGIN_EVENT_TABLE(Launcher, wxDialog)
180         EVT_CLOSE(Launcher::OnClose)
181         EVT_BUTTON(ID_B_PLAY, Launcher::OnPlay)
182         EVT_BUTTON(ID_B_SETUP, Launcher::OnSetup)
183         EVT_BUTTON(ID_B_README, Launcher::OnReadme)
184         EVT_BUTTON(ID_B_UPDATE, Launcher::OnUpdate)
185         EVT_BUTTON(ID_B_HELP, Launcher::OnHelp)
186         EVT_BUTTON(ID_B_UNINSTALL, Launcher::OnUninstall)
187         EVT_BUTTON(ID_B_VOLITION, Launcher::OnVolition)
188         EVT_BUTTON(ID_B_PXO, Launcher::OnPXO)
189         EVT_BUTTON(ID_B_QUIT, Launcher::OnQuit)
190 wxEND_EVENT_TABLE()
191
192
193 Launcher::Launcher( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style )
194         : wxDialog( parent, id, title, pos, size, style )
195 {
196         this->SetBackgroundColour( wxColour( 0, 0, 0 ) );
197
198         use_sound = false;
199
200 #ifndef MAKE_FS1
201         this->SetClientSize(375, 440);
202         this->SetTitle( wxT("FreeSpace 2 Launcher") );
203
204         init_sound();
205
206
207         p_background = new wxBackgroundBitmap(fs2_background_xpm);
208
209         m_panel = new wxPanel(this);
210         m_panel->PushEventHandler(p_background);
211
212
213         m_btn_Play = new wxLauncherButton( m_panel, ID_B_PLAY, wxBitmap(fs2_btn_play_xpm), wxPoint(45, 102), wxSize(131, 58) );
214         m_btn_Play->SetBitmapHover( wxBitmap(fs2_btn_play_hover_xpm) );
215         m_btn_Play->SetBitmapPressed( wxBitmap(fs2_btn_play_click_xpm) );
216
217         m_btn_Setup = new wxLauncherButton( m_panel, ID_B_SETUP, wxBitmap(fs2_btn_setup_xpm), wxPoint(199, 102), wxSize(131, 58) );
218         m_btn_Setup->SetBitmapHover( wxBitmap(fs2_btn_setup_hover_xpm) );
219         m_btn_Setup->SetBitmapPressed( wxBitmap(fs2_btn_setup_click_xpm) );
220
221         m_btn_Readme = new wxLauncherButton( m_panel, ID_B_README, wxBitmap(fs2_btn_readme_xpm), wxPoint(45, 175), wxSize(131, 58) );
222         m_btn_Readme->SetBitmapHover( wxBitmap(fs2_btn_readme_hover_xpm) );
223         m_btn_Readme->SetBitmapPressed( wxBitmap(fs2_btn_readme_click_xpm) );
224
225         m_btn_Update = new wxLauncherButton( m_panel, ID_B_UPDATE, wxBitmap(fs2_btn_update_xpm), wxPoint(199, 175), wxSize(131, 58) );
226         m_btn_Update->SetBitmapHover( wxBitmap(fs2_btn_update_hover_xpm) );
227         m_btn_Update->SetBitmapPressed( wxBitmap(fs2_btn_update_click_xpm) );
228
229         m_btn_Help = new wxLauncherButton( m_panel, ID_B_HELP, wxBitmap(fs2_btn_help_xpm), wxPoint(45, 247), wxSize(131, 58) );
230         m_btn_Help->SetBitmapHover( wxBitmap(fs2_btn_help_hover_xpm) );
231         m_btn_Help->SetBitmapPressed( wxBitmap(fs2_btn_help_click_xpm) );
232
233         m_btn_Uninstall = new wxLauncherButton( m_panel, ID_B_UNINSTALL, wxBitmap(fs2_btn_uninstall_xpm), wxPoint(199, 247), wxSize(131, 58) );
234         m_btn_Uninstall->SetBitmapHover( wxBitmap(fs2_btn_uninstall_hover_xpm) );
235         m_btn_Uninstall->SetBitmapPressed( wxBitmap(fs2_btn_uninstall_click_xpm) );
236
237         m_btn_Volition = new wxLauncherButton( m_panel, ID_B_VOLITION, wxBitmap(fs2_btn_volition_xpm), wxPoint(15, 304), wxSize(90, 108) );
238         m_btn_Volition->SetBitmapHover( wxBitmap(fs2_btn_volition_hover_xpm) );
239         m_btn_Volition->SetBitmapPressed( wxBitmap(fs2_btn_volition_click_xpm) );
240
241         m_btn_PXO = new wxLauncherButton( m_panel, ID_B_PXO, wxBitmap(fs2_btn_pxo_xpm), wxPoint(249, 305), wxSize(114, 113) );
242         m_btn_PXO->SetBitmapHover( wxBitmap(fs2_btn_pxo_hover_xpm) );
243         m_btn_PXO->SetBitmapPressed( wxBitmap(fs2_btn_pxo_click_xpm) );
244
245         m_btn_Quit = new wxLauncherButton( m_panel, ID_B_QUIT, wxBitmap(fs2_btn_quit_xpm), wxPoint(116, 339), wxSize(131, 58) );
246         m_btn_Quit->SetBitmapHover( wxBitmap(fs2_btn_quit_hover_xpm) );
247         m_btn_Quit->SetBitmapPressed( wxBitmap(fs2_btn_quit_click_xpm) );
248 #else
249         this->SetSizeHints( wxDefaultSize, wxDefaultSize );
250         this->SetTitle( wxT("FreeSpace Launcher") );
251
252         wxBoxSizer* bSizer3;
253         bSizer3 = new wxBoxSizer( wxVERTICAL );
254
255         wxStaticBitmap *m_bitmap1 = new wxStaticBitmap( this, wxID_ANY, wxBitmap( freespace_img_xpm ), wxDefaultPosition, wxDefaultSize, 0 );
256         bSizer3->Add( m_bitmap1, 0, wxALIGN_LEFT|wxALIGN_TOP|wxALL, 5 );
257
258         wxFlexGridSizer* fgSizer3;
259         fgSizer3 = new wxFlexGridSizer( 0, 2, 0, 0 );
260         fgSizer3->SetFlexibleDirection( wxBOTH );
261         fgSizer3->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
262
263         wxStaticBitmap *m_bitmap2 = new wxStaticBitmap( this, wxID_ANY, wxBitmap( volition_img_xpm ), wxDefaultPosition, wxDefaultSize, 0 );
264         fgSizer3->Add( m_bitmap2, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
265
266         wxBoxSizer* bSizer5;
267         bSizer5 = new wxBoxSizer( wxVERTICAL );
268
269         m_btn_Play = new wxButton( this, ID_B_PLAY, wxT("Play FreeSpace"), wxDefaultPosition, wxDefaultSize, 0 );
270         m_btn_Play->SetDefault();
271         bSizer5->Add( m_btn_Play, 0, wxALL|wxEXPAND, 5 );
272
273         m_btn_Setup = new wxButton( this, ID_B_SETUP, wxT("Setup"), wxDefaultPosition, wxDefaultSize, 0 );
274         bSizer5->Add( m_btn_Setup, 0, wxALL|wxEXPAND, 5 );
275
276         m_btn_Readme = new wxButton( this, ID_B_README, wxT("View README"), wxDefaultPosition, wxDefaultSize, 0 );
277         bSizer5->Add( m_btn_Readme, 0, wxALL|wxEXPAND, 5 );
278
279         m_btn_Update = new wxButton( this, ID_B_UPDATE, wxT("Update FreeSpace"), wxDefaultPosition, wxDefaultSize, 0 );
280         bSizer5->Add( m_btn_Update, 0, wxALL|wxEXPAND, 5 );
281
282         m_btn_Volition = new wxButton( this, ID_B_VOLITION, wxT("FreeSpace Webpage"), wxDefaultPosition, wxDefaultSize, 0 );
283         bSizer5->Add( m_btn_Volition, 0, wxALL|wxEXPAND, 5 );
284
285         m_btn_Uninstall = new wxButton( this, ID_B_UNINSTALL, wxT("Uninstall"), wxDefaultPosition, wxDefaultSize, 0 );
286         bSizer5->Add( m_btn_Uninstall, 0, wxALL|wxEXPAND, 5 );
287
288         m_btn_Quit = new wxButton( this, ID_B_QUIT, wxT("Quit"), wxDefaultPosition, wxDefaultSize, 0 );
289         bSizer5->Add( m_btn_Quit, 0, wxALL|wxEXPAND, 5 );
290
291         fgSizer3->Add( bSizer5, 1, wxALIGN_CENTER|wxALL, 10 );
292
293         bSizer3->Add( fgSizer3, 1, wxALIGN_BOTTOM|wxALIGN_RIGHT|wxEXPAND, 5 );
294
295         this->SetSizer( bSizer3 );
296         this->Layout();
297         bSizer3->Fit( this );
298 #endif
299
300
301         this->Centre( wxBOTH );
302 }
303
304 Launcher::~Launcher()
305 {
306         close_sound();
307 }
308
309 void Launcher::OnClose( wxCloseEvent& WXUNUSED(event) )
310 {
311 #ifndef MAKE_FS1
312         m_panel->RemoveEventHandler(p_background);
313         delete p_background;
314 #endif
315
316         Destroy();
317 }
318
319 void Launcher::OnPlay( wxCommandEvent& WXUNUSED(event) )
320 {
321         wxString epath = wxFileName(wxStandardPaths::Get().GetExecutablePath()).GetPath(true);
322
323 #ifndef MAKE_FS1
324         epath.Append( wxT("freespace2") );
325 #else
326         epath.Append( wxT("freespace") );
327 #endif
328
329 #ifdef FS2_DEMO
330         epath.Append( wxT("_demo") );
331 #endif
332
333 #ifdef _WIN32
334         epath.Append( wxT(".exe") );
335 #endif
336
337         wxExecute(epath, wxEXEC_ASYNC | wxEXEC_MAKE_GROUP_LEADER | wxEXEC_HIDE_CONSOLE);
338
339         this->Close();
340 }
341
342 void Launcher::OnSetup( wxCommandEvent& WXUNUSED(event) )
343 {
344         LauncherSetup setup(this);
345
346         setup.ShowModal();
347 }
348
349 void Launcher::OnReadme( wxCommandEvent& WXUNUSED(event) )
350 {
351         wxLaunchDefaultApplication("README.txt");
352 }
353
354 void Launcher::OnUpdate( wxCommandEvent& WXUNUSED(event) )
355 {
356         wxMessageBox( wxT("Not implemented") );
357 }
358
359 void Launcher::OnHelp( wxCommandEvent& WXUNUSED(event) )
360 {
361
362 }
363
364 void Launcher::OnUninstall( wxCommandEvent& WXUNUSED(event) )
365 {
366         wxMessageBox( wxT("Not implemented") );
367 }
368
369 void Launcher::OnVolition( wxCommandEvent& WXUNUSED(event) )
370 {
371         wxLaunchDefaultBrowser( wxT("http://www.volition-inc.com") );
372 }
373
374 void Launcher::OnPXO( wxCommandEvent& WXUNUSED(event) )
375 {
376         wxLaunchDefaultBrowser( wxT("http://www.pxo.net") );
377 }
378
379 void Launcher::OnQuit( wxCommandEvent& WXUNUSED(event) )
380 {
381         this->Close();
382 }
383
384 void Launcher::JumpToSetup()
385 {
386         if ( !os_config_read_uint(NULL, "StraightToSetup", 1) ) {
387                 return;
388         }
389
390         // FS1 doesn't do a setup jump so just go with what the user sets up
391         // and/or what the game binary will set
392
393 #ifndef MAKE_FS1
394         wxString title( wxT("Welcome to FreeSpace 2!") );
395
396         wxString message( wxT("Since this is your first time running FreeSapce2, "
397                                                   "you will now be automatically taken to the Setup "
398                                                   "window.") );
399
400         wxString ext_message( wxT("NOTE TO USER:\n"
401                                                           "It is important that you view each section of "
402                                                           "the Setup window and configure it to your "
403                                                           "liking. Press the Help button if you have "
404                                                           "questions about a particular section. Once you "
405                                                           "are satisfied with your settings, select the OK "
406                                                           "button at the bottom of the Setup window to "
407                                                           "save them.") );
408
409         wxMessageDialog prompt(this, message, title, wxOK | wxICON_INFORMATION);
410         prompt.SetExtendedMessage(ext_message);
411
412         prompt.ShowModal();
413
414         // now jump to setup dialog
415         LauncherSetup setup(this);
416
417         setup.ShowModal();
418 #endif
419 }
420
421 void Launcher::SndPlayHover()
422 {
423         if (use_sound) {
424                 alSourcePlay(m_snd_hover_source_id);
425         }
426 }
427
428 void Launcher::SndPlayPressed()
429 {
430         if (use_sound) {
431                 alSourcePlay(m_snd_click_source_id);
432         }
433 }
434
435 void Launcher::SndEnable(bool enabled)
436 {
437         if (enabled) {
438                 init_sound();
439         } else {
440                 close_sound();
441         }
442 }
443
444 void Launcher::init_sound()
445 {
446 #ifndef MAKE_FS1
447         if (use_sound) {
448                 return;
449         }
450
451         if ( os_config_read_uint("Audio", "LauncherSoundEnabled", 1) == 0 ) {
452                 return;
453         }
454
455         al_device = alcOpenDevice(NULL);
456
457         if (al_device == NULL) {
458                 return;
459         }
460
461         al_context = alcCreateContext(al_device, NULL);
462
463         if (al_context == NULL) {
464                 alcCloseDevice(al_device);
465                 return;
466         }
467
468         alcMakeContextCurrent(al_context);
469
470         // 'hover' sound
471         alGenBuffers(1, &m_snd_hover_buf_id);
472         alBufferData(m_snd_hover_buf_id, AL_FORMAT_MONO8, fs2_snd_hover_wav, sizeof(fs2_snd_hover_wav), 22050);
473
474         alGenSources(1, &m_snd_hover_source_id);
475         alSourcef(m_snd_hover_source_id, AL_GAIN, 1.0f);
476         alSourcei(m_snd_hover_source_id, AL_BUFFER, m_snd_hover_buf_id);
477
478         // 'click' sound
479         alGenBuffers(1, &m_snd_click_buf_id);
480         alBufferData(m_snd_click_buf_id, AL_FORMAT_MONO8, fs2_snd_click_wav, sizeof(fs2_snd_click_wav), 22050);
481
482         alGenSources(1, &m_snd_click_source_id);
483         alSourcef(m_snd_click_source_id, AL_GAIN, 1.0f);
484         alSourcei(m_snd_click_source_id, AL_BUFFER, m_snd_click_buf_id);
485
486         use_sound = true;
487 #endif
488 }
489
490 void Launcher::close_sound()
491 {
492         if ( !use_sound ) {
493                 return;
494         }
495
496         alSourceStop(m_snd_click_source_id);
497
498         alSourcei(m_snd_click_source_id, AL_BUFFER, 0);
499         alDeleteSources(1, &m_snd_click_source_id);
500         alDeleteBuffers(1, &m_snd_click_buf_id);
501
502         alSourceStop(m_snd_hover_source_id);
503
504         alSourcei(m_snd_hover_source_id, AL_BUFFER, 0);
505         alDeleteSources(1, &m_snd_hover_source_id);
506         alDeleteBuffers(1, &m_snd_hover_buf_id);
507
508         alcMakeContextCurrent(NULL);
509         alcDestroyContext(al_context);
510         alcCloseDevice(al_device);
511
512         use_sound = false;
513 }