File: RocketRenderingInterface.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (351 lines) | stat: -rw-r--r-- 10,629 bytes parent folder | download | duplicates (2)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//
//

// Undefine stupid windows API defines that get included by the prefix headers
#ifdef GetNextSibling
#undef GetNextSibling
#endif

#ifdef GetFirstChild
#undef GetFirstChild
#endif

// Our Assert conflicts with the definitions inside libRocket
#pragma push_macro("Assert")
#undef Assert

#include "RocketRenderingInterface.h"
#include "RocketFileInterface.h"

#pragma pop_macro("Assert")

#include "graphics/2d.h"
#include "graphics/generic.h"
#include "graphics/material.h"
#include "mod_table/mod_table.h"
#include "tracing/categories.h"
#include "tracing/tracing.h"
#define BMPMAN_INTERNAL
#include "bmpman/bm_internal.h"

using namespace Rocket::Core;

namespace scpui {

RocketRenderingInterface::RocketRenderingInterface()
{
	renderOffset.x = 0.0f;
	renderOffset.y = 0.0f;

	vertex_stream_buffer = gr_create_buffer(BufferType::Vertex, BufferUsageHint::Streaming);
	index_stream_buffer  = gr_create_buffer(BufferType::Index, BufferUsageHint::Streaming);

	layout.add_vertex_component(vertex_format_data::POSITION2, sizeof(Vertex), offsetof(Vertex, position));
	layout.add_vertex_component(vertex_format_data::COLOR4, sizeof(Vertex), offsetof(Vertex, colour));
	layout.add_vertex_component(vertex_format_data::TEX_COORD2, sizeof(Vertex), offsetof(Vertex, tex_coord));
}

RocketRenderingInterface::~RocketRenderingInterface()
{
	if (vertex_stream_buffer.isValid()) {
		gr_delete_buffer(vertex_stream_buffer);
	}
	if (index_stream_buffer.isValid()) {
		gr_delete_buffer(index_stream_buffer);
	}
}
RocketRenderingInterface::Texture* RocketRenderingInterface::get_texture(Rocket::Core::TextureHandle handle)
{
	return reinterpret_cast<Texture*>(handle);
}
Rocket::Core::TextureHandle RocketRenderingInterface::get_texture_handle(RocketRenderingInterface::Texture* bitmap)
{
	return reinterpret_cast<Rocket::Core::TextureHandle>(bitmap);
}

CompiledGeometryHandle RocketRenderingInterface::CompileGeometry(Vertex* vertices, int num_vertices, int* indices,
                                                                 int num_indices, TextureHandle texture)
{
	TRACE_SCOPE(tracing::RocketCompileGeometry);
	GR_DEBUG_SCOPE("libRocket::CompileGeometry");

	auto* geom          = new CompiledGeometry();
	geom->vertex_buffer = gr_create_buffer(BufferType::Vertex, BufferUsageHint::Static);
	gr_update_buffer_data(geom->vertex_buffer, num_vertices * sizeof(Vertex), reinterpret_cast<void*>(vertices));

	geom->index_buffer = gr_create_buffer(BufferType::Index, BufferUsageHint::Static);
	gr_update_buffer_data(geom->index_buffer, num_indices * sizeof(int), reinterpret_cast<void*>(indices));
	geom->num_elements = num_indices;

	geom->texture = get_texture(texture);

	return reinterpret_cast<CompiledGeometryHandle>(geom);
}
void RocketRenderingInterface::RenderCompiledGeometry(CompiledGeometryHandle geometry, const Vector2f& translation)
{
	TRACE_SCOPE(tracing::RocketRenderCompiledGeometry);
	GR_DEBUG_SCOPE("libRocket::RenderCompiledGeometry");

	auto geom = reinterpret_cast<CompiledGeometry*>(geometry);

	auto bitmap = -1;
	if (geom->texture != nullptr) {
		if (geom->texture->is_animation) {
			bitmap = geom->texture->animation.bitmap_id;
		} else {
			bitmap = geom->texture->bm_handle;
		}
	}

	renderGeometry(geom->vertex_buffer, geom->index_buffer, geom->num_elements, bitmap, translation);
}
void RocketRenderingInterface::ReleaseCompiledGeometry(CompiledGeometryHandle geometry)
{
	GR_DEBUG_SCOPE("libRocket::ReleaseCompiledGeometry");

	auto geom = reinterpret_cast<CompiledGeometry*>(geometry);

	gr_delete_buffer(geom->vertex_buffer);
	gr_delete_buffer(geom->index_buffer);

	delete geom;
}
void RocketRenderingInterface::EnableScissorRegion(bool enable)
{
	GR_DEBUG_SCOPE("libRocket::EnableScissorRegion");
	if (!enable) {
		// Disable clipping if requested
		gr_reset_clip();
	}

	// If clipping is active then SetScissorRegion will be called next
	clipping_active = enable;
}
void RocketRenderingInterface::SetScissorRegion(int x, int y, int width, int height)
{
	GR_DEBUG_SCOPE("libRocket::SetScissorRegion");
	if (clipping_active) {
		gr_set_clip((int)(x + renderOffset.x), (int)(y + renderOffset.y), width, height, GR_RESIZE_NONE);
	}
}
bool RocketRenderingInterface::LoadTexture(TextureHandle& texture_handle, Vector2i& texture_dimensions,
                                           const String& source)
{
	TRACE_SCOPE(tracing::RocketLoadTexture);
	GR_DEBUG_SCOPE("libRocket::LoadTexture");

	if (source.Find("data:image/") == 0) {
		//Special mode to catch blob textures
		String submode = source.Substring(11);

		if (submode.Find("png;base64,") == 0) {
			SCP_string data = submode.Substring(11).CString();

			int w, h, bpp;
			png_read_header(data, &w, &h, &bpp);
			if (w * h < 0)
				return false;

			//if it's not 32-bit, we expand when we read it
			bpp = 32;
			int d_size = bpp >> 3;

			//we waste memory if it turns out to be 24-bit, but the way this whole thing works is dodgy anyway
			ubyte* rawdata = new ubyte[w * h * d_size];
			if (rawdata == nullptr)
				return false;

			memset(rawdata, 0, w * h * d_size);
			png_read_bitmap(data, rawdata, &bpp);

			texture_dimensions = { w, h };
			bool success = RocketRenderingInterface::GenerateTexture(texture_handle, rawdata, texture_dimensions);
			
			delete[] rawdata;

			return success;
		}
		else if (submode.Find("bmpman,") == 0) {
			int handle = std::atoi(submode.Substring(7).CString());

			auto* entry = bm_get_entry(handle);
			if (entry->handle != handle)
				return false;

			entry->load_count++;

			bm_get_info(handle, &texture_dimensions.x, &texture_dimensions.y);

			std::unique_ptr<Texture> tex(new Texture());
			tex->bm_handle = handle;
			texture_handle = get_texture_handle(tex.release());
			return true;
		}
		else {
			return false;
		}
	}

	SCP_string filename;
	int dir_type;
	if (!RocketFileInterface::getCFilePath(source, filename, dir_type)) {
		return false;
	}

	auto period_pos = filename.rfind('.');
	if (period_pos != SCP_string::npos) {
		filename = filename.substr(0, period_pos);
	}

	std::unique_ptr<Texture> tex(new Texture());
	// If there is a file that ends with an animation extension, try to load that
	if (generic_anim_init_and_stream(&tex->animation, filename.c_str(), BM_TYPE_NONE, SCPUI_loads_hi_res_animations) ==
		0) {
		tex->is_animation = true;

		texture_dimensions.x = tex->animation.width;
		texture_dimensions.y = tex->animation.height;
	}

	if (!tex->is_animation) {
		// Try to load as standalone image instead
		auto id = bm_load_either(filename.c_str(), nullptr, nullptr, nullptr, false, dir_type);
		if (id < 0) {
			return false;
		}

		int w, h;
		bm_get_info(id, &w, &h);

		texture_dimensions.x = w;
		texture_dimensions.y = h;

		tex->bm_handle = id;
	}

	// We give the pointer to libRocket now so we release it from our unique ptr
	texture_handle = get_texture_handle(tex.release());
	return true;
}
bool RocketRenderingInterface::GenerateTexture(TextureHandle& texture_handle, const Rocket::Core::byte* source,
                                               const Vector2i& source_dimensions)
{
	TRACE_SCOPE(tracing::RocketGenerateTexture);
	GR_DEBUG_SCOPE("libRocket::GenerateTexture");
	auto size = (size_t)(source_dimensions.x * source_dimensions.y * 4); // RGBA format

	std::unique_ptr<uint8_t[]> buffer(new uint8_t[size]);
	memcpy(buffer.get(), source, size);

	auto id = bm_create(32, source_dimensions.x, source_dimensions.y, buffer.get());
	if (id < 0) {
		return false;
	}

	auto* tex      = new Texture();
	tex->bm_handle = id;
	tex->data      = std::move(buffer);

	texture_handle = get_texture_handle(tex);

	return true;
}
void RocketRenderingInterface::ReleaseTexture(TextureHandle texture)
{
	GR_DEBUG_SCOPE("libRocket::ReleaseTexture");
	Assertion(texture, "Invalid texture handle!");

	auto tex = get_texture(texture);

	if (tex->is_animation) {
		generic_anim_unload(&tex->animation);
	} else {
		bm_release(tex->bm_handle);
	}
	delete tex;
}
void RocketRenderingInterface::RenderGeometry(Vertex* vertices, int num_vertices, int* indices, int num_indices,
                                              TextureHandle texture, const Vector2f& translation)
{
	TRACE_SCOPE(tracing::RocketRenderGeometry);
	GR_DEBUG_SCOPE("libRocket::RenderGeometry");
	gr_update_buffer_data(vertex_stream_buffer, sizeof(*vertices) * num_vertices, vertices);
	gr_update_buffer_data(index_stream_buffer, sizeof(*indices) * num_indices, indices);

	int bitmap;
	if (texture == 0) {
		bitmap = -1;
	} else {
		if (get_texture(texture)->is_animation) {
			bitmap = get_texture(texture)->animation.bitmap_id;
		} else {
			bitmap = get_texture(texture)->bm_handle;
		}
	}

	renderGeometry(vertex_stream_buffer, index_stream_buffer, num_indices, bitmap, translation);
}

void RocketRenderingInterface::setRenderOffset(const vec2d& render_offset) { renderOffset = render_offset; }
float RocketRenderingInterface::GetPixelsPerInch()
{
#if SDL_VERSION_ATLEAST(2, 0, 4)
	auto display = os_config_read_uint("Video", "Display", 0);
	float ddpi;
	if (SDL_GetDisplayDPI(display, &ddpi, nullptr, nullptr) != 0) {
		mprintf(("Failed to determine display DPI: %s\n", SDL_GetError()));
		return 96.f;
	}

	// Diagonal DPI should be accurate enough
	return ddpi;
#else
	// return a default value
	return 96.f;
#endif
}

void RocketRenderingInterface::renderGeometry(gr_buffer_handle vertex_buffer,
	gr_buffer_handle index_buffer,
	int num_elements,
	int bitmap,
	const Rocket::Core::Vector2f& translation)
{
	interface_material material;

	vec2d transl;
	transl.x = translation.x + renderOffset.x;
	transl.y = translation.y + renderOffset.y;

	material_set_rocket_interface(&material, bitmap, transl, horizontal_swipe_offset);

	gr_render_rocket_primitives(&material, PRIM_TYPE_TRIS, &layout, num_elements, vertex_buffer, index_buffer);
}
int RocketRenderingInterface::getBitmapNum(Rocket::Core::TextureHandle handle)
{
	int bitmap;
	if (handle == 0) {
		bitmap = -1;
	} else {
		bitmap = get_texture(handle)->bm_handle;
	}

	return bitmap;
}
void RocketRenderingInterface::advanceAnimation(Rocket::Core::TextureHandle handle, float advanceTime)
{
	Assertion(handle != 0, "Invalid handle for setAnimationFrame");
	Assertion(get_texture(handle)->is_animation, "Tried to use advanceAnimation with a non-animation!");

	auto tex = get_texture(handle);

	generic_extras extras;
	extras.draw = false; // We only want to advance the time but not actually render the animation

	generic_anim_render(&tex->animation, advanceTime, -1, -1, false, &extras);
}
void RocketRenderingInterface::setHorizontalSwipeOffset(float value) {
	horizontal_swipe_offset = value;
}

} // namespace scpui