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