2 pygame - Python Game Library
3 Copyright (C) 2000-2001 Pete Shinners
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /** This header file includes all the definitions for the
27 ** base pygame extensions. This header only requires
28 ** Python includes (and SDL.h for functions that use SDL types).
29 ** The reason for functions prototyped with #define's is
30 ** to allow for maximum Python portability. It also uses
31 ** Python as the runtime linker, which allows for late binding.
32 '' For more information on this style of development, read
33 ** the Python docs on this subject.
34 ** http://www.python.org/doc/current/ext/using-cobjects.html
36 ** If using this to build your own derived extensions,
37 ** you'll see that the functions available here are mainly
38 ** used to help convert between python objects and SDL objects.
39 ** Since this library doesn't add a lot of functionality to
40 ** the SDL library, it doesn't need to offer a lot either.
42 ** When initializing your extension module, you must manually
43 ** import the modules you want to use. (this is the part about
44 ** using python as the runtime linker). Each module has its
45 ** own import_xxx() routine. You need to perform this import
46 ** after you have initialized your own module, and before
47 ** you call any routines from that module. Since every module
48 ** in pygame does this, there are plenty of examples.
50 ** The base module does include some useful conversion routines
51 ** that you are free to use in your own extension.
54 #include "pgplatform.h"
58 /* version macros (defined since version 1.9.5) */
59 #define PG_MAJOR_VERSION 2
60 #define PG_MINOR_VERSION 0
61 #define PG_PATCH_VERSION 1
62 #define PG_VERSIONNUM(MAJOR, MINOR, PATCH) (1000*(MAJOR) + 100*(MINOR) + (PATCH))
63 #define PG_VERSION_ATLEAST(MAJOR, MINOR, PATCH) \
64 (PG_VERSIONNUM(PG_MAJOR_VERSION, PG_MINOR_VERSION, PG_PATCH_VERSION) >= \
65 PG_VERSIONNUM(MAJOR, MINOR, PATCH))
67 /* SDL 1.x/2.x and pygame 1.x/2.x
69 #if defined(SDL_VERSION_ATLEAST)
70 #if (SDL_VERSION_ATLEAST(2, 0, 0))
71 #define PG_API_VERSION 2
73 /* for now: allow pygame 2 to be compiled with SDL 1. */
74 #define PG_API_VERSION 1
77 #define PG_API_VERSION ((PG_MAJOR_VERSION == 1) ? 1 : 2)
83 /* Flag indicating a pg_buffer; used for assertions within callbacks */
85 #define PyBUF_PYGAME 0x4000
87 #define PyBUF_HAS_FLAG(f, F) (((f) & (F)) == (F))
89 /* Array information exchange struct C type; inherits from Py_buffer
91 * Pygame uses its own Py_buffer derived C struct as an internal representation
92 * of an imported array buffer. The extended Py_buffer allows for a
93 * per-instance release callback,
95 typedef void (*pybuffer_releaseproc)(Py_buffer *);
97 typedef struct pg_bufferinfo_s {
99 PyObject *consumer; /* Input: Borrowed reference */
100 pybuffer_releaseproc release_buffer;
103 #include "pgimport.h"
108 #ifndef PYGAMEAPI_BASE_INTERNAL
109 #define pgExc_SDLError \
111 PYGAMEAPI_GET_SLOT(base, 0))
113 #define pg_RegisterQuit \
114 (*(void (*)(void (*)(void))) \
115 PYGAMEAPI_GET_SLOT(base, 1))
117 #define pg_IntFromObj \
118 (*(int (*)(PyObject *, int *)) \
119 PYGAMEAPI_GET_SLOT(base, 2))
121 #define pg_IntFromObjIndex \
122 (*(int (*)(PyObject *, int, int *)) \
123 PYGAMEAPI_GET_SLOT(base, 3))
125 #define pg_TwoIntsFromObj \
126 (*(int (*)(PyObject *, int *, int *)) \
127 PYGAMEAPI_GET_SLOT(base, 4))
129 #define pg_FloatFromObj \
130 (*(int (*)(PyObject *, float *)) \
131 PYGAMEAPI_GET_SLOT(base, 5))
133 #define pg_FloatFromObjIndex \
134 (*(int (*)(PyObject *, int, float *)) \
135 PYGAMEAPI_GET_SLOT(base, 6))
137 #define pg_TwoFloatsFromObj \
138 (*(int (*)(PyObject *, float *, float *)) \
139 PYGAMEAPI_GET_SLOT(base, 7))
141 #define pg_UintFromObj \
142 (*(int (*)(PyObject *, Uint32 *)) \
143 PYGAMEAPI_GET_SLOT(base, 8))
145 #define pg_UintFromObjIndex \
146 (*(int (*)(PyObject *, int, Uint32 *)) \
147 PYGAMEAPI_GET_SLOT(base, 9))
149 #define pgVideo_AutoQuit \
151 PYGAMEAPI_GET_SLOT(base, 10))
153 #define pgVideo_AutoInit \
155 PYGAMEAPI_GET_SLOT(base, 11))
157 #define pg_RGBAFromObj \
158 (*(int (*)(PyObject *, Uint8 *)) \
159 PYGAMEAPI_GET_SLOT(base, 12))
161 #define pgBuffer_AsArrayInterface \
162 (*(PyObject * (*)(Py_buffer *)) \
163 PYGAMEAPI_GET_SLOT(base, 13))
165 #define pgBuffer_AsArrayStruct \
166 (*(PyObject * (*)(Py_buffer *)) \
167 PYGAMEAPI_GET_SLOT(base, 14))
169 #define pgObject_GetBuffer \
170 (*(int (*)(PyObject *, pg_buffer *, int)) \
171 PYGAMEAPI_GET_SLOT(base, 15))
173 #define pgBuffer_Release \
174 (*(void (*)(pg_buffer *)) \
175 PYGAMEAPI_GET_SLOT(base, 16))
177 #define pgDict_AsBuffer \
178 (*(int (*)(pg_buffer *, PyObject *, int)) \
179 PYGAMEAPI_GET_SLOT(base, 17))
181 #define pgExc_BufferError \
183 PYGAMEAPI_GET_SLOT(base, 18))
185 #if PG_API_VERSION == 2
186 #define pg_GetDefaultWindow \
187 (*(SDL_Window * (*)(void)) \
188 PYGAMEAPI_GET_SLOT(base, 19))
190 #define pg_SetDefaultWindow \
191 (*(void (*)(SDL_Window *)) \
192 PYGAMEAPI_GET_SLOT(base, 20))
194 #define pg_GetDefaultWindowSurface \
195 (*(pgSurfaceObject * (*)(void)) \
196 PYGAMEAPI_GET_SLOT(base, 21))
198 #define pg_SetDefaultWindowSurface \
199 (*(void (*)(pgSurfaceObject *)) \
200 PYGAMEAPI_GET_SLOT(base, 22))
202 #define pg_EnvShouldBlendAlphaSDL2 \
203 (*(char * (*)(void)) \
204 PYGAMEAPI_GET_SLOT(base, 23))
206 #endif /* PG_API_VERSION == 2 */
208 #define import_pygame_base() IMPORT_PYGAME_MODULE(base)
209 #endif /* ~PYGAMEAPI_BASE_INTERNAL */
214 #if !defined(SDL_VERSION_ATLEAST) || PG_API_VERSION == 1
220 typedef SDL_Rect GAME_Rect;
224 PyObject_HEAD GAME_Rect r;
225 PyObject *weakreflist;
228 #define pgRect_AsRect(x) (((pgRectObject *)x)->r)
229 #ifndef PYGAMEAPI_RECT_INTERNAL
230 #define pgRect_Type \
232 PYGAMEAPI_GET_SLOT(rect, 0))
234 #define pgRect_Check(x) \
235 ((x)->ob_type == &pgRect_Type)
237 (*(PyObject * (*)(SDL_Rect *)) \
238 PYGAMEAPI_GET_SLOT(rect, 1))
240 #define pgRect_New4 \
241 (*(PyObject * (*)(int, int, int, int)) \
242 PYGAMEAPI_GET_SLOT(rect, 2))
244 #define pgRect_FromObject \
245 (*(GAME_Rect * (*)(PyObject *, GAME_Rect *)) \
246 PYGAMEAPI_GET_SLOT(rect, 3))
248 #define pgRect_Normalize (*(void (*)(GAME_Rect *)) PYGAMEAPI_GET_SLOT(rect, 4))
250 #define import_pygame_rect() IMPORT_PYGAME_MODULE(rect)
251 #endif /* ~PYGAMEAPI_RECT_INTERNAL */
258 PyObject_HEAD int id;
261 #define pgCD_AsID(x) (((pgCDObject *)x)->id)
262 #ifndef PYGAMEAPI_CDROM_INTERNAL
265 PYGAMEAPI_GET_SLOT(cdrom, 0))
267 #define pgCD_Check(x) \
268 ((x)->ob_type == &pgCD_Type)
270 (*(PyObject * (*)(int)) \
271 PYGAMEAPI_GET_SLOT(cdrom, 1))
273 #define import_pygame_cd() IMPORT_PYGAME_MODULE(cdrom)
279 typedef struct pgJoystickObject {
284 /* Joysticks form an intrusive linked list.
286 * Note that we don't maintain refcounts for these so they are weakrefs from
289 struct pgJoystickObject *next;
290 struct pgJoystickObject *prev;
293 #define pgJoystick_AsID(x) (((pgJoystickObject *)x)->id)
294 #define pgJoystick_AsSDL(x) (((pgJoystickObject *)x)->joy)
296 #ifndef PYGAMEAPI_JOYSTICK_INTERNAL
297 #define pgJoystick_Type \
299 PYGAMEAPI_GET_SLOT(joystick, 0))
301 #define pgJoystick_Check(x) \
302 ((x)->ob_type == &pgJoystick_Type)
303 #define pgJoystick_New \
304 (*(PyObject * (*)(int)) \
305 PYGAMEAPI_GET_SLOT(joystick, 1))
307 #define import_pygame_joystick() IMPORT_PYGAME_MODULE(joystick)
314 #if defined(SDL_VERSION_ATLEAST)
316 #if PG_API_VERSION == 2
318 Uint32 hw_available:1;
319 Uint32 wm_available:1;
328 SDL_PixelFormat *vfmt;
329 SDL_PixelFormat vfmt_data;
333 #endif /* PG_API_VERSION == 2 */
336 #if PG_API_VERSION == 1
337 PyObject_HEAD SDL_VideoInfo info;
339 PyObject_HEAD pg_VideoInfo info;
343 #define pgVidInfo_AsVidInfo(x) (((pgVidInfoObject *)x)->info)
344 #endif /* defined(SDL_VERSION_ATLEAST) */
346 #ifndef PYGAMEAPI_DISPLAY_INTERNAL
347 #define pgVidInfo_Type \
349 PYGAMEAPI_GET_SLOT(display, 0))
351 #define pgVidInfo_Check(x) \
352 ((x)->ob_type == &pgVidInfo_Type)
354 #if PG_API_VERSION == 1
355 #define pgVidInfo_New \
356 (*(PyObject * (*)(SDL_VideoInfo *)) \
357 PYGAMEAPI_GET_SLOT(display, 1))
359 #define pgVidInfo_New \
360 (*(PyObject * (*)(pg_VideoInfo *)) \
361 PYGAMEAPI_GET_SLOT(display, 1))
364 #define import_pygame_display() IMPORT_PYGAME_MODULE(display)
365 #endif /* ~PYGAMEAPI_DISPLAY_INTERNAL */
370 struct pgSubSurface_Data;
374 PyObject_HEAD struct SDL_Surface *surf;
375 #if PG_API_VERSION == 2
377 #endif /* PG_API_VERSION == 2 */
378 struct pgSubSurface_Data *subsurface; /* ptr to subsurface data (if a
380 PyObject *weakreflist;
382 PyObject *dependency;
384 #define pgSurface_AsSurface(x) (((pgSurfaceObject *)x)->surf)
386 #ifndef PYGAMEAPI_SURFACE_INTERNAL
387 #define pgSurface_Type \
389 PYGAMEAPI_GET_SLOT(surface, 0))
391 #define pgSurface_Check(x) \
392 (PyObject_IsInstance((x), (PyObject *) &pgSurface_Type))
393 #if PG_API_VERSION == 1
394 #define pgSurface_New \
395 (*(pgSurfaceObject * (*)(SDL_Surface *)) \
396 PYGAMEAPI_GET_SLOT(surface, 1))
398 #define pgSurface_SetSurface \
399 (*(int (*)(pgSurfaceObject *, SDL_Surface *)) \
400 PYGAMEAPI_GET_SLOT(surface, 3))
402 #else /* PG_API_VERSION == 2 */
403 #define pgSurface_New2 \
404 (*(pgSurfaceObject * (*)(SDL_Surface *, int)) \
405 PYGAMEAPI_GET_SLOT(surface, 1))
407 #define pgSurface_SetSurface \
408 (*(int (*)(pgSurfaceObject *, SDL_Surface *, int)) \
409 PYGAMEAPI_GET_SLOT(surface, 3))
411 #endif /* PG_API_VERSION == 2 */
412 #define pgSurface_Blit \
413 (*(int (*)(pgSurfaceObject *, pgSurfaceObject *, GAME_Rect *, GAME_Rect *, int)) \
414 PYGAMEAPI_GET_SLOT(surface, 2))
416 #define import_pygame_surface() \
418 IMPORT_PYGAME_MODULE(surface); \
419 if (PyErr_Occurred() != NULL) \
421 IMPORT_PYGAME_MODULE(surflock); \
424 #if PG_API_VERSION == 2
425 #define pgSurface_New(surface) pgSurface_New2((surface), 1)
426 #define pgSurface_NewNoOwn(surface) pgSurface_New2((surface), 0)
427 #endif /* PG_API_VERSION == 2 */
429 #endif /* ~PYGAMEAPI_SURFACE_INTERNAL */
433 * auto imported/initialized by surface
435 #ifndef PYGAMEAPI_SURFLOCK_INTERNAL
436 #define pgLifetimeLock_Type \
438 PYGAMEAPI_GET_SLOT(surflock, 0))
440 #define pgLifetimeLock_Check(x) \
441 ((x)->ob_type == &pgLifetimeLock_Type)
443 #define pgSurface_Prep(x) \
444 if ((x)->subsurface) \
445 (*(*(void (*)(pgSurfaceObject *)) \
446 PYGAMEAPI_GET_SLOT(surflock, 1)))(x)
448 #define pgSurface_Unprep(x) \
449 if ((x)->subsurface) \
450 (*(*(void (*)(pgSurfaceObject *)) \
451 PYGAMEAPI_GET_SLOT(surflock, 2)))(x)
453 #define pgSurface_Lock \
454 (*(int (*)(pgSurfaceObject *)) \
455 PYGAMEAPI_GET_SLOT(surflock, 3))
457 #define pgSurface_Unlock \
458 (*(int (*)(pgSurfaceObject *)) \
459 PYGAMEAPI_GET_SLOT(surflock, 4))
461 #define pgSurface_LockBy \
462 (*(int (*)(pgSurfaceObject *, PyObject *)) \
463 PYGAMEAPI_GET_SLOT(surflock, 5))
465 #define pgSurface_UnlockBy \
466 (*(int (*)(pgSurfaceObject *, PyObject *)) \
467 PYGAMEAPI_GET_SLOT(surflock, 6))
469 #define pgSurface_LockLifetime \
470 (*(PyObject * (*)(PyObject *, PyObject *)) \
471 PYGAMEAPI_GET_SLOT(surflock, 7))
477 typedef struct pgEventObject pgEventObject;
479 #ifndef PYGAMEAPI_EVENT_INTERNAL
480 #define pgEvent_Type \
482 PYGAMEAPI_GET_SLOT(event, 0))
484 #define pgEvent_Check(x) \
485 ((x)->ob_type == &pgEvent_Type)
487 #define pgEvent_New \
488 (*(PyObject * (*)(SDL_Event *)) \
489 PYGAMEAPI_GET_SLOT(event, 1))
491 #define pgEvent_New2 \
492 (*(PyObject * (*)(int, PyObject *)) \
493 PYGAMEAPI_GET_SLOT(event, 2))
495 #define pgEvent_FillUserEvent \
496 (*(int (*)(pgEventObject *, SDL_Event *)) \
497 PYGAMEAPI_GET_SLOT(event, 3))
499 #if PG_API_VERSION == 2
500 #define pg_EnableKeyRepeat \
501 (*(int (*)(int, int)) \
502 PYGAMEAPI_GET_SLOT(event, 4))
504 #define pg_GetKeyRepeat \
505 (*(void (*)(int *, int *)) \
506 PYGAMEAPI_GET_SLOT(event, 5))
507 #endif /* PG_API_VERSION == 2 */
509 #define import_pygame_event() IMPORT_PYGAME_MODULE(event)
514 * the rwobject are only needed for C side work, not accessable from python.
516 #ifndef PYGAMEAPI_RWOBJECT_INTERNAL
517 #define pgRWops_FromObject \
518 (*(SDL_RWops * (*)(PyObject *)) \
519 PYGAMEAPI_GET_SLOT(rwobject, 0))
521 #define pgRWops_IsFileObject \
522 (*(int (*)(SDL_RWops *)) \
523 PYGAMEAPI_GET_SLOT(rwobject, 1))
525 #define pg_EncodeFilePath \
526 (*(PyObject * (*)(PyObject *, PyObject *)) \
527 PYGAMEAPI_GET_SLOT(rwobject, 2))
529 #define pg_EncodeString \
530 (*(PyObject * (*)(PyObject *, const char *, const char *, PyObject *)) \
531 PYGAMEAPI_GET_SLOT(rwobject, 3))
533 #define pgRWops_FromFileObject \
534 (*(SDL_RWops * (*)(PyObject *)) \
535 PYGAMEAPI_GET_SLOT(rwobject, 4))
537 #define pgRWops_ReleaseObject \
538 (*(int (*)(SDL_RWops *)) \
539 PYGAMEAPI_GET_SLOT(rwobject, 5))
541 #define import_pygame_rwobject() IMPORT_PYGAME_MODULE(rwobject)
548 #ifndef PYGAMEAPI_PIXELARRAY_INTERNAL
549 #define PyPixelArray_Type \
551 PYGAMEAPI_GET_SLOT(pixelarray, 0))
553 #define PyPixelArray_Check(x) \
554 ((x)->ob_type == &PyPixelArray_Type)
555 #define PyPixelArray_New \
557 PYGAMEAPI_GET_SLOT(pixelarray, 1))
559 #define import_pygame_pixelarray() IMPORT_PYGAME_MODULE(pixelarray)
560 #endif /* PYGAMEAPI_PIXELARRAY_INTERNAL */
565 typedef struct pgColorObject pgColorObject;
567 #ifndef PYGAMEAPI_COLOR_INTERNAL
568 #define pgColor_Type (*(PyObject *) \
569 PYGAMEAPI_GET_SLOT(color, 0))
571 #define pgColor_Check(x) \
572 ((x)->ob_type == &pgColor_Type)
573 #define pgColor_New \
574 (*(PyObject * (*)(Uint8 *)) \
575 PYGAMEAPI_GET_SLOT(color, 1))
577 #define pgColor_NewLength \
578 (*(PyObject * (*)(Uint8 *, Uint8)) \
579 PYGAMEAPI_GET_SLOT(color, 3))
581 #define pg_RGBAFromColorObj \
582 (*(int (*)(PyObject *, Uint8 *)) \
583 PYGAMEAPI_GET_SLOT(color, 2))
585 #define pg_RGBAFromFuzzyColorObj \
586 (*(int (*)(PyObject *, Uint8 *)) \
587 PYGAMEAPI_GET_SLOT(color, 4))
589 #define pgColor_AsArray(x) (((pgColorObject *)x)->data)
590 #define pgColor_NumComponents(x) (((pgColorObject *)x)->len)
593 #define import_pygame_color() IMPORT_PYGAME_MODULE(color)
594 #endif /* PYGAMEAPI_COLOR_INTERNAL */
599 #ifndef PYGAMEAPI_MATH_INTERNAL
600 #define pgVector2_Check(x) \
601 ((x)->ob_type == (PyTypeObject *) \
602 PYGAMEAPI_GET_SLOT(math, 0))
604 #define pgVector3_Check(x) \
605 ((x)->ob_type == (PyTypeObject *) \
606 PYGAMEAPI_GET_SLOT(math, 1))
608 #define pgVector2_New \
610 PYGAMEAPI_GET_SLOT(PyGAME_C_API, 1))
612 #define import_pygame_math() IMPORT_PYGAME_MODULE(math)
613 #endif /* PYGAMEAPI_MATH_INTERNAL */
615 #define IMPORT_PYGAME_MODULE _IMPORT_PYGAME_MODULE
618 * base pygame API slots
619 * disable slots with NO_PYGAME_C_API
622 PYGAMEAPI_DEFINE_SLOTS(base);
623 PYGAMEAPI_DEFINE_SLOTS(rect);
624 PYGAMEAPI_DEFINE_SLOTS(cdrom);
625 PYGAMEAPI_DEFINE_SLOTS(joystick);
626 PYGAMEAPI_DEFINE_SLOTS(display);
627 PYGAMEAPI_DEFINE_SLOTS(surface);
628 PYGAMEAPI_DEFINE_SLOTS(surflock);
629 PYGAMEAPI_DEFINE_SLOTS(event);
630 PYGAMEAPI_DEFINE_SLOTS(rwobject);
631 PYGAMEAPI_DEFINE_SLOTS(pixelarray);
632 PYGAMEAPI_DEFINE_SLOTS(color);
633 PYGAMEAPI_DEFINE_SLOTS(math);
634 #else /* ~PYGAME_H */
635 PYGAMEAPI_EXTERN_SLOTS(base);
636 PYGAMEAPI_EXTERN_SLOTS(rect);
637 PYGAMEAPI_EXTERN_SLOTS(cdrom);
638 PYGAMEAPI_EXTERN_SLOTS(joystick);
639 PYGAMEAPI_EXTERN_SLOTS(display);
640 PYGAMEAPI_EXTERN_SLOTS(surface);
641 PYGAMEAPI_EXTERN_SLOTS(surflock);
642 PYGAMEAPI_EXTERN_SLOTS(event);
643 PYGAMEAPI_EXTERN_SLOTS(rwobject);
644 PYGAMEAPI_EXTERN_SLOTS(pixelarray);
645 PYGAMEAPI_EXTERN_SLOTS(color);
646 PYGAMEAPI_EXTERN_SLOTS(math);
647 #endif /* ~PYGAME_H */
649 #endif /* PYGAME_H */