Simulant  21.12-1292
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
partitioner.h
1 /* * Copyright (c) 2011-2017 Luke Benstead https://simulant-engine.appspot.com
2  *
3  * This file is part of Simulant.
4  *
5  * Simulant is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Simulant is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with Simulant. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef PARTITIONER_H
20 #define PARTITIONER_H
21 
22 #include <memory>
23 #include <set>
24 #include <map>
25 #include <vector>
26 
27 #include "generic/containers/contiguous_map.h"
28 #include "generic/property.h"
29 #include "generic/managed.h"
30 #include "renderers/renderer.h"
31 #include "types.h"
32 #include "interfaces.h"
33 #include "nodes/stage_node.h"
34 
35 namespace smlt {
36 
37 class StageNode;
38 class SubActor;
39 
40 enum WriteOperation {
41  WRITE_OPERATION_ADD,
42  WRITE_OPERATION_UPDATE,
43  WRITE_OPERATION_REMOVE,
44  WRITE_OPERATION_MAX
45 };
46 
47 struct StagedWrite {
48  WriteOperation operation;
49  AABB new_bounds;
50  StageNode* node = nullptr;
51 };
52 
53 #define MAX_STAGED_WRITES 1024
54 
56  public RefCounted<Partitioner>,
57  public StageNode {
58 
59 public:
60  struct WriteSlots {
61  StagedWrite slot[WRITE_OPERATION_MAX];
62  uint8_t bits = 0;
63  };
64 
65  Partitioner(Scene* owner, StageNodeType node_type):
66  StageNode(owner, node_type) {}
67 
68  void add_stage_node(StageNode* node) {
69  StagedWrite write;
70  write.operation = WRITE_OPERATION_ADD;
71  write.node = node;
72  stage_write(node, write);
73  }
74 
75  void update_stage_node(StageNode* node, const AABB& bounds) {
76  StagedWrite write;
77  write.operation = WRITE_OPERATION_UPDATE;
78  write.new_bounds = bounds;
79  write.node = node;
80  stage_write(node, write);
81  }
82 
83  void remove_stage_node(StageNode* node) {
84  StagedWrite write;
85  write.operation = WRITE_OPERATION_REMOVE;
86  write.node = node;
87  stage_write(node, write);
88  }
89 
90  void _apply_writes();
91 
92 protected:
93  virtual void apply_staged_write(const StagedWrite& write) = 0;
94 
95  void stage_write(StageNode* node, const StagedWrite& op);
96 
97  /* Partitioners take over the responsibility of deciding which
98  * of their children gets rendered */
99  bool do_generates_renderables_for_descendents() const override {
100  return true;
101  }
102 
103 private:
104  thread::Mutex staging_lock_;
105 
106  ContiguousMap<StageNode*, std::vector<StagedWrite>> staged_writes_;
107  std::size_t write_count_ = 0;
108 };
109 
110 }
111 
112 namespace std {
113  DEFINE_ENUM_HASH(smlt::WriteOperation);
114 }
115 
116 #endif // PARTITIONER_H
smlt::StagedWrite
Definition: partitioner.h:47
smlt::Partitioner::WriteSlots
Definition: partitioner.h:60
smlt::Partitioner
Definition: partitioner.h:57
smlt::RefCounted
Definition: managed.h:71
smlt
Definition: animation.cpp:25
smlt::Scene
Definition: scene.h:94
smlt::AABB
Definition: aabb.h:22
smlt::StageNode
Definition: stage_node.h:442
std
Extensions to the C++ standard library.
Definition: variant.hpp:2752