1 // splash.cxx -- draws the initial splash screen
3 // Written by Curtis Olson, started July 1998. (With a little looking
4 // at Freidemann's panel code.) :-)
6 // Copyright (C) 1997 Michele F. America - nomimarketing@mail.telepac.pt
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <osg/BlendFunc>
32 #include <osg/Geometry>
34 #include <osg/NodeCallback>
35 #include <osg/NodeVisitor>
36 #include <osg/StateSet>
38 #include <osg/Texture2D>
39 #include <osgUtil/CullVisitor>
40 #include <osgText/Text>
41 #include <osgDB/ReadFile>
43 #include <simgear/compiler.h>
45 #include <simgear/debug/logstream.hxx>
46 #include <simgear/math/sg_random.h>
47 #include <simgear/misc/sg_path.hxx>
51 #include "GUI/FGFontCache.hxx"
52 #include "GUI/FGColor.hxx"
54 #include <Main/globals.hxx>
55 #include <Main/fg_props.hxx>
56 #include <Main/fg_os.hxx>
57 #include <Main/locale.hxx>
59 #include "renderer.hxx"
61 class FGSplashUpdateCallback : public osg::Drawable::UpdateCallback {
63 FGSplashUpdateCallback(osg::Vec4Array* colorArray, SGPropertyNode* prop) :
64 _colorArray(colorArray),
66 _alphaProperty(fgGetNode("/sim/startup/splash-alpha", true))
68 virtual void update(osg::NodeVisitor*, osg::Drawable*)
72 c.merge(_colorProperty);
73 (*_colorArray)[0][0] = c.red();
74 (*_colorArray)[0][1] = c.green();
75 (*_colorArray)[0][2] = c.blue();
77 (*_colorArray)[0][3] = _alphaProperty->getFloatValue();
81 osg::ref_ptr<osg::Vec4Array> _colorArray;
82 SGSharedPtr<const SGPropertyNode> _colorProperty;
83 SGSharedPtr<const SGPropertyNode> _alphaProperty;
86 class FGSplashTextUpdateCallback : public osg::Drawable::UpdateCallback {
88 FGSplashTextUpdateCallback(const SGPropertyNode* prop) :
90 _alphaProperty(fgGetNode("/sim/startup/splash-alpha", true)),
91 _styleProperty(fgGetNode("/sim/gui/style[0]", true))
93 virtual void update(osg::NodeVisitor*, osg::Drawable* drawable)
95 assert(dynamic_cast<osgText::Text*>(drawable));
96 osgText::Text* text = static_cast<osgText::Text*>(drawable);
98 FGColor c(1.0, 0.9, 0.0);
99 c.merge(_styleProperty->getNode("colors/splash-font"));
100 float alpha = _alphaProperty->getFloatValue();
101 text->setColor(osg::Vec4(c.red(), c.green(), c.blue(), alpha));
103 const char* s = _textProperty->getStringValue();
104 if (s && fgGetBool("/sim/startup/splash-progress", true))
110 SGSharedPtr<const SGPropertyNode> _textProperty;
111 SGSharedPtr<const SGPropertyNode> _alphaProperty;
112 SGSharedPtr<const SGPropertyNode> _styleProperty;
117 class FGSplashContentProjectionCalback : public osg::NodeCallback {
119 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
121 assert(dynamic_cast<osgUtil::CullVisitor*>(nv));
122 osgUtil::CullVisitor* cullVisitor = static_cast<osgUtil::CullVisitor*>(nv);
124 // adjust the projection matrix in a way that preserves the aspect ratio
125 // of the content ...
126 const osg::Viewport* viewport = cullVisitor->getViewport();
127 float viewportAspect = float(viewport->height())/float(viewport->width());
130 if (viewportAspect < 1) {
132 width = 1/viewportAspect;
134 height = viewportAspect;
138 osg::RefMatrix* matrix = new osg::RefMatrix;
139 matrix->makeOrtho2D(-width, width, -height, height);
141 // The trick is to have the projection matrix adapted independent
142 // of the scenegraph but dependent on the viewport of this current
143 // camera we cull for. Therefore we do not put that projection matrix into
144 // an additional camera rather than from within that cull callback.
145 cullVisitor->pushProjectionMatrix(matrix);
147 cullVisitor->popProjectionMatrix();
151 char *genNameString()
153 std::string website = "http://www.flightgear.org";
154 std::string programName = "FlightGear";
155 char *name = new char[26];
185 static osg::Node* fgCreateSplashCamera()
187 const char* splash_texture = fgGetString("/sim/startup/splash-texture");
188 SGSharedPtr<SGPropertyNode> style = fgGetNode("/sim/gui/style[0]", true);
190 char *namestring = genNameString();
191 fgSetString("/sim/startup/program-name", namestring);
195 if (splash_texture && strcmp(splash_texture, "")) {
196 tpath = globals->resolve_maybe_aircraft_path(splash_texture);
199 SG_LOG( SG_VIEW, SG_ALERT, "Cannot find splash screen file '" << splash_texture
200 << "'. Using default." );
204 if (tpath.isNull()) {
205 // no splash screen specified - select random image
206 tpath = globals->get_fg_root();
207 // load in the texture data
208 int num = (int)(sg_random() * 5.0 + 1.0);
210 snprintf(num_str, 4, "%d", num);
212 tpath.append( "Textures/Splash" );
213 tpath.concat( num_str );
214 tpath.concat( ".png" );
217 osg::Texture2D* splashTexture = new osg::Texture2D;
218 splashTexture->setImage(osgDB::readImageFile(tpath.c_str()));
220 osg::Camera* camera = new osg::Camera;
221 camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
222 camera->setProjectionMatrix(osg::Matrix::ortho2D(-1, 1, -1, 1));
223 camera->setViewMatrix(osg::Matrix::identity());
224 camera->setRenderOrder(osg::Camera::POST_RENDER, 10000);
225 camera->setClearMask(0);
226 camera->setAllowEventFocus(false);
227 camera->setCullingActive(false);
229 osg::StateSet* stateSet = camera->getOrCreateStateSet();
230 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
231 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
232 stateSet->setAttribute(new osg::BlendFunc);
233 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
234 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
235 stateSet->setAttribute(new osg::Depth(osg::Depth::ALWAYS, 0, 1, false));
236 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
239 osg::Geometry* geometry = new osg::Geometry;
240 geometry->setSupportsDisplayList(false);
242 osg::Vec3Array* vertexArray = new osg::Vec3Array;
243 vertexArray->push_back(osg::Vec3(-1, -1, 0));
244 vertexArray->push_back(osg::Vec3( 1, -1, 0));
245 vertexArray->push_back(osg::Vec3( 1, 1, 0));
246 vertexArray->push_back(osg::Vec3(-1, 1, 0));
247 geometry->setVertexArray(vertexArray);
248 osg::Vec4Array* colorArray = new osg::Vec4Array;
249 colorArray->push_back(osg::Vec4(0, 0, 0, 1));
250 geometry->setColorArray(colorArray);
251 geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
252 geometry->addPrimitiveSet(new osg::DrawArrays(GL_POLYGON, 0, 4));
253 geometry->setUpdateCallback(new FGSplashUpdateCallback(colorArray,
254 style->getNode("colors/splash-screen")));
256 osg::Geode* geode = new osg::Geode;
257 geode->addDrawable(geometry);
259 stateSet = geode->getOrCreateStateSet();
260 stateSet->setRenderBinDetails(1, "RenderBin");
261 camera->addChild(geode);
264 // The group is needed because of osg is handling the cull callbacks in a
265 // different way for groups than for a geode. It does not hurt here ...
266 osg::Group* group = new osg::Group;
267 group->setCullCallback(new FGSplashContentProjectionCalback);
268 camera->addChild(group);
270 geode = new osg::Geode;
271 stateSet = geode->getOrCreateStateSet();
272 stateSet->setRenderBinDetails(2, "RenderBin");
273 group->addChild(geode);
276 geometry = new osg::Geometry;
277 geometry->setSupportsDisplayList(false);
279 vertexArray = new osg::Vec3Array;
280 vertexArray->push_back(osg::Vec3(-0.84, -0.84, 0));
281 vertexArray->push_back(osg::Vec3( 0.84, -0.84, 0));
282 vertexArray->push_back(osg::Vec3( 0.84, 0.84, 0));
283 vertexArray->push_back(osg::Vec3(-0.84, 0.84, 0));
284 geometry->setVertexArray(vertexArray);
285 osg::Vec2Array* texCoordArray = new osg::Vec2Array;
286 texCoordArray->push_back(osg::Vec2(0, 0));
287 texCoordArray->push_back(osg::Vec2(1, 0));
288 texCoordArray->push_back(osg::Vec2(1, 1));
289 texCoordArray->push_back(osg::Vec2(0, 1));
290 geometry->setTexCoordArray(0, texCoordArray);
291 colorArray = new osg::Vec4Array;
292 colorArray->push_back(osg::Vec4(1, 1, 1, 1));
293 geometry->setColorArray(colorArray);
294 geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
295 geometry->addPrimitiveSet(new osg::DrawArrays(GL_POLYGON, 0, 4));
296 geometry->setUpdateCallback(new FGSplashUpdateCallback(colorArray, 0));
297 stateSet = geometry->getOrCreateStateSet();
298 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON);
299 stateSet->setTextureAttribute(0, splashTexture);
300 geode->addDrawable(geometry);
303 osgText::Text* text = new osgText::Text;
304 std::string fn = style->getStringValue("fonts/splash", "");
305 text->setFont(globals->get_fontcache()->getfntpath(fn.c_str()).str());
306 text->setCharacterSize(0.06);
307 text->setColor(osg::Vec4(1, 1, 1, 1));
308 text->setPosition(osg::Vec3(0, -0.92, 0));
309 text->setAlignment(osgText::Text::CENTER_CENTER);
310 SGPropertyNode* prop = fgGetNode("/sim/startup/splash-progress-text", true);
311 prop->setStringValue("");
312 text->setUpdateCallback(new FGSplashTextUpdateCallback(prop));
313 geode->addDrawable(text);
315 osgText::Text* spinnertext = new osgText::Text;
316 spinnertext->setFont(globals->get_fontcache()->getfntpath(fn.c_str()).str());
317 spinnertext->setCharacterSize(0.06);
318 spinnertext->setColor(osg::Vec4(1, 1, 1, 1));
319 spinnertext->setPosition(osg::Vec3(0, -0.97, 0));
320 spinnertext->setAlignment(osgText::Text::CENTER_CENTER);
321 prop = fgGetNode("/sim/startup/splash-progress-spinner", true);
322 prop->setStringValue("");
323 spinnertext->setUpdateCallback(new FGSplashTextUpdateCallback(prop));
324 geode->addDrawable(spinnertext);
326 text = new osgText::Text;
327 text->setFont(globals->get_fontcache()->getfntpath(fn.c_str()).str());
328 text->setCharacterSize(0.08);
329 text->setColor(osg::Vec4(1, 1, 1, 1));
330 text->setPosition(osg::Vec3(0, 0.92, 0));
331 text->setAlignment(osgText::Text::CENTER_CENTER);
332 prop = fgGetNode("/sim/startup/program-name", "FlightGear");
333 text->setUpdateCallback(new FGSplashTextUpdateCallback(prop));
334 geode->addDrawable(text);
337 text = new osgText::Text;
338 text->setFont(globals->get_fontcache()->getfntpath(fn.c_str()).str());
339 text->setCharacterSize(0.06);
340 text->setColor(osg::Vec4(1, 1, 1, 1));
341 text->setPosition(osg::Vec3(0, 0.82, 0));
342 text->setAlignment(osgText::Text::CENTER_CENTER);
343 prop = fgGetNode("/sim/startup/splash-title", true);
344 text->setUpdateCallback(new FGSplashTextUpdateCallback(prop));
345 geode->addDrawable(text);
347 fgSplashProgress("init");
352 // update callback for the switch node guarding that splash
353 class FGSplashGroupUpdateCallback : public osg::NodeCallback {
355 FGSplashGroupUpdateCallback() :
356 _splashAlphaNode(fgGetNode("/sim/startup/splash-alpha", true))
358 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
360 assert(dynamic_cast<osg::Group*>(node));
361 osg::Group* group = static_cast<osg::Group*>(node);
363 double alpha = _splashAlphaNode->getDoubleValue();
364 if (alpha <= 0 || !fgGetBool("/sim/startup/splash-screen"))
365 group->removeChild(0, group->getNumChildren());
366 else if (group->getNumChildren() == 0)
367 group->addChild(fgCreateSplashCamera());
372 SGSharedPtr<const SGPropertyNode> _splashAlphaNode;
375 osg::Node* fgCreateSplashNode() {
376 osg::Group* group = new osg::Group;
377 group->setName("splashGroup");
378 group->setUpdateCallback(new FGSplashGroupUpdateCallback);
382 // Initialize the splash screen
383 void fgSplashInit () {
384 SG_LOG( SG_VIEW, SG_INFO, "Initializing splash screen" );
385 globals->get_renderer()->splashinit();
388 void fgSplashProgress( const char *identifier ) {
389 const char* spinChars = "-\\|/";
390 static int spin_count = 0;
391 std::string spin_status = std::string("");
393 if (identifier[0] != 0)
394 spin_status += spinChars[spin_count++ % 4];
396 fgSetString("/sim/startup/splash-progress-spinner", spin_status);
398 const char* text = "";
399 if (identifier[0] != 0)
401 std::string id = std::string("splash/") + identifier;
402 text = globals->get_locale()->getLocalizedString(id.c_str(),
403 "sys", "<incomplete language resource>");
406 if (!strcmp(identifier,"downloading-scenery")) {
407 std::ostringstream oss;
408 unsigned int kbytesPerSec = fgGetInt("/sim/terrasync/transfer-rate-bytes-sec") / 1024;
410 if (kbytesPerSec > 0) {
411 oss << " - " << kbytesPerSec << " KBytes/sec";
413 fgSetString("/sim/startup/splash-progress-text", oss.str());
417 if (!strcmp(fgGetString("/sim/startup/splash-progress-text"), text)) {
421 SG_LOG( SG_VIEW, SG_INFO, "Splash screen progress " << identifier );
422 fgSetString("/sim/startup/splash-progress-text", text);