~jtaylor/ubuntu/oneiric/soya/fix-780305

« back to all changes in this revision

Viewing changes to c_src/python/i_world.c

  • Committer: Bazaar Package Importer
  • Author(s): Marc Dequènes (Duck)
  • Date: 2005-01-30 09:55:06 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 hoary)
  • Revision ID: james.westby@ubuntu.com-20050130095506-f21p6v6cgaobhn5j
Tags: 0.9.2-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * P3 python wrapper
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License as published by
6
 
 * the Free Software Foundation; either version 2 of the License, or
7
 
 * (at your option) any later version.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program; if not, write to the Free Software
16
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
 */
18
 
 
19
 
/*****************************************
20
 
 * Copyright (C) 2003 Bertrand 'blam' LAMY
21
 
 *****************************************/
22
 
 
23
 
/*=======+
24
 
 | WORLD |
25
 
 +=======*/
26
 
 
27
 
/*---------+
28
 
 | Methods |
29
 
 +---------*/
30
 
 
31
 
static PyObject* PyP3World_GetBox (P3_world* w) {
32
 
  PyObject* o;
33
 
  P3_point* p;
34
 
  int edited = P3_FALSE;
35
 
  GLfloat box[6] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
36
 
  P3_object_get_box ((P3_any_object*) w, NULL, box, &edited);
37
 
  o = PyTuple_New (2);
38
 
  p = (P3_point*) PyP3Point_Type.tp_alloc (&PyP3Point_Type, 0);
39
 
  p->parent = (P3_coordsys*) w;
40
 
  Py_INCREF ((PyObject*) w);
41
 
  memcpy (p->coord, box, 3 * sizeof (GLfloat));
42
 
  PyTuple_SET_ITEM (o, 0, (PyObject*) p);
43
 
  p = (P3_point*) PyP3Point_Type.tp_alloc (&PyP3Point_Type, 0);
44
 
  p->parent = (P3_coordsys*) w;
45
 
  Py_INCREF ((PyObject*) w);
46
 
  memcpy (p->coord, box + 3, 3 * sizeof (GLfloat));
47
 
  PyTuple_SET_ITEM (o, 1, (PyObject*) p);
48
 
  return o;
49
 
}
50
 
 
51
 
static PyObject* PyP3World_GetDimensions (P3_world* w) {
52
 
  PyObject* tuple;
53
 
  int edited = P3_FALSE;
54
 
  GLfloat box[6] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
55
 
  P3_object_get_box ((P3_any_object*) w, NULL, box, &edited);
56
 
  tuple = PyTuple_New (3);
57
 
  PyTuple_SET_ITEM (tuple, 0, PyFloat_FromDouble ((double) box[3] - box[0]));
58
 
  PyTuple_SET_ITEM (tuple, 1, PyFloat_FromDouble ((double) box[4] - box[1]));
59
 
  PyTuple_SET_ITEM (tuple, 2, PyFloat_FromDouble ((double) box[5] - box[2]));
60
 
  return tuple;
61
 
}
62
 
 
63
 
static PyObject* PyP3World_SetDimensions (P3_world* w, PyObject* args) {
64
 
  GLfloat dim[3];
65
 
  dim[0] = PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (args, 0));
66
 
  dim[1] = PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (args, 1));
67
 
  dim[2] = PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (args, 2));
68
 
  P3_world_set_dimensions (w, dim);
69
 
  Py_INCREF (Py_None);
70
 
  return Py_None;
71
 
}
72
 
 
73
 
static PyObject* PyP3World_GetRaypickContext (P3_world* w, PyObject* args) {
74
 
  PyP3RaypickContext* rc; 
75
 
  P3_coordsys* csys;
76
 
  GLfloat* coord;
77
 
  GLfloat sphere[4];
78
 
//  PyObject* o;
79
 
//  PyObject* p;
80
 
  /* args are: (point, radius, [raypick_context]) */
81
 
  if (PySequence_Size (args) == 3) {
82
 
    rc = (PyP3RaypickContext*) PySequence_Fast_GET_ITEM (args, 2);
83
 
  } else {
84
 
    rc = (PyP3RaypickContext*) PyP3RaypickContext_Type.tp_alloc (&PyP3RaypickContext_Type, 0);
85
 
    rc->chunk = P3_chunk_new ();
86
 
  }
87
 
  PyP3_GetPosition (PySequence_Fast_GET_ITEM (args, 0), &coord, &csys);
88
 
  memcpy (sphere, coord, 3 * sizeof (GLfloat));
89
 
/*
90
 
  o = PySequence_Fast_GET_ITEM (args, 0);
91
 
  p = PyObject_GetAttrString (o, "x");
92
 
  sphere[0] = (GLfloat) PyFloat_AS_DOUBLE (p);
93
 
  Py_DECREF (p);
94
 
  p = PyObject_GetAttrString (o, "y");
95
 
  sphere[1] = (GLfloat) PyFloat_AS_DOUBLE (p);
96
 
  Py_DECREF (p);
97
 
  p = PyObject_GetAttrString (o, "z");
98
 
  sphere[2] = (GLfloat) PyFloat_AS_DOUBLE (p);
99
 
  Py_DECREF (p);
100
 
  p = PyObject_GetAttrString (o, "parent");
101
 
  if (p != Py_None) P3_point_by_matrix (sphere, P3_coordsys_get_root_matrix ((P3_coordsys*) p));
102
 
  Py_DECREF (p);
103
 
*/
104
 
  sphere[3] = PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (args, 1));
105
 
  if (csys != NULL) P3_point_by_matrix (sphere, P3_coordsys_get_root_matrix (csys));
106
 
  P3_get_raypick_context ((P3_any_object*) w, NULL, sphere, sphere, rc->chunk);
107
 
  return (PyObject*) rc;
108
 
}
109
 
 
110
 
static PyObject* PyP3World_BuildMesh (P3_world* w, PyObject* args) {
111
 
  P3_xmesh_from_world ((P3_xmesh*) PySequence_Fast_GET_ITEM (args, 0), w, 
112
 
    P3_to_radians ((GLfloat) PyFloat_AS_DOUBLE (PySequence_Fast_GET_ITEM (args, 1))),
113
 
    (int) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 2)));
114
 
  Py_INCREF (Py_None);
115
 
  return Py_None;
116
 
}
117
 
 
118
 
/*
119
 
static PyObject* PyP3World_BuildMorph (P3_world* w) {
120
 
  P3_morph_data* data;
121
 
  data = (P3_morph_data*) PyObject_CallMethod (P3Module, "new_morph", NULL, NULL);
122
 
    if (PyErr_Occurred() != NULL) { PyErr_Print(); }
123
 
  return (PyObject*) P3_morph_data_from_world (data, w);
124
 
}
125
 
*/
126
 
 
127
 
static int PyP3World_Init (P3_world* a, PyObject* args, PyObject* kwds) {
128
 
  P3_world_new (a);
129
 
  return 0;
130
 
}
131
 
 
132
 
static void PyP3World_Dealloc (P3_world* a) {
133
 
  PyObject_GC_UnTrack ((PyObject*) a);
134
 
  Py_XDECREF (a->shape);
135
 
  Py_XDECREF (a->parent);
136
 
  Py_XDECREF (a->children);
137
 
  Py_XDECREF (a->atmosphere);
138
 
  a->ob_type->tp_free ((PyObject*) a); 
139
 
}
140
 
 
141
 
static int PyP3World_Traverse (P3_world* a, visitproc visit, void* arg) {
142
 
  int err;
143
 
  if (a->shape != NULL) {
144
 
    err = visit ((PyObject*) a->shape, arg);
145
 
    if (err) { return err; }
146
 
  }
147
 
  if (a->parent != NULL) {
148
 
    err = visit ((PyObject*) a->parent, arg);
149
 
    if (err) { return err; }
150
 
  }
151
 
  if (a->children != NULL) {
152
 
    err = visit ((PyObject*) a->children, arg);
153
 
    if (err) { return err; }
154
 
  }
155
 
  if (a->atmosphere != NULL) {
156
 
    err = visit ((PyObject*) a->atmosphere, arg);
157
 
    if (err) { return err; }
158
 
  }
159
 
  return 0;
160
 
}
161
 
 
162
 
static int PyP3World_Clear (P3_world* a) {
163
 
  Py_XDECREF (a->shape);
164
 
  a->shape = NULL;
165
 
  Py_XDECREF (a->parent);
166
 
  a->parent = NULL;
167
 
  Py_XDECREF (a->children);
168
 
  a->children = NULL;
169
 
  Py_XDECREF (a->atmosphere);
170
 
  a->atmosphere = NULL;
171
 
  return 0;
172
 
}
173
 
 
174
 
static PyObject* PyP3World_GetState (P3_world* a) {
175
 
  P3_chunk* chunk = P3_chunk_new ();
176
 
  PyObject* tuple;
177
 
  P3_world_get_data (a, chunk);
178
 
  tuple = PyTuple_New (5);
179
 
  PyTuple_SET_ITEM (tuple, 0, PyString_FromStringAndSize ((char*) chunk->content, chunk->nb));
180
 
// HACK because previous object saved their parent
181
 
  Py_INCREF (Py_None);
182
 
  PyTuple_SET_ITEM (tuple, 1, Py_None);
183
 
  if (a->shape == NULL) {
184
 
    Py_INCREF (Py_None);
185
 
    PyTuple_SET_ITEM (tuple, 2, Py_None);
186
 
  } else {
187
 
    Py_INCREF ((PyObject*) a->shape);
188
 
    PyTuple_SET_ITEM (tuple, 2, (PyObject*) a->shape);
189
 
  }
190
 
  if (a->children == NULL) {
191
 
    Py_INCREF (Py_None);
192
 
    PyTuple_SET_ITEM (tuple, 3, Py_None);
193
 
  } else {
194
 
    Py_INCREF ((PyObject*) a->children);
195
 
    PyTuple_SET_ITEM (tuple, 3, (PyObject*) a->children);
196
 
  }
197
 
  if (a->atmosphere == NULL) {
198
 
    Py_INCREF (Py_None);
199
 
    PyTuple_SET_ITEM (tuple, 4, Py_None);
200
 
  } else {
201
 
    Py_INCREF ((PyObject*) a->atmosphere);
202
 
    PyTuple_SET_ITEM (tuple, 4, (PyObject*) a->atmosphere);
203
 
  }
204
 
  P3_chunk_dealloc (chunk);
205
 
  return tuple;
206
 
}
207
 
 
208
 
static PyObject* PyP3World_SetState (P3_world* a, PyObject* args) {
209
 
  P3_chunk* chunk = P3_chunk_new ();
210
 
  PyObject* o;
211
 
  int i;
212
 
  o = PySequence_Fast_GET_ITEM (args, 0);
213
 
  chunk->content = PyString_AS_STRING (o);
214
 
  P3_world_set_data (a, chunk);
215
 
  a->shape = (P3_any_object*) PySequence_Fast_GET_ITEM (args, 2);
216
 
  if ((PyObject*) a->shape == Py_None) {
217
 
    a->shape = NULL;
218
 
  } else {
219
 
    Py_INCREF ((PyObject*) a->shape);
220
 
  }
221
 
  a->children = (P3_children) PySequence_Fast_GET_ITEM (args, 3);
222
 
  if ((PyObject*) a->children == Py_None) {
223
 
    a->children = NULL;
224
 
  } else {
225
 
    Py_INCREF ((PyObject*) a->children);
226
 
  }
227
 
  a->atmosphere = (P3_atmosphere*) PySequence_Fast_GET_ITEM (args, 4);
228
 
  if ((PyObject*) a->atmosphere == Py_None) {
229
 
    a->atmosphere = NULL;
230
 
  } else {
231
 
    Py_INCREF ((PyObject*) a->atmosphere);
232
 
  }
233
 
  for (i = 0; i < P3_children_size (a->children); i++) {
234
 
    o = (PyObject*) P3_children_get (a->children, i);
235
 
    PyObject_SetAttrString (o, "_parent", (PyObject*) a);
236
 
  }
237
 
  free (chunk);
238
 
  Py_INCREF (Py_None);
239
 
  return Py_None;
240
 
}
241
 
 
242
 
static PyMethodDef PyP3World_Methods[] = {
243
 
 
244
 
//  PYP3_COORDSYS_FUNCS,
245
 
 
246
 
  { "_getstate",      (PyCFunction) PyP3World_GetState,          METH_NOARGS },
247
 
  { "_setstate",      (PyCFunction) PyP3World_SetState,          METH_O },
248
 
  { "set_dimensions", (PyCFunction) PyP3World_SetDimensions,     METH_VARARGS },
249
 
  { "get_dimensions", (PyCFunction) PyP3World_GetDimensions,     METH_NOARGS },
250
 
  { "get_box",        (PyCFunction) PyP3World_GetBox,            METH_NOARGS },
251
 
  { "_to_shape",      (PyCFunction) PyP3World_BuildMesh,         METH_VARARGS },
252
 
//  { "_to_morph",      (PyCFunction) PyP3World_BuildMorph,        METH_NOARGS },
253
 
  { "RaypickContext", (PyCFunction) PyP3World_GetRaypickContext, METH_VARARGS },
254
 
  { NULL, NULL } /* sentinel */
255
 
};
256
 
 
257
 
/*---------+
258
 
 | Get Set |
259
 
 +---------*/
260
 
 
261
 
PY_GET_SET_ON_OBJECT (World, P3_world*, Atmosphere, atmosphere, P3_atmosphere*)
262
 
PY_GET_SET_ON_OBJECT (World, P3_world*, Children, children, PyObject*)
263
 
 
264
 
static PyGetSetDef PyP3World_GetSets[] = {
265
 
//  PYP3_CHILD_GETSETS,
266
 
//  PYP3_SOLID_GETSETS,
267
 
//  PYP3_COORDSYS_GETSETS,
268
 
//  PYP3_VISIBLE_GETSETS,
269
 
 
270
 
  { "children",   (getter) PyP3World_GetChildren,   (setter) PyP3World_SetChildren,   NULL },
271
 
  { "atmosphere", (getter) PyP3World_GetAtmosphere, (setter) PyP3World_SetAtmosphere, NULL },
272
 
 
273
 
//  { "shape",      (getter) PyP3Volume_GetShape,     (setter) PyP3Volume_SetShape,     NULL },
274
 
 
275
 
  { NULL }
276
 
};
277
 
 
278
 
/*------+
279
 
 | Type |
280
 
 +------*/
281
 
 
282
 
PyTypeObject PyP3World_Type = {
283
 
  PyObject_HEAD_INIT(NULL)
284
 
  0,
285
 
  "_soya._World",
286
 
  sizeof(P3_world),
287
 
  0,
288
 
  (destructor) PyP3World_Dealloc,/* tp_dealloc */
289
 
  0,/* tp_print */
290
 
  0,/* tp_getattr */
291
 
  0,/* tp_setattr */
292
 
  0,/* tp_compare */
293
 
  0,/* tp_repr */
294
 
  0,/* tp_as_number */
295
 
  0,/* tp_as_sequence */
296
 
  0,/* tp_as_mapping */
297
 
  0,/* tp_hash */
298
 
  0,/* tp_call */
299
 
  0,/* tp_str */
300
 
  PYP3_GENERIC_GETATTR,/* tp_getattro */
301
 
  0,/* tp_setattro */
302
 
  0,/* tp_as_buffer */
303
 
  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,/* tp_flags */
304
 
  0,/* tp_doc */
305
 
  (traverseproc) PyP3World_Traverse,/* tp_traverse */
306
 
  (inquiry) PyP3World_Clear,/* tp_clear */
307
 
  0,/* tp_richcompare */
308
 
  0,/* tp_weaklistoffset */
309
 
  0,/* tp_iter */
310
 
  0,/* tp_iternext */
311
 
  (PyMethodDef*) &PyP3World_Methods,/* tp_methods */
312
 
  0,/* tp_members */
313
 
  (PyGetSetDef*) &PyP3World_GetSets,/* tp_getset */
314
 
  &PyP3Volume_Type,/* tp_base */
315
 
  0,/* tp_dict */
316
 
  0,/* tp_descr_get */
317
 
  0,/* tp_descr_set */
318
 
  0,/* tp_dictoffset */
319
 
  (initproc) PyP3World_Init,/* tp_init */
320
 
  PYP3_GENERIC_ALLOC,/* tp_alloc */
321
 
  (newfunc) PyP3Object_New,/* tp_new */
322
 
  PYP3_GENERIC_GC_FREE,/* tp_free */
323
 
};
324