remove config.h references, was added for automake build system rev around 124-126...
[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                 bool nodepwarnings = (SYS_GetCommandLineInt(syshandle, "ignore_deprecation_warnings", 0) != 0);
170                 bool novertexarrays = (SYS_GetCommandLineInt(syshandle, "novertexarrays", 0) != 0);
171                 if(animation_record) usefixed= true; /* override since you's always want fixed time for sim recording */
172
173                 // create the canvas, rasterizer and rendertools
174                 RAS_ICanvas* canvas = new KX_BlenderCanvas(win, area_rect);
175                 canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
176                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
177                 RAS_IRasterizer* rasterizer = NULL;
178                 
179                 if(displaylists) {
180                         if (GLEW_VERSION_1_1 && !novertexarrays)
181                                 rasterizer = new RAS_ListRasterizer(canvas, true, true);
182                         else
183                                 rasterizer = new RAS_ListRasterizer(canvas);
184                 }
185                 else if (GLEW_VERSION_1_1 && !novertexarrays)
186                         rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
187                 else
188                         rasterizer = new RAS_OpenGLRasterizer(canvas);
189                 
190                 // create the inputdevices
191                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
192                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
193                 
194                 // create a networkdevice
195                 NG_NetworkDeviceInterface* networkdevice = new
196                         NG_LoopBackNetworkDeviceInterface();
197
198                 //
199                 // create a ketsji/blendersystem (only needed for timing and stuff)
200                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
201                 
202                 // create the ketsjiengine
203                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
204                 
205                 // set the devices
206                 ketsjiengine->SetKeyboardDevice(keyboarddevice);
207                 ketsjiengine->SetMouseDevice(mousedevice);
208                 ketsjiengine->SetNetworkDevice(networkdevice);
209                 ketsjiengine->SetCanvas(canvas);
210                 ketsjiengine->SetRenderTools(rendertools);
211                 ketsjiengine->SetRasterizer(rasterizer);
212                 ketsjiengine->SetNetworkDevice(networkdevice);
213                 ketsjiengine->SetUseFixedTime(usefixed);
214                 ketsjiengine->SetTimingDisplay(frameRate, profile, properties);
215
216 #ifndef DISABLE_PYTHON
217                 CValue::SetDeprecationWarnings(nodepwarnings);
218 #endif
219
220                 //lock frame and camera enabled - storing global values
221                 int tmp_lay= scene->lay;
222                 Object *tmp_camera = scene->camera;
223
224                 if (v3d->scenelock==0){
225                         scene->lay= v3d->lay;
226                         scene->camera= v3d->camera;
227                 }
228
229                 // some blender stuff
230                 MT_CmMatrix4x4 projmat;
231                 MT_CmMatrix4x4 viewmat;
232                 float camzoom;
233                 int i;
234
235                 for (i = 0; i < 16; i++)
236                 {
237                         float *viewmat_linear= (float*) rv3d->viewmat;
238                         viewmat.setElem(i, viewmat_linear[i]);
239                 }
240                 for (i = 0; i < 16; i++)
241                 {
242                         float *projmat_linear= (float*) rv3d->winmat;
243                         projmat.setElem(i, projmat_linear[i]);
244                 }
245                 
246                 if(rv3d->persp==RV3D_CAMOB) {
247                         if(scene->gm.framing.type == SCE_GAMEFRAMING_BARS) { /* Letterbox */
248                                 camzoom = 1.0f;
249                         }
250                         else {
251                                 camzoom = (1.41421 + (rv3d->camzoom / 50.0));
252                                 camzoom *= camzoom;
253                                 camzoom = 4.0 / camzoom;
254                         }
255                 }
256                 else {
257                         camzoom = 2.0;
258                 }
259
260                 
261
262                 ketsjiengine->SetDrawType(v3d->drawtype);
263                 ketsjiengine->SetCameraZoom(camzoom);
264                 
265                 // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
266                 if (exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME || exitrequested == KX_EXIT_REQUEST_RESTART_GAME)
267                 {
268                         exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
269                         if (bfd) BLO_blendfiledata_free(bfd);
270                         
271                         char basedpath[240];
272                         // base the actuator filename with respect
273                         // to the original file working directory
274
275                         if (exitstring != "")
276                                 strcpy(basedpath, exitstring.Ptr());
277
278                         // load relative to the last loaded file, this used to be relative
279                         // to the first file but that makes no sense, relative paths in
280                         // blend files should be relative to that file, not some other file
281                         // that happened to be loaded first
282                         BLI_path_abs(basedpath, pathname);
283                         bfd = load_game_data(basedpath);
284                         
285                         // if it wasn't loaded, try it forced relative
286                         if (!bfd)
287                         {
288                                 // just add "//" in front of it
289                                 char temppath[242];
290                                 strcpy(temppath, "//");
291                                 strcat(temppath, basedpath);
292                                 
293                                 BLI_path_abs(temppath, pathname);
294                                 bfd = load_game_data(temppath);
295                         }
296                         
297                         // if we got a loaded blendfile, proceed
298                         if (bfd)
299                         {
300                                 blenderdata = bfd->main;
301                                 startscenename = bfd->curscene->id.name + 2;
302
303                                 if(blenderdata) {
304                                         BLI_strncpy(G.sce, blenderdata->name, sizeof(G.sce));
305                                         BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
306 #ifndef DISABLE_PYTHON
307                                         setGamePythonPath(G.sce);
308 #endif
309                                 }
310                         }
311                         // else forget it, we can't find it
312                         else
313                         {
314                                 exitrequested = KX_EXIT_REQUEST_QUIT_GAME;
315                         }
316                 }
317
318                 Scene *blscene= bfd ? bfd->curscene : (Scene *)BLI_findstring(&blenderdata->scene, startscenename, offsetof(ID, name) + 2);
319
320                 if (blscene)
321                 {
322                         int startFrame = blscene->r.cfra;
323                         ketsjiengine->SetAnimRecordMode(animation_record, startFrame);
324                         
325                         // Quad buffered needs a special window.
326                         if(blscene->gm.stereoflag == STEREO_ENABLED){
327                                 if (blscene->gm.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
328                                         rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->gm.stereomode);
329
330                                 rasterizer->SetEyeSeparation(blscene->gm.eyeseparation);
331                         }
332
333                         rasterizer->SetBackColor(blscene->gm.framing.col[0], blscene->gm.framing.col[1], blscene->gm.framing.col[2], 0.0f);
334                 }
335                 
336                 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
337                 {
338                         if (rv3d->persp != RV3D_CAMOB)
339                         {
340                                 ketsjiengine->EnableCameraOverride(startscenename);
341                                 ketsjiengine->SetCameraOverrideUseOrtho((rv3d->persp == RV3D_ORTHO));
342                                 ketsjiengine->SetCameraOverrideProjectionMatrix(projmat);
343                                 ketsjiengine->SetCameraOverrideViewMatrix(viewmat);
344                                 ketsjiengine->SetCameraOverrideClipping(v3d->near, v3d->far);
345                                 ketsjiengine->SetCameraOverrideLens(v3d->lens);
346                         }
347                         
348                         // create a scene converter, create and convert the startingscene
349                         KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(blenderdata, ketsjiengine);
350                         ketsjiengine->SetSceneConverter(sceneconverter);
351                         sceneconverter->addInitFromFrame=false;
352                         if (always_use_expand_framing)
353                                 sceneconverter->SetAlwaysUseExpandFraming(true);
354
355                         bool usemat = false, useglslmat = false;
356
357                         if(GLEW_ARB_multitexture && GLEW_VERSION_1_1)
358                                 usemat = true;
359
360                         if(GPU_glsl_support())
361                                 useglslmat = true;
362                         else if(blscene->gm.matmode == GAME_MAT_GLSL)
363                                 usemat = false;
364
365             if(usemat && (blscene->gm.matmode != GAME_MAT_TEXFACE))
366                                 sceneconverter->SetMaterials(true);
367                         if(useglslmat && (blscene->gm.matmode == GAME_MAT_GLSL))
368                                 sceneconverter->SetGLSLMaterials(true);
369                                         
370                         KX_Scene* startscene = new KX_Scene(keyboarddevice,
371                                 mousedevice,
372                                 networkdevice,
373                                 startscenename,
374                                 blscene);
375
376 #ifndef DISABLE_PYTHON
377                         // some python things
378                         PyObject *gameLogic, *gameLogic_keys;
379                         setupGamePython(ketsjiengine, startscene, blenderdata, pyGlobalDict, &gameLogic, &gameLogic_keys, 0, NULL);
380 #endif // DISABLE_PYTHON
381
382                         //initialize Dome Settings
383                         if(blscene->gm.stereoflag == STEREO_DOME)
384                                 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);
385
386                         // initialize 3D Audio Settings
387                         AUD_set3DSetting(AUD_3DS_SPEED_OF_SOUND, blscene->audio.speed_of_sound);
388                         AUD_set3DSetting(AUD_3DS_DOPPLER_FACTOR, blscene->audio.doppler_factor);
389                         AUD_set3DSetting(AUD_3DS_DISTANCE_MODEL, blscene->audio.distance_model);
390
391                         if (sceneconverter)
392                         {
393                                 // convert and add scene
394                                 sceneconverter->ConvertScene(
395                                         startscene,
396                                         rendertools,
397                                         canvas);
398                                 ketsjiengine->AddScene(startscene);
399                                 
400                                 // init the rasterizer
401                                 rasterizer->Init();
402                                 
403                                 // start the engine
404                                 ketsjiengine->StartEngine(true);
405                                 
406
407                                 // Set the animation playback rate for ipo's and actions
408                                 // the framerate below should patch with FPS macro defined in blendef.h
409                                 // Could be in StartEngine set the framerate, we need the scene to do this
410                                 ketsjiengine->SetAnimFrameRate(FPS);
411                                 
412                                 // the mainloop
413                                 printf("\nBlender Game Engine Started\n\n");
414                                 while (!exitrequested)
415                                 {
416                                         // first check if we want to exit
417                                         exitrequested = ketsjiengine->GetExitCode();
418                                         
419                                         // kick the engine
420                                         bool render = ketsjiengine->NextFrame(); // XXX 2.5 Bug, This is never true! FIXME-  Campbell
421                                         
422                                         if (render)
423                                         {
424                                                 // render the frame
425                                                 ketsjiengine->Render();
426                                         }
427                                         
428                                         wm_window_process_events_nosleep(C);
429                                         
430                                         // test for the ESC key
431                                         //XXX while (qtest())
432                                         while(wmEvent *event= (wmEvent *)win->queue.first)
433                                         {
434                                                 short val = 0;
435                                                 //unsigned short event = 0; //XXX extern_qread(&val);
436                                                 
437                                                 if (keyboarddevice->ConvertBlenderEvent(event->type,event->val))
438                                                         exitrequested = KX_EXIT_REQUEST_BLENDER_ESC;
439                                                 
440                                                         /* Coordinate conversion... where
441                                                         * should this really be?
442                                                 */
443                                                 if (event->type==MOUSEMOVE) {
444                                                         /* Note, not nice! XXX 2.5 event hack */
445                                                         val = event->x - ar->winrct.xmin;
446                                                         mousedevice->ConvertBlenderEvent(MOUSEX, val);
447                                                         
448                                                         val = ar->winy - (event->y - ar->winrct.ymin) - 1;
449                                                         mousedevice->ConvertBlenderEvent(MOUSEY, val);
450                                                 }
451                                                 else {
452                                                         mousedevice->ConvertBlenderEvent(event->type,event->val);
453                                                 }
454                                                 
455                                                 BLI_remlink(&win->queue, event);
456                                                 wm_event_free(event);
457                                         }
458                                         
459                                 }
460                                 printf("\nBlender Game Engine Finished\n\n");
461                                 exitstring = ketsjiengine->GetExitString();
462
463
464                                 // when exiting the mainloop
465 #ifndef DISABLE_PYTHON
466                                 // Clears the dictionary by hand:
467                                 // This prevents, extra references to global variables
468                                 // inside the GameLogic dictionary when the python interpreter is finalized.
469                                 // which allows the scene to safely delete them :)
470                                 // see: (space.c)->start_game
471                                 
472                                 //PyDict_Clear(PyModule_GetDict(gameLogic));
473                                 
474                                 // Keep original items, means python plugins will autocomplete members
475                                 int listIndex;
476                                 PyObject *gameLogic_keys_new = PyDict_Keys(PyModule_GetDict(gameLogic));
477                                 for (listIndex=0; listIndex < PyList_Size(gameLogic_keys_new); listIndex++)  {
478                                         PyObject* item = PyList_GET_ITEM(gameLogic_keys_new, listIndex);
479                                         if (!PySequence_Contains(gameLogic_keys, item)) {
480                                                 PyDict_DelItem( PyModule_GetDict(gameLogic), item);
481                                         }
482                                 }
483                                 Py_DECREF(gameLogic_keys_new);
484                                 gameLogic_keys_new = NULL;
485 #endif
486                                 ketsjiengine->StopEngine();
487 #ifndef DISABLE_PYTHON
488                                 exitGamePythonScripting();
489 #endif
490                                 networkdevice->Disconnect();
491                         }
492                         if (sceneconverter)
493                         {
494                                 delete sceneconverter;
495                                 sceneconverter = NULL;
496                         }
497
498 #ifndef DISABLE_PYTHON
499                         Py_DECREF(gameLogic_keys);
500                         gameLogic_keys = NULL;
501 #endif
502                 }
503                 //lock frame and camera enabled - restoring global values
504                 if (v3d->scenelock==0){
505                         scene->lay= tmp_lay;
506                         scene->camera= tmp_camera;
507                 }
508
509                 // set the cursor back to normal
510                 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
511                 
512                 // clean up some stuff
513                 if (ketsjiengine)
514                 {
515                         delete ketsjiengine;
516                         ketsjiengine = NULL;
517                 }
518                 if (kxsystem)
519                 {
520                         delete kxsystem;
521                         kxsystem = NULL;
522                 }
523                 if (networkdevice)
524                 {
525                         delete networkdevice;
526                         networkdevice = NULL;
527                 }
528                 if (keyboarddevice)
529                 {
530                         delete keyboarddevice;
531                         keyboarddevice = NULL;
532                 }
533                 if (mousedevice)
534                 {
535                         delete mousedevice;
536                         mousedevice = NULL;
537                 }
538                 if (rasterizer)
539                 {
540                         delete rasterizer;
541                         rasterizer = NULL;
542                 }
543                 if (rendertools)
544                 {
545                         delete rendertools;
546                         rendertools = NULL;
547                 }
548                 if (canvas)
549                 {
550                         delete canvas;
551                         canvas = NULL;
552                 }
553         
554         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
555         
556         if (bfd) BLO_blendfiledata_free(bfd);
557
558         BLI_strncpy(G.sce, oldsce, sizeof(G.sce));
559
560 #ifndef DISABLE_PYTHON
561         Py_DECREF(pyGlobalDict);
562
563         // Release Python's GIL
564         PyGILState_Release(gilstate);
565 #endif
566
567 }