annotate src/draw.c @ 457:c7745825439b

trunc fix
author pygame
date Mon, 12 May 2003 14:54:13 +0000
parents ad9e57cfa846
children 4c398dd9d49b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1 /*
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
2 pygame - Python Game Library
107
cbb297365480 ready for 0.9
pygame
parents: 99
diff changeset
3 Copyright (C) 2000-2001 Pete Shinners
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
4
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
6 modify it under the terms of the GNU Library General Public
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
7 License as published by the Free Software Foundation; either
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
8 version 2 of the License, or (at your option) any later version.
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
9
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
13 Library General Public License for more details.
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
14
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
15 You should have received a copy of the GNU Library General Public
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
16 License along with this library; if not, write to the Free
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
18
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
19 Pete Shinners
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
20 pete@shinners.org
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
21 */
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
22
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
23 /*
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
24 * drawing module for pygame
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
25 */
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
26 #include "pygame.h"
442
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
27 #include <math.h>
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
28
457
c7745825439b trunc fix
pygame
parents: 442
diff changeset
29 #ifdef _MSC_VER
c7745825439b trunc fix
pygame
parents: 442
diff changeset
30 #pragma warning (disable:4244)
c7745825439b trunc fix
pygame
parents: 442
diff changeset
31
c7745825439b trunc fix
pygame
parents: 442
diff changeset
32 float trunc(float d)
c7745825439b trunc fix
pygame
parents: 442
diff changeset
33 {
c7745825439b trunc fix
pygame
parents: 442
diff changeset
34 if (d >= 0)
c7745825439b trunc fix
pygame
parents: 442
diff changeset
35 return floor(d);
c7745825439b trunc fix
pygame
parents: 442
diff changeset
36 return ceil(d);
c7745825439b trunc fix
pygame
parents: 442
diff changeset
37 }
c7745825439b trunc fix
pygame
parents: 442
diff changeset
38
c7745825439b trunc fix
pygame
parents: 442
diff changeset
39 #endif
c7745825439b trunc fix
pygame
parents: 442
diff changeset
40
442
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
41 #define FRAC(z) (z-trunc(z))
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
42 #define INVFRAC(z) (1-(z-trunc(z)))
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
43
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
44 static int clip_and_draw_line(SDL_Surface* surf, SDL_Rect* rect, Uint32 color, int* pts);
442
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
45 static int clip_and_draw_aaline(SDL_Surface* surf, SDL_Rect* rect, Uint32 color, float* pts, int blend);
176
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
46 static int clip_and_draw_line_width(SDL_Surface* surf, SDL_Rect* rect, Uint32 color, int width, int* pts);
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
47 static int clipline(int* pts, int left, int top, int right, int bottom);
442
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
48 static int clipaaline(float* pts, int left, int top, int right, int bottom);
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
49 static void drawline(SDL_Surface* surf, Uint32 color, int startx, int starty, int endx, int endy);
442
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
50 static void drawaaline(SDL_Surface* surf, Uint32 color, float startx, float starty, float endx, float endy,
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
51 int blend);
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
52 static void drawhorzline(SDL_Surface* surf, Uint32 color, int startx, int starty, int endx);
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
53 static void drawvertline(SDL_Surface* surf, Uint32 color, int x1, int y1, int y2);
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
54 static void draw_ellipse(SDL_Surface *dst, int x, int y, int rx, int ry, Uint32 color);
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
55 static void draw_fillellipse(SDL_Surface *dst, int x, int y, int rx, int ry, Uint32 color);
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
56 static void draw_fillpoly(SDL_Surface *dst, int *vx, int *vy, int n, Uint32 color);
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
57
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
58
442
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
59 /*DOC*/ static char doc_aaline[] =
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
60 /*DOC*/ "pygame.draw.aaline(Surface, color, startpos, endpos, blend=1) -> Rect\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
61 /*DOC*/ "draw a line on a surface\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
62 /*DOC*/ "\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
63 /*DOC*/ "Draws an anti-aliased line on a surface. This will respect the\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
64 /*DOC*/ "clipping rectangle. A bounding box of the effected area is returned\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
65 /*DOC*/ "returned as a rectangle. The if blend is true, the shades will be\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
66 /*DOC*/ "be blended with existing pixel shades instead of overwriting them.\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
67 /*DOC*/ "This function accepts floating point values for the end points.\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
68 /*DOC*/ "\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
69 /*DOC*/ "The color argument must be a RGB sequence.\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
70 /*DOC*/ "\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
71 /*DOC*/ "This function will temporarily lock the surface.\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
72 /*DOC*/ ;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
73
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
74 static PyObject* aaline(PyObject* self, PyObject* arg)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
75 {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
76 PyObject *surfobj, *colorobj, *start, *end;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
77 SDL_Surface* surf;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
78 float startx, starty, endx, endy;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
79 int top, left, bottom, right;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
80 int blend=1;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
81 float pts[4];
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
82 Uint8 rgba[4];
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
83 Uint32 color;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
84 int anydraw;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
85
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
86 /*get all the arguments*/
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
87 if(!PyArg_ParseTuple(arg, "O!OOO|i", &PySurface_Type, &surfobj, &colorobj, &start, &end, &blend))
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
88 return NULL;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
89 surf = PySurface_AsSurface(surfobj);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
90
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
91 if(surf->format->BytesPerPixel !=3 && surf->format->BytesPerPixel != 4)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
92 return RAISE(PyExc_ValueError, "unsupported bit depth for aaline draw (supports 32 & 24 bit)");
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
93
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
94 if(RGBAFromObj(colorobj, rgba))
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
95 color = SDL_MapRGBA(surf->format, rgba[0], rgba[1], rgba[2], rgba[3]);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
96 else
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
97 return RAISE(PyExc_TypeError, "invalid color argument");
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
98
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
99 if(!TwoFloatsFromObj(start, &startx, &starty))
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
100 return RAISE(PyExc_TypeError, "Invalid start position argument");
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
101 if(!TwoFloatsFromObj(end, &endx, &endy))
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
102 return RAISE(PyExc_TypeError, "Invalid end position argument");
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
103
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
104 if(!PySurface_Lock(surfobj)) return NULL;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
105
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
106 pts[0] = startx; pts[1] = starty;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
107 pts[2] = endx; pts[3] = endy;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
108 anydraw = clip_and_draw_aaline(surf, &surf->clip_rect, color, pts, blend);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
109
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
110 if(!PySurface_Unlock(surfobj)) return NULL;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
111
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
112 /*compute return rect*/
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
113 if(!anydraw)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
114 return PyRect_New4((short)(startx), (short)(starty), 0, 0);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
115 if(pts[0] < pts[2])
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
116 {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
117 left = (int)(pts[0]);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
118 right = (int)(pts[2]);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
119 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
120 else
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
121 {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
122 left = (int)(pts[2]);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
123 right = (int)(pts[0]);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
124 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
125 if(pts[1] < pts[3])
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
126 {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
127 top = (int)(pts[1]);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
128 bottom = (int)(pts[3]);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
129 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
130 else
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
131 {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
132 top = (int)(pts[3]);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
133 bottom = (int)(pts[1]);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
134 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
135 return PyRect_New4((short)left, (short)top, (short)(right-left+2), (short)(bottom-top+2));
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
136 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
137
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
138 /*DOC*/ static char doc_line[] =
177
2ad237361ff3 line widths
pygame
parents: 176
diff changeset
139 /*DOC*/ "pygame.draw.line(Surface, color, startpos, endpos, width=1) -> Rect\n"
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
140 /*DOC*/ "draw a line on a surface\n"
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
141 /*DOC*/ "\n"
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
142 /*DOC*/ "Draws a line on a surface. This will respect the clipping\n"
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
143 /*DOC*/ "rectangle. A bounding box of the effected area is returned\n"
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
144 /*DOC*/ "as a rectangle.\n"
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
145 /*DOC*/ "\n"
96
c9113776fe5e colors can be mapped ints
pygame
parents: 93
diff changeset
146 /*DOC*/ "The color argument can be either a RGB sequence or mapped color integer.\n"
c9113776fe5e colors can be mapped ints
pygame
parents: 93
diff changeset
147 /*DOC*/ "\n"
92
44239866fe40 locking doc updates
pygame
parents: 90
diff changeset
148 /*DOC*/ "This function will temporarily lock the surface.\n"
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
149 /*DOC*/ ;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
150
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
151 static PyObject* line(PyObject* self, PyObject* arg)
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
152 {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
153 PyObject *surfobj, *colorobj, *start, *end;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
154 SDL_Surface* surf;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
155 short startx, starty, endx, endy;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
156 int top, left, bottom, right;
176
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
157 int width = 1;
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
158 int pts[4];
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
159 Uint8 rgba[4];
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
160 Uint32 color;
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
161 int anydraw;
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
162
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
163 /*get all the arguments*/
176
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
164 if(!PyArg_ParseTuple(arg, "O!OOO|i", &PySurface_Type, &surfobj, &colorobj, &start, &end, &width))
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
165 return NULL;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
166 surf = PySurface_AsSurface(surfobj);
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
167
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
168 if(surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4)
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
169 return RAISE(PyExc_ValueError, "unsupport bit depth for line draw");
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
170
96
c9113776fe5e colors can be mapped ints
pygame
parents: 93
diff changeset
171 if(PyInt_Check(colorobj))
c9113776fe5e colors can be mapped ints
pygame
parents: 93
diff changeset
172 color = (Uint32)PyInt_AsLong(colorobj);
c9113776fe5e colors can be mapped ints
pygame
parents: 93
diff changeset
173 else if(RGBAFromObj(colorobj, rgba))
c9113776fe5e colors can be mapped ints
pygame
parents: 93
diff changeset
174 color = SDL_MapRGBA(surf->format, rgba[0], rgba[1], rgba[2], rgba[3]);
c9113776fe5e colors can be mapped ints
pygame
parents: 93
diff changeset
175 else
c9113776fe5e colors can be mapped ints
pygame
parents: 93
diff changeset
176 return RAISE(PyExc_TypeError, "invalid color argument");
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
177
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
178 if(!TwoShortsFromObj(start, &startx, &starty))
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
179 return RAISE(PyExc_TypeError, "Invalid start position argument");
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
180 if(!TwoShortsFromObj(end, &endx, &endy))
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
181 return RAISE(PyExc_TypeError, "Invalid end position argument");
176
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
182
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
183 if(width < 1)
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
184 return PyRect_New4(startx, starty, 0, 0);
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
185
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
186
90
1f713299efcd major locking enhancements. Surface.subsurface added
pygame
parents: 87
diff changeset
187 if(!PySurface_Lock(surfobj)) return NULL;
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
188
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
189 pts[0] = startx; pts[1] = starty;
77aae459b436 doc updates
pygame
parents: 92
diff changeset
190 pts[2] = endx; pts[3] = endy;
176
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
191 anydraw = clip_and_draw_line_width(surf, &surf->clip_rect, color, width, pts);
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
192
90
1f713299efcd major locking enhancements. Surface.subsurface added
pygame
parents: 87
diff changeset
193 if(!PySurface_Unlock(surfobj)) return NULL;
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
194
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
195
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
196 /*compute return rect*/
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
197 if(!anydraw)
77aae459b436 doc updates
pygame
parents: 92
diff changeset
198 return PyRect_New4(startx, starty, 0, 0);
77aae459b436 doc updates
pygame
parents: 92
diff changeset
199 if(pts[0] < pts[2])
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
200 {
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
201 left = pts[0];
77aae459b436 doc updates
pygame
parents: 92
diff changeset
202 right = pts[2];
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
203 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
204 else
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
205 {
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
206 left = pts[2];
77aae459b436 doc updates
pygame
parents: 92
diff changeset
207 right = pts[0];
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
208 }
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
209 if(pts[1] < pts[3])
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
210 {
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
211 top = pts[1];
77aae459b436 doc updates
pygame
parents: 92
diff changeset
212 bottom = pts[3];
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
213 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
214 else
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
215 {
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
216 top = pts[3];
77aae459b436 doc updates
pygame
parents: 92
diff changeset
217 bottom = pts[1];
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
218 }
110
544b3e6cd502 line drawing cleanups
pygame
parents: 107
diff changeset
219 return PyRect_New4((short)left, (short)top, (short)(right-left+1), (short)(bottom-top+1));
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
220 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
221
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
222
442
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
223 /*DOC*/ static char doc_aalines[] =
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
224 /*DOC*/ "pygame.draw.aalines(Surface, color, closed, point_array, blend=1) -> Rect\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
225 /*DOC*/ "draw multiple connected anti-aliased lines on a surface\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
226 /*DOC*/ "\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
227 /*DOC*/ "Draws a sequence on a surface. You must pass at least two points\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
228 /*DOC*/ "in the sequence of points. The closed argument is a simple boolean\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
229 /*DOC*/ "and if true, a line will be draw between the first and last points.\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
230 /*DOC*/ "The boolean blend argument set to true will blend the shades with\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
231 /*DOC*/ "existing shades instead of overwriting them.\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
232 /*DOC*/ "This function accepts floating point values for the end points.\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
233 /*DOC*/ "\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
234 /*DOC*/ "This will respect the clipping rectangle. A bounding box of the\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
235 /*DOC*/ "effected area is returned as a rectangle.\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
236 /*DOC*/ "\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
237 /*DOC*/ "The color argument must be an RGB sequence.\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
238 /*DOC*/ "\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
239 /*DOC*/ "This function will temporarily lock the surface.\n"
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
240 /*DOC*/ ;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
241
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
242 static PyObject* aalines(PyObject* self, PyObject* arg)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
243 {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
244 PyObject *surfobj, *colorobj, *closedobj, *points, *item;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
245 SDL_Surface* surf;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
246 float x, y;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
247 int top, left, bottom, right;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
248 float pts[4];
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
249 Uint8 rgba[4];
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
250 Uint32 color;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
251 int closed, blend;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
252 int result, loop, length, drawn;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
253 float startx, starty;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
254
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
255 /*get all the arguments*/
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
256 if(!PyArg_ParseTuple(arg, "O!OOO|i", &PySurface_Type, &surfobj, &colorobj, &closedobj,
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
257 &points, &blend))
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
258 return NULL;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
259 surf = PySurface_AsSurface(surfobj);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
260
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
261 if(surf->format->BytesPerPixel !=3 && surf->format->BytesPerPixel != 4)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
262 return RAISE(PyExc_ValueError, "unsupported bit depth for aaline draw (supports 32 & 24 bit)");
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
263
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
264 if(RGBAFromObj(colorobj, rgba))
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
265 color = SDL_MapRGBA(surf->format, rgba[0], rgba[1], rgba[2], rgba[3]);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
266 else
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
267 return RAISE(PyExc_TypeError, "invalid color argument");
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
268
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
269 closed = PyObject_IsTrue(closedobj);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
270
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
271 if(!PySequence_Check(points))
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
272 return RAISE(PyExc_TypeError, "points argument must be a sequence of number pairs");
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
273 length = PySequence_Length(points);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
274 if(length < 2)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
275 return RAISE(PyExc_ValueError, "points argument must contain more than 1 points");
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
276
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
277 item = PySequence_GetItem(points, 0);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
278 result = TwoFloatsFromObj(item, &x, &y);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
279 Py_DECREF(item);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
280 if(!result) return RAISE(PyExc_TypeError, "points must be number pairs");
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
281
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
282 startx = pts[0] = x;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
283 starty = pts[1] = y;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
284 left = right = (int)x;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
285 top = bottom = (int)y;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
286
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
287 if(!PySurface_Lock(surfobj)) return NULL;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
288
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
289 drawn = 1;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
290 for(loop = 1; loop < length; ++loop)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
291 {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
292 item = PySequence_GetItem(points, loop);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
293 result = TwoFloatsFromObj(item, &x, &y);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
294 Py_DECREF(item);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
295 if(!result) continue; /*note, we silently skip over bad points :[ */
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
296 ++drawn;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
297 pts[0] = startx;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
298 pts[1] = starty;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
299 startx = pts[2] = x;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
300 starty = pts[3] = y;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
301 if(clip_and_draw_aaline(surf, &surf->clip_rect, color, pts, blend))
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
302 {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
303 left = min((int)min(pts[0], pts[2]), left);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
304 top = min((int)min(pts[1], pts[3]), top);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
305 right = max((int)max(pts[0], pts[2]), right);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
306 bottom = max((int)max(pts[1], pts[3]), bottom);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
307 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
308 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
309 if(closed && drawn > 2)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
310 {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
311 item = PySequence_GetItem(points, 0);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
312 result = TwoFloatsFromObj(item, &x, &y);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
313 Py_DECREF(item);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
314 if(result)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
315 {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
316 pts[0] = startx;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
317 pts[1] = starty;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
318 pts[2] = x;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
319 pts[3] = y;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
320 clip_and_draw_aaline(surf, &surf->clip_rect, color, pts, blend);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
321 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
322 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
323
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
324 if(!PySurface_Unlock(surfobj)) return NULL;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
325
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
326 /*compute return rect*/
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
327 return PyRect_New4((short)left, (short)top, (short)(right-left+2), (short)(bottom-top+2));
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
328 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
329
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
330 /*DOC*/ static char doc_lines[] =
176
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
331 /*DOC*/ "pygame.draw.lines(Surface, color, closed, point_array, width=1) -> Rect\n"
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
332 /*DOC*/ "draw multiple connected lines on a surface\n"
77aae459b436 doc updates
pygame
parents: 92
diff changeset
333 /*DOC*/ "\n"
77aae459b436 doc updates
pygame
parents: 92
diff changeset
334 /*DOC*/ "Draws a sequence on a surface. You must pass at least two points\n"
77aae459b436 doc updates
pygame
parents: 92
diff changeset
335 /*DOC*/ "in the sequence of points. The closed argument is a simple boolean\n"
77aae459b436 doc updates
pygame
parents: 92
diff changeset
336 /*DOC*/ "and if true, a line will be draw between the first and last points.\n"
176
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
337 /*DOC*/ "Note that specifying a linewidth wider than 1 does not fill in the\n"
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
338 /*DOC*/ "gaps between the lines. Therefore wide lines and sharp corners won't\n"
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
339 /*DOC*/ "be joined seamlessly.\n"
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
340 /*DOC*/ "\n"
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
341 /*DOC*/ "This will respect the clipping rectangle. A bounding box of the\n"
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
342 /*DOC*/ "effected area is returned as a rectangle.\n"
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
343 /*DOC*/ "\n"
96
c9113776fe5e colors can be mapped ints
pygame
parents: 93
diff changeset
344 /*DOC*/ "The color argument can be either a RGB sequence or mapped color integer.\n"
c9113776fe5e colors can be mapped ints
pygame
parents: 93
diff changeset
345 /*DOC*/ "\n"
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
346 /*DOC*/ "This function will temporarily lock the surface.\n"
77aae459b436 doc updates
pygame
parents: 92
diff changeset
347 /*DOC*/ ;
77aae459b436 doc updates
pygame
parents: 92
diff changeset
348
77aae459b436 doc updates
pygame
parents: 92
diff changeset
349 static PyObject* lines(PyObject* self, PyObject* arg)
77aae459b436 doc updates
pygame
parents: 92
diff changeset
350 {
77aae459b436 doc updates
pygame
parents: 92
diff changeset
351 PyObject *surfobj, *colorobj, *closedobj, *points, *item;
77aae459b436 doc updates
pygame
parents: 92
diff changeset
352 SDL_Surface* surf;
77aae459b436 doc updates
pygame
parents: 92
diff changeset
353 short x, y;
77aae459b436 doc updates
pygame
parents: 92
diff changeset
354 int top, left, bottom, right;
176
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
355 int pts[4], width=1;
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
356 Uint8 rgba[4];
77aae459b436 doc updates
pygame
parents: 92
diff changeset
357 Uint32 color;
77aae459b436 doc updates
pygame
parents: 92
diff changeset
358 int closed;
77aae459b436 doc updates
pygame
parents: 92
diff changeset
359 int result, loop, length, drawn;
186
fa74e65cc266 line drawing fixups
pygame
parents: 177
diff changeset
360 int startx, starty;
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
361
77aae459b436 doc updates
pygame
parents: 92
diff changeset
362 /*get all the arguments*/
176
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
363 if(!PyArg_ParseTuple(arg, "O!OOO|i", &PySurface_Type, &surfobj, &colorobj, &closedobj, &points, &width))
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
364 return NULL;
77aae459b436 doc updates
pygame
parents: 92
diff changeset
365 surf = PySurface_AsSurface(surfobj);
77aae459b436 doc updates
pygame
parents: 92
diff changeset
366
77aae459b436 doc updates
pygame
parents: 92
diff changeset
367 if(surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4)
77aae459b436 doc updates
pygame
parents: 92
diff changeset
368 return RAISE(PyExc_ValueError, "unsupport bit depth for line draw");
77aae459b436 doc updates
pygame
parents: 92
diff changeset
369
96
c9113776fe5e colors can be mapped ints
pygame
parents: 93
diff changeset
370 if(PyInt_Check(colorobj))
c9113776fe5e colors can be mapped ints
pygame
parents: 93
diff changeset
371 color = (Uint32)PyInt_AsLong(colorobj);
c9113776fe5e colors can be mapped ints
pygame
parents: 93
diff changeset
372 else if(RGBAFromObj(colorobj, rgba))
c9113776fe5e colors can be mapped ints
pygame
parents: 93
diff changeset
373 color = SDL_MapRGBA(surf->format, rgba[0], rgba[1], rgba[2], rgba[3]);
c9113776fe5e colors can be mapped ints
pygame
parents: 93
diff changeset
374 else
c9113776fe5e colors can be mapped ints
pygame
parents: 93
diff changeset
375 return RAISE(PyExc_TypeError, "invalid color argument");
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
376
77aae459b436 doc updates
pygame
parents: 92
diff changeset
377 closed = PyObject_IsTrue(closedobj);
77aae459b436 doc updates
pygame
parents: 92
diff changeset
378
77aae459b436 doc updates
pygame
parents: 92
diff changeset
379 if(!PySequence_Check(points))
77aae459b436 doc updates
pygame
parents: 92
diff changeset
380 return RAISE(PyExc_TypeError, "points argument must be a sequence of number pairs");
77aae459b436 doc updates
pygame
parents: 92
diff changeset
381 length = PySequence_Length(points);
77aae459b436 doc updates
pygame
parents: 92
diff changeset
382 if(length < 2)
77aae459b436 doc updates
pygame
parents: 92
diff changeset
383 return RAISE(PyExc_ValueError, "points argument must contain more than 1 points");
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
384
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
385 item = PySequence_GetItem(points, 0);
77aae459b436 doc updates
pygame
parents: 92
diff changeset
386 result = TwoShortsFromObj(item, &x, &y);
77aae459b436 doc updates
pygame
parents: 92
diff changeset
387 Py_DECREF(item);
77aae459b436 doc updates
pygame
parents: 92
diff changeset
388 if(!result) return RAISE(PyExc_TypeError, "points must be number pairs");
77aae459b436 doc updates
pygame
parents: 92
diff changeset
389
186
fa74e65cc266 line drawing fixups
pygame
parents: 177
diff changeset
390 startx = pts[0] = left = right = x;
fa74e65cc266 line drawing fixups
pygame
parents: 177
diff changeset
391 starty = pts[1] = top = bottom = y;
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
392
176
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
393 if(width < 1)
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
394 return PyRect_New4((short)left, (short)top, 0, 0);
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
395
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
396 if(!PySurface_Lock(surfobj)) return NULL;
77aae459b436 doc updates
pygame
parents: 92
diff changeset
397
77aae459b436 doc updates
pygame
parents: 92
diff changeset
398 drawn = 1;
77aae459b436 doc updates
pygame
parents: 92
diff changeset
399 for(loop = 1; loop < length; ++loop)
77aae459b436 doc updates
pygame
parents: 92
diff changeset
400 {
77aae459b436 doc updates
pygame
parents: 92
diff changeset
401 item = PySequence_GetItem(points, loop);
77aae459b436 doc updates
pygame
parents: 92
diff changeset
402 result = TwoShortsFromObj(item, &x, &y);
77aae459b436 doc updates
pygame
parents: 92
diff changeset
403 Py_DECREF(item);
77aae459b436 doc updates
pygame
parents: 92
diff changeset
404 if(!result) continue; /*note, we silently skip over bad points :[ */
77aae459b436 doc updates
pygame
parents: 92
diff changeset
405 ++drawn;
186
fa74e65cc266 line drawing fixups
pygame
parents: 177
diff changeset
406 pts[0] = startx;
fa74e65cc266 line drawing fixups
pygame
parents: 177
diff changeset
407 pts[1] = starty;
fa74e65cc266 line drawing fixups
pygame
parents: 177
diff changeset
408 startx = pts[2] = x;
fa74e65cc266 line drawing fixups
pygame
parents: 177
diff changeset
409 starty = pts[3] = y;
176
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
410 if(clip_and_draw_line_width(surf, &surf->clip_rect, color, width, pts))
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
411 {
77aae459b436 doc updates
pygame
parents: 92
diff changeset
412 left = min(min(pts[0], pts[2]), left);
77aae459b436 doc updates
pygame
parents: 92
diff changeset
413 top = min(min(pts[1], pts[3]), top);
77aae459b436 doc updates
pygame
parents: 92
diff changeset
414 right = max(max(pts[0], pts[2]), right);
77aae459b436 doc updates
pygame
parents: 92
diff changeset
415 bottom = max(max(pts[1], pts[3]), bottom);
77aae459b436 doc updates
pygame
parents: 92
diff changeset
416 }
77aae459b436 doc updates
pygame
parents: 92
diff changeset
417 }
77aae459b436 doc updates
pygame
parents: 92
diff changeset
418 if(closed && drawn > 2)
77aae459b436 doc updates
pygame
parents: 92
diff changeset
419 {
77aae459b436 doc updates
pygame
parents: 92
diff changeset
420 item = PySequence_GetItem(points, 0);
77aae459b436 doc updates
pygame
parents: 92
diff changeset
421 result = TwoShortsFromObj(item, &x, &y);
77aae459b436 doc updates
pygame
parents: 92
diff changeset
422 Py_DECREF(item);
77aae459b436 doc updates
pygame
parents: 92
diff changeset
423 if(result)
77aae459b436 doc updates
pygame
parents: 92
diff changeset
424 {
186
fa74e65cc266 line drawing fixups
pygame
parents: 177
diff changeset
425 pts[0] = startx;
fa74e65cc266 line drawing fixups
pygame
parents: 177
diff changeset
426 pts[1] = starty;
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
427 pts[2] = x;
77aae459b436 doc updates
pygame
parents: 92
diff changeset
428 pts[3] = y;
176
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
429 clip_and_draw_line_width(surf, &surf->clip_rect, color, width, pts);
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
430 }
77aae459b436 doc updates
pygame
parents: 92
diff changeset
431 }
77aae459b436 doc updates
pygame
parents: 92
diff changeset
432
77aae459b436 doc updates
pygame
parents: 92
diff changeset
433
77aae459b436 doc updates
pygame
parents: 92
diff changeset
434 if(!PySurface_Unlock(surfobj)) return NULL;
77aae459b436 doc updates
pygame
parents: 92
diff changeset
435
77aae459b436 doc updates
pygame
parents: 92
diff changeset
436 /*compute return rect*/
110
544b3e6cd502 line drawing cleanups
pygame
parents: 107
diff changeset
437 return PyRect_New4((short)left, (short)top, (short)(right-left+1), (short)(bottom-top+1));
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
438 }
77aae459b436 doc updates
pygame
parents: 92
diff changeset
439
77aae459b436 doc updates
pygame
parents: 92
diff changeset
440
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
441 /*DOC*/ static char doc_ellipse[] =
349
c4d9d1425143 draw primitives have default width
pygame
parents: 339
diff changeset
442 /*DOC*/ "pygame.draw.ellipse(Surface, color, Rect, width=0) -> Rect\n"
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
443 /*DOC*/ "draw an ellipse on a surface\n"
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
444 /*DOC*/ "\n"
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
445 /*DOC*/ "Draws an elliptical shape on the Surface. The given rectangle\n"
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
446 /*DOC*/ "is the area that the circle will fill. The width argument is\n"
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
447 /*DOC*/ "the thickness to draw the outer edge. If width is zero then\n"
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
448 /*DOC*/ "the ellipse will be filled.\n"
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
449 /*DOC*/ "\n"
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
450 /*DOC*/ "The color argument can be either a RGB sequence or mapped color integer.\n"
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
451 /*DOC*/ "\n"
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
452 /*DOC*/ "This function will temporarily lock the surface.\n"
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
453 /*DOC*/ ;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
454
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
455 static PyObject* ellipse(PyObject* self, PyObject* arg)
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
456 {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
457 PyObject *surfobj, *colorobj, *rectobj;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
458 GAME_Rect *rect, temp;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
459 SDL_Surface* surf;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
460 Uint8 rgba[4];
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
461 Uint32 color;
349
c4d9d1425143 draw primitives have default width
pygame
parents: 339
diff changeset
462 int width=0, loop, t, l, b, r;
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
463
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
464 /*get all the arguments*/
349
c4d9d1425143 draw primitives have default width
pygame
parents: 339
diff changeset
465 if(!PyArg_ParseTuple(arg, "O!OO|i", &PySurface_Type, &surfobj, &colorobj, &rectobj, &width))
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
466 return NULL;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
467 rect = GameRect_FromObject(rectobj, &temp);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
468 if(!rect)
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
469 return RAISE(PyExc_TypeError, "Invalid recstyle argument");
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
470
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
471 surf = PySurface_AsSurface(surfobj);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
472 if(surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4)
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
473 return RAISE(PyExc_ValueError, "unsupport bit depth for drawing");
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
474
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
475 if(PyInt_Check(colorobj))
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
476 color = (Uint32)PyInt_AsLong(colorobj);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
477 else if(RGBAFromObj(colorobj, rgba))
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
478 color = SDL_MapRGBA(surf->format, rgba[0], rgba[1], rgba[2], rgba[3]);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
479 else
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
480 return RAISE(PyExc_TypeError, "invalid color argument");
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
481
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
482 if ( width < 0 )
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
483 return RAISE(PyExc_ValueError, "negative width");
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
484 if ( width > rect->w / 2 || width > rect->h / 2 )
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
485 return RAISE(PyExc_ValueError, "width greater than ellipse radius");
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
486
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
487 if(!PySurface_Lock(surfobj)) return NULL;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
488
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
489 if(!width)
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
490 draw_fillellipse(surf, (Sint16)(rect->x+rect->w/2), (Sint16)(rect->y+rect->h/2),
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
491 (Sint16)(rect->w/2), (Sint16)(rect->h/2), color);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
492 else
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
493 {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
494 width = min(width, min(rect->w, rect->h) / 2);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
495 for(loop=0; loop<width; ++loop)
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
496 {
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
497 draw_ellipse(surf, rect->x+rect->w/2, rect->y+rect->h/2,
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
498 rect->w/2-loop, rect->h/2-loop, color);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
499 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
500 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
501
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
502 if(!PySurface_Unlock(surfobj)) return NULL;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
503
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
504 l = max(rect->x, surf->clip_rect.x);
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
505 t = max(rect->y, surf->clip_rect.y);
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
506 r = min(rect->x + rect->w, surf->clip_rect.x + surf->clip_rect.w);
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
507 b = min(rect->y + rect->h, surf->clip_rect.y + surf->clip_rect.h);
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
508 return PyRect_New4((short)l, (short)t, (short)max(r-l, 0), (short)max(b-t, 0));
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
509 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
510
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
511
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
512
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
513 /*DOC*/ static char doc_circle[] =
349
c4d9d1425143 draw primitives have default width
pygame
parents: 339
diff changeset
514 /*DOC*/ "pygame.draw.circle(Surface, color, pos, radius, width=0) -> Rect\n"
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
515 /*DOC*/ "draw a circle on a surface\n"
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
516 /*DOC*/ "\n"
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
517 /*DOC*/ "Draws a circular shape on the Surface. The given position\n"
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
518 /*DOC*/ "is the center of the circle, and radius is the size. The width\n"
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
519 /*DOC*/ "argument is the thickness to draw the outer edge. If width is\n"
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
520 /*DOC*/ "zero then the circle will be filled.\n"
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
521 /*DOC*/ "\n"
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
522 /*DOC*/ "The color argument can be either a RGB sequence or mapped color integer.\n"
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
523 /*DOC*/ "\n"
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
524 /*DOC*/ "This function will temporarily lock the surface.\n"
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
525 /*DOC*/ ;
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
526
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
527 static PyObject* circle(PyObject* self, PyObject* arg)
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
528 {
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
529 PyObject *surfobj, *colorobj;
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
530 SDL_Surface* surf;
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
531 Uint8 rgba[4];
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
532 Uint32 color;
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
533 int posx, posy, radius, t, l, b, r;
349
c4d9d1425143 draw primitives have default width
pygame
parents: 339
diff changeset
534 int width=0, loop;
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
535
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
536 /*get all the arguments*/
349
c4d9d1425143 draw primitives have default width
pygame
parents: 339
diff changeset
537 if(!PyArg_ParseTuple(arg, "O!O(ii)i|i", &PySurface_Type, &surfobj, &colorobj, &posx, &posy, &radius, &width))
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
538 return NULL;
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
539
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
540 surf = PySurface_AsSurface(surfobj);
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
541 if(surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4)
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
542 return RAISE(PyExc_ValueError, "unsupport bit depth for drawing");
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
543
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
544 if(PyInt_Check(colorobj))
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
545 color = (Uint32)PyInt_AsLong(colorobj);
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
546 else if(RGBAFromObj(colorobj, rgba))
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
547 color = SDL_MapRGBA(surf->format, rgba[0], rgba[1], rgba[2], rgba[3]);
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
548 else
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
549 return RAISE(PyExc_TypeError, "invalid color argument");
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
550
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
551 if ( radius < 0 )
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
552 return RAISE(PyExc_ValueError, "negative radius");
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
553 if ( width < 0 )
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
554 return RAISE(PyExc_ValueError, "negative width");
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
555 if ( width > radius )
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
556 return RAISE(PyExc_ValueError, "width greater than radius");
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
557
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
558
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
559 if(!PySurface_Lock(surfobj)) return NULL;
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
560
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
561 if(!width)
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
562 draw_fillellipse(surf, (Sint16)posx, (Sint16)posy, (Sint16)radius, (Sint16)radius, color);
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
563 else
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
564 for(loop=0; loop<width; ++loop)
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
565 draw_ellipse(surf, posx, posy, radius-loop, radius-loop, color);
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
566
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
567 if(!PySurface_Unlock(surfobj)) return NULL;
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
568
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
569 l = max(posx - radius, surf->clip_rect.x);
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
570 t = max(posy - radius, surf->clip_rect.y);
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
571 r = min(posx + radius, surf->clip_rect.x + surf->clip_rect.w);
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
572 b = min(posy + radius, surf->clip_rect.y + surf->clip_rect.h);
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
573 return PyRect_New4((short)l, (short)t, (short)max(r-l, 0), (short)max(b-t, 0));
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
574 }
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
575
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
576
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
577
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
578
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
579 /*DOC*/ static char doc_polygon[] =
349
c4d9d1425143 draw primitives have default width
pygame
parents: 339
diff changeset
580 /*DOC*/ "pygame.draw.polygon(Surface, color, pointslist, width=0) -> Rect\n"
226
73b3cda42d93 draw module gets boosted
pygame
parents: 225
diff changeset
581 /*DOC*/ "draws a polygon on a surface\n"
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
582 /*DOC*/ "\n"
226
73b3cda42d93 draw module gets boosted
pygame
parents: 225
diff changeset
583 /*DOC*/ "Draws a polygonal shape on the Surface. The given pointlist\n"
73b3cda42d93 draw module gets boosted
pygame
parents: 225
diff changeset
584 /*DOC*/ "is the vertices of the polygon. The width argument is\n"
73b3cda42d93 draw module gets boosted
pygame
parents: 225
diff changeset
585 /*DOC*/ "the thickness to draw the outer edge. If width is zero then\n"
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
586 /*DOC*/ "the polygon will be filled.\n"
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
587 /*DOC*/ "\n"
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
588 /*DOC*/ "The color argument can be either a RGB sequence or mapped color integer.\n"
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
589 /*DOC*/ "\n"
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
590 /*DOC*/ "This function will temporarily lock the surface.\n"
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
591 /*DOC*/ ;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
592
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
593 static PyObject* polygon(PyObject* self, PyObject* arg)
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
594 {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
595 PyObject *surfobj, *colorobj, *points, *item;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
596 SDL_Surface* surf;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
597 Uint8 rgba[4];
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
598 Uint32 color;
349
c4d9d1425143 draw primitives have default width
pygame
parents: 339
diff changeset
599 int width=0, length, loop, numpoints;
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
600 int *xlist, *ylist;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
601 short x, y, top, left, bottom, right, result;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
602
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
603 /*get all the arguments*/
349
c4d9d1425143 draw primitives have default width
pygame
parents: 339
diff changeset
604 if(!PyArg_ParseTuple(arg, "O!OO|i", &PySurface_Type, &surfobj, &colorobj, &points, &width))
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
605 return NULL;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
606
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
607
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
608 if(width)
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
609 {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
610 PyObject *args, *ret;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
611 args = Py_BuildValue("(OOiOi)", surfobj, colorobj, 1, points, width);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
612 if(!args) return NULL;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
613 ret = lines(NULL, args);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
614 Py_DECREF(args);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
615 return ret;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
616 }
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
617
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
618
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
619 surf = PySurface_AsSurface(surfobj);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
620
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
621 if(surf->format->BytesPerPixel <= 0 || surf->format->BytesPerPixel > 4)
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
622 return RAISE(PyExc_ValueError, "unsupport bit depth for line draw");
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
623
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
624 if(PyInt_Check(colorobj))
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
625 color = (Uint32)PyInt_AsLong(colorobj);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
626 else if(RGBAFromObj(colorobj, rgba))
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
627 color = SDL_MapRGBA(surf->format, rgba[0], rgba[1], rgba[2], rgba[3]);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
628 else
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
629 return RAISE(PyExc_TypeError, "invalid color argument");
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
630
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
631 if(!PySequence_Check(points))
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
632 return RAISE(PyExc_TypeError, "points argument must be a sequence of number pairs");
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
633 length = PySequence_Length(points);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
634 if(length < 3)
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
635 return RAISE(PyExc_ValueError, "points argument must contain more than 2 points");
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
636
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
637
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
638 item = PySequence_GetItem(points, 0);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
639 result = TwoShortsFromObj(item, &x, &y);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
640 Py_DECREF(item);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
641 if(!result) return RAISE(PyExc_TypeError, "points must be number pairs");
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
642 left = right = x;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
643 top = bottom = y;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
644
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
645 xlist = PyMem_New(int, length);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
646 ylist = PyMem_New(int, length);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
647
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
648 numpoints = 0;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
649 for(loop = 0; loop < length; ++loop)
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
650 {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
651 item = PySequence_GetItem(points, loop);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
652 result = TwoShortsFromObj(item, &x, &y);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
653 Py_DECREF(item);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
654 if(!result) continue; /*note, we silently skip over bad points :[ */
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
655 xlist[numpoints] = x;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
656 ylist[numpoints] = y;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
657 ++numpoints;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
658 left = min(x, left);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
659 top = min(y, top);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
660 right = max(x, right);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
661 bottom = max(y, bottom);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
662 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
663
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
664 if(!PySurface_Lock(surfobj))
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
665 {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
666 PyMem_Del(xlist); PyMem_Del(ylist);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
667 return NULL;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
668 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
669
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
670 draw_fillpoly(surf, xlist, ylist, numpoints, color);
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
671
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
672 PyMem_Del(xlist); PyMem_Del(ylist);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
673 if(!PySurface_Unlock(surfobj))
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
674 return NULL;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
675
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
676 left = max(left, surf->clip_rect.x);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
677 top = max(top, surf->clip_rect.y);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
678 right = min(right, surf->clip_rect.x + surf->clip_rect.w);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
679 bottom = min(bottom, surf->clip_rect.y + surf->clip_rect.h);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
680 return PyRect_New4(left, top, (short)(right-left+1), (short)(bottom-top+1));
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
681 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
682
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
683
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
684 /*DOC*/ static char doc_rect[] =
349
c4d9d1425143 draw primitives have default width
pygame
parents: 339
diff changeset
685 /*DOC*/ "pygame.draw.rect(Surface, color, Rect, width=0) -> Rect\n"
317
0758ee161bd5 transform.rotate fixes
pygame
parents: 260
diff changeset
686 /*DOC*/ "draws a rectangle on a surface\n"
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
687 /*DOC*/ "\n"
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
688 /*DOC*/ "Draws a rectangular shape on the Surface. The given Rect\n"
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
689 /*DOC*/ "is the area of the rectangle. The width argument is\n"
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
690 /*DOC*/ "the thickness to draw the outer edge. If width is zero then\n"
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
691 /*DOC*/ "the rectangle will be filled.\n"
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
692 /*DOC*/ "\n"
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
693 /*DOC*/ "The color argument can be either a RGB sequence or mapped color integer.\n"
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
694 /*DOC*/ "\n"
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
695 /*DOC*/ "This function will temporarily lock the surface.\n"
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
696 /*DOC*/ "\n"
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
697 /*DOC*/ "Keep in mind the Surface.fill() method works just as well\n"
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
698 /*DOC*/ "for drawing filled rectangles. In fact the Surface.fill()\n"
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
699 /*DOC*/ "can be hardware accelerated when the moons are in alignement.\n"
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
700 /*DOC*/ ;
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
701
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
702 static PyObject* rect(PyObject* self, PyObject* arg)
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
703 {
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
704 PyObject *surfobj, *colorobj, *rectobj, *points, *args, *ret=NULL;
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
705 GAME_Rect* rect, temp;
349
c4d9d1425143 draw primitives have default width
pygame
parents: 339
diff changeset
706 int t, l, b, r, width=0;
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
707
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
708 /*get all the arguments*/
349
c4d9d1425143 draw primitives have default width
pygame
parents: 339
diff changeset
709 if(!PyArg_ParseTuple(arg, "O!OO|i", &PySurface_Type, &surfobj, &colorobj, &rectobj, &width))
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
710 return NULL;
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
711
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
712 if(!(rect = GameRect_FromObject(rectobj, &temp)))
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
713 return RAISE(PyExc_TypeError, "Rect argument is invalid");
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
714
339
e03ae1679a27 no more debug printing
pygame
parents: 317
diff changeset
715 l = rect->x; r = rect->x + rect->w - 1;
e03ae1679a27 no more debug printing
pygame
parents: 317
diff changeset
716 t = rect->y; b = rect->y + rect->h - 1;
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
717
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
718 /*build the pointlist*/
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
719 points = Py_BuildValue("((ii)(ii)(ii)(ii))", l, t, r, t, r, b, l, b);
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
720
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
721 args = Py_BuildValue("(OOOi)", surfobj, colorobj, points, width);
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
722 if(args) ret = polygon(NULL, args);
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
723
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
724 Py_XDECREF(args);
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
725 Py_XDECREF(points);
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
726 return ret;
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
727 }
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
728
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
729
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
730
77aae459b436 doc updates
pygame
parents: 92
diff changeset
731
77aae459b436 doc updates
pygame
parents: 92
diff changeset
732 /*internal drawing tools*/
77aae459b436 doc updates
pygame
parents: 92
diff changeset
733
442
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
734 static int clip_and_draw_aaline(SDL_Surface* surf, SDL_Rect* rect, Uint32 color, float* pts, int blend)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
735 {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
736 if(!clipaaline(pts, rect->x+1, rect->y+1, rect->x+rect->w-2, rect->y+rect->h-2))
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
737 return 0;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
738 drawaaline(surf, color, pts[0], pts[1], pts[2], pts[3], blend);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
739 return 1;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
740 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
741
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
742 static int clip_and_draw_line(SDL_Surface* surf, SDL_Rect* rect, Uint32 color, int* pts)
77aae459b436 doc updates
pygame
parents: 92
diff changeset
743 {
77aae459b436 doc updates
pygame
parents: 92
diff changeset
744 if(!clipline(pts, rect->x, rect->y, rect->x+rect->w-1, rect->y+rect->h-1))
77aae459b436 doc updates
pygame
parents: 92
diff changeset
745 return 0;
77aae459b436 doc updates
pygame
parents: 92
diff changeset
746 if(pts[1] == pts[3])
77aae459b436 doc updates
pygame
parents: 92
diff changeset
747 drawhorzline(surf, color, pts[0], pts[1], pts[2]);
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
748 else if(pts[0] == pts[2])
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
749 drawvertline(surf, color, pts[0], pts[1], pts[3]);
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
750 else
77aae459b436 doc updates
pygame
parents: 92
diff changeset
751 drawline(surf, color, pts[0], pts[1], pts[2], pts[3]);
77aae459b436 doc updates
pygame
parents: 92
diff changeset
752 return 1;
77aae459b436 doc updates
pygame
parents: 92
diff changeset
753 }
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
754
176
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
755 static int clip_and_draw_line_width(SDL_Surface* surf, SDL_Rect* rect, Uint32 color, int width, int* pts)
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
756 {
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
757 int loop;
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
758 int xinc=0, yinc=0;
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
759 int newpts[4];
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
760 int range[4];
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
761 int anydrawn = 0;
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
762
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
763 if(abs(pts[0]-pts[2]) > abs(pts[1]-pts[3]))
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
764 yinc = 1;
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
765 else
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
766 xinc = 1;
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
767
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
768 memcpy(newpts, pts, sizeof(int)*4);
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
769 if(clip_and_draw_line(surf, rect, color, newpts))
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
770 {
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
771 anydrawn = 1;
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
772 memcpy(range, newpts, sizeof(int)*4);
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
773 }
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
774 else
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
775 {
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
776 range[0] = range[1] = 10000;
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
777 range[2] = range[3] = -10000;
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
778 }
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
779
186
fa74e65cc266 line drawing fixups
pygame
parents: 177
diff changeset
780 for(loop = 1; loop < width; loop += 2)
176
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
781 {
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
782 newpts[0] = pts[0] + xinc*(loop/2+1);
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
783 newpts[1] = pts[1] + yinc*(loop/2+1);
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
784 newpts[2] = pts[2] + xinc*(loop/2+1);
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
785 newpts[3] = pts[3] + yinc*(loop/2+1);
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
786 if(clip_and_draw_line(surf, rect, color, newpts))
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
787 {
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
788 anydrawn = 1;
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
789 range[0] = min(newpts[0], range[0]);
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
790 range[1] = min(newpts[1], range[1]);
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
791 range[2] = max(newpts[2], range[2]);
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
792 range[3] = max(newpts[3], range[3]);
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
793 }
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
794 if(loop+1<width)
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
795 {
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
796 newpts[0] = pts[0] - xinc*(loop/2+1);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
797 newpts[1] = pts[1] - yinc*(loop/2+1);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
798 newpts[2] = pts[2] - xinc*(loop/2+1);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
799 newpts[3] = pts[3] - yinc*(loop/2+1);
176
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
800 if(clip_and_draw_line(surf, rect, color, newpts))
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
801 {
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
802 anydrawn = 1;
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
803 range[0] = min(newpts[0], range[0]);
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
804 range[1] = min(newpts[1], range[1]);
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
805 range[2] = max(newpts[2], range[2]);
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
806 range[3] = max(newpts[3], range[3]);
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
807 }
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
808 }
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
809 }
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
810 if(anydrawn)
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
811 memcpy(pts, range, sizeof(int)*4);
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
812 return anydrawn;
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
813 }
c1697a051ba1 line widths
pygame
parents: 154
diff changeset
814
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
815
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
816 /*this line clipping based heavily off of code from
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
817 http://www.ncsa.uiuc.edu/Vis/Graphics/src/clipCohSuth.c */
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
818 #define LEFT_EDGE 0x1
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
819 #define RIGHT_EDGE 0x2
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
820 #define BOTTOM_EDGE 0x4
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
821 #define TOP_EDGE 0x8
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
822 #define INSIDE(a) (!a)
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
823 #define REJECT(a,b) (a&b)
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
824 #define ACCEPT(a,b) (!(a|b))
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
825
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
826 static int encode(int x, int y, int left, int top, int right, int bottom)
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
827 {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
828 int code = 0;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
829 if(x < left) code |= LEFT_EDGE;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
830 if(x > right) code |= RIGHT_EDGE;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
831 if(y < top) code |= TOP_EDGE;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
832 if(y > bottom)code |= BOTTOM_EDGE;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
833 return code;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
834 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
835
442
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
836 static int encodeFloat(float x, float y, int left, int top, int right, int bottom)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
837 {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
838 int code = 0;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
839 if(x < left) code |= LEFT_EDGE;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
840 if(x > right) code |= RIGHT_EDGE;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
841 if(y < top) code |= TOP_EDGE;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
842 if(y > bottom)code |= BOTTOM_EDGE;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
843 return code;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
844 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
845
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
846 static int clipaaline(float* pts, int left, int top, int right, int bottom)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
847 {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
848 float x1 = pts[0];
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
849 float y1 = pts[1];
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
850 float x2 = pts[2];
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
851 float y2 = pts[3];
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
852 int code1, code2;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
853 int draw = 0;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
854 float swaptmp;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
855 int intswaptmp;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
856 float m; /*slope*/
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
857
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
858 while(1) {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
859 code1 = encodeFloat(x1, y1, left, top, right, bottom);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
860 code2 = encodeFloat(x2, y2, left, top, right, bottom);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
861 if(ACCEPT(code1, code2)) {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
862 draw = 1;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
863 break;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
864 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
865 else if(REJECT(code1, code2)) {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
866 break;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
867 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
868 else {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
869 if(INSIDE(code1)) {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
870 swaptmp = x2; x2 = x1; x1 = swaptmp;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
871 swaptmp = y2; y2 = y1; y1 = swaptmp;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
872 intswaptmp = code2; code2 = code1; code1 = intswaptmp;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
873 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
874 if(x2 != x1)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
875 m = (y2 - y1) / (x2 - x1);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
876 else
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
877 m = 1.0f;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
878 if(code1 & LEFT_EDGE) {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
879 y1 += ((float)left - x1) * m;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
880 x1 = (float)left;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
881 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
882 else if(code1 & RIGHT_EDGE) {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
883 y1 += ((float)right - x1) * m;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
884 x1 = (float)right;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
885 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
886 else if(code1 & BOTTOM_EDGE) {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
887 if(x2 != x1)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
888 x1 += ((float)bottom - y1) / m;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
889 y1 = (float)bottom;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
890 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
891 else if(code1 & TOP_EDGE) {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
892 if(x2 != x1)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
893 x1 += ((float)top - y1) / m;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
894 y1 = (float)top;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
895 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
896 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
897 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
898 if(draw) {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
899 pts[0] = x1; pts[1] = y1;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
900 pts[2] = x2; pts[3] = y2;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
901 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
902 return draw;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
903 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
904
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
905 static int clipline(int* pts, int left, int top, int right, int bottom)
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
906 {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
907 int x1 = pts[0];
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
908 int y1 = pts[1];
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
909 int x2 = pts[2];
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
910 int y2 = pts[3];
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
911 int code1, code2;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
912 int draw = 0;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
913 int swaptmp;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
914 float m; /*slope*/
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
915
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
916 while(1)
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
917 {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
918 code1 = encode(x1, y1, left, top, right, bottom);
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
919 code2 = encode(x2, y2, left, top, right, bottom);
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
920 if(ACCEPT(code1, code2)) {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
921 draw = 1;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
922 break;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
923 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
924 else if(REJECT(code1, code2))
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
925 break;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
926 else {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
927 if(INSIDE(code1)) {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
928 swaptmp = x2; x2 = x1; x1 = swaptmp;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
929 swaptmp = y2; y2 = y1; y1 = swaptmp;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
930 swaptmp = code2; code2 = code1; code1 = swaptmp;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
931 }
238
52a1054f4616 added pygame.movie
pygame
parents: 226
diff changeset
932 if(x2 != x1)
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
933 m = (y2 - y1) / (float)(x2 - x1);
99
f370f63e87f7 final warning cleaned
pygame
parents: 96
diff changeset
934 else
f370f63e87f7 final warning cleaned
pygame
parents: 96
diff changeset
935 m = 1.0f;
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
936 if(code1 & LEFT_EDGE) {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
937 y1 += (int)((left - x1) * m);
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
938 x1 = left;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
939 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
940 else if(code1 & RIGHT_EDGE) {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
941 y1 += (int)((right - x1) * m);
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
942 x1 = right;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
943 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
944 else if(code1 & BOTTOM_EDGE) {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
945 if(x2 != x1)
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
946 x1 += (int)((bottom - y1) / m);
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
947 y1 = bottom;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
948 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
949 else if(code1 & TOP_EDGE) {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
950 if(x2 != x1)
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
951 x1 += (int)((top - y1) / m);
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
952 y1 = top;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
953 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
954 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
955 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
956 if(draw) {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
957 pts[0] = x1; pts[1] = y1;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
958 pts[2] = x2; pts[3] = y2;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
959 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
960 return draw;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
961 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
962
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
963
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
964
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
965 static int set_at(SDL_Surface* surf, int x, int y, Uint32 color)
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
966 {
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
967 SDL_PixelFormat* format = surf->format;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
968 Uint8* pixels = (Uint8*)surf->pixels;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
969 Uint8* byte_buf, rgb[4];
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
970
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
971 if(x < surf->clip_rect.x || x >= surf->clip_rect.x + surf->clip_rect.w ||
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
972 y < surf->clip_rect.y || y >= surf->clip_rect.y + surf->clip_rect.h)
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
973 return 0;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
974
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
975 switch(format->BytesPerPixel)
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
976 {
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
977 case 1:
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
978 *((Uint8*)pixels + y * surf->pitch + x) = (Uint8)color;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
979 break;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
980 case 2:
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
981 *((Uint16*)(pixels + y * surf->pitch) + x) = (Uint16)color;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
982 break;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
983 case 4:
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
984 *((Uint32*)(pixels + y * surf->pitch) + x) = color;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
985 /* *((Uint32*)(pixels + y * surf->pitch) + x) =
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
986 ~(*((Uint32*)(pixels + y * surf->pitch) + x)) * 31;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
987 */ break;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
988 default:/*case 3:*/
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
989 SDL_GetRGB(color, format, rgb, rgb+1, rgb+2);
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
990 byte_buf = (Uint8*)(pixels + y * surf->pitch) + x * 3;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
991 *(byte_buf + (format->Rshift >> 3)) = rgb[0];
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
992 *(byte_buf + (format->Gshift >> 3)) = rgb[1];
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
993 *(byte_buf + (format->Bshift >> 3)) = rgb[2];
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
994 break;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
995 }
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
996 return 1;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
997 }
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
998
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
999
442
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1000 #define DRAWPIX32(pixel,colorptr,br,blend) \
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1001 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) color <<= 8; \
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1002 if(blend) { \
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1003 short x; \
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1004 x = colorptr[0]*br+pixel[0]; \
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1005 pixel[0]= (x>254) ? 255: x; \
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1006 x = colorptr[1]*br+pixel[1]; \
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1007 pixel[1]= (x>254) ? 255: x; \
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1008 x = colorptr[2]*br+pixel[2]; \
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1009 pixel[2]= (x>254) ? 255: x; \
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1010 } else { \
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1011 pixel[0]=(Uint8)(colorptr[0]*br); \
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1012 pixel[1]=(Uint8)(colorptr[1]*br); \
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1013 pixel[2]=(Uint8)(colorptr[2]*br); \
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1014 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1015
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1016 /* Adapted from http://freespace.virgin.net/hugo.elias/graphics/x_wuline.htm */
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1017 static void drawaaline(SDL_Surface* surf, Uint32 color, float x1, float y1, float x2, float y2, int blend) {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1018 float grad, xd, yd;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1019 float xgap, ygap, xend, yend, xf, yf;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1020 float brightness1, brightness2;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1021 float swaptmp;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1022 int x, y, ix1, ix2, iy1, iy2;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1023 int pixx, pixy;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1024 Uint8* pixel;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1025 Uint8* pm = (Uint8*)surf->pixels;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1026 Uint8* colorptr = (Uint8*)&color;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1027
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1028 pixx = surf->format->BytesPerPixel;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1029 pixy = surf->pitch;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1030
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1031 xd = x2-x1;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1032 yd = y2-y1;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1033 if(fabs(xd)>fabs(yd)) {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1034 if(x1>x2) {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1035 swaptmp=x1; x1=x2; x2=swaptmp;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1036 swaptmp=y1; y1=y2; y2=swaptmp;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1037 xd = (x2-x1);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1038 yd = (y2-y1);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1039 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1040 grad = yd/xd;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1041 xend = trunc(x1)+0.5; /* This makes more sense than trunc(x1+0.5) */
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1042 yend = y1+grad*(xend-x1);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1043 xgap = INVFRAC(x1);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1044 ix1 = (int)xend;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1045 iy1 = (int)yend;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1046 yf = yend+grad;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1047 brightness1 = INVFRAC(yend) * xgap;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1048 brightness2 = FRAC(yend) * xgap;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1049 pixel = pm + pixx * ix1 + pixy * iy1;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1050 DRAWPIX32(pixel, colorptr, brightness1, blend)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1051 pixel += pixy;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1052 DRAWPIX32(pixel, colorptr, brightness2, blend)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1053 xend = trunc(x2)+0.5;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1054 yend = y2+grad*(xend-x2);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1055 xgap = FRAC(x2); /* this also differs from Hugo's description. */
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1056 ix2 = (int)xend;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1057 iy2 = (int)yend;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1058 brightness1 = INVFRAC(yend) * xgap;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1059 brightness2 = FRAC(yend) * xgap;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1060 pixel = pm + pixx * ix2 + pixy * iy2;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1061 DRAWPIX32(pixel, colorptr, brightness1, blend)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1062 pixel += pixy;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1063 DRAWPIX32(pixel, colorptr, brightness2, blend)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1064 for(x=ix1+1; x<ix2; ++x) {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1065 brightness1=INVFRAC(yf);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1066 brightness2= FRAC(yf);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1067 pixel = pm + pixx * x + pixy * (int)yf;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1068 DRAWPIX32(pixel, colorptr, brightness1, blend)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1069 pixel += pixy;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1070 DRAWPIX32(pixel, colorptr, brightness2, blend)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1071 yf += grad;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1072 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1073 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1074 else {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1075 if(y1>y2) {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1076 swaptmp=y1; y1=y2; y2=swaptmp;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1077 swaptmp=x1; x1=x2; x2=swaptmp;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1078 yd = (y2-y1);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1079 xd = (x2-x1);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1080 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1081 grad = xd/yd;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1082 yend = trunc(y1)+0.5; /* This makes more sense than trunc(x1+0.5) */
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1083 xend = x1+grad*(yend-y1);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1084 ygap = INVFRAC(y1);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1085 iy1 = (int)yend;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1086 ix1 = (int)xend;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1087 xf = xend+grad;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1088 brightness1 = INVFRAC(xend) * ygap;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1089 brightness2 = FRAC(xend) * ygap;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1090 pixel = pm + pixx * ix1 + pixy * iy1;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1091 DRAWPIX32(pixel, colorptr, brightness1, blend)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1092 pixel += pixx;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1093 DRAWPIX32(pixel, colorptr, brightness2, blend)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1094 yend = trunc(y2)+0.5;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1095 xend = x2+grad*(yend-y2);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1096 ygap = FRAC(y2);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1097 iy2 = (int)yend;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1098 ix2 = (int)xend;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1099 brightness1 = INVFRAC(xend) * ygap;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1100 brightness2 = FRAC(xend) * ygap;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1101 pixel = pm + pixx * ix2 + pixy * iy2;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1102 DRAWPIX32(pixel, colorptr, brightness1, blend)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1103 pixel += pixx;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1104 DRAWPIX32(pixel, colorptr, brightness2, blend)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1105 for(y=iy1+1; y<iy2; ++y) {
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1106 brightness1=INVFRAC(xf);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1107 brightness2= FRAC(xf);
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1108 pixel = pm + pixx * (int)xf + pixy * y;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1109 DRAWPIX32(pixel, colorptr, brightness1, blend)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1110 pixel += pixx;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1111 DRAWPIX32(pixel, colorptr, brightness2, blend)
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1112 xf += grad;
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1113 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1114 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1115 }
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1116
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1117
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
1118 /*here's my sdl'ized version of bresenham*/
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1119 static void drawline(SDL_Surface* surf, Uint32 color, int x1, int y1, int x2, int y2)
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1120 {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1121 int deltax, deltay, signx, signy;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1122 int pixx, pixy;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1123 int x = 0, y = 0;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1124 int swaptmp;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1125 Uint8 *pixel;
87
cbad64e4a5e9 fixed big-endian line draw
pygame
parents: 85
diff changeset
1126 Uint8 *colorptr;
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1127
238
52a1054f4616 added pygame.movie
pygame
parents: 226
diff changeset
1128 deltax = x2 - x1;
52a1054f4616 added pygame.movie
pygame
parents: 226
diff changeset
1129 deltay = y2 - y1;
52a1054f4616 added pygame.movie
pygame
parents: 226
diff changeset
1130 signx = (deltax < 0) ? -1 : 1;
52a1054f4616 added pygame.movie
pygame
parents: 226
diff changeset
1131 signy = (deltay < 0) ? -1 : 1;
52a1054f4616 added pygame.movie
pygame
parents: 226
diff changeset
1132 deltax = signx * deltax + 1;
52a1054f4616 added pygame.movie
pygame
parents: 226
diff changeset
1133 deltay = signy * deltay + 1;
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1134
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1135 pixx = surf->format->BytesPerPixel;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1136 pixy = surf->pitch;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1137 pixel = ((Uint8*)surf->pixels) + pixx * x1 + pixy * y1;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1138
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1139 pixx *= signx;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1140 pixy *= signy;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1141 if(deltax < deltay) /*swap axis if rise > run*/
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1142 {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1143 swaptmp = deltax; deltax = deltay; deltay = swaptmp;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1144 swaptmp = pixx; pixx = pixy; pixy = swaptmp;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1145 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1146
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1147 switch(surf->format->BytesPerPixel)
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1148 {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1149 case 1:
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1150 for(; x < deltax; x++, pixel += pixx) {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1151 *pixel = (Uint8)color;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1152 y += deltay; if(y >= deltax) {y -= deltax; pixel += pixy;}
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1153 }break;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1154 case 2:
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1155 for(; x < deltax; x++, pixel += pixx) {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1156 *(Uint16*)pixel = (Uint16)color;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1157 y += deltay; if(y >= deltax) {y -= deltax; pixel += pixy;}
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1158 }break;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1159 case 3:
87
cbad64e4a5e9 fixed big-endian line draw
pygame
parents: 85
diff changeset
1160 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) color <<= 8;
cbad64e4a5e9 fixed big-endian line draw
pygame
parents: 85
diff changeset
1161 colorptr = (Uint8*)&color;
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1162 for(; x < deltax; x++, pixel += pixx) {
87
cbad64e4a5e9 fixed big-endian line draw
pygame
parents: 85
diff changeset
1163 pixel[0] = colorptr[0];
cbad64e4a5e9 fixed big-endian line draw
pygame
parents: 85
diff changeset
1164 pixel[1] = colorptr[1];
cbad64e4a5e9 fixed big-endian line draw
pygame
parents: 85
diff changeset
1165 pixel[2] = colorptr[2];
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1166 y += deltay; if(y >= deltax) {y -= deltax; pixel += pixy;}
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1167 }break;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1168 default: /*case 4*/
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1169 for(; x < deltax; x++, pixel += pixx) {
442
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1170 *(Uint32*)pixel = (Uint32)color;
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1171 y += deltay; if(y >= deltax) {y -= deltax; pixel += pixy;}
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1172 }break;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1173 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1174 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1175
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1176
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1177
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
1178 static void drawhorzline(SDL_Surface* surf, Uint32 color, int x1, int y1, int x2)
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1179 {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1180 Uint8 *pixel, *end;
87
cbad64e4a5e9 fixed big-endian line draw
pygame
parents: 85
diff changeset
1181 Uint8 *colorptr;
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1182
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1183 if(x1 == x2) return;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1184
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1185 pixel = ((Uint8*)surf->pixels) + surf->pitch * y1;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1186 if(x1 < x2)
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1187 {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1188 end = pixel + x2 * surf->format->BytesPerPixel;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1189 pixel += x1 * surf->format->BytesPerPixel;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1190 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1191 else
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1192 {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1193 end = pixel + x1 * surf->format->BytesPerPixel;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1194 pixel += x2 * surf->format->BytesPerPixel;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1195 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1196 switch(surf->format->BytesPerPixel)
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1197 {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1198 case 1:
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1199 for(; pixel <= end; ++pixel) {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1200 *pixel = (Uint8)color;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1201 }break;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1202 case 2:
110
544b3e6cd502 line drawing cleanups
pygame
parents: 107
diff changeset
1203 for(; pixel <= end; pixel+=2) {
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1204 *(Uint16*)pixel = (Uint16)color;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1205 }break;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1206 case 3:
87
cbad64e4a5e9 fixed big-endian line draw
pygame
parents: 85
diff changeset
1207 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) color <<= 8;
cbad64e4a5e9 fixed big-endian line draw
pygame
parents: 85
diff changeset
1208 colorptr = (Uint8*)&color;
110
544b3e6cd502 line drawing cleanups
pygame
parents: 107
diff changeset
1209 for(; pixel <= end; pixel+=3) {
87
cbad64e4a5e9 fixed big-endian line draw
pygame
parents: 85
diff changeset
1210 pixel[0] = colorptr[0];
cbad64e4a5e9 fixed big-endian line draw
pygame
parents: 85
diff changeset
1211 pixel[1] = colorptr[1];
cbad64e4a5e9 fixed big-endian line draw
pygame
parents: 85
diff changeset
1212 pixel[2] = colorptr[2];
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1213 }break;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1214 default: /*case 4*/
110
544b3e6cd502 line drawing cleanups
pygame
parents: 107
diff changeset
1215 for(; pixel <= end; pixel+=4) {
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1216 *(Uint32*)pixel = color;
260
ad329e5495c7 filled drawing clip fix and rect comparisons
pygame
parents: 258
diff changeset
1217 }break;
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1218 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1219 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1220
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1221 static void drawhorzlineclip(SDL_Surface* surf, Uint32 color, int x1, int y1, int x2)
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1222 {
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1223 if(y1 < surf->clip_rect.y || y1 >= surf->clip_rect.y + surf->clip_rect.h)
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1224 return;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1225
260
ad329e5495c7 filled drawing clip fix and rect comparisons
pygame
parents: 258
diff changeset
1226 if( x2 < x1 )
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1227 {
260
ad329e5495c7 filled drawing clip fix and rect comparisons
pygame
parents: 258
diff changeset
1228 int temp = x1;
ad329e5495c7 filled drawing clip fix and rect comparisons
pygame
parents: 258
diff changeset
1229 x1 = x2; x2 = temp;
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1230 }
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1231
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1232 x1 = max(x1, surf->clip_rect.x);
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1233 x2 = min(x2, surf->clip_rect.x + surf->clip_rect.w-1);
260
ad329e5495c7 filled drawing clip fix and rect comparisons
pygame
parents: 258
diff changeset
1234
ad329e5495c7 filled drawing clip fix and rect comparisons
pygame
parents: 258
diff changeset
1235 if(x2 < surf->clip_rect.x || x1 >= surf->clip_rect.x + surf->clip_rect.w)
ad329e5495c7 filled drawing clip fix and rect comparisons
pygame
parents: 258
diff changeset
1236 return;
ad329e5495c7 filled drawing clip fix and rect comparisons
pygame
parents: 258
diff changeset
1237
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1238 if(x1 == x2)
260
ad329e5495c7 filled drawing clip fix and rect comparisons
pygame
parents: 258
diff changeset
1239 set_at(surf, x1, y1, color);
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1240 else
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1241 drawhorzline(surf, color, x1, y1, x2);
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1242 }
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1243
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1244 static void drawvertline(SDL_Surface* surf, Uint32 color, int x1, int y1, int y2)
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1245 {
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1246 Uint8 *pixel, *end;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1247 Uint8 *colorptr;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1248 Uint32 pitch = surf->pitch;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1249
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1250 if(y1 == y2) return;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1251
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1252 pixel = ((Uint8*)surf->pixels) + x1 * surf->format->BytesPerPixel;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1253 if(y1 < y2)
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1254 {
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1255 end = pixel + surf->pitch * y2;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1256 pixel += surf->pitch * y1;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1257 }
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1258 else
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1259 {
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1260 end = pixel + surf->pitch * y1;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1261 pixel += surf->pitch * y2;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1262 }
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1263
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1264 switch(surf->format->BytesPerPixel)
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1265 {
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1266 case 1:
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1267 for(; pixel <= end; pixel+=pitch) {
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1268 *pixel = (Uint8)color;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1269 }break;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1270 case 2:
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1271 for(; pixel <= end; pixel+=pitch) {
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1272 *(Uint16*)pixel = (Uint16)color;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1273 }break;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1274 case 3:
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1275 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) color <<= 8;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1276 colorptr = (Uint8*)&color;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1277 for(; pixel <= end; pixel+=pitch) {
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1278 pixel[0] = colorptr[0];
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1279 pixel[1] = colorptr[1];
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1280 pixel[2] = colorptr[2];
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1281 }break;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1282 default: /*case 4*/
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1283 for(; pixel <= end; pixel+=pitch) {
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1284 *(Uint32*)pixel = color;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1285 }break;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1286 }
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1287 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1288
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1289
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1290 static void drawvertlineclip(SDL_Surface* surf, Uint32 color, int x1, int y1, int y2)
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1291 {
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1292 if(x1 < surf->clip_rect.x || x1 >= surf->clip_rect.x + surf->clip_rect.w)
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1293 return;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1294 if ( y2 < y1 )
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1295 {
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1296 int temp = y1;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1297 y1 = y2; y2 = temp;
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1298 }
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1299 y1 = max(y1, surf->clip_rect.y);
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1300 y2 = min(y2, surf->clip_rect.y + surf->clip_rect.h-1);
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1301 if(y2 - y1 < 1)
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1302 set_at( surf, x1, y1, color);
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1303 else
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1304 drawvertline(surf, color, x1, y1, y2);
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1305 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1306
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
1307 static void draw_ellipse(SDL_Surface *dst, int x, int y, int rx, int ry, Uint32 color)
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1308 {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1309 int ix, iy;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1310 int h, i, j, k;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1311 int oh, oi, oj, ok;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1312 int xmh, xph, ypk, ymk;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1313 int xmi, xpi, ymj, ypj;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1314 int xmj, xpj, ymi, ypi;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1315 int xmk, xpk, ymh, yph;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1316
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1317 if (rx==0 && ry==0) { /* Special case - draw a single pixel */
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1318 set_at( dst, x, y, color);
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1319 return;
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1320 }
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1321 if (rx==0) { /* Special case for rx=0 - draw a vline */
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1322 drawvertlineclip( dst, color, x, (Sint16)(y-ry), (Sint16)(y+ry) );
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1323 return;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1324 }
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1325 if (ry==0) { /* Special case for ry=0 - draw a hline */
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1326 drawhorzlineclip( dst, color, (Sint16)(x-rx), y, (Sint16)(x+rx) );
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1327 return;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1328 }
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1329
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1330
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1331 /* Init vars */
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1332 oh = oi = oj = ok = 0xFFFF;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1333 if (rx > ry) {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1334 ix = 0;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1335 iy = rx * 64;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1336 do {
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1337 h = (ix + 16) >> 6;
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1338 i = (iy + 16) >> 6;
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1339 j = (h * ry) / rx;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1340 k = (i * ry) / rx;
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1341
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1342 if (((ok!=k) && (oj!=k)) || ((oj!=j) && (ok!=j)) || (k!=j)) {
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
1343 xph=x+h-1;
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1344 xmh=x-h;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1345 if (k>0) {
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
1346 ypk=y+k-1;
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1347 ymk=y-k;
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1348 if(h > 0) {
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1349 set_at(dst, xmh, ypk, color);
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1350 set_at(dst, xmh, ymk, color);
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1351 }
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1352 set_at(dst, xph, ypk, color);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1353 set_at(dst, xph, ymk, color);
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1354 }
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1355 ok=k;
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
1356 xpi=x+i-1;
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1357 xmi=x-i;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1358 if (j>0) {
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
1359 ypj=y+j-1;
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1360 ymj=y-j;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1361 set_at(dst, xmi, ypj, color);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1362 set_at(dst, xpi, ypj, color);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1363 set_at(dst, xmi, ymj, color);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1364 set_at(dst, xpi, ymj, color);
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1365 }
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1366 oj=j;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1367 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1368 ix = ix + iy / rx;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1369 iy = iy - ix / rx;
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1370
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1371 } while (i > h);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1372 } else {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1373 ix = 0;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1374 iy = ry * 64;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1375 do {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1376 h = (ix + 32) >> 6;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1377 i = (iy + 32) >> 6;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1378 j = (h * rx) / ry;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1379 k = (i * rx) / ry;
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1380
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1381 if (((oi!=i) && (oh!=i)) || ((oh!=h) && (oi!=h) && (i!=h))) {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1382 xmj=x-j;
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
1383 xpj=x+j-1;
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1384 if (i>0) {
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
1385 ypi=y+i-1;
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1386 ymi=y-i;
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1387 if(j > 0) {
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1388 set_at(dst, xmj, ypi, color);
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1389 set_at(dst, xmj, ymi, color);
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1390 }
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1391 set_at(dst, xpj, ypi, color);
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1392 set_at(dst, xpj, ymi, color);
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1393 }
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1394 oi=i;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1395 xmk=x-k;
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
1396 xpk=x+k-1;
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1397 if (h>0) {
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
1398 yph=y+h-1;
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1399 ymh=y-h;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1400 set_at(dst, xmk, yph, color);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1401 set_at(dst, xpk, yph, color);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1402 set_at(dst, xmk, ymh, color);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1403 set_at(dst, xpk, ymh, color);
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1404 }
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1405 oh=h;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1406 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1407 ix = ix + iy / ry;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1408 iy = iy - ix / ry;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1409 } while(i > h);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1410 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1411 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1412
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1413
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1414
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1415
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1416
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1417
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1418
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
1419 static void draw_fillellipse(SDL_Surface *dst, int x, int y, int rx, int ry, Uint32 color)
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1420 {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1421 int ix, iy;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1422 int h, i, j, k;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1423 int oh, oi, oj, ok;
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1424
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1425 if (rx==0 && ry==0) { /* Special case - draw a single pixel */
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1426 set_at( dst, x, y, color);
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1427 return;
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1428 }
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1429 if (rx==0) { /* Special case for rx=0 - draw a vline */
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1430 drawvertlineclip( dst, color, x, (Sint16)(y-ry), (Sint16)(y+ry) );
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1431 return;
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1432 }
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1433 if (ry==0) { /* Special case for ry=0 - draw a hline */
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1434 drawhorzlineclip( dst, color, (Sint16)(x-rx), y, (Sint16)(x+rx) );
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1435 return;
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1436 }
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1437
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1438 /* Init vars */
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1439 oh = oi = oj = ok = 0xFFFF;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1440
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1441 /* Draw */
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1442 if (rx >= ry) {
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1443 ix = 0;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1444 iy = rx * 64;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1445
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1446 do {
258
ca48dc71cfad small filled ellipse fix
pygame
parents: 257
diff changeset
1447 h = (ix + 4/*32*/) >> 6;
ca48dc71cfad small filled ellipse fix
pygame
parents: 257
diff changeset
1448 i = (iy + 4/*32*/) >> 6;
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1449 j = (h * ry) / rx;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1450 k = (i * ry) / rx;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1451 if ((ok!=k) && (oj!=k)) {
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1452 drawhorzlineclip(dst, color, x-h, y+k, x+h-1);
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1453 drawhorzlineclip(dst, color, x-h, y-k-1, x+h-1);
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1454 ok=k;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1455 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1456 if ((oj!=j) && (ok!=j) && (k!=j)) {
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1457 drawhorzlineclip(dst, color, x-i, y+j, x+i-1);
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1458 drawhorzlineclip(dst, color, x-i, y-j-1, x+i-1);
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1459 oj=j;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1460 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1461 ix = ix + iy / rx;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1462 iy = iy - ix / rx;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1463
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1464 } while (i > h);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1465 } else {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1466 ix = 0;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1467 iy = ry * 64;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1468
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1469 do {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1470 h = (ix + 32) >> 6;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1471 i = (iy + 32) >> 6;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1472 j = (h * rx) / ry;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1473 k = (i * rx) / ry;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1474
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1475 if ((oi!=i) && (oh!=i)) {
257
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1476 drawhorzlineclip(dst, color, x-j, y+i, x+j-1);
bc5028f12aba draw cleanups and mixer chunks
pygame
parents: 256
diff changeset
1477 drawhorzlineclip(dst, color, x-j, y-i-1, x+j-1);
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1478 oi=i;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1479 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1480 if ((oh!=h) && (oi!=h) && (i!=h)) {
258
ca48dc71cfad small filled ellipse fix
pygame
parents: 257
diff changeset
1481 drawhorzlineclip(dst, color, x-k, y+h, x+k-1);
ca48dc71cfad small filled ellipse fix
pygame
parents: 257
diff changeset
1482 drawhorzlineclip(dst, color, x-k, y-h-1, x+k-1);
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1483 oh=h;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1484 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1485
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1486 ix = ix + iy / ry;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1487 iy = iy - ix / ry;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1488
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1489 } while(i > h);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1490 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1491 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1492
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1493
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1494 static int compare_int(const void *a, const void *b)
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1495 {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1496 return (*(const int *)a) - (*(const int *)b);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1497 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1498
256
54b0c759187d circle and ellipse touch ups
pygame
parents: 244
diff changeset
1499 static void draw_fillpoly(SDL_Surface *dst, int *vx, int *vy, int n, Uint32 color)
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1500 {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1501 int i;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1502 int y;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1503 int miny, maxy;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1504 int x1, y1;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1505 int x2, y2;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1506 int ind1, ind2;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1507 int ints;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1508 int *polyints = PyMem_New(int, n);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1509
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1510
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1511 /* Determine Y maxima */
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1512 miny = vy[0];
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1513 maxy = vy[0];
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1514 for (i=1; (i < n); i++)
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1515 {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1516 miny = min(miny, vy[i]);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1517 maxy = max(maxy, vy[i]);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1518 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1519
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1520 /* Draw, scanning y */
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1521 for(y=miny; (y <= maxy); y++) {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1522 ints = 0;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1523 for (i=0; (i < n); i++) {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1524 if (!i) {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1525 ind1 = n-1;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1526 ind2 = 0;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1527 } else {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1528 ind1 = i-1;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1529 ind2 = i;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1530 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1531 y1 = vy[ind1];
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1532 y2 = vy[ind2];
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1533 if (y1 < y2) {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1534 x1 = vx[ind1];
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1535 x2 = vx[ind2];
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1536 } else if (y1 > y2) {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1537 y2 = vy[ind1];
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1538 y1 = vy[ind2];
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1539 x2 = vx[ind1];
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1540 x1 = vx[ind2];
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1541 } else {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1542 continue;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1543 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1544 if ((y >= y1) && (y < y2)) {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1545 polyints[ints++] = (y-y1) * (x2-x1) / (y2-y1) + x1;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1546 } else if ((y == maxy) && (y > y1) && (y <= y2)) {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1547 polyints[ints++] = (y-y1) * (x2-x1) / (y2-y1) + x1;
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1548 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1549 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1550 qsort(polyints, ints, sizeof(int), compare_int);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1551
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1552 for (i=0; (i<ints); i+=2) {
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1553 drawhorzlineclip(dst, color, polyints[i], y, polyints[i+1]);
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1554 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1555 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1556 }
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1557
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1558
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1559
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1560
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1561
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1562
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1563
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1564 static PyMethodDef draw_builtins[] =
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1565 {
442
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1566 { "aaline", aaline, 1, doc_aaline },
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1567 { "line", line, 1, doc_line },
442
ad9e57cfa846 antialiased lines
pygame
parents: 436
diff changeset
1568 { "aalines", aalines, 1, doc_aalines },
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
1569 { "lines", lines, 1, doc_lines },
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1570 { "ellipse", ellipse, 1, doc_ellipse },
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
1571 { "circle", circle, 1, doc_circle },
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1572 { "polygon", polygon, 1, doc_polygon },
244
afeaff39a636 draw convenciences
pygame
parents: 238
diff changeset
1573 { "rect", rect, 1, doc_rect },
225
0598a53bf75b draw module gets boosted
pygame
parents: 186
diff changeset
1574
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1575 { NULL, NULL }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1576 };
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1577
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1578
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1579
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1580 /*DOC*/ static char doc_pygame_draw_MODULE[] =
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1581 /*DOC*/ "Contains routines to draw onto a surface.\n"
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1582 /*DOC*/ "\n"
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1583 /*DOC*/ "Note that all\n"
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1584 /*DOC*/ "drawing routines use direct pixel access, so the surfaces\n"
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1585 /*DOC*/ "must be locked for use. The draw functions will temporarily\n"
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1586 /*DOC*/ "lock the surface if needed, but if performing many drawing\n"
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1587 /*DOC*/ "routines together, it would be best to surround the drawing\n"
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1588 /*DOC*/ "code with a lock/unlock pair.\n"
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1589 /*DOC*/ ;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1590
120
19072dafa19d initial mac support
pygame
parents: 110
diff changeset
1591 PYGAME_EXPORT
85
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1592 void initdraw(void)
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1593 {
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1594 PyObject *module, *dict;
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1595
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1596 /* create the module */
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1597 module = Py_InitModule3("draw", draw_builtins, doc_pygame_draw_MODULE);
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1598 dict = PyModule_GetDict(module);
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1599
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1600 /*imported needed apis*/
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1601 import_pygame_base();
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1602 import_pygame_rect();
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1603 import_pygame_surface();
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1604 }
37e90d2bc423 surfarray alpha routines added
pygame
parents:
diff changeset
1605
93
77aae459b436 doc updates
pygame
parents: 92
diff changeset
1606