f37928fe2d51ea8ef428eb256d147e32deafac00
[blender.git] / source / gameengine / BlenderRoutines / BL_KetsjiEmbedStart.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  * Blender's Ketsji startpoint
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <signal.h>
39
40 #ifdef WIN32
41 // don't show stl-warnings
42 #pragma warning (disable:4786)
43 #endif
44
45 #ifdef __APPLE__
46 #define GL_GLEXT_LEGACY 1
47 #endif 
48
49 #include "KX_BlenderGL.h"
50 #include "KX_BlenderCanvas.h"
51 #include "KX_BlenderKeyboardDevice.h"
52 #include "KX_BlenderMouseDevice.h"
53 #include "KX_BlenderRenderTools.h"
54 #include "KX_BlenderSystem.h"
55 #include "BL_Material.h"
56
57 #include "KX_KetsjiEngine.h"
58 #include "KX_BlenderSceneConverter.h"
59 #include "KX_PythonInit.h"
60 #include "KX_PyConstraintBinding.h"
61
62 #include "RAS_OpenGLRasterizer.h"
63 #include "RAS_VAOpenGLRasterizer.h"
64 #include "RAS_ListRasterizer.h"
65 #include "RAS_GLExtensionManager.h"
66
67 #include "NG_LoopBackNetworkDeviceInterface.h"
68 #include "SND_DeviceManager.h"
69
70 #include "SYS_System.h"
71
72         /***/
73
74 #include "DNA_view3d_types.h"
75 #include "DNA_screen_types.h"
76 #include "BKE_global.h"
77 #include "BIF_screen.h"
78 #include "BIF_scrarea.h"
79
80 #include "BKE_main.h"   
81 #include "BLI_blenlib.h"
82 #include "BLO_readfile.h"
83 #include "DNA_scene_types.h"
84         /***/
85
86 static BlendFileData *load_game_data(char *filename) {
87         BlendReadError error;
88         //this doesn't work anymore for relative paths, so use BLO_read_from_memory instead
89         //BlendFileData *bfd= BLO_read_from_file(filename, &error);
90         FILE* file = fopen(filename,"rb");
91         BlendFileData *bfd  = 0;
92         if (file)
93         {
94                 fseek(file, 0L, SEEK_END);
95                 int len= ftell(file);
96                 fseek(file, 0L, SEEK_SET);      
97                 char* filebuffer= new char[len];//MEM_mallocN(len, "text_buffer");
98                 int sizeread = fread(filebuffer,len,1,file);
99                 if (sizeread==1)
100                 {
101                         bfd = BLO_read_from_memory(filebuffer, len, &error);
102                 }
103                 fclose(file);
104         }
105
106         if (!bfd) {
107                 printf("Loading %s failed: %s\n", filename, BLO_bre_as_string(error));
108         }
109         
110         return bfd;
111 }
112
113 extern "C" void StartKetsjiShell(struct ScrArea *area,
114                                                                  char* scenename,
115                                                                  struct Main* maggie1,
116                                                                  struct SpaceIpo *sipo,
117                                                                  int always_use_expand_framing)
118 {
119         int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
120         
121         Main* blenderdata = maggie1;
122
123         char* startscenename = scenename;
124         char pathname[160];
125         strcpy (pathname, blenderdata->name);
126         STR_String exitstring = "";
127         BlendFileData *bfd= NULL;
128
129         bgl::InitExtensions(1);
130         
131         do
132         {
133                 View3D *v3d= (View3D*) area->spacedata.first;
134                 
135                 // get some preferences
136                 SYS_SystemHandle syshandle = SYS_GetSystem();
137                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
138                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
139                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
140                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
141                 bool game2ipo = (SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
142                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
143                 bool usemat = false;
144                 
145                 #ifdef GL_ARB_multitexture
146                 if(bgl::RAS_EXT_support._ARB_multitexture && bgl::QueryVersion(1, 1)) {
147                         usemat = (SYS_GetCommandLineInt(syshandle, "blender_material", 0) != 0);
148                         int unitmax=0;
149                         glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, (GLint*)&unitmax);
150                         bgl::max_texture_units = MAXTEX>unitmax?unitmax:MAXTEX;
151                         //std::cout << "using(" << bgl::max_texture_units << ") of(" << unitmax << ") texture units." << std::endl;
152                 } else {
153                         bgl::max_texture_units = 0;
154                 }
155                 #endif
156
157
158                 // create the canvas, rasterizer and rendertools
159                 RAS_ICanvas* canvas = new KX_BlenderCanvas(area);
160                 canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
161                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
162                 RAS_IRasterizer* rasterizer = NULL;
163                 
164                 // let's see if we want to use vertexarrays or not
165                 int usevta = SYS_GetCommandLineInt(syshandle,"vertexarrays",1);
166                 bool useVertexArrays = (usevta > 0);
167                 
168                 bool lock_arrays = (displaylists && useVertexArrays);
169
170                 if(displaylists && !useVertexArrays)
171                         rasterizer = new RAS_ListRasterizer(canvas);
172                 else if (useVertexArrays && bgl::QueryVersion(1, 1))
173                         rasterizer = new RAS_VAOpenGLRasterizer(canvas, lock_arrays);
174                 else
175                         rasterizer = new RAS_OpenGLRasterizer(canvas);
176                 
177                 // create the inputdevices
178                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
179                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
180                 
181                 // create a networkdevice
182                 NG_NetworkDeviceInterface* networkdevice = new
183                         NG_LoopBackNetworkDeviceInterface();
184                 
185                 // get an audiodevice
186                 SND_DeviceManager::Subscribe();
187                 SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
188                 audiodevice->UseCD();
189                 
190                 // create a ketsji/blendersystem (only needed for timing and stuff)
191                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
192                 
193                 // create the ketsjiengine
194                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
195                 
196                 // set the devices
197                 ketsjiengine->SetKeyboardDevice(keyboarddevice);
198                 ketsjiengine->SetMouseDevice(mousedevice);
199                 ketsjiengine->SetNetworkDevice(networkdevice);
200                 ketsjiengine->SetCanvas(canvas);
201                 ketsjiengine->SetRenderTools(rendertools);
202                 ketsjiengine->SetRasterizer(rasterizer);
203                 ketsjiengine->SetNetworkDevice(networkdevice);
204                 ketsjiengine->SetAudioDevice(audiodevice);
205                 ketsjiengine->SetUseFixedTime(usefixed);
206                 ketsjiengine->SetTimingDisplay(frameRate, profile, properties);
207
208                 
209         
210                 // some blender stuff
211                 MT_CmMatrix4x4 projmat;
212                 MT_CmMatrix4x4 viewmat;
213                 int i;
214                 
215                 for (i = 0; i < 16; i++)
216                 {
217                         float *viewmat_linear= (float*) v3d->viewmat;
218                         viewmat.setElem(i, viewmat_linear[i]);
219                 }
220                 for (i = 0; i < 16; i++)
221                 {
222                         float *projmat_linear = (float*) area->winmat;
223                         projmat.setElem(i, projmat_linear[i]);
224                 }
225                 
226                 float camzoom = (1.41421 + (v3d->camzoom / 50.0));
227                 camzoom *= camzoom;
228                 camzoom = 4.0 / camzoom;
229                 
230                 ketsjiengine->SetDrawType(v3d->drawtype);
231                 ketsjiengine->SetCameraZoom(camzoom);
232                 
233                 // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
234                 if (exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME || exitrequested == KX_EXIT_REQUEST_RESTART_GAME)
235                 {
236                         exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
237                         if (bfd) BLO_blendfiledata_free(bfd);
238                         
239                         char basedpath[160];
240                         // base the actuator filename with respect
241                         // to the original file working directory
242                         if (exitstring != "")
243                                 strcpy(basedpath, exitstring.Ptr());
244
245                         BLI_convertstringcode(basedpath, pathname, 0);
246                         bfd = load_game_data(basedpath);
247                         
248                         // if it wasn't loaded, try it forced relative
249                         if (!bfd)
250                         {
251                                 // just add "//" in front of it
252                                 char temppath[162];
253                                 strcpy(temppath, "//");
254                                 strcat(temppath, basedpath);
255                                 
256                                 BLI_convertstringcode(temppath, pathname, 0);
257                                 bfd = load_game_data(temppath);
258                         }
259                         
260                         // if we got a loaded blendfile, proceed
261                         if (bfd)
262                         {
263                                 blenderdata = bfd->main;
264                                 startscenename = bfd->curscene->id.name + 2;
265                         }
266                         // else forget it, we can't find it
267                         else
268                         {
269                                 exitrequested = KX_EXIT_REQUEST_QUIT_GAME;
270                         }
271                 }
272                 
273                 Scene *blscene = NULL;
274                 if (!bfd)
275                 {
276                         blscene = (Scene*) blenderdata->scene.first;
277                         for (Scene *sce= (Scene*) blenderdata->scene.first; sce; sce= (Scene*) sce->id.next)
278                         {
279                                 if (startscenename == (sce->id.name+2))
280                                 {
281                                         blscene = sce;
282                                         break;
283                                 }
284                         }
285                 } else {
286                         blscene = bfd->curscene;
287                 }
288
289                 if (blscene)
290                 {
291                         int startFrame = blscene->r.cfra;
292                         ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
293                 }
294
295
296                 // Quad buffered needs a special window.
297                 if (blscene->r.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
298                         rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->r.stereomode);
299                 
300                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
301                 {
302                         if (v3d->persp != 2)
303                         {
304                                 ketsjiengine->EnableCameraOverride(startscenename);
305                                 ketsjiengine->SetCameraOverrideUseOrtho((v3d->persp == 0));
306                                 ketsjiengine->SetCameraOverrideProjectionMatrix(projmat);
307                                 ketsjiengine->SetCameraOverrideViewMatrix(viewmat);
308                         }
309                         
310                         // create a scene converter, create and convert the startingscene
311                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(blenderdata,sipo, ketsjiengine);
312                         ketsjiengine->SetSceneConverter(sceneconverter);
313                         
314                         if (always_use_expand_framing)
315                                 sceneconverter->SetAlwaysUseExpandFraming(true);
316                         
317                         if(usemat)
318                                 sceneconverter->SetMaterials(true);
319                                         
320                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
321                                 mousedevice,
322                                 networkdevice,
323                                 audiodevice,
324                                 startscenename);
325                         
326                         // some python things
327                         PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest);
328                         ketsjiengine->SetPythonDictionary(dictionaryobject);
329                         initRasterizer(rasterizer, canvas);
330                         PyObject *gameLogic = initGameLogic(startscene);
331                         initGameKeys();
332                         initPythonConstraintBinding();
333
334                         
335                         if (sceneconverter)
336                         {
337                                 // convert and add scene
338                                 sceneconverter->ConvertScene(
339                                         startscenename,
340                                         startscene,
341                                         dictionaryobject,
342                                         keyboarddevice,
343                                         rendertools,
344                                         canvas);
345                                 ketsjiengine->AddScene(startscene);
346                                 
347                                 // init the rasterizer
348                                 rasterizer->Init();
349                                 
350                                 // start the engine
351                                 ketsjiengine->StartEngine(true);
352                                 
353                                 // the mainloop
354                                 while (!exitrequested)
355                                 {
356                                         // first check if we want to exit
357                                         exitrequested = ketsjiengine->GetExitCode();
358                                         
359                                         // kick the engine
360                                         bool render = ketsjiengine->NextFrame();
361                                         
362                                         if (render)
363                                         {
364                                                 // render the frame
365                                                 ketsjiengine->Render();
366                                         }
367                                         
368                                         // test for the ESC key
369                                         while (qtest())
370                                         {
371                                                 short val; 
372                                                 unsigned short event = extern_qread(&val);
373                                                 
374                                                 if (keyboarddevice->ConvertBlenderEvent(event,val))
375                                                         exitrequested = KX_EXIT_REQUEST_BLENDER_ESC;
376                                                 
377                                                         /* Coordinate conversion... where
378                                                         * should this really be?
379                                                 */
380                                                 if (event==MOUSEX) {
381                                                         val = val - scrarea_get_win_x(area);
382                                                 } else if (event==MOUSEY) {
383                                                         val = scrarea_get_win_height(area) - (val - scrarea_get_win_y(area)) - 1;
384                                                 }
385                                                 
386                                                 mousedevice->ConvertBlenderEvent(event,val);
387                                         }
388                                 }
389                                 exitstring = ketsjiengine->GetExitString();
390                                 
391                                 // when exiting the mainloop
392                                 dictionaryClearByHand(gameLogic);
393                                 ketsjiengine->StopEngine();
394                                 exitGamePythonScripting();
395                                 networkdevice->Disconnect();
396                         }
397                         if (sceneconverter)
398                         {
399                                 delete sceneconverter;
400                                 sceneconverter = NULL;
401                         }
402                 }
403                 // set the cursor back to normal
404                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
405                 
406                 // clean up some stuff
407                 audiodevice->StopCD();
408                 
409                 if (ketsjiengine)
410                 {
411                         delete ketsjiengine;
412                         ketsjiengine = NULL;
413                 }
414                 if (kxsystem)
415                 {
416                         delete kxsystem;
417                         kxsystem = NULL;
418                 }
419                 if (networkdevice)
420                 {
421                         delete networkdevice;
422                         networkdevice = NULL;
423                 }
424                 if (keyboarddevice)
425                 {
426                         delete keyboarddevice;
427                         keyboarddevice = NULL;
428                 }
429                 if (mousedevice)
430                 {
431                         delete mousedevice;
432                         mousedevice = NULL;
433                 }
434                 if (rasterizer)
435                 {
436                         delete rasterizer;
437                         rasterizer = NULL;
438                 }
439                 if (rendertools)
440                 {
441                         delete rendertools;
442                         rendertools = NULL;
443                 }
444                 if (canvas)
445                 {
446                         delete canvas;
447                         canvas = NULL;
448                 }
449                 SND_DeviceManager::Unsubscribe();
450         
451         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
452
453         if (bfd) BLO_blendfiledata_free(bfd);
454 }
455 #ifdef __cplusplus
456 extern "C" {
457 #endif
458 #include "BSE_headerbuttons.h"
459 void update_for_newframe();
460 #ifdef __cplusplus
461 }
462 #endif
463
464 extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
465                                                                  char* scenename,
466                                                                  struct Main* maggie,
467                                                                  struct SpaceIpo *sipo,
468                                                                  int always_use_expand_framing)
469 {
470     int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
471
472         Main* blenderdata = maggie;
473
474         char* startscenename = scenename;
475         char pathname[160];
476         strcpy (pathname, maggie->name);
477         STR_String exitstring = "";
478         BlendFileData *bfd= NULL;
479
480         bgl::InitExtensions(1);
481
482         do
483         {
484 //              View3D *v3d= (View3D*) area->spacedata.first;
485
486                 // get some preferences
487                 SYS_SystemHandle syshandle = SYS_GetSystem();
488                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
489                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
490                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
491                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
492                 bool game2ipo = true;//(SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0);
493                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
494                 bool usemat = false;
495
496 /*              #ifdef GL_ARB_multitexture
497                 if(bgl::RAS_EXT_support._ARB_multitexture && bgl::QueryVersion(1, 1)) {
498                         usemat = (SYS_GetCommandLineInt(syshandle, "blender_material", 0) != 0);
499                         int unitmax=0;
500                         glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, (GLint*)&unitmax);
501                         bgl::max_texture_units = MAXTEX>unitmax?unitmax:MAXTEX;
502                         //std::cout << "using(" << bgl::max_texture_units << ") of(" << unitmax << ") texture units." << std::endl;
503                 } else {
504                         bgl::max_texture_units = 0;
505                 }
506                 #endif
507
508 */
509                 // create the canvas, rasterizer and rendertools
510                 RAS_ICanvas* canvas = new KX_BlenderCanvas(area);
511                 //canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
512                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
513                 RAS_IRasterizer* rasterizer = NULL;
514
515                 // let's see if we want to use vertexarrays or not
516                 int usevta = SYS_GetCommandLineInt(syshandle,"vertexarrays",1);
517                 bool useVertexArrays = (usevta > 0);
518
519                 bool lock_arrays = (displaylists && useVertexArrays);
520
521                 if(displaylists && !useVertexArrays)
522                         rasterizer = new RAS_ListRasterizer(canvas);
523                 else if (useVertexArrays && bgl::QueryVersion(1, 1))
524                         rasterizer = new RAS_VAOpenGLRasterizer(canvas, lock_arrays);
525                 else
526                         rasterizer = new RAS_OpenGLRasterizer(canvas);
527
528                 // create the inputdevices
529                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
530                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
531
532                 // create a networkdevice
533                 NG_NetworkDeviceInterface* networkdevice = new
534                         NG_LoopBackNetworkDeviceInterface();
535
536                 // get an audiodevice
537                 SND_DeviceManager::Subscribe();
538                 SND_IAudioDevice* audiodevice = SND_DeviceManager::Instance();
539                 audiodevice->UseCD();
540
541                 // create a ketsji/blendersystem (only needed for timing and stuff)
542                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
543
544                 // create the ketsjiengine
545                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
546
547 /*              // set the devices
548                 ketsjiengine->SetKeyboardDevice(keyboarddevice);
549                 ketsjiengine->SetMouseDevice(mousedevice);
550                 ketsjiengine->SetNetworkDevice(networkdevice);
551                 ketsjiengine->SetCanvas(canvas);
552                 ketsjiengine->SetRenderTools(rendertools);
553                 ketsjiengine->SetRasterizer(rasterizer);
554                 ketsjiengine->SetNetworkDevice(networkdevice);
555                 ketsjiengine->SetAudioDevice(audiodevice);
556                 ketsjiengine->SetUseFixedTime(usefixed);
557                 ketsjiengine->SetTimingDisplay(frameRate, profile, properties);
558
559 */
560
561                 // some blender stuff
562                 MT_CmMatrix4x4 projmat;
563                 MT_CmMatrix4x4 viewmat;
564                 int i;
565
566 /*              for (i = 0; i < 16; i++)
567                 {
568                         float *viewmat_linear= (float*) v3d->viewmat;
569                         viewmat.setElem(i, viewmat_linear[i]);
570                 }
571                 for (i = 0; i < 16; i++)
572                 {
573                         float *projmat_linear = (float*) area->winmat;
574                         projmat.setElem(i, projmat_linear[i]);
575                 }
576 */
577 /*              float camzoom = (1.41421 + (v3d->camzoom / 50.0));
578                 camzoom *= camzoom;
579                 camzoom = 4.0 / camzoom;
580 */
581 //              ketsjiengine->SetDrawType(v3d->drawtype);
582 //              ketsjiengine->SetCameraZoom(camzoom);
583 /*
584                 // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
585                 if (exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME || exitrequested == KX_EXIT_REQUEST_RESTART_GAME)
586                 {
587                         exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
588                         if (bfd) BLO_blendfiledata_free(bfd);
589
590                         char basedpath[160];
591                         // base the actuator filename with respect
592                         // to the original file working directory
593                         if (exitstring != "")
594                                 strcpy(basedpath, exitstring.Ptr());
595
596                         BLI_convertstringcode(basedpath, pathname, 0);
597                         bfd = load_game_data(basedpath);
598
599                         // if it wasn't loaded, try it forced relative
600                         if (!bfd)
601                         {
602                                 // just add "//" in front of it
603                                 char temppath[162];
604                                 strcpy(temppath, "//");
605                                 strcat(temppath, basedpath);
606
607                                 BLI_convertstringcode(temppath, pathname, 0);
608                                 bfd = load_game_data(temppath);
609                         }
610
611                         // if we got a loaded blendfile, proceed
612                         if (bfd)
613                         {
614                                 blenderdata = bfd->main;
615                                 startscenename = bfd->curscene->id.name + 2;
616                         }
617                         // else forget it, we can't find it
618                         else
619                         {
620                                 exitrequested = KX_EXIT_REQUEST_QUIT_GAME;
621                         }
622                 }
623 */
624                 Scene *blscene = NULL;
625                 if (!bfd)
626                 {
627                         blscene = (Scene*) maggie->scene.first;
628                         for (Scene *sce= (Scene*) maggie->scene.first; sce; sce= (Scene*) sce->id.next)
629                         {
630                                 if (startscenename == (sce->id.name+2))
631                                 {
632                                         blscene = sce;
633                                         break;
634                                 }
635                         }
636                 } else {
637                         blscene = bfd->curscene;
638                 }
639         int cframe,startFrame;
640                 if (blscene)
641                 {
642                         cframe=blscene->r.cfra;
643                         startFrame = blscene->r.sfra;
644                         blscene->r.cfra=startFrame;
645                         update_for_newframe();
646                         ketsjiengine->SetGame2IpoMode(game2ipo,startFrame);
647                 }
648
649                 // Quad buffered needs a special window.
650                 if (blscene->r.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
651                         rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->r.stereomode);
652
653                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
654                 {
655                         /*if (v3d->persp != 2)
656                         {
657                                 ketsjiengine->EnableCameraOverride(startscenename);
658                                 ketsjiengine->SetCameraOverrideUseOrtho((v3d->persp == 0));
659                                 ketsjiengine->SetCameraOverrideProjectionMatrix(projmat);
660                                 ketsjiengine->SetCameraOverrideViewMatrix(viewmat);
661                         }*/
662
663                         // create a scene converter, create and convert the startingscene
664                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(maggie,sipo, ketsjiengine);
665                         ketsjiengine->SetSceneConverter(sceneconverter);
666
667                         if (always_use_expand_framing)
668                                 sceneconverter->SetAlwaysUseExpandFraming(true);
669
670                         if(usemat)
671                                 sceneconverter->SetMaterials(true);
672
673                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
674                                 mousedevice,
675                                 networkdevice,
676                                 audiodevice,
677                                 startscenename);
678
679                         // some python things
680                         PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest);
681                         ketsjiengine->SetPythonDictionary(dictionaryobject);
682                         initRasterizer(rasterizer, canvas);
683                         PyObject *gameLogic = initGameLogic(startscene);
684                         initGameKeys();
685                         initPythonConstraintBinding();
686
687                         if (sceneconverter)
688                         {
689                                 // convert and add scene
690                                 sceneconverter->ConvertScene(
691                                         startscenename,
692                                         startscene,
693                                         dictionaryobject,
694                                         keyboarddevice,
695                                         rendertools,
696                                         canvas);
697                                 ketsjiengine->AddScene(startscene);
698
699                                 // init the rasterizer
700                                 //rasterizer->Init();
701
702                                 // start the engine
703                                 ketsjiengine->StartEngine(false);
704
705                                 // the mainloop
706                                 while ((blscene->r.cfra<=blscene->r.efra)&&(!exitrequested))
707                                 {
708                     printf("frame %i\n",blscene->r.cfra);
709                     // first check if we want to exit
710                                         exitrequested = ketsjiengine->GetExitCode();
711
712                                         // kick the engine
713                                         ketsjiengine->NextFrame();
714                                     blscene->r.cfra=blscene->r.cfra+1;
715                                     update_for_newframe();
716                                         // render the frame
717                                         //ketsjiengine->Render();
718
719                                         // test for the ESC key
720                                         /*while (qtest())
721                                         {
722                                                 short val;
723                                                 unsigned short event = extern_qread(&val);
724
725                                                 if (keyboarddevice->ConvertBlenderEvent(event,val))
726                                                         exitrequested = KX_EXIT_REQUEST_BLENDER_ESC;
727
728                                                 if (event==MOUSEX) {
729                                                         val = val - scrarea_get_win_x(area);
730                                                 } else if (event==MOUSEY) {
731                                                         val = scrarea_get_win_height(area) - (val - scrarea_get_win_y(area)) - 1;
732                                                 }
733
734                                                 mousedevice->ConvertBlenderEvent(event,val);
735                                         }*/
736                                 }
737                                 exitstring = ketsjiengine->GetExitString();
738                                 // when exiting the mainloop
739                                 //dictionaryClearByHand(gameLogic);
740                                 //ketsjiengine->StopEngine();
741                                 //exitGamePythonScripting();
742                                 //networkdevice->Disconnect();
743                         }
744                         if (sceneconverter)
745                         {
746                                 delete sceneconverter;
747                                 sceneconverter = NULL;
748                         }
749                 }
750                 blscene->r.cfra=cframe;
751                 // set the cursor back to normal
752                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
753
754                 // clean up some stuff
755                 audiodevice->StopCD();
756                 if (ketsjiengine)
757                 {
758                         delete ketsjiengine;
759                         ketsjiengine = NULL;
760                 }
761                 if (kxsystem)
762                 {
763                         delete kxsystem;
764                         kxsystem = NULL;
765                 }
766                 if (networkdevice)
767                 {
768                         delete networkdevice;
769                         networkdevice = NULL;
770                 }
771                 if (keyboarddevice)
772                 {
773                         delete keyboarddevice;
774                         keyboarddevice = NULL;
775                 }
776                 if (mousedevice)
777                 {
778                         delete mousedevice;
779                         mousedevice = NULL;
780                 }
781                 /*if (rasterizer)
782                 {
783                         delete rasterizer;
784                         rasterizer = NULL;
785                 }
786                 if (rendertools)
787                 {
788                         delete rendertools;
789                         rendertools = NULL;
790                 }
791                 if (canvas)
792                 {
793                         delete canvas;
794                         canvas = NULL;
795                 }*/
796                 SND_DeviceManager::Unsubscribe();
797
798         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
799         if (bfd) BLO_blendfiledata_free(bfd);
800 }