2008-05-19 21:21:03 +00:00
|
|
|
// Copyright (C) 2008 Tim Moore
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
#ifndef FLIGHTGEAR_WINDOWSYSTEMADAPTER_HXX
|
|
|
|
#define FLIGHTGEAR_WINDOWSYSTEMADAPTER_HXX 1
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <osg/Referenced>
|
|
|
|
#include <osg/Camera>
|
|
|
|
#include <osg/GraphicsThread>
|
2008-08-01 15:57:29 +00:00
|
|
|
#include <osg/ref_ptr>
|
2008-05-19 21:21:03 +00:00
|
|
|
|
|
|
|
#include <simgear/structure/SGAtomic.hxx>
|
|
|
|
|
2008-08-01 15:57:29 +00:00
|
|
|
namespace osg
|
|
|
|
{
|
|
|
|
class GraphicsContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flexible window support
|
2008-05-19 21:21:03 +00:00
|
|
|
namespace flightgear
|
|
|
|
{
|
2008-08-01 15:57:29 +00:00
|
|
|
/** A window with a graphics context and an integer ID
|
2008-05-19 21:21:03 +00:00
|
|
|
*/
|
|
|
|
class GraphicsWindow : public osg::Referenced
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GraphicsWindow(osg::GraphicsContext* gc_, const std::string& name_,
|
|
|
|
int id_, unsigned flags_ = 0) :
|
|
|
|
gc(gc_), name(name_), id(id_), flags(flags_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
/** The OSG graphics context for this window.
|
|
|
|
*/
|
|
|
|
osg::ref_ptr<osg::GraphicsContext> gc;
|
|
|
|
/** The window's internal name.
|
|
|
|
*/
|
|
|
|
std::string name;
|
2008-08-01 15:57:29 +00:00
|
|
|
/** A unique ID for the window.
|
|
|
|
*/
|
2008-05-19 21:21:03 +00:00
|
|
|
int id;
|
|
|
|
enum Flags {
|
2008-08-01 15:57:29 +00:00
|
|
|
GUI = 1 /**< The GUI (and 2D cockpit) will be drawn on this window. */
|
2008-05-19 21:21:03 +00:00
|
|
|
};
|
2008-08-01 15:57:29 +00:00
|
|
|
/** Flags for the window.
|
|
|
|
*/
|
2008-05-19 21:21:03 +00:00
|
|
|
unsigned flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<osg::ref_ptr<GraphicsWindow> > WindowVector;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An operation that is run once with a particular GraphicsContext
|
2008-08-01 15:57:29 +00:00
|
|
|
* current. It will probably be deferred and may run in a different
|
|
|
|
* thread.
|
2008-05-19 21:21:03 +00:00
|
|
|
*/
|
|
|
|
class GraphicsContextOperation : public osg::GraphicsOperation
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GraphicsContextOperation(const std::string& name) :
|
|
|
|
osg::GraphicsOperation(name, false)
|
|
|
|
{
|
|
|
|
}
|
2008-08-01 15:57:29 +00:00
|
|
|
/** Don't override this!
|
|
|
|
*/
|
2008-05-19 21:21:03 +00:00
|
|
|
virtual void operator()(osg::GraphicsContext* gc);
|
2008-08-01 15:57:29 +00:00
|
|
|
/** The body of the operation.
|
|
|
|
*/
|
2008-05-19 21:21:03 +00:00
|
|
|
virtual void run(osg::GraphicsContext* gc) = 0;
|
2008-08-01 15:57:29 +00:00
|
|
|
/** Test if the operation has completed.
|
|
|
|
* @return true if the run() method has finished.
|
|
|
|
*/
|
2008-05-19 21:21:03 +00:00
|
|
|
bool isFinished() const { return done != 0; }
|
|
|
|
private:
|
|
|
|
SGAtomic done;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Adapter from windows system / graphics context management API to
|
|
|
|
* functions used by flightgear. This papers over the difference
|
2008-08-01 15:57:29 +00:00
|
|
|
* between osgViewer::Viewer, which handles multiple windows, graphics
|
2008-05-19 21:21:03 +00:00
|
|
|
* threads, etc., and the embedded viewer used with GLUT and SDL.
|
|
|
|
*/
|
|
|
|
class WindowSystemAdapter : public osg::Referenced
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WindowSystemAdapter();
|
|
|
|
virtual ~WindowSystemAdapter() {}
|
2008-08-01 15:57:29 +00:00
|
|
|
/** Vector of all the registered windows.
|
|
|
|
*/
|
2008-05-19 21:21:03 +00:00
|
|
|
WindowVector windows;
|
2008-08-01 15:57:29 +00:00
|
|
|
/** Register a window, assigning it an ID.
|
|
|
|
* @param gc graphics context
|
|
|
|
* @param windowName internal name (not displayed)
|
|
|
|
* @return a graphics window
|
|
|
|
*/
|
2008-05-19 21:21:03 +00:00
|
|
|
GraphicsWindow* registerWindow(osg::GraphicsContext* gc,
|
|
|
|
const std::string& windowName);
|
|
|
|
/** Initialize the plib pui interface library. This might happen
|
|
|
|
*in another thread and may be deferred.
|
|
|
|
*/
|
2008-05-20 06:35:37 +00:00
|
|
|
virtual void puInitialize();
|
2008-08-01 15:57:29 +00:00
|
|
|
/** Find a window by name.
|
|
|
|
* @param name the window name
|
|
|
|
* @return the window or 0
|
|
|
|
*/
|
|
|
|
GraphicsWindow* findWindow(const std::string& name);
|
|
|
|
/** Get the global WindowSystemAdapter
|
|
|
|
* @return the adapter
|
2008-05-19 21:21:03 +00:00
|
|
|
*/
|
|
|
|
static WindowSystemAdapter* getWSA() { return _wsa.get(); }
|
2008-08-01 15:57:29 +00:00
|
|
|
/** Set the global adapter
|
|
|
|
* @param wsa the adapter
|
|
|
|
*/
|
2008-05-19 21:21:03 +00:00
|
|
|
static void setWSA(WindowSystemAdapter* wsa) { _wsa = wsa; }
|
|
|
|
protected:
|
|
|
|
int _nextWindowID;
|
|
|
|
osg::ref_ptr<GraphicsContextOperation> _puInitOp;
|
|
|
|
bool _isPuInitialized;
|
|
|
|
static osg::ref_ptr<WindowSystemAdapter> _wsa;
|
|
|
|
// Default callbacks for plib
|
|
|
|
static int puGetWindow();
|
|
|
|
static void puGetWindowSize(int* width, int* height);
|
|
|
|
|
|
|
|
};
|
2008-08-01 15:57:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for testing if flags are set in an object with a flags member.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
class FlagTester : public std::unary_function<osg::ref_ptr<T>, bool>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** Initialize with flags to test for.
|
|
|
|
* @param flags logical or of flags to test.
|
|
|
|
*/
|
|
|
|
FlagTester(unsigned flags_) : flags(flags_) {}
|
|
|
|
/** test operator
|
|
|
|
* @param obj An object with a flags member
|
|
|
|
* @return true if flags member of obj contains any of the flags
|
|
|
|
* (bitwise and with flags is nonzero).
|
|
|
|
*/
|
|
|
|
bool operator() (const osg::ref_ptr<T>& obj)
|
|
|
|
{
|
|
|
|
return (obj->flags & flags) != 0;
|
|
|
|
}
|
|
|
|
unsigned flags;
|
|
|
|
};
|
|
|
|
|
2008-05-19 21:21:03 +00:00
|
|
|
}
|
|
|
|
#endif
|