Browse Source

Outout im JOSN Format

Das SDL-Eumel wirft jetzt eine Weltbeschreibung im JSON-Format aus,
damit Visualisierungs-Clients was zum Testen haben. Erstmal nur eine
Liste an Planeten, als nächstes kommen aber auch Raumschiffe die die
geklickte Route abfliegen.
Patrick Kilian 9 years ago
parent
commit
7dedecc4ce
6 changed files with 48 additions and 49 deletions
  1. 1
    1
      gitter/Makefile
  2. 0
    8
      gitter/globals.h
  3. 28
    0
      gitter/json_output.c
  4. 7
    0
      gitter/json_output.h
  5. 1
    24
      gitter/route.c
  6. 11
    16
      gitter/screen.c

+ 1
- 1
gitter/Makefile View File

@@ -2,7 +2,7 @@ CXX = gcc
2 2
 FLAGS = -Wall -g3 -O2 -march=native -mtune=native
3 3
 CLIBS = `sdl-config --cflags`
4 4
 LDFLAGS = `sdl-config --libs` -lSDL_gfx  -lm -lpthread
5
-OBJ = main.o screen.o route.o
5
+OBJ = main.o screen.o route.o json_output.o
6 6
 
7 7
 main: $(OBJ)
8 8
 	$(CXX) $(FLAGS) $(LDFLAGS) -o $@ $(OBJ) 

+ 0
- 8
gitter/globals.h View File

@@ -10,15 +10,7 @@ typedef struct {
10 10
 	int FULLSCREEN_HEIGHT;
11 11
 	int DELTA;
12 12
 	int FULLSCREEN;
13
-	int VERBOSE;
14 13
 } options_t;
15 14
 
16 15
 options_t GLOBALS;
17
-
18
-#define VERBOSE(...) \
19
-	if (GLOBALS.VERBOSE) {\
20
-		printf(__VA_ARGS__);\
21
-	}
22
-
23
-#define ERROR(...) fprintf(stderr, __VA_ARGS__);
24 16
 #endif  /*GLOBALS_H*/

+ 28
- 0
gitter/json_output.c View File

@@ -0,0 +1,28 @@
1
+#include <math.h>
2
+#include <stdio.h>
3
+#include <stdlib.h>
4
+#include "globals.h"
5
+#include "screen.h"
6
+
7
+void json_output(pixel_t* planets, int n_planets) {
8
+	int i;
9
+
10
+	printf("{ \"world\":\n");
11
+	printf("  { \"bounding-box\": { \"xmin\": 0, \"xmax\": %d, \"ymin\": 0, \"ymax\": %d},\n", GLOBALS.WIDTH, GLOBALS.HEIGHT);
12
+	printf("    \"planets\": [\n");
13
+	for(i = 0; i < n_planets; i++) {
14
+		printf("      {\"id\":%d, \"x\": %f, \"y\": %f, \"owner\": null},\n", i, planets[i].x, planets[i].y);
15
+	}
16
+	printf("    ],\n");
17
+	printf("    \"ships\": [ ],\n");
18
+	printf("    \"asteroids\": [ ],\n");
19
+	printf("    \"players\": [\n");
20
+	printf("      {\"id\": 1, \"name\": \"SDL\"},\n");
21
+	printf("      {\"id\": 2, \"name\": \"Test\"},\n");
22
+	printf("    ],\n");
23
+	printf("    \"shots\": [ ],\n");
24
+	printf("    \"hits\": [ ],\n");
25
+	printf("    \"explosions\": [ ],\n");
26
+	printf("  }\n");
27
+	printf("}\n");
28
+}

+ 7
- 0
gitter/json_output.h View File

@@ -0,0 +1,7 @@
1
+#ifndef JSON_H
2
+#define JSON_H
3
+
4
+#include "screen.h"
5
+void json_output(pixel_t* planets, int n_planets);
6
+
7
+#endif  /*JSON_H*/

+ 1
- 24
gitter/route.c View File

@@ -50,31 +50,10 @@ waypoint_t* go_around(pixel_t* A, pixel_t* B, pixel_t* C, double r) {
50 50
 	wp->next = NULL;
51 51
 	wp->point.x = W.x;
52 52
 	wp->point.y = W.y;
53
-	//printf("Suggest you go via (%f,%f) to avoid (%f,%f)\n", W.x, W.y, C->x, C->y);
54 53
 	return wp;
55 54
 }
56 55
 
57 56
 waypoint_t* route(pixel_t* start, pixel_t* stop, pixel_t* points, int n_points) {
58
-	//printf("Running route\n");
59
-
60
-	/*
61
-	pixel_t A = {0, 0};
62
-	pixel_t B = {1000, 0};
63
-	pixel_t C1 = {500, 200};
64
-	printf("A = (0,0), B=(1000,0), C1=(500,200)\n");
65
-	printf("dist(A,B) = %f\n", dist(&A, &B));
66
-	printf("r(AB, C1) = %f\n", dividing_ratio(&A, &B, &C1));
67
-	printf("dist_to_seg(A, B, C1) = %f\n", dist_to_seg(&A, &B, &C1));
68
-	pixel_t C2 = {-100, 200};
69
-	printf("C2 = (-100,200)\n");
70
-	printf("r(AB, C2) = %f\n", dividing_ratio(&A, &B, &C2));
71
-	printf("dist_to_seg(A, B, C2) = %f\n", dist_to_seg(&A, &B, &C2));
72
-	pixel_t C3 = {200, -400};
73
-	printf("C3 = (200,-400)\n");
74
-	printf("r(AB, C3) = %f\n", dividing_ratio(&A, &B, &C2));
75
-	printf("dist_to_seg(A, B, C3) = %f\n", dist_to_seg(&A, &B, &C3));
76
-	*/
77
-
78 57
 	int i;
79 58
 
80 59
 	int i_min = -1;
@@ -97,7 +76,6 @@ waypoint_t* route(pixel_t* start, pixel_t* stop, pixel_t* points, int n_points)
97 76
 		if (r > 0 && r < 1) {
98 77
 			double d = dist_to_line(start, stop, &(points[i]));
99 78
 			if (fabs(d) < safety_radius) {
100
-				//printf("Point #%d at (%f,%f) is an obstacle %f pixel away from the course\n", i, points[i].x, points[i].y, d);
101 79
 				if(fabs(r-0.5) < fabs(r_min-0.5)) {
102 80
 					i_min = i;
103 81
 					r_min = r;
@@ -107,7 +85,6 @@ waypoint_t* route(pixel_t* start, pixel_t* stop, pixel_t* points, int n_points)
107 85
 	}
108 86
 
109 87
 	if(i_min >= 0) {
110
-		//printf("Point #%d at (%f,%f) is the first obstacle %f pixel down the course\n", i_min, points[i_min].x, points[i_min].y, r_min * dist(start, stop));
111 88
 		waypoint_t* wp = go_around(start, stop, &(points[i_min]), r_min);
112 89
 
113 90
 		waypoint_t* part1 = route(start, &(wp->point), points, n_points);
@@ -202,7 +179,7 @@ void find_face(pixel_t *p, pixel_t *points, int n, int *x, int *y) {
202 179
 			double r = dist_to_line(surrounding + edge, surrounding + (edge + 1) % 4, p);
203 180
 			if (r > 0) {
204 181
 
205
-				printf("edge: %d, r %f, x %d, y %d\n", edge, r, face_x, face_y);
182
+				fprintf(stderr, "edge: %d, r %f, x %d, y %d\n", edge, r, face_x, face_y);
206 183
 				retry = 1;
207 184
 				count++;
208 185
 

+ 11
- 16
gitter/screen.c View File

@@ -8,6 +8,7 @@
8 8
 #include "globals.h"
9 9
 #include "screen.h"
10 10
 #include "route.h"
11
+#include "json_output.h"
11 12
 
12 13
 /* TODO:
13 14
  *
@@ -21,7 +22,7 @@ int safety_radius;
21 22
 void sdl_setup() {
22 23
 	const SDL_VideoInfo *info;
23 24
 	if (SDL_Init(SDL_INIT_VIDEO) == -1) {
24
-		ERROR("SDL_Init() failed: %s\n", SDL_GetError());
25
+		fprintf(stderr, "SDL_Init() failed: %s\n", SDL_GetError());
25 26
 		exit(1);
26 27
 	}
27 28
 
@@ -45,7 +46,7 @@ void sdl_init() {
45 46
 	sdl_screen_init();
46 47
 
47 48
 	if (screen == NULL) {
48
-		ERROR("SDL_SetVideoMode() failed: %s\n", SDL_GetError());
49
+		fprintf(stderr, "SDL_SetVideoMode() failed: %s\n", SDL_GetError());
49 50
 		exit(1);
50 51
 	}
51 52
 }
@@ -207,42 +208,35 @@ int main_loop() {
207 208
 						break;
208 209
 				}
209 210
 				if (start.x != 0 && start.y != 0 && stop.x != 0 && stop.y != 0) {
210
-					printf("\n\nRoute from (%f,%f) to (%f,%f)\n", start.x, start.y, stop.x, stop.y);
211
+					fprintf(stderr, "\n\nRoute from (%f,%f) to (%f,%f)\n", start.x, start.y, stop.x, stop.y);
211 212
 
212 213
 					gettimeofday(&t1, NULL);
213 214
 					route1 = plotCourse(&start, &stop, points, n);
214 215
 					gettimeofday(&t2, NULL);
215 216
 					elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;
216 217
 					elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;
217
-					printf("plotCourse scanline based took %f ms\n", elapsedTime);
218
+					fprintf(stderr, "plotCourse scanline based took %f ms\n", elapsedTime);
218 219
 
219 220
 					gettimeofday(&t1, NULL);
220 221
 					route1s = smooth(route1, 5);
221 222
 					gettimeofday(&t2, NULL);
222 223
 					elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;
223 224
 					elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;
224
-					printf("smoth scanline based route took %f ms\n", elapsedTime);
225
+					fprintf(stderr, "smoth scanline based route took %f ms\n", elapsedTime);
225 226
 
226 227
 					gettimeofday(&t1, NULL);
227 228
 					route2 = plotCourse_gridbased(&start, &stop, points, n);
228 229
 					gettimeofday(&t2, NULL);
229 230
 					elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;
230 231
 					elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;
231
-					printf("plotCourse gridline based took %f ms\n", elapsedTime);
232
+					fprintf(stderr, "plotCourse gridline based took %f ms\n", elapsedTime);
232 233
 
233 234
 					gettimeofday(&t1, NULL);
234 235
 					route2s = smooth(route2, 5);
235 236
 					gettimeofday(&t2, NULL);
236 237
 					elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;
237 238
 					elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;
238
-					printf("smooth gridline based route took %f ms\n", elapsedTime);
239
-					/*
240
-					waypoint_t* r = route;
241
-					while (r != NULL) {
242
-						printf("Waypoint (%f,%f)\n", r->point.x, r->point.y);
243
-						r = r->next;
244
-					}
245
-					*/
239
+					fprintf(stderr, "smooth gridline based route took %f ms\n", elapsedTime);
246 240
 				}
247 241
 				break;
248 242
 			case SDL_KEYDOWN :
@@ -284,14 +278,14 @@ int main_loop() {
284 278
 					case SDLK_PLUS:
285 279
 					case SDLK_KP_PLUS:
286 280
 						fac *= 1.1;
287
-						printf("fac: %f\n", fac);
281
+						fprintf(stderr, "fac: %f\n", fac);
288 282
 						set(points, n);
289 283
 						randomize(points, n, fac);
290 284
 						break;
291 285
 					case SDLK_MINUS:
292 286
 					case SDLK_KP_MINUS:
293 287
 						fac /= 1.1;
294
-						printf("fac: %f\n", fac);
288
+						fprintf(stderr, "fac: %f\n", fac);
295 289
 						set(points, n);
296 290
 						randomize(points, n, fac);
297 291
 						break;
@@ -307,6 +301,7 @@ int main_loop() {
307 301
 			default :
308 302
 				break;
309 303
 		}
304
+		json_output(points, n*n);
310 305
 	}
311 306
 
312 307
 	free(points);