Das Programmierspiel von der Gulasch-Programmier-Nacht 11

(Latest mirror + merged latest fork by qr4 on Lua 5.3)

Entropia info page https://entropia.de/GPN11:Programmierspiel (dead links)
Origin Gitlab
https://code.nerd2nerd.org/n2n/WeltraumProgrammierNacht

screen.c 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <sys/time.h>
  4. #include <SDL/SDL.h>
  5. #include <SDL/SDL_gfxPrimitives.h>
  6. #include <errno.h>
  7. #include "globals.h"
  8. #include "screen.h"
  9. #include "route.h"
  10. #include "json_output.h"
  11. /* TODO:
  12. *
  13. * a global scene for easy rerender
  14. * fast image switch
  15. */
  16. SDL_Surface *screen;
  17. int safety_radius;
  18. void sdl_setup() {
  19. const SDL_VideoInfo *info;
  20. if (SDL_Init(SDL_INIT_VIDEO) == -1) {
  21. fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
  22. exit(1);
  23. }
  24. info = SDL_GetVideoInfo();
  25. GLOBALS.DISPLAY_WIDTH = info->current_w;
  26. GLOBALS.DISPLAY_HEIGHT = info->current_h;
  27. atexit(SDL_Quit);
  28. }
  29. void sdl_screen_init() {
  30. if (GLOBALS.FULLSCREEN) {
  31. screen = SDL_SetVideoMode(GLOBALS.DISPLAY_WIDTH, GLOBALS.DISPLAY_HEIGHT, 0, SDL_FULLSCREEN);
  32. } else {
  33. screen = SDL_SetVideoMode(GLOBALS.WIDTH, GLOBALS.HEIGHT, 0, 0);
  34. }
  35. }
  36. void sdl_init() {
  37. sdl_setup();
  38. sdl_screen_init();
  39. if (screen == NULL) {
  40. fprintf(stderr, "SDL_SetVideoMode() failed: %s\n", SDL_GetError());
  41. exit(1);
  42. }
  43. }
  44. void draw_line(vector_t* p1, vector_t* p2) {
  45. aalineRGBA(screen, p1->x, p1->y, p2->x, p2->y, 255, 255, 255, 255);
  46. }
  47. void draw_blob(cluster_t* p) {
  48. aacircleRGBA(screen, p->center.x, p->center.y, 2, 255, 255, 255, 255);
  49. aacircleRGBA(screen, p->center.x, p->center.y, p->safety_radius, 64, 64, 64, 255);
  50. }
  51. void draw_clusters(cluster_t* clusters, int n) {
  52. int x, y;
  53. for (y = 0; y < n; y++) {
  54. for (x = 0; x < n; x++) {
  55. draw_blob(&clusters[y*n + x]);
  56. }
  57. }
  58. }
  59. void draw_grid(cluster_t *clusters, int n) {
  60. int x, y;
  61. for (y = 0; y < n - 1; y++) {
  62. for (x = 0; x < n - 1; x++) {
  63. draw_line(&(clusters[y*n + x].center), &(clusters[y*n + x + 1].center));
  64. draw_line(&(clusters[y*n + x].center), &(clusters[(y + 1)*n + x].center));
  65. }
  66. draw_line(&(clusters[y*n + x].center), &(clusters[(y + 1)*n + x].center));
  67. }
  68. for (x = 0; x < n - 1; x++) {
  69. draw_line(&(clusters[y*n + x].center), &(clusters[y*n + x + 1].center));
  70. }
  71. }
  72. void draw_route(waypoint_t* route, int r, int g, int b, int with_blobs) {
  73. if (route == NULL) return;
  74. waypoint_t* t = route;
  75. while (t->next != NULL) {
  76. if (with_blobs) aacircleRGBA(screen, t->point.x, t->point.y, 2, r, g, b, 255);
  77. aalineRGBA(screen, t->point.x, t->point.y, t->next->point.x, t->next->point.y, r, g, b, 128);
  78. t = t->next;
  79. }
  80. aacircleRGBA(screen, t->point.x, t->point.y, 2, r, g, g, 255);
  81. }
  82. void squirl(cluster_t *clusters, int n) {
  83. int i, j;
  84. for (i = 0; i < n; i++) {
  85. for (j = 0; j < n; j++) {
  86. float x = clusters[i*n + j].center.x;
  87. float y = clusters[i*n + j].center.y;
  88. float r = hypotf(x-GLOBALS.WIDTH / 2, y - GLOBALS.HEIGHT/2);
  89. float phi = atan2f(y-GLOBALS.HEIGHT / 2, x - GLOBALS.WIDTH/2);
  90. float angle = (1e-3 * r - 0.5);
  91. phi += angle;
  92. x = GLOBALS.WIDTH/2 + r*cos(phi);
  93. y = GLOBALS.HEIGHT/2 + r * sin(phi);
  94. clusters[i*n + j].center.x = x;
  95. clusters[i*n + j].center.y = y;
  96. }
  97. }
  98. }
  99. void randomize(cluster_t *clusters, int n, float fac) {
  100. int i;
  101. for (i = 0; i < n * n; i++) {
  102. clusters[i].center.x += (((float) rand() / RAND_MAX) - 0.5) * 2 * fac;
  103. clusters[i].center.y += (((float) rand() / RAND_MAX) - 0.5) * 2 * fac;
  104. }
  105. }
  106. void set(cluster_t *clusters, int n, float safety_radius) {
  107. int x, y;
  108. for (y = 0; y < n; y++) {
  109. for (x = 0; x < n; x++) {
  110. clusters[y*n + x].center.x = GLOBALS.WIDTH * (x + 1) / (n + 1);
  111. clusters[y*n + x].center.y = GLOBALS.HEIGHT * (y + 1) / (n + 1);
  112. clusters[y*n + x].safety_radius = (y*n+x)%5 ? safety_radius : safety_radius/2;
  113. }
  114. }
  115. squirl(clusters, n);
  116. }
  117. int main_loop() {
  118. int quit = 0;
  119. int n = 16;
  120. int view_grid = 0;
  121. int view_points = 1;
  122. int view_waypoints = 1;
  123. int view_smoothed = 1;
  124. int view_json = 0;
  125. float fac = GLOBALS.WIDTH / (n + 1) * 0.3;
  126. SDL_Event e;
  127. vector_t start = {0, 0};
  128. vector_t stop = {0, 0};
  129. waypoint_t* route1 = NULL;
  130. waypoint_t* route1s = NULL;
  131. waypoint_t* route2 = NULL;
  132. waypoint_t* route2s = NULL;
  133. char needJsonOutput = 0;
  134. struct timeval t1, t2;
  135. double elapsedTime;
  136. cluster_t* clusters = malloc (sizeof(cluster_t) * n * n);
  137. set(clusters, n, safety_radius);
  138. while (!quit) {
  139. safety_radius = GLOBALS.HEIGHT / (n + 1) / 5 ;
  140. SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
  141. if (view_grid) {
  142. draw_grid(clusters, n);
  143. }
  144. if (view_points) {
  145. draw_clusters(clusters, n);
  146. }
  147. if (view_waypoints) {
  148. draw_route(route1, 0, 0, 255, 1);
  149. draw_route(route2, 255, 0, 0, 1);
  150. }
  151. if (view_smoothed) {
  152. draw_route(route1s, 0, 255, 255, 0);
  153. draw_route(route2s, 255, 255, 0, 0);
  154. }
  155. if (start.x != 0 && start.y != 0) {
  156. aacircleRGBA(screen, start.x, start.y, 2, 0, 255, 0, 255);
  157. }
  158. if (stop.x != 0 && stop.y != 0) {
  159. aacircleRGBA(screen, stop.x, stop.y, 2, 255, 0, 0, 255);
  160. }
  161. SDL_Flip(screen);
  162. SDL_WaitEvent(&e);
  163. needJsonOutput = view_json;
  164. switch (e.type) {
  165. case SDL_QUIT :
  166. quit = 1;
  167. break;
  168. case SDL_MOUSEBUTTONDOWN :
  169. switch (e.button.button) {
  170. case SDL_BUTTON_LEFT:
  171. start.x = e.button.x;
  172. start.y = e.button.y;
  173. break;
  174. case SDL_BUTTON_RIGHT:
  175. stop.x = e.button.x;
  176. stop.y = e.button.y;
  177. break;
  178. }
  179. if (start.x != 0 && start.y != 0 && stop.x != 0 && stop.y != 0) {
  180. fprintf(stderr, "\n\nRoute from (%f,%f) to (%f,%f)\n", start.x, start.y, stop.x, stop.y);
  181. gettimeofday(&t1, NULL);
  182. route1 = plotCourse(&start, &stop, clusters, n);
  183. gettimeofday(&t2, NULL);
  184. elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;
  185. elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;
  186. fprintf(stderr, "plotCourse scanline based took %f ms\n", elapsedTime);
  187. gettimeofday(&t1, NULL);
  188. route1s = smooth(route1, 5);
  189. gettimeofday(&t2, NULL);
  190. elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;
  191. elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;
  192. fprintf(stderr, "smoth scanline based route took %f ms\n", elapsedTime);
  193. gettimeofday(&t1, NULL);
  194. route2 = plotCourse_gridbased(&start, &stop, clusters, n);
  195. gettimeofday(&t2, NULL);
  196. elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;
  197. elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;
  198. fprintf(stderr, "plotCourse gridline based took %f ms\n", elapsedTime);
  199. gettimeofday(&t1, NULL);
  200. route2s = smooth(route2, 5);
  201. gettimeofday(&t2, NULL);
  202. elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;
  203. elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;
  204. fprintf(stderr, "smooth gridline based route took %f ms\n", elapsedTime);
  205. if(needJsonOutput) {
  206. json_update(route1s, route2s, n*n);
  207. }
  208. }
  209. break;
  210. case SDL_KEYDOWN :
  211. switch (e.key.keysym.sym) {
  212. case SDLK_p:
  213. view_points = !view_points;
  214. break;
  215. case SDLK_g:
  216. case SDLK_l:
  217. view_grid = !view_grid;
  218. break;
  219. case SDLK_w:
  220. view_waypoints = !view_waypoints;
  221. break;
  222. case SDLK_s:
  223. view_smoothed = !view_smoothed;
  224. break;
  225. case SDLK_j:
  226. view_json = !view_json;
  227. needJsonOutput = view_json;
  228. break;
  229. case SDLK_r:
  230. randomize(clusters, n, fac);
  231. break;
  232. case SDLK_BACKSPACE:
  233. set(clusters, n, safety_radius);
  234. break;
  235. case SDLK_q:
  236. quit = 1;
  237. break;
  238. case SDLK_UP:
  239. n++;
  240. clusters = realloc(clusters, sizeof(cluster_t) * n * n);
  241. fac = GLOBALS.WIDTH / (n + 1) * 0.3;
  242. set(clusters, n, safety_radius);
  243. break;
  244. case SDLK_DOWN:
  245. n--;
  246. clusters = realloc (clusters, sizeof(cluster_t) * n * n);
  247. fac = GLOBALS.WIDTH / (n + 1) * 0.3;
  248. set(clusters, n, safety_radius);
  249. break;
  250. case SDLK_PLUS:
  251. case SDLK_KP_PLUS:
  252. fac *= 1.1;
  253. fprintf(stderr, "fac: %f\n", fac);
  254. set(clusters, n, safety_radius);
  255. randomize(clusters, n, fac);
  256. break;
  257. case SDLK_MINUS:
  258. case SDLK_KP_MINUS:
  259. fac /= 1.1;
  260. fprintf(stderr, "fac: %f\n", fac);
  261. set(clusters, n, safety_radius);
  262. randomize(clusters, n, fac);
  263. break;
  264. default:
  265. needJsonOutput = 0;
  266. break;
  267. }
  268. break;
  269. case SDL_VIDEORESIZE:
  270. GLOBALS.WIDTH = e.resize.w;
  271. GLOBALS.HEIGHT = e.resize.h;
  272. sdl_screen_init();
  273. break;
  274. default :
  275. needJsonOutput = 0;
  276. break;
  277. }
  278. if(needJsonOutput) {
  279. json_output(clusters, n*n);
  280. }
  281. }
  282. free(clusters);
  283. return 0;
  284. }