_pygame.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /*
  2. pygame - Python Game Library
  3. Copyright (C) 2000-2001 Pete Shinners
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; if not, write to the Free
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. Pete Shinners
  16. pete@shinners.org
  17. */
  18. #ifndef _PYGAME_H
  19. #define _PYGAME_H
  20. /** This header file includes all the definitions for the
  21. ** base pygame extensions. This header only requires
  22. ** SDL and Python includes. The reason for functions
  23. ** prototyped with #define's is to allow for maximum
  24. ** python portability. It also uses python as the
  25. ** runtime linker, which allows for late binding. For more
  26. ** information on this style of development, read the Python
  27. ** docs on this subject.
  28. ** http://www.python.org/doc/current/ext/using-cobjects.html
  29. **
  30. ** If using this to build your own derived extensions,
  31. ** you'll see that the functions available here are mainly
  32. ** used to help convert between python objects and SDL objects.
  33. ** Since this library doesn't add a lot of functionality to
  34. ** the SDL libarary, it doesn't need to offer a lot either.
  35. **
  36. ** When initializing your extension module, you must manually
  37. ** import the modules you want to use. (this is the part about
  38. ** using python as the runtime linker). Each module has its
  39. ** own import_xxx() routine. You need to perform this import
  40. ** after you have initialized your own module, and before
  41. ** you call any routines from that module. Since every module
  42. ** in pygame does this, there are plenty of examples.
  43. **
  44. ** The base module does include some useful conversion routines
  45. ** that you are free to use in your own extension.
  46. **
  47. ** When making changes, it is very important to keep the
  48. ** FIRSTSLOT and NUMSLOT constants up to date for each
  49. ** section. Also be sure not to overlap any of the slots.
  50. ** When you do make a mistake with this, it will result
  51. ** is a dereferenced NULL pointer that is easier to diagnose
  52. ** than it could be :]
  53. **/
  54. #if defined(HAVE_SNPRINTF) /* defined in python.h (pyerrors.h) and SDL.h \
  55. (SDL_config.h) */
  56. #undef HAVE_SNPRINTF /* remove GCC redefine warning */
  57. #endif
  58. // This must be before all else
  59. #if defined(__SYMBIAN32__) && defined(OPENC)
  60. #include <sys/types.h>
  61. #if defined(__WINS__)
  62. void *
  63. _alloca(size_t size);
  64. #define alloca _alloca
  65. #endif
  66. #endif
  67. #define PG_STRINGIZE_HELPER(x) #x
  68. #define PG_STRINGIZE(x) PG_STRINGIZE_HELPER(x)
  69. #define PG_WARN(desc) message(__FILE__ "(" PG_STRINGIZE(__LINE__) "): WARNING: " #desc)
  70. /* This is unconditionally defined in Python.h */
  71. #if defined(_POSIX_C_SOURCE)
  72. #undef _POSIX_C_SOURCE
  73. #endif
  74. #include <Python.h>
  75. /* the version macros are defined since version 1.9.5 */
  76. #define PG_MAJOR_VERSION 1
  77. #define PG_MINOR_VERSION 9
  78. #define PG_PATCH_VERSION 6
  79. #define PG_VERSIONNUM(MAJOR, MINOR, PATCH) (1000*(MAJOR) + 100*(MINOR) + (PATCH))
  80. #define PG_VERSION_ATLEAST(MAJOR, MINOR, PATCH) \
  81. (PG_VERSIONNUM(PG_MAJOR_VERSION, PG_MINOR_VERSION, PG_PATCH_VERSION) >= \
  82. PG_VERSIONNUM(MAJOR, MINOR, PATCH))
  83. /* Cobjects vanish in Python 3.2; so we will code as though we use capsules */
  84. #if defined(Py_CAPSULE_H)
  85. #define PG_HAVE_CAPSULE 1
  86. #else
  87. #define PG_HAVE_CAPSULE 0
  88. #endif
  89. #if defined(Py_COBJECT_H)
  90. #define PG_HAVE_COBJECT 1
  91. #else
  92. #define PG_HAVE_COBJECT 0
  93. #endif
  94. #if !PG_HAVE_CAPSULE
  95. #define PyCapsule_New(ptr, n, dfn) PyCObject_FromVoidPtr(ptr, dfn)
  96. #define PyCapsule_GetPointer(obj, n) PyCObject_AsVoidPtr(obj)
  97. #define PyCapsule_CheckExact(obj) PyCObject_Check(obj)
  98. #endif
  99. /* Pygame uses Py_buffer (PEP 3118) to exchange array information internally;
  100. * define here as needed.
  101. */
  102. #if !defined(PyBUF_SIMPLE)
  103. typedef struct bufferinfo {
  104. void *buf;
  105. PyObject *obj;
  106. Py_ssize_t len;
  107. Py_ssize_t itemsize;
  108. int readonly;
  109. int ndim;
  110. char *format;
  111. Py_ssize_t *shape;
  112. Py_ssize_t *strides;
  113. Py_ssize_t *suboffsets;
  114. void *internal;
  115. } Py_buffer;
  116. /* Flags for getting buffers */
  117. #define PyBUF_SIMPLE 0
  118. #define PyBUF_WRITABLE 0x0001
  119. /* we used to include an E, backwards compatible alias */
  120. #define PyBUF_WRITEABLE PyBUF_WRITABLE
  121. #define PyBUF_FORMAT 0x0004
  122. #define PyBUF_ND 0x0008
  123. #define PyBUF_STRIDES (0x0010 | PyBUF_ND)
  124. #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES)
  125. #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES)
  126. #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES)
  127. #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES)
  128. #define PyBUF_CONTIG (PyBUF_ND | PyBUF_WRITABLE)
  129. #define PyBUF_CONTIG_RO (PyBUF_ND)
  130. #define PyBUF_STRIDED (PyBUF_STRIDES | PyBUF_WRITABLE)
  131. #define PyBUF_STRIDED_RO (PyBUF_STRIDES)
  132. #define PyBUF_RECORDS (PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT)
  133. #define PyBUF_RECORDS_RO (PyBUF_STRIDES | PyBUF_FORMAT)
  134. #define PyBUF_FULL (PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT)
  135. #define PyBUF_FULL_RO (PyBUF_INDIRECT | PyBUF_FORMAT)
  136. #define PyBUF_READ 0x100
  137. #define PyBUF_WRITE 0x200
  138. #define PyBUF_SHADOW 0x400
  139. typedef int (*getbufferproc)(PyObject *, Py_buffer *, int);
  140. typedef void (*releasebufferproc)(Py_buffer *);
  141. #endif /* #if !defined(PyBUF_SIMPLE) */
  142. /* Flag indicating a pg_buffer; used for assertions within callbacks */
  143. #ifndef NDEBUG
  144. #define PyBUF_PYGAME 0x4000
  145. #endif
  146. #define PyBUF_HAS_FLAG(f, F) (((f) & (F)) == (F))
  147. /* Array information exchange struct C type; inherits from Py_buffer
  148. *
  149. * Pygame uses its own Py_buffer derived C struct as an internal representation
  150. * of an imported array buffer. The extended Py_buffer allows for a
  151. * per-instance release callback,
  152. */
  153. typedef void (*pybuffer_releaseproc)(Py_buffer *);
  154. typedef struct pg_bufferinfo_s {
  155. Py_buffer view;
  156. PyObject *consumer; /* Input: Borrowed reference */
  157. pybuffer_releaseproc release_buffer;
  158. } pg_buffer;
  159. /* Operating system specific adjustments
  160. */
  161. // No signal()
  162. #if defined(__SYMBIAN32__) && defined(HAVE_SIGNAL_H)
  163. #undef HAVE_SIGNAL_H
  164. #endif
  165. #if defined(HAVE_SNPRINTF)
  166. #undef HAVE_SNPRINTF
  167. #endif
  168. #ifdef MS_WIN32 /*Python gives us MS_WIN32, SDL needs just WIN32*/
  169. #ifndef WIN32
  170. #define WIN32
  171. #endif
  172. #endif
  173. /// Prefix when initializing module
  174. #define MODPREFIX ""
  175. /// Prefix when importing module
  176. #define IMPPREFIX "pygame."
  177. #ifdef __SYMBIAN32__
  178. #undef MODPREFIX
  179. #undef IMPPREFIX
  180. // On Symbian there is no pygame package. The extensions are built-in or in
  181. // sys\bin.
  182. #define MODPREFIX "pygame_"
  183. #define IMPPREFIX "pygame_"
  184. #endif
  185. #include <SDL.h>
  186. /* Pygame's SDL version macros:
  187. * IS_SDLv1 is 1 if SDL 1.x.x, 0 otherwise
  188. * IS_SDLv2 is 1 if at least SDL 2.0.0, 0 otherwise
  189. */
  190. #if (SDL_VERSION_ATLEAST(2, 0, 0))
  191. #define IS_SDLv1 0
  192. #define IS_SDLv2 1
  193. #else
  194. #define IS_SDLv1 1
  195. #define IS_SDLv2 0
  196. #endif
  197. /*#if IS_SDLv1 && PG_MAJOR_VERSION >= 2
  198. #error pygame 2 requires SDL 2
  199. #endif*/
  200. #if IS_SDLv2
  201. /* SDL 1.2 constants removed from SDL 2 */
  202. typedef enum {
  203. SDL_HWSURFACE = 0,
  204. SDL_RESIZABLE = SDL_WINDOW_RESIZABLE,
  205. SDL_ASYNCBLIT = 0,
  206. SDL_OPENGL = SDL_WINDOW_OPENGL,
  207. SDL_OPENGLBLIT = 0,
  208. SDL_ANYFORMAT = 0,
  209. SDL_HWPALETTE = 0,
  210. SDL_DOUBLEBUF = 0,
  211. SDL_FULLSCREEN = SDL_WINDOW_FULLSCREEN,
  212. SDL_HWACCEL = 0,
  213. SDL_SRCCOLORKEY = 0,
  214. SDL_RLEACCELOK = 0,
  215. SDL_SRCALPHA = 0,
  216. SDL_NOFRAME = SDL_WINDOW_BORDERLESS,
  217. SDL_GL_SWAP_CONTROL = 0,
  218. TIMER_RESOLUTION = 0
  219. } PygameVideoFlags;
  220. /* the wheel button constants were removed from SDL 2 */
  221. typedef enum {
  222. PGM_BUTTON_LEFT = SDL_BUTTON_LEFT,
  223. PGM_BUTTON_RIGHT = SDL_BUTTON_RIGHT,
  224. PGM_BUTTON_MIDDLE = SDL_BUTTON_MIDDLE,
  225. PGM_BUTTON_WHEELUP = 4,
  226. PGM_BUTTON_WHEELDOWN = 5,
  227. PGM_BUTTON_X1 = SDL_BUTTON_X1 + 2,
  228. PGM_BUTTON_X2 = SDL_BUTTON_X2 + 2,
  229. PGM_BUTTON_KEEP = 0x80
  230. } PygameMouseFlags;
  231. typedef enum {
  232. SDL_NOEVENT = 0,
  233. /* SDL 1.2 allowed for 8 user defined events. */
  234. SDL_NUMEVENTS = SDL_USEREVENT + 8,
  235. SDL_ACTIVEEVENT = SDL_NUMEVENTS,
  236. PGE_EVENTBEGIN = SDL_NUMEVENTS,
  237. SDL_VIDEORESIZE,
  238. SDL_VIDEOEXPOSE,
  239. PGE_KEYREPEAT,
  240. PGE_EVENTEND
  241. } PygameEventCode;
  242. #define PGE_NUMEVENTS (PGE_EVENTEND - PGE_EVENTBEGIN)
  243. typedef enum {
  244. SDL_APPFOCUSMOUSE,
  245. SDL_APPINPUTFOCUS,
  246. SDL_APPACTIVE
  247. } PygameAppCode;
  248. /* Surface flags: based on SDL 1.2 flags */
  249. typedef enum {
  250. PGS_SWSURFACE = 0x00000000,
  251. PGS_HWSURFACE = 0x00000001,
  252. PGS_ASYNCBLIT = 0x00000004,
  253. PGS_ANYFORMAT = 0x10000000,
  254. PGS_HWPALETTE = 0x20000000,
  255. PGS_DOUBLEBUF = 0x40000000,
  256. PGS_FULLSCREEN = 0x80000000,
  257. PGS_OPENGL = 0x00000002,
  258. PGS_OPENGLBLIT = 0x0000000A,
  259. PGS_RESIZABLE = 0x00000010,
  260. PGS_NOFRAME = 0x00000020,
  261. PGS_SHOWN = 0x00000040, /* Added from SDL 2 */
  262. PGS_HIDDEN = 0x00000080, /* Added from SDL 2 */
  263. PGS_HWACCEL = 0x00000100,
  264. PGS_SRCCOLORKEY = 0x00001000,
  265. PGS_RLEACCELOK = 0x00002000,
  266. PGS_RLEACCEL = 0x00004000,
  267. PGS_SRCALPHA = 0x00010000,
  268. PGS_PREALLOC = 0x01000000
  269. } PygameSurfaceFlags;
  270. typedef struct {
  271. Uint32 hw_available:1;
  272. Uint32 wm_available:1;
  273. Uint32 blit_hw:1;
  274. Uint32 blit_hw_CC:1;
  275. Uint32 blit_hw_A:1;
  276. Uint32 blit_sw:1;
  277. Uint32 blit_sw_CC:1;
  278. Uint32 blit_sw_A:1;
  279. Uint32 blit_fill:1;
  280. Uint32 video_mem;
  281. SDL_PixelFormat *vfmt;
  282. SDL_PixelFormat vfmt_data;
  283. int current_w;
  284. int current_h;
  285. } pg_VideoInfo;
  286. #endif /* IS_SDLv2 */
  287. /* macros used throughout the source */
  288. #define RAISE(x, y) (PyErr_SetString((x), (y)), (PyObject *)NULL)
  289. #ifdef WITH_THREAD
  290. #define PG_CHECK_THREADS() (1)
  291. #else /* ~WITH_THREAD */
  292. #define PG_CHECK_THREADS() \
  293. (RAISE(PyExc_NotImplementedError, \
  294. "Python built without thread support"))
  295. #endif /* ~WITH_THREAD */
  296. #define PyType_Init(x) (((x).ob_type) = &PyType_Type)
  297. #define PYGAMEAPI_LOCAL_ENTRY "_PYGAME_C_API"
  298. #ifndef MIN
  299. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  300. #endif
  301. #ifndef MAX
  302. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  303. #endif
  304. #ifndef ABS
  305. #define ABS(a) (((a) < 0) ? -(a) : (a))
  306. #endif
  307. /* test sdl initializations */
  308. #define VIDEO_INIT_CHECK() \
  309. if (!SDL_WasInit(SDL_INIT_VIDEO)) \
  310. return RAISE(pgExc_SDLError, "video system not initialized")
  311. #define CDROM_INIT_CHECK() \
  312. if (!SDL_WasInit(SDL_INIT_CDROM)) \
  313. return RAISE(pgExc_SDLError, "cdrom system not initialized")
  314. #define JOYSTICK_INIT_CHECK() \
  315. if (!SDL_WasInit(SDL_INIT_JOYSTICK)) \
  316. return RAISE(pgExc_SDLError, "joystick system not initialized")
  317. /* BASE */
  318. #define VIEW_CONTIGUOUS 1
  319. #define VIEW_C_ORDER 2
  320. #define VIEW_F_ORDER 4
  321. #define PYGAMEAPI_BASE_FIRSTSLOT 0
  322. #if IS_SDLv1
  323. #define PYGAMEAPI_BASE_NUMSLOTS 19
  324. #else /* IS_SDLv2 */
  325. #define PYGAMEAPI_BASE_NUMSLOTS 23
  326. #endif /* IS_SDLv2 */
  327. #ifndef PYGAMEAPI_BASE_INTERNAL
  328. #define pgExc_SDLError ((PyObject *)PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT])
  329. #define pg_RegisterQuit \
  330. (*(void (*)(void (*)(void)))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 1])
  331. #define pg_IntFromObj \
  332. (*(int (*)(PyObject *, int *))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 2])
  333. #define pg_IntFromObjIndex \
  334. (*(int (*)(PyObject *, int, \
  335. int *))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 3])
  336. #define pg_TwoIntsFromObj \
  337. (*(int (*)(PyObject *, int *, \
  338. int *))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 4])
  339. #define pg_FloatFromObj \
  340. (*(int (*)(PyObject *, float *))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 5])
  341. #define pg_FloatFromObjIndex \
  342. (*(int (*)(PyObject *, int, \
  343. float *))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 6])
  344. #define pg_TwoFloatsFromObj \
  345. (*(int (*)(PyObject *, float *, \
  346. float *))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 7])
  347. #define pg_UintFromObj \
  348. (*(int (*)(PyObject *, \
  349. Uint32 *))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 8])
  350. #define pg_UintFromObjIndex \
  351. (*(int (*)(PyObject *, int, \
  352. Uint32 *))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 9])
  353. #define pgVideo_AutoQuit \
  354. (*(void (*)(void))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 10])
  355. #define pgVideo_AutoInit \
  356. (*(int (*)(void))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 11])
  357. #define pg_RGBAFromObj \
  358. (*(int (*)(PyObject *, \
  359. Uint8 *))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 12])
  360. #define pgBuffer_AsArrayInterface \
  361. (*(PyObject * (*)(Py_buffer *)) \
  362. PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 13])
  363. #define pgBuffer_AsArrayStruct \
  364. (*(PyObject * (*)(Py_buffer *)) \
  365. PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 14])
  366. #define pgObject_GetBuffer \
  367. (*(int (*)(PyObject *, pg_buffer *, \
  368. int))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 15])
  369. #define pgBuffer_Release \
  370. (*(void (*)(pg_buffer *))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 16])
  371. #define pgDict_AsBuffer \
  372. (*(int (*)(pg_buffer *, PyObject *, \
  373. int))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 17])
  374. #define pgExc_BufferError \
  375. ((PyObject *)PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 18])
  376. #if IS_SDLv2
  377. #define pg_GetDefaultWindow \
  378. (*(SDL_Window * (*)(void)) PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 19])
  379. #define pg_SetDefaultWindow \
  380. (*(void (*)(SDL_Window *))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 20])
  381. #define pg_GetDefaultWindowSurface \
  382. (*(PyObject * (*)(void)) PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 21])
  383. #define pg_SetDefaultWindowSurface \
  384. (*(void (*)(PyObject *))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 22])
  385. #endif /* IS_SDLv2 */
  386. #define import_pygame_base() IMPORT_PYGAME_MODULE(base, BASE)
  387. #endif
  388. /* RECT */
  389. #define PYGAMEAPI_RECT_FIRSTSLOT \
  390. (PYGAMEAPI_BASE_FIRSTSLOT + PYGAMEAPI_BASE_NUMSLOTS)
  391. #define PYGAMEAPI_RECT_NUMSLOTS 4
  392. #if IS_SDLv1
  393. typedef struct {
  394. int x, y;
  395. int w, h;
  396. } GAME_Rect;
  397. #else
  398. typedef SDL_Rect GAME_Rect;
  399. #endif
  400. typedef struct {
  401. PyObject_HEAD GAME_Rect r;
  402. PyObject *weakreflist;
  403. } pgRectObject;
  404. #define pgRect_AsRect(x) (((pgRectObject *)x)->r)
  405. #ifndef PYGAMEAPI_RECT_INTERNAL
  406. #define pgRect_Check(x) \
  407. ((x)->ob_type == \
  408. (PyTypeObject *)PyGAME_C_API[PYGAMEAPI_RECT_FIRSTSLOT + 0])
  409. #define pgRect_Type \
  410. (*(PyTypeObject *)PyGAME_C_API[PYGAMEAPI_RECT_FIRSTSLOT + 0])
  411. #define pgRect_New \
  412. (*(PyObject * (*)(SDL_Rect *)) PyGAME_C_API[PYGAMEAPI_RECT_FIRSTSLOT + 1])
  413. #define pgRect_New4 \
  414. (*(PyObject * (*)(int, int, int, int)) \
  415. PyGAME_C_API[PYGAMEAPI_RECT_FIRSTSLOT + 2])
  416. #define pgRect_FromObject \
  417. (*(GAME_Rect * (*)(PyObject *, GAME_Rect *)) \
  418. PyGAME_C_API[PYGAMEAPI_RECT_FIRSTSLOT + 3])
  419. #define import_pygame_rect() IMPORT_PYGAME_MODULE(rect, RECT)
  420. #endif
  421. /* CDROM */
  422. #define PYGAMEAPI_CDROM_FIRSTSLOT \
  423. (PYGAMEAPI_RECT_FIRSTSLOT + PYGAMEAPI_RECT_NUMSLOTS)
  424. #define PYGAMEAPI_CDROM_NUMSLOTS 2
  425. typedef struct {
  426. PyObject_HEAD int id;
  427. } pgCDObject;
  428. #define pgCD_AsID(x) (((pgCDObject *)x)->id)
  429. #ifndef PYGAMEAPI_CDROM_INTERNAL
  430. #define pgCD_Check(x) \
  431. ((x)->ob_type == \
  432. (PyTypeObject *)PyGAME_C_API[PYGAMEAPI_CDROM_FIRSTSLOT + 0])
  433. #define pgCD_Type \
  434. (*(PyTypeObject *)PyGAME_C_API[PYGAMEAPI_CDROM_FIRSTSLOT + 0])
  435. #define pgCD_New \
  436. (*(PyObject * (*)(int)) PyGAME_C_API[PYGAMEAPI_CDROM_FIRSTSLOT + 1])
  437. #define import_pygame_cd() IMPORT_PYGAME_MODULE(cdrom, CDROM)
  438. #endif
  439. /* JOYSTICK */
  440. #define PYGAMEAPI_JOYSTICK_FIRSTSLOT \
  441. (PYGAMEAPI_CDROM_FIRSTSLOT + PYGAMEAPI_CDROM_NUMSLOTS)
  442. #define PYGAMEAPI_JOYSTICK_NUMSLOTS 2
  443. typedef struct {
  444. PyObject_HEAD int id;
  445. } pgJoystickObject;
  446. #define pgJoystick_AsID(x) (((pgJoystickObject *)x)->id)
  447. #ifndef PYGAMEAPI_JOYSTICK_INTERNAL
  448. #define pgJoystick_Check(x) \
  449. ((x)->ob_type == \
  450. (PyTypeObject *)PyGAME_C_API[PYGAMEAPI_JOYSTICK_FIRSTSLOT + 0])
  451. #define pgJoystick_Type \
  452. (*(PyTypeObject *)PyGAME_C_API[PYGAMEAPI_JOYSTICK_FIRSTSLOT + 0])
  453. #define pgJoystick_New \
  454. (*(PyObject * (*)(int)) PyGAME_C_API[PYGAMEAPI_JOYSTICK_FIRSTSLOT + 1])
  455. #define import_pygame_joystick() IMPORT_PYGAME_MODULE(joystick, JOYSTICK)
  456. #endif
  457. /* DISPLAY */
  458. #define PYGAMEAPI_DISPLAY_FIRSTSLOT \
  459. (PYGAMEAPI_JOYSTICK_FIRSTSLOT + PYGAMEAPI_JOYSTICK_NUMSLOTS)
  460. #define PYGAMEAPI_DISPLAY_NUMSLOTS 2
  461. typedef struct {
  462. #if IS_SDLv1
  463. PyObject_HEAD SDL_VideoInfo info;
  464. #else
  465. PyObject_HEAD pg_VideoInfo info;
  466. #endif
  467. } pgVidInfoObject;
  468. #define pgVidInfo_AsVidInfo(x) (((pgVidInfoObject *)x)->info)
  469. #ifndef PYGAMEAPI_DISPLAY_INTERNAL
  470. #define pgVidInfo_Check(x) \
  471. ((x)->ob_type == \
  472. (PyTypeObject *)PyGAME_C_API[PYGAMEAPI_DISPLAY_FIRSTSLOT + 0])
  473. #define pgVidInfo_Type \
  474. (*(PyTypeObject *)PyGAME_C_API[PYGAMEAPI_DISPLAY_FIRSTSLOT + 0])
  475. #if IS_SDLv1
  476. #define pgVidInfo_New \
  477. (*(PyObject * (*)(SDL_VideoInfo *)) \
  478. PyGAME_C_API[PYGAMEAPI_DISPLAY_FIRSTSLOT + 1])
  479. #else
  480. #define pgVidInfo_New \
  481. (*(PyObject * (*)(pg_VideoInfo *)) \
  482. PyGAME_C_API[PYGAMEAPI_DISPLAY_FIRSTSLOT + 1])
  483. #endif
  484. #define import_pygame_display() IMPORT_PYGAME_MODULE(display, DISPLAY)
  485. #endif
  486. /* SURFACE */
  487. #define PYGAMEAPI_SURFACE_FIRSTSLOT \
  488. (PYGAMEAPI_DISPLAY_FIRSTSLOT + PYGAMEAPI_DISPLAY_NUMSLOTS)
  489. #define PYGAMEAPI_SURFACE_NUMSLOTS 3
  490. typedef struct {
  491. PyObject_HEAD SDL_Surface *surf;
  492. #if IS_SDLv2
  493. int owner;
  494. #endif /* IS_SDLv2 */
  495. struct pgSubSurface_Data *subsurface; /*ptr to subsurface data (if a
  496. * subsurface)*/
  497. PyObject *weakreflist;
  498. PyObject *locklist;
  499. PyObject *dependency;
  500. } pgSurfaceObject;
  501. #define pgSurface_AsSurface(x) (((pgSurfaceObject *)x)->surf)
  502. #ifndef PYGAMEAPI_SURFACE_INTERNAL
  503. #define pgSurface_Check(x) \
  504. (PyObject_IsInstance((x), \
  505. (PyObject *)PyGAME_C_API[PYGAMEAPI_SURFACE_FIRSTSLOT + 0]))
  506. #define pgSurface_Type \
  507. (*(PyTypeObject *)PyGAME_C_API[PYGAMEAPI_SURFACE_FIRSTSLOT + 0])
  508. #if IS_SDLv1
  509. #define pgSurface_New \
  510. (*(PyObject * (*)(SDL_Surface *)) \
  511. PyGAME_C_API[PYGAMEAPI_SURFACE_FIRSTSLOT + 1])
  512. #else /* IS_SDLv2 */
  513. #define pgSurface_New2 \
  514. (*(PyObject * (*)(SDL_Surface *, int)) \
  515. PyGAME_C_API[PYGAMEAPI_SURFACE_FIRSTSLOT + 1])
  516. #endif /* IS_SDLv2 */
  517. #define pgSurface_Blit \
  518. (*(int (*)(PyObject *, PyObject *, SDL_Rect *, SDL_Rect *, \
  519. int))PyGAME_C_API[PYGAMEAPI_SURFACE_FIRSTSLOT + 2])
  520. #define import_pygame_surface() \
  521. do { \
  522. IMPORT_PYGAME_MODULE(surface, SURFACE); \
  523. if (PyErr_Occurred() != NULL) \
  524. break; \
  525. IMPORT_PYGAME_MODULE(surflock, SURFLOCK); \
  526. } while (0)
  527. #if IS_SDLv2
  528. #define pgSurface_New(surface) pgSurface_New2((surface), 1)
  529. #define pgSurface_NewNoOwn(surface) pgSurface_New2((surface), 0)
  530. #endif /* IS_SDLv2 */
  531. #endif
  532. /* SURFLOCK */ /*auto import/init by surface*/
  533. #define PYGAMEAPI_SURFLOCK_FIRSTSLOT \
  534. (PYGAMEAPI_SURFACE_FIRSTSLOT + PYGAMEAPI_SURFACE_NUMSLOTS)
  535. #define PYGAMEAPI_SURFLOCK_NUMSLOTS 8
  536. struct pgSubSurface_Data {
  537. PyObject *owner;
  538. int pixeloffset;
  539. int offsetx, offsety;
  540. };
  541. typedef struct {
  542. PyObject_HEAD PyObject *surface;
  543. PyObject *lockobj;
  544. PyObject *weakrefs;
  545. } pgLifetimeLockObject;
  546. #ifndef PYGAMEAPI_SURFLOCK_INTERNAL
  547. #define pgLifetimeLock_Check(x) \
  548. ((x)->ob_type == \
  549. (PyTypeObject *)PyGAME_C_API[PYGAMEAPI_SURFLOCK_FIRSTSLOT + 0])
  550. #define pgSurface_Prep(x) \
  551. if (((pgSurfaceObject *)x)->subsurface) \
  552. (*(*(void (*)( \
  553. PyObject *))PyGAME_C_API[PYGAMEAPI_SURFLOCK_FIRSTSLOT + 1]))(x)
  554. #define pgSurface_Unprep(x) \
  555. if (((pgSurfaceObject *)x)->subsurface) \
  556. (*(*(void (*)( \
  557. PyObject *))PyGAME_C_API[PYGAMEAPI_SURFLOCK_FIRSTSLOT + 2]))(x)
  558. #define pgSurface_Lock \
  559. (*(int (*)(PyObject *))PyGAME_C_API[PYGAMEAPI_SURFLOCK_FIRSTSLOT + 3])
  560. #define pgSurface_Unlock \
  561. (*(int (*)(PyObject *))PyGAME_C_API[PYGAMEAPI_SURFLOCK_FIRSTSLOT + 4])
  562. #define pgSurface_LockBy \
  563. (*(int (*)(PyObject *, \
  564. PyObject *))PyGAME_C_API[PYGAMEAPI_SURFLOCK_FIRSTSLOT + 5])
  565. #define pgSurface_UnlockBy \
  566. (*(int (*)(PyObject *, \
  567. PyObject *))PyGAME_C_API[PYGAMEAPI_SURFLOCK_FIRSTSLOT + 6])
  568. #define pgSurface_LockLifetime \
  569. (*(PyObject * (*)(PyObject *, PyObject *)) \
  570. PyGAME_C_API[PYGAMEAPI_SURFLOCK_FIRSTSLOT + 7])
  571. #endif
  572. /* EVENT */
  573. #define PYGAMEAPI_EVENT_FIRSTSLOT \
  574. (PYGAMEAPI_SURFLOCK_FIRSTSLOT + PYGAMEAPI_SURFLOCK_NUMSLOTS)
  575. #if IS_SDLv1
  576. #define PYGAMEAPI_EVENT_NUMSLOTS 4
  577. #else /* IS_SDLv2 */
  578. #define PYGAMEAPI_EVENT_NUMSLOTS 6
  579. #endif /* IS_SDLv2 */
  580. typedef struct {
  581. PyObject_HEAD int type;
  582. PyObject *dict;
  583. } pgEventObject;
  584. #ifndef PYGAMEAPI_EVENT_INTERNAL
  585. #define pgEvent_Check(x) \
  586. ((x)->ob_type == \
  587. (PyTypeObject *)PyGAME_C_API[PYGAMEAPI_EVENT_FIRSTSLOT + 0])
  588. #define pgEvent_Type \
  589. (*(PyTypeObject *)PyGAME_C_API[PYGAMEAPI_EVENT_FIRSTSLOT + 0])
  590. #define pgEvent_New \
  591. (*(PyObject * (*)(SDL_Event *)) \
  592. PyGAME_C_API[PYGAMEAPI_EVENT_FIRSTSLOT + 1])
  593. #define pgEvent_New2 \
  594. (*(PyObject * (*)(int, PyObject *)) \
  595. PyGAME_C_API[PYGAMEAPI_EVENT_FIRSTSLOT + 2])
  596. #define pgEvent_FillUserEvent \
  597. (*(int (*)(pgEventObject *, \
  598. SDL_Event *))PyGAME_C_API[PYGAMEAPI_EVENT_FIRSTSLOT + 3])
  599. #if IS_SDLv2
  600. #define pg_EnableKeyRepeat \
  601. (*(int (*)(int, int))PyGAME_C_API[PYGAMEAPI_EVENT_FIRSTSLOT + 4])
  602. #define pg_GetKeyRepeat \
  603. (*(void (*)(int *, int *))PyGAME_C_API[PYGAMEAPI_EVENT_FIRSTSLOT + 5])
  604. #endif /* IS_SDLv2 */
  605. #define import_pygame_event() IMPORT_PYGAME_MODULE(event, EVENT)
  606. #endif
  607. /* RWOBJECT */
  608. /*the rwobject are only needed for C side work, not accessable from python*/
  609. #define PYGAMEAPI_RWOBJECT_FIRSTSLOT \
  610. (PYGAMEAPI_EVENT_FIRSTSLOT + PYGAMEAPI_EVENT_NUMSLOTS)
  611. #define PYGAMEAPI_RWOBJECT_NUMSLOTS 6
  612. #ifndef PYGAMEAPI_RWOBJECT_INTERNAL
  613. #define pgRWops_FromObject \
  614. (*(SDL_RWops * (*)(PyObject *)) \
  615. PyGAME_C_API[PYGAMEAPI_RWOBJECT_FIRSTSLOT + 0])
  616. #define pgRWops_IsFileObject \
  617. (*(int (*)(SDL_RWops *))PyGAME_C_API[PYGAMEAPI_RWOBJECT_FIRSTSLOT + 1])
  618. #define pg_EncodeFilePath \
  619. (*(PyObject * (*)(PyObject *, PyObject *)) \
  620. PyGAME_C_API[PYGAMEAPI_RWOBJECT_FIRSTSLOT + 2])
  621. #define pg_EncodeString \
  622. (*(PyObject * (*)(PyObject *, const char *, const char *, PyObject *)) \
  623. PyGAME_C_API[PYGAMEAPI_RWOBJECT_FIRSTSLOT + 3])
  624. #define pgRWops_FromFileObject \
  625. (*(SDL_RWops * (*)(PyObject *)) \
  626. PyGAME_C_API[PYGAMEAPI_RWOBJECT_FIRSTSLOT + 4])
  627. #define pgRWops_ReleaseObject \
  628. (*(int (*)(SDL_RWops *)) \
  629. PyGAME_C_API[PYGAMEAPI_RWOBJECT_FIRSTSLOT + 5])
  630. #define import_pygame_rwobject() IMPORT_PYGAME_MODULE(rwobject, RWOBJECT)
  631. #endif
  632. /* PixelArray */
  633. #define PYGAMEAPI_PIXELARRAY_FIRSTSLOT \
  634. (PYGAMEAPI_RWOBJECT_FIRSTSLOT + PYGAMEAPI_RWOBJECT_NUMSLOTS)
  635. #define PYGAMEAPI_PIXELARRAY_NUMSLOTS 2
  636. #ifndef PYGAMEAPI_PIXELARRAY_INTERNAL
  637. #define PyPixelArray_Check(x) \
  638. ((x)->ob_type == \
  639. (PyTypeObject *)PyGAME_C_API[PYGAMEAPI_PIXELARRAY_FIRSTSLOT + 0])
  640. #define PyPixelArray_New \
  641. (*(PyObject * (*)) PyGAME_C_API[PYGAMEAPI_PIXELARRAY_FIRSTSLOT + 1])
  642. #define import_pygame_pixelarray() IMPORT_PYGAME_MODULE(pixelarray, PIXELARRAY)
  643. #endif /* PYGAMEAPI_PIXELARRAY_INTERNAL */
  644. /* Color */
  645. #define PYGAMEAPI_COLOR_FIRSTSLOT \
  646. (PYGAMEAPI_PIXELARRAY_FIRSTSLOT + PYGAMEAPI_PIXELARRAY_NUMSLOTS)
  647. #define PYGAMEAPI_COLOR_NUMSLOTS 4
  648. #ifndef PYGAMEAPI_COLOR_INTERNAL
  649. #define pgColor_Check(x) \
  650. ((x)->ob_type == \
  651. (PyTypeObject *)PyGAME_C_API[PYGAMEAPI_COLOR_FIRSTSLOT + 0])
  652. #define pgColor_Type (*(PyObject *)PyGAME_C_API[PYGAMEAPI_COLOR_FIRSTSLOT])
  653. #define pgColor_New \
  654. (*(PyObject * (*)(Uint8 *)) PyGAME_C_API[PYGAMEAPI_COLOR_FIRSTSLOT + 1])
  655. #define pgColor_NewLength \
  656. (*(PyObject * (*)(Uint8 *, Uint8)) \
  657. PyGAME_C_API[PYGAMEAPI_COLOR_FIRSTSLOT + 3])
  658. #define pg_RGBAFromColorObj \
  659. (*(int (*)(PyObject *, \
  660. Uint8 *))PyGAME_C_API[PYGAMEAPI_COLOR_FIRSTSLOT + 2])
  661. #define import_pygame_color() IMPORT_PYGAME_MODULE(color, COLOR)
  662. #endif /* PYGAMEAPI_COLOR_INTERNAL */
  663. /* Math */
  664. #define PYGAMEAPI_MATH_FIRSTSLOT \
  665. (PYGAMEAPI_COLOR_FIRSTSLOT + PYGAMEAPI_COLOR_NUMSLOTS)
  666. #define PYGAMEAPI_MATH_NUMSLOTS 2
  667. #ifndef PYGAMEAPI_MATH_INTERNAL
  668. #define pgVector2_Check(x) \
  669. ((x)->ob_type == \
  670. (PyTypeObject *)PyGAME_C_API[PYGAMEAPI_MATH_FIRSTSLOT + 0])
  671. #define pgVector3_Check(x) \
  672. ((x)->ob_type == \
  673. (PyTypeObject *)PyGAME_C_API[PYGAMEAPI_MATH_FIRSTSLOT + 1])
  674. /*
  675. #define pgVector2_New \
  676. (*(PyObject*(*)) PyGAME_C_API[PYGAMEAPI_MATH_FIRSTSLOT + 1])
  677. */
  678. #define import_pygame_math() IMPORT_PYGAME_MODULE(math, MATH)
  679. #endif /* PYGAMEAPI_MATH_INTERNAL */
  680. #define PG_CAPSULE_NAME(m) (IMPPREFIX m "." PYGAMEAPI_LOCAL_ENTRY)
  681. #define _IMPORT_PYGAME_MODULE(module, MODULE, api_root) \
  682. { \
  683. PyObject *_module = PyImport_ImportModule(IMPPREFIX #module); \
  684. \
  685. if (_module != NULL) { \
  686. PyObject *_c_api = \
  687. PyObject_GetAttrString(_module, PYGAMEAPI_LOCAL_ENTRY); \
  688. \
  689. Py_DECREF(_module); \
  690. if (_c_api != NULL && PyCapsule_CheckExact(_c_api)) { \
  691. void **localptr = (void **)PyCapsule_GetPointer( \
  692. _c_api, PG_CAPSULE_NAME(#module)); \
  693. \
  694. if (localptr != NULL) { \
  695. memcpy(api_root + PYGAMEAPI_##MODULE##_FIRSTSLOT, \
  696. localptr, \
  697. sizeof(void **) * PYGAMEAPI_##MODULE##_NUMSLOTS); \
  698. } \
  699. } \
  700. Py_XDECREF(_c_api); \
  701. } \
  702. }
  703. #ifndef NO_PYGAME_C_API
  704. #define IMPORT_PYGAME_MODULE(module, MODULE) \
  705. _IMPORT_PYGAME_MODULE(module, MODULE, PyGAME_C_API)
  706. #define PYGAMEAPI_TOTALSLOTS \
  707. (PYGAMEAPI_MATH_FIRSTSLOT + PYGAMEAPI_MATH_NUMSLOTS)
  708. #ifdef PYGAME_H
  709. void *PyGAME_C_API[PYGAMEAPI_TOTALSLOTS] = {NULL};
  710. #else
  711. extern void *PyGAME_C_API[PYGAMEAPI_TOTALSLOTS];
  712. #endif
  713. #endif
  714. #if PG_HAVE_CAPSULE
  715. #define encapsulate_api(ptr, module) \
  716. PyCapsule_New(ptr, PG_CAPSULE_NAME(module), NULL)
  717. #else
  718. #define encapsulate_api(ptr, module) PyCObject_FromVoidPtr(ptr, NULL)
  719. #endif
  720. #ifndef PG_INLINE
  721. #if defined(__clang__)
  722. #define PG_INLINE __inline__ __attribute__((__unused__))
  723. #elif defined(__GNUC__)
  724. #define PG_INLINE __inline__
  725. #elif defined(_MSC_VER)
  726. #define PG_INLINE __inline
  727. #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
  728. #define PG_INLINE inline
  729. #else
  730. #define PG_INLINE
  731. #endif
  732. #endif
  733. /*last platform compiler stuff*/
  734. #if defined(macintosh) && defined(__MWERKS__) || defined(__SYMBIAN32__)
  735. #define PYGAME_EXPORT __declspec(export)
  736. #else
  737. #define PYGAME_EXPORT
  738. #endif
  739. #endif /* PYGAME_H */