P-E-R-R-Y
Utilitaires, contrats et vendors du moteur, en une seule doc
Loading...
Searching...
No Matches
RayWindow.hpp
Go to the documentation of this file.
1
10
11#ifndef RAYWINDOW_HPP_
12#define RAYWINDOW_HPP_
13
14//Raylib (without conflict)
15#include "RaylibAliases.hpp"
16
17//Interface
18#include "IWindow3.hpp"
19
20//encapsulation
21#include "RayCamera.hpp"
22#include "RayModel.hpp"
23#include "RayPolygon.hpp"
24#include "RaySprite.hpp"
25#include "RayText.hpp"
26
38
39 public:
40 RayWindow(int32_t screenWidth, int32_t screenHeight, std::string title) : _quitRequested(false) {
41 raylib::InitWindow(screenWidth, screenHeight, title.c_str());
42 }
43
45 /* Le curseur de raylib est GLOBAL au processus, pas attache a
46 * la fenetre : partir en le laissant cache priverait de pointeur
47 * la borne, et le bureau avec elle. */
48 raylib::ShowCursor();
49 raylib::CloseWindow();
50 }
51
52 //lifecycle
53 bool isOpen() override {
54 return raylib::IsWindowReady() && !raylib::WindowShouldClose() && !_quitRequested;
55 }
56
57 void close() override {
58 _quitRequested = true;
59 }
60
61 Vector2f getPosition() override {
62 const raylib::Vector2 position = raylib::GetWindowPosition();
63 return {static_cast<double>(position.x), static_cast<double>(position.y)};
64 }
65
66 void setPosition(Vector2f position) override {
67 raylib::SetWindowPosition(static_cast<int>(position.x), static_cast<int>(position.y));
68 }
69
70 Vector2f getSize() override {
71 return {static_cast<double>(raylib::GetScreenWidth()),
72 static_cast<double>(raylib::GetScreenHeight())};
73 }
74
75 void setSize(Vector2f size) override {
76 raylib::SetWindowSize(static_cast<int>(size.x), static_cast<int>(size.y));
77 }
78
79 void setFrameLimit(int32_t limit) override {
80 raylib::SetTargetFPS(limit);
81 }
82
83 void setMouseVisibility(bool visible) override {
84 if (visible)
85 raylib::ShowCursor();
86 else
87 raylib::HideCursor();
88 }
89
90 int32_t getDelta() override {
91 return static_cast<int32_t>(raylib::GetFrameTime() * 1000);
92 }
93
106 bool pollEvent() override {
107 return true;
108 }
109
110 void eventClose() override {
111 //no-op : the close condition is already checked in isOpen()
112 }
113
114 //2D
115 void beginDraw() override {
116 raylib::BeginDrawing();
117 raylib::ClearBackground({0, 0, 0, 255});
118 }
119
120 void endDraw() override {
121 raylib::EndDrawing();
122 }
123
124 void drawPoly(graphic::IPolygon *polygon) override;
125 void drawSprite(graphic::ISprite *sprite) override;
126 void drawText(graphic::IText *text) override;
127
128 //3D
129 void beginMode3(graphic::ICamera *camera) override;
130 void drawModel(graphic::IModel *model) override;
131 void drawText3D(graphic::IText *text, Vector3f position) override;
132 void endMode3() override;
133
134 private:
135 bool _quitRequested;
136 raylib::Camera *_activeCamera = nullptr;
137};
138
139//2D
140
142 RayPolygon *rayPolygon = static_cast<RayPolygon *>(polygon);
143 // by reference : copying the vector every frame would be a per-frame
144 // allocation for nothing, the triangles are computed at construction
145 const std::vector<Triangle<double>> &triangles = rayPolygon->_triangles;
146
147 for (size_t i = 0; i < triangles.size(); i++) {
148 //raylib asks for counter clockwise, hence the reversed order
149 raylib::DrawTriangle(
150 {static_cast<float>(triangles[i].p3.x + rayPolygon->getPosition().x), static_cast<float>(triangles[i].p3.y + rayPolygon->getPosition().y)},
151 {static_cast<float>(triangles[i].p2.x + rayPolygon->getPosition().x), static_cast<float>(triangles[i].p2.y + rayPolygon->getPosition().y)},
152 {static_cast<float>(triangles[i].p1.x + rayPolygon->getPosition().x), static_cast<float>(triangles[i].p1.y + rayPolygon->getPosition().y)},
153 rayPolygon->_color);
154 }
155}
156
158 RaySprite *raySprite = static_cast<RaySprite *>(sprite);
159 raylib::Rectangle posSize = {float(raySprite->getPosition().x), float(raySprite->getPosition().y),
160 float(raySprite->getSize().x), float(raySprite->getSize().y)};
161
162 raylib::DrawTexturePro(raySprite->_texture.handle(), raySprite->_crop, posSize, {0, 0}, raySprite->_rotation,
163 {255, 255, 255, 255});
164}
165
167 RayText *rayText = static_cast<RayText *>(text);
168
169 raylib::DrawTextPro(rayText->_font->handle(), rayText->_data.c_str(), rayText->_position, {0, 0},
170 rayText->_rotation, rayText->_size, rayText->_spacing, rayText->_color);
171}
172
173//3D
174
176 RayCamera *rayCamera = static_cast<RayCamera *>(camera);
177
178 raylib::UpdateCamera(&rayCamera->_camera, rayCamera->_mode);
179 _activeCamera = &rayCamera->_camera;
180
181 raylib::BeginMode3D(rayCamera->_camera);
182}
183
185 RayModel *rayModel = static_cast<RayModel *>(model);
186
187 raylib::DrawModelEx(rayModel->_mesh.handle(), rayModel->_position, {0, 1, 0}, 0, rayModel->_scale, raylib::WHITE);
188}
189
191 //raylib has no built-in DrawText3D : project the world position through
192 //the active camera and draw the same 2D text there (screen-space billboard).
193 //2D draw calls need orthographic mode, so step out of BeginMode3D and
194 //back in around the projected draw - the caller's own endMode3() closes
195 //the block they think they're still in.
196 RayText *rayText = static_cast<RayText *>(text);
197 if (!_activeCamera) return;
198
199 raylib::Vector3 worldPos = {float(position.x), float(position.y), float(position.z)};
200 raylib::Vector2 screenPos = raylib::GetWorldToScreen(worldPos, *_activeCamera);
201
202 raylib::EndMode3D();
203 raylib::DrawTextPro(rayText->_font->handle(), rayText->_data.c_str(), screenPos, {0, 0}, rayText->_rotation,
204 rayText->_size, rayText->_spacing, rayText->_color);
205 raylib::BeginMode3D(*_activeCamera);
206}
207
209 raylib::EndMode3D();
210 _activeCamera = nullptr;
211}
212
214
215#endif /* !RAYWINDOW_HPP_ */
Raylib Camera class.
Definition RayCamera.hpp:23
raylib::Font handle() const
Definition RayFont.hpp:39
raylib::Model & handle()
Definition RayMesh.hpp:42
Raylib Model class - references a RayMesh, does not own it.
Definition RayModel.hpp:47
Definition RayPolygon.hpp:25
Vector2f getPosition() const override
Get the Position object.
Definition RayPolygon.hpp:45
Raylib Sprite class - references a RayTexture, does not own it. Deleting the sprite never touches the...
Definition RaySprite.hpp:27
Vector2f getSize() const override
Get the Sprite size.
Definition RaySprite.hpp:67
Vector2f getPosition() const override
Get the Position object.
Definition RaySprite.hpp:52
Raylib Text class - references a RayFont, does not own it. Deleting the text never touches the font.
Definition RayText.hpp:30
raylib::Texture2D handle() const
Definition RayTexture.hpp:45
~RayWindow()
Definition RayWindow.hpp:44
void setPosition(Vector2f position) override
Move the window on the desktop.
Definition RayWindow.hpp:66
RayWindow(int32_t screenWidth, int32_t screenHeight, std::string title)
Definition RayWindow.hpp:40
void endDraw() override
end the 2D drawing
Definition RayWindow.hpp:120
void setSize(Vector2f size) override
Resize the drawable area.
Definition RayWindow.hpp:75
void eventClose() override
notice the window that a close was requested
Definition RayWindow.hpp:110
Vector2f getSize() override
The drawable area, in pixels - NOT the desktop size. A hit-test built on IMouse::getPosition() compar...
Definition RayWindow.hpp:70
void beginDraw() override
allowing to draw 2D on the window
Definition RayWindow.hpp:115
void setFrameLimit(int32_t limit) override
Set the Frame Limit object.
Definition RayWindow.hpp:79
int32_t getDelta() override
Get the Delta object.
Definition RayWindow.hpp:90
bool isOpen() override
notice if the window is open
Definition RayWindow.hpp:53
void close() override
close the window
Definition RayWindow.hpp:57
void setMouseVisibility(bool visible) override
Show or hide the system cursor over THIS window.
Definition RayWindow.hpp:83
Vector2f getPosition() override
Where the window sits on the desktop, in pixels, origin top-left of the primary screen.
Definition RayWindow.hpp:61
bool pollEvent() override
raylib has NO event queue : only state queries, refreshed once per frame by PollInputEvents() inside ...
Definition RayWindow.hpp:106
Camera interface.
Definition ICamera.hpp:23
Model interface.
Definition IModel.hpp:27
Polygon interface.
Definition IPolygon.hpp:34
Sprite interface.
Definition ISprite.hpp:22
Text interface.
Definition IText.hpp:26
A window that can also draw 3D. Extends IWindow2, so anything that only needs 2D can keep using it as...
Definition IWindow3.hpp:25
void drawPoly(graphic::IPolygon *polygon) override
draw a polygon
Definition RayWindow.hpp:141
void beginMode3(graphic::ICamera *camera) override
allowing to draw 3D on the window
Definition RayWindow.hpp:175
void drawModel(graphic::IModel *model) override
draw a model
Definition RayWindow.hpp:184
void endMode3() override
end the 3D drawing
Definition RayWindow.hpp:208
void drawSprite(graphic::ISprite *sprite) override
draw a sprite
Definition RayWindow.hpp:157
void drawText3D(graphic::IText *text, Vector3f position) override
draw text at a 3D world position - the same IText used by drawText(), just projected through the acti...
Definition RayWindow.hpp:190
void drawText(graphic::IText *text) override
draw a text
Definition RayWindow.hpp:166
Vector2< double > Vector2f
Definition Vector2.hpp:112
Vector3< double > Vector3f
Definition Vector3.hpp:121
T y
Definition Vector2.hpp:21
T x
Definition Vector2.hpp:21
T x
Definition Vector3.hpp:21
T y
Definition Vector3.hpp:21
T z
Definition Vector3.hpp:21