StelAppGraphicsWidget.cpp   StelAppGraphicsWidget.cpp 
skipping to change at line 23 skipping to change at line 23
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA. * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*/ */
#include "StelApp.hpp" #include "StelApp.hpp"
#include "StelCore.hpp" #include "StelCore.hpp"
#include "StelAppGraphicsWidget.hpp" #include "StelAppGraphicsWidget.hpp"
#include "StelPainter.hpp" #include "StelRenderer.hpp"
#include "StelGuiBase.hpp" #include "StelGuiBase.hpp"
#include "StelViewportEffect.hpp" #include "StelViewportEffect.hpp"
#include <QPainter> #include <QPainter>
#include <QPaintEngine> #include <QPaintEngine>
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QGLFramebufferObject> #include <QGLFramebufferObject>
#include <QSettings> #include <QSettings>
StelAppGraphicsWidget::StelAppGraphicsWidget() //! Provides access to paintPartial and drawing related classes to Renderer
: paintState(0), useBuffers(false), backgroundBuffer(NULL), foregrou .
ndBuffer(NULL), viewportEffect(NULL), doPaint(true) //!
//! This is basically just a callback object, it is created and passed
//! on every frame.
//!
//! Renderer uses drawPartial to draw parts of the scene, allowing it
//! to measure rendering time and suspend rendering if it's too slow.
//!
//! @see StelRenderClient
class StelAppGraphicsWidgetRenderClient : public StelRenderClient
{
public:
//! Construct a StelAppGraphicsWidgetRenderClient providing access t
o specified widget.
//!
//! @param widget Widget that manages drawing of the scene.
//! @param painter Painter that paints on the widget.
explicit StelAppGraphicsWidgetRenderClient(StelAppGraphicsWidget* wi
dget,
QPainter* painter)
: StelRenderClient()
, widget(widget)
, painter(painter)
{
}
virtual bool drawPartial() {return widget->paintPartial();}
virtual QPainter* getPainter()
{
return painter;
}
virtual StelViewportEffect* getViewportEffect() {return widget->view
portEffect;}
private:
//! Widget that manages drawing of the scene.
StelAppGraphicsWidget* const widget;
//! QPainter that paints on the graphics widget.
QPainter* const painter;
};
StelAppGraphicsWidget::StelAppGraphicsWidget(StelRenderer* renderer)
: paintState(0),
renderer(renderer),
viewportEffect(NULL),
doPaint(true)
{ {
previousPaintTime = StelApp::getTotalRunTime(); previousPaintTime = StelApp::getTotalRunTime();
setFocusPolicy(Qt::StrongFocus); setFocusPolicy(Qt::StrongFocus);
stelApp = new StelApp(); stelApp = new StelApp();
} }
StelAppGraphicsWidget::~StelAppGraphicsWidget() StelAppGraphicsWidget::~StelAppGraphicsWidget()
{ {
delete stelApp; delete stelApp;
stelApp = NULL; stelApp = NULL;
if (backgroundBuffer) delete backgroundBuffer;
backgroundBuffer = NULL;
if (foregroundBuffer) delete foregroundBuffer;
foregroundBuffer = NULL;
if (viewportEffect) delete viewportEffect; if (viewportEffect) delete viewportEffect;
viewportEffect = NULL; viewportEffect = NULL;
StelApp::deinitStatic(); StelApp::deinitStatic();
} }
void StelAppGraphicsWidget::init(QSettings* conf) void StelAppGraphicsWidget::init(QSettings* conf)
{ {
stelApp->init(conf); stelApp->init(conf, renderer);
Q_ASSERT(viewportEffect==NULL); Q_ASSERT(viewportEffect==NULL);
setViewportEffect(conf->value("video/viewport_effect", "none").toStr ing()); setViewportEffect(conf->value("video/viewport_effect", "none").toStr ing());
renderer->viewportHasBeenResized(scene()->sceneRect().size().toSize( ));
//previousPaintTime needs to be updated after the time zone is set //previousPaintTime needs to be updated after the time zone is set
//in StelLocaleMgr::init(), otherwise this causes an invalid value o f //in StelLocaleMgr::init(), otherwise this causes an invalid value o f
//deltaT the first time it is calculated in paintPartial(), which in //deltaT the first time it is calculated in paintPartial(), which in
//turn causes Stellarium to start with a wrong time. //turn causes Stellarium to start with a wrong time.
previousPaintTime = StelApp::getTotalRunTime(); previousPaintTime = StelApp::getTotalRunTime();
} }
void StelAppGraphicsWidget::setViewportEffect(const QString& name) void StelAppGraphicsWidget::setViewportEffect(const QString& name)
{ {
if (viewportEffect) if (viewportEffect)
{ {
if (viewportEffect->getName()==name) if (viewportEffect->getName()==name)
return; return;
delete viewportEffect; delete viewportEffect;
viewportEffect=NULL; viewportEffect=NULL;
} }
if (name=="none")
{
useBuffers = false;
return;
}
if (!QGLFramebufferObject::hasOpenGLFramebufferObjects())
{
qWarning() << "Don't support OpenGL framebuffer objects, can
't use Viewport effect: " << name;
useBuffers = false;
return;
}
qDebug() << "Use OpenGL framebuffer objects for viewport effect: " < if (name == "none")
< name;
useBuffers = true;
if (name == "framebufferOnly")
{ {
viewportEffect = new StelViewportEffect(); qDebug() << "Not using any viewport effect";
} }
else if (name == "sphericMirrorDistorter") else if (name == "sphericMirrorDistorter")
{ {
viewportEffect = new StelViewportDistorterFisheyeToSphericMi qDebug() << "Setting sphericMirrorDistorter viewport effect"
rror(size().width(), size().height()); ;
viewportEffect = new StelViewportDistorterFisheyeToSphericMi
rror(size().width(),
size().height(),
renderer);
} }
else else
{ {
qWarning() << "Unknown viewport effect name: " << name; qWarning() << "Unknown viewport effect name (using no effect
useBuffers=false; ): " << name;
} }
} }
QString StelAppGraphicsWidget::getViewportEffect() const QString StelAppGraphicsWidget::getViewportEffect() const
{ {
if (viewportEffect) if (viewportEffect)
return viewportEffect->getName(); return viewportEffect->getName();
return "none"; return "none";
} }
skipping to change at line 146 skipping to change at line 176
return false; return false;
// Update the core and all modules // Update the core and all modules
stelApp->update(dt); stelApp->update(dt);
paintState = 1; paintState = 1;
return true; return true;
} }
if (paintState == 1) if (paintState == 1)
{ {
// And draw them // And draw them
if (stelApp->drawPartial()) if (stelApp->drawPartial(renderer))
return true; return true;
paintState = 0; paintState = 0;
return false; return false;
} }
Q_ASSERT(false); Q_ASSERT(false);
return false; return false;
} }
void StelAppGraphicsWidget::paint(QPainter* painter, const QStyleOptionGrap hicsItem*, QWidget*) void StelAppGraphicsWidget::paint(QPainter* painter, const QStyleOptionGrap hicsItem*, QWidget*)
{ {
// Don't even try to draw if we don't have a core yet (fix a bug dur ing splash screen) // Don't even try to draw if we don't have a core yet (fix a bug dur ing splash screen)
if (!stelApp || !stelApp->getCore() || !doPaint) if (!stelApp || !stelApp->getCore() || !doPaint)
return; return;
StelPainter::setQPainter(painter); StelAppGraphicsWidgetRenderClient client(this, painter);
if (useBuffers) // Renderer manages the rendering process in detail, e.g.
{ // deciding whether or not to suspend drawing because FPS is too low
StelPainter::makeMainGLContextCurrent(); .
initBuffers(); renderer->renderFrame(client);
backgroundBuffer->bind();
QPainter* pa = new QPainter(backgroundBuffer);
StelPainter::setQPainter(pa);
// If we are using the gui, then we try to have the best rea
ctivity, even if we need to lower the fps for that.
int minFps = StelApp::getInstance().getGui()->isCurrentlyUse
d() ? 16 : 2;
while (true)
{
bool keep = paintPartial();
if (!keep) // The paint is done
{
delete pa;
backgroundBuffer->release();
swapBuffers();
break;
}
double spentTime = StelApp::getTotalRunTime() - prev
iousPaintFrameTime;
if (1. / spentTime <= minFps) // we spent too much t
ime
{
// We stop the painting operation for now
delete pa;
backgroundBuffer->release();
break;
}
}
Q_ASSERT(!backgroundBuffer->isBound());
Q_ASSERT(!foregroundBuffer->isBound());
// Paint the last completed painted buffer
StelPainter::setQPainter(painter);
viewportEffect->paintViewportBuffer(foregroundBuffer);
}
else
{
while (paintPartial()) {;}
}
StelPainter::setQPainter(NULL);
previousPaintFrameTime = StelApp::getTotalRunTime();
}
//! Swap the buffers
//! this should be called after we finish the paint
void StelAppGraphicsWidget::swapBuffers()
{
Q_ASSERT(useBuffers);
QGLFramebufferObject* tmp = backgroundBuffer;
backgroundBuffer = foregroundBuffer;
foregroundBuffer = tmp;
}
//! Initialize the opengl buffer objects.
void StelAppGraphicsWidget::initBuffers()
{
Q_ASSERT(useBuffers);
Q_ASSERT(QGLFramebufferObject::hasOpenGLFramebufferObjects());
if (!backgroundBuffer)
{
backgroundBuffer = new QGLFramebufferObject(scene()->sceneRe
ct().size().toSize(), QGLFramebufferObject::CombinedDepthStencil);
foregroundBuffer = new QGLFramebufferObject(scene()->sceneRe
ct().size().toSize(), QGLFramebufferObject::CombinedDepthStencil);
Q_ASSERT(backgroundBuffer->isValid());
Q_ASSERT(foregroundBuffer->isValid());
}
} }
void StelAppGraphicsWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event) void StelAppGraphicsWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{ {
// Apply distortion on the mouse position. // Apply distortion on the mouse position.
QPointF pos = event->scenePos(); QPointF pos = event->scenePos();
distortPos(&pos); distortPos(&pos);
pos.setY(scene()->height() - 1 - pos.y()); pos.setY(scene()->height() - 1 - pos.y());
stelApp->handleMove(pos.x(), pos.y(), event->buttons()); stelApp->handleMove(pos.x(), pos.y(), event->buttons());
} }
skipping to change at line 285 skipping to change at line 253
} }
void StelAppGraphicsWidget::keyReleaseEvent(QKeyEvent* event) void StelAppGraphicsWidget::keyReleaseEvent(QKeyEvent* event)
{ {
stelApp->handleKeys(event); stelApp->handleKeys(event);
} }
void StelAppGraphicsWidget::resizeEvent(QGraphicsSceneResizeEvent* event) void StelAppGraphicsWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
{ {
QGraphicsWidget::resizeEvent(event); QGraphicsWidget::resizeEvent(event);
stelApp->glWindowHasBeenResized(scenePos().x(), scene()->sceneRect() const float w = geometry().width();
.height()-(scenePos().y()+geometry().height()), geometry().width(), geometr const float h = geometry().height();
y().height()); const float x = scenePos().x();
if (backgroundBuffer) const float y = scene()->sceneRect().height() - (scenePos().y() + ge
{ ometry().height());
delete backgroundBuffer;
backgroundBuffer = NULL; stelApp->windowHasBeenResized(x, y, w, h);
} renderer->viewportHasBeenResized(scene()->sceneRect().size().toSize(
if (foregroundBuffer) ));
{
delete foregroundBuffer;
foregroundBuffer = NULL;
}
} }
 End of changes. 14 change blocks. 
117 lines changed or deleted 81 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/