- WITH_CXX_GUARDEDALLOC working again
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #include <signal.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34
35 #ifdef WIN32
36 // don't show stl-warnings
37 #pragma warning (disable:4786)
38 #endif
39
40 #include "GL/glew.h"
41
42 #include "KX_BlenderGL.h"
43 #include "KX_BlenderCanvas.h"
44 #include "KX_BlenderKeyboardDevice.h"
45 #include "KX_BlenderMouseDevice.h"
46 #include "KX_BlenderRenderTools.h"
47 #include "KX_BlenderSystem.h"
48 #include "BL_Material.h"
49
50 #include "KX_KetsjiEngine.h"
51 #include "KX_BlenderSceneConverter.h"
52 #include "KX_PythonInit.h"
53 #include "KX_PyConstraintBinding.h"
54
55 #include "RAS_GLExtensionManager.h"
56 #include "RAS_OpenGLRasterizer.h"
57 #include "RAS_VAOpenGLRasterizer.h"
58 #include "RAS_ListRasterizer.h"
59
60 #include "NG_LoopBackNetworkDeviceInterface.h"
61
62 #include "SYS_System.h"
63
64 #include "GPU_extensions.h"
65 #include "Value.h"
66
67
68
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72         /***/
73 #include "DNA_view3d_types.h"
74 #include "DNA_screen_types.h"
75 #include "DNA_windowmanager_types.h"
76 #include "BKE_global.h"
77 #include "BKE_report.h"
78
79 #include "BKE_utildefines.h"
80 //XXX #include "BIF_screen.h"
81 //XXX #include "BIF_scrarea.h"
82
83 #include "BKE_main.h"
84 #include "BLI_blenlib.h"
85 #include "BLO_readfile.h"
86 #include "DNA_scene_types.h"
87         /***/
88
89 #include "AUD_C-API.h"
90
91 //XXX #include "BSE_headerbuttons.h"
92 #include "BKE_context.h"
93 #include "../../blender/windowmanager/WM_types.h"
94 #include "../../blender/windowmanager/wm_window.h"
95 #include "../../blender/windowmanager/wm_event_system.h"
96 #ifdef __cplusplus
97 }
98 #endif
99
100
101 static BlendFileData *load_game_data(char *filename)
102 {
103         ReportList reports;
104         BlendFileData *bfd;
105         
106         BKE_reports_init(&reports, RPT_STORE);
107         bfd= BLO_read_from_file(filename, &reports);
108
109         if (!bfd) {
110                 printf("Loading %s failed: ", filename);
111                 BKE_reports_print(&reports, RPT_ERROR);
112         }
113
114         BKE_reports_clear(&reports);
115
116         return bfd;
117 }
118
119 extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *cam_frame, int always_use_expand_framing)
120 {
121         /* context values */
122         struct wmWindow *win= CTX_wm_window(C);
123         struct Scene *scene= CTX_data_scene(C);
124         struct Main* maggie1= CTX_data_main(C);
125
126
127         RAS_Rect area_rect;
128         area_rect.SetLeft(cam_frame->xmin);
129         area_rect.SetBottom(cam_frame->ymin);
130         area_rect.SetRight(cam_frame->xmax);
131         area_rect.SetTop(cam_frame->ymax);
132
133         int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
134         Main* blenderdata = maggie1;
135
136         char* startscenename = scene->id.name+2;
137         char pathname[FILE_MAXDIR+FILE_MAXFILE], oldsce[FILE_MAXDIR+FILE_MAXFILE];
138         STR_String exitstring = "";
139         BlendFileData *bfd= NULL;
140
141         BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
142         BLI_strncpy(oldsce, G.sce, sizeof(oldsce));
143 #ifndef DISABLE_PYTHON
144         resetGamePythonPath(); // need this so running a second time wont use an old blendfiles path
145         setGamePythonPath(G.sce);
146
147         // Acquire Python's GIL (global interpreter lock)
148         // so we can safely run Python code and API calls
149         PyGILState_STATE gilstate = PyGILState_Ensure();
150         
151         PyObject *pyGlobalDict = PyDict_New(); /* python utility storage, spans blend file loading */
152 #endif
153         
154         bgl::InitExtensions(true);
155
156         do
157         {
158                 View3D *v3d= CTX_wm_view3d(C);
159                 RegionView3D *rv3d= CTX_wm_region_view3d(C);
160
161                 // get some preferences
162                 SYS_SystemHandle syshandle = SYS_GetSystem();
163                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
164                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
165                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
166                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
167                 bool animation_record = (SYS_GetCommandLineInt(syshandle, "animation_record", 0) != 0);
168                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
169 #ifndef DISABLE_PYTHON
170                 bool nodepwarnings = (SYS_GetCommandLineInt(syshandle, "ignore_deprecation_warnings", 0) != 0);
171 #endif
172                 bool novertexarrays = (SYS_GetCommandLineInt(syshandle, "novertexarrays", 0) != 0);
173                 if(animation_record) usefixed= true; /* override since you's always want fixed time for sim recording */
174
175                 // create the canvas, rasterizer and rendertools
176                 RAS_ICanvas* canvas = new KX_BlenderCanvas(win, area_rect, ar);
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==RV3D_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_path_abs(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_path_abs(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= bfd ? bfd->curscene : (Scene *)BLI_findstring(&blenderdata->scene, startscenename, offsetof(ID, name) + 2);
321
322                 if (blscene)
323                 {
324                         int startFrame = blscene->r.cfra;
325                         ketsjiengine->SetAnimRecordMode(animation_record, startFrame);
326                         
327                         // Quad buffered needs a special window.
328                         if(blscene->gm.stereoflag == STEREO_ENABLED){
329                                 if (blscene->gm.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
330                                         rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->gm.stereomode);
331
332                                 rasterizer->SetEyeSeparation(blscene->gm.eyeseparation);
333                         }
334
335                         rasterizer->SetBackColor(blscene->gm.framing.col[0], blscene->gm.framing.col[1], blscene->gm.framing.col[2], 0.0f);
336                 }
337                 
338                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
339                 {
340                         if (rv3d->persp != RV3D_CAMOB)
341                         {
342                                 ketsjiengine->EnableCameraOverride(startscenename);
343                                 ketsjiengine->SetCameraOverrideUseOrtho((rv3d->persp == RV3D_ORTHO));
344                                 ketsjiengine->SetCameraOverrideProjectionMatrix(projmat);
345                                 ketsjiengine->SetCameraOverrideViewMatrix(viewmat);
346                                 ketsjiengine->SetCameraOverrideClipping(v3d->near, v3d->far);
347                                 ketsjiengine->SetCameraOverrideLens(v3d->lens);
348                         }
349                         
350                         // create a scene converter, create and convert the startingscene
351                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(blenderdata, ketsjiengine);
352                         ketsjiengine->SetSceneConverter(sceneconverter);
353                         sceneconverter->addInitFromFrame=false;
354                         if (always_use_expand_framing)
355                                 sceneconverter->SetAlwaysUseExpandFraming(true);
356
357                         bool usemat = false, useglslmat = false;
358
359                         if(GLEW_ARB_multitexture && GLEW_VERSION_1_1)
360                                 usemat = true;
361
362                         if(GPU_glsl_support())
363                                 useglslmat = true;
364                         else if(blscene->gm.matmode == GAME_MAT_GLSL)
365                                 usemat = false;
366
367             if(usemat && (blscene->gm.matmode != GAME_MAT_TEXFACE))
368                                 sceneconverter->SetMaterials(true);
369                         if(useglslmat && (blscene->gm.matmode == GAME_MAT_GLSL))
370                                 sceneconverter->SetGLSLMaterials(true);
371                                         
372                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
373                                 mousedevice,
374                                 networkdevice,
375                                 startscenename,
376                                 blscene,
377                                 canvas);
378
379 #ifndef DISABLE_PYTHON
380                         // some python things
381                         PyObject *gameLogic, *gameLogic_keys;
382                         setupGamePython(ketsjiengine, startscene, blenderdata, pyGlobalDict, &gameLogic, &gameLogic_keys, 0, NULL);
383 #endif // DISABLE_PYTHON
384
385                         //initialize Dome Settings
386                         if(blscene->gm.stereoflag == STEREO_DOME)
387                                 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);
388
389                         // initialize 3D Audio Settings
390                         AUD_set3DSetting(AUD_3DS_SPEED_OF_SOUND, blscene->audio.speed_of_sound);
391                         AUD_set3DSetting(AUD_3DS_DOPPLER_FACTOR, blscene->audio.doppler_factor);
392                         AUD_set3DSetting(AUD_3DS_DISTANCE_MODEL, blscene->audio.distance_model);
393
394                         if (sceneconverter)
395                         {
396                                 // convert and add scene
397                                 sceneconverter->ConvertScene(
398                                         startscene,
399                                         rendertools,
400                                         canvas);
401                                 ketsjiengine->AddScene(startscene);
402                                 
403                                 // init the rasterizer
404                                 rasterizer->Init();
405                                 
406                                 // start the engine
407                                 ketsjiengine->StartEngine(true);
408                                 
409
410                                 // Set the animation playback rate for ipo's and actions
411                                 // the framerate below should patch with FPS macro defined in blendef.h
412                                 // Could be in StartEngine set the framerate, we need the scene to do this
413                                 ketsjiengine->SetAnimFrameRate(FPS);
414                                 
415                                 // the mainloop
416                                 printf("\nBlender Game Engine Started\n\n");
417                                 while (!exitrequested)
418                                 {
419                                         // first check if we want to exit
420                                         exitrequested = ketsjiengine->GetExitCode();
421                                         
422                                         // kick the engine
423                                         bool render = ketsjiengine->NextFrame(); // XXX 2.5 Bug, This is never true! FIXME-  Campbell
424                                         
425                                         if (render)
426                                         {
427                                                 // render the frame
428                                                 ketsjiengine->Render();
429                                         }
430                                         
431                                         wm_window_process_events_nosleep(C);
432                                         
433                                         // test for the ESC key
434                                         //XXX while (qtest())
435                                         while(wmEvent *event= (wmEvent *)win->queue.first)
436                                         {
437                                                 short val = 0;
438                                                 //unsigned short event = 0; //XXX extern_qread(&val);
439                                                 
440                                                 if (keyboarddevice->ConvertBlenderEvent(event->type,event->val))
441                                                         exitrequested = KX_EXIT_REQUEST_BLENDER_ESC;
442                                                 
443                                                         /* Coordinate conversion... where
444                                                         * should this really be?
445                                                 */
446                                                 if (event->type==MOUSEMOVE) {
447                                                         /* Note, not nice! XXX 2.5 event hack */
448                                                         val = event->x - ar->winrct.xmin;
449                                                         mousedevice->ConvertBlenderEvent(MOUSEX, val);
450                                                         
451                                                         val = ar->winy - (event->y - ar->winrct.ymin) - 1;
452                                                         mousedevice->ConvertBlenderEvent(MOUSEY, val);
453                                                 }
454                                                 else {
455                                                         mousedevice->ConvertBlenderEvent(event->type,event->val);
456                                                 }
457                                                 
458                                                 BLI_remlink(&win->queue, event);
459                                                 wm_event_free(event);
460                                         }
461                                         
462                                 }
463                                 printf("\nBlender Game Engine Finished\n\n");
464                                 exitstring = ketsjiengine->GetExitString();
465
466
467                                 // when exiting the mainloop
468 #ifndef DISABLE_PYTHON
469                                 // Clears the dictionary by hand:
470                                 // This prevents, extra references to global variables
471                                 // inside the GameLogic dictionary when the python interpreter is finalized.
472                                 // which allows the scene to safely delete them :)
473                                 // see: (space.c)->start_game
474                                 
475                                 //PyDict_Clear(PyModule_GetDict(gameLogic));
476                                 
477                                 // Keep original items, means python plugins will autocomplete members
478                                 int listIndex;
479                                 PyObject *gameLogic_keys_new = PyDict_Keys(PyModule_GetDict(gameLogic));
480                                 for (listIndex=0; listIndex < PyList_Size(gameLogic_keys_new); listIndex++)  {
481                                         PyObject* item = PyList_GET_ITEM(gameLogic_keys_new, listIndex);
482                                         if (!PySequence_Contains(gameLogic_keys, item)) {
483                                                 PyDict_DelItem( PyModule_GetDict(gameLogic), item);
484                                         }
485                                 }
486                                 Py_DECREF(gameLogic_keys_new);
487                                 gameLogic_keys_new = NULL;
488 #endif
489                                 ketsjiengine->StopEngine();
490 #ifndef DISABLE_PYTHON
491                                 exitGamePythonScripting();
492 #endif
493                                 networkdevice->Disconnect();
494                         }
495                         if (sceneconverter)
496                         {
497                                 delete sceneconverter;
498                                 sceneconverter = NULL;
499                         }
500
501 #ifndef DISABLE_PYTHON
502                         Py_DECREF(gameLogic_keys);
503                         gameLogic_keys = NULL;
504 #endif
505                 }
506                 //lock frame and camera enabled - restoring global values
507                 if (v3d->scenelock==0){
508                         scene->lay= tmp_lay;
509                         scene->camera= tmp_camera;
510                 }
511
512                 // set the cursor back to normal
513                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
514                 
515                 // clean up some stuff
516                 if (ketsjiengine)
517                 {
518                         delete ketsjiengine;
519                         ketsjiengine = NULL;
520                 }
521                 if (kxsystem)
522                 {
523                         delete kxsystem;
524                         kxsystem = NULL;
525                 }
526                 if (networkdevice)
527                 {
528                         delete networkdevice;
529                         networkdevice = NULL;
530                 }
531                 if (keyboarddevice)
532                 {
533                         delete keyboarddevice;
534                         keyboarddevice = NULL;
535                 }
536                 if (mousedevice)
537                 {
538                         delete mousedevice;
539                         mousedevice = NULL;
540                 }
541                 if (rasterizer)
542                 {
543                         delete rasterizer;
544                         rasterizer = NULL;
545                 }
546                 if (rendertools)
547                 {
548                         delete rendertools;
549                         rendertools = NULL;
550                 }
551                 if (canvas)
552                 {
553                         delete canvas;
554                         canvas = NULL;
555                 }
556         
557         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
558         
559         if (bfd) BLO_blendfiledata_free(bfd);
560
561         BLI_strncpy(G.sce, oldsce, sizeof(G.sce));
562
563 #ifndef DISABLE_PYTHON
564         Py_DECREF(pyGlobalDict);
565
566         // Release Python's GIL
567         PyGILState_Release(gilstate);
568 #endif
569
570 }