pygame.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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 (SDL_config.h) */
  55. #undef HAVE_SNPRINTF /* remove GCC redefine warning */
  56. #endif
  57. // This must be before all else
  58. #if defined(__SYMBIAN32__) && defined( OPENC )
  59. #include <sys/types.h>
  60. #if defined(__WINS__)
  61. void* _alloca(size_t size);
  62. # define alloca _alloca
  63. #endif
  64. #endif
  65. #include <Python.h>
  66. // No signal()
  67. #if defined(__SYMBIAN32__) && defined(HAVE_SIGNAL_H)
  68. #undef HAVE_SIGNAL_H
  69. #endif
  70. #if defined(HAVE_SNPRINTF)
  71. #undef HAVE_SNPRINTF
  72. #endif
  73. #ifdef MS_WIN32 /*Python gives us MS_WIN32, SDL needs just WIN32*/
  74. #ifndef WIN32
  75. #define WIN32
  76. #endif
  77. #endif
  78. /// Prefix when initializing module
  79. #define MODPREFIX ""
  80. /// Prefix when importing module
  81. #define IMPPREFIX "pygame."
  82. #ifdef __SYMBIAN32__
  83. #undef MODPREFIX
  84. #undef IMPPREFIX
  85. // On Symbian there is no pygame package. The extensions are built-in or in sys\bin.
  86. #define MODPREFIX "pygame_"
  87. #define IMPPREFIX "pygame_"
  88. #endif
  89. #include <SDL.h>
  90. /* macros used throughout the source */
  91. #define RAISE(x,y) (PyErr_SetString((x), (y)), (PyObject*)NULL)
  92. #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION == 3
  93. # define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
  94. # define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
  95. # define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
  96. #endif
  97. /* Py_ssize_t availability. */
  98. #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
  99. typedef int Py_ssize_t;
  100. #define PY_SSIZE_T_MAX INT_MAX
  101. #define PY_SSIZE_T_MIN INT_MIN
  102. typedef inquiry lenfunc;
  103. typedef intargfunc ssizeargfunc;
  104. typedef intobjargproc ssizeobjargproc;
  105. typedef intintargfunc ssizessizeargfunc;
  106. typedef intintobjargproc ssizessizeobjargproc;
  107. typedef getreadbufferproc readbufferproc;
  108. typedef getwritebufferproc writebufferproc;
  109. typedef getsegcountproc segcountproc;
  110. typedef getcharbufferproc charbufferproc;
  111. #endif
  112. #define PyType_Init(x) (((x).ob_type) = &PyType_Type)
  113. #define PYGAMEAPI_LOCAL_ENTRY "_PYGAME_C_API"
  114. #ifndef MIN
  115. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  116. #endif
  117. #ifndef MAX
  118. #define MAX(a,b) ( (a) > (b) ? (a) : (b))
  119. #endif
  120. #ifndef ABS
  121. #define ABS(a) (((a) < 0) ? -(a) : (a))
  122. #endif
  123. /* test sdl initializations */
  124. #define VIDEO_INIT_CHECK() \
  125. if(!SDL_WasInit(SDL_INIT_VIDEO)) \
  126. return RAISE(PyExc_SDLError, "video system not initialized")
  127. #define CDROM_INIT_CHECK() \
  128. if(!SDL_WasInit(SDL_INIT_CDROM)) \
  129. return RAISE(PyExc_SDLError, "cdrom system not initialized")
  130. #define JOYSTICK_INIT_CHECK() \
  131. if(!SDL_WasInit(SDL_INIT_JOYSTICK)) \
  132. return RAISE(PyExc_SDLError, "joystick system not initialized")
  133. /* BASE */
  134. #define PYGAMEAPI_BASE_FIRSTSLOT 0
  135. #define PYGAMEAPI_BASE_NUMSLOTS 13
  136. #ifndef PYGAMEAPI_BASE_INTERNAL
  137. #define PyExc_SDLError ((PyObject*)PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT])
  138. #define PyGame_RegisterQuit \
  139. (*(void(*)(void(*)(void)))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 1])
  140. #define IntFromObj \
  141. (*(int(*)(PyObject*, int*))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 2])
  142. #define IntFromObjIndex \
  143. (*(int(*)(PyObject*, int, int*))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 3])
  144. #define TwoIntsFromObj \
  145. (*(int(*)(PyObject*, int*, int*))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 4])
  146. #define FloatFromObj \
  147. (*(int(*)(PyObject*, float*))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 5])
  148. #define FloatFromObjIndex \
  149. (*(float(*)(PyObject*, int, float*)) \
  150. PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 6])
  151. #define TwoFloatsFromObj \
  152. (*(int(*)(PyObject*, float*, float*)) \
  153. PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 7])
  154. #define UintFromObj \
  155. (*(int(*)(PyObject*, Uint32*))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 8])
  156. #define UintFromObjIndex \
  157. (*(int(*)(PyObject*, int, Uint32*)) \
  158. PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 9])
  159. #define PyGame_Video_AutoQuit \
  160. (*(void(*)(void))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 10])
  161. #define PyGame_Video_AutoInit \
  162. (*(int(*)(void))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 11])
  163. #define RGBAFromObj \
  164. (*(int(*)(PyObject*, Uint8*))PyGAME_C_API[PYGAMEAPI_BASE_FIRSTSLOT + 12])
  165. #define import_pygame_base() { \
  166. PyObject *_module = PyImport_ImportModule(IMPPREFIX "base"); \
  167. if (_module != NULL) { \
  168. PyObject *_dict = PyModule_GetDict(_module); \
  169. PyObject *_c_api = PyDict_GetItemString(_dict, \
  170. PYGAMEAPI_LOCAL_ENTRY); \
  171. if(PyCObject_Check(_c_api)) { \
  172. int i; void** localptr = (void**)PyCObject_AsVoidPtr(_c_api); \
  173. for(i = 0; i < PYGAMEAPI_BASE_NUMSLOTS; ++i) \
  174. PyGAME_C_API[i + PYGAMEAPI_BASE_FIRSTSLOT] = localptr[i]; \
  175. } \
  176. Py_DECREF(_module); \
  177. } \
  178. }
  179. #endif
  180. /* RECT */
  181. #define PYGAMEAPI_RECT_FIRSTSLOT \
  182. (PYGAMEAPI_BASE_FIRSTSLOT + PYGAMEAPI_BASE_NUMSLOTS)
  183. #define PYGAMEAPI_RECT_NUMSLOTS 4
  184. typedef struct {
  185. int x, y;
  186. int w, h;
  187. }GAME_Rect;
  188. typedef struct {
  189. PyObject_HEAD
  190. GAME_Rect r;
  191. PyObject *weakreflist;
  192. } PyRectObject;
  193. #define PyRect_AsRect(x) (((PyRectObject*)x)->r)
  194. #ifndef PYGAMEAPI_RECT_INTERNAL
  195. #define PyRect_Check(x) \
  196. ((x)->ob_type == (PyTypeObject*)PyGAME_C_API[PYGAMEAPI_RECT_FIRSTSLOT + 0])
  197. #define PyRect_Type (*(PyTypeObject*)PyGAME_C_API[PYGAMEAPI_RECT_FIRSTSLOT + 0])
  198. #define PyRect_New \
  199. (*(PyObject*(*)(SDL_Rect*))PyGAME_C_API[PYGAMEAPI_RECT_FIRSTSLOT + 1])
  200. #define PyRect_New4 \
  201. (*(PyObject*(*)(int,int,int,int))PyGAME_C_API[PYGAMEAPI_RECT_FIRSTSLOT + 2])
  202. #define GameRect_FromObject \
  203. (*(GAME_Rect*(*)(PyObject*, GAME_Rect*)) \
  204. PyGAME_C_API[PYGAMEAPI_RECT_FIRSTSLOT + 3])
  205. #define import_pygame_rect() { \
  206. PyObject *_module = PyImport_ImportModule(IMPPREFIX "rect"); \
  207. if (_module != NULL) { \
  208. PyObject *_dict = PyModule_GetDict(_module); \
  209. PyObject *_c_api = PyDict_GetItemString(_dict, \
  210. PYGAMEAPI_LOCAL_ENTRY); \
  211. if(PyCObject_Check(_c_api)) { \
  212. int i; void** localptr = (void**)PyCObject_AsVoidPtr(_c_api); \
  213. for(i = 0; i < PYGAMEAPI_RECT_NUMSLOTS; ++i) \
  214. PyGAME_C_API[i + PYGAMEAPI_RECT_FIRSTSLOT] = localptr[i]; \
  215. } \
  216. Py_DECREF(_module); \
  217. } \
  218. }
  219. #endif
  220. /* CDROM */
  221. #define PYGAMEAPI_CDROM_FIRSTSLOT \
  222. (PYGAMEAPI_RECT_FIRSTSLOT + PYGAMEAPI_RECT_NUMSLOTS)
  223. #define PYGAMEAPI_CDROM_NUMSLOTS 2
  224. typedef struct {
  225. PyObject_HEAD
  226. int id;
  227. } PyCDObject;
  228. #define PyCD_AsID(x) (((PyCDObject*)x)->id)
  229. #ifndef PYGAMEAPI_CDROM_INTERNAL
  230. #define PyCD_Check(x) \
  231. ((x)->ob_type == (PyTypeObject*)PyGAME_C_API[PYGAMEAPI_CDROM_FIRSTSLOT + 0])
  232. #define PyCD_Type (*(PyTypeObject*)PyGAME_C_API[PYGAMEAPI_CDROM_FIRSTSLOT + 0])
  233. #define PyCD_New \
  234. (*(PyObject*(*)(int))PyGAME_C_API[PYGAMEAPI_CDROM_FIRSTSLOT + 1])
  235. #define import_pygame_cd() { \
  236. PyObject *_module = PyImport_ImportModule(IMPPREFIX "cdrom"); \
  237. if (_module != NULL) { \
  238. PyObject *_dict = PyModule_GetDict(_module); \
  239. PyObject *_c_api = PyDict_GetItemString(_dict, \
  240. PYGAMEAPI_LOCAL_ENTRY); \
  241. if(PyCObject_Check(_c_api)) { \
  242. int i; void** localptr = (void**)PyCObject_AsVoidPtr(_c_api); \
  243. for(i = 0; i < PYGAMEAPI_CDROM_NUMSLOTS; ++i) \
  244. PyGAME_C_API[i + PYGAMEAPI_CDROM_FIRSTSLOT] = localptr[i]; \
  245. } \
  246. Py_DECREF(_module); \
  247. } \
  248. }
  249. #endif
  250. /* JOYSTICK */
  251. #define PYGAMEAPI_JOYSTICK_FIRSTSLOT \
  252. (PYGAMEAPI_CDROM_FIRSTSLOT + PYGAMEAPI_CDROM_NUMSLOTS)
  253. #define PYGAMEAPI_JOYSTICK_NUMSLOTS 2
  254. typedef struct {
  255. PyObject_HEAD
  256. int id;
  257. } PyJoystickObject;
  258. #define PyJoystick_AsID(x) (((PyJoystickObject*)x)->id)
  259. #ifndef PYGAMEAPI_JOYSTICK_INTERNAL
  260. #define PyJoystick_Check(x) \
  261. ((x)->ob_type == (PyTypeObject*) \
  262. PyGAME_C_API[PYGAMEAPI_JOYSTICK_FIRSTSLOT + 0])
  263. #define PyJoystick_Type \
  264. (*(PyTypeObject*)PyGAME_C_API[PYGAMEAPI_JOYSTICK_FIRSTSLOT + 0])
  265. #define PyJoystick_New \
  266. (*(PyObject*(*)(int))PyGAME_C_API[PYGAMEAPI_JOYSTICK_FIRSTSLOT + 1])
  267. #define import_pygame_joystick() { \
  268. PyObject *_module = PyImport_ImportModule(IMPPREFIX "joystick"); \
  269. if (_module != NULL) { \
  270. PyObject *_dict = PyModule_GetDict(_module); \
  271. PyObject *_c_api = PyDict_GetItemString(_dict, \
  272. PYGAMEAPI_LOCAL_ENTRY); \
  273. if(PyCObject_Check(_c_api)) { \
  274. int i; void** localptr = (void**)PyCObject_AsVoidPtr(_c_api); \
  275. for(i = 0; i < PYGAMEAPI_JOYSTICK_NUMSLOTS; ++i) \
  276. PyGAME_C_API[i + PYGAMEAPI_JOYSTICK_FIRSTSLOT] = \
  277. localptr[i]; \
  278. } \
  279. Py_DECREF(_module); \
  280. } \
  281. }
  282. #endif
  283. /* DISPLAY */
  284. #define PYGAMEAPI_DISPLAY_FIRSTSLOT \
  285. (PYGAMEAPI_JOYSTICK_FIRSTSLOT + PYGAMEAPI_JOYSTICK_NUMSLOTS)
  286. #define PYGAMEAPI_DISPLAY_NUMSLOTS 2
  287. typedef struct {
  288. PyObject_HEAD
  289. SDL_VideoInfo info;
  290. } PyVidInfoObject;
  291. #define PyVidInfo_AsVidInfo(x) (((PyVidInfoObject*)x)->info)
  292. #ifndef PYGAMEAPI_DISPLAY_INTERNAL
  293. #define PyVidInfo_Check(x) \
  294. ((x)->ob_type == (PyTypeObject*) \
  295. PyGAME_C_API[PYGAMEAPI_DISPLAY_FIRSTSLOT + 0])
  296. #define PyVidInfo_Type \
  297. (*(PyTypeObject*)PyGAME_C_API[PYGAMEAPI_DISPLAY_FIRSTSLOT + 0])
  298. #define PyVidInfo_New \
  299. (*(PyObject*(*)(SDL_VideoInfo*)) \
  300. PyGAME_C_API[PYGAMEAPI_DISPLAY_FIRSTSLOT + 1])
  301. #define import_pygame_display() { \
  302. PyObject *_module = PyImport_ImportModule(IMPPREFIX "display"); \
  303. if (_module != NULL) { \
  304. PyObject *_dict = PyModule_GetDict(_module); \
  305. PyObject *_c_api = PyDict_GetItemString(_dict, \
  306. PYGAMEAPI_LOCAL_ENTRY); \
  307. if(PyCObject_Check(_c_api)) { \
  308. int i; void** localptr = (void**)PyCObject_AsVoidPtr(_c_api); \
  309. for(i = 0; i < PYGAMEAPI_DISPLAY_NUMSLOTS; ++i) \
  310. PyGAME_C_API[i + PYGAMEAPI_DISPLAY_FIRSTSLOT] = \
  311. localptr[i]; \
  312. } \
  313. Py_DECREF(_module); \
  314. } \
  315. }
  316. #endif
  317. /* SURFACE */
  318. #define PYGAMEAPI_SURFACE_FIRSTSLOT \
  319. (PYGAMEAPI_DISPLAY_FIRSTSLOT + PYGAMEAPI_DISPLAY_NUMSLOTS)
  320. #define PYGAMEAPI_SURFACE_NUMSLOTS 3
  321. typedef struct {
  322. PyObject_HEAD
  323. SDL_Surface* surf;
  324. struct SubSurface_Data* subsurface; /*ptr to subsurface data (if a
  325. * subsurface)*/
  326. PyObject *weakreflist;
  327. PyObject *locklist;
  328. PyObject *dependency;
  329. } PySurfaceObject;
  330. #define PySurface_AsSurface(x) (((PySurfaceObject*)x)->surf)
  331. #ifndef PYGAMEAPI_SURFACE_INTERNAL
  332. #define PySurface_Check(x) \
  333. ((x)->ob_type == (PyTypeObject*) \
  334. PyGAME_C_API[PYGAMEAPI_SURFACE_FIRSTSLOT + 0])
  335. #define PySurface_Type \
  336. (*(PyTypeObject*)PyGAME_C_API[PYGAMEAPI_SURFACE_FIRSTSLOT + 0])
  337. #define PySurface_New \
  338. (*(PyObject*(*)(SDL_Surface*)) \
  339. PyGAME_C_API[PYGAMEAPI_SURFACE_FIRSTSLOT + 1])
  340. #define PySurface_Blit \
  341. (*(int(*)(PyObject*,PyObject*,SDL_Rect*,SDL_Rect*,int)) \
  342. PyGAME_C_API[PYGAMEAPI_SURFACE_FIRSTSLOT + 2])
  343. #define import_pygame_surface() do { \
  344. PyObject *_module = PyImport_ImportModule(IMPPREFIX "surface"); \
  345. if (_module != NULL) { \
  346. PyObject *_dict = PyModule_GetDict(_module); \
  347. PyObject *_c_api = PyDict_GetItemString(_dict, \
  348. PYGAMEAPI_LOCAL_ENTRY); \
  349. if(PyCObject_Check(_c_api)) { \
  350. int i; void** localptr = (void**)PyCObject_AsVoidPtr(_c_api); \
  351. for(i = 0; i < PYGAMEAPI_SURFACE_NUMSLOTS; ++i) \
  352. PyGAME_C_API[i + PYGAMEAPI_SURFACE_FIRSTSLOT] = \
  353. localptr[i]; \
  354. } \
  355. Py_DECREF(_module); \
  356. } \
  357. else \
  358. { \
  359. break; \
  360. } \
  361. _module = PyImport_ImportModule(IMPPREFIX "surflock"); \
  362. if (_module != NULL) { \
  363. PyObject *_dict = PyModule_GetDict(_module); \
  364. PyObject *_c_api = PyDict_GetItemString(_dict, \
  365. PYGAMEAPI_LOCAL_ENTRY); \
  366. if(PyCObject_Check(_c_api)) { \
  367. int i; void** localptr = (void**)PyCObject_AsVoidPtr(_c_api); \
  368. for(i = 0; i < PYGAMEAPI_SURFLOCK_NUMSLOTS; ++i) \
  369. PyGAME_C_API[i + PYGAMEAPI_SURFLOCK_FIRSTSLOT] = \
  370. localptr[i]; \
  371. } \
  372. Py_DECREF(_module); \
  373. } \
  374. } while (0)
  375. #endif
  376. /* SURFLOCK */ /*auto import/init by surface*/
  377. #define PYGAMEAPI_SURFLOCK_FIRSTSLOT \
  378. (PYGAMEAPI_SURFACE_FIRSTSLOT + PYGAMEAPI_SURFACE_NUMSLOTS)
  379. #define PYGAMEAPI_SURFLOCK_NUMSLOTS 8
  380. struct SubSurface_Data
  381. {
  382. PyObject* owner;
  383. int pixeloffset;
  384. int offsetx, offsety;
  385. };
  386. typedef struct
  387. {
  388. PyObject_HEAD
  389. PyObject *surface;
  390. PyObject *lockobj;
  391. PyObject *weakrefs;
  392. } PyLifetimeLock;
  393. #ifndef PYGAMEAPI_SURFLOCK_INTERNAL
  394. #define PyLifetimeLock_Check(x) \
  395. ((x)->ob_type == (PyTypeObject*) \
  396. PyGAME_C_API[PYGAMEAPI_SURFLOCK_FIRSTSLOT + 0])
  397. #define PySurface_Prep(x) \
  398. if(((PySurfaceObject*)x)->subsurface) \
  399. (*(*(void(*)(PyObject*)) \
  400. PyGAME_C_API[PYGAMEAPI_SURFLOCK_FIRSTSLOT + 1]))(x)
  401. #define PySurface_Unprep(x) \
  402. if(((PySurfaceObject*)x)->subsurface) \
  403. (*(*(void(*)(PyObject*)) \
  404. PyGAME_C_API[PYGAMEAPI_SURFLOCK_FIRSTSLOT + 2]))(x)
  405. #define PySurface_Lock \
  406. (*(int(*)(PyObject*))PyGAME_C_API[PYGAMEAPI_SURFLOCK_FIRSTSLOT + 3])
  407. #define PySurface_Unlock \
  408. (*(int(*)(PyObject*))PyGAME_C_API[PYGAMEAPI_SURFLOCK_FIRSTSLOT + 4])
  409. #define PySurface_LockBy \
  410. (*(int(*)(PyObject*,PyObject*)) \
  411. PyGAME_C_API[PYGAMEAPI_SURFLOCK_FIRSTSLOT + 5])
  412. #define PySurface_UnlockBy \
  413. (*(int(*)(PyObject*,PyObject*)) \
  414. PyGAME_C_API[PYGAMEAPI_SURFLOCK_FIRSTSLOT + 6])
  415. #define PySurface_LockLifetime \
  416. (*(PyObject*(*)(PyObject*,PyObject*)) \
  417. PyGAME_C_API[PYGAMEAPI_SURFLOCK_FIRSTSLOT + 7])
  418. #endif
  419. /* EVENT */
  420. #define PYGAMEAPI_EVENT_FIRSTSLOT \
  421. (PYGAMEAPI_SURFLOCK_FIRSTSLOT + PYGAMEAPI_SURFLOCK_NUMSLOTS)
  422. #define PYGAMEAPI_EVENT_NUMSLOTS 4
  423. typedef struct {
  424. PyObject_HEAD
  425. int type;
  426. PyObject* dict;
  427. } PyEventObject;
  428. #ifndef PYGAMEAPI_EVENT_INTERNAL
  429. #define PyEvent_Check(x) \
  430. ((x)->ob_type == (PyTypeObject*)PyGAME_C_API[PYGAMEAPI_EVENT_FIRSTSLOT + 0])
  431. #define PyEvent_Type \
  432. (*(PyTypeObject*)PyGAME_C_API[PYGAMEAPI_EVENT_FIRSTSLOT + 0])
  433. #define PyEvent_New \
  434. (*(PyObject*(*)(SDL_Event*))PyGAME_C_API[PYGAMEAPI_EVENT_FIRSTSLOT + 1])
  435. #define PyEvent_New2 \
  436. (*(PyObject*(*)(int, PyObject*))PyGAME_C_API[PYGAMEAPI_EVENT_FIRSTSLOT + 2])
  437. #define PyEvent_FillUserEvent \
  438. (*(int (*)(PyEventObject*, SDL_Event*)) \
  439. PyGAME_C_API[PYGAMEAPI_EVENT_FIRSTSLOT + 3])
  440. #define import_pygame_event() { \
  441. PyObject *_module = PyImport_ImportModule(IMPPREFIX "event"); \
  442. if (_module != NULL) { \
  443. PyObject *_dict = PyModule_GetDict(_module); \
  444. PyObject *_c_api = PyDict_GetItemString(_dict, \
  445. PYGAMEAPI_LOCAL_ENTRY); \
  446. if(PyCObject_Check(_c_api)) { \
  447. int i; void** localptr = (void**)PyCObject_AsVoidPtr(_c_api); \
  448. for(i = 0; i < PYGAMEAPI_EVENT_NUMSLOTS; ++i) \
  449. PyGAME_C_API[i + PYGAMEAPI_EVENT_FIRSTSLOT] = localptr[i]; \
  450. } \
  451. Py_DECREF(_module); \
  452. } \
  453. }
  454. #endif
  455. /* RWOBJECT */
  456. /*the rwobject are only needed for C side work, not accessable from python*/
  457. #define PYGAMEAPI_RWOBJECT_FIRSTSLOT \
  458. (PYGAMEAPI_EVENT_FIRSTSLOT + PYGAMEAPI_EVENT_NUMSLOTS)
  459. #define PYGAMEAPI_RWOBJECT_NUMSLOTS 4
  460. #ifndef PYGAMEAPI_RWOBJECT_INTERNAL
  461. #define RWopsFromPython \
  462. (*(SDL_RWops*(*)(PyObject*))PyGAME_C_API[PYGAMEAPI_RWOBJECT_FIRSTSLOT + 0])
  463. #define RWopsCheckPython \
  464. (*(int(*)(SDL_RWops*))PyGAME_C_API[PYGAMEAPI_RWOBJECT_FIRSTSLOT + 1])
  465. #define RWopsFromPythonThreaded \
  466. (*(SDL_RWops*(*)(PyObject*))PyGAME_C_API[PYGAMEAPI_RWOBJECT_FIRSTSLOT + 2])
  467. #define RWopsCheckPythonThreaded \
  468. (*(int(*)(SDL_RWops*))PyGAME_C_API[PYGAMEAPI_RWOBJECT_FIRSTSLOT + 3])
  469. #define import_pygame_rwobject() { \
  470. PyObject *_module = PyImport_ImportModule(IMPPREFIX "rwobject"); \
  471. if (_module != NULL) { \
  472. PyObject *_dict = PyModule_GetDict(_module); \
  473. PyObject *_c_api = PyDict_GetItemString(_dict, \
  474. PYGAMEAPI_LOCAL_ENTRY); \
  475. if(PyCObject_Check(_c_api)) { \
  476. int i; void** localptr = (void**)PyCObject_AsVoidPtr(_c_api); \
  477. for(i = 0; i < PYGAMEAPI_RWOBJECT_NUMSLOTS; ++i) \
  478. PyGAME_C_API[i + PYGAMEAPI_RWOBJECT_FIRSTSLOT] = \
  479. localptr[i]; \
  480. } \
  481. Py_DECREF(_module); \
  482. } \
  483. }
  484. #endif
  485. /* BufferProxy */
  486. typedef struct
  487. {
  488. PyObject_HEAD
  489. PyObject *dict; /* dict for subclassing */
  490. PyObject *weakrefs; /* Weakrefs for subclassing */
  491. void *buffer; /* Pointer to the buffer of the parent object. */
  492. Py_ssize_t length; /* Length of the buffer. */
  493. PyObject *parent; /* Parent object associated with this object. */
  494. PyObject *lock; /* Lock object for the surface. */
  495. } PyBufferProxy;
  496. #define PYGAMEAPI_BUFFERPROXY_FIRSTSLOT \
  497. (PYGAMEAPI_RWOBJECT_FIRSTSLOT + PYGAMEAPI_RWOBJECT_NUMSLOTS)
  498. #define PYGAMEAPI_BUFFERPROXY_NUMSLOTS 2
  499. #ifndef PYGAMEAPI_BUFFERPROXY_INTERNAL
  500. #define PyBufferProxy_Check(x) \
  501. ((x)->ob_type == (PyTypeObject*) \
  502. PyGAME_C_API[PYGAMEAPI_BUFFERPROXY_FIRSTSLOT + 0])
  503. #define PyBufferProxy_New \
  504. (*(PyObject*(*)(PyObject*, void*, Py_ssize_t, PyObject*)) \
  505. PyGAME_C_API[PYGAMEAPI_BUFFERPROXY_FIRSTSLOT + 1])
  506. #define import_pygame_bufferproxy() \
  507. { \
  508. PyObject *_module = PyImport_ImportModule (IMPPREFIX "bufferproxy");\
  509. if (_module != NULL) \
  510. { \
  511. PyObject *_dict = PyModule_GetDict (_module); \
  512. PyObject *_c_api = PyDict_GetItemString \
  513. (_dict, PYGAMEAPI_LOCAL_ENTRY); \
  514. if (PyCObject_Check (_c_api)) \
  515. { \
  516. int i; \
  517. void** localptr = (void**) PyCObject_AsVoidPtr (_c_api); \
  518. for (i = 0; i < PYGAMEAPI_BUFFERPROXY_NUMSLOTS; ++i) \
  519. PyGAME_C_API[i + PYGAMEAPI_BUFFERPROXY_FIRSTSLOT] = \
  520. localptr[i]; \
  521. } \
  522. Py_DECREF (_module); \
  523. } \
  524. }
  525. #endif /* PYGAMEAPI_BUFFERPROXY_INTERNAL */
  526. /* PixelArray */
  527. #define PYGAMEAPI_PIXELARRAY_FIRSTSLOT \
  528. (PYGAMEAPI_BUFFERPROXY_FIRSTSLOT + PYGAMEAPI_BUFFERPROXY_NUMSLOTS)
  529. #define PYGAMEAPI_PIXELARRAY_NUMSLOTS 2
  530. #ifndef PYGAMEAPI_PIXELARRAY_INTERNAL
  531. #define PyPixelArray_Check(x) \
  532. ((x)->ob_type == (PyTypeObject*) \
  533. PyGAME_C_API[PYGAMEAPI_PIXELARRAY_FIRSTSLOT + 0])
  534. #define PyPixelArray_New \
  535. (*(PyObject*(*)) PyGAME_C_API[PYGAMEAPI_PIXELARRAY_FIRSTSLOT + 1])
  536. #define import_pygame_pixelarray() \
  537. { \
  538. PyObject *_module = PyImport_ImportModule (IMPPREFIX "pixelarray"); \
  539. if (_module != NULL) \
  540. { \
  541. PyObject *_dict = PyModule_GetDict (_module); \
  542. PyObject *_c_api = PyDict_GetItemString \
  543. (_dict, PYGAMEAPI_LOCAL_ENTRY); \
  544. if (PyCObject_Check (_c_api)) \
  545. { \
  546. int i; \
  547. void** localptr = (void**) PyCObject_AsVoidPtr (_c_api); \
  548. for (i = 0; i < PYGAMEAPI_PIXELARRAY_NUMSLOTS; ++i) \
  549. PyGAME_C_API[i + PYGAMEAPI_PIXELARRAY_FIRSTSLOT] = \
  550. localptr[i]; \
  551. } \
  552. Py_DECREF (_module); \
  553. } \
  554. }
  555. #endif /* PYGAMEAPI_PIXELARRAY_INTERNAL */
  556. /* Color */
  557. #define PYGAMEAPI_COLOR_FIRSTSLOT \
  558. (PYGAMEAPI_PIXELARRAY_FIRSTSLOT + PYGAMEAPI_PIXELARRAY_NUMSLOTS)
  559. #define PYGAMEAPI_COLOR_NUMSLOTS 4
  560. #ifndef PYGAMEAPI_COLOR_INTERNAL
  561. #define PyColor_Check(x) \
  562. ((x)->ob_type == (PyTypeObject*) \
  563. PyGAME_C_API[PYGAMEAPI_COLOR_FIRSTSLOT + 0])
  564. #define PyColor_New \
  565. (*(PyObject *(*)(Uint8*)) PyGAME_C_API[PYGAMEAPI_COLOR_FIRSTSLOT + 1])
  566. #define PyColor_NewLength \
  567. (*(PyObject *(*)(Uint8*, Uint8)) PyGAME_C_API[PYGAMEAPI_COLOR_FIRSTSLOT + 3])
  568. #define RGBAFromColorObj \
  569. (*(int(*)(PyObject*, Uint8*)) PyGAME_C_API[PYGAMEAPI_COLOR_FIRSTSLOT + 2])
  570. #define import_pygame_color() \
  571. { \
  572. PyObject *_module = PyImport_ImportModule (IMPPREFIX "color"); \
  573. if (_module != NULL) \
  574. { \
  575. PyObject *_dict = PyModule_GetDict (_module); \
  576. PyObject *_c_api = PyDict_GetItemString \
  577. (_dict, PYGAMEAPI_LOCAL_ENTRY); \
  578. if (PyCObject_Check (_c_api)) \
  579. { \
  580. int i; \
  581. void** localptr = (void**) PyCObject_AsVoidPtr (_c_api); \
  582. for (i = 0; i < PYGAMEAPI_COLOR_NUMSLOTS; ++i) \
  583. PyGAME_C_API[i + PYGAMEAPI_COLOR_FIRSTSLOT] = \
  584. localptr[i]; \
  585. } \
  586. Py_DECREF (_module); \
  587. } \
  588. }
  589. #endif /* PYGAMEAPI_COLOR_INTERNAL */
  590. #ifndef NO_PYGAME_C_API
  591. #define PYGAMEAPI_TOTALSLOTS \
  592. (PYGAMEAPI_COLOR_FIRSTSLOT + PYGAMEAPI_COLOR_NUMSLOTS)
  593. static void* PyGAME_C_API[PYGAMEAPI_TOTALSLOTS] = { NULL };
  594. #endif
  595. /*last platform compiler stuff*/
  596. #if defined(macintosh) && defined(__MWERKS__) || defined(__SYMBIAN32__)
  597. #define PYGAME_EXPORT __declspec(export)
  598. #else
  599. #define PYGAME_EXPORT
  600. #endif
  601. #if defined(__SYMBIAN32__) && PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION == 2
  602. // These are missing from Python 2.2
  603. #ifndef Py_RETURN_NONE
  604. #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
  605. #define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
  606. #define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
  607. #ifndef intrptr_t
  608. #define intptr_t int
  609. // No PySlice_GetIndicesEx on Py 2.2
  610. #define PySlice_GetIndicesEx(a,b,c,d,e,f) PySlice_GetIndices(a,b,c,d,e)
  611. #define PyBool_FromLong(x) Py_BuildValue("b", x)
  612. #endif
  613. // _symport_free and malloc are not exported in python.dll
  614. // See http://discussion.forum.nokia.com/forum/showthread.php?t=57874
  615. #undef PyObject_NEW
  616. #define PyObject_NEW PyObject_New
  617. #undef PyMem_MALLOC
  618. #define PyMem_MALLOC PyMem_Malloc
  619. #undef PyObject_DEL
  620. #define PyObject_DEL PyObject_Del
  621. #endif // intptr_t
  622. #endif // __SYMBIAN32__ Python 2.2.2
  623. #endif /* PYGAME_H */