r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
settings.cpp
Go to the documentation of this file.
2#include "Client/Common.hpp"
3#include "ECS/Component.hpp"
5
6static constexpr eng::Color WHITE = {.r = 255U, .g = 255U, .b = 255U, .a = 255U};
7
8cli::Settings::Settings(const std::shared_ptr<eng::IRenderer> &renderer, const std::shared_ptr<eng::IAudio> &audio)
9 : m_audio(audio)
10{
11 auto &registry = AScene::getRegistry();
12
13 registry.onComponentAdded(
14 [&renderer, &audio, &registry](const ecs::Entity e, const std::type_info &type)
15 {
16 const auto *audioComp = registry.getComponent<ecs::Audio>(e);
17 const auto *colorComp = registry.getComponent<ecs::Color>(e);
18 const auto *fontComp = registry.getComponent<ecs::Font>(e);
19 const auto *rectComp = registry.getComponent<ecs::Rect>(e);
20 const auto *scaleComp = registry.getComponent<ecs::Scale>(e);
21 const auto *textComp = registry.getComponent<ecs::Text>(e);
22 const auto *textureComp = registry.getComponent<ecs::Texture>(e);
23 const auto *transform = registry.getComponent<ecs::Transform>(e);
24
25 if (type == typeid(ecs::Text))
26 {
27 if (textComp && transform && fontComp)
28 {
29 renderer->createFont(fontComp->id, fontComp->path);
30 renderer->createText(
31 {.font_name = fontComp->id,
32 .color = {.r = colorComp->r, .g = colorComp->g, .b = colorComp->b, .a = colorComp->a},
33 .content = textComp->content,
34 .size = textComp->font_size,
35 .x = transform->x,
36 .y = transform->y,
37 .name = textComp->id});
38 }
39 }
40 else if (type == typeid(ecs::Texture))
41 {
42 const float scale_x = scaleComp ? scaleComp->x : 1.F;
43 const float scale_y = scaleComp ? scaleComp->y : 1.F;
44
45 renderer->createTexture(textureComp->id, textureComp->path);
46
47 if (transform && textureComp)
48 {
49 if (rectComp)
50 {
51 renderer->createSprite(textureComp->id + std::to_string(e), textureComp->id, transform->x,
52 transform->y, scale_x, scale_y, static_cast<int>(rectComp->pos_x),
53 static_cast<int>(rectComp->pos_y), rectComp->size_x, rectComp->size_y);
54 }
55 else
56 {
57 renderer->createSprite(textureComp->id + std::to_string(e), textureComp->id, transform->x,
58 transform->y);
59 }
60 }
61 }
62 else if (type == typeid(ecs::Audio))
63 {
64 if (audioComp)
65 {
66 audio->createAudio(audioComp->path, audioComp->volume, audioComp->loop,
67 audioComp->id + std::to_string(e));
68 }
69 }
70 });
71
72 registry.createEntity().with<ecs::Audio>("id_audio", Path::Audio::AUDIO_TITLE, 5.F, true, true).build();
73 registry.createEntity()
74 .with<ecs::Font>("main_font", Path::Font::FONTS_RTYPE)
75 .with<ecs::Transform>("transform_title", 10.F, 10.F, 0.F)
76 .with<ecs::Color>("color_title", WHITE.r, WHITE.g, WHITE.b, WHITE.a)
77 .with<ecs::Text>("id", std::string("RType Client"), 50U)
78 .build();
79}
80
81void cli::Settings::update(const float dt, const eng::WindowSize &size)
82{
83 auto &reg = getRegistry();
84
85 auto &transforms = reg.getAll<ecs::Transform>();
86 auto &colors = reg.getAll<ecs::Color>();
87 auto &texts = reg.getAll<ecs::Text>();
88 auto &audios = reg.getAll<ecs::Audio>();
89
90 for (auto &audio : audios)
91 {
92 if (!audio.second.play && (m_audio->isPlaying(audio.second.id) == eng::Status::Playing))
93 {
94 m_audio->stopAudio(audio.second.id);
95 }
96 }
97}
98
100{
101 switch (event.type)
102 {
104 if (event.key == eng::Key::Escape)
105 onLeave();
106 break;
107
108 default:
109 break;
110 }
111}
This file contains the component definitions.
This file contains the Audio interface.
This file contains the settings scene.
void update(float dt, const eng::WindowSize &size) override
Definition settings.cpp:81
void event(const eng::Event &event) override
Definition settings.cpp:99
Settings(const std::shared_ptr< eng::IRenderer > &renderer, const std::shared_ptr< eng::IAudio > &audio)
Definition settings.cpp:8
This file contains common definitions and constants.
static constexpr eng::Color WHITE
constexpr auto AUDIO_TITLE
Definition Common.hpp:44
constexpr auto FONTS_RTYPE
Definition Common.hpp:50
std::uint32_t Entity
Definition Entity.hpp:13
static constexpr eng::Color WHITE
Definition settings.cpp:6
std::string id
Definition Component.hpp:15
unsigned char r
Definition IRenderer.hpp:17
unsigned char a
Definition IRenderer.hpp:20
unsigned char g
Definition IRenderer.hpp:18
unsigned char b
Definition IRenderer.hpp:19
EventType type
Definition IRenderer.hpp:90