-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.c
349 lines (245 loc) · 8.73 KB
/
graphics.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <stdio.h>
#include <gtk/gtk.h>
#include <chipmunk/chipmunk.h>
#include <stdlib.h>
#include <stdbool.h>
#include "specs/common.h"
#include "specs/graphics.h"
#include "specs/physics.h"
/*
graphics.c
contains the functions for drawing everything to
the GUI. The functions here are called by client
and used in conjuction with data passed to client
by the server.
*/
/*
drawing_coordinates struct
contains data used by graphics_get_shape
and graphics_draw_body for iteration with
Chipmunk
*/
typedef struct {
int capacity;
int size;
cpVect *positions;
} drawing_coordinates;
//function prototypes
static void graphics_set_color_from_body (cairo_t **cr, cpBody *body);
static void graphics_set_rgb_from_color (cairo_t **cr, COLOR color);
static void graphics_draw_body (cpBody *body, graphics_world *world);
static void graphics_get_shape (cpBody *body, cpShape *shape, drawing_coordinates *coords);
static void graphics_write_message (graphics_world *world);
static void graphics_draw_zone (graphics_world *world);
static void graphics_partial_shape (graphics_world *world);
/*
iterates over the bodies of the cpSpace inside the world
and calls graphics_draw_body on each body
Parameters:
*world = graphics world
Returns: nothing
*/
void
graphics_space_iterate (graphics_world *world) {
//draw the drawing zone
if (world->display)
graphics_draw_zone (world);
//draw the outline of the user's object
if (world->user_points->len > 0)
graphics_partial_shape (world);
//chipmunk iterator for each body
cpSpaceEachBody(world->space, (cpSpaceBodyIteratorFunc) graphics_draw_body, world);
//draw the ground
graphics_draw_body (world->space->staticBody, world);
graphics_write_message (world);
}
/*
draws the line tracing the user's drawing in
the currently set color
Parameters:
*world = graphics world
Returns: nothing
*/
static void
graphics_partial_shape (graphics_world *world) {
int space_height = 50, space_width = 50;
int window_height = gtk_widget_get_allocated_height(world -> drawing_screen);
int window_width = gtk_widget_get_allocated_width(world -> drawing_screen);
float screen_height_ratio = (float)window_height / space_height;
float screen_width_ratio = (float)window_width / space_width;
static const double dashed[] = {14.0, 6.0};
static int len = sizeof(dashed) / sizeof(dashed[0]);
cairo_t *cr = gdk_cairo_create (gtk_widget_get_window(world->drawing_screen));
cairo_set_dash(cr, dashed, len, 1);
graphics_set_rgb_from_color (&cr, world->color);
cairo_translate (cr, window_width / 2 , window_height / 2);
cpVect *vect = (cpVect *)world->user_points->data;
cairo_move_to (cr, screen_width_ratio * vect[0].x, -screen_height_ratio * vect[0].y);
//connect each user input mouse point with dashes
for (int i = 1; i < world->user_points->len; i++) {
cairo_line_to (cr, screen_width_ratio * vect[i].x, -screen_height_ratio * vect[i].y);
}
cairo_stroke(cr);
cairo_destroy(cr);
}
/*
writes the message to the screen
Parameters:
*world = graphics world
Returns: nothing
*/
static void
graphics_write_message (graphics_world *world) {
int window_height = gtk_widget_get_allocated_height(world -> drawing_screen);
int window_width = gtk_widget_get_allocated_width(world -> drawing_screen);
cairo_text_extents_t extents;
cairo_t *cr = gdk_cairo_create (gtk_widget_get_window(world->drawing_screen));
cairo_set_source_rgb (cr, 1, 0, 0);
cairo_select_font_face(cr, "Purisa", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(cr, 15);
// null checking
if (world->message != NULL) {
cairo_text_extents(cr, world->message, &extents);
cairo_move_to (cr, window_width / 2 - extents.width / 2, window_height / 10);
cairo_show_text (cr, world->message);
}
cairo_destroy (cr);
}
/*
draws the drawing zone in which
the player is allowed to draw
Parameters:
*world = graphics world
Returns: nothing
*/
static void
graphics_draw_zone (graphics_world *world) {
int space_height = 50, space_width = 50;
int window_height = gtk_widget_get_allocated_height(world -> drawing_screen);
int window_width = gtk_widget_get_allocated_width(world -> drawing_screen);
float screen_height_ratio = (float)window_height / space_height;
float screen_width_ratio = (float)window_width / space_width;
cairo_t *cr = gdk_cairo_create (gtk_widget_get_window(world->drawing_screen));
cairo_set_source_rgba(cr, 0, 0, 0, 1);
cairo_translate (cr, window_width / 2 , window_height / 2);
//Dash influenced from zetcode.com
static const double dashed[] = {14.0, 6.0};
static int len = sizeof(dashed) / sizeof(dashed[0]);
cairo_set_dash(cr, dashed, len, 1);
cairo_move_to(cr, screen_width_ratio * world->x1, -screen_height_ratio * world->y1);
cairo_line_to(cr, screen_width_ratio * world->x1, -screen_height_ratio * world->y2);
cairo_line_to(cr, screen_width_ratio * world->x2, -screen_height_ratio * world->y2);
cairo_line_to(cr, screen_width_ratio * world->x2, -screen_height_ratio * world->y1);
cairo_line_to(cr, screen_width_ratio * world->x1, -screen_height_ratio * world->y1);
cairo_stroke(cr);
cairo_destroy(cr);
}
/*
draws the body to the screen
Parameters:
*body = body to be drawn
*world = graphics world
Returns: nothing
*/
static void
graphics_draw_body ( cpBody *body, graphics_world *world ) {
cpVect point_position = cpBodyGetPos (body);
cpFloat angle = cpBodyGetAngle (body);
drawing_coordinates *coords = (drawing_coordinates *) malloc(sizeof(drawing_coordinates));
//null checking
if (coords == NULL) {
printf("Memory allocation error: in graphics_draw_body\n");
exit(-1);
}
coords->capacity = 1;
coords->size = 0;
coords->positions = (cpVect *) malloc(coords->capacity * sizeof(cpVect));
//null checking
if (coords->positions == NULL) {
printf("Memory allocation error: in graphics_draw_body\n");
exit(-1);
}
//iterate over each shape for new coordinates
cpBodyEachShape(body, (cpBodyShapeIteratorFunc) graphics_get_shape, coords);
if (coords->size > 0) {
int space_height = 50, space_width = 50;
int window_height = gtk_widget_get_allocated_height(world -> drawing_screen);
int window_width = gtk_widget_get_allocated_width(world -> drawing_screen);
float screen_height_ratio = (float)window_height / space_height;
float screen_width_ratio = (float)window_width / space_width;
cairo_t *cr = gdk_cairo_create (gtk_widget_get_window(world->drawing_screen));
graphics_set_color_from_body (&cr, body);
cairo_matrix_t saved_transform;
cairo_get_matrix(cr, &saved_transform);
cairo_set_line_width (cr, 6);
cairo_translate (cr, window_width / 2 + screen_width_ratio * point_position.x, window_height - window_height / 2 - screen_height_ratio * point_position.y);
cairo_rotate(cr, -angle);
cairo_move_to (cr, screen_width_ratio * coords->positions[0].x, -screen_height_ratio * coords->positions[0].y);
// creates the outline of the shape
for (int i = 1; i < coords->size; i++) {
cairo_line_to (cr, screen_width_ratio * coords->positions[i].x, -screen_height_ratio * coords->positions[i].y);
cairo_stroke_preserve (cr);
}
// close and fill
cairo_close_path(cr);
cairo_fill (cr);
cairo_destroy(cr);
}
free (coords->positions);
free (coords);
}
/*
updates the coordinates of the shape being drawn
Parameters:
*body = body being drawn
*shape = shape representation of it
*coords = coordinates of the last draw callback
Returns: nothing
*/
static void
graphics_get_shape (cpBody *body, cpShape *shape, drawing_coordinates *coords) {
coords->size += 1;
if (coords->size >= coords->capacity) {
coords->capacity *= 2;
coords->positions = (cpVect *)realloc(coords->positions, coords->capacity * sizeof(cpVect));
}
coords->positions[coords->size - 1] = cpSegmentShapeGetA (shape);
}
/*
Sets the color according of the cairo pointer according to the shape color
Parameters:
**cr = cairo pointer that is used to draw to drawing area
shape = shape that must be drawn on screen
Returns: nothing
*/
static void
graphics_set_color_from_body (cairo_t **cr, cpBody *body) {
body_information *info = cpBodyGetUserData(body);
if ((info == NULL) || (&(info->color) == NULL)) {
cairo_set_source_rgb (*cr, 0, 0, 0);
}
else {
graphics_set_rgb_from_color (cr, info->color);
}
}
/*
sets the rgb of the shapes based off the color
type passed to it.
parameters: cairo context, the color
returns: nothing
*/
static void
graphics_set_rgb_from_color (cairo_t **cr, COLOR color) {
if (color == GREEN) {
cairo_set_source_rgb (*cr, 0, 255, 0);
}
else if (color == RED) {
cairo_set_source_rgb (*cr, 255, 0, 0);
}
else if (color == BLUE) {
cairo_set_source_rgb (*cr, 0, 0, 255);
}
else if (color == YELLOW) {
cairo_set_source_rgb (*cr, 200, 200, 0);
}
}