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 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include <stdio.h>
  2. #include <SDL/SDL.h>
  3. #include <SDL/SDL_gfxPrimitives.h>
  4. #include <errno.h>
  5. #include "globals.h"
  6. #include "screen.h"
  7. #include "route.h"
  8. /* TODO:
  9. *
  10. * a global scene for easy rerender
  11. * fast image switch
  12. */
  13. SDL_Surface *screen;
  14. void sdl_setup() {
  15. const SDL_VideoInfo *info;
  16. if (SDL_Init(SDL_INIT_VIDEO) == -1) {
  17. ERROR("SDL_Init() failed: %s\n", SDL_GetError());
  18. exit(1);
  19. }
  20. info = SDL_GetVideoInfo();
  21. GLOBALS.DISPLAY_WIDTH = info->current_w;
  22. GLOBALS.DISPLAY_HEIGHT = info->current_h;
  23. atexit(SDL_Quit);
  24. }
  25. void sdl_screen_init() {
  26. if (GLOBALS.FULLSCREEN) {
  27. screen = SDL_SetVideoMode(GLOBALS.DISPLAY_WIDTH, GLOBALS.DISPLAY_HEIGHT, 0, SDL_FULLSCREEN);
  28. } else {
  29. screen = SDL_SetVideoMode(GLOBALS.WIDTH, GLOBALS.HEIGHT, 0, 0);
  30. }
  31. }
  32. void sdl_init() {
  33. sdl_setup();
  34. sdl_screen_init();
  35. if (screen == NULL) {
  36. ERROR("SDL_SetVideoMode() failed: %s\n", SDL_GetError());
  37. exit(1);
  38. }
  39. }
  40. void draw_line(pixel_t *p1, pixel_t *p2) {
  41. aalineRGBA(screen, p1->x, p1->y, p2->x, p2->y, 255, 255, 255, 255);
  42. }
  43. void draw_blob(pixel_t *p1) {
  44. aacircleRGBA(screen, p1->x, p1->y, 2, 255, 255, 255, 255);
  45. //aacircleRGBA(screen, p1->x, p1->y, 10, 64, 64, 64, 255);
  46. }
  47. void draw_points(pixel_t *points, int n) {
  48. int x, y;
  49. for (y = 0; y < n; y++) {
  50. for (x = 0; x < n; x++) {
  51. draw_blob(&points[y*n + x]);
  52. }
  53. }
  54. }
  55. void draw_grid(pixel_t *points, int n) {
  56. int x, y;
  57. for (y = 0; y < n - 1; y++) {
  58. for (x = 0; x < n - 1; x++) {
  59. draw_line(&points[y*n + x], &points[y*n + x + 1]);
  60. draw_line(&points[y*n + x], &points[(y + 1)*n + x]);
  61. }
  62. draw_line(&points[y*n + x], &points[(y + 1)*n + x]);
  63. }
  64. for (x = 0; x < n - 1; x++) {
  65. draw_line(&points[y*n + x], &points[y*n + x + 1]);
  66. }
  67. }
  68. void randomize(pixel_t *points, int n, float fac) {
  69. int i;
  70. for (i = 0; i < n * n; i++) {
  71. points[i].x += (((float) rand() / RAND_MAX) - 0.5) * 2 * fac;
  72. points[i].y += (((float) rand() / RAND_MAX) - 0.5) * 2 * fac;
  73. }
  74. }
  75. void set(pixel_t *points, int n) {
  76. int x, y;
  77. for (y = 0; y < n; y++) {
  78. for (x = 0; x < n; x++) {
  79. points[y*n + x].x = GLOBALS.WIDTH * (x + 1) / (n + 1);
  80. points[y*n + x].y = GLOBALS.HEIGHT * (y + 1) / (n + 1);
  81. }
  82. }
  83. }
  84. int main_loop() {
  85. int quit = 0;
  86. int n = 5;
  87. int view_grid = 0;
  88. int view_points = 1;
  89. float fac = GLOBALS.WIDTH / (n + 1) * 0.3;
  90. SDL_Event e;
  91. pixel_t *points;
  92. pixel_t start = {0, 0};
  93. pixel_t stop = {0, 0};
  94. waypoint_t* route = NULL;
  95. points = (pixel_t *) malloc (sizeof(pixel_t) * n * n);
  96. set(points, n);
  97. while (!quit) {
  98. SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
  99. if (view_grid) {
  100. draw_grid(points, n);
  101. }
  102. if (view_points) {
  103. draw_points(points, n);
  104. }
  105. if (route != NULL) {
  106. waypoint_t* r = route;
  107. while (r->next != NULL) {
  108. aacircleRGBA(screen, r->point.x, r->point.y, 2, 0, 0, 255, 255);
  109. aalineRGBA(screen, r->point.x, r->point.y, r->next->point.x, r->next->point.y, 0, 0, 255, 64);
  110. r = r->next;
  111. }
  112. aacircleRGBA(screen, r->point.x, r->point.y, 2, 0, 0, 255, 255);
  113. r = smooth(route, 5);
  114. while (r->next != NULL) {
  115. aacircleRGBA(screen, r->point.x, r->point.y, 2, 0, 255, 255, 255);
  116. aalineRGBA(screen, r->point.x, r->point.y, r->next->point.x, r->next->point.y, 0, 255, 255, 64);
  117. r = r->next;
  118. }
  119. aacircleRGBA(screen, r->point.x, r->point.y, 2, 0, 0, 255, 255);
  120. }
  121. if (start.x != 0 && start.y != 0) {
  122. aacircleRGBA(screen, start.x, start.y, 2, 0, 255, 0, 255);
  123. }
  124. if (stop.x != 0 && stop.y != 0) {
  125. aacircleRGBA(screen, stop.x, stop.y, 2, 255, 0, 0, 255);
  126. }
  127. SDL_Flip(screen);
  128. SDL_WaitEvent(&e);
  129. switch (e.type) {
  130. case SDL_QUIT :
  131. quit = 1;
  132. break;
  133. case SDL_MOUSEBUTTONDOWN :
  134. switch (e.button.button) {
  135. case SDL_BUTTON_LEFT:
  136. start.x = e.button.x;
  137. start.y = e.button.y;
  138. break;
  139. case SDL_BUTTON_RIGHT:
  140. stop.x = e.button.x;
  141. stop.y = e.button.y;
  142. break;
  143. }
  144. if (start.x != 0 && start.y != 0 && stop.x != 0 && stop.y != 0) {
  145. printf("Route from (%f,%f) to (%f,%f)\n", start.x, start.y, stop.x, stop.y);
  146. route = plotCourse_gridbased(&start, &stop, points, n);
  147. waypoint_t* r = route;
  148. while (r != NULL) {
  149. printf("Waypoint (%f,%f)\n", r->point.x, r->point.y);
  150. r = r->next;
  151. }
  152. }
  153. break;
  154. case SDL_KEYDOWN :
  155. switch (e.key.keysym.sym) {
  156. case SDLK_p:
  157. view_points = !view_points;
  158. break;
  159. case SDLK_g:
  160. case SDLK_l:
  161. view_grid = !view_grid;
  162. break;
  163. case SDLK_r:
  164. randomize(points, n, fac);
  165. break;
  166. case SDLK_s:
  167. set(points, n);
  168. break;
  169. case SDLK_q:
  170. quit = 1;
  171. break;
  172. case SDLK_UP:
  173. n++;
  174. points = (pixel_t *) realloc (points, sizeof(pixel_t) * n * n);
  175. fac = GLOBALS.WIDTH / (n + 1) * 0.3;
  176. set(points, n);
  177. break;
  178. case SDLK_DOWN:
  179. n--;
  180. points = (pixel_t *) realloc (points, sizeof(pixel_t) * n * n);
  181. fac = GLOBALS.WIDTH / (n + 1) * 0.3;
  182. set(points, n);
  183. break;
  184. case SDLK_PLUS:
  185. case SDLK_KP_PLUS:
  186. fac *= 1.1;
  187. printf("fac: %f\n", fac);
  188. set(points, n);
  189. randomize(points, n, fac);
  190. break;
  191. case SDLK_MINUS:
  192. case SDLK_KP_MINUS:
  193. fac /= 1.1;
  194. printf("fac: %f\n", fac);
  195. set(points, n);
  196. randomize(points, n, fac);
  197. break;
  198. default:
  199. break;
  200. }
  201. break;
  202. case SDL_VIDEORESIZE:
  203. GLOBALS.WIDTH = e.resize.w;
  204. GLOBALS.HEIGHT = e.resize.h;
  205. sdl_screen_init();
  206. break;
  207. default :
  208. break;
  209. }
  210. }
  211. free(points);
  212. return 0;
  213. }