summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile7
-rw-r--r--macosx/main.mm223
-rw-r--r--src/geometry/linevertexbuffer.cpp2
-rw-r--r--src/renderer/shader.cpp1
4 files changed, 229 insertions, 4 deletions
diff --git a/Makefile b/Makefile
index 106055e55d..a43f4f5f6c 100644
--- a/Makefile
+++ b/Makefile
@@ -14,16 +14,15 @@ SRCS += src/renderer/shader-fill.cpp
SRCS += src/renderer/shader-line.cpp
SRCS += src/util/mat4.c
-SRCS += macosx/main.mm
-
OBJS = $(patsubst %.mm,%.o,$(patsubst %.cpp,%.o,$(patsubst %.c,%.o,$(SRCS))))
main: macosx
library: $(OBJS)
+macosx: SRCS += macosx/main.mm
macosx: library
- $(CXX) $(OBJS) $(INCLUDE) -lglfw3 -framework OpenGL -framework Foundation -o bin/macosx
+ $(CXX) $(OBJS) $(INCLUDE) -lglfw3 -framework OpenGL -framework Foundation -o macosx/main
%.o: %.cpp
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(INCLUDE) -c -o $@ $^
@@ -36,6 +35,6 @@ macosx: library
clean:
rm -rf src/*/*.o
- rm -rf bin/macosx
+ rm -rf macosx/main
.PHONY: macosx
diff --git a/macosx/main.mm b/macosx/main.mm
new file mode 100644
index 0000000000..2462d2e34e
--- /dev/null
+++ b/macosx/main.mm
@@ -0,0 +1,223 @@
+#include <llmr/llmr.hpp>
+#include <GLFW/glfw3.h>
+#include <cstdlib>
+#import <Foundation/Foundation.h>
+
+#include <llmr/platform/platform.hpp>
+#include <llmr/map/tile.hpp>
+
+class MapView {
+public:
+ MapView() :
+ dirty(true),
+ tracking(false),
+ platform(new llmr::platform(this)),
+ painter(new llmr::painter(platform)),
+ map(new llmr::map(platform, painter)) {
+ if (!glfwInit()) {
+ fprintf(stderr, "Failed to initialize glfw\n");
+ exit(1);
+ }
+
+ window = glfwCreateWindow(640, 480, "llmr", NULL, NULL);
+ if (!window) {
+ glfwTerminate();
+ fprintf(stderr, "Failed to initialize window\n");
+ exit(1);
+ }
+
+ glfwMakeContextCurrent(window);
+
+ painter->setup();
+
+
+ int width, height;
+ glfwGetWindowSize(window, &width, &height);
+ painter->resize(width, height);
+
+ glfwSwapInterval(1);
+
+
+ glfwSetCursorPosCallback(window, mousemove);
+ glfwSetMouseButtonCallback(window, mouseclick);
+ glfwSetWindowSizeCallback(window, resize);
+ glfwSetScrollCallback(window, scroll);
+
+ glfwSetWindowUserPointer(window, this);
+ }
+
+ static void scroll(GLFWwindow *window, double xoffset, double yoffset) {
+ MapView *view = (MapView *)glfwGetWindowUserPointer(window);
+ double delta = yoffset * 40;
+
+ bool is_wheel = delta != 0 && fmod(delta, 4.000244140625) == 0;
+
+ double absdelta = delta < 0 ? -delta : delta;
+ double scale = 2.0 / (1.0 + exp(-absdelta / 100.0));
+
+ // Make the scroll wheel a bit slower.
+ if (!is_wheel) {
+ scale = (scale - 1.0) / 2.0 + 1.0;
+ }
+
+ // Zooming out.
+ if (delta < 0 && scale != 0) {
+ scale = 1.0 / scale;
+ }
+
+ view->map->scaleBy(scale, view->last_x, view->last_y);
+ }
+
+ static void resize(GLFWwindow *window, int width, int height) {
+ MapView *view = (MapView *)glfwGetWindowUserPointer(window);
+ view->painter->resize(width, height);
+ }
+
+ static void mouseclick(GLFWwindow *window, int button, int action, int modifiers) {
+ MapView *view = (MapView *)glfwGetWindowUserPointer(window);
+ if (button == GLFW_MOUSE_BUTTON_1) {
+ view->tracking = action == GLFW_PRESS;
+ }
+ }
+
+ static void mousemove(GLFWwindow *window, double x, double y) {
+ MapView *view = (MapView *)glfwGetWindowUserPointer(window);
+ if (view->tracking) {
+ view->map->moveBy(x - view->last_x, y - view->last_y);
+ }
+ view->last_x = x;
+ view->last_y = y;
+ }
+
+ int run() {
+ while (!glfwWindowShouldClose(window)) {
+ if (dirty) {
+ dirty = render();
+ glfwSwapBuffers(window);
+ fps();
+ }
+
+ if (dirty) {
+ glfwPollEvents();
+ } else {
+ glfwWaitEvents();
+ }
+ }
+
+ return 0;
+ }
+
+ bool render() {
+ return map->render();
+ }
+
+ void fps() {
+ static int frames = 0;
+ static double time_elapsed = 0;
+
+ frames++;
+ double current_time = glfwGetTime();
+
+ if (current_time - time_elapsed >= 1) {
+ fprintf(stderr, "FPS: %4.2f\n", frames / (current_time - time_elapsed));
+ time_elapsed = current_time;
+ frames = 0;
+ }
+ }
+
+ ~MapView() {
+ delete map;
+ delete painter;
+ delete platform;
+ glfwTerminate();
+ }
+
+public:
+ bool dirty;
+ double last_x, last_y;
+ bool tracking;
+
+ GLFWwindow *window;
+ llmr::platform *platform;
+ llmr::painter *painter;
+ llmr::map *map;
+};
+
+
+
+
+
+
+const char *llmr::platform::shaderSource(const char *name, const char *type) {
+ char filename[80];
+ snprintf(filename, 80, "src/shader/%s.%s", name, type);
+
+ FILE *file = fopen(filename, "rb");
+ if (!file) {
+ fprintf(stderr, "Could not open file %s", filename);
+ return NULL;
+ }
+
+ fseek(file, 0, SEEK_END);
+ long size = ftell(file);
+ fseek(file, 0, SEEK_SET);
+ char *shader = (char *)malloc(size + 1);
+ if (!fread(shader, size, 1, file)) {
+ fclose(file);
+ free(shader);
+ shader = 0;
+ fprintf(stderr, "Failed to read file %s\n", filename);
+ return NULL;
+ }
+ fclose(file);
+ shader[size] = 0;
+ return shader;
+}
+
+void llmr::platform::restart() {
+ ((MapView *)obj)->dirty = true;
+}
+
+void llmr::platform::request(tile *tile) {
+ fprintf(stderr, "request %d/%d/%d\n", tile->z, tile->x, tile->y);
+
+ fprintf(stderr, "requesting tile\n");
+ NSString *urlTemplate = @"http://api.tiles.mapbox.com/v3/mapbox.mapbox-streets-v4/%d/%d/%d.vector.pbf";
+ NSString *urlString = [NSString
+ stringWithFormat:urlTemplate,
+ tile->z,
+ tile->x,
+ tile->y];
+ NSURL *url = [NSURL URLWithString:urlString];
+ NSLog(@"Requesting %@", urlString);
+
+ NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
+
+ NSOperationQueue *queue = [[NSOperationQueue alloc] init];
+ [NSURLConnection
+ sendAsynchronousRequest:urlRequest
+ queue:queue
+ completionHandler: ^ (NSURLResponse * response,
+ NSData * data,
+ NSError * error) {
+ if (error == nil) {
+ tile->setData((uint8_t *)[data bytes], [data length]);
+ if (tile->parse()) {
+ dispatch_async(dispatch_get_main_queue(), ^ {
+ ((MapView *)obj)->map->tileLoaded(tile);
+ });
+ return;
+ }
+ }
+
+ dispatch_async(dispatch_get_main_queue(), ^ {
+ ((MapView *)obj)->map->tileFailed(tile);
+ });
+ }];
+}
+
+
+int main() {
+ MapView view;
+ return view.run();
+}
diff --git a/src/geometry/linevertexbuffer.cpp b/src/geometry/linevertexbuffer.cpp
index 03cbdb79b1..7d48f79604 100644
--- a/src/geometry/linevertexbuffer.cpp
+++ b/src/geometry/linevertexbuffer.cpp
@@ -1,5 +1,7 @@
#include <llmr/geometry/linevertexbuffer.hpp>
+#include <cstdio>
+
using namespace llmr;
linevertexbuffer::linevertexbuffer()
diff --git a/src/renderer/shader.cpp b/src/renderer/shader.cpp
index d05fb2a65f..00af17e607 100644
--- a/src/renderer/shader.cpp
+++ b/src/renderer/shader.cpp
@@ -1,6 +1,7 @@
#include <llmr/renderer/shader.hpp>
#include <cstdlib>
+#include <cstdio>
using namespace llmr;