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