correct fsf address
[blender.git] / source / gameengine / BlenderRoutines / BL_KetsjiEmbedStart.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * Blender's Ketsji startpoint
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <signal.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38
39 #ifdef WIN32
40 // don't show stl-warnings
41 #pragma warning (disable:4786)
42 #endif
43
44 #include "GL/glew.h"
45
46 #include "KX_BlenderGL.h"
47 #include "KX_BlenderCanvas.h"
48 #include "KX_BlenderKeyboardDevice.h"
49 #include "KX_BlenderMouseDevice.h"
50 #include "KX_BlenderRenderTools.h"
51 #include "KX_BlenderSystem.h"
52 #include "BL_Material.h"
53
54 #include "KX_KetsjiEngine.h"
55 #include "KX_BlenderSceneConverter.h"
56 #include "KX_PythonInit.h"
57 #include "KX_PyConstraintBinding.h"
58
59 #include "RAS_GLExtensionManager.h"
60 #include "RAS_OpenGLRasterizer.h"
61 #include "RAS_VAOpenGLRasterizer.h"
62 #include "RAS_ListRasterizer.h"
63
64 #include "NG_LoopBackNetworkDeviceInterface.h"
65
66 #include "SYS_System.h"
67
68 #include "GPU_extensions.h"
69 #include "Value.h"
70
71
72
73 #ifdef __cplusplus
74 extern "C" {
75 #endif
76         /***/
77 #include "DNA_view3d_types.h"
78 #include "DNA_screen_types.h"
79 #include "DNA_windowmanager_types.h"
80 #include "BKE_global.h"
81 #include "BKE_report.h"
82
83 #include "BKE_utildefines.h"
84 //XXX #include "BIF_screen.h"
85 //XXX #include "BIF_scrarea.h"
86
87 #include "BKE_main.h"
88 #include "BLI_blenlib.h"
89 #include "BLO_readfile.h"
90 #include "DNA_scene_types.h"
91         /***/
92
93 #include "AUD_C-API.h"
94
95 //XXX #include "BSE_headerbuttons.h"
96 #include "BKE_context.h"
97 #include "../../blender/windowmanager/WM_types.h"
98 #include "../../blender/windowmanager/wm_window.h"
99 #include "../../blender/windowmanager/wm_event_system.h"
100 #ifdef __cplusplus
101 }
102 #endif
103
104
105 static BlendFileData *load_game_data(char *filename)
106 {
107         ReportList reports;
108         BlendFileData *bfd;
109         
110         BKE_reports_init(&reports, RPT_STORE);
111         bfd= BLO_read_from_file(filename, &reports);
112
113         if (!bfd) {
114                 printf("Loading %s failed: ", filename);
115                 BKE_reports_print(&reports, RPT_ERROR);
116         }
117
118         BKE_reports_clear(&reports);
119
120         return bfd;
121 }
122
123 extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *cam_frame, int always_use_expand_framing)
124 {
125         /* context values */
126         struct wmWindow *win= CTX_wm_window(C);
127         struct Scene *scene= CTX_data_scene(C);
128         struct Main* maggie1= CTX_data_main(C);
129
130
131         RAS_Rect area_rect;
132         area_rect.SetLeft(cam_frame->xmin);
133         area_rect.SetBottom(cam_frame->ymin);
134         area_rect.SetRight(cam_frame->xmax);
135         area_rect.SetTop(cam_frame->ymax);
136
137         int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
138         Main* blenderdata = maggie1;
139
140         char* startscenename = scene->id.name+2;
141         char pathname[FILE_MAXDIR+FILE_MAXFILE], oldsce[FILE_MAXDIR+FILE_MAXFILE];
142         STR_String exitstring = "";
143         BlendFileData *bfd= NULL;
144
145         BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
146         BLI_strncpy(oldsce, G.sce, sizeof(oldsce));
147 #ifndef DISABLE_PYTHON
148         resetGamePythonPath(); // need this so running a second time wont use an old blendfiles path
149         setGamePythonPath(G.sce);
150
151         // Acquire Python's GIL (global interpreter lock)
152         // so we can safely run Python code and API calls
153         PyGILState_STATE gilstate = PyGILState_Ensure();
154         
155         PyObject *pyGlobalDict = PyDict_New(); /* python utility storage, spans blend file loading */
156 #endif
157         
158         bgl::InitExtensions(true);
159
160         do
161         {
162                 View3D *v3d= CTX_wm_view3d(C);
163                 RegionView3D *rv3d= CTX_wm_region_view3d(C);
164
165                 // get some preferences
166                 SYS_SystemHandle syshandle = SYS_GetSystem();
167                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
168                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
169                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
170                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
171                 bool game2ipo = (SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
172                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
173                 bool nodepwarnings = (SYS_GetCommandLineInt(syshandle, "ignore_deprecation_warnings", 0) != 0);
174                 bool novertexarrays = (SYS_GetCommandLineInt(syshandle, "novertexarrays", 0) != 0);
175                 // create the canvas, rasterizer and rendertools
176                 RAS_ICanvas* canvas = new KX_BlenderCanvas(win, area_rect);
177                 canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
178                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
179                 RAS_IRasterizer* rasterizer = NULL;
180                 
181                 if(displaylists) {
182                         if (GLEW_VERSION_1_1 && !novertexarrays)
183                                 rasterizer = new RAS_ListRasterizer(canvas, true, true);
184                         else
185                                 rasterizer = new RAS_ListRasterizer(canvas);
186                 }
187                 else if (GLEW_VERSION_1_1 && !novertexarrays)
188                         rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
189                 else
190                         rasterizer = new RAS_OpenGLRasterizer(canvas);
191                 
192                 // create the inputdevices
193                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
194                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
195                 
196                 // create a networkdevice
197                 NG_NetworkDeviceInterface* networkdevice = new
198                         NG_LoopBackNetworkDeviceInterface();
199
200                 //
201                 // create a ketsji/blendersystem (only needed for timing and stuff)
202                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
203                 
204                 // create the ketsjiengine
205                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
206                 
207                 // set the devices
208                 ketsjiengine->SetKeyboardDevice(keyboarddevice);
209                 ketsjiengine->SetMouseDevice(mousedevice);
210                 ketsjiengine->SetNetworkDevice(networkdevice);
211                 ketsjiengine->SetCanvas(canvas);
212                 ketsjiengine->SetRenderTools(rendertools);
213                 ketsjiengine->SetRasterizer(rasterizer);
214                 ketsjiengine->SetNetworkDevice(networkdevice);
215                 ketsjiengine->SetUseFixedTime(usefixed);
216                 ketsjiengine->SetTimingDisplay(frameRate, profile, properties);
217
218 #ifndef DISABLE_PYTHON
219                 CValue::SetDeprecationWarnings(nodepwarnings);
220 #endif
221
222                 //lock frame and camera enabled - storing global values
223                 int tmp_lay= scene->lay;
224                 Object *tmp_camera = scene->camera;
225
226                 if (v3d->scenelock==0){
227                         scene->lay= v3d->lay;
228                         scene->camera= v3d->camera;
229                 }
230
231                 // some blender stuff
232                 MT_CmMatrix4x4 projmat;
233                 MT_CmMatrix4x4 viewmat;
234                 float camzoom;
235                 int i;
236
237                 for (i = 0; i < 16; i++)
238                 {
239                         float *viewmat_linear= (float*) rv3d->viewmat;
240                         viewmat.setElem(i, viewmat_linear[i]);
241                 }
242                 for (i = 0; i < 16; i++)
243                 {
244                         float *projmat_linear= (float*) rv3d->winmat;
245                         projmat.setElem(i, projmat_linear[i]);
246                 }
247                 
248                 if(rv3d->persp==RV3D_CAMOB) {
249                         if(scene->gm.framing.type == SCE_GAMEFRAMING_BARS) { /* Letterbox */
250                                 camzoom = 1.0f;
251                         }
252                         else {
253                                 camzoom = (1.41421 + (rv3d->camzoom / 50.0));
254                                 camzoom *= camzoom;
255                                 camzoom = 4.0 / camzoom;
256                         }
257                 }
258                 else {
259                         camzoom = 2.0;
260                 }
261
262                 
263
264                 ketsjiengine->SetDrawType(v3d->drawtype);
265                 ketsjiengine->SetCameraZoom(camzoom);
266                 
267                 // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
268                 if (exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME || exitrequested == KX_EXIT_REQUEST_RESTART_GAME)
269                 {
270                         exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
271                         if (bfd) BLO_blendfiledata_free(bfd);
272                         
273                         char basedpath[240];
274                         // base the actuator filename with respect
275                         // to the original file working directory
276
277                         if (exitstring != "")
278                                 strcpy(basedpath, exitstring.Ptr());
279
280                         // load relative to the last loaded file, this used to be relative
281                         // to the first file but that makes no sense, relative paths in
282                         // blend files should be relative to that file, not some other file
283                         // that happened to be loaded first
284                         BLI_convertstringcode(basedpath, pathname);
285                         bfd = load_game_data(basedpath);
286                         
287                         // if it wasn't loaded, try it forced relative
288                         if (!bfd)
289                         {
290                                 // just add "//" in front of it
291                                 char temppath[242];
292                                 strcpy(temppath, "//");
293                                 strcat(temppath, basedpath);
294                                 
295                                 BLI_convertstringcode(temppath, pathname);
296                                 bfd = load_game_data(temppath);
297                         }
298                         
299                         // if we got a loaded blendfile, proceed
300                         if (bfd)
301                         {
302                                 blenderdata = bfd->main;
303                                 startscenename = bfd->curscene->id.name + 2;
304
305                                 if(blenderdata) {
306                                         BLI_strncpy(G.sce, blenderdata->name, sizeof(G.sce));
307                                         BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
308 #ifndef DISABLE_PYTHON
309                                         setGamePythonPath(G.sce);
310 #endif
311                                 }
312                         }
313                         // else forget it, we can't find it
314                         else
315                         {
316                                 exitrequested = KX_EXIT_REQUEST_QUIT_GAME;
317                         }
318                 }
319                 
320                 Scene *blscene = NULL;
321                 if (!bfd)
322                 {
323                         blscene = (Scene*) blenderdata->scene.first;
324                         for (Scene *sce= (Scene*) blenderdata->scene.first; sce; sce= (Scene*) sce->id.next)
325                         {
326                                 if (startscenename == (sce->id.name+2))
327                                 {
328                                         blscene = sce;
329                                         break;
330                                 }
331                         }
332                 } else {
333                         blscene = bfd->curscene;
334                 }
335
336                 if (blscene)
337                 {
338                         int startFrame = blscene->r.cfra;
339                         ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
340                         
341                         // Quad buffered needs a special window.
342                         if(blscene->gm.stereoflag == STEREO_ENABLED){
343                                 if (blscene->gm.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
344                                         rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->gm.stereomode);
345
346                                 rasterizer->SetEyeSeparation(blscene->gm.eyeseparation);
347                         }
348
349                         rasterizer->SetBackColor(blscene->gm.framing.col[0], blscene->gm.framing.col[1], blscene->gm.framing.col[2], 0.0f);
350                 }
351                 
352                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
353                 {
354                         if (rv3d->persp != RV3D_CAMOB)
355                         {
356                                 ketsjiengine->EnableCameraOverride(startscenename);
357                                 ketsjiengine->SetCameraOverrideUseOrtho((rv3d->persp == RV3D_ORTHO));
358                                 ketsjiengine->SetCameraOverrideProjectionMatrix(projmat);
359                                 ketsjiengine->SetCameraOverrideViewMatrix(viewmat);
360                                 ketsjiengine->SetCameraOverrideClipping(v3d->near, v3d->far);
361                                 ketsjiengine->SetCameraOverrideLens(v3d->lens);
362                         }
363                         
364                         // create a scene converter, create and convert the startingscene
365                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(blenderdata, ketsjiengine);
366                         ketsjiengine->SetSceneConverter(sceneconverter);
367                         sceneconverter->addInitFromFrame=false;
368                         if (always_use_expand_framing)
369                                 sceneconverter->SetAlwaysUseExpandFraming(true);
370
371                         bool usemat = false, useglslmat = false;
372
373                         if(GLEW_ARB_multitexture && GLEW_VERSION_1_1)
374                                 usemat = true;
375
376                         if(GPU_glsl_support())
377                                 useglslmat = true;
378                         else if(blscene->gm.matmode == GAME_MAT_GLSL)
379                                 usemat = false;
380
381             if(usemat && (blscene->gm.matmode != GAME_MAT_TEXFACE))
382                                 sceneconverter->SetMaterials(true);
383                         if(useglslmat && (blscene->gm.matmode == GAME_MAT_GLSL))
384                                 sceneconverter->SetGLSLMaterials(true);
385                                         
386                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
387                                 mousedevice,
388                                 networkdevice,
389                                 startscenename,
390                                 blscene);
391
392 #ifndef DISABLE_PYTHON
393                         // some python things
394                         PyObject *gameLogic, *gameLogic_keys;
395                         setupGamePython(ketsjiengine, startscene, blenderdata, pyGlobalDict, &gameLogic, &gameLogic_keys, 0, NULL);
396 #endif // DISABLE_PYTHON
397
398                         //initialize Dome Settings
399                         if(blscene->gm.stereoflag == STEREO_DOME)
400                                 ketsjiengine->InitDome(blscene->gm.dome.res, blscene->gm.dome.mode, blscene->gm.dome.angle, blscene->gm.dome.resbuf, blscene->gm.dome.tilt, blscene->gm.dome.warptext);
401
402                         // initialize 3D Audio Settings
403                         AUD_set3DSetting(AUD_3DS_SPEED_OF_SOUND, blscene->audio.speed_of_sound);
404                         AUD_set3DSetting(AUD_3DS_DOPPLER_FACTOR, blscene->audio.doppler_factor);
405                         AUD_set3DSetting(AUD_3DS_DISTANCE_MODEL, blscene->audio.distance_model);
406
407                         if (sceneconverter)
408                         {
409                                 // convert and add scene
410                                 sceneconverter->ConvertScene(
411                                         startscene,
412                                         rendertools,
413                                         canvas);
414                                 ketsjiengine->AddScene(startscene);
415                                 
416                                 // init the rasterizer
417                                 rasterizer->Init();
418                                 
419                                 // start the engine
420                                 ketsjiengine->StartEngine(true);
421                                 
422
423                                 // Set the animation playback rate for ipo's and actions
424                                 // the framerate below should patch with FPS macro defined in blendef.h
425                                 // Could be in StartEngine set the framerate, we need the scene to do this
426                                 ketsjiengine->SetAnimFrameRate( (((double) blscene->r.frs_sec) / blscene->r.frs_sec_base) );
427                                 
428                                 // the mainloop
429                                 printf("\nBlender Game Engine Started\n\n");
430                                 while (!exitrequested)
431                                 {
432                                         // first check if we want to exit
433                                         exitrequested = ketsjiengine->GetExitCode();
434                                         
435                                         // kick the engine
436                                         bool render = ketsjiengine->NextFrame(); // XXX 2.5 Bug, This is never true! FIXME-  Campbell
437                                         
438                                         if (render)
439                                         {
440                                                 // render the frame
441                                                 ketsjiengine->Render();
442                                         }
443                                         
444                                         wm_window_process_events_nosleep(C);
445                                         
446                                         // test for the ESC key
447                                         //XXX while (qtest())
448                                         while(wmEvent *event= (wmEvent *)win->queue.first)
449                                         {
450                                                 short val = 0;
451                                                 //unsigned short event = 0; //XXX extern_qread(&val);
452                                                 
453                                                 if (keyboarddevice->ConvertBlenderEvent(event->type,event->val))
454                                                         exitrequested = KX_EXIT_REQUEST_BLENDER_ESC;
455                                                 
456                                                         /* Coordinate conversion... where
457                                                         * should this really be?
458                                                 */
459                                                 if (event->type==MOUSEMOVE) {
460                                                         /* Note nice! XXX 2.5 event hack */
461                                                         val = event->x - ar->winrct.xmin;
462                                                         mousedevice->ConvertBlenderEvent(MOUSEX, val);
463                                                         
464                                                         val = ar->winy - (event->y - ar->winrct.ymin) - 1;
465                                                         mousedevice->ConvertBlenderEvent(MOUSEY, val);
466                                                 }
467                                                 else {
468                                                         mousedevice->ConvertBlenderEvent(event->type,event->val);
469                                                 }
470                                                 
471                                                 BLI_remlink(&win->queue, event);
472                                                 wm_event_free(event);
473                                         }
474                                         
475                                 }
476                                 printf("\nBlender Game Engine Finished\n\n");
477                                 exitstring = ketsjiengine->GetExitString();
478
479
480                                 // when exiting the mainloop
481 #ifndef DISABLE_PYTHON
482                                 // Clears the dictionary by hand:
483                                 // This prevents, extra references to global variables
484                                 // inside the GameLogic dictionary when the python interpreter is finalized.
485                                 // which allows the scene to safely delete them :)
486                                 // see: (space.c)->start_game
487                                 
488                                 //PyDict_Clear(PyModule_GetDict(gameLogic));
489                                 
490                                 // Keep original items, means python plugins will autocomplete members
491                                 int listIndex;
492                                 PyObject *gameLogic_keys_new = PyDict_Keys(PyModule_GetDict(gameLogic));
493                                 for (listIndex=0; listIndex < PyList_Size(gameLogic_keys_new); listIndex++)  {
494                                         PyObject* item = PyList_GET_ITEM(gameLogic_keys_new, listIndex);
495                                         if (!PySequence_Contains(gameLogic_keys, item)) {
496                                                 PyDict_DelItem( PyModule_GetDict(gameLogic), item);
497                                         }
498                                 }
499                                 Py_DECREF(gameLogic_keys_new);
500                                 gameLogic_keys_new = NULL;
501 #endif
502                                 ketsjiengine->StopEngine();
503 #ifndef DISABLE_PYTHON
504                                 exitGamePythonScripting();
505 #endif
506                                 networkdevice->Disconnect();
507                         }
508                         if (sceneconverter)
509                         {
510                                 delete sceneconverter;
511                                 sceneconverter = NULL;
512                         }
513
514 #ifndef DISABLE_PYTHON
515                         Py_DECREF(gameLogic_keys);
516                         gameLogic_keys = NULL;
517 #endif
518                 }
519                 //lock frame and camera enabled - restoring global values
520                 if (v3d->scenelock==0){
521                         scene->lay= tmp_lay;
522                         scene->camera= tmp_camera;
523                 }
524
525                 // set the cursor back to normal
526                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
527                 
528                 // clean up some stuff
529                 if (ketsjiengine)
530                 {
531                         delete ketsjiengine;
532                         ketsjiengine = NULL;
533                 }
534                 if (kxsystem)
535                 {
536                         delete kxsystem;
537                         kxsystem = NULL;
538                 }
539                 if (networkdevice)
540                 {
541                         delete networkdevice;
542                         networkdevice = NULL;
543                 }
544                 if (keyboarddevice)
545                 {
546                         delete keyboarddevice;
547                         keyboarddevice = NULL;
548                 }
549                 if (mousedevice)
550                 {
551                         delete mousedevice;
552                         mousedevice = NULL;
553                 }
554                 if (rasterizer)
555                 {
556                         delete rasterizer;
557                         rasterizer = NULL;
558                 }
559                 if (rendertools)
560                 {
561                         delete rendertools;
562                         rendertools = NULL;
563                 }
564                 if (canvas)
565                 {
566                         delete canvas;
567                         canvas = NULL;
568                 }
569         
570         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
571         
572         if (bfd) BLO_blendfiledata_free(bfd);
573
574         BLI_strncpy(G.sce, oldsce, sizeof(G.sce));
575
576 #ifndef DISABLE_PYTHON
577         Py_DECREF(pyGlobalDict);
578
579         // Release Python's GIL
580         PyGILState_Release(gilstate);
581 #endif
582
583 }
584
585 extern "C" void StartKetsjiShellSimulation(struct wmWindow *win,
586                                                                  struct ARegion *ar,
587                                                                  char* scenename,
588                                                                  struct Main* maggie,
589                                                                  int always_use_expand_framing)
590 {
591     int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
592
593         Main* blenderdata = maggie;
594
595         RAS_Rect area_rect;
596         area_rect.SetLeft(ar->winrct.xmin);
597         area_rect.SetBottom(ar->winrct.ymin);
598         area_rect.SetRight(ar->winrct.xmax);
599         area_rect.SetTop(ar->winrct.ymax);
600
601         char* startscenename = scenename;
602         char pathname[FILE_MAXDIR+FILE_MAXFILE];
603         STR_String exitstring = "";
604
605         BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
606
607 #ifndef DISABLE_PYTHON
608         // Acquire Python's GIL (global interpreter lock)
609         // so we can safely run Python code and API calls
610         PyGILState_STATE gilstate = PyGILState_Ensure();
611         PyObject *pyGlobalDict = PyDict_New(); /* python utility storage, spans blend file loading */
612 #endif
613
614         bgl::InitExtensions(true);
615
616         do
617         {
618
619                 // get some preferences
620                 SYS_SystemHandle syshandle = SYS_GetSystem();
621                 /*
622                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
623                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
624                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
625                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
626                 */
627                 bool game2ipo = true;//(SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
628                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
629                 bool usemat = false;
630
631                 // create the canvas, rasterizer and rendertools
632                 RAS_ICanvas* canvas = new KX_BlenderCanvas(win, area_rect);
633                 //canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
634                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
635                 RAS_IRasterizer* rasterizer = NULL;
636
637                 if(displaylists) {
638                         if (GLEW_VERSION_1_1)
639                                 rasterizer = new RAS_ListRasterizer(canvas, true, true);
640                         else
641                                 rasterizer = new RAS_ListRasterizer(canvas);
642                 }
643                 else if (GLEW_VERSION_1_1)
644                         rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
645                 else
646                         rasterizer = new RAS_OpenGLRasterizer(canvas);
647
648                 // create the inputdevices
649                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
650                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
651
652                 // create a networkdevice
653                 NG_NetworkDeviceInterface* networkdevice = new
654                         NG_LoopBackNetworkDeviceInterface();
655
656                 // create a ketsji/blendersystem (only needed for timing and stuff)
657                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
658
659                 // create the ketsjiengine
660                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
661
662                 Scene *blscene = NULL;
663
664                 blscene = (Scene*) maggie->scene.first;
665                 for (Scene *sce= (Scene*) maggie->scene.first; sce; sce= (Scene*) sce->id.next)
666                 {
667                         if (startscenename == (sce->id.name+2))
668                         {
669                                 blscene = sce;
670                                 break;
671                         }
672                 }
673
674         int cframe = 1, startFrame;
675                 if (blscene)
676                 {
677                         cframe=blscene->r.cfra;
678                         startFrame = blscene->r.sfra;
679                         blscene->r.cfra=startFrame;
680                         // update_for_newframe(); // XXX scene_update_for_newframe wont cut it!
681                         ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
682                 }
683
684                 // Quad buffered needs a special window.
685                 if(blscene->gm.stereoflag == STEREO_ENABLED){
686                         if (blscene->gm.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
687                                 rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->gm.stereomode);
688                 }
689                 rasterizer->SetBackColor(blscene->gm.framing.col[0], blscene->gm.framing.col[1], blscene->gm.framing.col[2], 0.0f);
690
691                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
692                 {
693                         // create a scene converter, create and convert the startingscene
694                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(maggie, ketsjiengine);
695                         ketsjiengine->SetSceneConverter(sceneconverter);
696                         sceneconverter->addInitFromFrame=true;
697                         
698                         if (always_use_expand_framing)
699                                 sceneconverter->SetAlwaysUseExpandFraming(true);
700
701                         if(usemat)
702                                 sceneconverter->SetMaterials(true);
703
704                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
705                                 mousedevice,
706                                 networkdevice,
707                                 startscenename,
708                                 blscene);
709
710 #ifndef DISABLE_PYTHON
711                         // some python things
712                         PyObject *gameLogic, *gameLogic_keys;
713                         setupGamePython(ketsjiengine, startscene, blenderdata, pyGlobalDict, &gameLogic, &gameLogic_keys, 0, NULL);
714 #endif // DISABLE_PYTHON
715
716                         if (sceneconverter)
717                         {
718                                 // convert and add scene
719                                 sceneconverter->ConvertScene(
720                                         startscene,
721                                         rendertools,
722                                         canvas);
723                                 ketsjiengine->AddScene(startscene);
724
725                                 // start the engine
726                                 ketsjiengine->StartEngine(false);
727                                 
728                                 ketsjiengine->SetUseFixedTime(true);
729                                 
730                                 ketsjiengine->SetTicRate(
731                                         (double) blscene->r.frs_sec /
732                                         (double) blscene->r.frs_sec_base);
733
734                                 // the mainloop
735                                 while ((blscene->r.cfra<=blscene->r.efra)&&(!exitrequested))
736                                 {
737                     printf("frame %i\n",blscene->r.cfra);
738                     // first check if we want to exit
739                                         exitrequested = ketsjiengine->GetExitCode();
740         
741                                         // kick the engine
742                                         ketsjiengine->NextFrame();
743                                     blscene->r.cfra=blscene->r.cfra+1;
744                                     // update_for_newframe(); // XXX scene_update_for_newframe wont cut it
745                                         
746                                 }
747                                 exitstring = ketsjiengine->GetExitString();
748                         }
749                         if (sceneconverter)
750                         {
751                                 delete sceneconverter;
752                                 sceneconverter = NULL;
753                         }
754                 }
755                 blscene->r.cfra=cframe;
756                 // set the cursor back to normal
757                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
758
759                 // clean up some stuff
760                 if (ketsjiengine)
761                 {
762                         delete ketsjiengine;
763                         ketsjiengine = NULL;
764                 }
765                 if (kxsystem)
766                 {
767                         delete kxsystem;
768                         kxsystem = NULL;
769                 }
770                 if (networkdevice)
771                 {
772                         delete networkdevice;
773                         networkdevice = NULL;
774                 }
775                 if (keyboarddevice)
776                 {
777                         delete keyboarddevice;
778                         keyboarddevice = NULL;
779                 }
780                 if (mousedevice)
781                 {
782                         delete mousedevice;
783                         mousedevice = NULL;
784                 }
785                 if (rasterizer)
786                 {
787                         delete rasterizer;
788                         rasterizer = NULL;
789                 }
790                 if (rendertools)
791                 {
792                         delete rendertools;
793                         rendertools = NULL;
794                 }
795
796         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
797
798 #ifndef DISABLE_PYTHON
799         Py_DECREF(pyGlobalDict);
800
801         // Release Python's GIL
802         PyGILState_Release(gilstate);
803 #endif
804 }