You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <list>
  5. #include <set>
  6. #include <string>
  7. #include <vector>
  8. #include "SDL.h"
  9. #define TILESIZE 256
  10. #include "sdlmap.h"
  11. #include "tile.h"
  12. #include "tile_collection.h"
  13. #include "map_view.h"
  14. void runloop(MapView &view){
  15. bool mousedown = false;
  16. bool dirty = true;
  17. Uint32 lastclick = 0;
  18. int zoomdf = 0;
  19. for(;;){
  20. SDL_Event event;
  21. while((dirty ? SDL_PollEvent : SDL_WaitEvent)(&event)){
  22. switch (event.type) {
  23. case SDL_MOUSEBUTTONUP:
  24. mousedown = false;
  25. break;
  26. case SDL_MOUSEBUTTONDOWN:
  27. if(event.button.button == 1){
  28. mousedown = true;
  29. if(SDL_GetTicks() - lastclick < 250){
  30. lastclick = 0;
  31. view.zoom_at(event.button.x, event.button.y);
  32. dirty = true;
  33. }
  34. lastclick = SDL_GetTicks();
  35. }
  36. break;
  37. case SDL_MOUSEWHEEL:
  38. zoomdf += event.wheel.y;
  39. if(zoomdf >= 5){
  40. zoomdf = 0;
  41. view.zoom_in();
  42. dirty = true;
  43. }else if(zoomdf <= -5){
  44. zoomdf = 0;
  45. view.zoom_out();
  46. dirty = true;
  47. }
  48. break;
  49. case SDL_MOUSEMOTION:
  50. if(mousedown){
  51. view.move_by(-event.motion.xrel, -event.motion.yrel);
  52. dirty = true;
  53. }
  54. break;
  55. case SDL_KEYDOWN:
  56. switch(event.key.keysym.sym){
  57. case SDLK_ESCAPE:
  58. exit(0);
  59. break;
  60. case SDLK_PLUS:
  61. case SDLK_EQUALS:
  62. view.zoom_in();
  63. dirty = true;
  64. break;
  65. case SDLK_MINUS:
  66. view.zoom_out();
  67. dirty = true;
  68. break;
  69. }
  70. break;
  71. case SDL_WINDOWEVENT:
  72. switch(event.window.event){
  73. case SDL_WINDOWEVENT_RESIZED:
  74. view.resize(event.window.data1, event.window.data2);
  75. dirty = true;
  76. break;
  77. case SDL_WINDOWEVENT_EXPOSED:
  78. dirty = true;
  79. break;
  80. }
  81. break;
  82. case SDL_QUIT:
  83. exit(0);
  84. }
  85. }
  86. if(dirty){
  87. view.update_bounds();
  88. dirty = view.tiles.work();
  89. view.render();
  90. }
  91. }
  92. }
  93. int main(int argc, char *argv[]){
  94. if(SDL_Init(SDL_INIT_VIDEO)){
  95. fprintf(stderr, "SDL_Init failed: %s", SDL_GetError());
  96. exit(-1);
  97. }
  98. int width = 600, height = 800;
  99. int zoom = 12;
  100. SDL_Window *window = SDL_CreateWindow("SDLmap", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE);
  101. MapView view(window, width, height, zoom);
  102. view.center_coords(48.4284, -123.3656);
  103. runloop(view);
  104. return 0;
  105. }