option to build the BGE without python, uses existing python check (cmake and scons)
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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, 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         int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
132         Main* blenderdata = maggie1;
133
134         char* startscenename = scene->id.name+2;
135         char pathname[FILE_MAXDIR+FILE_MAXFILE], oldsce[FILE_MAXDIR+FILE_MAXFILE];
136         STR_String exitstring = "";
137         BlendFileData *bfd= NULL;
138
139         BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
140         BLI_strncpy(oldsce, G.sce, sizeof(oldsce));
141 #ifndef DISABLE_PYTHON
142         resetGamePythonPath(); // need this so running a second time wont use an old blendfiles path
143         setGamePythonPath(G.sce);
144
145         // Acquire Python's GIL (global interpreter lock)
146         // so we can safely run Python code and API calls
147         PyGILState_STATE gilstate = PyGILState_Ensure();
148         
149         PyObject *pyGlobalDict = PyDict_New(); /* python utility storage, spans blend file loading */
150 #endif
151         
152         bgl::InitExtensions(true);
153
154         do
155         {
156                 View3D *v3d= CTX_wm_view3d(C);
157                 RegionView3D *rv3d= CTX_wm_region_view3d(C);
158
159                 // get some preferences
160                 SYS_SystemHandle syshandle = SYS_GetSystem();
161                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
162                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
163                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
164                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
165                 bool game2ipo = (SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
166                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
167                 bool nodepwarnings = (SYS_GetCommandLineInt(syshandle, "ignore_deprecation_warnings", 0) != 0);
168                 bool novertexarrays = (SYS_GetCommandLineInt(syshandle, "novertexarrays", 0) != 0);
169                 // create the canvas, rasterizer and rendertools
170                 RAS_ICanvas* canvas = new KX_BlenderCanvas(win, ar);
171                 canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
172                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
173                 RAS_IRasterizer* rasterizer = NULL;
174                 
175                 if(displaylists) {
176                         if (GLEW_VERSION_1_1 && !novertexarrays)
177                                 rasterizer = new RAS_ListRasterizer(canvas, true, true);
178                         else
179                                 rasterizer = new RAS_ListRasterizer(canvas);
180                 }
181                 else if (GLEW_VERSION_1_1 && !novertexarrays)
182                         rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
183                 else
184                         rasterizer = new RAS_OpenGLRasterizer(canvas);
185                 
186                 // create the inputdevices
187                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
188                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
189                 
190                 // create a networkdevice
191                 NG_NetworkDeviceInterface* networkdevice = new
192                         NG_LoopBackNetworkDeviceInterface();
193
194                 //
195                 // create a ketsji/blendersystem (only needed for timing and stuff)
196                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
197                 
198                 // create the ketsjiengine
199                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
200                 
201                 // set the devices
202                 ketsjiengine->SetKeyboardDevice(keyboarddevice);
203                 ketsjiengine->SetMouseDevice(mousedevice);
204                 ketsjiengine->SetNetworkDevice(networkdevice);
205                 ketsjiengine->SetCanvas(canvas);
206                 ketsjiengine->SetRenderTools(rendertools);
207                 ketsjiengine->SetRasterizer(rasterizer);
208                 ketsjiengine->SetNetworkDevice(networkdevice);
209                 ketsjiengine->SetUseFixedTime(usefixed);
210                 ketsjiengine->SetTimingDisplay(frameRate, profile, properties);
211
212 #ifndef DISABLE_PYTHON
213                 CValue::SetDeprecationWarnings(nodepwarnings);
214 #endif
215
216                 //lock frame and camera enabled - storing global values
217                 int tmp_lay= scene->lay;
218                 Object *tmp_camera = scene->camera;
219
220                 if (v3d->scenelock==0){
221                         scene->lay= v3d->lay;
222                         scene->camera= v3d->camera;
223                 }
224
225                 // some blender stuff
226                 MT_CmMatrix4x4 projmat;
227                 MT_CmMatrix4x4 viewmat;
228                 float camzoom;
229                 int i;
230
231                 for (i = 0; i < 16; i++)
232                 {
233                         float *viewmat_linear= (float*) rv3d->viewmat;
234                         viewmat.setElem(i, viewmat_linear[i]);
235                 }
236                 for (i = 0; i < 16; i++)
237                 {
238                         float *projmat_linear= (float*) rv3d->winmat;
239                         projmat.setElem(i, projmat_linear[i]);
240                 }
241                 
242                 if(rv3d->persp==V3D_CAMOB) {
243                         camzoom = (1.41421 + (rv3d->camzoom / 50.0));
244                         camzoom *= camzoom;
245                 }
246                 else
247                         camzoom = 2.0;
248
249                 camzoom = 4.0 / camzoom;
250                 
251                 ketsjiengine->SetDrawType(v3d->drawtype);
252                 ketsjiengine->SetCameraZoom(camzoom);
253                 
254                 // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
255                 if (exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME || exitrequested == KX_EXIT_REQUEST_RESTART_GAME)
256                 {
257                         exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
258                         if (bfd) BLO_blendfiledata_free(bfd);
259                         
260                         char basedpath[240];
261                         // base the actuator filename with respect
262                         // to the original file working directory
263
264                         if (exitstring != "")
265                                 strcpy(basedpath, exitstring.Ptr());
266
267                         // load relative to the last loaded file, this used to be relative
268                         // to the first file but that makes no sense, relative paths in
269                         // blend files should be relative to that file, not some other file
270                         // that happened to be loaded first
271                         BLI_convertstringcode(basedpath, pathname);
272                         bfd = load_game_data(basedpath);
273                         
274                         // if it wasn't loaded, try it forced relative
275                         if (!bfd)
276                         {
277                                 // just add "//" in front of it
278                                 char temppath[242];
279                                 strcpy(temppath, "//");
280                                 strcat(temppath, basedpath);
281                                 
282                                 BLI_convertstringcode(temppath, pathname);
283                                 bfd = load_game_data(temppath);
284                         }
285                         
286                         // if we got a loaded blendfile, proceed
287                         if (bfd)
288                         {
289                                 blenderdata = bfd->main;
290                                 startscenename = bfd->curscene->id.name + 2;
291
292                                 if(blenderdata) {
293                                         BLI_strncpy(G.sce, blenderdata->name, sizeof(G.sce));
294                                         BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
295 #ifndef DISABLE_PYTHON
296                                         setGamePythonPath(G.sce);
297 #endif
298                                 }
299                         }
300                         // else forget it, we can't find it
301                         else
302                         {
303                                 exitrequested = KX_EXIT_REQUEST_QUIT_GAME;
304                         }
305                 }
306                 
307                 Scene *blscene = NULL;
308                 if (!bfd)
309                 {
310                         blscene = (Scene*) blenderdata->scene.first;
311                         for (Scene *sce= (Scene*) blenderdata->scene.first; sce; sce= (Scene*) sce->id.next)
312                         {
313                                 if (startscenename == (sce->id.name+2))
314                                 {
315                                         blscene = sce;
316                                         break;
317                                 }
318                         }
319                 } else {
320                         blscene = bfd->curscene;
321                 }
322
323                 if (blscene)
324                 {
325                         int startFrame = blscene->r.cfra;
326                         ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
327                         
328                         // Quad buffered needs a special window.
329                         if(blscene->gm.stereoflag == STEREO_ENABLED){
330                                 if (blscene->gm.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
331                                         rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->gm.stereomode);
332                         }
333                 }
334                 
335                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
336                 {
337                         if (rv3d->persp != V3D_CAMOB)
338                         {
339                                 ketsjiengine->EnableCameraOverride(startscenename);
340                                 ketsjiengine->SetCameraOverrideUseOrtho((rv3d->persp == V3D_ORTHO));
341                                 ketsjiengine->SetCameraOverrideProjectionMatrix(projmat);
342                                 ketsjiengine->SetCameraOverrideViewMatrix(viewmat);
343                                 ketsjiengine->SetCameraOverrideClipping(v3d->near, v3d->far);
344                                 ketsjiengine->SetCameraOverrideLens(v3d->lens);
345                         }
346                         
347                         // create a scene converter, create and convert the startingscene
348                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(blenderdata, ketsjiengine);
349                         ketsjiengine->SetSceneConverter(sceneconverter);
350                         sceneconverter->addInitFromFrame=false;
351                         if (always_use_expand_framing)
352                                 sceneconverter->SetAlwaysUseExpandFraming(true);
353
354                         bool usemat = false, useglslmat = false;
355
356                         if(GLEW_ARB_multitexture && GLEW_VERSION_1_1)
357                                 usemat = true;
358
359                         if(GPU_extensions_minimum_support())
360                                 useglslmat = true;
361                         else if(blscene->gm.matmode == GAME_MAT_GLSL)
362                                 usemat = false;
363
364             if(usemat && (blscene->gm.matmode != GAME_MAT_TEXFACE))
365                                 sceneconverter->SetMaterials(true);
366                         if(useglslmat && (blscene->gm.matmode == GAME_MAT_GLSL))
367                                 sceneconverter->SetGLSLMaterials(true);
368                                         
369                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
370                                 mousedevice,
371                                 networkdevice,
372                                 startscenename,
373                                 blscene);
374
375 #ifndef DISABLE_PYTHON
376                         // some python things
377                         PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest, blenderdata);
378                         ketsjiengine->SetPythonDictionary(dictionaryobject);
379                         initRasterizer(rasterizer, canvas);
380                         PyObject *gameLogic = initGameLogic(ketsjiengine, startscene);
381                         PyDict_SetItemString(PyModule_GetDict(gameLogic), "globalDict", pyGlobalDict); // Same as importing the module.
382                         PyObject *gameLogic_keys = PyDict_Keys(PyModule_GetDict(gameLogic));
383                         PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module.
384                         
385                         initGameKeys();
386                         initPythonConstraintBinding();
387                         initMathutils();
388                         initGeometry();
389                         initBGL();
390 #ifdef WITH_FFMPEG
391                         initVideoTexture();
392 #endif
393 #endif // DISABLE_PYTHON
394
395                         //initialize Dome Settings
396                         if(blscene->gm.stereoflag == STEREO_DOME)
397                                 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);
398
399                         // initialize 3D Audio Settings
400                         AUD_set3DSetting(AUD_3DS_SPEED_OF_SOUND, blscene->audio.speed_of_sound);
401                         AUD_set3DSetting(AUD_3DS_DOPPLER_FACTOR, blscene->audio.doppler_factor);
402                         AUD_set3DSetting(AUD_3DS_DISTANCE_MODEL, blscene->audio.distance_model);
403
404                         if (sceneconverter)
405                         {
406                                 // convert and add scene
407                                 sceneconverter->ConvertScene(
408                                         startscene,
409 #ifndef DISABLE_PYTHON
410                                         dictionaryobject,
411 #endif
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         char* startscenename = scenename;
596         char pathname[FILE_MAXDIR+FILE_MAXFILE];
597         STR_String exitstring = "";
598
599         BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
600
601 #ifndef DISABLE_PYTHON
602         // Acquire Python's GIL (global interpreter lock)
603         // so we can safely run Python code and API calls
604         PyGILState_STATE gilstate = PyGILState_Ensure();
605 #endif
606
607         bgl::InitExtensions(true);
608
609         do
610         {
611
612                 // get some preferences
613                 SYS_SystemHandle syshandle = SYS_GetSystem();
614                 /*
615                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
616                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
617                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
618                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
619                 */
620                 bool game2ipo = true;//(SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
621                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
622                 bool usemat = false;
623
624                 // create the canvas, rasterizer and rendertools
625                 RAS_ICanvas* canvas = new KX_BlenderCanvas(win, ar);
626                 //canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
627                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
628                 RAS_IRasterizer* rasterizer = NULL;
629
630                 if(displaylists) {
631                         if (GLEW_VERSION_1_1)
632                                 rasterizer = new RAS_ListRasterizer(canvas, true, true);
633                         else
634                                 rasterizer = new RAS_ListRasterizer(canvas);
635                 }
636                 else if (GLEW_VERSION_1_1)
637                         rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
638                 else
639                         rasterizer = new RAS_OpenGLRasterizer(canvas);
640
641                 // create the inputdevices
642                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
643                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
644
645                 // create a networkdevice
646                 NG_NetworkDeviceInterface* networkdevice = new
647                         NG_LoopBackNetworkDeviceInterface();
648
649                 // create a ketsji/blendersystem (only needed for timing and stuff)
650                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
651
652                 // create the ketsjiengine
653                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
654
655                 Scene *blscene = NULL;
656
657                 blscene = (Scene*) maggie->scene.first;
658                 for (Scene *sce= (Scene*) maggie->scene.first; sce; sce= (Scene*) sce->id.next)
659                 {
660                         if (startscenename == (sce->id.name+2))
661                         {
662                                 blscene = sce;
663                                 break;
664                         }
665                 }
666
667         int cframe = 1, startFrame;
668                 if (blscene)
669                 {
670                         cframe=blscene->r.cfra;
671                         startFrame = blscene->r.sfra;
672                         blscene->r.cfra=startFrame;
673                         // update_for_newframe(); // XXX scene_update_for_newframe wont cut it!
674                         ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
675                 }
676
677                 // Quad buffered needs a special window.
678                 if(blscene->gm.stereoflag == STEREO_ENABLED){
679                         if (blscene->gm.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
680                                 rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->gm.stereomode);
681                 }
682
683                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
684                 {
685                         // create a scene converter, create and convert the startingscene
686                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(maggie, ketsjiengine);
687                         ketsjiengine->SetSceneConverter(sceneconverter);
688                         sceneconverter->addInitFromFrame=true;
689                         
690                         if (always_use_expand_framing)
691                                 sceneconverter->SetAlwaysUseExpandFraming(true);
692
693                         if(usemat)
694                                 sceneconverter->SetMaterials(true);
695
696                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
697                                 mousedevice,
698                                 networkdevice,
699                                 startscenename,
700                                 blscene);
701
702 #ifndef DISABLE_PYTHON
703                         // some python things
704                         PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest, blenderdata);
705                         ketsjiengine->SetPythonDictionary(dictionaryobject);
706                         initRasterizer(rasterizer, canvas);
707                         PyObject *gameLogic = initGameLogic(ketsjiengine, startscene);
708                         PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module
709                         initGameKeys();
710                         initPythonConstraintBinding();
711                         initMathutils();
712                         initGeometry();
713                         initBGL();
714 #ifdef WITH_FFMPEG
715                         initVideoTexture();
716 #endif
717 #endif // DISABLE_PYTHON
718
719                         if (sceneconverter)
720                         {
721                                 // convert and add scene
722                                 sceneconverter->ConvertScene(
723                                         startscene,
724 #ifndef DISABLE_PYTHON
725                                         dictionaryobject,
726 #endif
727                                         rendertools,
728                                         canvas);
729                                 ketsjiengine->AddScene(startscene);
730
731                                 // start the engine
732                                 ketsjiengine->StartEngine(false);
733                                 
734                                 ketsjiengine->SetUseFixedTime(true);
735                                 
736                                 ketsjiengine->SetTicRate(
737                                         (double) blscene->r.frs_sec /
738                                         (double) blscene->r.frs_sec_base);
739
740                                 // the mainloop
741                                 while ((blscene->r.cfra<=blscene->r.efra)&&(!exitrequested))
742                                 {
743                     printf("frame %i\n",blscene->r.cfra);
744                     // first check if we want to exit
745                                         exitrequested = ketsjiengine->GetExitCode();
746         
747                                         // kick the engine
748                                         ketsjiengine->NextFrame();
749                                     blscene->r.cfra=blscene->r.cfra+1;
750                                     // update_for_newframe(); // XXX scene_update_for_newframe wont cut it
751                                         
752                                 }
753                                 exitstring = ketsjiengine->GetExitString();
754                         }
755                         if (sceneconverter)
756                         {
757                                 delete sceneconverter;
758                                 sceneconverter = NULL;
759                         }
760                 }
761                 blscene->r.cfra=cframe;
762                 // set the cursor back to normal
763                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
764
765                 // clean up some stuff
766                 if (ketsjiengine)
767                 {
768                         delete ketsjiengine;
769                         ketsjiengine = NULL;
770                 }
771                 if (kxsystem)
772                 {
773                         delete kxsystem;
774                         kxsystem = NULL;
775                 }
776                 if (networkdevice)
777                 {
778                         delete networkdevice;
779                         networkdevice = NULL;
780                 }
781                 if (keyboarddevice)
782                 {
783                         delete keyboarddevice;
784                         keyboarddevice = NULL;
785                 }
786                 if (mousedevice)
787                 {
788                         delete mousedevice;
789                         mousedevice = NULL;
790                 }
791                 if (rasterizer)
792                 {
793                         delete rasterizer;
794                         rasterizer = NULL;
795                 }
796                 if (rendertools)
797                 {
798                         delete rendertools;
799                         rendertools = NULL;
800                 }
801
802         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
803
804 #ifndef DISABLE_PYTHON
805         // Release Python's GIL
806         PyGILState_Release(gilstate);
807 #endif
808 }