Skip to content

Commit

Permalink
Updated to Mapbox GL 0.5 API
Browse files Browse the repository at this point in the history
The main change is that we now drive rendering from the main thread.
  • Loading branch information
tmpsantos committed Jul 20, 2015
1 parent 4a05283 commit d99df20
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 33 deletions.
3 changes: 2 additions & 1 deletion qmapboxgl.pro
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ PKGCONFIG += libmbgl
SOURCES += example/example.cpp \
src/qmapboxgl.cpp

HEADERS += include/qmapboxgl.h
HEADERS += include/qmapboxgl.h \
src/qmapboxgl_p.h

RESOURCES += styles/styles.qrc

Expand Down
84 changes: 61 additions & 23 deletions src/qmapboxgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,42 +31,42 @@ QMapboxGL::~QMapboxGL()

void QMapboxGL::setAccessToken(const QString &token)
{
d_ptr->fileSource.setAccessToken(token.toUtf8().constData());
d_ptr->fileSourceObj.setAccessToken(token.toUtf8().constData());
}

void QMapboxGL::setStyleJSON(const QString &style)
{
d_ptr->map.setStyleJSON(style.toUtf8().constData());
d_ptr->mapObj.setStyleJSON(style.toUtf8().constData());
}

double QMapboxGL::latitude() const
{
return d_ptr->map.getLatLng().latitude;
return d_ptr->mapObj.getLatLng().latitude;
}

void QMapboxGL::setLatitude(double latitude)
{
d_ptr->map.setLatLng({latitude, longitude()});
d_ptr->mapObj.setLatLng({latitude, longitude()});
}

double QMapboxGL::longitude() const
{
return d_ptr->map.getLatLng().longitude;
return d_ptr->mapObj.getLatLng().longitude;
}

void QMapboxGL::setLongitude(double longitude)
{
d_ptr->map.setLatLng({latitude(), longitude});
d_ptr->mapObj.setLatLng({latitude(), longitude});
}

double QMapboxGL::zoom() const
{
return d_ptr->map.getZoom();
return d_ptr->mapObj.getZoom();
}

void QMapboxGL::setZoom(double zoom)
{
d_ptr->map.setZoom(zoom);
d_ptr->mapObj.setZoom(zoom);
}

void QMapboxGL::mousePressEvent(QMouseEvent *ev)
Expand Down Expand Up @@ -94,7 +94,7 @@ void QMapboxGL::mouseMoveEvent(QMouseEvent *ev)
d_ptr->lastY = ev->y();

if (dx || dy) {
d_ptr->map.moveBy(dx, dy);
d_ptr->mapObj.moveBy(dx, dy);
}

ev->accept();
Expand All @@ -105,49 +105,77 @@ void QMapboxGL::wheelEvent(QWheelEvent *ev)
QPoint numDegrees = ev->angleDelta();

if (numDegrees.y() > 0) {
d_ptr->map.scaleBy(1.10, ev->x(), ev->y());
d_ptr->mapObj.scaleBy(1.10, ev->x(), ev->y());
} else {
d_ptr->map.scaleBy(0.90, ev->x(), ev->y());
d_ptr->mapObj.scaleBy(0.90, ev->x(), ev->y());
}

ev->accept();
}

void QMapboxGL::resizeGL(int w, int h)
{
#ifdef Q_OS_LINUX
// FIXME: Find out why sprites don't get rendered
// correctly if I use 1.0 here.
d_ptr->map.resize(w, h, devicePixelRatio() + .00001);
#else
d_ptr->map.resize(w, h, devicePixelRatio());
#endif
d_ptr->size = { static_cast<uint16_t>(w), static_cast<uint16_t>(h) };
d_ptr->mapObj.update(mbgl::Update::Dimensions);
}

QMapboxGLPrivate::QMapboxGLPrivate(QMapboxGL *q)
: fileSource(nullptr)
, map(*this, fileSource)
: fileSourceObj(nullptr)
, mapObj(*this, fileSourceObj)
, q_ptr(q)
{
connect(this, SIGNAL(viewInvalidated(void)), this, SLOT(triggerRender()), Qt::QueuedConnection);
}

QMapboxGLPrivate::~QMapboxGLPrivate()
{
}

float QMapboxGLPrivate::getPixelRatio() const
{
return 1.0;
}

std::array<uint16_t, 2> QMapboxGLPrivate::getSize() const
{
return size;
}

std::array<uint16_t, 2> QMapboxGLPrivate::getFramebufferSize() const
{
return size;
}

void QMapboxGLPrivate::initialize(mbgl::Map *map_)
{
View::initialize(map_);
}

void QMapboxGLPrivate::activate()
{
// Map thread
}

void QMapboxGLPrivate::deactivate()
{
// Map thread
context->doneCurrent();
}

void QMapboxGLPrivate::invalidate(std::function<void()> renderCallback)
void QMapboxGLPrivate::notify()
{
// Map thread
}

void QMapboxGLPrivate::invalidate()
{
// Map thread
if (!q_ptr->isExposed()) {
return;
}

if (!context) {
context = new QOpenGLContext();
context.reset(new QOpenGLContext());
context->setFormat(q_ptr->requestedFormat());
context->create();
context->makeCurrent(q_ptr);
Expand All @@ -158,7 +186,17 @@ void QMapboxGLPrivate::invalidate(std::function<void()> renderCallback)
});
}

renderCallback();
emit viewInvalidated();
}

void QMapboxGLPrivate::swap()
{
// Map thread
context->swapBuffers(q_ptr);
}

void QMapboxGLPrivate::triggerRender() {
mapObj.renderSync();
}

#include "moc_qmapboxgl_p.cpp"
36 changes: 27 additions & 9 deletions src/qmapboxgl_p.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef QMAPBOXGL_P_H
#define QMAPBOXGL_P_H

#include <QObject>
#include <QScopedPointer>

#include <mbgl/map/map.hpp>
#include <mbgl/map/view.hpp>
#include <mbgl/storage/default_file_source.hpp>
Expand All @@ -14,27 +17,42 @@ class FileSource;

class QOpenGLContext;

class QMapboxGLPrivate : public mbgl::View
class QMapboxGLPrivate : public QObject, public mbgl::View
{
Q_OBJECT

public:
explicit QMapboxGLPrivate(QMapboxGL *q);
virtual ~QMapboxGLPrivate();

// mbgl::View implementation.
void activate() final {}
void deactivate() final;
void notify() final {}
void invalidate(std::function<void()> renderCallback) final;

mbgl::DefaultFileSource fileSource;
mbgl::Map map;
float getPixelRatio() const override;
std::array<uint16_t, 2> getSize() const override;
std::array<uint16_t, 2> getFramebufferSize() const override;
void initialize(mbgl::Map *map) override;
void activate() override;
void deactivate() override;
void notify() override;
void invalidate() override;
void swap() override;

int lastX = 0;
int lastY = 0;

QOpenGLContext *context = nullptr;
std::array<uint16_t, 2> size;

QScopedPointer<QOpenGLContext> context;

mbgl::DefaultFileSource fileSourceObj;
mbgl::Map mapObj;

QMapboxGL *q_ptr;

signals:
void viewInvalidated();

public slots:
void triggerRender();
};

#endif // QMAPBOXGL_P_H

0 comments on commit d99df20

Please sign in to comment.