-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplaymanager.cpp
More file actions
381 lines (326 loc) · 10.4 KB
/
displaymanager.cpp
File metadata and controls
381 lines (326 loc) · 10.4 KB
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//
// Created by Ian Parker on 06/03/2025.
//
#include "displaymanager.h"
#include <XPLMProcessing.h>
#include <XPLMDisplay.h>
#include <XPLMDataAccess.h>
#include <filesystem>
#include <yaml-cpp/yaml.h>
#include <fnmatch.h>
#include <png.h>
#ifdef __APPLE__
#define GL_DO_NOT_WARN_IF_MULTI_GL_VERSION_HEADERS_INCLUDED 1
#include <OpenGL/OpenGLAvailability.h>
#include <OpenGL/gl.h>
#include <OpenGL/gl3.h>
#else
#define GL_GLEXT_PROTOTYPES 1
#define GL3_PROTOTYPES 1
#include <GL/gl.h>
#endif
using namespace std;
int DisplayManager::updateCallback([[maybe_unused]] XPLMDrawingPhase inPhase, [[maybe_unused]] int inIsBefore, void *inRefcon)
{
auto displayManager = static_cast<DisplayManager*>(inRefcon);
displayManager->update();
return 0;
}
bool DisplayManager::start()
{
if (m_running)
{
return true;
}
if (m_textures.empty())
{
if (!findDisplays())
{
return false;
}
}
log(DEBUG, "startStream: Registering callback...");
XPLMRegisterDrawCallback(updateCallback, xplm_Phase_Panel, 0, this);
m_running = true;
return true;
}
bool DisplayManager::stop()
{
if (m_running)
{
log(DEBUG, "stop: Stopping...");
m_running = false;
XPLMUnregisterDrawCallback(updateCallback, xplm_Phase_Panel, 0, this);
}
return true;
}
bool DisplayManager::findDisplays()
{
YAML::Node displayDef;
if (!findDefinition(displayDef))
{
return false;
}
shared_ptr<Texture> texture = nullptr;
YAML::Node textureNode;
for (int i = 1; i < 1000; i++)
{
if (glIsTexture(i))
{
log(DEBUG, "findDisplay: %d: Found texture!");
texture = checkTexture(displayDef, i, textureNode);
if (texture != nullptr)
{
break;
}
}
}
glBindTexture(GL_TEXTURE_2D, 0);
if (texture != nullptr)
{
log(DEBUG, "findDisplay: Adding display for texture %d", texture->textureNum);
YAML::Node displaysNode = textureNode["displays"];
for (auto displayNode : displaysNode)
{
auto display = make_shared<Display>(
displayNode["x"].as<int>(),
displayNode["y"].as<int>(),
displayNode["width"].as<int>(),
displayNode["height"].as<int>(),
displayNode["name"].as<string>(),
texture);
texture->displays.push_back(display);
m_displays.push_back(display);
}
m_textures.push_back(texture);
}
return !m_textures.empty();
}
std::string readString(const std::string& dataRefName)
{
XPLMDataRef dataRef = XPLMFindDataRef(dataRefName.c_str());
if (dataRef == nullptr)
{
return "";
}
// Get the length
int bytes = XPLMGetDatab(dataRef, nullptr, 0, 0);
// Read the value
char buffer[4096];
XPLMGetDatab(dataRef, buffer, 0, bytes);
buffer[bytes] = '\0';
return {buffer};
}
bool DisplayManager::findDefinition(YAML::Node& result)
{
string aircraftAuthor = readString("sim/aircraft/view/acf_studio");
if (aircraftAuthor.empty())
{
aircraftAuthor = readString("sim/aircraft/view/acf_author");
}
string aircraftICAO = readString("sim/aircraft/view/acf_ICAO");
log(DEBUG, "findDefinition: Author: %s, ICAO type: %s", aircraftAuthor.c_str(), aircraftICAO.c_str());
for (const auto & entry : filesystem::directory_iterator("Resources/plugins/xstream/data"))
{
if (entry.path().extension() == ".yaml")
{
auto aircraftFile = YAML::LoadFile(entry.path());
if (!aircraftFile["author"] || !aircraftFile["icao"])
{
log(ERROR, "%s: Not a valid aircraft definition", entry.path().c_str());
continue;
}
auto fileAuthor = aircraftFile["author"].as<string>();
vector<string> fileICAOs;
auto icaoNode = aircraftFile["icao"];
if (icaoNode.IsScalar())
{
auto fileICAO = aircraftFile["icao"].as<string>();
fileICAOs.push_back(fileICAO);
}
else
{
for (auto fileICAO : icaoNode)
{
fileICAOs.push_back(fileICAO.as<string>());
}
}
int match = fnmatch(fileAuthor.c_str(), aircraftAuthor.c_str(), 0);
if (match != 0)
{
continue;
}
bool foundICAO = false;
for (const auto& fileICAO : fileICAOs)
{
match = fnmatch(aircraftICAO.c_str(), fileICAO.c_str(), 0);
if (match == 0)
{
foundICAO = true;
break;
}
}
if (!foundICAO)
{
continue;
}
log(INFO, "%s: Aircraft definition found!", entry.path().c_str());
result = aircraftFile;
return true;
}
}
return false;
}
shared_ptr<Texture> DisplayManager::checkTexture(YAML::Node& displayDef, int textureNum, YAML::Node& matchingDef)
{
glBindTexture(GL_TEXTURE_2D, textureNum);
GLint width;
GLint height;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &height);
log(DEBUG, "findDisplay: Texture %d: -> size=%d, %d", textureNum, width, height);
YAML::Node textures = displayDef["textures"];
for (const YAML::Node& textureNode : textures)
{
int requiredWidth = textureNode["width"].as<int>();
int requiredHeight = textureNode["height"].as<int>();
auto requiredBytes = textureNode["bytes"].as<vector<int>>();
if (width == requiredWidth && height == requiredHeight)
{
const unique_ptr<uint8_t[]> data(new uint8_t[width * height * 4]);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data.get());
log(DEBUG, "findDisplay: Texture %d: Correct size. Checking bytes (%02x %02x %02x %02x)", textureNum, data[0], data[1], data[2], data[3]);
bool bytesMatch = true;
for (int i = 0; i < requiredBytes.size(); i++)
{
if (requiredBytes[i] != data[i])
{
bytesMatch = false;
break;
}
}
if (bytesMatch)
{
log(DEBUG, "findDisplay: Texture %d: -> Texture matches pattern!", textureNum);
auto texture = make_shared<Texture>();
texture->textureNum = textureNum;
texture->textureWidth = width;
texture->textureHeight = height;
texture->buffer = new uint8_t[width * height * 4];
matchingDef = textureNode;
return texture;
}
}
}
// Not the texture we're looking for!
return nullptr;
}
void DisplayManager::update()
{
if (!m_running)
{
return;
}
float now = XPLMGetElapsedTime();
if (now - m_lastUpdate < 0.5f)
{
return;
}
m_lastUpdate = now;
for (const auto& texture : m_textures)
{
#ifdef DEBUG
log(DEBUG, "updateDisplay: Texture: %d", texture->textureNum);
#endif
glBindTexture(GL_TEXTURE_2D, texture->textureNum);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture->buffer);
// Slice the texture up in to the separate displays
for (const auto& display : texture->displays)
{
copyDisplay(texture, display);
}
}
glBindTexture(GL_TEXTURE_2D, 0);
}
void DisplayManager::copyDisplay(const shared_ptr<Texture>& texture, const shared_ptr<Display>& display)
{
uintptr_t srcPos = 0;
uintptr_t dstStride = display->width * 4;
uintptr_t srcStride = texture->textureWidth * 4;
uintptr_t dstPos = dstStride * (display->height - 1);
srcPos += display->x * 4;
srcPos += display->y * srcStride;
// Copy backwards!
for (int y = 0; y < display->height; y++)
{
memcpy(display->buffer + dstPos, texture->buffer + srcPos, dstStride);
srcPos += srcStride;
dstPos -= dstStride;
}
}
void DisplayManager::dumpTextures()
{
string icao = readString("sim/aircraft/view/acf_ICAO");
for (int i = 0; i < 1000; i++)
{
if (glIsTexture(i))
{
glBindTexture(GL_TEXTURE_2D, i);
dumpTexture(i, icao);
}
}
glBindTexture(GL_TEXTURE_2D, 0);
}
void DisplayManager::dumpTexture(int i, std::string icao)
{
GLint width;
GLint height;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &height);
if (width >= 2048 && height >= 2048)
{
log(DEBUG, "dumpTexture: Texture %d: Size: %d, %d", i, width, height);
unique_ptr<uint8_t[]> data(new uint8_t[width * height * 4]);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data.get());
char filename[1024];
snprintf(filename, 1024, "dump/texture_%s_%d.png", icao.c_str(), i);
FILE* fp = fopen(filename, "wb");
if (fp == nullptr)
{
log(ERROR, "dumpTexture: Failed to open file %s", filename);
return;
}
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
png_infop info_ptr = png_create_info_struct(png_ptr);
png_init_io(png_ptr, fp);
// Output is 8bit depth, RGBA format.
png_set_IHDR(
png_ptr,
info_ptr,
width, height,
8,
PNG_COLOR_TYPE_RGBA,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT
);
png_write_info(png_ptr, info_ptr);
png_bytep row = data.get();
for (int y = 0; y < height; y++)
{
png_write_row(png_ptr, row);
row += width * 4;
}
png_write_end(png_ptr, nullptr);
fclose(fp);
snprintf(filename, 1024, "dump/texture_%s_%d.dat", icao.c_str(), i);
fp = fopen(filename, "wb");
if (fp == nullptr)
{
log(ERROR, "dumpTexture: Failed to open file %s", filename);
return;
}
fwrite(data.get(), width * height * 4, 1, fp);
fclose(fp);
}
}