summaryrefslogtreecommitdiff
path: root/platform/glfw/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'platform/glfw/main.cpp')
-rw-r--r--platform/glfw/main.cpp148
1 files changed, 66 insertions, 82 deletions
diff --git a/platform/glfw/main.cpp b/platform/glfw/main.cpp
index 7192475835..c4e0ecf692 100644
--- a/platform/glfw/main.cpp
+++ b/platform/glfw/main.cpp
@@ -1,4 +1,5 @@
#include "glfw_view.hpp"
+#include "glfw_renderer_frontend.hpp"
#include "settings_json.hpp"
#include <mbgl/util/default_styles.hpp>
@@ -7,11 +8,13 @@
#include <mbgl/util/default_thread_pool.hpp>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/style/style.hpp>
+#include <mbgl/renderer/renderer.hpp>
+
+#include <args/args.hxx>
#include <csignal>
-#include <getopt.h>
#include <fstream>
-#include <sstream>
+#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <array>
@@ -32,69 +35,48 @@ void quit_handler(int) {
}
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, nullptr, 'f'},
- {"benchmark", no_argument, nullptr, 'b'},
- {"style", required_argument, nullptr, 's'},
- {"lon", required_argument, nullptr, 'x'},
- {"lat", required_argument, nullptr, 'y'},
- {"zoom", required_argument, nullptr, 'z'},
- {"bearing", required_argument, nullptr, 'r'},
- {"pitch", required_argument, nullptr, 'p'},
- {nullptr, 0, nullptr, 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 != nullptr)
- 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;
- }
-
+ args::ArgumentParser argumentParser("Mapbox GL GLFW example");
+ args::HelpFlag helpFlag(argumentParser, "help", "Display this help menu", {'h', "help"});
+
+ args::Flag fullscreenFlag(argumentParser, "fullscreen", "Toggle fullscreen", {'f', "fullscreen"});
+ args::Flag benchmarkFlag(argumentParser, "benchmark", "Toggle benchmark", {'b', "benchmark"});
+ args::Flag offlineFlag(argumentParser, "offline", "Toggle offline", {'o', "offline"});
+
+ args::ValueFlag<std::string> styleValue(argumentParser, "URL", "Map stylesheet", {'s', "style"});
+ args::ValueFlag<double> lonValue(argumentParser, "degrees", "Longitude", {'x', "lon"});
+ args::ValueFlag<double> latValue(argumentParser, "degrees", "Latitude", {'y', "lat"});
+ args::ValueFlag<double> zoomValue(argumentParser, "number", "Zoom level", {'z', "zoom"});
+ args::ValueFlag<double> bearingValue(argumentParser, "degrees", "Bearing", {'b', "bearing"});
+ args::ValueFlag<double> pitchValue(argumentParser, "degrees", "Pitch", {'p', "pitch"});
+
+ try {
+ argumentParser.ParseCLI(argc, argv);
+ } catch (args::Help) {
+ std::cout << argumentParser;
+ exit(0);
+ } catch (args::ParseError e) {
+ std::cerr << e.what() << std::endl;
+ std::cerr << argumentParser;
+ exit(1);
+ } catch (args::ValidationError e) {
+ std::cerr << e.what() << std::endl;
+ std::cerr << argumentParser;
+ exit(2);
}
+ // Load settings
+ mbgl::Settings_JSON settings;
+ settings.online = !offlineFlag;
+ if (lonValue) settings.longitude = args::get(lonValue);
+ if (latValue) settings.latitude = args::get(latValue);
+ if (zoomValue) settings.zoom = args::get(zoomValue);
+ if (bearingValue) settings.bearing = args::get(bearingValue);
+ if (pitchValue) settings.pitch = args::get(pitchValue);
+
+ const bool fullscreen = fullscreenFlag ? args::get(fullscreenFlag) : false;
+ const bool benchmark = benchmarkFlag ? args::get(benchmarkFlag) : false;
+ std::string style = styleValue ? args::get(styleValue) : "";
+
// sigint handling
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = quit_handler;
@@ -110,6 +92,10 @@ int main(int argc, char *argv[]) {
view = &backend;
mbgl::DefaultFileSource fileSource("/tmp/mbgl-cache.db", ".");
+ if (!settings.online) {
+ fileSource.setOnlineStatus(false);
+ mbgl::Log::Warning(mbgl::Event::Setup, "Application is offline. Press `O` to toggle online status.");
+ }
// Set access token if present
const char *token = getenv("MAPBOX_ACCESS_TOKEN");
@@ -120,26 +106,26 @@ int main(int argc, char *argv[]) {
}
mbgl::ThreadPool threadPool(4);
-
- mbgl::Map map(backend, view->getSize(), view->getPixelRatio(), fileSource, threadPool);
+ GLFWRendererFrontend rendererFrontend { std::make_unique<mbgl::Renderer>(backend, view->getPixelRatio(), fileSource, threadPool), backend };
+ mbgl::Map map(rendererFrontend, 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));
+ if (!style.empty() && style.find("://") == std::string::npos) {
+ style = std::string("file://") + style;
}
+ 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->setOnlineStatusCallback([&settings, &fileSource]() {
+ settings.online = !settings.online;
+ fileSource.setOnlineStatus(settings.online);
+ mbgl::Log::Info(mbgl::Event::Setup, "Application is %s. Press `O` to toggle online status.", settings.online ? "online" : "offline");
+ });
+
view->setChangeStyleCallback([&map] () {
static uint8_t currentStyleIndex;
@@ -191,13 +177,11 @@ int main(int argc, char *argv[]) {
settings.bearing = map.getBearing();
settings.pitch = map.getPitch();
settings.debug = mbgl::EnumType(map.getDebug());
- if (!skipConfig) {
- settings.save();
- }
+ settings.save();
mbgl::Log::Info(mbgl::Event::General,
R"(Exit location: --lat="%f" --lon="%f" --zoom="%f" --bearing "%f")",
settings.latitude, settings.longitude, settings.zoom, settings.bearing);
view = nullptr;
return 0;
-} \ No newline at end of file
+}