diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2016-11-22 12:29:50 +0100 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2016-11-22 20:58:51 +0100 |
commit | 67ed3ae3eb25e5bb0ec2d0369d042438bd9b2c7f (patch) | |
tree | a71ef8fdf2ec4d080ceef8e4106e8db9ef5a26ff /platform | |
parent | 9353fcbf4cde1f8ca2e0d30cabeb64aa3528682e (diff) | |
download | qtlocation-mapboxgl-67ed3ae3eb25e5bb0ec2d0369d042438bd9b2c7f.tar.gz |
[build] move GLFW-related files to platform/glfw
Diffstat (limited to 'platform')
-rw-r--r-- | platform/glfw/glfw_view.cpp (renamed from platform/default/glfw_view.cpp) | 3 | ||||
-rw-r--r-- | platform/glfw/glfw_view.hpp | 111 | ||||
-rw-r--r-- | platform/glfw/main.cpp | 190 | ||||
-rw-r--r-- | platform/glfw/settings_json.cpp (renamed from platform/default/settings_json.cpp) | 2 | ||||
-rw-r--r-- | platform/glfw/settings_json.hpp | 24 |
5 files changed, 328 insertions, 2 deletions
diff --git a/platform/default/glfw_view.cpp b/platform/glfw/glfw_view.cpp index 07959b2002..ecc5e73da1 100644 --- a/platform/default/glfw_view.cpp +++ b/platform/glfw/glfw_view.cpp @@ -1,4 +1,5 @@ -#include <mbgl/platform/default/glfw_view.hpp> +#include "glfw_view.hpp" + #include <mbgl/annotation/annotation.hpp> #include <mbgl/sprite/sprite_image.hpp> #include <mbgl/style/transition_options.hpp> diff --git a/platform/glfw/glfw_view.hpp b/platform/glfw/glfw_view.hpp new file mode 100644 index 0000000000..c640f188f9 --- /dev/null +++ b/platform/glfw/glfw_view.hpp @@ -0,0 +1,111 @@ +#pragma once + +#include <mbgl/mbgl.hpp> +#include <mbgl/map/backend.hpp> +#include <mbgl/util/run_loop.hpp> +#include <mbgl/util/timer.hpp> +#include <mbgl/util/geometry.hpp> + +#if MBGL_USE_GLES2 +#define GLFW_INCLUDE_ES2 +#endif +#define GL_GLEXT_PROTOTYPES +#include <GLFW/glfw3.h> + +class GLFWView : public mbgl::View, public mbgl::Backend { +public: + GLFWView(bool fullscreen = false, bool benchmark = false); + ~GLFWView() override; + + float getPixelRatio() const; + + void setMap(mbgl::Map*); + + // Callback called when the user presses the key mapped to style change. + // The expected action is to set a new style, different to the current one. + void setChangeStyleCallback(std::function<void()> callback); + + void setShouldClose(); + + void setWindowTitle(const std::string&); + + void run(); + + // mbgl::View implementation + void updateViewBinding(); + void bind() override; + mbgl::Size getSize() const; + mbgl::Size getFramebufferSize() const; + + // mbgl::Backend implementation + void activate() override; + void deactivate() override; + void invalidate() override; + +private: + // Window callbacks + static void onKey(GLFWwindow *window, int key, int scancode, int action, int mods); + static void onScroll(GLFWwindow *window, double xoffset, double yoffset); + static void onWindowResize(GLFWwindow *window, int width, int height); + static void onFramebufferResize(GLFWwindow *window, int width, int height); + static void onMouseClick(GLFWwindow *window, int button, int action, int modifiers); + static void onMouseMove(GLFWwindow *window, double x, double y); + + // Internal + void report(float duration); + + void setMapChangeCallback(std::function<void(mbgl::MapChange)> callback); + void notifyMapChange(mbgl::MapChange change) override; + + mbgl::Color makeRandomColor() const; + mbgl::Point<double> makeRandomPoint() const; + static std::shared_ptr<const mbgl::SpriteImage> + makeSpriteImage(int width, int height, float pixelRatio); + + void nextOrientation(); + + void addRandomPointAnnotations(int count); + void addRandomLineAnnotations(int count); + void addRandomShapeAnnotations(int count); + void addRandomCustomPointAnnotations(int count); + + void clearAnnotations(); + void popAnnotation(); + + mbgl::AnnotationIDs annotationIDs; + std::vector<std::string> spriteIDs; + + std::function<void(mbgl::MapChange)> mapChangeCallback; + +private: + mbgl::Map* map = nullptr; + + bool fullscreen = false; + const bool benchmark = false; + bool tracking = false; + bool rotating = false; + bool pitching = false; + + // Frame timer + int frames = 0; + float frameTime = 0; + double lastReported = 0; + + int width = 1024; + int height = 768; + int fbWidth; + int fbHeight; + float pixelRatio; + + double lastX = 0, lastY = 0; + + double lastClick = -1; + + std::function<void()> changeStyleCallback; + + mbgl::util::RunLoop runLoop; + mbgl::util::Timer frameTick; + + GLFWwindow *window = nullptr; + bool dirty = false; +}; diff --git a/platform/glfw/main.cpp b/platform/glfw/main.cpp new file mode 100644 index 0000000000..6e4b324c43 --- /dev/null +++ b/platform/glfw/main.cpp @@ -0,0 +1,190 @@ +#include "glfw_view.hpp" +#include "settings_json.hpp" + +#include <mbgl/util/default_styles.hpp> +#include <mbgl/platform/log.hpp> +#include <mbgl/platform/platform.hpp> +#include <mbgl/platform/default/thread_pool.hpp> +#include <mbgl/storage/default_file_source.hpp> + +#include <signal.h> +#include <getopt.h> +#include <fstream> +#include <sstream> +#include <cstdlib> +#include <cstdio> +#include <array> + +namespace { + +GLFWView* view = nullptr; + +} + +void quit_handler(int) { + if (view) { + mbgl::Log::Info(mbgl::Event::Setup, "waiting for quit..."); + view->setShouldClose(); + } else { + exit(0); + } +} + +int main(int argc, char *argv[]) { + bool fullscreen = false; + bool benchmark = false; + std::string style; + double latitude = 0, longitude = 0; + double bearing = 0, zoom = 1, pitch = 0; + bool skipConfig = false; + + const struct option long_options[] = { + {"fullscreen", no_argument, 0, 'f'}, + {"benchmark", no_argument, 0, 'b'}, + {"style", required_argument, 0, 's'}, + {"lon", required_argument, 0, 'x'}, + {"lat", required_argument, 0, 'y'}, + {"zoom", required_argument, 0, 'z'}, + {"bearing", required_argument, 0, 'r'}, + {"pitch", required_argument, 0, 'p'}, + {0, 0, 0, 0} + }; + + while (true) { + int option_index = 0; + int opt = getopt_long(argc, argv, "fbs:", long_options, &option_index); + if (opt == -1) break; + switch (opt) + { + case 0: + if (long_options[option_index].flag != 0) + break; + case 'f': + fullscreen = true; + break; + case 'b': + benchmark = true; + break; + case 's': + style = std::string("asset://") + std::string(optarg); + break; + case 'x': + longitude = atof(optarg); + skipConfig = true; + break; + case 'y': + latitude = atof(optarg); + skipConfig = true; + break; + case 'z': + zoom = atof(optarg); + skipConfig = true; + break; + case 'r': + bearing = atof(optarg); + skipConfig = true; + break; + case 'p': + pitch = atof(optarg); + skipConfig = true; + break; + default: + break; + } + + } + + // sigint handling + struct sigaction sigIntHandler; + sigIntHandler.sa_handler = quit_handler; + sigemptyset(&sigIntHandler.sa_mask); + sigIntHandler.sa_flags = 0; + sigaction(SIGINT, &sigIntHandler, NULL); + + if (benchmark) { + mbgl::Log::Info(mbgl::Event::General, "BENCHMARK MODE: Some optimizations are disabled."); + } + + GLFWView backend(fullscreen, benchmark); + view = &backend; + + mbgl::DefaultFileSource fileSource("/tmp/mbgl-cache.db", "."); + + // Set access token if present + const char *token = getenv("MAPBOX_ACCESS_TOKEN"); + if (token == nullptr) { + mbgl::Log::Warning(mbgl::Event::Setup, "no access token set. mapbox.com tiles won't work."); + } else { + fileSource.setAccessToken(std::string(token)); + } + + mbgl::ThreadPool threadPool(4); + + mbgl::Map map(backend, view->getSize(), view->getPixelRatio(), fileSource, threadPool); + + backend.setMap(&map); + + // Load settings + mbgl::Settings_JSON settings; + + if (skipConfig) { + map.setLatLngZoom(mbgl::LatLng(latitude, longitude), zoom); + map.setBearing(bearing); + map.setPitch(pitch); + mbgl::Log::Info(mbgl::Event::General, "Location: %f/%f (z%.2f, %.2f deg)", latitude, longitude, zoom, bearing); + } else { + map.setLatLngZoom(mbgl::LatLng(settings.latitude, settings.longitude), settings.zoom); + map.setBearing(settings.bearing); + map.setPitch(settings.pitch); + map.setDebug(mbgl::MapDebugOptions(settings.debug)); + } + + view->setChangeStyleCallback([&map] () { + static uint8_t currentStyleIndex; + + if (++currentStyleIndex == mbgl::util::default_styles::numOrderedStyles) { + currentStyleIndex = 0; + } + + mbgl::util::default_styles::DefaultStyle newStyle = mbgl::util::default_styles::orderedStyles[currentStyleIndex]; + map.setStyleURL(newStyle.url); + view->setWindowTitle(newStyle.name); + + mbgl::Log::Info(mbgl::Event::Setup, "Changed style to: %s", newStyle.name); + }); + + // Load style + if (style.empty()) { + const char *url = getenv("MAPBOX_STYLE_URL"); + if (url == nullptr) { + mbgl::util::default_styles::DefaultStyle newStyle = mbgl::util::default_styles::orderedStyles[0]; + style = newStyle.url; + view->setWindowTitle(newStyle.name); + } else { + style = url; + view->setWindowTitle(url); + } + } + + map.setStyleURL(style); + + view->run(); + + // Save settings + mbgl::LatLng latLng = map.getLatLng(); + settings.latitude = latLng.latitude; + settings.longitude = latLng.longitude; + settings.zoom = map.getZoom(); + settings.bearing = map.getBearing(); + settings.pitch = map.getPitch(); + settings.debug = mbgl::EnumType(map.getDebug()); + if (!skipConfig) { + settings.save(); + } + mbgl::Log::Info(mbgl::Event::General, + "Exit location: --lat=\"%f\" --lon=\"%f\" --zoom=\"%f\" --bearing \"%f\"", + settings.latitude, settings.longitude, settings.zoom, settings.bearing); + + view = nullptr; + return 0; +} diff --git a/platform/default/settings_json.cpp b/platform/glfw/settings_json.cpp index ef53aa83e7..2ba1038dc7 100644 --- a/platform/default/settings_json.cpp +++ b/platform/glfw/settings_json.cpp @@ -1,4 +1,4 @@ -#include <mbgl/platform/default/settings_json.hpp> +#include "settings_json.hpp" #include <fstream> namespace mbgl { diff --git a/platform/glfw/settings_json.hpp b/platform/glfw/settings_json.hpp new file mode 100644 index 0000000000..eb23b28bc8 --- /dev/null +++ b/platform/glfw/settings_json.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include <mbgl/map/mode.hpp> + +namespace mbgl { + +class Settings_JSON { +public: + Settings_JSON(); + void load(); + void save(); + void clear(); + +public: + double longitude = 0; + double latitude = 0; + double zoom = 0; + double bearing = 0; + double pitch = 0; + + EnumType debug = 0; +}; + +} // namespace mbgl |