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

« back to all changes in this revision

Viewing changes to c_src/python/i_funcs.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) 2002 Bertrand 'blam' LAMY
21
 
 *****************************************/
22
 
 
23
 
/*=========*
24
 
 * MATH 3D *
25
 
 *=========*/
26
 
 
27
 
static PyObject* PyP3_PointsNormal (PyObject* module, PyObject* args) {
28
 
  P3_coordsys* csys;
29
 
  P3_vector* v;
30
 
  GLfloat* a;
31
 
  GLfloat b[3];
32
 
  GLfloat c[3];
33
 
  PyP3_GetPosition (PySequence_Fast_GET_ITEM (args, 0), &a, &csys);
34
 
  PyP3_GetPositionInto (PySequence_Fast_GET_ITEM (args, 1), csys, b);
35
 
  PyP3_GetPositionInto (PySequence_Fast_GET_ITEM (args, 2), csys, c);
36
 
  if (PySequence_Size (args) == 4) {
37
 
    v = (P3_vector*) PySequence_Fast_GET_ITEM (args, 3);
38
 
    Py_XDECREF ((PyObject*) v->parent);
39
 
    v->parent = csys;
40
 
    if (csys != NULL) Py_INCREF ((PyObject*) csys);
41
 
    P3_face_normal (v->coord, a, b, c);
42
 
    Py_INCREF (Py_None);
43
 
    return Py_None;
44
 
  } else {
45
 
    v = (P3_vector*) PyP3Vector_Type.tp_alloc (&PyP3Vector_Type, 0);
46
 
    v->parent = csys;
47
 
    if (csys != NULL) Py_INCREF ((PyObject*) csys);
48
 
    P3_face_normal (v->coord, a, b, c);
49
 
    return (PyObject*) v;
50
 
  }
51
 
}
52
 
 
53
 
 
54
 
/*==================*
55
 
 * ENGINE FUNCTIONS *
56
 
 *==================*/
57
 
 
58
 
static PyObject* PyP3_ActivateMaterial (PyObject* module, P3_material* a) {
59
 
  if ((PyObject*) a == Py_None) { a = NULL; }
60
 
  P3_material_activate (a);
61
 
  Py_INCREF (Py_None);
62
 
  return Py_None;
63
 
}
64
 
 
65
 
static PyObject* PyP3_GetScreenWidth (PyObject* module) {
66
 
  return PyInt_FromLong ((long) screen_w);
67
 
}
68
 
 
69
 
static PyObject* PyP3_GetScreenHeight (PyObject* module) {
70
 
  return PyInt_FromLong ((long) screen_h);
71
 
}
72
 
 
73
 
static PyObject* PyP3_GetCamera (PyObject* module) {
74
 
  if (renderer->c_camera == NULL) {
75
 
    Py_INCREF (Py_None);
76
 
    return Py_None;
77
 
  }
78
 
  Py_INCREF ((PyObject*) renderer->c_camera);
79
 
  return (PyObject*) renderer->c_camera;
80
 
}
81
 
 
82
 
static PyObject* PyP3_RendererAdd (PyObject* module, PyObject* args) {
83
 
  long i;
84
 
  P3_instance* parent;
85
 
  parent = (P3_instance*) PySequence_Fast_GET_ITEM (args, 1);
86
 
  i      = PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 2));
87
 
  if (i == 0) {
88
 
    P3_renderer_add (PySequence_Fast_GET_ITEM (args, 0), parent);
89
 
  } else if (i == 1) {
90
 
    P3_renderer_add_alpha (PySequence_Fast_GET_ITEM (args, 0), parent);
91
 
  } else if (i == 2) {
92
 
    void* o;
93
 
    o = PySequence_Fast_GET_ITEM (args, 0);
94
 
    P3_renderer_add       (o, parent);
95
 
    P3_renderer_add_alpha (o, parent);
96
 
  }
97
 
  Py_INCREF (Py_None);
98
 
  return Py_None;
99
 
}
100
 
 
101
 
static PyObject* PyP3_SetQuality (PyObject* module, PyObject* arg) {
102
 
  P3_set_quality ((int) PyInt_AS_LONG (arg));
103
 
  Py_INCREF (Py_None);
104
 
  return Py_None;
105
 
}
106
 
 
107
 
static PyObject* PyP3_SetShadow (PyObject* module, PyObject* arg) {
108
 
  if (PyObject_IsTrue (arg)) engine_option |=  P3_SHADOWS;
109
 
  else                       engine_option &= ~P3_SHADOWS;
110
 
  Py_INCREF (Py_None);
111
 
  return Py_None;
112
 
}
113
 
 
114
 
static PyObject* PyP3_ToggleWireFrame (PyObject* module) {
115
 
  P3_toggle_wireframe_mode ();
116
 
  Py_INCREF (Py_None);
117
 
  return Py_None;
118
 
}
119
 
 
120
 
static PyObject* PyP3_MinimizeWindow (PyObject* module) {
121
 
#ifdef USE_SDL
122
 
  SDL_WM_IconifyWindow ();
123
 
#endif /* USE_SDL */
124
 
  Py_INCREF (Py_None);
125
 
  return Py_None;
126
 
}
127
 
 
128
 
static PyObject* PyP3_ToggleFullscreen (PyObject* module) {
129
 
#ifdef USE_SDL
130
 
  SDL_WM_ToggleFullScreen (screen);
131
 
#endif /* USE_SDL */
132
 
  Py_INCREF (Py_None);
133
 
  return Py_None;
134
 
}
135
 
 
136
 
static PyObject* PyP3_SetCursorVisibility (PyObject* module, PyObject* arg) {
137
 
#ifdef USE_GLX
138
 
 
139
 
  static Cursor blank_cursor = 0;
140
 
 
141
 
  if (blank_cursor == 0) {
142
 
    blank_cursor = XCreateFontCursor (display, 0);
143
 
  }
144
 
 
145
 
// TO DO
146
 
  if (PyObject_IsTrue (arg) == 1) {
147
 
    XUndefineCursor (display, window);
148
 
  } else {
149
 
    XDefineCursor (display, window, blank_cursor);
150
 
  }
151
 
#endif /* USE_GLX */
152
 
#ifdef USE_SDL
153
 
  if (PyObject_IsTrue (arg) == 1) {
154
 
    SDL_ShowCursor (SDL_ENABLE);
155
 
  } else {
156
 
    SDL_ShowCursor (SDL_DISABLE);
157
 
  }
158
 
#endif /* USE_SDL */
159
 
  Py_INCREF (Py_None);
160
 
  return Py_None;
161
 
}
162
 
 
163
 
static PyObject* PyP3_SetCursorPosition (PyObject* module, PyObject* args) {
164
 
#ifdef USE_GLX
165
 
  XWarpPointer (display, window, window, 0, 0, 0, 0, PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 0)), PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 1)));
166
 
#endif /* USE_GLX */
167
 
#ifdef USE_SDL
168
 
  SDL_WarpMouse ((Uint16) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 0)), (Uint16) PyInt_AS_LONG (PySequence_Fast_GET_ITEM (args, 1)));
169
 
#endif /* USE_SDL */
170
 
  Py_INCREF (Py_None);
171
 
  return Py_None;
172
 
}
173
 
 
174
 
static PyObject* PyP3_Init (PyObject* module, PyObject* args) {
175
 
  PyObject* o;
176
 
  char* str;
177
 
  P3_base_init ();
178
 
  if (args != NULL && args != Py_None) {
179
 
    if (PySequence_Size (args) == 5) {
180
 
      o = PySequence_Fast_GET_ITEM (args, 0);
181
 
      if (o == NULL || o == Py_None) {
182
 
        str = NULL;
183
 
      } else {
184
 
        str = PyString_AsString (o);
185
 
      }
186
 
      P3_init_video (str, 
187
 
                     (int) PyInt_AsLong (PySequence_Fast_GET_ITEM (args, 1)), 
188
 
                     (int) PyInt_AsLong (PySequence_Fast_GET_ITEM (args, 2)), 
189
 
                     PyObject_IsTrue (PySequence_Fast_GET_ITEM (args, 3)), 
190
 
                     PyObject_IsTrue (PySequence_Fast_GET_ITEM (args, 4)));
191
 
    } else if (PySequence_Size (args) == 2) {
192
 
      screen_w = (int) PyInt_AsLong (PySequence_Fast_GET_ITEM (args, 0));
193
 
      screen_h = (int) PyInt_AsLong (PySequence_Fast_GET_ITEM (args, 1));
194
 
      P3_init_GL ();
195
 
    }
196
 
  }
197
 
  Py_INCREF (Py_None);
198
 
  return Py_None;
199
 
}
200
 
 
201
 
static PyObject* PyP3_SetVideo (PyObject* module, PyObject* args) {
202
 
  P3_set_video ((int) PyInt_AsLong (PySequence_Fast_GET_ITEM (args, 0)), 
203
 
                (int) PyInt_AsLong (PySequence_Fast_GET_ITEM (args, 1)), 
204
 
                PyObject_IsTrue (PySequence_Fast_GET_ITEM (args, 2)), 
205
 
                PyObject_IsTrue (PySequence_Fast_GET_ITEM (args, 3)));
206
 
  Py_INCREF (Py_None);
207
 
  return Py_None;
208
 
}
209
 
 
210
 
static PyObject* PyP3_Quit (PyObject* module) {
211
 
  P3_base_quit ();
212
 
  Py_INCREF (Py_None);
213
 
  return Py_None;
214
 
}
215
 
 
216
 
static PyObject* PyP3_Display (PyObject* module) {
217
 
  if (engine_option & P3_GL_INITED) {
218
 
    glPrintError ();
219
 
#ifdef USE_GLX
220
 
    glXSwapBuffers (display, window);
221
 
#endif /* USE_GLX */
222
 
#ifdef USE_SDL
223
 
    SDL_GL_SwapBuffers ();
224
 
#endif /* USE_SDL */
225
 
  }
226
 
  Py_INCREF (Py_None);
227
 
  return Py_None;
228
 
}
229
 
 
230
 
static PyObject* PyP3_Screenshot (PyObject* module, PyObject* arg) {
231
 
  P3_screenshot (PyString_AS_STRING (arg));
232
 
  Py_INCREF (Py_None);
233
 
  return Py_None;
234
 
}
235
 
 
236
 
 
237
 
/*========*
238
 
 * EVENTS *
239
 
 *========*/
240
 
 
241
 
static PyObject* PyP3_GetModifier (PyObject* module) {
242
 
#ifdef USE_GLX
243
 
  Window root_return;
244
 
  Window child_return;
245
 
  int root_x_return;
246
 
  int root_y_return;
247
 
  int win_x_return;
248
 
  int win_y_return;
249
 
  unsigned int mask_return;
250
 
  if (XQueryPointer (display, window, &root_return, &child_return, &root_x_return, &root_y_return, &win_x_return, &win_y_return, &mask_return) == True) {
251
 
    return PyInt_FromLong (mask_return);
252
 
  } else {
253
 
    return PyInt_FromLong (0);
254
 
  }
255
 
#endif /* USE_GLX */
256
 
#ifdef USE_SDL
257
 
  return PyInt_FromLong (SDL_GetModState ());
258
 
#endif /* USE_SDL */
259
 
}
260
 
 
261
 
static PyObject* PyP3_ProcessEvent (PyObject* module) {
262
 
  PyObject* tuple;
263
 
  PyObject* list;
264
 
 
265
 
#ifdef USE_GLX
266
 
  XEvent event;
267
 
  static int last_pointer_x;
268
 
  static int last_pointer_y;
269
 
  list = PyList_New (0);
270
 
  while (XCheckWindowEvent (display, window, ResizeRedirectMask | ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &event) == True) {
271
 
    switch (event.type) {
272
 
    case KeyPress:
273
 
    case KeyRelease:
274
 
      tuple = PyTuple_New (3);
275
 
      PyTuple_SET_ITEM (tuple, 0, PyInt_FromLong (event.type));
276
 
      PyTuple_SET_ITEM (tuple, 1, PyInt_FromLong (event.xkey.keycode));
277
 
      PyTuple_SET_ITEM (tuple, 2, PyInt_FromLong (event.xkey.state));
278
 
      PyList_Append (list, tuple);
279
 
      Py_DECREF (tuple);
280
 
      break;
281
 
    case ButtonPress:
282
 
    case ButtonRelease:
283
 
      tuple = PyTuple_New (4);
284
 
      PyTuple_SET_ITEM (tuple, 0, PyInt_FromLong (event.type));
285
 
      PyTuple_SET_ITEM (tuple, 1, PyInt_FromLong (event.xbutton.button));
286
 
      PyTuple_SET_ITEM (tuple, 2, PyInt_FromLong (event.xbutton.x));
287
 
      PyTuple_SET_ITEM (tuple, 3, PyInt_FromLong (event.xbutton.y));
288
 
      PyList_Append (list, tuple);
289
 
      Py_DECREF (tuple);
290
 
      break;
291
 
    case MotionNotify:
292
 
      tuple = PyTuple_New (6);
293
 
      PyTuple_SET_ITEM (tuple, 0, PyInt_FromLong (MotionNotify));
294
 
      PyTuple_SET_ITEM (tuple, 1, PyInt_FromLong (event.xmotion.x));
295
 
      PyTuple_SET_ITEM (tuple, 2, PyInt_FromLong (event.xmotion.y));
296
 
      PyTuple_SET_ITEM (tuple, 3, PyInt_FromLong (last_pointer_x - event.xmotion.x));
297
 
      PyTuple_SET_ITEM (tuple, 4, PyInt_FromLong (last_pointer_y - event.xmotion.y));
298
 
      PyTuple_SET_ITEM (tuple, 5, PyInt_FromLong (event.xmotion.state));
299
 
      PyList_Append (list, tuple);
300
 
      Py_DECREF (tuple);
301
 
      last_pointer_x = event.xmotion.x;
302
 
      last_pointer_y = event.xmotion.y;
303
 
      break;
304
 
    case Expose:
305
 
    case GraphicsExpose:
306
 
      tuple = PyTuple_New (1);
307
 
      PyTuple_SET_ITEM (tuple, 0, PyInt_FromLong (GraphicsExpose));
308
 
      PyList_Append (list, tuple);
309
 
      Py_DECREF (tuple);
310
 
      break;
311
 
    case ResizeRequest:
312
 
      if (engine_option & P3_FULLSCREEN) {
313
 
        P3_set_video (event.xresizerequest.width, event.xresizerequest.height, P3_TRUE, P3_TRUE);
314
 
      } else {
315
 
        P3_set_video (event.xresizerequest.width, event.xresizerequest.height, P3_FALSE, P3_TRUE);
316
 
      }
317
 
      tuple = PyTuple_New (3);
318
 
      PyTuple_SET_ITEM (tuple, 0, PyInt_FromLong (ResizeRequest));
319
 
      PyTuple_SET_ITEM (tuple, 1, PyInt_FromLong (event.xresizerequest.width));
320
 
      PyTuple_SET_ITEM (tuple, 2, PyInt_FromLong (event.xresizerequest.height));
321
 
      PyList_Append (list, tuple);
322
 
      Py_DECREF (tuple);
323
 
      break;
324
 
    }
325
 
  }
326
 
#endif /* USE_GLX */
327
 
 
328
 
#ifdef USE_SDL
329
 
  SDL_Event event;
330
 
  list = PyList_New (0);
331
 
  while (SDL_PollEvent (&event)) {
332
 
    switch (event.type) {
333
 
    case SDL_KEYDOWN:
334
 
    case SDL_KEYUP:
335
 
      tuple = PyTuple_New (3);
336
 
      PyTuple_SET_ITEM (tuple, 0, PyInt_FromLong (event.type));
337
 
      PyTuple_SET_ITEM (tuple, 1, PyInt_FromLong (event.key.keysym.sym));
338
 
      PyTuple_SET_ITEM (tuple, 2, PyInt_FromLong (event.key.keysym.mod));
339
 
      PyList_Append (list, tuple);
340
 
      Py_DECREF (tuple);
341
 
      break;
342
 
    case SDL_MOUSEMOTION:
343
 
      tuple = PyTuple_New (6);
344
 
      PyTuple_SET_ITEM (tuple, 0, PyInt_FromLong (SDL_MOUSEMOTION));
345
 
      PyTuple_SET_ITEM (tuple, 1, PyInt_FromLong (event.motion.x));
346
 
      PyTuple_SET_ITEM (tuple, 2, PyInt_FromLong (event.motion.y));
347
 
      PyTuple_SET_ITEM (tuple, 3, PyInt_FromLong (event.motion.xrel));
348
 
      PyTuple_SET_ITEM (tuple, 4, PyInt_FromLong (event.motion.yrel));
349
 
      PyTuple_SET_ITEM (tuple, 5, PyInt_FromLong (event.motion.state));
350
 
      PyList_Append (list, tuple);
351
 
      Py_DECREF (tuple);
352
 
      break;
353
 
    case SDL_MOUSEBUTTONDOWN:
354
 
    case SDL_MOUSEBUTTONUP:
355
 
      tuple = PyTuple_New (4);
356
 
      PyTuple_SET_ITEM (tuple, 0, PyInt_FromLong (event.type));
357
 
      PyTuple_SET_ITEM (tuple, 1, PyInt_FromLong (event.button.button));
358
 
      PyTuple_SET_ITEM (tuple, 2, PyInt_FromLong (event.button.x));
359
 
      PyTuple_SET_ITEM (tuple, 3, PyInt_FromLong (event.button.y));
360
 
      PyList_Append (list, tuple);
361
 
      Py_DECREF (tuple);
362
 
      break;
363
 
    case SDL_QUIT:
364
 
    case SDL_VIDEOEXPOSE:
365
 
      tuple = PyTuple_New (1);
366
 
      PyTuple_SET_ITEM (tuple, 0, PyInt_FromLong (event.type));
367
 
      PyList_Append (list, tuple);
368
 
      Py_DECREF (tuple);
369
 
      break;
370
 
    case SDL_VIDEORESIZE:
371
 
      if (engine_option & P3_FULLSCREEN)
372
 
        P3_set_video (event.resize.w, event.resize.h, P3_TRUE, P3_TRUE);
373
 
      else
374
 
        P3_set_video (event.resize.w, event.resize.h, P3_FALSE, P3_TRUE);
375
 
      tuple = PyTuple_New (3);
376
 
      PyTuple_SET_ITEM (tuple, 0, PyInt_FromLong (SDL_VIDEORESIZE));
377
 
      PyTuple_SET_ITEM (tuple, 1, PyInt_FromLong (event.resize.w));
378
 
      PyTuple_SET_ITEM (tuple, 2, PyInt_FromLong (event.resize.h));
379
 
      PyList_Append (list, tuple);
380
 
      Py_DECREF (tuple);
381
 
      break;
382
 
    case SDL_JOYAXISMOTION:
383
 
      tuple = PyTuple_New (3);
384
 
      PyTuple_SET_ITEM (tuple, 0, PyInt_FromLong (event.type));
385
 
      PyTuple_SET_ITEM (tuple, 1, PyInt_FromLong (event.jaxis.axis));
386
 
      PyTuple_SET_ITEM (tuple, 2, PyInt_FromLong (event.jaxis.value));
387
 
      PyList_Append (list, tuple);
388
 
      Py_DECREF (tuple);
389
 
      break;
390
 
    case SDL_JOYBUTTONDOWN:
391
 
      tuple = PyTuple_New (2);
392
 
      PyTuple_SET_ITEM (tuple, 0, PyInt_FromLong (event.type));
393
 
      PyTuple_SET_ITEM (tuple, 1, PyInt_FromLong (event.jbutton.button));
394
 
      PyList_Append (list, tuple);
395
 
      Py_DECREF (tuple);
396
 
      break;
397
 
    case SDL_JOYBUTTONUP:
398
 
      tuple = PyTuple_New (2);
399
 
      PyTuple_SET_ITEM (tuple, 0, PyInt_FromLong (event.type));
400
 
      PyTuple_SET_ITEM (tuple, 1, PyInt_FromLong (event.jbutton.button));
401
 
      PyList_Append (list, tuple);
402
 
      Py_DECREF (tuple);
403
 
      break;
404
 
    case SDL_JOYBALLMOTION:
405
 
printf ("joy ball %i motion x %i y %i\n", event.jball.ball, event.jball.xrel, event.jball.yrel);
406
 
      break;
407
 
    case SDL_JOYHATMOTION:
408
 
printf ("joy hat %i motion %i\n", event.jhat.hat, event.jhat.value);
409
 
      break;
410
 
    }
411
 
  }
412
 
#endif /* USE_SDL */
413
 
 
414
 
  return list;
415
 
}
416
 
 
417
 
static PyObject* PyP3_AdvanceTime (PyObject* module, PyObject* arg) {
418
 
  delta_time += (float) PyFloat_AS_DOUBLE (arg);
419
 
  Py_INCREF (Py_None);
420
 
  return Py_None;
421
 
}
422