2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
26 * Blender's Ketsji startpoint
29 /** \file gameengine/BlenderRoutines/BL_KetsjiEmbedStart.cpp
38 #if defined(WIN32) && !defined(FREE_WINDOWS)
39 // don't show stl-warnings
40 #pragma warning (disable:4786)
45 #include "KX_BlenderGL.h"
46 #include "KX_BlenderCanvas.h"
47 #include "KX_BlenderKeyboardDevice.h"
48 #include "KX_BlenderMouseDevice.h"
49 #include "KX_BlenderRenderTools.h"
50 #include "KX_BlenderSystem.h"
51 #include "BL_Material.h"
53 #include "KX_KetsjiEngine.h"
54 #include "KX_BlenderSceneConverter.h"
55 #include "KX_PythonInit.h"
56 #include "KX_PyConstraintBinding.h"
57 #include "KX_PythonMain.h"
59 #include "RAS_GLExtensionManager.h"
60 #include "RAS_OpenGLRasterizer.h"
61 #include "RAS_VAOpenGLRasterizer.h"
62 #include "RAS_ListRasterizer.h"
64 #include "NG_LoopBackNetworkDeviceInterface.h"
66 #include "BL_System.h"
68 #include "GPU_extensions.h"
77 #include "DNA_view3d_types.h"
78 #include "DNA_screen_types.h"
79 #include "DNA_userdef_types.h"
80 #include "DNA_windowmanager_types.h"
81 #include "BKE_global.h"
82 #include "BKE_report.h"
84 #include "MEM_guardedalloc.h"
86 /* #include "BKE_screen.h" */ /* cant include this because of 'new' function name */
87 extern float BKE_screen_view3d_zoom_to_fac(float camzoom);
90 #include "BLI_blenlib.h"
91 #include "BLO_readfile.h"
92 #include "DNA_scene_types.h"
96 #include "BKE_context.h"
97 #include "../../blender/windowmanager/WM_types.h"
98 #include "../../blender/windowmanager/wm_window.h"
99 #include "../../blender/windowmanager/wm_event_system.h"
104 #ifdef WITH_AUDASPACE
105 # include "AUD_C-API.h"
106 # include "AUD_I3DDevice.h"
107 # include "AUD_IDevice.h"
110 static BlendFileData *load_game_data(char *filename)
115 BKE_reports_init(&reports, RPT_STORE);
116 bfd= BLO_read_from_file(filename, &reports);
119 printf("Loading %s failed: ", filename);
120 BKE_reports_print(&reports, RPT_ERROR);
123 BKE_reports_clear(&reports);
128 int BL_KetsjiNextFrame(struct KX_KetsjiEngine* ketsjiengine, struct bContext *C, struct wmWindow* win, struct Scene* scene, struct ARegion *ar,
129 KX_BlenderKeyboardDevice* keyboarddevice, KX_BlenderMouseDevice* mousedevice, int draw_letterbox)
133 // first check if we want to exit
134 exitrequested = ketsjiengine->GetExitCode();
137 bool render = ketsjiengine->NextFrame();
142 // Clear screen to border color
143 // We do this here since we set the canvas to be within the frames. This means the engine
144 // itself is unaware of the extra space, so we clear the whole region for it.
145 glClearColor(scene->gm.framing.col[0], scene->gm.framing.col[1], scene->gm.framing.col[2], 1.0f);
146 glViewport(ar->winrct.xmin, ar->winrct.ymin,
147 BLI_RCT_SIZE_X(&ar->winrct), BLI_RCT_SIZE_Y(&ar->winrct));
148 glClear(GL_COLOR_BUFFER_BIT);
152 ketsjiengine->Render();
155 wm_window_process_events_nosleep();
157 // test for the ESC key
158 //XXX while (qtest())
159 while(wmEvent *event= (wmEvent *)win->queue.first)
162 //unsigned short event = 0; //XXX extern_qread(&val);
164 if (keyboarddevice->ConvertBlenderEvent(event->type,event->val))
165 exitrequested = KX_EXIT_REQUEST_BLENDER_ESC;
167 /* Coordinate conversion... where
168 * should this really be?
170 if (event->type==MOUSEMOVE) {
171 /* Note, not nice! XXX 2.5 event hack */
172 val = event->x - ar->winrct.xmin;
173 mousedevice->ConvertBlenderEvent(MOUSEX, val);
175 val = ar->winy - (event->y - ar->winrct.ymin) - 1;
176 mousedevice->ConvertBlenderEvent(MOUSEY, val);
179 mousedevice->ConvertBlenderEvent(event->type,event->val);
182 BLI_remlink(&win->queue, event);
183 wm_event_free(event);
186 if(win != CTX_wm_window(C)) {
187 exitrequested= KX_EXIT_REQUEST_OUTSIDE; /* window closed while bge runs */
189 return exitrequested;
192 struct BL_KetsjiNextFrameState {
193 struct KX_KetsjiEngine* ketsjiengine;
195 struct wmWindow* win;
198 KX_BlenderKeyboardDevice* keyboarddevice;
199 KX_BlenderMouseDevice* mousedevice;
201 } ketsjinextframestate;
203 int BL_KetsjiPyNextFrame(void *state0)
205 BL_KetsjiNextFrameState *state = (BL_KetsjiNextFrameState *) state0;
206 return BL_KetsjiNextFrame(
212 state->keyboarddevice,
214 state->draw_letterbox);
217 extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *cam_frame, int always_use_expand_framing)
220 struct wmWindow *win= CTX_wm_window(C);
221 struct Scene *startscene= CTX_data_scene(C);
222 struct Main* maggie1= CTX_data_main(C);
226 area_rect.SetLeft(cam_frame->xmin);
227 area_rect.SetBottom(cam_frame->ymin);
228 area_rect.SetRight(cam_frame->xmax);
229 area_rect.SetTop(cam_frame->ymax);
231 int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
232 Main* blenderdata = maggie1;
234 char* startscenename = startscene->id.name+2;
235 char pathname[FILE_MAXDIR+FILE_MAXFILE], oldsce[FILE_MAXDIR+FILE_MAXFILE];
236 STR_String exitstring = "";
237 BlendFileData *bfd= NULL;
239 BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
240 BLI_strncpy(oldsce, G.main->name, sizeof(oldsce));
242 resetGamePythonPath(); // need this so running a second time wont use an old blendfiles path
243 setGamePythonPath(G.main->name);
245 // Acquire Python's GIL (global interpreter lock)
246 // so we can safely run Python code and API calls
247 PyGILState_STATE gilstate = PyGILState_Ensure();
249 PyObject *pyGlobalDict = PyDict_New(); /* python utility storage, spans blend file loading */
252 bgl::InitExtensions(true);
254 // VBO code for derived mesh is not compatible with BGE (couldn't find why), so disable
255 int disableVBO = (U.gameflags & USER_DISABLE_VBO);
256 U.gameflags |= USER_DISABLE_VBO;
258 // Globals to be carried on over blender files
260 gs.matmode= startscene->gm.matmode;
261 gs.glslflag= startscene->gm.flag;
265 View3D *v3d= CTX_wm_view3d(C);
266 RegionView3D *rv3d= CTX_wm_region_view3d(C);
268 // get some preferences
269 SYS_SystemHandle syshandle = SYS_GetSystem();
270 bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
271 bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
272 bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
273 bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
274 bool animation_record = (SYS_GetCommandLineInt(syshandle, "animation_record", 0) != 0);
275 bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
277 bool nodepwarnings = (SYS_GetCommandLineInt(syshandle, "ignore_deprecation_warnings", 0) != 0);
279 bool novertexarrays = (SYS_GetCommandLineInt(syshandle, "novertexarrays", 0) != 0);
280 bool mouse_state = startscene->gm.flag & GAME_SHOW_MOUSE;
281 bool restrictAnimFPS = startscene->gm.flag & GAME_RESTRICT_ANIM_UPDATES;
283 if (animation_record) usefixed= false; /* override since you don't want to run full-speed for sim recording */
285 // create the canvas, rasterizer and rendertools
286 RAS_ICanvas* canvas = new KX_BlenderCanvas(win, area_rect, ar);
288 // default mouse state set on render panel
290 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
292 canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
293 RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
294 RAS_IRasterizer* rasterizer = NULL;
297 if (GLEW_VERSION_1_1 && !novertexarrays)
298 rasterizer = new RAS_ListRasterizer(canvas, true, true);
300 rasterizer = new RAS_ListRasterizer(canvas);
302 else if (GLEW_VERSION_1_1 && !novertexarrays)
303 rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
305 rasterizer = new RAS_OpenGLRasterizer(canvas);
307 // create the inputdevices
308 KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
309 KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
311 // create a networkdevice
312 NG_NetworkDeviceInterface* networkdevice = new
313 NG_LoopBackNetworkDeviceInterface();
316 // create a ketsji/blendersystem (only needed for timing and stuff)
317 KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
319 // create the ketsjiengine
320 KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
323 ketsjiengine->SetKeyboardDevice(keyboarddevice);
324 ketsjiengine->SetMouseDevice(mousedevice);
325 ketsjiengine->SetNetworkDevice(networkdevice);
326 ketsjiengine->SetCanvas(canvas);
327 ketsjiengine->SetRenderTools(rendertools);
328 ketsjiengine->SetRasterizer(rasterizer);
329 ketsjiengine->SetUseFixedTime(usefixed);
330 ketsjiengine->SetTimingDisplay(frameRate, profile, properties);
331 ketsjiengine->SetRestrictAnimationFPS(restrictAnimFPS);
332 KX_KetsjiEngine::SetExitKey(ConvertKeyCode(startscene->gm.exitkey));
334 //set the global settings (carried over if restart/load new files)
335 ketsjiengine->SetGlobalSettings(&gs);
338 CValue::SetDeprecationWarnings(nodepwarnings);
341 //lock frame and camera enabled - storing global values
342 int tmp_lay= startscene->lay;
343 Object *tmp_camera = startscene->camera;
345 if (v3d->scenelock==0) {
346 startscene->lay= v3d->lay;
347 startscene->camera= v3d->camera;
350 // some blender stuff
352 int draw_letterbox = 0;
354 if (rv3d->persp==RV3D_CAMOB) {
355 if (startscene->gm.framing.type == SCE_GAMEFRAMING_BARS) { /* Letterbox */
360 camzoom = 1.0f / BKE_screen_view3d_zoom_to_fac(rv3d->camzoom);
368 ketsjiengine->SetDrawType(v3d->drawtype);
369 ketsjiengine->SetCameraZoom(camzoom);
371 // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
372 if (exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME || exitrequested == KX_EXIT_REQUEST_RESTART_GAME)
374 exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
375 if (bfd) BLO_blendfiledata_free(bfd);
377 char basedpath[FILE_MAX];
378 // base the actuator filename with respect
379 // to the original file working directory
381 if (exitstring != "")
382 strcpy(basedpath, exitstring.Ptr());
384 // load relative to the last loaded file, this used to be relative
385 // to the first file but that makes no sense, relative paths in
386 // blend files should be relative to that file, not some other file
387 // that happened to be loaded first
388 BLI_path_abs(basedpath, pathname);
389 bfd = load_game_data(basedpath);
391 // if it wasn't loaded, try it forced relative
394 // just add "//" in front of it
396 strcpy(temppath, "//");
397 strcat(temppath, basedpath);
399 BLI_path_abs(temppath, pathname);
400 bfd = load_game_data(temppath);
403 // if we got a loaded blendfile, proceed
406 blenderdata = bfd->main;
407 startscenename = bfd->curscene->id.name + 2;
410 BLI_strncpy(G.main->name, blenderdata->name, sizeof(G.main->name));
411 BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
413 setGamePythonPath(G.main->name);
417 // else forget it, we can't find it
420 exitrequested = KX_EXIT_REQUEST_QUIT_GAME;
424 Scene *scene= bfd ? bfd->curscene : (Scene *)BLI_findstring(&blenderdata->scene, startscenename, offsetof(ID, name) + 2);
428 int startFrame = scene->r.cfra;
429 ketsjiengine->SetAnimRecordMode(animation_record, startFrame);
431 // Quad buffered needs a special window.
432 if (scene->gm.stereoflag == STEREO_ENABLED) {
433 if (scene->gm.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
434 rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) scene->gm.stereomode);
436 rasterizer->SetEyeSeparation(scene->gm.eyeseparation);
439 rasterizer->SetBackColor(scene->gm.framing.col[0], scene->gm.framing.col[1], scene->gm.framing.col[2], 0.0f);
442 if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
444 if (rv3d->persp != RV3D_CAMOB)
446 ketsjiengine->EnableCameraOverride(startscenename);
447 ketsjiengine->SetCameraOverrideUseOrtho((rv3d->persp == RV3D_ORTHO));
448 ketsjiengine->SetCameraOverrideProjectionMatrix(MT_CmMatrix4x4(rv3d->winmat));
449 ketsjiengine->SetCameraOverrideViewMatrix(MT_CmMatrix4x4(rv3d->viewmat));
450 if (rv3d->persp == RV3D_ORTHO)
452 ketsjiengine->SetCameraOverrideClipping(-v3d->far, v3d->far);
456 ketsjiengine->SetCameraOverrideClipping(v3d->near, v3d->far);
458 ketsjiengine->SetCameraOverrideLens(v3d->lens);
461 // create a scene converter, create and convert the startingscene
462 KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(blenderdata, ketsjiengine);
463 ketsjiengine->SetSceneConverter(sceneconverter);
464 sceneconverter->addInitFromFrame=false;
465 if (always_use_expand_framing)
466 sceneconverter->SetAlwaysUseExpandFraming(true);
468 bool usemat = false, useglslmat = false;
470 if (GLEW_ARB_multitexture && GLEW_VERSION_1_1)
473 if (GPU_glsl_support())
475 else if (gs.matmode == GAME_MAT_GLSL)
478 if (usemat && (gs.matmode != GAME_MAT_TEXFACE))
479 sceneconverter->SetMaterials(true);
480 if (useglslmat && (gs.matmode == GAME_MAT_GLSL))
481 sceneconverter->SetGLSLMaterials(true);
483 KX_Scene* startscene = new KX_Scene(keyboarddevice,
491 // some python things
492 PyObject *gameLogic, *gameLogic_keys;
493 setupGamePython(ketsjiengine, startscene, blenderdata, pyGlobalDict, &gameLogic, &gameLogic_keys, 0, NULL);
494 #endif // WITH_PYTHON
496 //initialize Dome Settings
497 if (scene->gm.stereoflag == STEREO_DOME)
498 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);
500 // initialize 3D Audio Settings
501 AUD_I3DDevice* dev = AUD_get3DDevice();
504 dev->setSpeedOfSound(scene->audio.speed_of_sound);
505 dev->setDopplerFactor(scene->audio.doppler_factor);
506 dev->setDistanceModel(AUD_DistanceModel(scene->audio.distance_model));
509 // from see blender.c:
510 // FIXME: this version patching should really be part of the file-reading code,
511 // but we still get too many unrelated data-corruption crashes otherwise...
512 if (blenderdata->versionfile < 250)
513 do_versions_ipos_to_animato(blenderdata);
517 // convert and add scene
518 sceneconverter->ConvertScene(
522 ketsjiengine->AddScene(startscene);
524 // init the rasterizer
528 ketsjiengine->StartEngine(true);
531 // Set the animation playback rate for ipo's and actions
532 // the framerate below should patch with FPS macro defined in blendef.h
533 // Could be in StartEngine set the framerate, we need the scene to do this
534 ketsjiengine->SetAnimFrameRate(FPS);
537 char *python_main = NULL;
538 pynextframestate.state = NULL;
539 pynextframestate.func = NULL;
540 python_main = KX_GetPythonMain(scene);
543 printf("\nBlender Game Engine Started\n");
545 char *python_code = KX_GetPythonCode(blenderdata, python_main);
547 ketsjinextframestate.ketsjiengine = ketsjiengine;
548 ketsjinextframestate.C = C;
549 ketsjinextframestate.win = win;
550 ketsjinextframestate.scene = scene;
551 ketsjinextframestate.ar = ar;
552 ketsjinextframestate.keyboarddevice = keyboarddevice;
553 ketsjinextframestate.mousedevice = mousedevice;
554 ketsjinextframestate.draw_letterbox = draw_letterbox;
556 pynextframestate.state = &ketsjinextframestate;
557 pynextframestate.func = &BL_KetsjiPyNextFrame;
558 printf("Yielding control to Python script '%s'...\n", python_main);
559 PyRun_SimpleString(python_code);
560 printf("Exit Python script '%s'\n", python_main);
561 MEM_freeN(python_code);
565 #endif /* WITH_PYTHON */
567 while (!exitrequested)
569 exitrequested = BL_KetsjiNextFrame(ketsjiengine, C, win, scene, ar, keyboarddevice, mousedevice, draw_letterbox);
572 printf("Blender Game Engine Finished\n");
573 exitstring = ketsjiengine->GetExitString();
575 if (python_main) MEM_freeN(python_main);
576 #endif /* WITH_PYTHON */
578 gs = *(ketsjiengine->GetGlobalSettings());
580 // when exiting the mainloop
582 // Clears the dictionary by hand:
583 // This prevents, extra references to global variables
584 // inside the GameLogic dictionary when the python interpreter is finalized.
585 // which allows the scene to safely delete them :)
586 // see: (space.c)->start_game
588 //PyDict_Clear(PyModule_GetDict(gameLogic));
590 // Keep original items, means python plugins will autocomplete members
591 PyObject *gameLogic_keys_new = PyDict_Keys(PyModule_GetDict(gameLogic));
592 const Py_ssize_t numitems= PyList_GET_SIZE(gameLogic_keys_new);
593 Py_ssize_t listIndex;
594 for (listIndex=0; listIndex < numitems; listIndex++) {
595 PyObject* item = PyList_GET_ITEM(gameLogic_keys_new, listIndex);
596 if (!PySequence_Contains(gameLogic_keys, item)) {
597 PyDict_DelItem( PyModule_GetDict(gameLogic), item);
600 Py_DECREF(gameLogic_keys_new);
601 gameLogic_keys_new = NULL;
603 ketsjiengine->StopEngine();
605 exitGamePythonScripting();
607 networkdevice->Disconnect();
611 delete sceneconverter;
612 sceneconverter = NULL;
616 Py_DECREF(gameLogic_keys);
617 gameLogic_keys = NULL;
620 //lock frame and camera enabled - restoring global values
621 if (v3d->scenelock==0) {
622 startscene->lay= tmp_lay;
623 startscene->camera= tmp_camera;
626 if (exitrequested != KX_EXIT_REQUEST_OUTSIDE)
628 // set the cursor back to normal
629 canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
632 // clean up some stuff
645 delete networkdevice;
646 networkdevice = NULL;
650 delete keyboarddevice;
651 keyboarddevice = NULL;
674 // stop all remaining playing sounds
675 AUD_getDevice()->stopAll();
677 } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
680 U.gameflags &= ~USER_DISABLE_VBO;
682 if (bfd) BLO_blendfiledata_free(bfd);
684 BLI_strncpy(G.main->name, oldsce, sizeof(G.main->name));
687 Py_DECREF(pyGlobalDict);
689 // Release Python's GIL
690 PyGILState_Release(gilstate);