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