Skip to content

Commit 2a4daf0

Browse files
committed
Added docs (both web and in C) and refactors
1 parent b60a413 commit 2a4daf0

File tree

6 files changed

+101
-32
lines changed

6 files changed

+101
-32
lines changed

docs/reST/ext/boilerplate.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ def lowercase_name(d):
337337
"music",
338338
"pygame",
339339
"Rect",
340+
"geometry",
340341
"Surface",
341342
"sprite",
342343
"time",

docs/reST/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ Reference
179179
:doc:`ref/rect`
180180
Flexible container for a rectangle.
181181

182+
:doc:`ref/geometry`
183+
Geometry types and operations.
184+
182185
:doc:`ref/scrap`
183186
Native clipboard access.
184187

docs/reST/ref/geometry.rst

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
.. include:: common.txt
2+
3+
:mod:`pygame.geometry`
4+
======================
5+
6+
.. module:: pygame.geometry
7+
:synopsis: pygame geometry module
8+
9+
| :sl:`pygame module for the Circle, Rect, and Polygon objects`
10+
11+
.. currentmodule:: pygame
12+
13+
.. class:: Circle
14+
15+
| :sl:`pygame object representing a circle`
16+
| :sg:`Circle(center, radius) -> Circle`
17+
| :sg:`Circle(x, y, radius) -> Circle`
18+
| :sg:`Circle(object) -> Circle`
19+
20+
.. attribute:: x
21+
22+
| :sl:`center x coordinate of the circle`
23+
| :sg:`x -> float`
24+
25+
The `x` coordinate of the center of the circle.
26+
27+
.. ## Circle.x ##
28+
29+
.. attribute:: y
30+
31+
| :sl:`center y coordinate of the circle`
32+
| :sg:`y -> float`
33+
34+
The `y` coordinate of the center of the circle.
35+
36+
.. ## Circle.y ##
37+
38+
.. attribute:: r
39+
40+
| :sl:`radius of the circle`
41+
| :sg:`r -> float`
42+
43+
The `r` coordinate of the center of the circle. You cannot set the radius to a negative value.
44+
45+
.. ## Circle.r ##
46+
47+
.. method:: copy
48+
49+
| :sl:`returns a copy of the circle`
50+
| :sg:`copy() -> Circle`
51+
52+
The `copy` method returns a new `Circle` object having the same position and radius
53+
as the original `Circle`. The function takes no arguments.
54+
55+
.. ## Circle.copy ##
56+
57+
.. ## pygame.Circle ##

docs/reST/themes/classic/elements.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ <h5>pygame-ce documentation</h5>
4040
We render three sets of items based on how useful to most apps.
4141

4242
#}
43-
{%- set basic = ['Color', 'display', 'draw', 'event', 'font', 'image', 'key', 'locals', 'mixer', 'mouse', 'music', 'pygame', 'Rect', 'Surface', 'time'] %}
43+
{%- set basic = ['Color', 'display', 'draw', 'event', 'font', 'image', 'key', 'locals', 'mixer', 'mouse', 'music', 'pygame', 'Rect', 'geometry', 'Surface', 'time'] %}
4444
{%- set advanced = ['BufferProxy', 'freetype', 'gfxdraw', 'midi', 'PixelArray', 'pixelcopy', 'sndarray', 'surfarray', 'cursors', 'joystick', 'mask', 'math', 'sprite', 'transform'] %}
4545
{%- set hidden = ['Overlay', 'cdrom', 'sdl2_video', 'sdl2_controller'] %}
4646
{%- if pyg_sections %}

src_c/circle.c

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "geometry.h"
2+
#include "doc/geometry_doc.h"
23

34
static PyObject *
45
_pg_circle_subtype_new(PyTypeObject *type, pgCircleBase *circle)
@@ -34,6 +35,7 @@ pgCircle_FromObject(PyObject *obj, pgCircleBase *out)
3435
return 1;
3536
}
3637

38+
/* Paths for sequences */
3739
if (pgSequenceFast_Check(obj)) {
3840
PyObject **f_arr = PySequence_Fast_ITEMS(obj);
3941
length = PySequence_Fast_GET_SIZE(obj);
@@ -66,7 +68,6 @@ pgCircle_FromObject(PyObject *obj, pgCircleBase *out)
6668
}
6769
}
6870
else if (PySequence_Check(obj)) {
69-
/* Path for other sequences or Types that count as sequences*/
7071
PyObject *tmp = NULL;
7172
length = PySequence_Length(obj);
7273
if (length == 3) {
@@ -128,41 +129,41 @@ pgCircle_FromObject(PyObject *obj, pgCircleBase *out)
128129
}
129130
}
130131

131-
if (PyObject_HasAttrString(obj, "circle")) {
132-
PyObject *circleattr;
133-
circleattr = PyObject_GetAttrString(obj, "circle");
134-
if (!circleattr) {
135-
PyErr_Clear();
136-
return 0;
137-
}
138-
if (PyCallable_Check(circleattr)) /*call if it's a method*/
139-
{
140-
PyObject *circleresult = PyObject_CallObject(circleattr, NULL);
141-
Py_DECREF(circleattr);
142-
if (!circleresult) {
143-
PyErr_Clear();
144-
return 0;
145-
}
146-
circleattr = circleresult;
147-
}
148-
if (!pgCircle_FromObject(circleattr, out)) {
132+
/* Path for objects that have a circle attribute */
133+
PyObject *circleattr;
134+
if (!(circleattr = PyObject_GetAttrString(obj, "circle"))) {
135+
PyErr_Clear();
136+
return 0;
137+
}
138+
139+
if (PyCallable_Check(circleattr)) /*call if it's a method*/
140+
{
141+
PyObject *circleresult = PyObject_CallObject(circleattr, NULL);
142+
Py_DECREF(circleattr);
143+
if (!circleresult) {
149144
PyErr_Clear();
150-
Py_DECREF(circleattr);
151145
return 0;
152146
}
153-
Py_DECREF(circleattr);
147+
circleattr = circleresult;
148+
}
154149

155-
return 1;
150+
if (!pgCircle_FromObject(circleattr, out)) {
151+
PyErr_Clear();
152+
Py_DECREF(circleattr);
153+
return 0;
156154
}
157-
return 0;
155+
156+
Py_DECREF(circleattr);
157+
158+
return 1;
158159
}
159160

160161
static PyObject *
161162
pg_circle_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
162163
{
163164
pgCircleObject *self = (pgCircleObject *)type->tp_alloc(type, 0);
164165

165-
if (self != NULL) {
166+
if (self) {
166167
self->circle.x = self->circle.y = 0;
167168
self->circle.r = 1;
168169
self->weakreflist = NULL;
@@ -186,7 +187,7 @@ pg_circle_init(pgCircleObject *self, PyObject *args, PyObject *kwds)
186187
static void
187188
pg_circle_dealloc(pgCircleObject *self)
188189
{
189-
if (self->weakreflist != NULL) {
190+
if (self->weakreflist) {
190191
PyObject_ClearWeakRefs((PyObject *)self);
191192
}
192193

@@ -236,8 +237,8 @@ pg_circle_str(pgCircleObject *self)
236237
}
237238

238239
static struct PyMethodDef pg_circle_methods[] = {
239-
{"__copy__", (PyCFunction)pg_circle_copy, METH_NOARGS, NULL},
240-
{"copy", (PyCFunction)pg_circle_copy, METH_NOARGS, NULL},
240+
{"__copy__", (PyCFunction)pg_circle_copy, METH_NOARGS, DOC_CIRCLE_COPY},
241+
{"copy", (PyCFunction)pg_circle_copy, METH_NOARGS, DOC_CIRCLE_COPY},
241242
{NULL, NULL, 0, NULL}};
242243

243244
#define GETTER_SETTER(name) \
@@ -294,9 +295,9 @@ pg_circle_setr(pgCircleObject *self, PyObject *value, void *closure)
294295
}
295296

296297
static PyGetSetDef pg_circle_getsets[] = {
297-
{"x", (getter)pg_circle_getx, (setter)pg_circle_setx, NULL, NULL},
298-
{"y", (getter)pg_circle_gety, (setter)pg_circle_sety, NULL, NULL},
299-
{"r", (getter)pg_circle_getr, (setter)pg_circle_setr, NULL, NULL},
298+
{"x", (getter)pg_circle_getx, (setter)pg_circle_setx, DOC_CIRCLE_X, NULL},
299+
{"y", (getter)pg_circle_gety, (setter)pg_circle_sety, DOC_CIRCLE_Y, NULL},
300+
{"r", (getter)pg_circle_getr, (setter)pg_circle_setr, DOC_CIRCLE_R, NULL},
300301
{NULL, 0, NULL, NULL, NULL}};
301302

302303
static PyTypeObject pgCircle_Type = {
@@ -306,7 +307,7 @@ static PyTypeObject pgCircle_Type = {
306307
.tp_repr = (reprfunc)pg_circle_repr,
307308
.tp_str = (reprfunc)pg_circle_str,
308309
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
309-
.tp_doc = NULL,
310+
.tp_doc = DOC_CIRCLE,
310311
.tp_weaklistoffset = offsetof(pgCircleObject, weakreflist),
311312
.tp_methods = pg_circle_methods,
312313
.tp_getset = pg_circle_getsets,

src_c/doc/geometry_doc.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* Auto generated file: with makeref.py . Docs go in docs/reST/ref/ . */
2+
#define DOC_GEOMETRY "pygame module for the Circle, Rect, and Polygon objects"
3+
#define DOC_CIRCLE "Circle(center, radius) -> Circle\nCircle(x, y, radius) -> Circle\nCircle(object) -> Circle\npygame object representing a circle"
4+
#define DOC_CIRCLE_X "x -> float\ncenter x coordinate of the circle"
5+
#define DOC_CIRCLE_Y "y -> float\ncenter y coordinate of the circle"
6+
#define DOC_CIRCLE_R "r -> float\nradius of the circle"
7+
#define DOC_CIRCLE_COPY "copy() -> Circle\nreturns a copy of the circle"

0 commit comments

Comments
 (0)