BGE: when letterbox is enabled use the camera framing as a clipping area (good for...
[blender.git] / source / gameengine / BlenderRoutines / BL_KetsjiEmbedStart.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * Blender's Ketsji startpoint
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <signal.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38
39 #ifdef WIN32
40 // don't show stl-warnings
41 #pragma warning (disable:4786)
42 #endif
43
44 #include "GL/glew.h"
45
46 #include "KX_BlenderGL.h"
47 #include "KX_BlenderCanvas.h"
48 #include "KX_BlenderKeyboardDevice.h"
49 #include "KX_BlenderMouseDevice.h"
50 #include "KX_BlenderRenderTools.h"
51 #include "KX_BlenderSystem.h"
52 #include "BL_Material.h"
53
54 #include "KX_KetsjiEngine.h"
55 #include "KX_BlenderSceneConverter.h"
56 #include "KX_PythonInit.h"
57 #include "KX_PyConstraintBinding.h"
58
59 #include "RAS_GLExtensionManager.h"
60 #include "RAS_OpenGLRasterizer.h"
61 #include "RAS_VAOpenGLRasterizer.h"
62 #include "RAS_ListRasterizer.h"
63
64 #include "NG_LoopBackNetworkDeviceInterface.h"
65
66 #include "SYS_System.h"
67
68 #include "GPU_extensions.h"
69 #include "Value.h"
70
71
72
73 #ifdef __cplusplus
74 extern "C" {
75 #endif
76         /***/
77 #include "DNA_view3d_types.h"
78 #include "DNA_screen_types.h"
79 #include "DNA_windowmanager_types.h"
80 #include "BKE_global.h"
81 #include "BKE_report.h"
82
83 #include "BKE_utildefines.h"
84 //XXX #include "BIF_screen.h"
85 //XXX #include "BIF_scrarea.h"
86
87 #include "BKE_main.h"
88 #include "BLI_blenlib.h"
89 #include "BLO_readfile.h"
90 #include "DNA_scene_types.h"
91         /***/
92
93 #include "AUD_C-API.h"
94
95 //XXX #include "BSE_headerbuttons.h"
96 #include "BKE_context.h"
97 #include "../../blender/windowmanager/WM_types.h"
98 #include "../../blender/windowmanager/wm_window.h"
99 #include "../../blender/windowmanager/wm_event_system.h"
100 #ifdef __cplusplus
101 }
102 #endif
103
104
105 static BlendFileData *load_game_data(char *filename)
106 {
107         ReportList reports;
108         BlendFileData *bfd;
109         
110         BKE_reports_init(&reports, RPT_STORE);
111         bfd= BLO_read_from_file(filename, &reports);
112
113         if (!bfd) {
114                 printf("Loading %s failed: ", filename);
115                 BKE_reports_print(&reports, RPT_ERROR);
116         }
117
118         BKE_reports_clear(&reports);
119
120         return bfd;
121 }
122
123 extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *cam_frame, 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(cam_frame->xmin);
133         area_rect.SetBottom(cam_frame->ymin);
134         area_rect.SetRight(cam_frame->xmax);
135         area_rect.SetTop(cam_frame->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                         if(scene->gm.framing.type == SCE_GAMEFRAMING_BARS) { /* Letterbox */
250                                 camzoom = 1.0f;
251                         }
252                         else {
253                                 camzoom = (1.41421 + (rv3d->camzoom / 50.0));
254                                 camzoom *= camzoom;
255                                 camzoom = 4.0 / camzoom;
256                         }
257                 }
258                 else {
259                         camzoom = 2.0;
260                 }
261
262                 
263
264                 ketsjiengine->SetDrawType(v3d->drawtype);
265                 ketsjiengine->SetCameraZoom(camzoom);
266                 
267                 // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
268                 if (exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME || exitrequested == KX_EXIT_REQUEST_RESTART_GAME)
269                 {
270                         exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
271                         if (bfd) BLO_blendfiledata_free(bfd);
272                         
273                         char basedpath[240];
274                         // base the actuator filename with respect
275                         // to the original file working directory
276
277                         if (exitstring != "")
278                                 strcpy(basedpath, exitstring.Ptr());
279
280                         // load relative to the last loaded file, this used to be relative
281                         // to the first file but that makes no sense, relative paths in
282                         // blend files should be relative to that file, not some other file
283                         // that happened to be loaded first
284                         BLI_convertstringcode(basedpath, pathname);
285                         bfd = load_game_data(basedpath);
286                         
287                         // if it wasn't loaded, try it forced relative
288                         if (!bfd)
289                         {
290                                 // just add "//" in front of it
291                                 char temppath[242];
292                                 strcpy(temppath, "//");
293                                 strcat(temppath, basedpath);
294                                 
295                                 BLI_convertstringcode(temppath, pathname);
296                                 bfd = load_game_data(temppath);
297                         }
298                         
299                         // if we got a loaded blendfile, proceed
300                         if (bfd)
301                         {
302                                 blenderdata = bfd->main;
303                                 startscenename = bfd->curscene->id.name + 2;
304
305                                 if(blenderdata) {
306                                         BLI_strncpy(G.sce, blenderdata->name, sizeof(G.sce));
307                                         BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
308 #ifndef DISABLE_PYTHON
309                                         setGamePythonPath(G.sce);
310 #endif
311                                 }
312                         }
313                         // else forget it, we can't find it
314                         else
315                         {
316                                 exitrequested = KX_EXIT_REQUEST_QUIT_GAME;
317                         }
318                 }
319                 
320                 Scene *blscene = NULL;
321                 if (!bfd)
322                 {
323                         blscene = (Scene*) blenderdata->scene.first;
324                         for (Scene *sce= (Scene*) blenderdata->scene.first; sce; sce= (Scene*) sce->id.next)
325                         {
326                                 if (startscenename == (sce->id.name+2))
327                                 {
328                                         blscene = sce;
329                                         break;
330                                 }
331                         }
332                 } else {
333                         blscene = bfd->curscene;
334                 }
335
336                 if (blscene)
337                 {
338                         int startFrame = blscene->r.cfra;
339                         ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
340                         
341                         // Quad buffered needs a special window.
342                         if(blscene->gm.stereoflag == STEREO_ENABLED){
343                                 if (blscene->gm.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
344                                         rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->gm.stereomode);
345                         }
346
347                         rasterizer->SetBackColor(blscene->gm.framing.col[0], blscene->gm.framing.col[1], blscene->gm.framing.col[2], 0.0f);
348                 }
349                 
350                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
351                 {
352                         if (rv3d->persp != V3D_CAMOB)
353                         {
354                                 ketsjiengine->EnableCameraOverride(startscenename);
355                                 ketsjiengine->SetCameraOverrideUseOrtho((rv3d->persp == V3D_ORTHO));
356                                 ketsjiengine->SetCameraOverrideProjectionMatrix(projmat);
357                                 ketsjiengine->SetCameraOverrideViewMatrix(viewmat);
358                                 ketsjiengine->SetCameraOverrideClipping(v3d->near, v3d->far);
359                                 ketsjiengine->SetCameraOverrideLens(v3d->lens);
360                         }
361                         
362                         // create a scene converter, create and convert the startingscene
363                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(blenderdata, ketsjiengine);
364                         ketsjiengine->SetSceneConverter(sceneconverter);
365                         sceneconverter->addInitFromFrame=false;
366                         if (always_use_expand_framing)
367                                 sceneconverter->SetAlwaysUseExpandFraming(true);
368
369                         bool usemat = false, useglslmat = false;
370
371                         if(GLEW_ARB_multitexture && GLEW_VERSION_1_1)
372                                 usemat = true;
373
374                         if(GPU_glsl_support())
375                                 useglslmat = true;
376                         else if(blscene->gm.matmode == GAME_MAT_GLSL)
377                                 usemat = false;
378
379             if(usemat && (blscene->gm.matmode != GAME_MAT_TEXFACE))
380                                 sceneconverter->SetMaterials(true);
381                         if(useglslmat && (blscene->gm.matmode == GAME_MAT_GLSL))
382                                 sceneconverter->SetGLSLMaterials(true);
383                                         
384                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
385                                 mousedevice,
386                                 networkdevice,
387                                 startscenename,
388                                 blscene);
389
390 #ifndef DISABLE_PYTHON
391                         // some python things
392                         PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest, blenderdata);
393                         ketsjiengine->SetPyNamespace(dictionaryobject);
394                         initRasterizer(rasterizer, canvas);
395                         PyObject *gameLogic = initGameLogic(ketsjiengine, startscene);
396                         PyDict_SetItemString(PyModule_GetDict(gameLogic), "globalDict", pyGlobalDict); // Same as importing the module.
397                         PyObject *gameLogic_keys = PyDict_Keys(PyModule_GetDict(gameLogic));
398                         PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module.
399                         
400                         initGameKeys();
401                         initPythonConstraintBinding();
402                         initMathutils();
403                         initGeometry();
404                         initBGL();
405 #ifdef WITH_FFMPEG
406                         initVideoTexture();
407 #endif
408 #endif // DISABLE_PYTHON
409
410                         //initialize Dome Settings
411                         if(blscene->gm.stereoflag == STEREO_DOME)
412                                 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);
413
414                         // initialize 3D Audio Settings
415                         AUD_set3DSetting(AUD_3DS_SPEED_OF_SOUND, blscene->audio.speed_of_sound);
416                         AUD_set3DSetting(AUD_3DS_DOPPLER_FACTOR, blscene->audio.doppler_factor);
417                         AUD_set3DSetting(AUD_3DS_DISTANCE_MODEL, blscene->audio.distance_model);
418
419                         if (sceneconverter)
420                         {
421                                 // convert and add scene
422                                 sceneconverter->ConvertScene(
423                                         startscene,
424                                         rendertools,
425                                         canvas);
426                                 ketsjiengine->AddScene(startscene);
427                                 
428                                 // init the rasterizer
429                                 rasterizer->Init();
430                                 
431                                 // start the engine
432                                 ketsjiengine->StartEngine(true);
433                                 
434
435                                 // Set the animation playback rate for ipo's and actions
436                                 // the framerate below should patch with FPS macro defined in blendef.h
437                                 // Could be in StartEngine set the framerate, we need the scene to do this
438                                 ketsjiengine->SetAnimFrameRate( (((double) blscene->r.frs_sec) / blscene->r.frs_sec_base) );
439                                 
440                                 // the mainloop
441                                 printf("\nBlender Game Engine Started\n\n");
442                                 while (!exitrequested)
443                                 {
444                                         // first check if we want to exit
445                                         exitrequested = ketsjiengine->GetExitCode();
446                                         
447                                         // kick the engine
448                                         bool render = ketsjiengine->NextFrame(); // XXX 2.5 Bug, This is never true! FIXME-  Campbell
449                                         
450                                         if (render)
451                                         {
452                                                 // render the frame
453                                                 ketsjiengine->Render();
454                                         }
455                                         
456                                         wm_window_process_events_nosleep(C);
457                                         
458                                         // test for the ESC key
459                                         //XXX while (qtest())
460                                         while(wmEvent *event= (wmEvent *)win->queue.first)
461                                         {
462                                                 short val = 0;
463                                                 //unsigned short event = 0; //XXX extern_qread(&val);
464                                                 
465                                                 if (keyboarddevice->ConvertBlenderEvent(event->type,event->val))
466                                                         exitrequested = KX_EXIT_REQUEST_BLENDER_ESC;
467                                                 
468                                                         /* Coordinate conversion... where
469                                                         * should this really be?
470                                                 */
471                                                 if (event->type==MOUSEMOVE) {
472                                                         /* Note nice! XXX 2.5 event hack */
473                                                         val = event->x - ar->winrct.xmin;
474                                                         mousedevice->ConvertBlenderEvent(MOUSEX, val);
475                                                         
476                                                         val = ar->winy - (event->y - ar->winrct.ymin) - 1;
477                                                         mousedevice->ConvertBlenderEvent(MOUSEY, val);
478                                                 }
479                                                 else {
480                                                         mousedevice->ConvertBlenderEvent(event->type,event->val);
481                                                 }
482                                                 
483                                                 BLI_remlink(&win->queue, event);
484                                                 wm_event_free(event);
485                                         }
486                                         
487                                 }
488                                 printf("\nBlender Game Engine Finished\n\n");
489                                 exitstring = ketsjiengine->GetExitString();
490
491
492                                 // when exiting the mainloop
493 #ifndef DISABLE_PYTHON
494                                 // Clears the dictionary by hand:
495                                 // This prevents, extra references to global variables
496                                 // inside the GameLogic dictionary when the python interpreter is finalized.
497                                 // which allows the scene to safely delete them :)
498                                 // see: (space.c)->start_game
499                                 
500                                 //PyDict_Clear(PyModule_GetDict(gameLogic));
501                                 
502                                 // Keep original items, means python plugins will autocomplete members
503                                 int listIndex;
504                                 PyObject *gameLogic_keys_new = PyDict_Keys(PyModule_GetDict(gameLogic));
505                                 for (listIndex=0; listIndex < PyList_Size(gameLogic_keys_new); listIndex++)  {
506                                         PyObject* item = PyList_GET_ITEM(gameLogic_keys_new, listIndex);
507                                         if (!PySequence_Contains(gameLogic_keys, item)) {
508                                                 PyDict_DelItem( PyModule_GetDict(gameLogic), item);
509                                         }
510                                 }
511                                 Py_DECREF(gameLogic_keys_new);
512                                 gameLogic_keys_new = NULL;
513 #endif
514                                 ketsjiengine->StopEngine();
515 #ifndef DISABLE_PYTHON
516                                 exitGamePythonScripting();
517 #endif
518                                 networkdevice->Disconnect();
519                         }
520                         if (sceneconverter)
521                         {
522                                 delete sceneconverter;
523                                 sceneconverter = NULL;
524                         }
525
526 #ifndef DISABLE_PYTHON
527                         Py_DECREF(gameLogic_keys);
528                         gameLogic_keys = NULL;
529 #endif
530                 }
531                 //lock frame and camera enabled - restoring global values
532                 if (v3d->scenelock==0){
533                         scene->lay= tmp_lay;
534                         scene->camera= tmp_camera;
535                 }
536
537                 // set the cursor back to normal
538                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
539                 
540                 // clean up some stuff
541                 if (ketsjiengine)
542                 {
543                         delete ketsjiengine;
544                         ketsjiengine = NULL;
545                 }
546                 if (kxsystem)
547                 {
548                         delete kxsystem;
549                         kxsystem = NULL;
550                 }
551                 if (networkdevice)
552                 {
553                         delete networkdevice;
554                         networkdevice = NULL;
555                 }
556                 if (keyboarddevice)
557                 {
558                         delete keyboarddevice;
559                         keyboarddevice = NULL;
560                 }
561                 if (mousedevice)
562                 {
563                         delete mousedevice;
564                         mousedevice = NULL;
565                 }
566                 if (rasterizer)
567                 {
568                         delete rasterizer;
569                         rasterizer = NULL;
570                 }
571                 if (rendertools)
572                 {
573                         delete rendertools;
574                         rendertools = NULL;
575                 }
576                 if (canvas)
577                 {
578                         delete canvas;
579                         canvas = NULL;
580                 }
581         
582         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
583         
584         if (bfd) BLO_blendfiledata_free(bfd);
585
586         BLI_strncpy(G.sce, oldsce, sizeof(G.sce));
587
588 #ifndef DISABLE_PYTHON
589         Py_DECREF(pyGlobalDict);
590
591         // Release Python's GIL
592         PyGILState_Release(gilstate);
593 #endif
594
595 }
596
597 extern "C" void StartKetsjiShellSimulation(struct wmWindow *win,
598                                                                  struct ARegion *ar,
599                                                                  char* scenename,
600                                                                  struct Main* maggie,
601                                                                  int always_use_expand_framing)
602 {
603     int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
604
605         Main* blenderdata = maggie;
606
607         RAS_Rect area_rect;
608         area_rect.SetLeft(ar->winrct.xmin);
609         area_rect.SetBottom(ar->winrct.ymin);
610         area_rect.SetRight(ar->winrct.xmax);
611         area_rect.SetTop(ar->winrct.ymax);
612
613         char* startscenename = scenename;
614         char pathname[FILE_MAXDIR+FILE_MAXFILE];
615         STR_String exitstring = "";
616
617         BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
618
619 #ifndef DISABLE_PYTHON
620         // Acquire Python's GIL (global interpreter lock)
621         // so we can safely run Python code and API calls
622         PyGILState_STATE gilstate = PyGILState_Ensure();
623 #endif
624
625         bgl::InitExtensions(true);
626
627         do
628         {
629
630                 // get some preferences
631                 SYS_SystemHandle syshandle = SYS_GetSystem();
632                 /*
633                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
634                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
635                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
636                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
637                 */
638                 bool game2ipo = true;//(SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
639                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
640                 bool usemat = false;
641
642                 // create the canvas, rasterizer and rendertools
643                 RAS_ICanvas* canvas = new KX_BlenderCanvas(win, area_rect);
644                 //canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
645                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
646                 RAS_IRasterizer* rasterizer = NULL;
647
648                 if(displaylists) {
649                         if (GLEW_VERSION_1_1)
650                                 rasterizer = new RAS_ListRasterizer(canvas, true, true);
651                         else
652                                 rasterizer = new RAS_ListRasterizer(canvas);
653                 }
654                 else if (GLEW_VERSION_1_1)
655                         rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
656                 else
657                         rasterizer = new RAS_OpenGLRasterizer(canvas);
658
659                 // create the inputdevices
660                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
661                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
662
663                 // create a networkdevice
664                 NG_NetworkDeviceInterface* networkdevice = new
665                         NG_LoopBackNetworkDeviceInterface();
666
667                 // create a ketsji/blendersystem (only needed for timing and stuff)
668                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
669
670                 // create the ketsjiengine
671                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
672
673                 Scene *blscene = NULL;
674
675                 blscene = (Scene*) maggie->scene.first;
676                 for (Scene *sce= (Scene*) maggie->scene.first; sce; sce= (Scene*) sce->id.next)
677                 {
678                         if (startscenename == (sce->id.name+2))
679                         {
680                                 blscene = sce;
681                                 break;
682                         }
683                 }
684
685         int cframe = 1, startFrame;
686                 if (blscene)
687                 {
688                         cframe=blscene->r.cfra;
689                         startFrame = blscene->r.sfra;
690                         blscene->r.cfra=startFrame;
691                         // update_for_newframe(); // XXX scene_update_for_newframe wont cut it!
692                         ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
693                 }
694
695                 // Quad buffered needs a special window.
696                 if(blscene->gm.stereoflag == STEREO_ENABLED){
697                         if (blscene->gm.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
698                                 rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->gm.stereomode);
699                 }
700                 rasterizer->SetBackColor(blscene->gm.framing.col[0], blscene->gm.framing.col[1], blscene->gm.framing.col[2], 0.0f);
701
702                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
703                 {
704                         // create a scene converter, create and convert the startingscene
705                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(maggie, ketsjiengine);
706                         ketsjiengine->SetSceneConverter(sceneconverter);
707                         sceneconverter->addInitFromFrame=true;
708                         
709                         if (always_use_expand_framing)
710                                 sceneconverter->SetAlwaysUseExpandFraming(true);
711
712                         if(usemat)
713                                 sceneconverter->SetMaterials(true);
714
715                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
716                                 mousedevice,
717                                 networkdevice,
718                                 startscenename,
719                                 blscene);
720
721 #ifndef DISABLE_PYTHON
722                         // some python things
723                         PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest, blenderdata);
724                         ketsjiengine->SetPyNamespace(dictionaryobject);
725                         initRasterizer(rasterizer, canvas);
726                         PyObject *gameLogic = initGameLogic(ketsjiengine, startscene);
727                         PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module
728                         initGameKeys();
729                         initPythonConstraintBinding();
730                         initMathutils();
731                         initGeometry();
732                         initBGL();
733 #ifdef WITH_FFMPEG
734                         initVideoTexture();
735 #endif
736 #endif // DISABLE_PYTHON
737
738                         if (sceneconverter)
739                         {
740                                 // convert and add scene
741                                 sceneconverter->ConvertScene(
742                                         startscene,
743                                         rendertools,
744                                         canvas);
745                                 ketsjiengine->AddScene(startscene);
746
747                                 // start the engine
748                                 ketsjiengine->StartEngine(false);
749                                 
750                                 ketsjiengine->SetUseFixedTime(true);
751                                 
752                                 ketsjiengine->SetTicRate(
753                                         (double) blscene->r.frs_sec /
754                                         (double) blscene->r.frs_sec_base);
755
756                                 // the mainloop
757                                 while ((blscene->r.cfra<=blscene->r.efra)&&(!exitrequested))
758                                 {
759                     printf("frame %i\n",blscene->r.cfra);
760                     // first check if we want to exit
761                                         exitrequested = ketsjiengine->GetExitCode();
762         
763                                         // kick the engine
764                                         ketsjiengine->NextFrame();
765                                     blscene->r.cfra=blscene->r.cfra+1;
766                                     // update_for_newframe(); // XXX scene_update_for_newframe wont cut it
767                                         
768                                 }
769                                 exitstring = ketsjiengine->GetExitString();
770                         }
771                         if (sceneconverter)
772                         {
773                                 delete sceneconverter;
774                                 sceneconverter = NULL;
775                         }
776                 }
777                 blscene->r.cfra=cframe;
778                 // set the cursor back to normal
779                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
780
781                 // clean up some stuff
782                 if (ketsjiengine)
783                 {
784                         delete ketsjiengine;
785                         ketsjiengine = NULL;
786                 }
787                 if (kxsystem)
788                 {
789                         delete kxsystem;
790                         kxsystem = NULL;
791                 }
792                 if (networkdevice)
793                 {
794                         delete networkdevice;
795                         networkdevice = NULL;
796                 }
797                 if (keyboarddevice)
798                 {
799                         delete keyboarddevice;
800                         keyboarddevice = NULL;
801                 }
802                 if (mousedevice)
803                 {
804                         delete mousedevice;
805                         mousedevice = NULL;
806                 }
807                 if (rasterizer)
808                 {
809                         delete rasterizer;
810                         rasterizer = NULL;
811                 }
812                 if (rendertools)
813                 {
814                         delete rendertools;
815                         rendertools = NULL;
816                 }
817
818         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
819
820 #ifndef DISABLE_PYTHON
821         // Release Python's GIL
822         PyGILState_Release(gilstate);
823 #endif
824 }