-
Notifications
You must be signed in to change notification settings - Fork 4
/
drawprocs.pas
575 lines (513 loc) · 11.1 KB
/
drawprocs.pas
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
unit drawprocs;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, math, rmtools;
type
DrawItemProc = procedure(x,y,index,mode : integer); //use mode to decide if we want to update maps/sprite values
GetItemProc = function(x,y : integer) : integer;
procedure SetDrawPixelProc(dic : drawitemproc);
procedure SetGetPixelProc(gic : GetItemProc);
procedure DrawItem(item,x,y,x2,y2,index,mode : integer);
procedure FloodFill(x,y, MaxX,MaxY,index,mode : integer);
procedure ReplaceFill(x, y, MaxX,MaxY,index,mode : integer);
implementation
var
DefaultDrawItemProc : DrawItemProc;
DefaultGetItemProc : GetItemProc;
//putx - could be pixel or tile
procedure PutItemX(x,y,index,mode : integer);
begin
DefaultDrawItemProc(x,y,index,mode);
end;
function GetItemX(x,y : integer) : integer;
begin
result:=DefaultGetItemProc(x,y);
end;
const
MaxQueueSize = 100;
Type
TFPoint = record
x,y : integer;
end;
TPixelQueue = Class
Count : integer;
PointList : array[1..MaxQueueSize] of TFPoint;
constructor Create;
procedure push(var pt : TFPoint);
procedure popfirst(var pt : TFPoint);
procedure pop(var pt : TFPoint);
function GetCount : integer;
end;
TSprayPointsRec = Record
x,y : integer;
end;
var
SprayPoints : array[1..MaxSprayPoints] of TSprayPointsRec;
constructor TPixelQueue.Create;
begin
count:=0;
end;
procedure TPixelQueue.push(var pt : TFPoint);
begin
if (count+1) > MaxQueueSize then exit;
inc(count);
PointList[count]:=pt;
end;
procedure TPixelQueue.popfirst(var pt : TFPoint);
var
i : integer;
begin
if count > 0 then
begin
pt:=PointList[1];
dec(count);
for i:=1 to count do
begin
PointList[i]:=PointList[i+1];
end;
end;
end;
procedure TPixelQueue.pop(var pt : TFPoint);
begin
if count > 0 then
begin
pt:=PointList[count];
dec(count);
end;
end;
function TPixelQueue.GetCount : integer;
begin
GetCount:=count;
end;
procedure FloodFill(x, y, MaxX,MaxY,index,mode : integer);
var
x1 : integer;
spanAbove, spanBelow : boolean;
PQ : TPixelQueue;
temp : TFPoint;
oldColor : integer;
begin
if GetItemX(x,y) = index then exit;
oldColor:=GetItemX(x,y);
PQ:=TPixelQueue.Create;
temp.x:=x;
temp.y:=y;
PQ.push(temp);
while PQ.GetCount>0 do
begin
PQ.popfirst(temp);
x:=temp.x;
y:=temp.y;
x1 := x;
while ((x1 >= 0) and (GetItemX(x1,y) = oldColor)) do
begin
x1:=x1-1;
end;
x1:=x1+1;
spanAbove := false;
spanBelow := false;
while((x1 < MaxX) and (GetItemX(x1,y) = oldColor)) do
begin
PutItemX( x1,y, index,mode);
if((NOT spanAbove) and (y > 0) and (GetItemX(x1,(y - 1) ) = oldColor)) then
begin
temp.x:=x1;
temp.y:=y-1;
PQ.push(temp);
spanAbove := true;
end
else if (spanAbove and (y > 0) and (GetItemX(x1,(y - 1)) <> oldColor)) then
begin
spanAbove := false;
end;
if ((NOT spanBelow) and (y < MaxY - 1) and (GetItemX(x1,(y + 1)) = oldColor)) then
begin
temp.x:=x1;
temp.y:=y+1;
PQ.push(temp);
spanBelow := true;
end
else if(spanBelow and (y < MaxY - 1) and (GetItemX(x1,(y + 1)) <> oldColor)) then
begin
spanBelow := false;
end;
x1:=x1+1;
end;
end;
end;
//replaces all occurances in Map of selected Tile - like flood fill but for all - does not need to be connected - mainly to be used for missing tiles
procedure ReplaceFill(x, y, MaxX,MaxY,index,mode : integer);
var
i,j,ReplaceColor : integer;
begin
if GetItemX(x,y) = index then exit;
ReplaceColor:=GetItemX(x,y);
for j:=0 to MaxY-1 do
begin
for i:=0 to MaxX-1 do
begin
if GetItemX(i,j) = ReplaceColor then PutItemX( i,j, index,mode);
end;
end;
end;
Procedure CreateRandomSprayPoints;
var
i : integer;
begin
for i:=1 to MaxSprayPoints do
begin
SprayPoints[i].x:=randomrange(-5,5);
SprayPoints[i].y:=randomrange(-5,5);
end;
end;
procedure SprayPaint(x,y,index,mode : integer);
var
i : integer;
begin
CreateRandomSprayPoints;
for i:=1 to MaxSprayPoints do
begin
PutItemX(x+SprayPoints[i].x,y+SprayPoints[i].y,index,mode);
end;
end;
procedure HLine(x,x2,y,index, mode : integer);
var
xx,newx, newx2 : integer;
begin
newx:=x;
newx2:=x2;
if x2 < x then
begin
newx:=x2;
newx2:=x;
end;
for xx:=newx to newx2 do
begin
PutItemX(xx,y,index,mode);
end;
end;
procedure VLine(y,y2,x, index, mode : integer);
var
yy,newy,newy2 : integer;
begin
newy:=y;
newy2:=y2;
if y2 < y then
begin
newy:=y2;
newy2:=y;
end;
for yy:=newy to newy2 do
begin
PutItemX(x,yy,index, mode);
end;
end;
procedure Rectangle(x,y,x2,y2,index,full,mode : integer);
var
newy,newy2 : integer;
newx, newx2 : integer;
i,j : integer;
begin
newx:=x;
newx2:=x2;
if x2 < x then
begin
newx:=x2;
newx2:=x;
end;
newy:=y;
newy2:=y2;
if y2 < y then
begin
newy:=y2;
newy2:=y;
end;
if (full = 1) then
begin
for i:=newx to newx2 do
begin
for j:=newy to newy2 do
begin
PutItemX(i,j,index,mode);
end;
end;
exit;
end;
if (newx=newx2) and (newy=newy2) then
begin
PutItemX(newx,newy,index,mode);
end
else if(newx+1=newx2) and(newy+1=newy2) then
begin
Hline(newx,newx2,newy,index,mode);
Hline(newx,newx2,newy2,index,mode);
end
else if (newy+1=newy2) then
begin
Hline(newx,newx2,newy,index,mode);
Hline(newx,newx2,newy2,index,mode);
end
else if (newx=newx2) and (newy<>newy2) then
begin
Vline(newy,newy2,newx,index,mode);
end
else if (newy=newy2) and (newx<>newx2) then
begin
Hline(newx,newx2,newy,index,mode);
end
else
begin
Hline(newx,newx2,newy,index,mode);
Hline(newx,newx2,newy2,index,mode);
Vline(newy+1,newy2-1,newx,index,mode);
Vline(newy+1,newy2-1,newx2,index,mode);
end;
end;
Procedure Line(x1,y1,x2,y2,index,mode : integer);
var
xr,yr,dxr,dyr:real;
x,y,dx,dy:integer;
begin
if abs(x2-x1)>=abs(y2-y1) then
begin
if x1<=x2 then dx:=1 else dx:=-1;
if x1<>x2 then dyr:=(y2-y1)/abs(x2-x1) else dyr:=0;
x:=x1-dx;
yr:=y1;
repeat
x:=x+dx;
PutItemX(x,round(yr),index,mode);
yr:=yr+dyr;
until x=x2;
end
else
begin
dxr:=(x2-x1)/abs(y2-y1);
if y1<y2 then dy:=1 else dy:=-1;
y:=y1-dy;
xr:=x1;
repeat
y:=y+dy;
PutItemX(round(xr),y,index,mode);
xr:=xr+dxr;
until y=y2;
end;
end;
procedure empty_ellipse(xc, yc, a, b,index, mode : integer);
var
x,y : integer;
a2,b2 : longint;
crit1,crit2,crit3 : longint;
t,dxt,d2xt,dyt,d2yt : longint;
begin
x := 0;
y := b;
a2 :=a*a;
b2 :=b*b;
crit1 := -(a2 div 4 + a mod 2 + b2);
crit2 := -(b2 div 4 + b mod 2 + a2);
crit3 := -(b2 div 4 + b mod 2);
t := -a2*y;
dxt := 2*b2*x;
dyt := -2*a2*y;
d2xt := 2*b2;
d2yt := 2*a2;
while (y>=0) AND (x<=a) do
begin
PutItemX(xc+x, yc+y,index,mode);
if (x<>0) OR (y<>0) then
begin
PutItemX(xc-x, yc-y,index,mode);
if (x<>0) AND (y<>0) then
begin
PutItemX(xc+x, yc-y,index,mode);
PutItemX(xc-x, yc+y,index,mode);
end;
if (t + b2*x <= crit1) OR (t + a2*y <= crit3) then
begin
inc(x);
inc(dxt,d2xt);
inc(t,dxt);
end
else if (t - a2*y > crit2) then
begin
dec(y);
inc(dyt,d2yt);
inc(t,dyt);
end
else
begin
inc(x);
inc(dxt,d2xt);
inc(t,dxt);
dec(y);
inc(dyt,d2yt);
inc(t,dyt);
end;
end;
end;
end;
procedure rLine(x,y,w,index,mode : integer);
var
i : integer;
begin
for i:=x to x+w-1 do
begin
PutItemX(i,y,index,mode);
end;
end;
procedure filled_ellipse(xc, yc, a, b ,index, mode : integer);
var
x,y : integer;
a2,b2 : longint;
crit1,crit2,crit3 : longint;
t,dxt,d2xt,dyt,d2yt : longint;
width : word;
begin
x := 0;
y := b;
width := 1;
a2 := longint(a)*a;
b2 := longint(b)*b;
crit1 := -(a2 div 4 + (a mod 2) + b2);
crit2 := -(b2 div 4 + (b mod 2) + a2);
crit3 := -(b2 div 4 + (b mod 2));
t := -a2*y;
dxt := 2*b2*x;
dyt := -2*a2*y;
d2xt := 2*b2;
d2yt := 2*a2;
while (y>=0) AND (x<=a) do
begin
if ((t + b2*x) <= crit1) OR ((t + a2*y) <= crit3) then
begin
inc(x);
inc(dxt,d2xt);
inc(t,dxt);
inc(width,2);
end
else if ((t - a2*y) > crit2) then
begin
rline(xc-x, yc-y, width,index,mode);
if (y<>0) then rline(xc-x, yc+y, width,index,mode);
dec(y);
inc(dyt,d2yt);
inc(t,dyt);
end
else
begin
rline(xc-x, yc-y, width,index,mode);
if (y<>0) then rline(xc-x, yc+y, width,index,mode);
inc(x);
inc(dxt,d2xt);
inc(t,dxt);
dec(y);
inc(dyt,d2yt);
inc(t,dyt);
inc(width,2);
end;
end;
if (b = 0) then rline(xc-a, yc, 2*a+1,index,mode);
end;
procedure rRectFill(x,y,w,h,index,mode : integer);
var
i,j : integer;
x2,y2 : integer;
t : integer;
begin
x2:=x+w;
y2:=y+h-1;
if x2 < x then
begin
t:=x2;
x2:=x;
x:=t;
end;
if y2 < y then
begin
t:=y2;
y2:=y;
y:=t;
end;
for j:=y to y2 do
begin
for i:=x to x2 do
begin
PutItemX(i,j,index,mode);
end;
end;
end;
Procedure Ellipse(xc,yc,x2,y2,index,full, mode : integer);
var
rx,ry : integer;
begin
rx:=abs(x2-xc);
if rx < 1 then rx:=1;
ry:=abs(y2-yc);
if ry < 1 then ry:=1;
if full=1 then
begin
filled_ellipse(xc,yc,rx,ry,index,mode);
end
else
begin
empty_ellipse(xc,yc,rx,ry,index,mode);
end;
end;
Procedure Circle(xc,yc,x2,y2,index,full, mode : integer);
var
rx,ry,radius : integer;
begin
rx:=abs(x2-xc);
ry:=abs(y2-yc);
radius:=rx;
if ry > radius then radius:=ry;
if radius < 1 then radius:=1;
if full=1 then
begin
filled_ellipse(xc,yc,radius,radius,index,mode);
end
else
begin
empty_ellipse(xc,yc,radius,radius,index,mode);
end;
end;
procedure dummyDrawPixelProc(x,y,index,mode : integer);
begin
//this should be overwriten to support what ever target we want. plot pixel to screen or memory or something else
end;
function dummyGetPixelProc(x,y : integer) : integer;
begin
//this should be overwriten to support what ever target we want. plot pixel to screen or memory or something else
result:=0;
end;
procedure SetDrawPixelProc(dic : drawitemproc);
begin
DefaultDrawItemProc:=dic;
end;
procedure SetGetPixelProc(gic : getitemproc);
begin
DefaultGetItemProc:=gic;
end;
procedure DrawItem(item,x,y,x2,y2,index,mode : integer);
begin
case item of DrawShapePencil:PutItemX(x,y,index,mode);
DrawShapeLine:Line(x,y,x2,y2,index,mode);
DrawShapeRectangle:Rectangle(x,y,x2,y2,index,0,mode);
DrawShapeFRectangle:Rectangle(x,y,x2,y2,index,1,mode);
DrawShapeCircle:Circle(x,y,x2,y2,index,0,mode);
DrawShapeFCircle:Circle(x,y,x2,y2,index,1,mode);
DrawShapeEllipse:Ellipse(x,y,x2,y2,index,0,mode);
DrawShapeFEllipse:Ellipse(x,y,x2,y2,index,1,mode);
DrawShapeSpray:SprayPaint(x,y,index,mode);
end;
end;
procedure InitDrawPixelProc;
begin
SetDrawPixelProc(@dummyDrawPixelProc);
SetGetPixelProc(@dummyGetPixelProc);
end;
begin
InitDrawPixelProc;
end.