0

fixed up new C code to be (mostly) C89-compliant

*mostly*, because the "inline" keyword isn't C standard until C99, but
I'm leaving it in because most compilers support it and it's really
handy. If it becomes an issue, we can deal with it later.
This commit is contained in:
Aaron Griffith
2011-03-17 23:04:41 -04:00
parent b20e881c5e
commit 6941bc3378
3 changed files with 65 additions and 42 deletions

View File

@@ -30,7 +30,7 @@
(tmp = (a) * (b) + 128, ((((tmp) >> 8) + (tmp)) >> 8)) (tmp = (a) * (b) + 128, ((((tmp) >> 8) + (tmp)) >> 8))
typedef struct { typedef struct {
PyObject_HEAD; PyObject_HEAD
Imaging image; Imaging image;
} ImagingObject; } ImagingObject;
@@ -214,6 +214,8 @@ alpha_over_wrap(PyObject *self, PyObject *args)
PyObject *dest, *src, *pos, *mask; PyObject *dest, *src, *pos, *mask;
/* destination position and size */ /* destination position and size */
int dx, dy, xsize, ysize; int dx, dy, xsize, ysize;
/* return value: dest image on success */
PyObject *ret;
if (!PyArg_ParseTuple(args, "OOOO", &dest, &src, &pos, &mask)) if (!PyArg_ParseTuple(args, "OOOO", &dest, &src, &pos, &mask))
return NULL; return NULL;
@@ -230,7 +232,7 @@ alpha_over_wrap(PyObject *self, PyObject *args)
} }
} }
PyObject *ret = alpha_over(dest, src, mask, dx, dy, xsize, ysize); ret = alpha_over(dest, src, mask, dx, dy, xsize, ysize);
if (ret == dest) { if (ret == dest) {
/* Python needs us to own our return value */ /* Python needs us to own our return value */
Py_INCREF(dest); Py_INCREF(dest);

View File

@@ -19,16 +19,16 @@
#include <numpy/arrayobject.h> #include <numpy/arrayobject.h>
// macro for getting blockID from a chunk of memory /* macro for getting blockID from a chunk of memory */
#define getBlock(blockThing, x,y,z) blockThing[ y + ( z * 128 + ( x * 128 * 16) ) ] #define getBlock(blockThing, x,y,z) blockThing[ y + ( z * 128 + ( x * 128 * 16) ) ]
static inline int isTransparent(unsigned char b) { static inline int isTransparent(unsigned char b) {
// TODO expand this to include all transparent blocks /* TODO expand this to include all transparent blocks */
return b == 0; return b == 0;
} }
// helper to handle alpha_over calls involving a texture tuple /* helper to handle alpha_over calls involving a texture tuple */
static inline PyObject * static inline PyObject *
texture_alpha_over(PyObject *dest, PyObject *t, int imgx, int imgy) texture_alpha_over(PyObject *dest, PyObject *t, int imgx, int imgy)
{ {
@@ -43,7 +43,7 @@ texture_alpha_over(PyObject *dest, PyObject *t, int imgx, int imgy)
return alpha_over(dest, src, mask, imgx, imgy, 0, 0); return alpha_over(dest, src, mask, imgx, imgy, 0, 0);
} }
// TODO triple check this to make sure reference counting is correct /* TODO triple check this to make sure reference counting is correct */
PyObject* PyObject*
chunk_render(PyObject *self, PyObject *args) { chunk_render(PyObject *self, PyObject *args) {
@@ -51,51 +51,63 @@ chunk_render(PyObject *self, PyObject *args) {
PyObject *blockdata_expanded; PyObject *blockdata_expanded;
int xoff, yoff; int xoff, yoff;
PyObject *img; PyObject *img;
PyObject *imgsize, *imgsize0_py, *imgsize1_py;
int imgsize0, imgsize1;
PyObject *blocks_py;
char *blocks;
PyObject *textures, *blockmap, *special_blocks, *specialblockmap;
int imgx, imgy;
int x, y, z;
if (!PyArg_ParseTuple(args, "OOiiO", &chunk, &img, &xoff, &yoff, &blockdata_expanded)) if (!PyArg_ParseTuple(args, "OOiiO", &chunk, &img, &xoff, &yoff, &blockdata_expanded))
return Py_BuildValue("i", "-1"); return Py_BuildValue("i", "-1");
// tuple /* tuple */
PyObject *imgsize = PyObject_GetAttrString(img, "size"); imgsize = PyObject_GetAttrString(img, "size");
PyObject *imgsize0_py = PySequence_GetItem(imgsize, 0); imgsize0_py = PySequence_GetItem(imgsize, 0);
PyObject *imgsize1_py = PySequence_GetItem(imgsize, 1); imgsize1_py = PySequence_GetItem(imgsize, 1);
Py_DECREF(imgsize); Py_DECREF(imgsize);
int imgsize0 = PyInt_AsLong(imgsize0_py); imgsize0 = PyInt_AsLong(imgsize0_py);
int imgsize1 = PyInt_AsLong(imgsize1_py); imgsize1 = PyInt_AsLong(imgsize1_py);
Py_DECREF(imgsize0_py); Py_DECREF(imgsize0_py);
Py_DECREF(imgsize1_py); Py_DECREF(imgsize1_py);
// get the block data directly from numpy: /* get the block data directly from numpy: */
PyObject *blocks_py = PyObject_GetAttrString(chunk, "blocks"); blocks_py = PyObject_GetAttrString(chunk, "blocks");
char *blocks = PyArray_BYTES(blocks_py); blocks = PyArray_BYTES(blocks_py);
Py_DECREF(blocks_py); Py_DECREF(blocks_py);
//PyObject *left_blocks = PyObject_GetAttrString(chunk, "left_blocks"); /*
//PyObject *right_blocks = PyObject_GetAttrString(chunk, "right_blocks"); PyObject *left_blocks = PyObject_GetAttrString(chunk, "left_blocks");
//PyObject *transparent_blocks = PyObject_GetAttrString(chunk, "transparent_blocks"); PyObject *right_blocks = PyObject_GetAttrString(chunk, "right_blocks");
PyObject *transparent_blocks = PyObject_GetAttrString(chunk, "transparent_blocks");
*/
PyObject *textures = PyImport_ImportModule("textures"); textures = PyImport_ImportModule("textures");
// TODO can these be global static? these don't change during program execution /* TODO can these be global static? these don't change during program execution */
PyObject *blockmap = PyObject_GetAttrString(textures, "blockmap"); blockmap = PyObject_GetAttrString(textures, "blockmap");
PyObject *special_blocks = PyObject_GetAttrString(textures, "special_blocks"); special_blocks = PyObject_GetAttrString(textures, "special_blocks");
PyObject *specialblockmap = PyObject_GetAttrString(textures, "specialblockmap"); specialblockmap = PyObject_GetAttrString(textures, "specialblockmap");
Py_DECREF(textures); Py_DECREF(textures);
//printf("render_loop\n");
int imgx, imgy;
int x, y, z;
for (x = 15; x > -1; x--) { for (x = 15; x > -1; x--) {
for (y = 0; y < 16; y++) { for (y = 0; y < 16; y++) {
imgx = xoff + x*12 + y*12; imgx = xoff + x*12 + y*12;
/* 128*12 -- offset for z direction, 15*6 -- offset for x */ /* 128*12 -- offset for z direction, 15*6 -- offset for x */
imgy = yoff - x*6 + y*6 + 128*12 + 15*6; imgy = yoff - x*6 + y*6 + 128*12 + 15*6;
for (z = 0; z < 128; z++) { for (z = 0; z < 128; z++) {
unsigned char block;
PyObject *blockid;
imgy -= 12; imgy -= 12;
if ((imgx >= imgsize0 + 24) || (imgx <= -24)) { if ((imgx >= imgsize0 + 24) || (imgx <= -24)) {
@@ -105,13 +117,16 @@ chunk_render(PyObject *self, PyObject *args) {
continue; continue;
} }
// get blockid /* get blockid
unsigned char block = getBlock(blocks, x, z, y); // Note the order: x,z,y note the order: x, z, y */
block = getBlock(blocks, x, z, y);
if (block == 0) { if (block == 0) {
continue; continue;
} }
//printf("checking blockid %hhu\n", block);
PyObject *blockid = PyInt_FromLong(block); // TODO figure out how to DECREF this easily, instead at every 'continue'. /* TODO figure out how to DECREF this easily, instead of at
every continue */
blockid = PyInt_FromLong(block);
if ( (x != 0) && (y != 15) && (z != 127) && if ( (x != 0) && (y != 15) && (z != 127) &&
@@ -123,32 +138,37 @@ chunk_render(PyObject *self, PyObject *args) {
if (!PySequence_Contains(special_blocks, blockid)) { if (!PySequence_Contains(special_blocks, blockid)) {
//t = textures.blockmap[blockid] /* t = textures.blockmap[blockid] */
PyObject *t = PyList_GetItem(blockmap, block); PyObject *t = PyList_GetItem(blockmap, block);
// PyList_GetItem returns borrowed ref /* PyList_GetItem returns borrowed ref */
if (t == Py_None) { if (t == Py_None) {
printf("t == Py_None. blockid=%d\n", block); printf("t == Py_None. blockid=%d\n", block);
continue; continue;
} }
// note that this version of alpha_over has a different signature than the /* note that this version of alpha_over has a different signature than the
// version in _composite.c version in _composite.c */
texture_alpha_over(img, t, imgx, imgy ); texture_alpha_over(img, t, imgx, imgy );
} else { } else {
// this should be a pointer to a unsigned char PyObject *tmp, *t;
/* this should be a pointer to a unsigned char */
void* ancilData_p = PyArray_GETPTR3(blockdata_expanded, x, y, z); void* ancilData_p = PyArray_GETPTR3(blockdata_expanded, x, y, z);
unsigned char ancilData = *((unsigned char*)ancilData_p); unsigned char ancilData = *((unsigned char*)ancilData_p);
if (block == 85) { // fence. skip the generate_pseudo_ancildata for now if (block == 85) {
/* fence. skip the generate_pseudo_ancildata for now */
continue; continue;
} }
PyObject *tmp = PyTuple_New(2);
tmp = PyTuple_New(2);
Py_INCREF(blockid); // because SetItem steals Py_INCREF(blockid); /* because SetItem steals */
PyTuple_SetItem(tmp, 0, blockid); PyTuple_SetItem(tmp, 0, blockid);
PyTuple_SetItem(tmp, 1, PyInt_FromLong(ancilData)); PyTuple_SetItem(tmp, 1, PyInt_FromLong(ancilData));
PyObject *t = PyDict_GetItem(specialblockmap, tmp); // this is a borrowed reference. no need to decref
/* this is a borrowed reference. no need to decref */
t = PyDict_GetItem(specialblockmap, tmp);
Py_DECREF(tmp); Py_DECREF(tmp);
if (t != NULL) if (t != NULL)
texture_alpha_over(img, t, imgx, imgy ); texture_alpha_over(img, t, imgx, imgy );

View File

@@ -31,5 +31,6 @@ PyMODINIT_FUNC
initc_overviewer(void) initc_overviewer(void)
{ {
(void)Py_InitModule("c_overviewer", COverviewerMethods); (void)Py_InitModule("c_overviewer", COverviewerMethods);
import_array(); // for numpy /* for numpy */
import_array();
} }