Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
/*
   Copyright (c) 2018 DIVIDE-Studio
   Copyright (c) 2009 Ionut Cava

   This file is part of DIVIDE Framework.

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software
   and associated documentation files (the "Software"), to deal in the Software
   without restriction,
   including without limitation the rights to use, copy, modify, merge, publish,
   distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so,
   subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED,
   INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
   PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
   DAMAGES OR OTHER LIABILITY,
   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
   IN CONNECTION WITH THE SOFTWARE
   OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 */

#ifndef _SCENE_H_
#define _SCENE_H_

#include "SceneState.h"
#include "SceneInput.h"
#include "Platform/Threading/Headers/Task.h"

/*All these includes are useful for a scene, so instead of forward declaring the
  classes, we include the headers
  to make them available in every scene source file. To reduce compile times,
  forward declare the "Scene" class instead
*/
// Core files
#include "Core/Headers/Kernel.h"
#include "Core/Headers/Application.h"
#include "Graphs/Headers/SceneGraph.h"
#include "Rendering/Camera/Headers/Camera.h"
#include "Rendering/Lighting/Headers/LightPool.h"
// Hardware
#include "Platform/Video/Headers/GFXDevice.h"
#include "Platform/Input/Headers/InputInterface.h"
// Scene Elements
#include "SceneEnvironmentProbePool.h"
#include "AI/Headers/AIManager.h"
#include "Environment/Sky/Headers/Sky.h"
#include "Rendering/Lighting/Headers/Light.h"
#include "Rendering/Lighting/Headers/DirectionalLight.h"
#include "Physics/Headers/PXDevice.h"
#include "Dynamics/Entities/Particles/Headers/ParticleEmitter.h"
// GUI
#include "GUI/Headers/GUI.h"
#include "GUI/Headers/SceneGUIElements.h"

#include <ArenaAllocator/arena_allocator.h>

namespace Divide {
class Sky;
class Light;
class Object3D;
class LoadSave;
class ParamHandler;
class TerrainDescriptor;
class ParticleEmitter;
class PhysicsSceneInterface;

FWD_DECLARE_MANAGED_CLASS(Mesh);
FWD_DECLARE_MANAGED_CLASS(Player);

namespace Attorney {
    class SceneManager;
    class SceneGraph;
    class SceneRenderPass;
    class SceneLoadSave;
    class SceneGUI;
    class SceneInput;
};

/// The scene is a resource (to enforce load/unload and setName) and it has a 2 states:
/// one for game information and one for rendering information

class Scene : public Resource, public PlatformContextComponent {
    friend class Attorney::SceneManager;
    friend class Attorney::SceneGraph;
    friend class Attorney::SceneRenderPass;
    friend class Attorney::SceneLoadSave;
    friend class Attorney::SceneGUI;
    friend class Attorney::SceneInput;

   protected:
    typedef std::stack<FileData, vectorImpl<FileData> > FileDataStack;
    static bool onStartup();
    static bool onShutdown();

   public:
    explicit Scene(PlatformContext& context, ResourceCache& cache, SceneManager& parent, const stringImpl& name);
    virtual ~Scene();

    /**Begin scene logic loop*/
    /// Get all input commands from the user
    virtual void processInput(PlayerIndex idx, const U64 deltaTimeUS);
    /// Update the scene based on the inputs
    virtual void processTasks(const U64 deltaTimeUS);
    virtual void processGUI(const U64 deltaTimeUS);
    /// Scene is rendering, so add intensive tasks here to save CPU cycles
    bool idle();  
    /// The application has lost focus
    void onLostFocus();  
    /**End scene logic loop*/

    /// Update animations, network data, sounds, triggers etc.
    void updateSceneState(const U64 deltaTimeUS);
    /// Override this for Scene specific updates
    virtual void updateSceneStateInternal(const U64 deltaTimeUS) { ACKNOWLEDGE_UNUSED(deltaTimeUS); }
    inline SceneState& state() { return *_sceneState; }
    inline const SceneState& state() const { return *_sceneState; }
    inline SceneRenderState& renderState() { return _sceneState->renderState(); }
    inline const SceneRenderState& renderState() const { return _sceneState->renderState(); }
    inline SceneInput& input() { return *_input; }
    inline const SceneInput& input() const { return *_input; }

    inline SceneGraph& sceneGraph() { return *_sceneGraph; }
    I64 registerTask(const TaskHandle& taskItem, bool start = true, U32 flags = 0, Task::TaskPriority priority = Task::TaskPriority::HIGH);
    void clearTasks();
    void removeTask(I64 jobIdentifier);

    inline void addModel(FileData& model) { _modelDataArray.push(model); }
    inline void addTerrain(const std::shared_ptr<TerrainDescriptor>& ter) { _terrainInfoArray.push_back(ter); }
    void addMusic(MusicType type, const stringImpl& name, const stringImpl& srcFile);
    void addPatch(vectorImpl<FileData>& data);

    // DIRECTIONAL lights have shadow mapping enabled automatically
    SceneGraphNode* addLight(LightType type, SceneGraphNode& parentNode);
    SceneGraphNode* addSky(const stringImpl& nodeName = "");

    /// Object picking
    inline const vectorImpl<I64>& getCurrentSelection(PlayerIndex index = 0) {
        return _currentSelection[index];
    }

    void findSelection(PlayerIndex idx, bool clearOld);

    inline void addSelectionCallback(const DELEGATE_CBK<void, U8, SceneGraphNode*>& selectionCallback) {
        _selectionChangeCallbacks.push_back(selectionCallback);
    }

    void resetSelection(PlayerIndex idx);
    void setSelected(PlayerIndex idx, SceneGraphNode& sgn);

    SceneGraphNode* addParticleEmitter(const stringImpl& name,
                                          std::shared_ptr<ParticleData> data,
                                          SceneGraphNode& parentNode);

    inline vectorImpl<Mesh_ptr>& getVegetationDataArray() { return _vegetationDataArray; }

    inline AI::AIManager& aiManager() { return *_aiManager; }
    inline const AI::AIManager& aiManager() const { return *_aiManager; }

    inline PlatformContext& platformContext() { return _context; }
    inline const PlatformContext& platformContext() const { return _context; }


    inline ResourceCache& resourceCache() { return _resCache; }
    inline const ResourceCache& resourceCache() const { return _resCache; }

    Camera* playerCamera() const;
    Camera* playerCamera(U8 index) const;

   protected:
    virtual void rebuildShaders();
    virtual void onSetActive();
    virtual void onRemoveActive();
    // returns the first available action ID
    virtual U16 registerInputActions();
    virtual void loadKeyBindings();

    void onNodeDestroy(SceneGraphNode& node);
    void findHoverTarget(PlayerIndex idx);
    bool checkCameraUnderwater(PlayerIndex idx) const;
    void toggleFlashlight(PlayerIndex idx);

    virtual bool save(ByteBuffer& outputBuffer) const;
    virtual bool load(ByteBuffer& inputBuffer);

    virtual bool frameStarted();
    virtual bool frameEnded();
    /// Description in SceneManager
    virtual bool loadResources(bool continueOnErrors);
    virtual bool loadTasks(bool continueOnErrors) { ACKNOWLEDGE_UNUSED(continueOnErrors); return true; }
    virtual bool loadPhysics(bool continueOnErrors);
    /// if singleStep is true, only the first model from the modelArray will be loaded.
    /// Useful for loading one model per frame
    virtual void loadXMLAssets(bool singleStep = false);
    virtual void loadDefaultCamera();

    virtual bool load(const stringImpl& name);
    Mesh_ptr loadModel(const FileData& data, bool addToSceneGraph);
    Object3D_ptr loadGeometry(const FileData& data, bool addToSceneGraph);
    virtual bool unload();
    virtual void postLoad();
    // gets called on the main thread when the scene finishes loading
    // used by the GUI system
    virtual void postLoadMainThread();
    /// Description in SceneManager
    virtual bool initializeAI(bool continueOnErrors);
    virtual bool deinitializeAI(bool continueOnErrors);
    /// Check if Scene::load() was called
    bool checkLoadFlag() const { return _loadComplete; }
    /// Unload scenegraph
    void clearObjects();
    /**End loading and unloading logic*/
    /// returns true if the camera was moved/rotated/etc
    bool updateCameraControls(PlayerIndex idx);
    /// Draw debug entities
    virtual void debugDraw(const Camera& activeCamera, const RenderStagePass& stagePass, GFX::CommandBuffer& bufferInOut);

    //Return true if input was consumed
    virtual bool mouseMoved(const Input::MouseEvent& arg);
    
    U8 getSceneIndexForPlayer(PlayerIndex idx) const;
    const Player_ptr& getPlayerForIndex(PlayerIndex idx) const;

    U8 getPlayerIndexForDevice(U8 deviceIndex) const;

    void addPlayerInternal(bool queue);
    void removePlayerInternal(PlayerIndex idx);
    void onPlayerAdd(const Player_ptr& player);
    void onPlayerRemove(const Player_ptr& player);

    /// simple function to load the scene elements.
    inline bool SCENE_LOAD(const stringImpl& name,
                           const bool contOnErrorRes,
                           const bool contOnErrorTasks) {
        if (!Scene::load(name)) {
            Console::errorfn(Locale::get(_ID("ERROR_SCENE_LOAD")), "scene load function");
            return false;
        }
        if (!loadResources(contOnErrorRes)) {
            Console::errorfn(Locale::get(_ID("ERROR_SCENE_LOAD")), "scene load resources");
            if (!contOnErrorRes) return false;
        }
        if (!loadTasks(contOnErrorTasks)) {
            Console::errorfn(Locale::get(_ID("ERROR_SCENE_LOAD")), "scene load tasks");
            if (!contOnErrorTasks) return false;
        }
        if (!loadPhysics(contOnErrorTasks)) {
            Console::errorfn(Locale::get(_ID("ERROR_SCENE_LOAD")), "scene load physics");
            if (!contOnErrorTasks) return false;
        }

        registerInputActions();
        loadKeyBindings();

        return true;
    }

    stringImpl getPlayerSGNName(PlayerIndex idx);

    void currentPlayerPass(PlayerIndex idx);

   protected:
       /// Global info
       SceneManager& _parent;
       ResourceCache& _resCache;
       ParamHandler&  _paramHandler;
       SceneGraph*    _sceneGraph;
       AI::AIManager* _aiManager;
       SceneGUIElements* _GUI;

       SceneGraphNode* _sun;

       vectorImpl<Player_ptr> _scenePlayers;
       U64 _sceneTimerUS;
       vectorImpl<D64> _taskTimers;
       vectorImpl<D64> _guiTimersMS;
       /// Datablocks for models,vegetation,terrains,tasks etc
       std::atomic_uint _loadingTasks;
       FileDataStack _modelDataArray;
       vectorImpl<Mesh_ptr> _vegetationDataArray;

       vectorImpl<std::shared_ptr<TerrainDescriptor>> _terrainInfoArray;
       F32 _LRSpeedFactor;
       /// Current selection
       hashMapImpl<PlayerIndex, vectorImpl<I64>> _currentSelection;
       hashMapImpl<PlayerIndex, I64> _currentHoverTarget;

       SceneGraphNode* _currentSky;
       hashMapImpl<PlayerIndex, SceneGraphNode*> _flashLight;
       hashMapImpl<PlayerIndex, U32> _cameraUpdateMap;
       /// Scene::load must be called by every scene. Add a load flag to make sure!
       bool _loadComplete;
       /// Schedule a scene graph parse with the physics engine to recreate/recheck
       /// the collision meshes used by each node
       bool _cookCollisionMeshesScheduled;

   private:
       SharedLock _tasksMutex;
       vectorImpl<TaskHandle> _tasks;
       /// Contains all game related info for the scene (wind speed, visibility ranges, etc)
       SceneState* _sceneState;
       vectorImpl<DELEGATE_CBK<void, U8 /*player index*/, SceneGraphNode* /*node*/> > _selectionChangeCallbacks;
       vectorImpl<I64> _sceneSelectionCandidates;
       std::unordered_set<PlayerIndex> _hoverUpdateQueue;

   protected:
       LightPool* _lightPool;
       SceneInput* _input;
       PhysicsSceneInterface* _pxScene;
       SceneEnvironmentProbePool* _envProbePool;

       IMPrimitive* _linesPrimitive;
       vectorImpl<IMPrimitive*> _octreePrimitives;
       vectorImpl<BoundingBox> _octreeBoundingBoxes;

       mutable std::mutex _perFrameArenaMutex;
       mutable MyArena<Config::REQUIRED_RAM_SIZE / 2> _perFrameArena;
};

namespace Attorney {
class SceneManager {
   private:
    static LightPool* lightPool(Scene& scene) {
        return scene._lightPool;
    }

    static bool updateCameraControls(Scene& scene, PlayerIndex idx) {
        return scene.updateCameraControls(idx);
    }

    static bool checkLoadFlag(const Scene& scene) {
        return scene.checkLoadFlag();
    }

    static bool deinitializeAI(Scene& scene) {
        return scene.deinitializeAI(true);
    }

    static void onPlayerAdd(Scene& scene, const Player_ptr& player) {
        scene.onPlayerAdd(player);
    }

    static void onPlayerRemove(Scene& scene, const Player_ptr& player) {
        scene.onPlayerRemove(player);
    }

    static void currentPlayerPass(Scene& scene, PlayerIndex idx) {
        scene.currentPlayerPass(idx);
    }

    /// Draw debug entities
    static void debugDraw(Scene& scene, const Camera& activeCamera, const RenderStagePass& stagePass, GFX::CommandBuffer& bufferInOut) {
        scene.debugDraw(activeCamera, stagePass, bufferInOut);
    }

    static bool frameStarted(Scene& scene) { return scene.frameStarted(); }
    static bool frameEnded(Scene& scene) { return scene.frameEnded(); }
    static bool load(Scene& scene, const stringImpl& name) {
        return scene.load(name);
    }
    static bool unload(Scene& scene) { 
        return scene.unload();
    }

    static void postLoad(Scene& scene) {
        scene.postLoad();
    }

    static void postLoadMainThread(Scene& scene) {
        scene.postLoadMainThread();
    }

    static void onSetActive(Scene& scene) {
        scene.onSetActive();
    }

    static void onRemoveActive(Scene& scene) {
        scene.onRemoveActive();
    }

    static bool onStartup() {
        return Scene::onStartup();
    }

    static bool onShutdown() {
        return Scene::onShutdown();
    }

    static SceneGUIElements* gui(Scene& scene) {
        return scene._GUI;
    }

    friend class Divide::SceneManager;
};

class SceneRenderPass {
 private:
    static SceneEnvironmentProbePool* getEnvProbes(Scene& scene) {
        return scene._envProbePool;
    }

    friend class Divide::RenderPass;
};

class SceneLoadSave {
 private:
    static bool save(const Scene& scene, ByteBuffer& outputBuffer) {
        return scene.save(outputBuffer);
    }

    static bool load(Scene& scene, ByteBuffer& inputBuffer) {
        return scene.load(inputBuffer);
    }

    friend class Divide::LoadSave;
};

class SceneGraph {
private:
    static void onNodeDestroy(Scene& scene, SceneGraphNode& node) {
        scene.onNodeDestroy(node);
    }
    friend class Divide::SceneGraph;
};

class SceneGUI {
private:
    static SceneGUIElements* guiElements(Scene& scene) {
        return scene._GUI;
    }

    friend class Divide::GUI;
};

class SceneInput {
private:
    static bool mouseMoved(Scene& scene, const Input::MouseEvent& arg) {
        return scene.mouseMoved(arg);
    }

    friend class Divide::SceneInput;
};

};  // namespace Attorney
};  // namespace Divide

#endif

Commits for Divide-Framework/trunk/Source Code/Scenes/Headers/Scene.h

Diff revisions: vs.
Revision Author Commited Message
1029 Diff Diff IonutCava picture IonutCava Tue 30 Jan, 2018 17:28:39 +0000

[Ionut]
- Update copyright notices

1028 Diff Diff IonutCava picture IonutCava Tue 30 Jan, 2018 17:26:19 +0000

[Ionut]
- Add some mouse position debug text
- Add the current scene name to the window’s title bar

1026 Diff Diff IonutCava picture IonutCava Mon 29 Jan, 2018 17:43:26 +0000

[Ionut]
- More work on editor:
— Initial implementation of component listing and editing

1025 Diff Diff IonutCava picture IonutCava Sun 28 Jan, 2018 22:32:29 +0000

[Ionut]
- Populate Solution Explorer with the list of scenes and the active scene’s scene graph hierarchy

1021 Diff Diff IonutCava picture IonutCava Sun 21 Jan, 2018 22:19:16 +0000

[Ionut]
- Performance optimizations

993 Diff Diff IonutCava picture IonutCava Sun 10 Dec, 2017 20:07:57 +0000

[Ionut]
- Finished initial port of the old scene graph system to the new ECS based system.
- Finished converting 2 components to the new ECS system: Transform and RigidBody
- Fixed the “Deploy” build error with the CEGUI OpenGL renderer.
- Compilation now needs C++14 support (it’s sufficiently wide-spread now)

989 Diff Diff IonutCava picture IonutCava Fri 01 Dec, 2017 16:10:10 +0000

[Ionut]
- Moved MainMenuBar out of PanelManager and modified it a bit
- Updated Imgui and addons to the latest version
- Added “Step 1 frame” and “Step 60 frames” buttons to the editor (when the simulation is paused, these advance time by the given number of frames only)
- Removed per-scene base camera and added a utility default camera, also used by the editor

969 Diff Diff IonutCava picture IonutCava Mon 13 Nov, 2017 00:15:57 +0000

[Ionut]
- Further improve DisplayWindow <-> Input relationship
- Add Flip-Y 2D camera (D3D style projection)
- Fix IMGUI rendering
- Add IMGUI sample and IMWINDOW sample rendering at the same time
- Improve IMWINDOW multi-window support
— Bug: nothing rendering in child platform windows
- Add draw command to move render context from one window to another

968 Diff Diff IonutCava picture IonutCava Tue 07 Nov, 2017 17:32:31 +0000

[Ionut]
- Consistent naming between timing variables

957 IonutCava picture IonutCava Tue 31 Oct, 2017 17:15:51 +0000

[Ionut]
- Some SGN node search refactoring
- typedef PlayerIndex