-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawing.cpp
318 lines (263 loc) · 5.77 KB
/
drawing.cpp
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
// drawing.m
#include "doombsp.h"
#include <SDL2/SDL.h>
//id window_i, view_i;
SDL_Window * window_i;
SDL_Renderer * renderer_i;
float scale = 0.125;
NXRect worldbounds;
SDL_Point draw_origin;
// TF 3/28: Wrappers for SDL Functions that offset by draw_origin
// draw a line with draw_origin offset
void DrawLine(int x1, int y1, int x2, int y2)
{
x1 -= draw_origin.x;
y1 -= draw_origin.y;
x2 -= draw_origin.x;
y2 -= draw_origin.y;
SDL_RenderDrawLine (renderer_i, x1, y1, x2, y2);
}
void FillRect(const SDL_Rect *r)
{
SDL_Rect adjust = *r;
adjust.x -= draw_origin.x;
adjust.y -= draw_origin.y;
SDL_RenderFillRect (renderer_i, &adjust);
}
void DrawRect(const SDL_Rect *r)
{
SDL_Rect adjust = *r;
adjust.x -=- draw_origin.x;
adjust.y -=- draw_origin.y;
SDL_RenderDrawRect (renderer_i, &adjust);
}
/*
================
=
= IDRectFromPoints
=
= Makes the rectangle just touch the two points
=
================
*/
void IDRectFromPoints(NXRect *rect, NXPoint const *p1, NXPoint const *p2 )
{
// return a rectangle that encloses the two points
if (p1->x < p2->x)
{
rect->origin.x = p1->x;
rect->size.width = p2->x - p1->x + 1;
}
else
{
rect->origin.x = p2->x;
rect->size.width = p1->x - p2->x + 1;
}
if (p1->y < p2->y)
{
rect->origin.y = p1->y;
rect->size.height = p2->y - p1->y + 1;
}
else
{
rect->origin.y = p2->y;
rect->size.height = p1->y - p2->y + 1;
}
}
/*
==================
=
= IDEnclosePoint
=
= Make the rect enclose the point if it doesn't allready
=
==================
*/
void IDEnclosePoint (NXRect *rect, NXPoint const *point)
{
float right, top;
right = rect->origin.x + rect->size.width - 1;
top = rect->origin.y + rect->size.height - 1;
if (point->x < rect->origin.x)
rect->origin.x = point->x;
if (point->y < rect->origin.y)
rect->origin.y = point->y;
if (point->x > right)
right = point->x;
if (point->y > top)
top = point->y;
rect->size.width = right - rect->origin.x + 1;
rect->size.height = top - rect->origin.y + 1;
}
/*
===========
=
= BoundLineStore
=
===========
*/
void BoundLineStore (Storage *lines_i, NXRect *r)
{
int i,c;
worldline_t *line_p;
c = lines_i->count();
if (!c)
Error ("BoundLineStore: empty list");
line_p = (worldline_t *)lines_i->elementAt(0);
IDRectFromPoints (r, &line_p->p1, &line_p->p2);
for (i=1 ; i<c ; i++)
{
line_p = (worldline_t *)lines_i->elementAt(i);
IDEnclosePoint (r, &line_p->p1);
IDEnclosePoint (r, &line_p->p2);
}
}
/*
===========
=
= DrawLineStore
=
= Draws all of the lines in the given storage object
=
===========
*/
void DrawLineStore (Storage *lines_i)
{
int i,c;
worldline_t *line_p;
if (!draw)
return;
c = lines_i->count();
for (i=0 ; i<c ; i++)
{
line_p = (worldline_t *)lines_i->elementAt(i);
//PSmoveto (line_p->p1.x, line_p->p1.y);
//PSlineto (line_p->p2.x, line_p->p2.y);
//PSstroke ();
DrawLine (line_p->p1.x, line_p->p1.y, line_p->p2.x, line_p->p2.y);
}
NXPing ();
}
/*
===========
=
= DrawLine
=
= Draws all of the lines in the given storage object
=
===========
*/
void DrawLineDef (maplinedef_t *ld)
{
mapvertex_t *v1, *v2;
if (!draw)
return;
v1 = (mapvertex_t *)mapvertexstore_i->elementAt (ld->v1);
v2 = (mapvertex_t *)mapvertexstore_i->elementAt (ld->v2);
// PSmoveto (v1->x, v1->y);
// PSlineto (v2->x, v2->y);
// PSstroke ();
// NXPing ();
DrawLine (v1->x, v1->y, v2->x, v2->y);
NXPing ();
}
/*
===========
=
= DrawMap
=
===========
*/
void DrawMap (void)
{
NXRect scaled;
BoundLineStore (linestore_i, &worldbounds);
worldbounds.origin.x -= 8;
worldbounds.origin.y -= 8;
worldbounds.size.width += 16;
worldbounds.size.height += 16;
if (!draw)
return;
scaled.origin.x = 300;
scaled.origin.y = 80;
scaled.size.width = worldbounds.size.width*scale * 2;
scaled.size.height = worldbounds.size.height* scale * 2; // TODO: window size
window_i = // MARK: SDL
#if 0
[[Window alloc]
initContent: &scaled
style: NX_TITLEDSTYLE
backing: NX_RETAINED
buttonMask: 0
defer: false
];
#endif
SDL_CreateWindow ("",
scaled.origin.x,
scaled.origin.y,
scaled.size.width,
scaled.size.height,
0);
//[window_i display];
//[window_i orderFront: nil];
//view_i = [window_i contentView];
renderer_i = SDL_CreateRenderer(window_i, -1, SDL_RENDERER_SOFTWARE);
SDL_PumpEvents();
#if 0
[view_i
setDrawSize: worldbounds.size.width
: worldbounds.size.height];
[view_i
setDrawOrigin: worldbounds.origin.x
: worldbounds.origin.y];
#endif
draw_origin.x = worldbounds.origin.x;
draw_origin.y = worldbounds.origin.y;
SDL_RenderSetLogicalSize (renderer_i,
worldbounds.size.width,
worldbounds.size.height);
//[view_i lockFocus];
//PSsetgray (NX_BLACK);
EraseWindow();
SDL_SetRenderDrawColor (renderer_i, 0, 0, 0, 255);
DrawLineStore (linestore_i);
}
/*
===========
=
= EraseWindow
=
===========
*/
void EraseWindow (void)
{
if (!draw)
return;
NXEraseRect (&worldbounds);
NXPing ();
}
/*
============================
=
= DrawDivLine
=
============================
*/
void DrawDivLine (divline_t *div)
{
float vx,vy, dist;
if (!draw)
return;
//PSsetgray (NX_BLACK);
SDL_SetRenderDrawColor (renderer_i, 0, 0, 0, 255);
dist = sqrt (pow(div->dx,2)+pow(div->dy,2));
vx = div->dx/dist;
vy = div->dy/dist;
dist = MAX(worldbounds.size.width,worldbounds.size.height);
DrawLine (div->pt.x - vx*dist, div->pt.y - vy*dist,
div->pt.x + vx*dist, div->pt.y + vy*dist);
// PSmoveto (div->pt.x - vx*dist, div->pt.y - vy*dist);
// PSlineto (div->pt.x + vx*dist, div->pt.y + vy*dist);
// PSstroke ();
NXPing ();
}