-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy path_pygame.h
834 lines (667 loc) · 26.4 KB
/
_pygame.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
/*
pygame-ce - Python Game Library
Copyright (C) 2000-2001 Pete Shinners
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Pete Shinners
*/
#ifndef _PYGAME_H
#define _PYGAME_H
/** This header file includes all the definitions for the
** base pygame extensions. This header only requires
** Python includes (and SDL.h for functions that use SDL types).
** The reason for functions prototyped with #define's is
** to allow for maximum Python portability. It also uses
** Python as the runtime linker, which allows for late binding.
'' For more information on this style of development, read
** the Python docs on this subject.
** http://www.python.org/doc/current/ext/using-cobjects.html
**
** If using this to build your own derived extensions,
** you'll see that the functions available here are mainly
** used to help convert between python objects and SDL objects.
** Since this library doesn't add a lot of functionality to
** the SDL library, it doesn't need to offer a lot either.
**
** When initializing your extension module, you must manually
** import the modules you want to use. (this is the part about
** using python as the runtime linker). Each module has its
** own import_xxx() routine. You need to perform this import
** after you have initialized your own module, and before
** you call any routines from that module. Since every module
** in pygame does this, there are plenty of examples.
**
** The base module does include some useful conversion routines
** that you are free to use in your own extension.
**/
#include "pgplatform.h"
#include <Python.h>
/* version macros (defined since version 1.9.5) */
#ifndef PG_MAJOR_VERSION
#error PG_MAJOR_VERSION must be defined
#endif
#ifndef PG_MINOR_VERSION
#error PG_MINOR_VERSION must be defined
#endif
#ifndef PG_PATCH_VERSION
#error PG_PATCH_VERSION must be defined
#endif
#ifndef PG_VERSION_TAG
#error PG_VERSION_TAG must be defined
#endif
#define PG_VERSIONNUM(MAJOR, MINOR, PATCH) \
(1000 * (MAJOR) + 100 * (MINOR) + (PATCH))
#define PG_VERSION_ATLEAST(MAJOR, MINOR, PATCH) \
(PG_VERSIONNUM(PG_MAJOR_VERSION, PG_MINOR_VERSION, PG_PATCH_VERSION) >= \
PG_VERSIONNUM(MAJOR, MINOR, PATCH))
#include "pgcompat.h"
/* Flag indicating a pg_buffer; used for assertions within callbacks */
#ifndef NDEBUG
#define PyBUF_PYGAME 0x4000
#endif
#define PyBUF_HAS_FLAG(f, F) (((f) & (F)) == (F))
/* Array information exchange struct C type; inherits from Py_buffer
*
* Pygame uses its own Py_buffer derived C struct as an internal representation
* of an imported array buffer. The extended Py_buffer allows for a
* per-instance release callback,
*/
typedef void (*pybuffer_releaseproc)(Py_buffer *);
typedef struct pg_bufferinfo_s {
Py_buffer view;
PyObject *consumer; /* Input: Borrowed reference */
pybuffer_releaseproc release_buffer;
} pg_buffer;
#include "pgimport.h"
#include "../pgcompat_rect.h"
/*
* BASE module
*/
#ifndef PYGAMEAPI_BASE_INTERNAL
#define pgExc_SDLError ((PyObject *)PYGAMEAPI_GET_SLOT(base, 0))
#define pg_RegisterQuit \
(*(void (*)(void (*)(void)))PYGAMEAPI_GET_SLOT(base, 1))
#define pg_IntFromObj \
(*(int (*)(PyObject *, int *))PYGAMEAPI_GET_SLOT(base, 2))
#define pg_IntFromObjIndex \
(*(int (*)(PyObject *, int, int *))PYGAMEAPI_GET_SLOT(base, 3))
#define pg_TwoIntsFromObj \
(*(int (*)(PyObject *, int *, int *))PYGAMEAPI_GET_SLOT(base, 4))
#define pg_FloatFromObj \
(*(int (*)(PyObject *, float *))PYGAMEAPI_GET_SLOT(base, 5))
#define pg_FloatFromObjIndex \
(*(int (*)(PyObject *, int, float *))PYGAMEAPI_GET_SLOT(base, 6))
#define pg_TwoFloatsFromObj \
(*(int (*)(PyObject *, float *, float *))PYGAMEAPI_GET_SLOT(base, 7))
#define pg_DoubleFromObj \
(*(int (*)(PyObject *, double *))PYGAMEAPI_GET_SLOT(base, 24))
#define pg_TwoDoublesFromObj \
(*(int (*)(PyObject *, double *, double *))PYGAMEAPI_GET_SLOT(base, 25))
#define pg_TwoDoublesFromFastcallArgs \
(*(int (*)(PyObject *const *, Py_ssize_t, double *, \
double *))PYGAMEAPI_GET_SLOT(base, 26))
#define pg_UintFromObj \
(*(int (*)(PyObject *, Uint32 *))PYGAMEAPI_GET_SLOT(base, 8))
#define pg_UintFromObjIndex \
(*(int (*)(PyObject *, int, Uint32 *))PYGAMEAPI_GET_SLOT(base, 9))
#define pg_mod_autoinit (*(int (*)(const char *))PYGAMEAPI_GET_SLOT(base, 10))
#define pg_mod_autoquit (*(void (*)(const char *))PYGAMEAPI_GET_SLOT(base, 11))
#define pg_RGBAFromObj \
(*(int (*)(PyObject *, Uint8 *))PYGAMEAPI_GET_SLOT(base, 12))
#define pgBuffer_AsArrayInterface \
(*(PyObject * (*)(Py_buffer *)) PYGAMEAPI_GET_SLOT(base, 13))
#define pgBuffer_AsArrayStruct \
(*(PyObject * (*)(Py_buffer *)) PYGAMEAPI_GET_SLOT(base, 14))
#define pgObject_GetBuffer \
(*(int (*)(PyObject *, pg_buffer *, int))PYGAMEAPI_GET_SLOT(base, 15))
#define pgBuffer_Release (*(void (*)(pg_buffer *))PYGAMEAPI_GET_SLOT(base, 16))
#define pgDict_AsBuffer \
(*(int (*)(pg_buffer *, PyObject *, int))PYGAMEAPI_GET_SLOT(base, 17))
#define pgExc_BufferError ((PyObject *)PYGAMEAPI_GET_SLOT(base, 18))
#define pg_GetDefaultWindow \
(*(SDL_Window * (*)(void)) PYGAMEAPI_GET_SLOT(base, 19))
#define pg_SetDefaultWindow \
(*(void (*)(SDL_Window *))PYGAMEAPI_GET_SLOT(base, 20))
#define pg_GetDefaultWindowSurface \
(*(pgSurfaceObject * (*)(void)) PYGAMEAPI_GET_SLOT(base, 21))
#define pg_SetDefaultWindowSurface \
(*(void (*)(pgSurfaceObject *))PYGAMEAPI_GET_SLOT(base, 22))
#define pg_EnvShouldBlendAlphaSDL2 \
(*(int (*)(void))PYGAMEAPI_GET_SLOT(base, 23))
#define pg_GetDefaultConvertFormat \
(*(PG_PixelFormatEnum(*)(void))PYGAMEAPI_GET_SLOT(base, 27))
#define pg_SetDefaultConvertFormat \
(*(void (*)(Uint32))PYGAMEAPI_GET_SLOT(base, 28))
#define pgObject_getRectHelper \
(*(PyObject * (*)(PyObject *, PyObject *const *, Py_ssize_t, PyObject *, \
char *)) PYGAMEAPI_GET_SLOT(base, 29))
#define import_pygame_base() IMPORT_PYGAME_MODULE(base)
#endif /* ~PYGAMEAPI_BASE_INTERNAL */
typedef struct {
PyObject_HEAD SDL_Rect r;
PyObject *weakreflist;
} pgRectObject;
typedef struct {
PyObject_HEAD SDL_FRect r;
PyObject *weakreflist;
} pgFRectObject;
#define pgRect_AsRect(x) (((pgRectObject *)x)->r)
#define pgFRect_AsRect(x) (((pgFRectObject *)x)->r)
#ifndef PYGAMEAPI_RECT_INTERNAL
#define pgRect_Type (*(PyTypeObject *)PYGAMEAPI_GET_SLOT(rect, 0))
#define pgRect_Check(x) ((x)->ob_type == &pgRect_Type)
#define pgRect_New (*(PyObject * (*)(SDL_Rect *)) PYGAMEAPI_GET_SLOT(rect, 1))
#define pgRect_New4 \
(*(PyObject * (*)(int, int, int, int)) PYGAMEAPI_GET_SLOT(rect, 2))
#define pgRect_FromObject \
(*(SDL_Rect * (*)(PyObject *, SDL_Rect *)) PYGAMEAPI_GET_SLOT(rect, 3))
#define pgRect_Normalize (*(void (*)(SDL_Rect *))PYGAMEAPI_GET_SLOT(rect, 4))
#define pgFRect_Type (*(PyTypeObject *)PYGAMEAPI_GET_SLOT(rect, 5))
#define pgFRect_Check(x) ((x)->ob_type == &pgFRect_Type)
#define pgFRect_New \
(*(PyObject * (*)(SDL_FRect *)) PYGAMEAPI_GET_SLOT(rect, 6))
#define pgFRect_New4 \
(*(PyObject * (*)(float, float, float, float)) PYGAMEAPI_GET_SLOT(rect, 7))
#define pgFRect_FromObject \
(*(SDL_FRect * (*)(PyObject *, SDL_FRect *)) PYGAMEAPI_GET_SLOT(rect, 8))
#define pgFRect_Normalize (*(void (*)(SDL_FRect *))PYGAMEAPI_GET_SLOT(rect, 9))
#define import_pygame_rect() IMPORT_PYGAME_MODULE(rect)
#endif /* ~PYGAMEAPI_RECT_INTERNAL */
/*
* JOYSTICK module
*/
typedef struct pgJoystickObject {
PyObject_HEAD int id;
SDL_Joystick *joy;
/* Joysticks form an intrusive linked list.
*
* Note that we don't maintain refcounts for these so they are weakrefs
* from the Python side.
*/
struct pgJoystickObject *next;
struct pgJoystickObject *prev;
} pgJoystickObject;
#define pgJoystick_AsID(x) (((pgJoystickObject *)x)->id)
#define pgJoystick_AsSDL(x) (((pgJoystickObject *)x)->joy)
#ifndef PYGAMEAPI_JOYSTICK_INTERNAL
#define pgJoystick_Type (*(PyTypeObject *)PYGAMEAPI_GET_SLOT(joystick, 0))
#define pgJoystick_Check(x) ((x)->ob_type == &pgJoystick_Type)
#define pgJoystick_New (*(PyObject * (*)(int)) PYGAMEAPI_GET_SLOT(joystick, 1))
#define pgJoystick_GetDeviceIndexByInstanceID \
(*(int (*)(int))PYGAMEAPI_GET_SLOT(joystick, 2))
#define import_pygame_joystick() IMPORT_PYGAME_MODULE(joystick)
#endif
/*
* DISPLAY module
*/
typedef struct {
Uint32 hw_available : 1;
Uint32 wm_available : 1;
Uint32 blit_hw : 1;
Uint32 blit_hw_CC : 1;
Uint32 blit_hw_A : 1;
Uint32 blit_sw : 1;
Uint32 blit_sw_CC : 1;
Uint32 blit_sw_A : 1;
Uint32 blit_fill : 1;
Uint32 video_mem;
SDL_PixelFormat *vfmt;
SDL_PixelFormat vfmt_data;
int current_w;
int current_h;
} pg_VideoInfo;
typedef struct {
PyObject_HEAD pg_VideoInfo info;
} pgVidInfoObject;
#define pgVidInfo_AsVidInfo(x) (((pgVidInfoObject *)x)->info)
#ifndef PYGAMEAPI_DISPLAY_INTERNAL
#define pgVidInfo_Type (*(PyTypeObject *)PYGAMEAPI_GET_SLOT(display, 0))
#define pgVidInfo_Check(x) ((x)->ob_type == &pgVidInfo_Type)
#define pgVidInfo_New \
(*(PyObject * (*)(pg_VideoInfo *)) PYGAMEAPI_GET_SLOT(display, 1))
#define import_pygame_display() IMPORT_PYGAME_MODULE(display)
#endif /* ~PYGAMEAPI_DISPLAY_INTERNAL */
/*
* SURFACE module
*/
struct pgSubSurface_Data;
struct SDL_Surface;
typedef struct {
PyObject_HEAD struct SDL_Surface *surf;
int owner;
struct pgSubSurface_Data *subsurface; /* ptr to subsurface data (if a
* subsurface)*/
PyObject *weakreflist;
PyObject *locklist;
PyObject *dependency;
#if PY_VERSION_HEX >= 0x030D0000 && Py_GIL_DISABLED
PyMutex mutex;
unsigned int num_locks;
uint64_t locking_thread_id;
#endif
} pgSurfaceObject;
#define pgSurface_AsSurface(x) (((pgSurfaceObject *)x)->surf)
#if PY_VERSION_HEX >= 0x030D0000 && Py_GIL_DISABLED
#define LOCK_pgSurfaceObject(pgSurfacePtr) \
{ \
pgSurfaceObject *surfObj = (pgSurfaceObject *)pgSurfacePtr; \
uint64_t thread_id = PyThreadState_Get()->id; \
if (surfObj->num_locks) { \
if (thread_id == surfObj->locking_thread_id) { \
++(surfObj->num_locks); \
} \
else { \
PyMutex_Lock(&surfObj->mutex); \
surfObj->num_locks = 1U; \
surfObj->locking_thread_id = thread_id; \
} \
} \
else { \
PyMutex_Lock(&surfObj->mutex); \
surfObj->num_locks = 1U; \
surfObj->locking_thread_id = thread_id; \
} \
}
#else
#define LOCK_pgSurfaceObject(pgSurfacePtr)
#endif
#if PY_VERSION_HEX >= 0x030D0000 && Py_GIL_DISABLED
#define UNLOCK_pgSurfaceObject(pgSurfacePtr) \
{ \
pgSurfaceObject *surfObj = (pgSurfaceObject *)pgSurfacePtr; \
uint64_t thread_id = PyThreadState_Get()->id; \
if (surfObj->num_locks) { \
if (thread_id == surfObj->locking_thread_id) { \
if (surfObj->num_locks > 1U) { \
--(surfObj->num_locks); \
} \
else { \
surfObj->locking_thread_id = 0U; \
surfObj->num_locks = 0U; \
PyMutex_Unlock(&surfObj->mutex); \
} \
} \
} \
}
#else
#define UNLOCK_pgSurfaceObject(pgSurfacePtr)
#endif
#ifndef PYGAMEAPI_SURFACE_INTERNAL
#define pgSurface_Type (*(PyTypeObject *)PYGAMEAPI_GET_SLOT(surface, 0))
#define pgSurface_Check(x) \
(PyObject_IsInstance((x), (PyObject *)&pgSurface_Type))
#define pgSurface_New2 \
(*(pgSurfaceObject * (*)(SDL_Surface *, int)) \
PYGAMEAPI_GET_SLOT(surface, 1))
#define pgSurface_SetSurface \
(*(int (*)(pgSurfaceObject *, SDL_Surface *, int))PYGAMEAPI_GET_SLOT( \
surface, 3))
#define pgSurface_Blit \
(*(int (*)(pgSurfaceObject *, pgSurfaceObject *, SDL_Rect *, SDL_Rect *, \
int))PYGAMEAPI_GET_SLOT(surface, 2))
#define import_pygame_surface() \
do { \
IMPORT_PYGAME_MODULE(surface); \
if (PyErr_Occurred() != NULL) \
break; \
IMPORT_PYGAME_MODULE(surflock); \
} while (0)
#define pgSurface_New(surface) pgSurface_New2((surface), 1)
#define pgSurface_NewNoOwn(surface) pgSurface_New2((surface), 0)
#endif /* ~PYGAMEAPI_SURFACE_INTERNAL */
/*
* SURFLOCK module
* auto imported/initialized by surface
*/
#ifndef PYGAMEAPI_SURFLOCK_INTERNAL
#define pgSurface_Prep(x) \
if ((x)->subsurface) \
(*(*(void (*)(pgSurfaceObject *))PYGAMEAPI_GET_SLOT(surflock, 0)))(x)
#define pgSurface_Unprep(x) \
if ((x)->subsurface) \
(*(*(void (*)(pgSurfaceObject *))PYGAMEAPI_GET_SLOT(surflock, 1)))(x)
#define pgSurface_Lock \
(*(int (*)(pgSurfaceObject *))PYGAMEAPI_GET_SLOT(surflock, 2))
#define pgSurface_Unlock \
(*(int (*)(pgSurfaceObject *))PYGAMEAPI_GET_SLOT(surflock, 3))
#define pgSurface_LockBy \
(*(int (*)(pgSurfaceObject *, PyObject *))PYGAMEAPI_GET_SLOT(surflock, 4))
#define pgSurface_UnlockBy \
(*(int (*)(pgSurfaceObject *, PyObject *))PYGAMEAPI_GET_SLOT(surflock, 5))
#endif
/*
* EVENT module
*/
typedef struct pgEventObject pgEventObject;
#ifndef PYGAMEAPI_EVENT_INTERNAL
#define pgEvent_Type (*(PyTypeObject *)PYGAMEAPI_GET_SLOT(event, 0))
#define pgEvent_Check(x) ((x)->ob_type == &pgEvent_Type)
#define pgEvent_New \
(*(PyObject * (*)(SDL_Event *)) PYGAMEAPI_GET_SLOT(event, 1))
#define pg_post_event \
(*(int (*)(Uint32, PyObject *))PYGAMEAPI_GET_SLOT(event, 2))
#define pg_post_event_dictproxy \
(*(int (*)(Uint32, pgEventDictProxy *))PYGAMEAPI_GET_SLOT(event, 3))
#define pg_EnableKeyRepeat (*(int (*)(int, int))PYGAMEAPI_GET_SLOT(event, 4))
#define pg_GetKeyRepeat (*(void (*)(int *, int *))PYGAMEAPI_GET_SLOT(event, 5))
#define pgEvent_GetKeyDownInfo (*(char *(*)(void))PYGAMEAPI_GET_SLOT(event, 6))
#define pgEvent_GetKeyUpInfo (*(char *(*)(void))PYGAMEAPI_GET_SLOT(event, 7))
#define pgEvent_GetMouseButtonDownInfo \
(*(char *(*)(void))PYGAMEAPI_GET_SLOT(event, 8))
#define pgEvent_GetMouseButtonUpInfo \
(*(char *(*)(void))PYGAMEAPI_GET_SLOT(event, 9))
#define import_pygame_event() IMPORT_PYGAME_MODULE(event)
#endif
/*
* RWOBJECT module
* the rwobject are only needed for C side work, not accessible from python.
*/
#ifndef PYGAMEAPI_RWOBJECT_INTERNAL
#define pgRWops_FromObject \
(*(SDL_RWops * (*)(PyObject *, char **)) PYGAMEAPI_GET_SLOT(rwobject, 0))
#define pgRWops_IsFileObject \
(*(int (*)(SDL_RWops *))PYGAMEAPI_GET_SLOT(rwobject, 1))
#define pg_EncodeFilePath \
(*(PyObject * (*)(PyObject *, PyObject *)) PYGAMEAPI_GET_SLOT(rwobject, 2))
#define pg_EncodeString \
(*(PyObject * (*)(PyObject *, const char *, const char *, PyObject *)) \
PYGAMEAPI_GET_SLOT(rwobject, 3))
#define pgRWops_FromFileObject \
(*(SDL_RWops * (*)(PyObject *)) PYGAMEAPI_GET_SLOT(rwobject, 4))
#define import_pygame_rwobject() IMPORT_PYGAME_MODULE(rwobject)
#endif
/*
* PixelArray module
*/
#ifndef PYGAMEAPI_PIXELARRAY_INTERNAL
#define PyPixelArray_Type ((PyTypeObject *)PYGAMEAPI_GET_SLOT(pixelarray, 0))
#define PyPixelArray_Check(x) ((x)->ob_type == &PyPixelArray_Type)
#define PyPixelArray_New (*(PyObject * (*)) PYGAMEAPI_GET_SLOT(pixelarray, 1))
#define import_pygame_pixelarray() IMPORT_PYGAME_MODULE(pixelarray)
#endif /* PYGAMEAPI_PIXELARRAY_INTERNAL */
/*
* Color module
*/
typedef struct pgColorObject pgColorObject;
#ifndef PYGAMEAPI_COLOR_INTERNAL
#define pgColor_Type (*(PyObject *)PYGAMEAPI_GET_SLOT(color, 0))
#define pgColor_CheckExact(x) ((x)->ob_type == &pgColor_Type)
#define pgColor_New (*(PyObject * (*)(Uint8 *)) PYGAMEAPI_GET_SLOT(color, 1))
#define pgColor_NewLength \
(*(PyObject * (*)(Uint8 *, Uint8)) PYGAMEAPI_GET_SLOT(color, 3))
#define pg_RGBAFromObjEx \
(*(int (*)(PyObject *, Uint8 *, pgColorHandleFlags))PYGAMEAPI_GET_SLOT( \
color, 2))
#define pg_MappedColorFromObj \
(*(int (*)(PyObject *, SDL_Surface *, Uint32 *, \
pgColorHandleFlags))PYGAMEAPI_GET_SLOT(color, 4))
#define pgColor_AsArray(x) (((pgColorObject *)x)->data)
#define pgColor_NumComponents(x) (((pgColorObject *)x)->len)
#define import_pygame_color() IMPORT_PYGAME_MODULE(color)
#endif /* PYGAMEAPI_COLOR_INTERNAL */
/*
* Math module
*/
#ifndef PYGAMEAPI_MATH_INTERNAL
#define pgVector2_Check(x) \
((x)->ob_type == (PyTypeObject *)PYGAMEAPI_GET_SLOT(math, 0))
#define pgVector3_Check(x) \
((x)->ob_type == (PyTypeObject *)PYGAMEAPI_GET_SLOT(math, 1))
/*
#define pgVector2_New \
(*(PyObject*(*)) \
PYGAMEAPI_GET_SLOT(PyGAME_C_API, 1))
*/
#define import_pygame_math() IMPORT_PYGAME_MODULE(math)
#endif /* PYGAMEAPI_MATH_INTERNAL */
#ifndef PYGAMEAPI_GEOMETRY_INTERNAL
#define import_pygame_geometry() IMPORT_PYGAME_MODULE(geometry)
#endif /* ~PYGAMEAPI_GEOMETRY_INTERNAL */
/*
* Window module
*/
typedef struct {
PyObject_HEAD SDL_Window *_win;
SDL_bool _is_borrowed;
pgSurfaceObject *surf;
SDL_GLContext context;
} pgWindowObject;
#ifndef PYGAMEAPI_WINDOW_INTERNAL
#define pgWindow_Type (*(PyTypeObject *)PYGAMEAPI_GET_SLOT(window, 0))
#define pgWindow_Check(x) \
(PyObject_IsInstance((x), (PyObject *)&pgWindow_Type))
#define import_pygame_window() IMPORT_PYGAME_MODULE(window)
#endif
typedef struct pgTextureObject pgTextureObject;
/*
* Render module
*/
typedef struct {
PyObject_HEAD SDL_Renderer *renderer;
pgWindowObject *window;
pgTextureObject *target;
SDL_bool _is_borrowed;
} pgRendererObject;
struct pgTextureObject {
PyObject_HEAD SDL_Texture *texture;
pgRendererObject *renderer;
int width;
int height;
};
typedef struct {
PyObject_HEAD pgTextureObject *texture;
pgRectObject *srcrect;
pgColorObject *color;
float angle;
float alpha;
SDL_bool has_origin;
SDL_FPoint origin;
SDL_bool flip_x;
SDL_bool flip_y;
SDL_BlendMode blend_mode;
} pgImageObject;
#ifndef PYGAMEAPI_RENDER_INTERNAL
#define pgRenderer_Type (*(PyTypeObject *)PYGAMEAPI_GET_SLOT(_render, 0))
#define pgTexture_Type (*(PyTypeObject *)PYGAMEAPI_GET_SLOT(_render, 1))
#define pgImage_Type (*(PyTypeObject *)PYGAMEAPI_GET_SLOT(_render, 2))
#define pgRenderer_Check(x) \
(PyObject_IsInstance((x), (PyObject *)&pgRender_Type))
#define pgTexture_Check(x) \
(PyObject_IsInstance((x), (PyObject *)&pgTexture_Type))
#define pgImage_Check(x) (PyObject_IsInstance((x), (PyObject *)&pgImage_Type))
#define import_pygame_render() IMPORT_PYGAME_MODULE(_render)
#endif
#define IMPORT_PYGAME_MODULE _IMPORT_PYGAME_MODULE
/*
* base pygame API slots
* disable slots with NO_PYGAME_C_API
*/
#ifdef PYGAME_H
PYGAMEAPI_DEFINE_SLOTS(base);
PYGAMEAPI_DEFINE_SLOTS(rect);
PYGAMEAPI_DEFINE_SLOTS(joystick);
PYGAMEAPI_DEFINE_SLOTS(display);
PYGAMEAPI_DEFINE_SLOTS(surface);
PYGAMEAPI_DEFINE_SLOTS(surflock);
PYGAMEAPI_DEFINE_SLOTS(event);
PYGAMEAPI_DEFINE_SLOTS(rwobject);
PYGAMEAPI_DEFINE_SLOTS(pixelarray);
PYGAMEAPI_DEFINE_SLOTS(color);
PYGAMEAPI_DEFINE_SLOTS(math);
PYGAMEAPI_DEFINE_SLOTS(window);
PYGAMEAPI_DEFINE_SLOTS(_render);
PYGAMEAPI_DEFINE_SLOTS(geometry);
#else /* ~PYGAME_H */
PYGAMEAPI_EXTERN_SLOTS(base);
PYGAMEAPI_EXTERN_SLOTS(rect);
PYGAMEAPI_EXTERN_SLOTS(joystick);
PYGAMEAPI_EXTERN_SLOTS(display);
PYGAMEAPI_EXTERN_SLOTS(surface);
PYGAMEAPI_EXTERN_SLOTS(surflock);
PYGAMEAPI_EXTERN_SLOTS(event);
PYGAMEAPI_EXTERN_SLOTS(rwobject);
PYGAMEAPI_EXTERN_SLOTS(pixelarray);
PYGAMEAPI_EXTERN_SLOTS(color);
PYGAMEAPI_EXTERN_SLOTS(math);
PYGAMEAPI_EXTERN_SLOTS(window);
PYGAMEAPI_EXTERN_SLOTS(_render);
PYGAMEAPI_EXTERN_SLOTS(geometry);
#endif /* ~PYGAME_H */
#endif /* PYGAME_H */
/* Use the end of this file for other cross module inline utility
* functions There seems to be no good reason to stick to macro only
* functions in Python 3.
*/
#define SURF_INIT_CHECK(surf) \
{ \
if (!surf) { \
return RAISE(pgExc_SDLError, "Surface is not initialized"); \
} \
}
#if Py_GIL_DISABLED
#define DISABLE_GIL_SINGLE_INITIALIZATION(module, name) \
if (PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED) < 0) { \
Py_DECREF(module); \
return NULL; \
} \
printf("%s was compiled with GIL disabled\n", name);
#else
#define DISABLE_GIL_SINGLE_INITIALIZATION(module, name) \
printf("%s was compiled with GIL enabled\n", name);
#endif
static PG_INLINE PyObject *
pg_tuple_couple_from_values_int(int val1, int val2)
{
/* This function turns two input integers into a python tuple object.
* Currently, 5th November 2022, this is faster than using Py_BuildValue
* to do the same thing.
*/
PyObject *tup = PyTuple_New(2);
if (!tup) {
return NULL;
}
PyObject *tmp = PyLong_FromLong(val1);
if (!tmp) {
Py_DECREF(tup);
return NULL;
}
PyTuple_SET_ITEM(tup, 0, tmp);
tmp = PyLong_FromLong(val2);
if (!tmp) {
Py_DECREF(tup);
return NULL;
}
PyTuple_SET_ITEM(tup, 1, tmp);
return tup;
}
static PG_INLINE PyObject *
pg_tuple_triple_from_values_int(int val1, int val2, int val3)
{
/* This function turns three input integers into a python tuple object.
* Currently, 5th November 2022, this is faster than using Py_BuildValue
* to do the same thing.
*/
PyObject *tup = PyTuple_New(3);
if (!tup) {
return NULL;
}
PyObject *tmp = PyLong_FromLong(val1);
if (!tmp) {
Py_DECREF(tup);
return NULL;
}
PyTuple_SET_ITEM(tup, 0, tmp);
tmp = PyLong_FromLong(val2);
if (!tmp) {
Py_DECREF(tup);
return NULL;
}
PyTuple_SET_ITEM(tup, 1, tmp);
tmp = PyLong_FromLong(val3);
if (!tmp) {
Py_DECREF(tup);
return NULL;
}
PyTuple_SET_ITEM(tup, 2, tmp);
return tup;
}
static PG_INLINE PyObject *
pg_tuple_couple_from_values_double(double val1, double val2)
{
/* This function turns two input doubles into a python tuple object.
* Currently, 5th November 2022, this is faster than using Py_BuildValue
* to do the same thing.
*/
PyObject *tuple = PyTuple_New(2);
if (!tuple)
return NULL;
PyObject *tmp = PyFloat_FromDouble(val1);
if (!tmp) {
Py_DECREF(tuple);
return NULL;
}
PyTuple_SET_ITEM(tuple, 0, tmp);
tmp = PyFloat_FromDouble(val2);
if (!tmp) {
Py_DECREF(tuple);
return NULL;
}
PyTuple_SET_ITEM(tuple, 1, tmp);
return tuple;
}
static PG_INLINE PyObject *
pg_PointList_FromArrayDouble(double const *array, int arr_length)
{
if (arr_length % 2) {
return RAISE(PyExc_ValueError, "array length must be even");
}
int num_points = arr_length / 2;
PyObject *sequence = PyList_New(num_points);
if (!sequence) {
return NULL;
}
int i;
PyObject *point = NULL;
for (i = 0; i < num_points; i++) {
point =
pg_tuple_couple_from_values_double(array[i * 2], array[i * 2 + 1]);
if (!point) {
Py_DECREF(sequence);
return NULL;
}
PyList_SET_ITEM(sequence, i, point);
point = NULL;
}
return sequence;
}
#if PY_VERSION_HEX >= 0x030C0000
#define PG_DECLARE_EXCEPTION_SAVER static PyObject *__cached_exception = NULL;
#define PG_SAVE_EXCEPTION __cached_exception = PyErr_GetRaisedException();
#define PG_UNSAVE_EXCEPTION \
PyErr_SetRaisedException(__cached_exception); \
__cached_exception = NULL;
#else
#define PG_DECLARE_EXCEPTION_SAVER \
static PyObject *__cached_exception_type = NULL; \
static PyObject *__cached_exception_value = NULL; \
static PyObject *__cached_exception_traceback = NULL;
#define PG_SAVE_EXCEPTION \
PyErr_Fetch(&__cached_exception_type, &__cached_exception_value, \
&__cached_exception_traceback);
#define PG_UNSAVE_EXCEPTION \
PyErr_Restore(__cached_exception_type, __cached_exception_value, \
__cached_exception_traceback); \
__cached_exception_type = NULL; \
__cached_exception_value = NULL; \
__cached_exception_traceback = NULL;
#endif