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