From e0e571971a5fc2bda90053377ee4b6ce9a672c47 Mon Sep 17 00:00:00 2001 From: Aaron Griffith Date: Sun, 27 Mar 2011 06:07:43 -0400 Subject: [PATCH] added endianness helper functions, and reading biome data directly --- setup.py | 2 +- src/endian.c | 39 +++++++++++++++++++++++++++++++++++++++ src/main.c | 2 ++ src/overviewer.h | 5 +++++ src/rendermode-normal.c | 9 ++------- 5 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 src/endian.c diff --git a/setup.py b/setup.py index 9d2f7aa..85ade1e 100644 --- a/setup.py +++ b/setup.py @@ -46,7 +46,7 @@ except AttributeError: numpy_include = numpy.get_numpy_include() -c_overviewer_files = ['src/main.c', 'src/composite.c', 'src/iterate.c'] +c_overviewer_files = ['src/main.c', 'src/composite.c', 'src/iterate.c', 'src/endian.c'] c_overviewer_files += ['src/rendermodes.c', 'src/rendermode-normal.c', 'src/rendermode-lighting.c', 'src/rendermode-night.c', 'src/rendermode-spawn.c'] setup_kwargs['ext_modules'].append(Extension('c_overviewer', c_overviewer_files, include_dirs=['.', numpy_include], extra_link_args=[])) # tell build_ext to build the extension in-place diff --git a/src/endian.c b/src/endian.c new file mode 100644 index 0000000..5b0a105 --- /dev/null +++ b/src/endian.c @@ -0,0 +1,39 @@ +/* + * This file is part of the Minecraft Overviewer. + * + * Minecraft Overviewer is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Minecraft Overviewer 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 General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with the Overviewer. If not, see . + */ + +/* simple routines for dealing with endian conversion */ + +#define UNKNOWN_ENDIAN 0 +#define BIG_ENDIAN 1 +#define LITTLE_ENDIAN 2 + +static int endianness = UNKNOWN_ENDIAN; + +void init_endian() { + /* figure out what our endianness is! */ + short word = 0x0001; + char* byte = (char*)(&word); + endianness = byte[0] ? LITTLE_ENDIAN : BIG_ENDIAN; +} + +unsigned short big_endian_ushort(unsigned short in) { + return (endianness == LITTLE_ENDIAN) ? ((in >> 8) | (in << 8)) : in; +} + +unsigned int big_endian_uint(unsigned int in) { + return (endianness == LITTLE_ENDIAN) ? (((in & 0x000000FF) << 24) + ((in & 0x0000FF00) << 8) + ((in & 0x00FF0000) >> 8) + ((in & 0xFF000000) >> 24)) : in; +} diff --git a/src/main.c b/src/main.c index 32ca491..a8c6075 100644 --- a/src/main.c +++ b/src/main.c @@ -37,4 +37,6 @@ initc_overviewer(void) fprintf(stderr, "failed to init_chunk_render\n"); exit(1); // TODO better way to indicate error? } + + init_endian(); } diff --git a/src/overviewer.h b/src/overviewer.h index ebf83ce..3475fdf 100644 --- a/src/overviewer.h +++ b/src/overviewer.h @@ -78,4 +78,9 @@ PyObject *chunk_render(PyObject *self, PyObject *args); /* pull in the rendermode info */ #include "rendermodes.h" +/* in endian.c */ +void init_endian(); +unsigned short big_endian_ushort(unsigned short in); +unsigned int big_endian_uint(unsigned int in); + #endif /* __OVERVIEWER_H_INCLUDED__ */ diff --git a/src/rendermode-normal.c b/src/rendermode-normal.c index 85e55ed..d7a1367 100644 --- a/src/rendermode-normal.c +++ b/src/rendermode-normal.c @@ -128,16 +128,11 @@ rendermode_normal_draw(void *data, RenderState *state, PyObject *src, PyObject * if (self->biome_data) { /* do the biome stuff! */ unsigned int index; - PyObject *index_py, *color = NULL, *facemask = NULL; + PyObject *color = NULL, *facemask = NULL; unsigned char r, g, b; index = ((self->chunk_y * 16) + state->y) * 16 * 32 + (self->chunk_x * 16) + state->x; - /* TODO for some reason, this one doesn't work: - * index = getArrayShort1D(self->biome_data, index); - */ - index_py = PySequence_GetItem(self->biome_data, index); - index = PyInt_AsLong(index_py); - Py_DECREF(index_py); + index = big_endian_ushort(getArrayShort1D(self->biome_data, index)); switch (state->block) { case 2: