Commits

illume committed e85cf3c

exposed mask C API from Michael George. Thanks!

Comments (0)

Files changed (3)

 # BUG    = fixed a bug that was (or could have been) crashing
 #
 #
+
+Dec 05, 2008
+    exposed mask C API from Michael George.  Thanks!
+
+
 Aug 30, 2008
-	[BUG] Fixed CREATE_PIXEL macro's use of Aloss
-		which was making surface.fill with surfaces with no alpha go green 
-		
+    [BUG] Fixed CREATE_PIXEL macro's use of Aloss
+    which was making surface.fill with surfaces with no alpha go green 
+
 Jul 18, 2008
     Added Surface.set_masks and .set_shifts useful for using data in
         b,g,r,a mode... and other fun hacks.
 #define CALLLOG(x) fprintf(stderr, (x));
 */
 
+#define PYGAMEAPI_MASK_INTERNAL 1
+#include "mask.h"
 #include "pygame.h"
 #include "pygamedocs.h"
 #include "structmember.h"
 #define M_PI 3.14159265358979323846
 #endif
 
-typedef struct {
-  PyObject_HEAD
-  bitmask_t *mask;
-} PyMaskObject;
-
 staticforward PyTypeObject PyMask_Type;
-#define PyMask_Check(x) ((x)->ob_type == &PyMask_Type)
-#define PyMask_AsBitmap(x) (((PyMaskObject*)x)->mask)
-
 
 /* mask object methods */
 
 
 void initmask(void)
 {
-  PyObject *module, *dict;
+  PyObject *module, *dict, *apiobj;
+  static void* c_api[PYGAMEAPI_MASK_NUMSLOTS];
+
+  /* create the mask type */
   PyType_Init(PyMask_Type);
 
   /* create the module */
   module = Py_InitModule3("mask", mask_builtins, DOC_PYGAMEMASK);
   dict = PyModule_GetDict(module);
   PyDict_SetItemString(dict, "MaskType", (PyObject *)&PyMask_Type);
+
+  /* export the c api */
+  Py_INCREF((PyObject*) &PyMask_Type);
+  c_api[0] = &PyMask_Type;
+  apiobj   = PyCObject_FromVoidPtr (c_api, NULL);
+  PyModule_AddObject (module, PYGAMEAPI_LOCAL_ENTRY, apiobj);
+
+  /* import other modules */
   import_pygame_base ();
   import_pygame_color ();
   import_pygame_surface ();
+#include <Python.h>
+#include "bitmask.h"
+
+#define PYGAMEAPI_MASK_NUMSLOTS 1
+#define PYGAMEAPI_LOCAL_ENTRY "_PYGAME_C_API"
+
+typedef struct {
+  PyObject_HEAD
+  bitmask_t *mask;
+} PyMaskObject;
+
+#define PyMask_AsBitmap(x) (((PyMaskObject*)x)->mask)
+
+#ifndef PYGAMEAPI_MASK_INTERNAL
+
+#define PyMask_Type     (*(PyTypeObject*)PyMASK_C_API[0])
+#define PyMask_Check(x) ((x)->ob_type == &PyMask_Type)
+
+#define import_pygame_mask() {                                                                 \
+	PyObject *module = PyImport_ImportModule("pygame.mask");                               \
+	if (module != NULL) {                                                                  \
+		PyObject *dict  = PyModule_GetDict(module);                                    \
+		PyObject *c_api = PyDict_GetItemString(dict, PYGAMEAPI_LOCAL_ENTRY);           \
+		if(PyCObject_Check(c_api)) {                                                   \
+			void** localptr = (void**) PyCObject_AsVoidPtr(c_api);                 \
+			memcpy(PyMASK_C_API, localptr, sizeof(void*)*PYGAMEAPI_MASK_NUMSLOTS); \
+		} Py_DECREF(module);                                                           \
+	}                                                                                      \
+}
+
+#endif /* !defined(PYGAMEAPI_MASK_INTERNAL) */
+
+static void* PyMASK_C_API[PYGAMEAPI_MASK_NUMSLOTS] = {NULL};
+