1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
|
/**
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
* Blender's Ketsji startpoint
*/
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#if defined(WIN32) && !defined(FREE_WINDOWS)
// don't show stl-warnings
#pragma warning (disable:4786)
#endif
#include "GL/glew.h"
#include "KX_BlenderGL.h"
#include "KX_BlenderCanvas.h"
#include "KX_BlenderKeyboardDevice.h"
#include "KX_BlenderMouseDevice.h"
#include "KX_BlenderRenderTools.h"
#include "KX_BlenderSystem.h"
#include "BL_Material.h"
#include "KX_KetsjiEngine.h"
#include "KX_BlenderSceneConverter.h"
#include "KX_PythonInit.h"
#include "KX_PyConstraintBinding.h"
#include "RAS_GLExtensionManager.h"
#include "RAS_OpenGLRasterizer.h"
#include "RAS_VAOpenGLRasterizer.h"
#include "RAS_ListRasterizer.h"
#include "NG_LoopBackNetworkDeviceInterface.h"
#include "SYS_System.h"
#include "GPU_extensions.h"
#include "Value.h"
#ifdef __cplusplus
extern "C" {
#endif
/***/
#include "DNA_view3d_types.h"
#include "DNA_screen_types.h"
#include "DNA_userdef_types.h"
#include "DNA_windowmanager_types.h"
#include "BKE_global.h"
#include "BKE_report.h"
#include "BKE_utildefines.h"
//XXX #include "BIF_screen.h"
//XXX #include "BIF_scrarea.h"
#include "BKE_main.h"
#include "BLI_blenlib.h"
#include "BLO_readfile.h"
#include "DNA_scene_types.h"
#include "BKE_ipo.h"
/***/
#include "AUD_C-API.h"
//XXX #include "BSE_headerbuttons.h"
#include "BKE_context.h"
#include "../../blender/windowmanager/WM_types.h"
#include "../../blender/windowmanager/wm_window.h"
#include "../../blender/windowmanager/wm_event_system.h"
#ifdef __cplusplus
}
#endif
static BlendFileData *load_game_data(char *filename)
{
ReportList reports;
BlendFileData *bfd;
BKE_reports_init(&reports, RPT_STORE);
bfd= BLO_read_from_file(filename, &reports);
if (!bfd) {
printf("Loading %s failed: ", filename);
BKE_reports_print(&reports, RPT_ERROR);
}
BKE_reports_clear(&reports);
return bfd;
}
extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *cam_frame, int always_use_expand_framing)
{
/* context values */
struct wmWindow *win= CTX_wm_window(C);
struct Scene *startscene= CTX_data_scene(C);
struct Main* maggie1= CTX_data_main(C);
RAS_Rect area_rect;
area_rect.SetLeft(cam_frame->xmin);
area_rect.SetBottom(cam_frame->ymin);
area_rect.SetRight(cam_frame->xmax);
area_rect.SetTop(cam_frame->ymax);
int exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
Main* blenderdata = maggie1;
char* startscenename = startscene->id.name+2;
char pathname[FILE_MAXDIR+FILE_MAXFILE], oldsce[FILE_MAXDIR+FILE_MAXFILE];
STR_String exitstring = "";
BlendFileData *bfd= NULL;
BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
BLI_strncpy(oldsce, G.sce, sizeof(oldsce));
#ifndef DISABLE_PYTHON
resetGamePythonPath(); // need this so running a second time wont use an old blendfiles path
setGamePythonPath(G.sce);
// Acquire Python's GIL (global interpreter lock)
// so we can safely run Python code and API calls
PyGILState_STATE gilstate = PyGILState_Ensure();
PyObject *pyGlobalDict = PyDict_New(); /* python utility storage, spans blend file loading */
#endif
bgl::InitExtensions(true);
// VBO code for derived mesh is not compatible with BGE (couldn't find why), so disable
int disableVBO = (U.gameflags & USER_DISABLE_VBO);
U.gameflags |= USER_DISABLE_VBO;
do
{
View3D *v3d= CTX_wm_view3d(C);
RegionView3D *rv3d= CTX_wm_region_view3d(C);
// get some preferences
SYS_SystemHandle syshandle = SYS_GetSystem();
bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0);
bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0);
bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0);
bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0);
bool animation_record = (SYS_GetCommandLineInt(syshandle, "animation_record", 0) != 0);
bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0);
#ifndef DISABLE_PYTHON
bool nodepwarnings = (SYS_GetCommandLineInt(syshandle, "ignore_deprecation_warnings", 0) != 0);
#endif
bool novertexarrays = (SYS_GetCommandLineInt(syshandle, "novertexarrays", 0) != 0);
if(animation_record) usefixed= true; /* override since you's always want fixed time for sim recording */
// create the canvas, rasterizer and rendertools
RAS_ICanvas* canvas = new KX_BlenderCanvas(win, area_rect, ar);
canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
RAS_IRenderTools* rendertools = new KX_BlenderRenderTools();
RAS_IRasterizer* rasterizer = NULL;
if(displaylists) {
if (GLEW_VERSION_1_1 && !novertexarrays)
rasterizer = new RAS_ListRasterizer(canvas, true, true);
else
rasterizer = new RAS_ListRasterizer(canvas);
}
else if (GLEW_VERSION_1_1 && !novertexarrays)
rasterizer = new RAS_VAOpenGLRasterizer(canvas, false);
else
rasterizer = new RAS_OpenGLRasterizer(canvas);
// create the inputdevices
KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice();
KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice();
// create a networkdevice
NG_NetworkDeviceInterface* networkdevice = new
NG_LoopBackNetworkDeviceInterface();
//
// create a ketsji/blendersystem (only needed for timing and stuff)
KX_BlenderSystem* kxsystem = new KX_BlenderSystem();
// create the ketsjiengine
KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem);
// set the devices
ketsjiengine->SetKeyboardDevice(keyboarddevice);
ketsjiengine->SetMouseDevice(mousedevice);
ketsjiengine->SetNetworkDevice(networkdevice);
ketsjiengine->SetCanvas(canvas);
ketsjiengine->SetRenderTools(rendertools);
ketsjiengine->SetRasterizer(rasterizer);
ketsjiengine->SetNetworkDevice(networkdevice);
ketsjiengine->SetUseFixedTime(usefixed);
ketsjiengine->SetTimingDisplay(frameRate, profile, properties);
#ifndef DISABLE_PYTHON
CValue::SetDeprecationWarnings(nodepwarnings);
#endif
//lock frame and camera enabled - storing global values
int tmp_lay= startscene->lay;
Object *tmp_camera = startscene->camera;
if (v3d->scenelock==0){
startscene->lay= v3d->lay;
startscene->camera= v3d->camera;
}
// some blender stuff
MT_CmMatrix4x4 projmat;
MT_CmMatrix4x4 viewmat;
float camzoom;
int i;
for (i = 0; i < 16; i++)
{
float *viewmat_linear= (float*) rv3d->viewmat;
viewmat.setElem(i, viewmat_linear[i]);
}
for (i = 0; i < 16; i++)
{
float *projmat_linear= (float*) rv3d->winmat;
projmat.setElem(i, projmat_linear[i]);
}
if(rv3d->persp==RV3D_CAMOB) {
if(startscene->gm.framing.type == SCE_GAMEFRAMING_BARS) { /* Letterbox */
camzoom = 1.0f;
}
else {
camzoom = (1.41421 + (rv3d->camzoom / 50.0));
camzoom *= camzoom;
camzoom = 4.0 / camzoom;
}
}
else {
camzoom = 2.0;
}
ketsjiengine->SetDrawType(v3d->drawtype);
ketsjiengine->SetCameraZoom(camzoom);
// if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file
if (exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME || exitrequested == KX_EXIT_REQUEST_RESTART_GAME)
{
exitrequested = KX_EXIT_REQUEST_NO_REQUEST;
if (bfd) BLO_blendfiledata_free(bfd);
char basedpath[240];
// base the actuator filename with respect
// to the original file working directory
if (exitstring != "")
strcpy(basedpath, exitstring.Ptr());
// load relative to the last loaded file, this used to be relative
// to the first file but that makes no sense, relative paths in
// blend files should be relative to that file, not some other file
// that happened to be loaded first
BLI_path_abs(basedpath, pathname);
bfd = load_game_data(basedpath);
// if it wasn't loaded, try it forced relative
if (!bfd)
{
// just add "//" in front of it
char temppath[242];
strcpy(temppath, "//");
strcat(temppath, basedpath);
BLI_path_abs(temppath, pathname);
bfd = load_game_data(temppath);
}
// if we got a loaded blendfile, proceed
if (bfd)
{
blenderdata = bfd->main;
startscenename = bfd->curscene->id.name + 2;
if(blenderdata) {
BLI_strncpy(G.sce, blenderdata->name, sizeof(G.sce));
BLI_strncpy(pathname, blenderdata->name, sizeof(pathname));
#ifndef DISABLE_PYTHON
setGamePythonPath(G.sce);
#endif
}
}
// else forget it, we can't find it
else
{
exitrequested = KX_EXIT_REQUEST_QUIT_GAME;
}
}
Scene *scene= bfd ? bfd->curscene : (Scene *)BLI_findstring(&blenderdata->scene, startscenename, offsetof(ID, name) + 2);
if (scene)
{
int startFrame = scene->r.cfra;
ketsjiengine->SetAnimRecordMode(animation_record, startFrame);
// Quad buffered needs a special window.
if(scene->gm.stereoflag == STEREO_ENABLED){
if (scene->gm.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED)
rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) scene->gm.stereomode);
rasterizer->SetEyeSeparation(scene->gm.eyeseparation);
}
rasterizer->SetBackColor(scene->gm.framing.col[0], scene->gm.framing.col[1], scene->gm.framing.col[2], 0.0f);
}
if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME)
{
if (rv3d->persp != RV3D_CAMOB)
{
ketsjiengine->EnableCameraOverride(startscenename);
ketsjiengine->SetCameraOverrideUseOrtho((rv3d->persp == RV3D_ORTHO));
ketsjiengine->SetCameraOverrideProjectionMatrix(projmat);
ketsjiengine->SetCameraOverrideViewMatrix(viewmat);
ketsjiengine->SetCameraOverrideClipping(v3d->near, v3d->far);
ketsjiengine->SetCameraOverrideLens(v3d->lens);
}
// create a scene converter, create and convert the startingscene
KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(blenderdata, ketsjiengine);
ketsjiengine->SetSceneConverter(sceneconverter);
sceneconverter->addInitFromFrame=false;
if (always_use_expand_framing)
sceneconverter->SetAlwaysUseExpandFraming(true);
bool usemat = false, useglslmat = false;
if(GLEW_ARB_multitexture && GLEW_VERSION_1_1)
usemat = true;
if(GPU_glsl_support())
useglslmat = true;
else if(scene->gm.matmode == GAME_MAT_GLSL)
usemat = false;
if(usemat && (scene->gm.matmode != GAME_MAT_TEXFACE))
sceneconverter->SetMaterials(true);
if(useglslmat && (scene->gm.matmode == GAME_MAT_GLSL))
sceneconverter->SetGLSLMaterials(true);
KX_Scene* startscene = new KX_Scene(keyboarddevice,
mousedevice,
networkdevice,
startscenename,
scene,
canvas);
#ifndef DISABLE_PYTHON
// some python things
PyObject *gameLogic, *gameLogic_keys;
setupGamePython(ketsjiengine, startscene, blenderdata, pyGlobalDict, &gameLogic, &gameLogic_keys, 0, NULL);
#endif // DISABLE_PYTHON
//initialize Dome Settings
if(scene->gm.stereoflag == STEREO_DOME)
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);
// initialize 3D Audio Settings
AUD_setSpeedOfSound(scene->audio.speed_of_sound);
AUD_setDopplerFactor(scene->audio.doppler_factor);
AUD_setDistanceModel(AUD_DistanceModel(scene->audio.distance_model));
// from see blender.c:
// FIXME: this version patching should really be part of the file-reading code,
// but we still get too many unrelated data-corruption crashes otherwise...
if (blenderdata->versionfile < 250)
do_versions_ipos_to_animato(blenderdata);
if (sceneconverter)
{
// convert and add scene
sceneconverter->ConvertScene(
startscene,
rendertools,
canvas);
ketsjiengine->AddScene(startscene);
// init the rasterizer
rasterizer->Init();
// start the engine
ketsjiengine->StartEngine(true);
// Set the animation playback rate for ipo's and actions
// the framerate below should patch with FPS macro defined in blendef.h
// Could be in StartEngine set the framerate, we need the scene to do this
ketsjiengine->SetAnimFrameRate(FPS);
// the mainloop
printf("\nBlender Game Engine Started\n");
while (!exitrequested)
{
// first check if we want to exit
exitrequested = ketsjiengine->GetExitCode();
// kick the engine
bool render = ketsjiengine->NextFrame(); // XXX 2.5 Bug, This is never true! FIXME- Campbell
if (render)
{
// render the frame
ketsjiengine->Render();
}
wm_window_process_events_nosleep();
// test for the ESC key
//XXX while (qtest())
while(wmEvent *event= (wmEvent *)win->queue.first)
{
short val = 0;
//unsigned short event = 0; //XXX extern_qread(&val);
if (keyboarddevice->ConvertBlenderEvent(event->type,event->val))
exitrequested = KX_EXIT_REQUEST_BLENDER_ESC;
/* Coordinate conversion... where
* should this really be?
*/
if (event->type==MOUSEMOVE) {
/* Note, not nice! XXX 2.5 event hack */
val = event->x - ar->winrct.xmin;
mousedevice->ConvertBlenderEvent(MOUSEX, val);
val = ar->winy - (event->y - ar->winrct.ymin) - 1;
mousedevice->ConvertBlenderEvent(MOUSEY, val);
}
else {
mousedevice->ConvertBlenderEvent(event->type,event->val);
}
BLI_remlink(&win->queue, event);
wm_event_free(event);
}
}
printf("Blender Game Engine Finished\n");
exitstring = ketsjiengine->GetExitString();
// when exiting the mainloop
#ifndef DISABLE_PYTHON
// Clears the dictionary by hand:
// This prevents, extra references to global variables
// inside the GameLogic dictionary when the python interpreter is finalized.
// which allows the scene to safely delete them :)
// see: (space.c)->start_game
//PyDict_Clear(PyModule_GetDict(gameLogic));
// Keep original items, means python plugins will autocomplete members
int listIndex;
PyObject *gameLogic_keys_new = PyDict_Keys(PyModule_GetDict(gameLogic));
for (listIndex=0; listIndex < PyList_Size(gameLogic_keys_new); listIndex++) {
PyObject* item = PyList_GET_ITEM(gameLogic_keys_new, listIndex);
if (!PySequence_Contains(gameLogic_keys, item)) {
PyDict_DelItem( PyModule_GetDict(gameLogic), item);
}
}
Py_DECREF(gameLogic_keys_new);
gameLogic_keys_new = NULL;
#endif
ketsjiengine->StopEngine();
#ifndef DISABLE_PYTHON
exitGamePythonScripting();
#endif
networkdevice->Disconnect();
}
if (sceneconverter)
{
delete sceneconverter;
sceneconverter = NULL;
}
#ifndef DISABLE_PYTHON
Py_DECREF(gameLogic_keys);
gameLogic_keys = NULL;
#endif
}
//lock frame and camera enabled - restoring global values
if (v3d->scenelock==0){
startscene->lay= tmp_lay;
startscene->camera= tmp_camera;
}
// set the cursor back to normal
canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
// clean up some stuff
if (ketsjiengine)
{
delete ketsjiengine;
ketsjiengine = NULL;
}
if (kxsystem)
{
delete kxsystem;
kxsystem = NULL;
}
if (networkdevice)
{
delete networkdevice;
networkdevice = NULL;
}
if (keyboarddevice)
{
delete keyboarddevice;
keyboarddevice = NULL;
}
if (mousedevice)
{
delete mousedevice;
mousedevice = NULL;
}
if (rasterizer)
{
delete rasterizer;
rasterizer = NULL;
}
if (rendertools)
{
delete rendertools;
rendertools = NULL;
}
if (canvas)
{
delete canvas;
canvas = NULL;
}
} while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME);
if (!disableVBO)
U.gameflags &= ~USER_DISABLE_VBO;
if (bfd) BLO_blendfiledata_free(bfd);
BLI_strncpy(G.sce, oldsce, sizeof(G.sce));
#ifndef DISABLE_PYTHON
Py_DECREF(pyGlobalDict);
// Release Python's GIL
PyGILState_Release(gilstate);
#endif
}
|