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
|
#include "draw.h"
#include "level.h"
#include "geometry.h"
#include <math.h>
#include <gtk/gtk.h>
#include <glib/gprintf.h>
#include <cairo/cairo.h>
static double scale = 100.0;
static double xTranslate = 0.0, yTranslate = 0.0;
static void drawGrid(cairo_t *cr, const RECTANGLE *rect) {
double depth = log10(scale)-0.75;
double depth2 = floor(depth);
double step = pow(0.1, depth2);
double d;
int i;
gchar buffer[10];
cairo_set_font_size(cr, 10.0/scale);
for(i = 0; 0.2*(depth-depth2+i-1) < 0.3; i++) {
d = MIN(0.2*(depth-depth2+i), 0.3);
cairo_set_source_rgb(cr, d, d, d);
for(d = rect->x - fmod(rect->x, step) - step; d <= rect->x+rect->width; d+=step) {
cairo_move_to(cr, d, rect->y);
cairo_line_to(cr, d, rect->y+rect->height);
cairo_stroke(cr);
if(step > 0.2)
g_snprintf(buffer, sizeof(buffer), "%i", (int)rint(d));
else
g_snprintf(buffer, sizeof(buffer), "%.*f", -(int)floor(log10(step*1.1)), d+step/10);
buffer[sizeof(buffer)-1] = '\0';
cairo_move_to(cr, d+1/scale, rect->y+11/scale);
cairo_show_text(cr, buffer);
}
for(d = rect->y - fmod(rect->y, step) - step; d <= rect->y+rect->height; d+=step) {
cairo_move_to(cr, rect->x, d);
cairo_line_to(cr, rect->x+rect->width, d);
cairo_stroke(cr);
if(step > 0.5)
g_snprintf(buffer, sizeof(buffer), "%i", (int)rint(d));
else
g_snprintf(buffer, sizeof(buffer), "%.*f", -(int)floor(log10(step*1.1)), d+step/10);
buffer[sizeof(buffer)-1] = '\0';
cairo_move_to(cr, rect->x+3/scale, d+11/scale);
cairo_show_text(cr, buffer);
}
step *= 10;
}
}
static void room2path(cairo_t *cr, const ROOM *room, const RECTANGLE *rect) {
int i;
ROOM room2 = {0, NULL};
// no vertices
if(room->nVertices == 0) return;
if(rect)
simplifyPolygon(room, rect, &room2);
else
room2 = *room;
if(room2.nVertices == 0) return;
cairo_new_sub_path(cr);
for(i = 0; i < room2.nVertices; i++) {
cairo_line_to(cr, room2.vertices[i].x, room2.vertices[i].y);
}
cairo_close_path(cr);
if(rect && room2.vertices)
free(room2.vertices);
}
gboolean drawTopView(GtkWidget *widget, GdkEventExpose *event, gpointer data) {
VERTEX vertices[4] = {{-1,-1}, {-1,1}, {1,1}, {1,-1}};
ROOM room = {sizeof(vertices)/sizeof(vertices[0]), vertices};
LEVEL lvl = {1, &room};
cairo_t *cr;
RECTANGLE rect = {-1, -1, widget->allocation.width+2, widget->allocation.height+2};
int i;
cr = gdk_cairo_create(widget->window);
gdk_cairo_region(cr, event->region);
cairo_clip(cr);
cairo_translate(cr, getImageWidth()/2-xTranslate, getImageHeight()/2-yTranslate);
cairo_scale(cr, scale, scale);
cairo_device_to_user(cr, &rect.x, &rect.y);
cairo_device_to_user_distance(cr, &rect.width, &rect.height);
cairo_set_line_width(cr, 1.0/scale);
cairo_set_line_join(cr, CAIRO_LINE_JOIN_MITER);
drawGrid(cr, &rect);
for(i = 0; i < lvl.nRooms; i++)
room2path(cr, &lvl.rooms[i], &rect);
cairo_set_source_rgba(cr, 0.0, 0.7, 1.0, 0.3);
cairo_fill_preserve(cr);
cairo_set_source_rgba(cr, 0.0, 0.7, 1.0, 0.7);
cairo_stroke(cr);
cairo_destroy (cr);
return FALSE;
}
double getScale() {
return scale;
}
void setScale(double s) {
scale = s;
}
double getImageWidth() {
return 5*scale;
}
double getImageHeight() {
return 5*scale;
}
double getXTranslate() {
return xTranslate;
}
void setXTranslate(double x) {
xTranslate = x;
}
double getYTranslate() {
return yTranslate;
}
void setYTranslate(double y) {
yTranslate = y;
}
|