4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
28 * Blender's Ketsji startpoint
35 #if defined(WIN32) && !defined(FREE_WINDOWS)
36 // don't show stl-warnings
37 #pragma warning (disable:4786)
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"
50 #include "KX_KetsjiEngine.h"
51 #include "KX_BlenderSceneConverter.h"
52 #include "KX_PythonInit.h"
53 #include "KX_PyConstraintBinding.h"
55 #include "RAS_GLExtensionManager.h"
56 #include "RAS_OpenGLRasterizer.h"
57 #include "RAS_VAOpenGLRasterizer.h"
58 #include "RAS_ListRasterizer.h"
60 #include "NG_LoopBackNetworkDeviceInterface.h"
62 #include "SYS_System.h"
64 #include "GPU_extensions.h"
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"
80 #include "BKE_utildefines.h"
81 //XXX #include "BIF_screen.h"
82 //XXX #include "BIF_scrarea.h"
85 #include "BLI_blenlib.h"
86 #include "BLO_readfile.h"
87 #include "DNA_scene_types.h"
91 #include "AUD_C-API.h"
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"
103 static BlendFileData *load_game_data(char *filename)
108 BKE_reports_init(&reports, RPT_STORE);
109 bfd= BLO_read_from_file(filename, &reports);
112 printf("Loading %s failed: ", filename);
113 BKE_reports_print(&reports, RPT_ERROR);
116 BKE_reports_clear(&reports);
121 extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *cam_frame, int always_use_expand_framing)
124 struct wmWindow *win= CTX_wm_window(C);
125 struct Scene *startscene= CTX_data_scene(C);
126 struct Main* maggie1= CTX_data_main(C);
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);
135 int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
136 Main* blenderdata = maggie1;
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;
143 BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
144 BLI_strncpy(oldsce, G.sce, sizeof(oldsce));
145 #ifndef DISABLE_PYTHON
146 resetGamePythonPath(); // need this so running a second time wont use an old blendfiles path
147 setGamePythonPath(G.sce);
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();
153 PyObject *pyGlobalDict = PyDict_New(); /* python utility storage, spans blend file loading */
156 bgl::InitExtensions(true);
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;
164 View3D *v3d= CTX_wm_view3d(C);
165 RegionView3D *rv3d= CTX_wm_region_view3d(C);
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 #ifndef DISABLE_PYTHON
176 bool nodepwarnings = (SYS_GetCommandLineInt(syshandle, "ignore_deprecation_warnings", 0) != 0);
178 bool novertexarrays = (SYS_GetCommandLineInt(syshandle, "novertexarrays", 0) != 0);
179 if(animation_record) usefixed= true; /* override since you's always want fixed time for sim recording */
181 // create the canvas, rasterizer and rendertools
182 RAS_ICanvas* canvas = new KX_BlenderCanvas(win, area_rect, ar);
183 canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
184 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
185 RAS_IRasterizer* rasterizer = NULL;
188 if (GLEW_VERSION_1_1 && !novertexarrays)
189 rasterizer = new RAS_ListRasterizer(canvas, true, true);
191 rasterizer = new RAS_ListRasterizer(canvas);
193 else if (GLEW_VERSION_1_1 && !novertexarrays)
194 rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
196 rasterizer = new RAS_OpenGLRasterizer(canvas);
198 // create the inputdevices
199 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
200 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
202 // create a networkdevice
203 NG_NetworkDeviceInterface* networkdevice = new
204 NG_LoopBackNetworkDeviceInterface();
207 // create a ketsji/blendersystem (only needed for timing and stuff)
208 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
210 // create the ketsjiengine
211 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
214 ketsjiengine->SetKeyboardDevice(keyboarddevice);
215 ketsjiengine->SetMouseDevice(mousedevice);
216 ketsjiengine->SetNetworkDevice(networkdevice);
217 ketsjiengine->SetCanvas(canvas);
218 ketsjiengine->SetRenderTools(rendertools);
219 ketsjiengine->SetRasterizer(rasterizer);
220 ketsjiengine->SetNetworkDevice(networkdevice);
221 ketsjiengine->SetUseFixedTime(usefixed);
222 ketsjiengine->SetTimingDisplay(frameRate, profile, properties);
224 #ifndef DISABLE_PYTHON
225 CValue::SetDeprecationWarnings(nodepwarnings);
228 //lock frame and camera enabled - storing global values
229 int tmp_lay= startscene->lay;
230 Object *tmp_camera = startscene->camera;
232 if (v3d->scenelock==0){
233 startscene->lay= v3d->lay;
234 startscene->camera= v3d->camera;
237 // some blender stuff
238 MT_CmMatrix4x4 projmat;
239 MT_CmMatrix4x4 viewmat;
243 for (i = 0; i < 16; i++)
245 float *viewmat_linear= (float*) rv3d->viewmat;
246 viewmat.setElem(i, viewmat_linear[i]);
248 for (i = 0; i < 16; i++)
250 float *projmat_linear= (float*) rv3d->winmat;
251 projmat.setElem(i, projmat_linear[i]);
254 if(rv3d->persp==RV3D_CAMOB) {
255 if(startscene->gm.framing.type == SCE_GAMEFRAMING_BARS) { /* Letterbox */
259 camzoom = (1.41421 + (rv3d->camzoom / 50.0));
261 camzoom = 4.0 / camzoom;
270 ketsjiengine->SetDrawType(v3d->drawtype);
271 ketsjiengine->SetCameraZoom(camzoom);
273 // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
274 if (exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME || exitrequested == KX_EXIT_REQUEST_RESTART_GAME)
276 exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
277 if (bfd) BLO_blendfiledata_free(bfd);
280 // base the actuator filename with respect
281 // to the original file working directory
283 if (exitstring != "")
284 strcpy(basedpath, exitstring.Ptr());
286 // load relative to the last loaded file, this used to be relative
287 // to the first file but that makes no sense, relative paths in
288 // blend files should be relative to that file, not some other file
289 // that happened to be loaded first
290 BLI_path_abs(basedpath, pathname);
291 bfd = load_game_data(basedpath);
293 // if it wasn't loaded, try it forced relative
296 // just add "//" in front of it
298 strcpy(temppath, "//");
299 strcat(temppath, basedpath);
301 BLI_path_abs(temppath, pathname);
302 bfd = load_game_data(temppath);
305 // if we got a loaded blendfile, proceed
308 blenderdata = bfd->main;
309 startscenename = bfd->curscene->id.name + 2;
312 BLI_strncpy(G.sce, blenderdata->name, sizeof(G.sce));
313 BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
314 #ifndef DISABLE_PYTHON
315 setGamePythonPath(G.sce);
319 // else forget it, we can't find it
322 exitrequested = KX_EXIT_REQUEST_QUIT_GAME;
326 Scene *scene= bfd ? bfd->curscene : (Scene *)BLI_findstring(&blenderdata->scene, startscenename, offsetof(ID, name) + 2);
330 int startFrame = scene->r.cfra;
331 ketsjiengine->SetAnimRecordMode(animation_record, startFrame);
333 // Quad buffered needs a special window.
334 if(scene->gm.stereoflag == STEREO_ENABLED){
335 if (scene->gm.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
336 rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) scene->gm.stereomode);
338 rasterizer->SetEyeSeparation(scene->gm.eyeseparation);
341 rasterizer->SetBackColor(scene->gm.framing.col[0], scene->gm.framing.col[1], scene->gm.framing.col[2], 0.0f);
344 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
346 if (rv3d->persp != RV3D_CAMOB)
348 ketsjiengine->EnableCameraOverride(startscenename);
349 ketsjiengine->SetCameraOverrideUseOrtho((rv3d->persp == RV3D_ORTHO));
350 ketsjiengine->SetCameraOverrideProjectionMatrix(projmat);
351 ketsjiengine->SetCameraOverrideViewMatrix(viewmat);
352 ketsjiengine->SetCameraOverrideClipping(v3d->near, v3d->far);
353 ketsjiengine->SetCameraOverrideLens(v3d->lens);
356 // create a scene converter, create and convert the startingscene
357 KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(blenderdata, ketsjiengine);
358 ketsjiengine->SetSceneConverter(sceneconverter);
359 sceneconverter->addInitFromFrame=false;
360 if (always_use_expand_framing)
361 sceneconverter->SetAlwaysUseExpandFraming(true);
363 bool usemat = false, useglslmat = false;
365 if(GLEW_ARB_multitexture && GLEW_VERSION_1_1)
368 if(GPU_glsl_support())
370 else if(scene->gm.matmode == GAME_MAT_GLSL)
373 if(usemat && (scene->gm.matmode != GAME_MAT_TEXFACE))
374 sceneconverter->SetMaterials(true);
375 if(useglslmat && (scene->gm.matmode == GAME_MAT_GLSL))
376 sceneconverter->SetGLSLMaterials(true);
378 KX_Scene* startscene = new KX_Scene(keyboarddevice,
385 #ifndef DISABLE_PYTHON
386 // some python things
387 PyObject *gameLogic, *gameLogic_keys;
388 setupGamePython(ketsjiengine, startscene, blenderdata, pyGlobalDict, &gameLogic, &gameLogic_keys, 0, NULL);
389 #endif // DISABLE_PYTHON
391 //initialize Dome Settings
392 if(scene->gm.stereoflag == STEREO_DOME)
393 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);
395 // initialize 3D Audio Settings
396 AUD_setSpeedOfSound(scene->audio.speed_of_sound);
397 AUD_setDopplerFactor(scene->audio.doppler_factor);
398 AUD_setDistanceModel(AUD_DistanceModel(scene->audio.distance_model));
400 // from see blender.c:
401 // FIXME: this version patching should really be part of the file-reading code,
402 // but we still get too many unrelated data-corruption crashes otherwise...
403 if (blenderdata->versionfile < 250)
404 do_versions_ipos_to_animato(blenderdata);
408 // convert and add scene
409 sceneconverter->ConvertScene(
413 ketsjiengine->AddScene(startscene);
415 // init the rasterizer
419 ketsjiengine->StartEngine(true);
422 // Set the animation playback rate for ipo's and actions
423 // the framerate below should patch with FPS macro defined in blendef.h
424 // Could be in StartEngine set the framerate, we need the scene to do this
425 ketsjiengine->SetAnimFrameRate(FPS);
428 printf("\nBlender Game Engine Started\n\n");
429 while (!exitrequested)
431 // first check if we want to exit
432 exitrequested = ketsjiengine->GetExitCode();
435 bool render = ketsjiengine->NextFrame(); // XXX 2.5 Bug, This is never true! FIXME- Campbell
440 ketsjiengine->Render();
443 wm_window_process_events_nosleep(C);
445 // test for the ESC key
446 //XXX while (qtest())
447 while(wmEvent *event= (wmEvent *)win->queue.first)
450 //unsigned short event = 0; //XXX extern_qread(&val);
452 if (keyboarddevice->ConvertBlenderEvent(event->type,event->val))
453 exitrequested = KX_EXIT_REQUEST_BLENDER_ESC;
455 /* Coordinate conversion... where
456 * should this really be?
458 if (event->type==MOUSEMOVE) {
459 /* Note, not nice! XXX 2.5 event hack */
460 val = event->x - ar->winrct.xmin;
461 mousedevice->ConvertBlenderEvent(MOUSEX, val);
463 val = ar->winy - (event->y - ar->winrct.ymin) - 1;
464 mousedevice->ConvertBlenderEvent(MOUSEY, val);
467 mousedevice->ConvertBlenderEvent(event->type,event->val);
470 BLI_remlink(&win->queue, event);
471 wm_event_free(event);
475 printf("\nBlender Game Engine Finished\n\n");
476 exitstring = ketsjiengine->GetExitString();
479 // when exiting the mainloop
480 #ifndef DISABLE_PYTHON
481 // Clears the dictionary by hand:
482 // This prevents, extra references to global variables
483 // inside the GameLogic dictionary when the python interpreter is finalized.
484 // which allows the scene to safely delete them :)
485 // see: (space.c)->start_game
487 //PyDict_Clear(PyModule_GetDict(gameLogic));
489 // Keep original items, means python plugins will autocomplete members
491 PyObject *gameLogic_keys_new = PyDict_Keys(PyModule_GetDict(gameLogic));
492 for (listIndex=0; listIndex < PyList_Size(gameLogic_keys_new); listIndex++) {
493 PyObject* item = PyList_GET_ITEM(gameLogic_keys_new, listIndex);
494 if (!PySequence_Contains(gameLogic_keys, item)) {
495 PyDict_DelItem( PyModule_GetDict(gameLogic), item);
498 Py_DECREF(gameLogic_keys_new);
499 gameLogic_keys_new = NULL;
501 ketsjiengine->StopEngine();
502 #ifndef DISABLE_PYTHON
503 exitGamePythonScripting();
505 networkdevice->Disconnect();
509 delete sceneconverter;
510 sceneconverter = NULL;
513 #ifndef DISABLE_PYTHON
514 Py_DECREF(gameLogic_keys);
515 gameLogic_keys = NULL;
518 //lock frame and camera enabled - restoring global values
519 if (v3d->scenelock==0){
520 startscene->lay= tmp_lay;
521 startscene->camera= tmp_camera;
524 // set the cursor back to normal
525 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
527 // clean up some stuff
540 delete networkdevice;
541 networkdevice = NULL;
545 delete keyboarddevice;
546 keyboarddevice = NULL;
569 } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
572 U.gameflags &= ~USER_DISABLE_VBO;
574 if (bfd) BLO_blendfiledata_free(bfd);
576 BLI_strncpy(G.sce, oldsce, sizeof(G.sce));
578 #ifndef DISABLE_PYTHON
579 Py_DECREF(pyGlobalDict);
581 // Release Python's GIL
582 PyGILState_Release(gilstate);