forked from mikke89/RmlUi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRmlUi_Backend_SFML_GL2.cpp
278 lines (226 loc) · 7.95 KB
/
RmlUi_Backend_SFML_GL2.cpp
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
/*
* This source file is part of RmlUi, the HTML/CSS Interface Middleware
*
* For the latest information, see http://github.com/mikke89/RmlUi
*
* Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
* Copyright (c) 2019 The RmlUi Team, and contributors
*
* 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.
*
*/
#include "RmlUi_Backend.h"
#include "RmlUi_Platform_SFML.h"
#include "RmlUi_Renderer_GL2.h"
#include <RmlUi/Core/Context.h>
#include <RmlUi/Core/Core.h>
#include <RmlUi/Core/FileInterface.h>
#include <RmlUi/Core/Profiling.h>
#include <RmlUi/Debugger/Debugger.h>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
/**
Custom render interface example for the SFML/GL2 backend.
Overloads the OpenGL2 render interface to load textures through SFML's built-in texture loading functionality.
*/
class RenderInterface_GL2_SFML : public RenderInterface_GL2 {
public:
// -- Inherited from Rml::RenderInterface --
void RenderGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rml::TextureHandle texture,
const Rml::Vector2f& translation) override
{
if (texture)
{
sf::Texture::bind((sf::Texture*)texture);
texture = RenderInterface_GL2::TextureEnableWithoutBinding;
}
RenderInterface_GL2::RenderGeometry(vertices, num_vertices, indices, num_indices, texture, translation);
}
bool LoadTexture(Rml::TextureHandle& texture_handle, Rml::Vector2i& texture_dimensions, const Rml::String& source) override
{
Rml::FileInterface* file_interface = Rml::GetFileInterface();
Rml::FileHandle file_handle = file_interface->Open(source);
if (!file_handle)
return false;
file_interface->Seek(file_handle, 0, SEEK_END);
size_t buffer_size = file_interface->Tell(file_handle);
file_interface->Seek(file_handle, 0, SEEK_SET);
char* buffer = new char[buffer_size];
file_interface->Read(buffer, buffer_size, file_handle);
file_interface->Close(file_handle);
sf::Texture* texture = new sf::Texture();
texture->setSmooth(true);
bool success = texture->loadFromMemory(buffer, buffer_size);
delete[] buffer;
if (success)
{
texture_handle = (Rml::TextureHandle)texture;
texture_dimensions = Rml::Vector2i(texture->getSize().x, texture->getSize().y);
}
else
{
delete texture;
}
return success;
}
bool GenerateTexture(Rml::TextureHandle& texture_handle, const Rml::byte* source, const Rml::Vector2i& source_dimensions) override
{
sf::Texture* texture = new sf::Texture();
texture->setSmooth(true);
if (!texture->create(source_dimensions.x, source_dimensions.y))
{
delete texture;
return false;
}
texture->update(source, source_dimensions.x, source_dimensions.y, 0, 0);
texture_handle = (Rml::TextureHandle)texture;
return true;
}
void ReleaseTexture(Rml::TextureHandle texture_handle) override { delete (sf::Texture*)texture_handle; }
};
// Updates the viewport and context dimensions, should be called whenever the window size changes.
static void UpdateWindowDimensions(sf::RenderWindow& window, RenderInterface_GL2_SFML& render_interface, Rml::Context* context)
{
const int width = (int)window.getSize().x;
const int height = (int)window.getSize().y;
if (context)
context->SetDimensions(Rml::Vector2i(width, height));
sf::View view(sf::FloatRect(0.f, 0.f, (float)width, (float)height));
window.setView(view);
render_interface.SetViewport(width, height);
}
/**
Global data used by this backend.
Lifetime governed by the calls to Backend::Initialize() and Backend::Shutdown().
*/
struct BackendData {
SystemInterface_SFML system_interface;
RenderInterface_GL2_SFML render_interface;
sf::RenderWindow window;
bool running = true;
};
static Rml::UniquePtr<BackendData> data;
bool Backend::Initialize(const char* window_name, int width, int height, bool allow_resize)
{
RMLUI_ASSERT(!data);
data = Rml::MakeUnique<BackendData>();
// Create the window.
sf::RenderWindow out_window;
sf::ContextSettings context_settings;
context_settings.stencilBits = 8;
context_settings.antialiasingLevel = 2;
const sf::Uint32 style = (allow_resize ? sf::Style::Default : (sf::Style::Titlebar | sf::Style::Close));
data->window.create(sf::VideoMode(width, height), window_name, style, context_settings);
data->window.setVerticalSyncEnabled(true);
if (!data->window.isOpen())
{
data.reset();
return false;
}
// Optionally apply the SFML window to the system interface so that it can change its mouse cursor.
data->system_interface.SetWindow(&data->window);
UpdateWindowDimensions(data->window, data->render_interface, nullptr);
return true;
}
void Backend::Shutdown()
{
data.reset();
}
Rml::SystemInterface* Backend::GetSystemInterface()
{
RMLUI_ASSERT(data);
return &data->system_interface;
}
Rml::RenderInterface* Backend::GetRenderInterface()
{
RMLUI_ASSERT(data);
return &data->render_interface;
}
bool Backend::ProcessEvents(Rml::Context* context, KeyDownCallback key_down_callback)
{
RMLUI_ASSERT(data && context);
// The contents of this function is intended to be copied directly into your main loop.
bool result = data->running;
sf::Event ev;
while (data->window.pollEvent(ev))
{
switch (ev.type)
{
case sf::Event::Resized:
UpdateWindowDimensions(data->window, data->render_interface, context);
break;
case sf::Event::KeyPressed:
{
const Rml::Input::KeyIdentifier key = RmlSFML::ConvertKey(ev.key.code);
const int key_modifier = RmlSFML::GetKeyModifierState();
const float native_dp_ratio = 1.f;
// See if we have any global shortcuts that take priority over the context.
if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, true))
break;
// Otherwise, hand the event over to the context by calling the input handler as normal.
if (!RmlSFML::InputHandler(context, ev))
break;
// The key was not consumed by the context either, try keyboard shortcuts of lower priority.
if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, false))
break;
}
break;
case sf::Event::Closed:
result = false;
break;
default:
RmlSFML::InputHandler(context, ev);
break;
}
}
return result;
}
void Backend::RequestExit()
{
RMLUI_ASSERT(data);
data->running = false;
}
void Backend::BeginFrame()
{
RMLUI_ASSERT(data);
sf::RenderWindow& window = data->window;
window.resetGLStates();
window.clear();
data->render_interface.BeginFrame();
#if 0
// Draw a simple shape with SFML for demonstration purposes. Make sure to push and pop GL states as appropriate.
sf::Vector2f circle_position(100.f, 100.f);
window.pushGLStates();
sf::CircleShape circle(50.f);
circle.setPosition(circle_position);
circle.setFillColor(sf::Color::Blue);
circle.setOutlineColor(sf::Color::Red);
circle.setOutlineThickness(10.f);
window.draw(circle);
window.popGLStates();
#endif
}
void Backend::PresentFrame()
{
RMLUI_ASSERT(data);
data->render_interface.EndFrame();
data->window.display();
// Optional, used to mark frames during performance profiling.
RMLUI_FrameMark;
}