BGE: option in the UI to start with the mouse cursor visible. Patch by Vitor Balbio...
[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 #if defined(WIN32) && !defined(FREE_WINDOWS)
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_userdef_types.h"
76 #include "DNA_windowmanager_types.h"
77 #include "BKE_global.h"
78 #include "BKE_report.h"
79
80
81 //XXX #include "BIF_screen.h"
82 //XXX #include "BIF_scrarea.h"
83
84 #include "BKE_main.h"
85 #include "BLI_blenlib.h"
86 #include "BLO_readfile.h"
87 #include "DNA_scene_types.h"
88 #include "BKE_ipo.h"
89         /***/
90
91 #include "AUD_C-API.h"
92
93 //XXX #include "BSE_headerbuttons.h"
94 #include "BKE_context.h"
95 #include "../../blender/windowmanager/WM_types.h"
96 #include "../../blender/windowmanager/wm_window.h"
97 #include "../../blender/windowmanager/wm_event_system.h"
98 #ifdef __cplusplus
99 }
100 #endif
101
102
103 static BlendFileData *load_game_data(char *filename)
104 {
105         ReportList reports;
106         BlendFileData *bfd;
107         
108         BKE_reports_init(&reports, RPT_STORE);
109         bfd= BLO_read_from_file(filename, &reports);
110
111         if (!bfd) {
112                 printf("Loading %s failed: ", filename);
113                 BKE_reports_print(&reports, RPT_ERROR);
114         }
115
116         BKE_reports_clear(&reports);
117
118         return bfd;
119 }
120
121 extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *cam_frame, int always_use_expand_framing)
122 {
123         /* context values */
124         struct wmWindow *win= CTX_wm_window(C);
125         struct Scene *startscene= CTX_data_scene(C);
126         struct Main* maggie1= CTX_data_main(C);
127
128
129         RAS_Rect area_rect;
130         area_rect.SetLeft(cam_frame->xmin);
131         area_rect.SetBottom(cam_frame->ymin);
132         area_rect.SetRight(cam_frame->xmax);
133         area_rect.SetTop(cam_frame->ymax);
134
135         int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
136         Main* blenderdata = maggie1;
137
138         char* startscenename = startscene->id.name+2;
139         char pathname[FILE_MAXDIR+FILE_MAXFILE], oldsce[FILE_MAXDIR+FILE_MAXFILE];
140         STR_String exitstring = "";
141         BlendFileData *bfd= NULL;
142
143         BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
144         BLI_strncpy(oldsce, G.main->name, sizeof(oldsce));
145 #ifdef WITH_PYTHON
146         resetGamePythonPath(); // need this so running a second time wont use an old blendfiles path
147         setGamePythonPath(G.main->name);
148
149         // Acquire Python's GIL (global interpreter lock)
150         // so we can safely run Python code and API calls
151         PyGILState_STATE gilstate = PyGILState_Ensure();
152         
153         PyObject *pyGlobalDict = PyDict_New(); /* python utility storage, spans blend file loading */
154 #endif
155         
156         bgl::InitExtensions(true);
157
158         // VBO code for derived mesh is not compatible with BGE (couldn't find why), so disable
159         int disableVBO = (U.gameflags & USER_DISABLE_VBO);
160         U.gameflags |= USER_DISABLE_VBO;
161
162         do
163         {
164                 View3D *v3d= CTX_wm_view3d(C);
165                 RegionView3D *rv3d= CTX_wm_region_view3d(C);
166
167                 // get some preferences
168                 SYS_SystemHandle syshandle = SYS_GetSystem();
169                 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
170                 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
171                 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
172                 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
173                 bool animation_record = (SYS_GetCommandLineInt(syshandle, "animation_record", 0) != 0);
174                 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
175 #ifdef WITH_PYTHON
176                 bool nodepwarnings = (SYS_GetCommandLineInt(syshandle, "ignore_deprecation_warnings", 0) != 0);
177 #endif
178                 bool novertexarrays = (SYS_GetCommandLineInt(syshandle, "novertexarrays", 0) != 0);
179                 bool mouse_state = startscene->gm.flag & GAME_SHOW_MOUSE;
180
181                 if(animation_record) usefixed= true; /* override since you's always want fixed time for sim recording */
182
183                 // create the canvas, rasterizer and rendertools
184                 RAS_ICanvas* canvas = new KX_BlenderCanvas(win, area_rect, ar);
185                 
186                 // default mouse state set on render panel
187                 if (mouse_state)
188                         canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
189                 else
190                         canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
191                 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
192                 RAS_IRasterizer* rasterizer = NULL;
193                 
194                 if(displaylists) {
195                         if (GLEW_VERSION_1_1 && !novertexarrays)
196                                 rasterizer = new RAS_ListRasterizer(canvas, true, true);
197                         else
198                                 rasterizer = new RAS_ListRasterizer(canvas);
199                 }
200                 else if (GLEW_VERSION_1_1 && !novertexarrays)
201                         rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
202                 else
203                         rasterizer = new RAS_OpenGLRasterizer(canvas);
204                 
205                 // create the inputdevices
206                 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
207                 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
208                 
209                 // create a networkdevice
210                 NG_NetworkDeviceInterface* networkdevice = new
211                         NG_LoopBackNetworkDeviceInterface();
212
213                 //
214                 // create a ketsji/blendersystem (only needed for timing and stuff)
215                 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
216                 
217                 // create the ketsjiengine
218                 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
219                 
220                 // set the devices
221                 ketsjiengine->SetKeyboardDevice(keyboarddevice);
222                 ketsjiengine->SetMouseDevice(mousedevice);
223                 ketsjiengine->SetNetworkDevice(networkdevice);
224                 ketsjiengine->SetCanvas(canvas);
225                 ketsjiengine->SetRenderTools(rendertools);
226                 ketsjiengine->SetRasterizer(rasterizer);
227                 ketsjiengine->SetNetworkDevice(networkdevice);
228                 ketsjiengine->SetUseFixedTime(usefixed);
229                 ketsjiengine->SetTimingDisplay(frameRate, profile, properties);
230
231 #ifdef WITH_PYTHON
232                 CValue::SetDeprecationWarnings(nodepwarnings);
233 #endif
234
235                 //lock frame and camera enabled - storing global values
236                 int tmp_lay= startscene->lay;
237                 Object *tmp_camera = startscene->camera;
238
239                 if (v3d->scenelock==0){
240                         startscene->lay= v3d->lay;
241                         startscene->camera= v3d->camera;
242                 }
243
244                 // some blender stuff
245                 float camzoom;
246                 
247                 if(rv3d->persp==RV3D_CAMOB) {
248                         if(startscene->gm.framing.type == SCE_GAMEFRAMING_BARS) { /* Letterbox */
249                                 camzoom = 1.0f;
250                         }
251                         else {
252                                 camzoom = (1.41421 + (rv3d->camzoom / 50.0));
253                                 camzoom *= camzoom;
254                                 camzoom = 4.0 / camzoom;
255                         }
256                 }
257                 else {
258                         camzoom = 2.0;
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.main->name, blenderdata->name, sizeof(G.main->name));
305                                         BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
306 #ifdef WITH_PYTHON
307                                         setGamePythonPath(G.main->name);
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 *scene= bfd ? bfd->curscene : (Scene *)BLI_findstring(&blenderdata->scene, startscenename, offsetof(ID, name) + 2);
319
320                 if (scene)
321                 {
322                         int startFrame = scene->r.cfra;
323                         ketsjiengine->SetAnimRecordMode(animation_record, startFrame);
324                         
325                         // Quad buffered needs a special window.
326                         if(scene->gm.stereoflag == STEREO_ENABLED){
327                                 if (scene->gm.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
328                                         rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) scene->gm.stereomode);
329
330                                 rasterizer->SetEyeSeparation(scene->gm.eyeseparation);
331                         }
332
333                         rasterizer->SetBackColor(scene->gm.framing.col[0], scene->gm.framing.col[1], scene->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(MT_CmMatrix4x4(rv3d->winmat));
343                                 ketsjiengine->SetCameraOverrideViewMatrix(MT_CmMatrix4x4(rv3d->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(scene->gm.matmode == GAME_MAT_GLSL)
363                                 usemat = false;
364
365                         if(usemat && (scene->gm.matmode != GAME_MAT_TEXFACE))
366                                 sceneconverter->SetMaterials(true);
367                         if(useglslmat && (scene->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                                 scene,
375                                 canvas);
376
377 #ifdef WITH_PYTHON
378                         // some python things
379                         PyObject *gameLogic, *gameLogic_keys;
380                         setupGamePython(ketsjiengine, startscene, blenderdata, pyGlobalDict, &gameLogic, &gameLogic_keys, 0, NULL);
381 #endif // WITH_PYTHON
382
383                         //initialize Dome Settings
384                         if(scene->gm.stereoflag == STEREO_DOME)
385                                 ketsjiengine->InitDome(scene->gm.dome.res, scene->gm.dome.mode, scene->gm.dome.angle, scene->gm.dome.resbuf, scene->gm.dome.tilt, scene->gm.dome.warptext);
386
387                         // initialize 3D Audio Settings
388                         AUD_setSpeedOfSound(scene->audio.speed_of_sound);
389                         AUD_setDopplerFactor(scene->audio.doppler_factor);
390                         AUD_setDistanceModel(AUD_DistanceModel(scene->audio.distance_model));
391
392                         // from see blender.c:
393                         // FIXME: this version patching should really be part of the file-reading code,
394                         // but we still get too many unrelated data-corruption crashes otherwise...
395                         if (blenderdata->versionfile < 250)
396                                 do_versions_ipos_to_animato(blenderdata);
397
398                         if (sceneconverter)
399                         {
400                                 // convert and add scene
401                                 sceneconverter->ConvertScene(
402                                         startscene,
403                                         rendertools,
404                                         canvas);
405                                 ketsjiengine->AddScene(startscene);
406                                 
407                                 // init the rasterizer
408                                 rasterizer->Init();
409                                 
410                                 // start the engine
411                                 ketsjiengine->StartEngine(true);
412                                 
413
414                                 // Set the animation playback rate for ipo's and actions
415                                 // the framerate below should patch with FPS macro defined in blendef.h
416                                 // Could be in StartEngine set the framerate, we need the scene to do this
417                                 ketsjiengine->SetAnimFrameRate(FPS);
418                                 
419                                 // the mainloop
420                                 printf("\nBlender Game Engine Started\n");
421                                 while (!exitrequested)
422                                 {
423                                         // first check if we want to exit
424                                         exitrequested = ketsjiengine->GetExitCode();
425                                         
426                                         // kick the engine
427                                         bool render = ketsjiengine->NextFrame();
428                                         
429                                         if (render)
430                                         {
431                                                 // render the frame
432                                                 ketsjiengine->Render();
433                                         }
434                                         
435                                         wm_window_process_events_nosleep();
436                                         
437                                         // test for the ESC key
438                                         //XXX while (qtest())
439                                         while(wmEvent *event= (wmEvent *)win->queue.first)
440                                         {
441                                                 short val = 0;
442                                                 //unsigned short event = 0; //XXX extern_qread(&val);
443                                                 
444                                                 if (keyboarddevice->ConvertBlenderEvent(event->type,event->val))
445                                                         exitrequested = KX_EXIT_REQUEST_BLENDER_ESC;
446                                                 
447                                                         /* Coordinate conversion... where
448                                                         * should this really be?
449                                                 */
450                                                 if (event->type==MOUSEMOVE) {
451                                                         /* Note, not nice! XXX 2.5 event hack */
452                                                         val = event->x - ar->winrct.xmin;
453                                                         mousedevice->ConvertBlenderEvent(MOUSEX, val);
454                                                         
455                                                         val = ar->winy - (event->y - ar->winrct.ymin) - 1;
456                                                         mousedevice->ConvertBlenderEvent(MOUSEY, val);
457                                                 }
458                                                 else {
459                                                         mousedevice->ConvertBlenderEvent(event->type,event->val);
460                                                 }
461                                                 
462                                                 BLI_remlink(&win->queue, event);
463                                                 wm_event_free(event);
464                                         }
465                                         
466                                         if(win != CTX_wm_window(C)) {
467                                                 exitrequested= KX_EXIT_REQUEST_OUTSIDE; /* window closed while bge runs */
468                                         }
469                                 }
470                                 printf("Blender Game Engine Finished\n");
471                                 exitstring = ketsjiengine->GetExitString();
472
473
474                                 // when exiting the mainloop
475 #ifdef WITH_PYTHON
476                                 // Clears the dictionary by hand:
477                                 // This prevents, extra references to global variables
478                                 // inside the GameLogic dictionary when the python interpreter is finalized.
479                                 // which allows the scene to safely delete them :)
480                                 // see: (space.c)->start_game
481                                 
482                                 //PyDict_Clear(PyModule_GetDict(gameLogic));
483                                 
484                                 // Keep original items, means python plugins will autocomplete members
485                                 int listIndex;
486                                 PyObject *gameLogic_keys_new = PyDict_Keys(PyModule_GetDict(gameLogic));
487                                 for (listIndex=0; listIndex < PyList_Size(gameLogic_keys_new); listIndex++)  {
488                                         PyObject* item = PyList_GET_ITEM(gameLogic_keys_new, listIndex);
489                                         if (!PySequence_Contains(gameLogic_keys, item)) {
490                                                 PyDict_DelItem( PyModule_GetDict(gameLogic), item);
491                                         }
492                                 }
493                                 Py_DECREF(gameLogic_keys_new);
494                                 gameLogic_keys_new = NULL;
495 #endif
496                                 ketsjiengine->StopEngine();
497 #ifdef WITH_PYTHON
498                                 exitGamePythonScripting();
499 #endif
500                                 networkdevice->Disconnect();
501                         }
502                         if (sceneconverter)
503                         {
504                                 delete sceneconverter;
505                                 sceneconverter = NULL;
506                         }
507
508 #ifdef WITH_PYTHON
509                         Py_DECREF(gameLogic_keys);
510                         gameLogic_keys = NULL;
511 #endif
512                 }
513                 //lock frame and camera enabled - restoring global values
514                 if (v3d->scenelock==0){
515                         startscene->lay= tmp_lay;
516                         startscene->camera= tmp_camera;
517                 }
518
519                 if(exitrequested != KX_EXIT_REQUEST_OUTSIDE)
520                 {
521                         // set the cursor back to normal
522                         canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
523                 }
524                 
525                 // clean up some stuff
526                 if (ketsjiengine)
527                 {
528                         delete ketsjiengine;
529                         ketsjiengine = NULL;
530                 }
531                 if (kxsystem)
532                 {
533                         delete kxsystem;
534                         kxsystem = NULL;
535                 }
536                 if (networkdevice)
537                 {
538                         delete networkdevice;
539                         networkdevice = NULL;
540                 }
541                 if (keyboarddevice)
542                 {
543                         delete keyboarddevice;
544                         keyboarddevice = NULL;
545                 }
546                 if (mousedevice)
547                 {
548                         delete mousedevice;
549                         mousedevice = NULL;
550                 }
551                 if (rasterizer)
552                 {
553                         delete rasterizer;
554                         rasterizer = NULL;
555                 }
556                 if (rendertools)
557                 {
558                         delete rendertools;
559                         rendertools = NULL;
560                 }
561                 if (canvas)
562                 {
563                         delete canvas;
564                         canvas = NULL;
565                 }
566         
567         } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
568         
569         if (!disableVBO)
570                 U.gameflags &= ~USER_DISABLE_VBO;
571
572         if (bfd) BLO_blendfiledata_free(bfd);
573
574         BLI_strncpy(G.main->name, oldsce, sizeof(G.main->name));
575
576 #ifdef WITH_PYTHON
577         Py_DECREF(pyGlobalDict);
578
579         // Release Python's GIL
580         PyGILState_Release(gilstate);
581 #endif
582
583 }