Skip to content

Commit 37714b2

Browse files
committed
added support for cameras to SfSystem + renamed MetaComponent to GraphicsComponent
1 parent d788e0b commit 37714b2

File tree

13 files changed

+85
-57
lines changed

13 files changed

+85
-57
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ These are pre-built, extensible and pluggable elements that can be used in any p
3131
##### Components
3232

3333
* [LuaComponent](common/components/LuaComponent.md): defines the lua scripts to be run by the `LuaSystem` for a `GameObject`
34-
* [MetaComponent](common/components/MetaComponent.md): provides metadata about a `GameObject`, such as its appearance, used by the `SfSystem`
34+
* [GraphicsComponent](common/components/GraphicsComponent.md): provides graphical information about a `GameObject`, such as its appearance, used by the `SfSystem`
3535
* [TransformComponent](common/components/TransformComponent.md): defines a `GameObject`'s position and size
3636
* [PhysicsComponent](common/components/PhysicsComponent.md): defines a `GameObject`'s movement
3737
* [PathfinderComponent](common/components/PathfinderComponent.md): defines a `GameObject`'s pathfinding information
@@ -83,7 +83,7 @@ Below is a commented main function that creates an entity and attaches some comp
8383

8484
#include "common/systems/LuaSystem.hpp"
8585
#include "common/systems/LogSystem.hpp"
86-
#include "common/components/MetaComponent.hpp"
86+
#include "common/components/GraphicsComponent.hpp"
8787
#include "common/components/TransformComponent.hpp"
8888

8989
int main(int, char **av)
@@ -110,7 +110,7 @@ int main(int, char **av)
110110
// Create a GameObject and attach Components to it
111111
auto &player = em.createEntity<kengine::GameObject>("player");
112112
player.attachComponent<kengine::TransformComponent3d>();
113-
player.attachComponent<kengine::MetaComponent>();
113+
player.attachComponent<kengine::GraphicsComponent>();
114114

115115
// Attach a lua script to a GameObject
116116
auto &luaComp = player.attachComponent<kengine::LuaComponent>();
@@ -122,7 +122,7 @@ int main(int, char **av)
122122
auto &lua = em.getSystem<kengine::LuaSystem>();
123123
lua.addScriptDirectory("scripts"); // The LuaSystem automatically opens the "scripts" directory, this is just an example
124124
lua.registerTypes<
125-
kengine::MetaComponent,
125+
kengine::GraphicsComponent,
126126
kengine::TransformComponent3d, putils::Point<double, 3>, putils::Rect<double, 3>,
127127
kengine::LuaComponent,
128128
kengine::packets::Log
@@ -157,7 +157,7 @@ local new = createEntity("GameObject", "bob",
157157
local otherRef = getEntity("bob")
158158
159159
-- attach a component
160-
local meta = new:attachMetaComponent()
160+
local meta = new:attachGraphicsComponent()
161161
meta.appearance = "human"
162162
163163
local transform = new:attachTransformComponent()
@@ -172,7 +172,7 @@ for i, e in ipairs(getGameObjects()) do
172172
end
173173
174174
-- iterate over specific entities
175-
for i, e in ipairs(getGameObjectsWithMetaComponent()) do
175+
for i, e in ipairs(getGameObjectsWithGraphicsComponent()) do
176176
local comp = e:getTransformComponent()
177177
print(comp)
178178
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include "SerializableComponent.hpp"
5+
6+
namespace kengine {
7+
class GraphicsComponent : public kengine::SerializableComponent<GraphicsComponent>,
8+
public putils::Reflectible<GraphicsComponent> {
9+
public:
10+
GraphicsComponent(std::string_view appearance = "")
11+
: appearance(appearance) {}
12+
13+
const std::string type = pmeta_nameof(GraphicsComponent);
14+
std::string appearance;
15+
16+
/*
17+
* Reflectible
18+
*/
19+
20+
public:
21+
pmeta_get_class_name(GraphicsComponent);
22+
pmeta_get_attributes(
23+
pmeta_reflectible_attribute(&GraphicsComponent::type),
24+
pmeta_reflectible_attribute(&GraphicsComponent::appearance)
25+
);
26+
};
27+
}

common/components/MetaComponent.md renamed to common/components/GraphicsComponent.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [MetaComponent](MetaComponent.hpp)
1+
# [GraphicsComponent](GraphicsComponent.hpp)
22

33
`Component` providing metadata about a `GameObject`, such as its appearance.
44

common/components/MetaComponent.hpp

Lines changed: 0 additions & 27 deletions
This file was deleted.

common/systems/LuaSystem.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ This also defines the following function for lua scripts:
4343
* `sendT(T packet)` (e.g. `sendLog(Log packet)`): sends `packet` as a datapacket to all `Systems`
4444

4545
If `T` is a `Component`, the following member functions is added to the `GameObject` lua type for lua scripts:
46-
* `getT()` (e.g. `getMetaComponent()`)
47-
* `hasT()` (e.g. `hasMetaComponent()`)
48-
* `attachT()` (e.g. `attachMetaComponent()`)
49-
* `detachT()` (e.g. `detachMetaComponent()`)
46+
* `getT()` (e.g. `getGraphicsComponent()`)
47+
* `hasT()` (e.g. `hasGraphicsComponent()`)
48+
* `attachT()` (e.g. `attachGraphicsComponent()`)
49+
* `detachT()` (e.g. `detachGraphicsComponent()`)
5050

5151
A global `getGameObjectsWithT()` is also defined, that returns all the `GameObjects` with a `T` component.
5252

common/systems/ogre/OgreSystem.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "OgreTextComponent.hpp"
1010

1111
#include "common/components/TransformComponent.hpp"
12-
#include "common/components/MetaComponent.hpp"
12+
#include "common/components/GraphicsComponent.hpp"
1313
#include "common/components/CameraComponent.hpp"
1414
#include "common/components/GUIComponent.hpp"
1515

@@ -159,7 +159,7 @@ void OgreSystem::execute()
159159

160160
void OgreSystem::registerGameObject(kengine::GameObject &go)
161161
{
162-
if (!go.hasComponent<kengine::MetaComponent>() && !go.hasComponent<kengine::CameraComponent3d>() && !go.hasComponent<kengine::GUIComponent>())
162+
if (!go.hasComponent<kengine::GraphicsComponent>() && !go.hasComponent<kengine::CameraComponent3d>() && !go.hasComponent<kengine::GUIComponent>())
163163
return;
164164
ToSpawnLock _(_toSpawnMutex);
165165
_toSpawn.push_back(&go);
@@ -266,7 +266,7 @@ void OgreSystem::createEntity(kengine::GameObject &go) noexcept
266266
return;
267267
}
268268

269-
const auto &appearance = go.getComponent<kengine::MetaComponent>().appearance;
269+
const auto &appearance = go.getComponent<kengine::GraphicsComponent>().appearance;
270270

271271
if (appearance == "light")
272272
{

common/systems/ogre/OgreSystem.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
##### 'Normal' objects
1010

11-
`OgreSystem` reads the mesh to be used for a `GameObject` from its [MetaComponent](../../components/MetaComponent.md)'s `appearance` property.
11+
`OgreSystem` reads the mesh to be used for a `GameObject` from its [GraphicsComponent](../../components/GraphicsComponent.md)'s `appearance` property.
1212

1313
If `appearance` is *"light"*, `OgreSystem` will spawn a `PointLight` instead of a mesh.
1414

common/systems/sfml/SfSystem.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "common/systems/LuaSystem.hpp"
88
#include "common/components/GUIComponent.hpp"
9+
#include "common/components/CameraComponent.hpp"
910

1011
EXPORT kengine::ISystem * getSystem(kengine::EntityManager & em) {
1112
return new kengine::SfSystem(em);
@@ -97,6 +98,23 @@ namespace kengine {
9798
*/
9899

99100
void SfSystem::execute() {
101+
for (auto go : _em.getGameObjects<kengine::CameraComponent3d>()) {
102+
auto & view = _engine.getView(go->getName());
103+
104+
const auto & frustrum = go->getComponent<kengine::CameraComponent3d>().frustrum;
105+
view.setCenter(
106+
(float)(frustrum.topLeft.x + frustrum.size.x / 2) * _tileSize.x,
107+
(float)(frustrum.topLeft.z + frustrum.size.z / 2) * _tileSize.y
108+
);
109+
view.setSize((float)frustrum.size.x * _tileSize.x, (float)frustrum.size.z * _tileSize.y);
110+
111+
const auto & box = go->getComponent<kengine::TransformComponent3d>().boundingBox;
112+
view.setViewport(sf::FloatRect{
113+
(float)box.topLeft.x, (float)box.topLeft.z,
114+
(float)box.size.x, (float)box.size.z
115+
});
116+
_engine.setViewHeight(go->getName(), (size_t)box.topLeft.y);
117+
}
100118

101119
// Update positions
102120
for (auto go : _em.getGameObjects<SfComponent>()) {
@@ -129,7 +147,6 @@ namespace kengine {
129147
}
130148

131149
void SfSystem::handleEvents() noexcept {
132-
// Event handling
133150
static const std::unordered_map<sf::Event::EventType, std::function<void(const sf::Event &)>> handlers {
134151
{
135152
{
@@ -212,7 +229,7 @@ namespace kengine {
212229

213230
void SfSystem::handle(const kengine::packets::RegisterGameObject & p) {
214231
auto & go = p.go;
215-
if (!go.hasComponent<SfComponent>() && !go.hasComponent<MetaComponent>() &&
232+
if (!go.hasComponent<SfComponent>() && !go.hasComponent<GraphicsComponent>() &&
216233
!go.hasComponent<kengine::GUIComponent>())
217234
return;
218235

@@ -238,14 +255,17 @@ namespace kengine {
238255
}
239256
catch (const std::exception & e) {
240257
send(kengine::packets::Log{
241-
putils::concat("[SfSystem] Unknown appearance: ", go.getComponent<MetaComponent>().appearance)
258+
putils::concat("[SfSystem] Unknown appearance: ", go.getComponent<GraphicsComponent>().appearance)
242259
});
243260
}
244261
}
245262

246263
void SfSystem::handle(const kengine::packets::RemoveGameObject & p) {
247264
auto & go = p.go;
248265

266+
if (go.hasComponent<kengine::CameraComponent3d>() && _engine.hasView(go.getName()))
267+
_engine.removeView(go.getName());
268+
249269
if (!go.hasComponent<SfComponent>())
250270
return;
251271

@@ -300,16 +320,14 @@ namespace kengine {
300320
return comp;
301321
}
302322

303-
const auto & meta = go.getComponent<MetaComponent>();
323+
const auto & meta = go.getComponent<GraphicsComponent>();
304324

305325
auto str = _appearances.find(meta.appearance) != _appearances.end()
306326
? _appearances.at(meta.appearance)
307327
: meta.appearance;
308328

309329
auto & comp = go.attachComponent<SfComponent>(
310-
std::make_unique<pse::Sprite>(str,
311-
sf::Vector2f{ 0, 0 },
312-
sf::Vector2f{ 16, 16 })
330+
std::make_unique<pse::Sprite>(str, sf::Vector2f{ 0, 0 }, sf::Vector2f{ 16, 16 })
313331
);
314332

315333
return comp;

common/systems/sfml/SfSystem.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <SDL2/SDL.h>
44
#include "System.hpp"
5-
#include "common/components/MetaComponent.hpp"
5+
#include "common/components/GraphicsComponent.hpp"
66
#include "common/packets/RegisterAppearance.hpp"
77
#include "common/packets/Input.hpp"
88
#include "common/packets/RemoveGameObject.hpp"

common/systems/sfml/SfSystem.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
##### 'Normal' objects
1010

11-
`SfSystem` reads the resource file to be used for a `GameObject` from its [MetaComponent](../../components/MetaComponent.md)'s `appearance` property.
11+
`SfSystem` reads the resource file to be used for a `GameObject` from its [GraphicsComponent](../../components/GraphicsComponent.md)'s `appearance` property.
1212

1313
If `appearance` was previously registered as an abstract appearance through a [RegisterAppearance](../../packets/RegisterAppearance.hpp) datapacket, the resource file that was associated to it is loaded instead.
1414

@@ -18,6 +18,16 @@ If `appearance` was previously registered as an abstract appearance through a [R
1818
/!\ That 3d is important! TransformComponent2d, 2i, 3i, 3f... Will not be detected!
1919
```
2020

21+
##### Cameras
22+
23+
A *"default"* camera is added upon system construction, meaning typical use does not require any action. For further configuration of the rendered areas, `CameraComponents` can be used.
24+
25+
If a `GameObject` is found to have a [CameraComponent](../../components/CameraComponent.hpp), a camera will be added to the scene.
26+
27+
The `CameraComponent`'s `frustrum` property defines the area to be rendered, whereas the `GameObject`'s `TransformComponent3d`'s `boundingBox` property defines the bounds of the viewport to be displayed.
28+
29+
Refer to [the SFML website](https://www.sfml-dev.org/tutorials/2.0/graphics-view.php) for more information about viewports.
30+
2131
##### GUI
2232

2333
If a `GameObject` is found to have a [GUIComponent](../../components/GUIComponent.md), it will be rendered as text using the information held in that `GUIComponent`.

0 commit comments

Comments
 (0)