/
ui.c
225 lines (207 loc) · 5.26 KB
/
ui.c
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "ui.h"
#include "load.h"
int Display_Application(int argc, char *argv[])
{
SDL_Window *window; // Declare a pointer, main window
int width = 640;
int height = 480;
GLdouble dx = 0;
GLdouble dy = 0;
GLdouble zoom = 1.0;
initNodesBounds(argv[1]);
min_x = minx;
max_x = maxx;
min_y = miny;
max_y = maxy;
// Initialize SDL2
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
// Create an application window with the following settings:
window = SDL_CreateWindow("OSM Renderer", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
width, // width, in pixels
height, // height, in pixels
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE // flags
);
// Check that the window was successfully created
if (window == NULL) {
// In the case that the window could not be made...
fprintf(stderr, "SDL_CreateWindow failed: %s\n",
SDL_GetError());
exit(EXIT_FAILURE);
}
fprintf(stdout, "Press the 'F' key to switch fullscreen.\n");
fprintf(stdout, "Press the 'R' key to reset the view.\n");
fprintf(stdout, "Press the [x] button to close the window.\n");
SDL_Renderer *displayRenderer = NULL;
SDL_RendererInfo displayRendererInfo;
displayRenderer =
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_GetRendererInfo(displayRenderer, &displayRendererInfo);
if ((displayRendererInfo.flags & SDL_RENDERER_ACCELERATED) == 0 ||
(displayRendererInfo.flags & SDL_RENDERER_TARGETTEXTURE) == 0) {
fprintf(stderr,
"We have no render surface and not accelerated.\n");
}
Display_InitGL();
Display_SetViewport(width, height, dx, dy, zoom);
int fullscreen = 0;
int drag = 0;
int xcursor = 0;
int ycursor = 0;
SDL_Event event;
int quit = 0;
time_t t = time(NULL);
time_t ctime = time(NULL);
int frames = 0;
GList *l;
for (l = g_hash_table_get_values(ways_hashtable); l != NULL;
l = l->next) {
way *w = l->data;
w->glist = tessellate(*w);
}
g_list_free(l);
while (!quit) {
Display_SetViewport(width, height, dx, dy, zoom);
Display_Render(displayRenderer, width, height, dx, dy, zoom);
while (SDL_PollEvent(&event)) // User's actions
{
switch (event.type) {
case SDL_QUIT: // Close button
quit = 1;
break;
case SDL_KEYUP: // Key release
if (event.key.keysym.sym == SDLK_f) // F key
{
if (fullscreen == 0) {
fullscreen = 1;
SDL_SetWindowFullscreen(window,
SDL_WINDOW_FULLSCREEN);
} else if (fullscreen == 1) {
fullscreen = 0;
SDL_SetWindowFullscreen(window,
0);
}
}
if (event.key.keysym.sym == SDLK_KP_PLUS) {
if (zoom > 0.1) {
dx = dx +
0.1 * pixelsize * (xcursor -
width /
2);
dy = dy -
0.1 * pixelsize * (ycursor -
height /
2);
}
if (zoom > 0.1)
zoom -= 0.1;
}
if (event.key.keysym.sym == SDLK_KP_MINUS) {
dx = dx - 0.1 * pixelsize * (xcursor -
width / 2);
dy = dy + 0.1 * pixelsize * (ycursor -
height /
2);
zoom += 0.1;
}
if (event.key.keysym.sym == SDLK_r) {
zoom = 1.0;
dx = 0.0;
dy = 0.0;
}
if (event.key.keysym.sym == SDLK_s) {
if (showFrame)
showFrame = 0;
else
showFrame = 1;
}
if (event.key.keysym.sym == SDLK_c) {
screenshoot = 1;
}
if (event.key.keysym.sym == SDLK_p) {
projection = !projection;
if (projection) {
min_x = minx;
max_x = maxx;
min_y = miny;
max_y = maxy;
} else {
min_x = minlon;
max_x = maxlon;
min_y = minlat;
max_y = maxlat;
}
}
break;
case SDL_WINDOWEVENT:
switch (event.window.event) {
case SDL_WINDOWEVENT_RESIZED:
width = event.window.data1;
height = event.window.data2;
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
width = event.window.data1;
height = event.window.data2;
break;
default:
// printf("Window %d got unknown event %d\n", event.window.windowID, event.window.event);
break;
}
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT)
drag = 1;
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT)
drag = 0;
break;
case SDL_MOUSEMOTION:
xcursor = event.motion.x;
ycursor = event.motion.y;
if (drag != 0) {
dx -=
pixelsize * event.motion.xrel *
zoom;
dy +=
pixelsize * event.motion.yrel *
zoom;
}
break;
case SDL_MOUSEWHEEL:
if (zoom > 0.01) {
dx = dx +
0.02 * pixelsize * event.wheel.y *
(xcursor - width / 2);
dy = dy -
0.02 * pixelsize * event.wheel.y *
(ycursor - height / 2);
}
if (event.wheel.y > 0 && zoom > 0.01)
zoom -= 0.02;
else if (event.wheel.y < 0)
zoom += 0.02;
break;
default:
//printf("unknown event");
break;
}
}
frames++;
ctime = time(NULL);
if (t != ctime) {
t = ctime;
//fprintf(stdout, "%d fps\n", frames);
frames = 0;
}
}
// Close and destroy the window
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
return EXIT_SUCCESS;
}