3
* Copyright (C) 2006 Fabien Chereau
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
#include "StelApp.hpp"
22
#include "StelCore.hpp"
23
#include "ViewportDistorter.hpp"
24
#include "StelUtils.hpp"
25
#include "stel_command_interface.h"
27
#include "StelTextureMgr.hpp"
28
#include "LoadingBar.hpp"
29
#include "StelObjectMgr.hpp"
30
#include "image_mgr.h"
31
#include "TelescopeMgr.hpp"
32
#include "ConstellationMgr.hpp"
33
#include "NebulaMgr.hpp"
34
#include "LandscapeMgr.hpp"
35
#include "GridLinesMgr.hpp"
36
#include "MilkyWay.hpp"
37
#include "MeteorMgr.hpp"
38
#include "StarMgr.hpp"
39
#include "SolarSystem.hpp"
40
#include "InitParser.hpp"
41
#include "Projector.hpp"
43
#include "StelModuleMgr.hpp"
44
#include "StelFontMgr.hpp"
45
#include "StelLocaleMgr.hpp"
46
#include "StelSkyCultureMgr.hpp"
47
#include "MovementMgr.hpp"
48
#include "StelFileMgr.hpp"
52
// Initialize static variables
53
StelApp* StelApp::singleton = NULL;
55
/*************************************************************************
56
Create and initialize the main Stellarium application.
57
*************************************************************************/
58
StelApp::StelApp(int argc, char** argv) :
59
maxfps(10000.f), core(NULL), fps(0), frame(0), timefr(0),
60
timeBase(0), ui(NULL), draw_mode(StelApp::DM_NORMAL), initialized(false)
72
// Can't create 2 StelApp instances
76
// C++-ize argv for use with StelUtils::argsHaveOption... functions
77
for(int i=0; i<argc; i++) { argList.push_back(argv[i]); }
79
stelFileMgr = new StelFileMgr();
81
// Check if the user directory exists, and create it if necessary
82
stelFileMgr->checkUserDir();
84
// Check some command-line options
87
setConfigFile(StelUtils::argsHaveOptionWithArg<string>(argList, "-c", "--config-file", "config.ini"));
91
cerr << "ERROR: while looking for --config-file option: " << e.what() << ". Using \"config.ini\"" << endl;
92
setConfigFile("config.ini");
94
cout << "config file is: " << configFile << endl;
96
if (!stelFileMgr->exists(configFile))
98
cerr << "config file \"" << configFile << "\" does not exist - copying the default file." << endl;
99
copyDefaultConfigFile();
102
textureMgr = new StelTextureMgr();
103
localeMgr = new StelLocaleMgr();
105
string fontMapFile("");
108
fontMapFile = stelFileMgr->findFile("data/fontmap.dat");
112
cerr << "ERROR when locating font map file: " << e.what() << endl;
114
fontManager = new StelFontMgr(fontMapFile);
116
skyCultureMgr = new StelSkyCultureMgr();
117
moduleMgr = new StelModuleMgr();
119
core = new StelCore();
120
commander = new StelCommandInterface(core, this);
121
scripts = new ScriptMgr(commander);
126
/*************************************************************************
127
Deinitialize and destroy the main Stellarium application.
128
*************************************************************************/
132
delete scripts; scripts=NULL;
133
delete commander; commander=NULL;
134
delete core; core=NULL;
135
if (distorter) {delete distorter; distorter=NULL;}
136
delete skyCultureMgr; skyCultureMgr=NULL;
137
delete localeMgr; localeMgr=NULL;
138
delete fontManager; fontManager=NULL;
139
delete stelObjectMgr; stelObjectMgr=NULL;
140
delete stelFileMgr; stelFileMgr=NULL;
142
// Delete all the modules
143
for (StelModuleMgr::Iterator iter=moduleMgr->begin();iter!=moduleMgr->end();++iter)
149
delete textureMgr; textureMgr=NULL;
150
delete moduleMgr; moduleMgr=NULL;
153
/*************************************************************************
154
Return the full name of stellarium, i.e. "stellarium 0.9.0"
155
*************************************************************************/
156
std::string StelApp::getApplicationName()
158
return string("Stellarium")+" "+PACKAGE_VERSION;
161
void StelApp::setViewPortDistorterType(const string &type)
163
if (type != getViewPortDistorterType()) setResizable(type == "none");
170
conf.load(getConfigFilePath());
171
distorter = ViewportDistorter::create(type,getScreenW(),getScreenH(),
172
core->getProjection(),conf);
175
string StelApp::getViewPortDistorterType() const
177
if (distorter) return distorter->getType();
184
Translator::initSystemLanguage();
186
// Load language codes
189
Translator::initIso639_1LanguageCodes(stelFileMgr->findFile("data/iso639-1.utf8"));
193
cerr << "ERROR while loading translations: " << e.what() << endl;
196
// Initialize video device and other sdl parameters
201
confPath = getConfigFilePath();
205
cerr << "ERROR finding config file: " << e.what() << endl;
210
string version = conf.get_str("main:version");
211
if (version!=string(PACKAGE_VERSION))
213
std::istringstream istr(version);
217
istr >> v1 >> tmp >> v2;
219
// Config versions less than 0.6.0 are not supported, otherwise we will try to use it
220
if( v1 == 0 && v2 < 6 )
222
// The config file is too old to try an importation
223
cout << "The current config file is from a version too old for parameters to be imported ("
224
<< (version.empty() ? "<0.6.0" : version.c_str())
226
<< "It will be replaced by the default config file." << endl;
228
copyDefaultConfigFile();
229
conf.load(configFile); // Read new config
233
cout << "Attempting to use an existing older config file." << endl;
237
// Create openGL context first
241
iconPath = stelFileMgr->findFile("data/icon.bmp");
245
cerr << "ERROR when trying to locate icon file: " << e.what() << endl;
247
initOpenGL(conf.get_int("video:screen_w"), conf.get_int("video:screen_h"), conf.get_int("video:bbp_mode"), conf.get_boolean("video:fullscreen"), iconPath);
249
// Initialize AFTER creation of openGL context
250
textureMgr->init(conf);
252
// Clear screen, this fixes a strange artifact at loading time in the upper corner.
253
glClearColor(0.0, 0.0, 0.0, 0.0);
254
glClear(GL_COLOR_BUFFER_BIT);
256
glClear(GL_COLOR_BUFFER_BIT);
258
maxfps = conf.get_double ("video","maximum_fps",10000);
259
minfps = conf.get_double ("video","minimum_fps",10000);
261
scripts->set_allow_ui( conf.get_boolean("gui","flag_script_allow_ui",0) );
263
core->initProj(conf);
265
LoadingBar lb(core->getProjection(), 12., "logo24bits.png",
266
getScreenW(), getScreenH(),
267
StelUtils::stringToWstring(PACKAGE_VERSION), 45, 320, 121);
269
// Stel Object Data Base manager
270
stelObjectMgr = new StelObjectMgr();
271
stelObjectMgr->init(conf, lb);
273
localeMgr->init(conf);
274
skyCultureMgr->init(conf);
276
ImageMgr* script_images = new ImageMgr();
277
script_images->init(conf, lb);
278
StelApp::getInstance().getModuleMgr().registerModule(script_images);
280
// Init the solar system first
281
SolarSystem* ssystem = new SolarSystem();
282
ssystem->init(conf, lb);
283
StelApp::getInstance().getModuleMgr().registerModule(ssystem);
285
// Load hipparcos stars & names
286
StarMgr* hip_stars = new StarMgr();
287
hip_stars->init(conf, lb);
288
StelApp::getInstance().getModuleMgr().registerModule(hip_stars);
290
core->init(conf, lb);
293
NebulaMgr* nebulas = new NebulaMgr();
294
nebulas->init(conf, lb);
295
StelApp::getInstance().getModuleMgr().registerModule(nebulas);
298
MilkyWay* milky_way = new MilkyWay();
299
milky_way->init(conf, lb);
300
StelApp::getInstance().getModuleMgr().registerModule(milky_way);
303
TelescopeMgr* telescope_mgr = new TelescopeMgr();
304
telescope_mgr->init(conf, lb);
305
StelApp::getInstance().getModuleMgr().registerModule(telescope_mgr);
308
ConstellationMgr* asterisms = new ConstellationMgr(hip_stars);
309
asterisms->init(conf, lb);
310
StelApp::getInstance().getModuleMgr().registerModule(asterisms);
312
// Landscape, atmosphere & cardinal points section
313
LandscapeMgr* landscape = new LandscapeMgr();
314
landscape->init(conf, lb);
315
StelApp::getInstance().getModuleMgr().registerModule(landscape);
317
GridLinesMgr* gridLines = new GridLinesMgr();
318
gridLines->init(conf, lb);
319
StelApp::getInstance().getModuleMgr().registerModule(gridLines);
322
MeteorMgr* meteors = new MeteorMgr(10, 60);
323
meteors->init(conf, lb);
324
StelApp::getInstance().getModuleMgr().registerModule(meteors);
326
//ugly fix by johannes: call skyCultureMgr->init twice so that
327
// star names are loaded again
328
skyCultureMgr->init(conf);
329
ui = new StelUI(core, this);
331
ui->init_tui(); // don't reinit tui since probably called from there
333
// Initialisation of the color scheme
334
draw_mode = draw_mode=DM_NIGHT; // fool caching
335
setVisionModeNormal();
336
if (conf.get_boolean("viewing:flag_night")) setVisionModeNight();
338
setViewPortDistorterType(conf.get_str("video","distorter","none"));
340
// Load dynamic external modules
345
oss << "external_modules:module" << i;
346
if (conf.find_entry(oss.str()))
348
StelModule* m = moduleMgr->loadExternalPlugin(conf.get_str(oss.str()));
352
moduleMgr->registerModule(m);
360
// Generate dependency Lists for all modules
361
moduleMgr->generateCallingLists();
363
if (conf.find_entry("files:removable_media_path"))
365
scripts->set_removable_media_path(conf.get_str("files:removable_media_path"));
369
scripts->set_removable_media_path("");
372
// play startup script, if available
373
scripts->play_startup_script();
378
void StelApp::update(int delta_time)
383
textureMgr->update();
387
if (timefr-timeBase > 1000)
389
fps=frame*1000.0/(timefr-timeBase); // Calc the FPS rate
394
// change time rate if needed to fast forward scripts
395
delta_time *= time_multiplier;
397
// keep audio position updated if changing time multiplier
398
if(!scripts->is_paused()) commander->update(delta_time);
400
// run command from a running script
401
scripts->update(delta_time);
404
ui->update((double)delta_time/1000);
406
core->update(delta_time);
408
if(!scripts->is_paused()) ((ImageMgr*)moduleMgr->getModule("image_mgr"))->update(delta_time);
410
// Send the event to every StelModule
411
std::vector<StelModule*> modList = moduleMgr->getCallOrders("update");
412
//cerr << "-------" << endl;
413
for (std::vector<StelModule*>::iterator i=modList.begin();i!=modList.end();++i)
415
//cerr << (*i)->getModuleID() << endl;
416
(*i)->update((double)delta_time/1000);
420
stelObjectMgr->update((double)delta_time/1000);
423
//! Main drawinf function called at each frame
424
double StelApp::draw(int delta_time)
429
// clear areas not redrawn by main viewport (i.e. fisheye square viewport)
430
// (because ui can draw outside the main viewport)
431
glClear(GL_COLOR_BUFFER_BIT);
433
distorter->prepare();
435
core->preDraw(delta_time);
437
// Render all the main objects of stellarium
438
double squaredDistance = 0.;
439
// Send the event to every StelModule
440
std::vector<StelModule*> modList = moduleMgr->getCallOrders("draw");
441
for (std::vector<StelModule*>::iterator i=modList.begin();i!=modList.end();++i)
443
double d = (*i)->draw(core->getProjection(), core->getNavigation(), core->getToneReproducer());
444
if (d>squaredDistance)
448
stelObjectMgr->draw(core->getProjection(), core->getNavigation(), core->getToneReproducer());
452
// Draw the Text ui into the predistorted image
453
ui->draw(core->getProjection(), core->getNavigation(), core->getToneReproducer());
455
distorter->distort();
457
// Draw the Graphical ui
460
return squaredDistance;
463
/*************************************************************************
464
Call this when the size of the GL window has changed
465
*************************************************************************/
466
void StelApp::glWindowHasBeenResized(int w, int h)
468
if (core && core->getProjection())
469
core->getProjection()->windowHasBeenResized(getScreenW(),getScreenH());
472
// Send the event to every StelModule
473
for (StelModuleMgr::Iterator iter=moduleMgr->begin();iter!=moduleMgr->end();++iter)
475
(*iter)->glWindowHasBeenResized(w, h);
479
// Handle mouse clics
480
int StelApp::handleClick(int x, int y, Uint8 button, Uint8 state, StelMod mod)
484
y = getScreenH() - 1 - y;
485
distorter->distortXY(x,y);
487
if (ui->handle_clic(ui_x, ui_y, button, state, mod)) return 1;
489
// Send the event to every StelModule
490
std::vector<StelModule*> modList = moduleMgr->getCallOrders("handleMouseClicks");
491
for (std::vector<StelModule*>::iterator i=modList.begin();i!=modList.end();++i)
493
if ((*i)->handleMouseClicks(x, y, button, state, mod)==true)
497
// Manage the event for the main window
499
// Deselect the selected object
500
if (button==Stel_BUTTON_RIGHT && state==Stel_MOUSEBUTTONUP)
502
commander->execute_command("select");
505
MovementMgr* mvmgr = (MovementMgr*)getModuleMgr().getModule("movements");
506
if (button==Stel_BUTTON_LEFT && state==Stel_MOUSEBUTTONUP && !mvmgr->getHasDragged())
509
// CTRL + left clic = right clic for 1 button mouse
510
if (mod & StelMod_CTRL)
512
commander->execute_command("select");
516
// Try to select object at that position
517
getStelObjectMgr().findAndSelect(core, x, y, mod & StelMod_META);
519
getStelObjectMgr().findAndSelect(core, x, y, mod & StelMod_CTRL);
521
// If an object was selected update informations
522
if (getStelObjectMgr().getWasSelected())
524
((MovementMgr*)moduleMgr->getModule("movements"))->setFlagTracking(false);
525
ui->updateInfoSelectString();
534
int StelApp::handleMove(int x, int y, StelMod mod)
538
y = getScreenH() - 1 - y;
539
distorter->distortXY(x,y);
541
// Send the event to every StelModule
542
std::vector<StelModule*> modList = moduleMgr->getCallOrders("handleMouseMoves");
543
for (std::vector<StelModule*>::iterator i=modList.begin();i!=modList.end();++i)
545
if ((*i)->handleMouseMoves(x, y, mod)==true)
549
return ui->handle_move(ui_x,ui_y, mod);
552
// Handle key press and release
553
int StelApp::handleKeys(StelKey key, StelMod mod, Uint16 unicode, Uint8 state)
556
// Standard keys should not be able to be hijacked by modules - Rob
557
// (this could be debated)
558
if (ui->handle_keys_tui(key, state)) return 1;
560
if (ui->handle_keysGUI(key, mod, unicode, state)) return 1;
562
// Send the event to every StelModule
563
std::vector<StelModule*> modList = moduleMgr->getCallOrders("handleKeys");
564
for (std::vector<StelModule*>::iterator i=modList.begin();i!=modList.end();++i)
566
if ((*i)->handleKeys(key, mod, unicode, state)==true)
570
// Non widget key handling
571
return ui->handle_keys(key, mod, unicode, state);
575
//! Set the drawing mode in 2D for drawing in the full screen
576
void StelApp::set2DfullscreenProjection() const
578
glViewport(0,0,getScreenW(),getScreenH());
579
glMatrixMode(GL_PROJECTION); // projection matrix mode
580
glPushMatrix(); // store previous matrix
582
glOrtho(0,getScreenW(),0,getScreenH(),-1,1); // set a 2D orthographic projection
583
glMatrixMode(GL_MODELVIEW); // modelview matrix mode
586
glScalef(1, -1, 1); // invert the y axis, down is positive
587
glTranslatef(0, -getScreenH(), 0); // move the origin from the bottom left corner to the upper left corner
590
void StelApp::setConfigFile(const string& configName)
594
configFile = stelFileMgr->findFile(configName, StelFileMgr::FLAGS(StelFileMgr::WRITABLE|StelFileMgr::FILE));
599
//cerr << "DEBUG StelApp::setConfigFile could not locate writable config file " << configName << endl;
604
configFile = stelFileMgr->findFile(configName, StelFileMgr::FILE);
609
//cerr << "DEBUG StelApp::setConfigFile could not find read only config file " << configName << endl;
614
configFile = stelFileMgr->findFile(configName, StelFileMgr::NEW);
615
//cerr << "DEBUG StelApp::setConfigFile found NEW file path: " << configFile << endl;
620
cerr << "ERROR StelApp::setConfigFile could not find or create configuration file " << configName << endl;
625
void StelApp::copyDefaultConfigFile()
627
string defaultConfigFilePath;
630
defaultConfigFilePath = stelFileMgr->findFile("data/default_config.ini");
634
cerr << "ERROR (copyDefaultConfigFile): failed to locate data/default_config.ini. Please check your installation." << endl;
638
StelFileMgr::copy(defaultConfigFilePath, configFile);
639
if (!stelFileMgr->exists(configFile))
641
cerr << "ERROR (copyDefaultConfigFile): failed to copy file " << defaultConfigFilePath << " to " << configFile << ". You could try to copy it by hand." << endl;
646
//! Restore previous projection mode
647
void StelApp::restoreFrom2DfullscreenProjection() const
649
glMatrixMode(GL_PROJECTION); // Restore previous matrix
651
glMatrixMode(GL_MODELVIEW);
655
// Set the colorscheme for all the modules
656
void StelApp::setColorScheme(const std::string& fileName, const std::string& section)
661
// Send the event to every StelModule
662
for (StelModuleMgr::Iterator iter=moduleMgr->begin();iter!=moduleMgr->end();++iter)
664
(*iter)->setColorScheme(conf, section);
667
ui->setColorScheme(conf, section);
670
//! Set flag for activating night vision mode
671
void StelApp::setVisionModeNight()
673
if (!getVisionModeNight())
675
setColorScheme(getConfigFilePath(), "night_color");
680
//! Set flag for activating chart vision mode
681
// ["color" section name used for easier backward compatibility for older configs - Rob]
682
void StelApp::setVisionModeNormal()
684
if (!getVisionModeNormal())
686
setColorScheme(getConfigFilePath(), "color");
691
void StelApp::recordCommand(string commandline)
693
scripts->record_command(commandline);
697
// Update translations and font everywhere in the program
698
void StelApp::updateAppLanguage()
700
// update translations and font in tui
706
// Update translations and font for sky everywhere in the program
707
void StelApp::updateSkyLanguage()
709
// Send the event to every StelModule
710
for (StelModuleMgr::Iterator iter=moduleMgr->begin();iter!=moduleMgr->end();++iter)
712
(*iter)->updateI18n();
716
// Update and reload sky culture informations everywhere in the program
717
void StelApp::updateSkyCulture()
719
LoadingBar lb(core->getProjection(), 12., "logo24bits.png",
720
core->getProjection()->getViewportWidth(), core->getProjection()->getViewportHeight(),
721
StelUtils::stringToWstring(PACKAGE_VERSION), 45, 320, 121);
722
// Send the event to every StelModule
723
for (StelModuleMgr::Iterator iter=moduleMgr->begin();iter!=moduleMgr->end();++iter)
725
(*iter)->updateSkyCulture(lb);