0

added render_mode_parse_option helper function

This commit is contained in:
Aaron Griffith
2011-06-19 13:06:12 -04:00
parent 57100c1912
commit f8b005d079
6 changed files with 74 additions and 64 deletions

View File

@@ -320,7 +320,7 @@ chunk_render(PyObject *self, PyObject *args) {
state.rendermode = rendermode = render_mode_create(PyString_AsString(rendermode_py), &state); state.rendermode = rendermode = render_mode_create(PyString_AsString(rendermode_py), &state);
Py_DECREF(rendermode_py); Py_DECREF(rendermode_py);
if (rendermode == NULL) { if (rendermode == NULL) {
return Py_BuildValue("i", "-1"); return NULL;
} }
/* get the image size */ /* get the image size */

View File

@@ -178,7 +178,6 @@ static int
rendermode_cave_start(void *data, RenderState *state, PyObject *options) { rendermode_cave_start(void *data, RenderState *state, PyObject *options) {
RenderModeCave* self; RenderModeCave* self;
int ret; int ret;
PyObject *opt;
self = (RenderModeCave *)data; self = (RenderModeCave *)data;
/* first, chain up */ /* first, chain up */
@@ -186,26 +185,17 @@ rendermode_cave_start(void *data, RenderState *state, PyObject *options) {
if (ret != 0) if (ret != 0)
return ret; return ret;
opt = PyDict_GetItemString(options, "depth_tinting"); self->depth_tinting = 1;
if (opt) { if (!render_mode_parse_option(options, "depth_tinting", "i", &(self->depth_tinting)))
self->depth_tinting = PyObject_IsTrue(opt); return 1;
} else {
self->depth_tinting = 1;
}
opt = PyDict_GetItemString(options, "only_lit"); self->only_lit = 0;
if (opt) { if (!render_mode_parse_option(options, "only_lit", "i", &(self->only_lit)))
self->only_lit = PyObject_IsTrue(opt); return 1;
} else {
self->only_lit = 0; self->lighting = 0;
} if (!render_mode_parse_option(options, "lighting", "i", &(self->lighting)))
return 1;
opt = PyDict_GetItemString(options, "lighting");
if (opt) {
self->lighting = PyObject_IsTrue(opt);
} else {
self->lighting = 0;
}
/* if there's skylight we are in the surface! */ /* if there's skylight we are in the surface! */
self->skylight = PyObject_GetAttrString(state->self, "skylight"); self->skylight = PyObject_GetAttrString(state->self, "skylight");

View File

@@ -168,7 +168,6 @@ do_shading_with_mask(RenderModeLighting *self, RenderState *state,
static int static int
rendermode_lighting_start(void *data, RenderState *state, PyObject *options) { rendermode_lighting_start(void *data, RenderState *state, PyObject *options) {
PyObject *opt;
RenderModeLighting* self; RenderModeLighting* self;
/* first, chain up */ /* first, chain up */
@@ -178,16 +177,9 @@ rendermode_lighting_start(void *data, RenderState *state, PyObject *options) {
self = (RenderModeLighting *)data; self = (RenderModeLighting *)data;
opt = PyDict_GetItemString(options, "shade_strength"); self->shade_strength = 1.0;
if (opt) { if (!render_mode_parse_option(options, "shade_strength", "f", &(self->shade_strength)))
if (!PyNumber_Check(opt)) { return 1;
PyErr_SetString(PyExc_TypeError, "'shade_strength' must be a number");
return 1;
}
self->shade_strength = PyFloat_AsDouble(opt);
} else {
self->shade_strength = 1.0;
}
self->black_color = PyObject_GetAttrString(state->chunk, "black_color"); self->black_color = PyObject_GetAttrString(state->chunk, "black_color");
self->facemasks_py = PyObject_GetAttrString(state->chunk, "facemasks"); self->facemasks_py = PyObject_GetAttrString(state->chunk, "facemasks");

View File

@@ -19,44 +19,23 @@
static int static int
rendermode_normal_start(void *data, RenderState *state, PyObject *options) { rendermode_normal_start(void *data, RenderState *state, PyObject *options) {
PyObject *opt, *chunk_x_py, *chunk_y_py, *world, *use_biomes, *worlddir; PyObject *chunk_x_py, *chunk_y_py, *world, *use_biomes, *worlddir;
RenderModeNormal *self = (RenderModeNormal *)data; RenderModeNormal *self = (RenderModeNormal *)data;
/* load up the given options, first */ /* load up the given options, first */
opt = PyDict_GetItemString(options, "edge_opacity"); self->edge_opacity = 0.15;
if (opt) { if (!render_mode_parse_option(options, "edge_opacity", "f", &(self->edge_opacity)))
if (!PyNumber_Check(opt)) { return 1;
PyErr_SetString(PyExc_TypeError, "'edge_opacity' must be a number");
return 1;
}
self->edge_opacity = PyFloat_AsDouble(opt);
} else {
self->edge_opacity = 0.15;
}
opt = PyDict_GetItemString(options, "min_depth"); self->min_depth = 0;
if (opt) { if (!render_mode_parse_option(options, "min_depth", "I", &(self->min_depth)))
if (!PyInt_Check(opt)) { return 1;
PyErr_SetString(PyExc_TypeError, "'min_depth' must be an integer");
return 1; self->max_depth = 127;
} if (!render_mode_parse_option(options, "max_depth", "I", &(self->max_depth)))
self->min_depth = PyInt_AsLong(opt); return 1;
} else {
self->min_depth = 0;
}
opt = PyDict_GetItemString(options, "max_depth");
if (opt) {
if (!PyInt_Check(opt)) {
PyErr_SetString(PyExc_TypeError, "'max_depth' must be an integer");
return 1;
}
self->max_depth = PyInt_AsLong(opt);
} else {
self->max_depth = 127;
}
chunk_x_py = PyObject_GetAttrString(state->self, "chunkX"); chunk_x_py = PyObject_GetAttrString(state->self, "chunkX");
chunk_y_py = PyObject_GetAttrString(state->self, "chunkY"); chunk_y_py = PyObject_GetAttrString(state->self, "chunkY");

View File

@@ -17,6 +17,7 @@
#include "overviewer.h" #include "overviewer.h"
#include <string.h> #include <string.h>
#include <stdarg.h>
/* list of all render modes, ending in NULL /* list of all render modes, ending in NULL
all of these will be available to the user, so DON'T include modes all of these will be available to the user, so DON'T include modes
@@ -174,6 +175,50 @@ void render_mode_draw(RenderMode *self, PyObject *img, PyObject *mask, PyObject
self->iface->draw(self->mode, self->state, img, mask, mask_light); self->iface->draw(self->mode, self->state, img, mask, mask_light);
} }
/* options parse helper */
int render_mode_parse_option(PyObject *dict, const char *name, const char *format, ...) {
va_list ap;
PyObject *item;
int ret;
if (dict == NULL || name == NULL)
return 1;
item = PyDict_GetItemString(dict, name);
if (item == NULL)
return 1;
/* make sure the item we're parsing is a tuple
for VaParse to work correctly */
if (!PyTuple_Check(item)) {
item = PyTuple_Pack(1, item);
} else {
Py_INCREF(item);
}
va_start(ap, format);
ret = PyArg_VaParse(item, format, ap);
va_end(ap);
Py_DECREF(item);
if (!ret) {
PyObject *errtype, *errvalue, *errtraceback;
const char *errstring;
PyErr_Fetch(&errtype, &errvalue, &errtraceback);
errstring = PyString_AsString(errvalue);
PyErr_Format(PyExc_TypeError, "rendermode option \"%s\" has incorrect type (%s)", name, errstring);
Py_DECREF(errtype);
Py_DECREF(errvalue);
Py_XDECREF(errtraceback);
}
return ret;
}
/* bindings for python -- get all the rendermode names */ /* bindings for python -- get all the rendermode names */
PyObject *get_render_modes(PyObject *self, PyObject *args) { PyObject *get_render_modes(PyObject *self, PyObject *args) {
PyObject *modes; PyObject *modes;

View File

@@ -81,6 +81,10 @@ void render_mode_destroy(RenderMode *self);
int render_mode_occluded(RenderMode *self, int x, int y, int z); int render_mode_occluded(RenderMode *self, int x, int y, int z);
void render_mode_draw(RenderMode *self, PyObject *img, PyObject *mask, PyObject *mask_light); void render_mode_draw(RenderMode *self, PyObject *img, PyObject *mask, PyObject *mask_light);
/* helper function for reading in rendermode options
works like PyArg_ParseTuple on a dictionary item */
int render_mode_parse_option(PyObject *dict, const char *name, const char *format, ...);
/* python metadata bindings */ /* python metadata bindings */
PyObject *get_render_modes(PyObject *self, PyObject *args); PyObject *get_render_modes(PyObject *self, PyObject *args);
PyObject *get_render_mode_info(PyObject *self, PyObject *args); PyObject *get_render_mode_info(PyObject *self, PyObject *args);