2011-04-14 09:11:17 +00:00
|
|
|
//
|
|
|
|
// Written and (c) Torsten Dreyer - Torsten(at)t3r_dot_de
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
2016-12-04 18:36:53 +00:00
|
|
|
//
|
2011-04-14 09:11:17 +00:00
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
//
|
2011-05-27 18:53:16 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
2020-04-19 11:18:33 +00:00
|
|
|
# include <windows.h>
|
|
|
|
# include <direct.h>
|
|
|
|
#else
|
|
|
|
# include <unistd.h>
|
2011-05-27 18:53:16 +00:00
|
|
|
#endif
|
|
|
|
|
2012-01-25 16:37:14 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
# include <CoreFoundation/CoreFoundation.h>
|
|
|
|
#endif
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2011-06-04 19:27:04 +00:00
|
|
|
#if defined (SG_MAC)
|
|
|
|
#include <OpenGL/gl.h>
|
|
|
|
#include <GLUT/glut.h>
|
2016-12-04 18:36:53 +00:00
|
|
|
#elif defined (_GLES2)
|
|
|
|
#include <GLES2/gl2.h>
|
2011-06-04 19:27:04 +00:00
|
|
|
#else
|
2016-12-04 18:36:53 +00:00
|
|
|
#include <GL/glew.h> // Must be included before <GL/gl.h>
|
2011-04-14 09:11:17 +00:00
|
|
|
#include <GL/gl.h>
|
|
|
|
#include <GL/glut.h>
|
2011-06-04 19:27:04 +00:00
|
|
|
#endif
|
2011-04-14 09:11:17 +00:00
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
#include "FGGLApplication.hxx"
|
|
|
|
#include "FGPanelApplication.hxx"
|
|
|
|
|
2011-04-14 09:11:17 +00:00
|
|
|
#include <simgear/math/SGMisc.hxx>
|
|
|
|
#include <simgear/misc/sg_path.hxx>
|
|
|
|
#include <simgear/props/props_io.hxx>
|
|
|
|
#include <simgear/structure/exception.hxx>
|
2011-10-08 09:28:28 +00:00
|
|
|
#include <simgear/misc/ResourceManager.hxx>
|
2011-04-14 09:11:17 +00:00
|
|
|
|
|
|
|
#include "panel_io.hxx"
|
|
|
|
#include "ApplicationProperties.hxx"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
inline static string
|
|
|
|
ParseArgs (int argc, char **argv, const string &token) {
|
|
|
|
for (int i = 0; i < argc; i++) {
|
|
|
|
const string arg (argv[i]);
|
|
|
|
if (arg.find (token) == 0) {
|
|
|
|
return arg.substr (token.length ());
|
|
|
|
}
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2012-01-25 16:37:14 +00:00
|
|
|
// define default location of fgdata (use the same as for fgfs)
|
2016-12-04 18:36:53 +00:00
|
|
|
inline static SGPath
|
|
|
|
platformDefaultRoot () {
|
2012-01-25 16:37:14 +00:00
|
|
|
#if defined(__CYGWIN__)
|
2016-12-04 18:36:53 +00:00
|
|
|
return SGPath ("../data");
|
2012-01-25 16:37:14 +00:00
|
|
|
#elif defined(_WIN32)
|
2016-12-04 18:36:53 +00:00
|
|
|
return SGPath ("..\\data");
|
2012-01-25 16:37:14 +00:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
/*
|
|
|
|
The following code looks for the base package inside the application
|
|
|
|
bundle, in the standard Contents/Resources location.
|
|
|
|
*/
|
2016-12-04 18:36:53 +00:00
|
|
|
CFURLRef resourcesUrl = CFBundleCopyResourcesDirectoryURL (CFBundleGetMainBundle ());
|
2012-01-25 16:37:14 +00:00
|
|
|
|
|
|
|
// look for a 'data' subdir
|
2016-12-04 18:36:53 +00:00
|
|
|
CFURLRef dataDir = CFURLCreateCopyAppendingPathComponent (NULL, resourcesUrl, CFSTR ("data"), true);
|
2012-01-25 16:37:14 +00:00
|
|
|
|
|
|
|
// now convert down to a path, and the a c-string
|
2016-12-04 18:36:53 +00:00
|
|
|
CFStringRef path = CFURLCopyFileSystemPath (dataDir, kCFURLPOSIXPathStyle);
|
|
|
|
string root = CFStringGetCStringPtr (path, CFStringGetSystemEncoding ());
|
2012-01-25 16:37:14 +00:00
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
CFRelease (resourcesUrl);
|
|
|
|
CFRelease (dataDir);
|
|
|
|
CFRelease (path);
|
2012-01-25 16:37:14 +00:00
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
return SGPath (root);
|
2012-01-25 16:37:14 +00:00
|
|
|
#else
|
2016-12-04 18:36:53 +00:00
|
|
|
return SGPath (PKGLIBDIR);
|
2012-01-25 16:37:14 +00:00
|
|
|
#endif
|
2016-12-04 18:36:53 +00:00
|
|
|
}
|
2012-01-25 16:37:14 +00:00
|
|
|
|
2011-04-14 09:11:17 +00:00
|
|
|
#include "FGPNGTextureLoader.hxx"
|
|
|
|
#include "FGRGBTextureLoader.hxx"
|
2016-12-04 18:36:53 +00:00
|
|
|
|
2011-04-14 09:11:17 +00:00
|
|
|
static FGPNGTextureLoader pngTextureLoader;
|
|
|
|
static FGRGBTextureLoader rgbTextureLoader;
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
FGPanelApplication::FGPanelApplication (int argc, char **argv) :
|
|
|
|
FGGLApplication ("FlightGear Panel", argc, argv) {
|
|
|
|
sglog().setLogLevels (SG_ALL, SG_WARN);
|
|
|
|
FGCroppedTexture::registerTextureLoader ("png", &pngTextureLoader);
|
|
|
|
FGCroppedTexture::registerTextureLoader ("rgb", &rgbTextureLoader);
|
2012-01-25 16:37:14 +00:00
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
ApplicationProperties::root = platformDefaultRoot ().local8BitStr ();
|
2011-04-14 09:11:17 +00:00
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
const string panelFilename (ParseArgs (argc, argv, "--panel="));
|
|
|
|
const string fgRoot (ParseArgs (argc, argv, "--fg-root="));
|
2011-04-14 09:11:17 +00:00
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
if (fgRoot.length () > 0) {
|
2011-04-14 09:11:17 +00:00
|
|
|
ApplicationProperties::root = fgRoot;
|
2016-12-04 18:36:53 +00:00
|
|
|
}
|
|
|
|
simgear::ResourceManager::instance ()->addBasePath (ApplicationProperties::root);
|
2011-04-14 09:11:17 +00:00
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
if (panelFilename.length () == 0 ) {
|
|
|
|
cerr << "Need a panel filename. Use --panel=path_to_filename" << endl;
|
|
|
|
throw exception ();
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
|
2011-09-30 20:20:00 +00:00
|
|
|
// see if we got a valid fgdata path
|
2016-12-04 18:36:53 +00:00
|
|
|
SGPath BaseCheck (ApplicationProperties::root);
|
|
|
|
BaseCheck.append ("version");
|
|
|
|
if (!BaseCheck.exists ()) {
|
|
|
|
cerr << "Missing base package. Use --fg-root=path_to_fgdata" << endl;
|
|
|
|
throw exception ();
|
2011-09-30 20:20:00 +00:00
|
|
|
}
|
|
|
|
|
2011-04-14 09:11:17 +00:00
|
|
|
try {
|
2016-12-04 18:36:53 +00:00
|
|
|
const SGPath tpath (ApplicationProperties::GetRootPath (panelFilename.c_str ()));
|
|
|
|
readProperties (tpath, ApplicationProperties::Properties);
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
2016-12-04 18:36:53 +00:00
|
|
|
catch (sg_io_exception & e) {
|
|
|
|
cerr << e.getFormattedMessage () << endl;
|
2011-04-14 09:11:17 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
const string arg (argv[i]);
|
|
|
|
if (arg.find ("--prop:") == 0 ) {
|
|
|
|
const string s2 (arg.substr (7));
|
|
|
|
string::size_type p (s2.find ("="));
|
|
|
|
if (p != string::npos) {
|
|
|
|
const string propertyName (s2.substr (0, p));
|
|
|
|
const string propertyValue (s2.substr (p + 1));
|
|
|
|
ApplicationProperties::Properties->getNode (propertyName.c_str (), true )->setValue (propertyValue.c_str ());
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
const SGPropertyNode_ptr n (ApplicationProperties::Properties->getNode ("panel"));
|
|
|
|
if (n != NULL) {
|
|
|
|
panel = FGReadablePanel::read (n);
|
|
|
|
}
|
|
|
|
protocol = new FGPanelProtocol (ApplicationProperties::Properties->getNode ("communication", true));
|
|
|
|
protocol->init ();
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
FGPanelApplication::~FGPanelApplication () {
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
void
|
|
|
|
FGPanelApplication::Run () {
|
|
|
|
#ifdef _GLES2
|
|
|
|
const int mode (0);
|
|
|
|
#else
|
|
|
|
const int mode (GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
|
|
|
|
#endif
|
|
|
|
int w (panel == NULL ? 0 : panel->getWidth ());
|
|
|
|
int h (panel == NULL ? 0 : panel->getHeight ());
|
|
|
|
if (w == 0 && h == 0) {
|
2011-04-14 09:11:17 +00:00
|
|
|
w = 1024;
|
|
|
|
h = 768;
|
2016-12-04 18:36:53 +00:00
|
|
|
} else if (w == 0) {
|
2011-04-14 09:11:17 +00:00
|
|
|
w = h / 0.75;
|
2016-12-04 18:36:53 +00:00
|
|
|
} else if (h == 0) {
|
2011-04-14 09:11:17 +00:00
|
|
|
h = w * 0.75;
|
|
|
|
}
|
2016-12-04 18:36:53 +00:00
|
|
|
|
|
|
|
const bool gameMode (ApplicationProperties::Properties->getNode ( "game-mode", true)->getBoolValue ());
|
|
|
|
FGGLApplication::Run (mode, gameMode, w, h);
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
void
|
|
|
|
FGPanelApplication::Init () {
|
|
|
|
#ifndef _GLES2
|
|
|
|
glutSetCursor (GLUT_CURSOR_NONE);
|
|
|
|
#endif
|
|
|
|
if (panel != NULL) {
|
|
|
|
panel->init ();
|
|
|
|
}
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
void
|
|
|
|
FGPanelApplication::Reshape (const int width, const int height) {
|
2011-04-14 09:11:17 +00:00
|
|
|
this->width = width;
|
|
|
|
this->height = height;
|
2016-12-04 18:36:53 +00:00
|
|
|
glViewport (0, 0, GLsizei (width), GLsizei (height));
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
void
|
|
|
|
FGPanelApplication::Idle () {
|
|
|
|
#ifndef _GLES2
|
|
|
|
const double d (glutGet (GLUT_ELAPSED_TIME));
|
|
|
|
#endif
|
|
|
|
const double dt (Sleep ());
|
|
|
|
if (dt == 0) {
|
2011-04-14 09:11:17 +00:00
|
|
|
return;
|
2016-12-04 18:36:53 +00:00
|
|
|
}
|
|
|
|
if (panel != NULL) {
|
|
|
|
panel->update (dt);
|
|
|
|
}
|
|
|
|
#ifndef _GLES2
|
|
|
|
glutSwapBuffers ();
|
|
|
|
#endif
|
|
|
|
if (protocol != NULL) {
|
|
|
|
protocol->update (dt);
|
|
|
|
}
|
|
|
|
#ifndef _GLES2
|
2011-04-14 09:11:17 +00:00
|
|
|
static double dsum = 0.0;
|
|
|
|
static unsigned cnt = 0;
|
2016-12-04 18:36:53 +00:00
|
|
|
dsum += glutGet (GLUT_ELAPSED_TIME) - d;
|
2011-04-14 09:11:17 +00:00
|
|
|
cnt++;
|
2016-12-04 18:36:53 +00:00
|
|
|
if (dsum > 1000.0) {
|
|
|
|
ApplicationProperties::Properties->getNode ("/sim/frame-rate", true)->setDoubleValue (cnt * 1000.0 / dsum);
|
2011-04-14 09:11:17 +00:00
|
|
|
dsum = 0.0;
|
|
|
|
cnt = 0;
|
|
|
|
}
|
2016-12-04 18:36:53 +00:00
|
|
|
#endif
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
void
|
|
|
|
FGPanelApplication::Key (const unsigned char key, const int x, const int y) {
|
|
|
|
switch (key) {
|
|
|
|
case 0x1b:
|
|
|
|
exit(0);
|
|
|
|
break;
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
double
|
|
|
|
FGPanelApplication::Sleep () {
|
2011-04-14 09:11:17 +00:00
|
|
|
SGTimeStamp current_time_stamp;
|
|
|
|
static SGTimeStamp last_time_stamp;
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
if (last_time_stamp.get_seconds () == 0) {
|
|
|
|
last_time_stamp.stamp ();
|
|
|
|
}
|
|
|
|
const double model_hz (60);
|
|
|
|
const double throttle_hz (ApplicationProperties::getDouble ("/sim/frame-rate-throttle-hz", 0.0));
|
|
|
|
if (throttle_hz > 0.0) {
|
2011-04-14 09:11:17 +00:00
|
|
|
// optionally throttle the frame rate (to get consistent frame
|
|
|
|
// rates or reduce cpu usage.
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
double frame_us (1.0e6 / throttle_hz);
|
2011-04-14 09:11:17 +00:00
|
|
|
|
|
|
|
// sleep based timing loop.
|
|
|
|
//
|
|
|
|
// Calling sleep, even usleep() on linux is less accurate than
|
|
|
|
// we like, but it does free up the cpu for other tasks during
|
|
|
|
// the sleep so it is desirable. Because of the way sleep()
|
|
|
|
// is implemented in consumer operating systems like windows
|
|
|
|
// and linux, you almost always sleep a little longer than the
|
|
|
|
// requested amount.
|
|
|
|
//
|
|
|
|
// To combat the problem of sleeping too long, we calculate the
|
|
|
|
// desired wait time and shorten it by 2000us (2ms) to avoid
|
|
|
|
// [hopefully] over-sleep'ing. The 2ms value was arrived at
|
|
|
|
// via experimentation. We follow this up at the end with a
|
|
|
|
// simple busy-wait loop to get the final pause timing exactly
|
|
|
|
// right.
|
|
|
|
//
|
|
|
|
// Assuming we don't oversleep by more than 2000us, this
|
|
|
|
// should be a reasonable compromise between sleep based
|
|
|
|
// waiting, and busy waiting.
|
|
|
|
|
|
|
|
// sleep() will always overshoot by a bit so undersleep by
|
|
|
|
// 2000us in the hopes of never oversleeping.
|
|
|
|
frame_us -= 2000.0;
|
2016-12-04 18:36:53 +00:00
|
|
|
if (frame_us < 0.0) {
|
2011-04-14 09:11:17 +00:00
|
|
|
frame_us = 0.0;
|
|
|
|
}
|
2016-12-04 18:36:53 +00:00
|
|
|
current_time_stamp.stamp ();
|
2011-04-14 09:11:17 +00:00
|
|
|
|
|
|
|
/* Convert to ms */
|
2016-12-04 18:36:53 +00:00
|
|
|
const double elapsed_us ((current_time_stamp - last_time_stamp).toUSecs ());
|
|
|
|
if (elapsed_us < frame_us) {
|
|
|
|
const double requested_us (frame_us - elapsed_us);
|
2011-05-27 18:53:16 +00:00
|
|
|
#ifdef _WIN32
|
2016-12-04 18:36:53 +00:00
|
|
|
::Sleep (int (requested_us / 1000.0));
|
2011-05-27 18:53:16 +00:00
|
|
|
#else
|
2016-12-04 18:36:53 +00:00
|
|
|
usleep (useconds_t (requested_us));
|
2011-05-27 18:53:16 +00:00
|
|
|
#endif
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
// busy wait timing loop.
|
|
|
|
//
|
|
|
|
// This yields the most accurate timing. If the previous
|
|
|
|
// usleep() call is omitted this will peg the cpu
|
|
|
|
// (which is just fine if FG is the only app you care about.)
|
2016-12-04 18:36:53 +00:00
|
|
|
current_time_stamp.stamp ();
|
|
|
|
const SGTimeStamp next_time_stamp (last_time_stamp + SGTimeStamp::fromSec (1e-6*frame_us));
|
|
|
|
while (current_time_stamp < next_time_stamp) {
|
|
|
|
current_time_stamp.stamp ();
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2016-12-04 18:36:53 +00:00
|
|
|
current_time_stamp.stamp ();
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|
|
|
|
|
2016-12-04 18:36:53 +00:00
|
|
|
static double reminder = 0.0;
|
|
|
|
static long global_multi_loop = 0;
|
|
|
|
const double real_delta_time_sec ((double (current_time_stamp.toUSecs () - last_time_stamp.toUSecs ()) / 1000000.0) + reminder);
|
2011-04-14 09:11:17 +00:00
|
|
|
last_time_stamp = current_time_stamp;
|
|
|
|
//fprintf(stdout,"\r%4.1lf ", 1/real_delta_time_sec );
|
|
|
|
//fflush(stdout);
|
|
|
|
|
|
|
|
// round the real time down to a multiple of 1/model-hz.
|
|
|
|
// this way all systems are updated the _same_ amount of dt.
|
2016-12-04 18:36:53 +00:00
|
|
|
global_multi_loop = long (floor (real_delta_time_sec * model_hz));
|
|
|
|
global_multi_loop = SGMisc<long>::max (0, global_multi_loop);
|
|
|
|
reminder = real_delta_time_sec - double (global_multi_loop) / double (model_hz);
|
|
|
|
return double (global_multi_loop) / double (model_hz);
|
2011-04-14 09:11:17 +00:00
|
|
|
}
|