summaryrefslogtreecommitdiff
path: root/macosx/main.mm
blob: caa56d999e8ce48d7e4e3f2b2fed68707a49e31e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <llmr/llmr.hpp>
#include <GLFW/glfw3.h>
#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;
};

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();
}