-
-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathdraw.c
3326 lines (3018 loc) · 115 KB
/
draw.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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
pygame-ce - Python Game Library
Copyright (C) 2000-2001 Pete Shinners
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Pete Shinners
pete@shinners.org
*/
/*
* drawing module for pygame
*/
#include "pygame.h"
#include "pgcompat.h"
#include "doc/draw_doc.h"
#include <math.h>
#include <float.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/* Declaration of drawing algorithms */
static void
draw_line_width(SDL_Surface *surf, Uint32 color, int x1, int y1, int x2,
int y2, int width, int *drawn_area);
static void
draw_line(SDL_Surface *surf, int x1, int y1, int x2, int y2, Uint32 color,
int *drawn_area);
void
line_width_corners(float from_x, float from_y, float to_x, float to_y,
int width, float *x1, float *y1, float *x2, float *y2,
float *x3, float *y3, float *x4, float *y4);
static void
draw_aaline(SDL_Surface *surf, Uint32 color, float startx, float starty,
float endx, float endy, int *drawn_area,
int disable_first_endpoint, int disable_second_endpoint,
int extra_pixel_for_aalines);
static void
draw_arc(SDL_Surface *surf, int x_center, int y_center, int radius1,
int radius2, int width, double angle_start, double angle_stop,
Uint32 color, int *drawn_area);
static void
draw_circle_bresenham(SDL_Surface *surf, int x0, int y0, int radius,
int thickness, Uint32 color, int *drawn_area);
static void
draw_circle_bresenham_thin(SDL_Surface *surf, int x0, int y0, int radius,
Uint32 color, int *drawn_area);
static void
draw_circle_xiaolinwu(SDL_Surface *surf, int x0, int y0, int radius,
int thickness, Uint32 color, int top_right, int top_left,
int bottom_left, int bottom_right, int *drawn_area);
static void
draw_circle_xiaolinwu_thin(SDL_Surface *surf, int x0, int y0, int radius,
Uint32 color, int top_right, int top_left,
int bottom_left, int bottom_right, int *drawn_area);
static void
draw_circle_filled(SDL_Surface *surf, int x0, int y0, int radius, Uint32 color,
int *drawn_area);
static void
draw_circle_quadrant(SDL_Surface *surf, int x0, int y0, int radius,
int thickness, Uint32 color, int top_right, int top_left,
int bottom_left, int bottom_right, int *drawn_area);
static void
draw_ellipse_filled(SDL_Surface *surf, int x0, int y0, int width, int height,
Uint32 color, int *drawn_area);
static void
draw_ellipse_thickness(SDL_Surface *surf, int x0, int y0, int width,
int height, int thickness, Uint32 color,
int *drawn_area);
static void
draw_fillpoly(SDL_Surface *surf, int *vx, int *vy, Py_ssize_t n, Uint32 color,
int *drawn_area);
static int
draw_filltri(SDL_Surface *surf, int *xlist, int *ylist, Uint32 color,
int *drawn_area);
static void
draw_rect(SDL_Surface *surf, int x1, int y1, int x2, int y2, int width,
Uint32 color);
static void
draw_round_rect(SDL_Surface *surf, int x1, int y1, int x2, int y2, int radius,
int width, Uint32 color, int top_left, int top_right,
int bottom_left, int bottom_right, int *drawn_area);
// validation of a draw color
#define CHECK_LOAD_COLOR(colorobj) \
if (!pg_MappedColorFromObj((colorobj), surf, &color, \
PG_COLOR_HANDLE_ALL)) { \
return NULL; \
}
/* Definition of functions that get called in Python */
/* Draws an antialiased line on the given surface.
*
* Returns a Rect bounding the drawn area.
*/
static PyObject *
aaline(PyObject *self, PyObject *arg, PyObject *kwargs)
{
pgSurfaceObject *surfobj;
PyObject *colorobj, *start, *end;
SDL_Surface *surf = NULL;
float startx, starty, endx, endy;
int width = 1; /* Default width. */
PyObject *blend = NULL;
int drawn_area[4] = {INT_MAX, INT_MAX, INT_MIN,
INT_MIN}; /* Used to store bounding box values */
Uint32 color;
static char *keywords[] = {"surface", "color", "start_pos", "end_pos",
"width", "blend", NULL};
if (!PyArg_ParseTupleAndKeywords(arg, kwargs, "O!OOO|iO", keywords,
&pgSurface_Type, &surfobj, &colorobj,
&start, &end, &width, &blend)) {
return NULL; /* Exception already set. */
}
if (blend != NULL) {
if (PyErr_WarnEx(
PyExc_DeprecationWarning,
"blend argument is deprecated and has no functionality and "
"will be completely removed in a future version of pygame-ce",
1) == -1) {
return NULL;
}
}
surf = pgSurface_AsSurface(surfobj);
SURF_INIT_CHECK(surf)
if (PG_SURF_BytesPerPixel(surf) <= 0 || PG_SURF_BytesPerPixel(surf) > 4) {
return PyErr_Format(PyExc_ValueError,
"unsupported surface bit depth (%d) for drawing",
PG_SURF_BytesPerPixel(surf));
}
CHECK_LOAD_COLOR(colorobj)
if (!pg_TwoFloatsFromObj(start, &startx, &starty)) {
return RAISE(PyExc_TypeError, "invalid start_pos argument");
}
if (!pg_TwoFloatsFromObj(end, &endx, &endy)) {
return RAISE(PyExc_TypeError, "invalid end_pos argument");
}
if (width < 1) {
return pgRect_New4((int)startx, (int)starty, 0, 0);
}
if (!pgSurface_Lock(surfobj)) {
return RAISE(PyExc_RuntimeError, "error locking surface");
}
if (width > 1) {
float x1, y1, x2, y2, x3, y3, x4, y4;
line_width_corners(startx, starty, endx, endy, width, &x1, &y1, &x2,
&y2, &x3, &y3, &x4, &y4);
draw_line_width(surf, color, (int)startx, (int)starty, (int)endx,
(int)endy, width, drawn_area);
draw_aaline(surf, color, x1, y1, x2, y2, drawn_area, 0, 0, 0);
draw_aaline(surf, color, x3, y3, x4, y4, drawn_area, 0, 0, 0);
}
else {
draw_aaline(surf, color, startx, starty, endx, endy, drawn_area, 0, 0,
0);
}
if (!pgSurface_Unlock(surfobj)) {
return RAISE(PyExc_RuntimeError, "error unlocking surface");
}
if (drawn_area[0] != INT_MAX && drawn_area[1] != INT_MAX &&
drawn_area[2] != INT_MIN && drawn_area[3] != INT_MIN)
return pgRect_New4(drawn_area[0], drawn_area[1],
drawn_area[2] - drawn_area[0] + 1,
drawn_area[3] - drawn_area[1] + 1);
else
return pgRect_New4((int)startx, (int)starty, 0, 0);
}
/* Draws a line on the given surface.
*
* Returns a Rect bounding the drawn area.
*/
static PyObject *
line(PyObject *self, PyObject *arg, PyObject *kwargs)
{
pgSurfaceObject *surfobj;
PyObject *colorobj, *start, *end;
SDL_Surface *surf = NULL;
int startx, starty, endx, endy;
Uint32 color;
int width = 1; /* Default width. */
int drawn_area[4] = {INT_MAX, INT_MAX, INT_MIN,
INT_MIN}; /* Used to store bounding box values */
static char *keywords[] = {"surface", "color", "start_pos",
"end_pos", "width", NULL};
if (!PyArg_ParseTupleAndKeywords(arg, kwargs, "O!OOO|i", keywords,
&pgSurface_Type, &surfobj, &colorobj,
&start, &end, &width)) {
return NULL; /* Exception already set. */
}
surf = pgSurface_AsSurface(surfobj);
SURF_INIT_CHECK(surf)
if (PG_SURF_BytesPerPixel(surf) <= 0 || PG_SURF_BytesPerPixel(surf) > 4) {
return PyErr_Format(PyExc_ValueError,
"unsupported surface bit depth (%d) for drawing",
PG_SURF_BytesPerPixel(surf));
}
CHECK_LOAD_COLOR(colorobj)
if (!pg_TwoIntsFromObj(start, &startx, &starty)) {
return RAISE(PyExc_TypeError, "invalid start_pos argument");
}
if (!pg_TwoIntsFromObj(end, &endx, &endy)) {
return RAISE(PyExc_TypeError, "invalid end_pos argument");
}
if (width < 1) {
return pgRect_New4(startx, starty, 0, 0);
}
if (!pgSurface_Lock(surfobj)) {
return RAISE(PyExc_RuntimeError, "error locking surface");
}
draw_line_width(surf, color, startx, starty, endx, endy, width,
drawn_area);
if (!pgSurface_Unlock(surfobj)) {
return RAISE(PyExc_RuntimeError, "error unlocking surface");
}
/* Compute return rect. */
if (drawn_area[0] != INT_MAX && drawn_area[1] != INT_MAX &&
drawn_area[2] != INT_MIN && drawn_area[3] != INT_MIN)
return pgRect_New4(drawn_area[0], drawn_area[1],
drawn_area[2] - drawn_area[0] + 1,
drawn_area[3] - drawn_area[1] + 1);
else
return pgRect_New4(startx, starty, 0, 0);
}
/* Draws a series of antialiased lines on the given surface.
*
* Returns a Rect bounding the drawn area.
*/
static PyObject *
aalines(PyObject *self, PyObject *arg, PyObject *kwargs)
{
pgSurfaceObject *surfobj;
PyObject *colorobj;
PyObject *points, *item = NULL;
SDL_Surface *surf = NULL;
Uint32 color;
float pts[4];
float pts_prev[4];
float *xlist, *ylist;
float x, y;
int l, t;
int extra_px;
int disable_endpoints;
int steep_prev;
int steep_curr;
PyObject *blend = NULL;
int drawn_area[4] = {INT_MAX, INT_MAX, INT_MIN,
INT_MIN}; /* Used to store bounding box values */
int result, closed;
Py_ssize_t loop, length;
static char *keywords[] = {"surface", "color", "closed",
"points", "blend", NULL};
if (!PyArg_ParseTupleAndKeywords(arg, kwargs, "O!OpO|O", keywords,
&pgSurface_Type, &surfobj, &colorobj,
&closed, &points, &blend)) {
return NULL; /* Exception already set. */
}
if (blend != NULL) {
if (PyErr_WarnEx(
PyExc_DeprecationWarning,
"blend argument is deprecated and has no functionality and "
"will be completely removed in a future version of pygame-ce",
1) == -1) {
return NULL;
}
}
surf = pgSurface_AsSurface(surfobj);
SURF_INIT_CHECK(surf)
if (PG_SURF_BytesPerPixel(surf) <= 0 || PG_SURF_BytesPerPixel(surf) > 4) {
return PyErr_Format(PyExc_ValueError,
"unsupported surface bit depth (%d) for drawing",
PG_SURF_BytesPerPixel(surf));
}
CHECK_LOAD_COLOR(colorobj)
if (!PySequence_Check(points)) {
return RAISE(PyExc_TypeError,
"points argument must be a sequence of number pairs");
}
length = PySequence_Length(points);
if (length < 2) {
return RAISE(PyExc_ValueError,
"points argument must contain 2 or more points");
}
xlist = PyMem_New(float, length);
ylist = PyMem_New(float, length);
if (NULL == xlist || NULL == ylist) {
if (xlist) {
PyMem_Free(xlist);
}
if (ylist) {
PyMem_Free(ylist);
}
return RAISE(PyExc_MemoryError,
"cannot allocate memory to draw aalines");
}
for (loop = 0; loop < length; ++loop) {
item = PySequence_GetItem(points, loop);
result = pg_TwoFloatsFromObj(item, &x, &y);
if (loop == 0) {
l = (int)x;
t = (int)y;
}
Py_DECREF(item);
if (!result) {
PyMem_Free(xlist);
PyMem_Free(ylist);
return RAISE(PyExc_TypeError, "points must be number pairs");
}
xlist[loop] = x;
ylist[loop] = y;
}
if (!pgSurface_Lock(surfobj)) {
PyMem_Free(xlist);
PyMem_Free(ylist);
return RAISE(PyExc_RuntimeError, "error locking surface");
}
/* first line - if open, add endpoint pixels.*/
pts[0] = xlist[0];
pts[1] = ylist[0];
pts[2] = xlist[1];
pts[3] = ylist[1];
/* Previous points.
* Used to compare previous and current line.*/
pts_prev[0] = pts[0];
pts_prev[1] = pts[1];
pts_prev[2] = pts[2];
pts_prev[3] = pts[3];
steep_prev =
fabs(pts_prev[2] - pts_prev[0]) < fabs(pts_prev[3] - pts_prev[1]);
steep_curr = fabs(xlist[2] - pts[2]) < fabs(ylist[2] - pts[1]);
extra_px = steep_prev > steep_curr;
disable_endpoints =
!((roundf(pts[2]) == pts[2]) && (roundf(pts[3]) == pts[3]));
if (closed) {
draw_aaline(surf, color, pts[0], pts[1], pts[2], pts[3], drawn_area,
disable_endpoints, disable_endpoints, extra_px);
}
else {
draw_aaline(surf, color, pts[0], pts[1], pts[2], pts[3], drawn_area, 0,
disable_endpoints, extra_px);
}
for (loop = 2; loop < length - 1; ++loop) {
pts[0] = xlist[loop - 1];
pts[1] = ylist[loop - 1];
pts[2] = xlist[loop];
pts[3] = ylist[loop];
/* Comparing previous and current line.
* If one is steep and other is not, extra pixel must be drawn.*/
steep_prev =
fabs(pts_prev[2] - pts_prev[0]) < fabs(pts_prev[3] - pts_prev[1]);
steep_curr = fabs(pts[2] - pts[0]) < fabs(pts[3] - pts[1]);
extra_px = steep_prev != steep_curr;
disable_endpoints =
!((roundf(pts[2]) == pts[2]) && (roundf(pts[3]) == pts[3]));
pts_prev[0] = pts[0];
pts_prev[1] = pts[1];
pts_prev[2] = pts[2];
pts_prev[3] = pts[3];
draw_aaline(surf, color, pts[0], pts[1], pts[2], pts[3], drawn_area,
disable_endpoints, disable_endpoints, extra_px);
}
/* Last line - if open, add endpoint pixels. */
pts[0] = xlist[length - 2];
pts[1] = ylist[length - 2];
pts[2] = xlist[length - 1];
pts[3] = ylist[length - 1];
steep_prev =
fabs(pts_prev[2] - pts_prev[0]) < fabs(pts_prev[3] - pts_prev[1]);
steep_curr = fabs(pts[2] - pts[0]) < fabs(pts[3] - pts[1]);
extra_px = steep_prev != steep_curr;
disable_endpoints =
!((roundf(pts[2]) == pts[2]) && (roundf(pts[3]) == pts[3]));
pts_prev[0] = pts[0];
pts_prev[1] = pts[1];
pts_prev[2] = pts[2];
pts_prev[3] = pts[3];
if (closed) {
draw_aaline(surf, color, pts[0], pts[1], pts[2], pts[3], drawn_area,
disable_endpoints, disable_endpoints, extra_px);
}
else {
draw_aaline(surf, color, pts[0], pts[1], pts[2], pts[3], drawn_area,
disable_endpoints, 0, extra_px);
}
if (closed && length > 2) {
pts[0] = xlist[length - 1];
pts[1] = ylist[length - 1];
pts[2] = xlist[0];
pts[3] = ylist[0];
steep_prev =
fabs(pts_prev[2] - pts_prev[0]) < fabs(pts_prev[3] - pts_prev[1]);
steep_curr = fabs(pts[2] - pts[0]) < fabs(pts[3] - pts[1]);
extra_px = steep_prev != steep_curr;
disable_endpoints =
!((roundf(pts[2]) == pts[2]) && (roundf(pts[3]) == pts[3]));
draw_aaline(surf, color, pts[0], pts[1], pts[2], pts[3], drawn_area,
disable_endpoints, disable_endpoints, extra_px);
}
PyMem_Free(xlist);
PyMem_Free(ylist);
if (!pgSurface_Unlock(surfobj)) {
return RAISE(PyExc_RuntimeError, "error unlocking surface");
}
/* Compute return rect. */
if (drawn_area[0] != INT_MAX && drawn_area[1] != INT_MAX &&
drawn_area[2] != INT_MIN && drawn_area[3] != INT_MIN)
return pgRect_New4(drawn_area[0], drawn_area[1],
drawn_area[2] - drawn_area[0] + 1,
drawn_area[3] - drawn_area[1] + 1);
else
return pgRect_New4(l, t, 0, 0);
}
/* Draws a series of lines on the given surface.
*
* Returns a Rect bounding the drawn area.
*/
static PyObject *
lines(PyObject *self, PyObject *arg, PyObject *kwargs)
{
pgSurfaceObject *surfobj;
PyObject *colorobj;
PyObject *points, *item = NULL;
SDL_Surface *surf = NULL;
Uint32 color;
int x, y, closed, result;
int *xlist = NULL, *ylist = NULL;
int width = 1; /* Default width. */
Py_ssize_t loop, length;
int drawn_area[4] = {INT_MAX, INT_MAX, INT_MIN,
INT_MIN}; /* Used to store bounding box values */
static char *keywords[] = {"surface", "color", "closed",
"points", "width", NULL};
if (!PyArg_ParseTupleAndKeywords(arg, kwargs, "O!OpO|i", keywords,
&pgSurface_Type, &surfobj, &colorobj,
&closed, &points, &width)) {
return NULL; /* Exception already set. */
}
surf = pgSurface_AsSurface(surfobj);
SURF_INIT_CHECK(surf)
if (PG_SURF_BytesPerPixel(surf) <= 0 || PG_SURF_BytesPerPixel(surf) > 4) {
return PyErr_Format(PyExc_ValueError,
"unsupported surface bit depth (%d) for drawing",
PG_SURF_BytesPerPixel(surf));
}
CHECK_LOAD_COLOR(colorobj)
if (!PySequence_Check(points)) {
return RAISE(PyExc_TypeError,
"points argument must be a sequence of number pairs");
}
length = PySequence_Length(points);
if (length < 2) {
return RAISE(PyExc_ValueError,
"points argument must contain 2 or more points");
}
xlist = PyMem_New(int, length);
ylist = PyMem_New(int, length);
if (NULL == xlist || NULL == ylist) {
if (xlist) {
PyMem_Free(xlist);
}
if (ylist) {
PyMem_Free(ylist);
}
return RAISE(PyExc_MemoryError,
"cannot allocate memory to draw lines");
}
for (loop = 0; loop < length; ++loop) {
item = PySequence_GetItem(points, loop);
result = pg_TwoIntsFromObj(item, &x, &y);
Py_DECREF(item);
if (!result) {
PyMem_Free(xlist);
PyMem_Free(ylist);
return RAISE(PyExc_TypeError, "points must be number pairs");
}
xlist[loop] = x;
ylist[loop] = y;
}
x = xlist[0];
y = ylist[0];
if (width < 1) {
PyMem_Free(xlist);
PyMem_Free(ylist);
return pgRect_New4(x, y, 0, 0);
}
if (!pgSurface_Lock(surfobj)) {
PyMem_Free(xlist);
PyMem_Free(ylist);
return RAISE(PyExc_RuntimeError, "error locking surface");
}
for (loop = 1; loop < length; ++loop) {
draw_line_width(surf, color, xlist[loop - 1], ylist[loop - 1],
xlist[loop], ylist[loop], width, drawn_area);
}
if (closed && length > 2) {
draw_line_width(surf, color, xlist[length - 1], ylist[length - 1],
xlist[0], ylist[0], width, drawn_area);
}
PyMem_Free(xlist);
PyMem_Free(ylist);
if (!pgSurface_Unlock(surfobj)) {
return RAISE(PyExc_RuntimeError, "error unlocking surface");
}
/* Compute return rect. */
if (drawn_area[0] != INT_MAX && drawn_area[1] != INT_MAX &&
drawn_area[2] != INT_MIN && drawn_area[3] != INT_MIN)
return pgRect_New4(drawn_area[0], drawn_area[1],
drawn_area[2] - drawn_area[0] + 1,
drawn_area[3] - drawn_area[1] + 1);
else
return pgRect_New4(x, y, 0, 0);
}
static PyObject *
arc(PyObject *self, PyObject *arg, PyObject *kwargs)
{
pgSurfaceObject *surfobj;
PyObject *colorobj, *rectobj;
SDL_Rect *rect = NULL, temp;
SDL_Surface *surf = NULL;
Uint32 color;
int width = 1; /* Default width. */
int drawn_area[4] = {INT_MAX, INT_MAX, INT_MIN,
INT_MIN}; /* Used to store bounding box values */
double angle_start, angle_stop;
static char *keywords[] = {"surface", "color", "rect", "start_angle",
"stop_angle", "width", NULL};
if (!PyArg_ParseTupleAndKeywords(
arg, kwargs, "O!OOdd|i", keywords, &pgSurface_Type, &surfobj,
&colorobj, &rectobj, &angle_start, &angle_stop, &width)) {
return NULL; /* Exception already set. */
}
rect = pgRect_FromObject(rectobj, &temp);
if (!rect) {
return RAISE(PyExc_TypeError, "rect argument is invalid");
}
surf = pgSurface_AsSurface(surfobj);
SURF_INIT_CHECK(surf)
if (PG_SURF_BytesPerPixel(surf) <= 0 || PG_SURF_BytesPerPixel(surf) > 4) {
return PyErr_Format(PyExc_ValueError,
"unsupported surface bit depth (%d) for drawing",
PG_SURF_BytesPerPixel(surf));
}
CHECK_LOAD_COLOR(colorobj)
if (width < 0) {
return pgRect_New4(rect->x, rect->y, 0, 0);
}
if (width > rect->w / 2 || width > rect->h / 2) {
width = MAX(rect->w / 2, rect->h / 2);
}
if (angle_stop < angle_start) {
// Angle is in radians
angle_stop += 2 * M_PI;
}
if (!pgSurface_Lock(surfobj)) {
return RAISE(PyExc_RuntimeError, "error locking surface");
}
width = MIN(width, MIN(rect->w, rect->h) / 2);
draw_arc(surf, rect->x + rect->w / 2, rect->y + rect->h / 2, rect->w / 2,
rect->h / 2, width, angle_start, angle_stop, color, drawn_area);
if (!pgSurface_Unlock(surfobj)) {
return RAISE(PyExc_RuntimeError, "error unlocking surface");
}
/* Compute return rect. */
if (drawn_area[0] != INT_MAX && drawn_area[1] != INT_MAX &&
drawn_area[2] != INT_MIN && drawn_area[3] != INT_MIN)
return pgRect_New4(drawn_area[0], drawn_area[1],
drawn_area[2] - drawn_area[0] + 1,
drawn_area[3] - drawn_area[1] + 1);
else
return pgRect_New4(rect->x, rect->y, 0, 0);
}
static PyObject *
ellipse(PyObject *self, PyObject *arg, PyObject *kwargs)
{
pgSurfaceObject *surfobj;
PyObject *colorobj, *rectobj;
SDL_Rect *rect = NULL, temp;
SDL_Surface *surf = NULL;
Uint32 color;
int width = 0; /* Default width. */
int drawn_area[4] = {INT_MAX, INT_MAX, INT_MIN,
INT_MIN}; /* Used to store bounding box values */
static char *keywords[] = {"surface", "color", "rect", "width", NULL};
if (!PyArg_ParseTupleAndKeywords(arg, kwargs, "O!OO|i", keywords,
&pgSurface_Type, &surfobj, &colorobj,
&rectobj, &width)) {
return NULL; /* Exception already set. */
}
rect = pgRect_FromObject(rectobj, &temp);
if (!rect) {
return RAISE(PyExc_TypeError, "rect argument is invalid");
}
surf = pgSurface_AsSurface(surfobj);
SURF_INIT_CHECK(surf)
if (PG_SURF_BytesPerPixel(surf) <= 0 || PG_SURF_BytesPerPixel(surf) > 4) {
return PyErr_Format(PyExc_ValueError,
"unsupported surface bit depth (%d) for drawing",
PG_SURF_BytesPerPixel(surf));
}
CHECK_LOAD_COLOR(colorobj)
if (width < 0) {
return pgRect_New4(rect->x, rect->y, 0, 0);
}
if (!pgSurface_Lock(surfobj)) {
return RAISE(PyExc_RuntimeError, "error locking surface");
}
if (!width ||
width >= MIN(rect->w / 2 + rect->w % 2, rect->h / 2 + rect->h % 2)) {
draw_ellipse_filled(surf, rect->x, rect->y, rect->w, rect->h, color,
drawn_area);
}
else {
draw_ellipse_thickness(surf, rect->x, rect->y, rect->w, rect->h,
width - 1, color, drawn_area);
}
if (!pgSurface_Unlock(surfobj)) {
return RAISE(PyExc_RuntimeError, "error unlocking surface");
}
if (drawn_area[0] != INT_MAX && drawn_area[1] != INT_MAX &&
drawn_area[2] != INT_MIN && drawn_area[3] != INT_MIN)
return pgRect_New4(drawn_area[0], drawn_area[1],
drawn_area[2] - drawn_area[0] + 1,
drawn_area[3] - drawn_area[1] + 1);
else
return pgRect_New4(rect->x, rect->y, 0, 0);
}
static PyObject *
circle(PyObject *self, PyObject *args, PyObject *kwargs)
{
pgSurfaceObject *surfobj;
PyObject *colorobj;
SDL_Surface *surf = NULL;
Uint32 color;
SDL_Rect cliprect;
PyObject *posobj, *radiusobj;
int posx, posy, radius;
int width = 0; /* Default values. */
int top_right = 0, top_left = 0, bottom_left = 0, bottom_right = 0;
int drawn_area[4] = {INT_MAX, INT_MAX, INT_MIN,
INT_MIN}; /* Used to store bounding box values */
static char *keywords[] = {"surface",
"color",
"center",
"radius",
"width",
"draw_top_right",
"draw_top_left",
"draw_bottom_left",
"draw_bottom_right",
NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!OOO|iiiii", keywords,
&pgSurface_Type, &surfobj, &colorobj,
&posobj, &radiusobj, &width, &top_right,
&top_left, &bottom_left, &bottom_right))
return NULL; /* Exception already set. */
if (!pg_TwoIntsFromObj(posobj, &posx, &posy)) {
PyErr_SetString(PyExc_TypeError,
"center argument must be a pair of numbers");
return 0;
}
if (!pg_IntFromObj(radiusobj, &radius)) {
PyErr_SetString(PyExc_TypeError, "radius argument must be a number");
return 0;
}
surf = pgSurface_AsSurface(surfobj);
SURF_INIT_CHECK(surf)
if (PG_SURF_BytesPerPixel(surf) <= 0 || PG_SURF_BytesPerPixel(surf) > 4) {
return PyErr_Format(PyExc_ValueError,
"unsupported surface bit depth (%d) for drawing",
PG_SURF_BytesPerPixel(surf));
}
CHECK_LOAD_COLOR(colorobj)
if (radius < 1 || width < 0) {
return pgRect_New4(posx, posy, 0, 0);
}
if (width > radius) {
width = radius;
}
SDL_GetClipRect(surf, &cliprect);
if (posx > cliprect.x + cliprect.w + radius ||
posx < cliprect.x - radius ||
posy > cliprect.y + cliprect.h + radius ||
posy < cliprect.y - radius) {
return pgRect_New4(posx, posy, 0, 0);
}
if (!pgSurface_Lock(surfobj)) {
return RAISE(PyExc_RuntimeError, "error locking surface");
}
if ((top_right == 0 && top_left == 0 && bottom_left == 0 &&
bottom_right == 0)) {
if (!width || width == radius) {
draw_circle_filled(surf, posx, posy, radius, color, drawn_area);
}
else if (width == 1) {
draw_circle_bresenham_thin(surf, posx, posy, radius, color,
drawn_area);
}
else {
draw_circle_bresenham(surf, posx, posy, radius, width, color,
drawn_area);
}
}
else {
draw_circle_quadrant(surf, posx, posy, radius, width, color, top_right,
top_left, bottom_left, bottom_right, drawn_area);
}
if (!pgSurface_Unlock(surfobj)) {
return RAISE(PyExc_RuntimeError, "error unlocking surface");
}
if (drawn_area[0] != INT_MAX && drawn_area[1] != INT_MAX &&
drawn_area[2] != INT_MIN && drawn_area[3] != INT_MIN)
return pgRect_New4(drawn_area[0], drawn_area[1],
drawn_area[2] - drawn_area[0] + 1,
drawn_area[3] - drawn_area[1] + 1);
else
return pgRect_New4(posx, posy, 0, 0);
}
static PyObject *
aacircle(PyObject *self, PyObject *args, PyObject *kwargs)
{
pgSurfaceObject *surfobj;
PyObject *colorobj;
SDL_Surface *surf = NULL;
Uint32 color;
SDL_Rect cliprect;
PyObject *posobj, *radiusobj;
int posx, posy, radius;
int width = 0; /* Default values. */
int top_right = 0, top_left = 0, bottom_left = 0, bottom_right = 0;
int drawn_area[4] = {INT_MAX, INT_MAX, INT_MIN,
INT_MIN}; /* Used to store bounding box values */
static char *keywords[] = {"surface",
"color",
"center",
"radius",
"width",
"draw_top_right",
"draw_top_left",
"draw_bottom_left",
"draw_bottom_right",
NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!OOO|iiiii", keywords,
&pgSurface_Type, &surfobj, &colorobj,
&posobj, &radiusobj, &width, &top_right,
&top_left, &bottom_left, &bottom_right))
return NULL; /* Exception already set. */
if (!pg_TwoIntsFromObj(posobj, &posx, &posy)) {
return RAISE(PyExc_TypeError,
"center argument must be a pair of numbers");
}
if (!pg_IntFromObj(radiusobj, &radius)) {
return RAISE(PyExc_TypeError, "radius argument must be a number");
}
surf = pgSurface_AsSurface(surfobj);
SURF_INIT_CHECK(surf)
if (PG_SURF_BytesPerPixel(surf) <= 0 || PG_SURF_BytesPerPixel(surf) > 4) {
return PyErr_Format(PyExc_ValueError,
"unsupported surface bit depth (%d) for drawing",
PG_SURF_BytesPerPixel(surf));
}
CHECK_LOAD_COLOR(colorobj)
if (radius < 1 || width < 0) {
return pgRect_New4(posx, posy, 0, 0);
}
if (width > radius) {
width = radius;
}
SDL_GetClipRect(surf, &cliprect);
if (posx > cliprect.x + cliprect.w + radius ||
posx < cliprect.x - radius ||
posy > cliprect.y + cliprect.h + radius ||
posy < cliprect.y - radius) {
return pgRect_New4(posx, posy, 0, 0);
}
if (!pgSurface_Lock(surfobj)) {
return RAISE(PyExc_RuntimeError, "error locking surface");
}
if ((top_right == 0 && top_left == 0 && bottom_left == 0 &&
bottom_right == 0)) {
if (!width || width == radius) {
draw_circle_filled(surf, posx, posy, radius - 1, color,
drawn_area);
draw_circle_xiaolinwu(surf, posx, posy, radius, 2, color, 1, 1, 1,
1, drawn_area);
}
else if (width == 1) {
draw_circle_xiaolinwu_thin(surf, posx, posy, radius, color, 1, 1,
1, 1, drawn_area);
}
else {
draw_circle_xiaolinwu(surf, posx, posy, radius, width, color, 1, 1,
1, 1, drawn_area);
}
}
else {
if (!width || width == radius) {
draw_circle_xiaolinwu(surf, posx, posy, radius, radius, color,
top_right, top_left, bottom_left,
bottom_right, drawn_area);
}
else if (width == 1) {
draw_circle_xiaolinwu_thin(surf, posx, posy, radius, color,
top_right, top_left, bottom_left,
bottom_right, drawn_area);
}
else {
draw_circle_xiaolinwu(surf, posx, posy, radius, width, color,
top_right, top_left, bottom_left,
bottom_right, drawn_area);
}
}
if (!pgSurface_Unlock(surfobj)) {
return RAISE(PyExc_RuntimeError, "error unlocking surface");
}
if (drawn_area[0] != INT_MAX && drawn_area[1] != INT_MAX &&
drawn_area[2] != INT_MIN && drawn_area[3] != INT_MIN)
return pgRect_New4(drawn_area[0], drawn_area[1],
drawn_area[2] - drawn_area[0] + 1,
drawn_area[3] - drawn_area[1] + 1);
else
return pgRect_New4(posx, posy, 0, 0);
}
static PyObject *
polygon(PyObject *self, PyObject *arg, PyObject *kwargs)
{
pgSurfaceObject *surfobj;
PyObject *colorobj, *points, *item = NULL;
SDL_Surface *surf = NULL;
Uint32 color;
int *xlist = NULL, *ylist = NULL;
int width = 0; /* Default width. */
int x, y, result, l, t;
int drawn_area[4] = {INT_MAX, INT_MAX, INT_MIN,
INT_MIN}; /* Used to store bounding box values */
Py_ssize_t loop, length;
static char *keywords[] = {"surface", "color", "points", "width", NULL};
if (!PyArg_ParseTupleAndKeywords(arg, kwargs, "O!OO|i", keywords,
&pgSurface_Type, &surfobj, &colorobj,
&points, &width)) {
return NULL; /* Exception already set. */
}
if (width) {
PyObject *ret = NULL;
PyObject *args =
Py_BuildValue("(OOiOi)", surfobj, colorobj, 1, points, width);
if (!args) {
return NULL; /* Exception already set. */
}
ret = lines(NULL, args, NULL);
Py_DECREF(args);
return ret;
}