-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUtility.cpp
More file actions
641 lines (565 loc) · 20.1 KB
/
Utility.cpp
File metadata and controls
641 lines (565 loc) · 20.1 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/* SimShip by Edouard Halbert
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License
http://creativecommons.org/licenses/by-nc-nd/4.0/ */
#include "Utility.h"
// Console
void InitConsole()
{
// Si l'attachement échoue, créez une nouvelle console
AllocConsole();
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
freopen("CONIN$", "r", stdin);
SetConsoleTitle(L"SimShip Console");
SetWindowPos(GetConsoleWindow(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE);
}
void ConsoleClose()
{
FreeConsole();
}
void ConsoleClear()
{
HANDLE hConsoleOut; // Handle to the console
hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbiInfo; // Console information
GetConsoleScreenBufferInfo(hConsoleOut, &csbiInfo);
DWORD dummy;
COORD Home = { 0, 0 };
FillConsoleOutputCharacter(hConsoleOut, ' ', csbiInfo.dwSize.X * csbiInfo.dwSize.Y, Home, &dummy);
}
// glm
void PrintGlmMatrix(mat4& mat, string name)
{
cout << "glm::mat : " << name << endl;
for (int i = 0; i < 4; ++i)
{
cout << "[ ";
for (int j = 0; j < 4; ++j)
cout << setw(10) << setprecision(4) << fixed << mat[j][i] << " ";
cout << "]" << endl;
}
}
void PrintGlmVec3(vec3 vec, string name)
{
cout << "glm::vec3 : " << name << endl;
cout << "[ ";
for (int i = 0; i < 3; ++i)
{
cout << setw(10) << setprecision(4) << fixed << vec[i] << " ";
}
cout << "]" << endl;
}
void PrintGlmVec3(vec3 vec)
{
cout << "[ ";
for (int i = 0; i < 3; ++i)
{
cout << setw(10) << setprecision(4) << fixed << vec[i] << " ";
}
cout << "]" << endl;
}
// OpenGL
void SetVsync(int interval)
{
glfwSwapInterval(interval);
}
void GetOpenGLInfo()
{
// Info on version
const GLubyte* version = glGetString(GL_VERSION);
std::cout << "Version OpenGL: " << version << std::endl;
const GLubyte* shading_version = glGetString(GL_SHADING_LANGUAGE_VERSION);
std::cout << "Version GLSL: " << shading_version << std::endl;
GLint profile;
glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &profile);
if (profile & GL_CONTEXT_CORE_PROFILE_BIT) cout << "Profil Core" << endl;
else if (profile & GL_CONTEXT_COMPATIBILITY_PROFILE_BIT) cout << "Profil Compatibility" << endl;
else cout << "Profil Not Specified" << endl;
cout << endl;
// Limits of the GPU
GLint workGroupSize[3], workGroupInvocations;
glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 0, &workGroupSize[0]);
glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 1, &workGroupSize[1]);
glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 2, &workGroupSize[2]);
glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, &workGroupInvocations);
cout << "Maximum size of work groups: " << workGroupSize[0] << " x " << workGroupSize[1] << " x " << workGroupSize[2] << endl;
cout << "Maximum number of invocations per group: " << workGroupInvocations << endl;
/*
// Paramètres optimisés pour RTX 4070
const GLuint groupSizeX = 16; // 16x16 = 256 threads par groupe
const GLuint groupSizeY = 16;
const GLuint groupSizeZ = 1;
// Calculez le nombre de groupes nécessaires pour couvrir votre problème
// Supposons que nous travaillons sur une grille 2D de 8192x8192
const GLuint numGroupsX = (8192 + groupSizeX - 1) / groupSizeX;
const GLuint numGroupsY = (8192 + groupSizeY - 1) / groupSizeY;
// Dispatch
glDispatchCompute(numGroupsX, numGroupsY, 1);
*/
}
string opengl_info_display()
{
std::string s;
s += "[VENDOR] : " + string((char*)(glGetString(GL_VENDOR))) + "\n";
s += "[RENDERER] : " + string((char*)(glGetString(GL_RENDERER))) + "\n";
s += "[VERSION] : " + string((char*)(glGetString(GL_VERSION))) + "\n";
s += "[GLSL VERSION]: " + string((char*)(glGetString(GL_SHADING_LANGUAGE_VERSION))) + "\n";
return s;
}
void gl_check_error()
{
#if defined(_DEBUG) || defined(DEBUG)
GLint error = glGetError();
if (error)
{
const char* errorStr = 0;
switch (error)
{
case GL_INVALID_ENUM: errorStr = "GL_INVALID_ENUM"; break;
case GL_INVALID_VALUE: errorStr = "GL_INVALID_VALUE"; break;
case GL_INVALID_OPERATION: errorStr = "GL_INVALID_OPERATION"; break;
case GL_OUT_OF_MEMORY: errorStr = "GL_OUT_OF_MEMORY"; break;
default: errorStr = "unknown error"; break;
}
printf("GL error : %s\n", errorStr);
error = 0;
}
#endif
}
static string opengl_error_to_string(GLenum error)
{
switch (error)
{
case GL_NO_ERROR:
return "GL_NO_ERROR";
case GL_INVALID_ENUM:
return "GL_INVALID_ENUM";
case GL_INVALID_VALUE:
return "GL_INVALID_VALUE";
case GL_INVALID_OPERATION:
return "GL_INVALID_OPERATION";
case GL_INVALID_FRAMEBUFFER_OPERATION:
return "GL_INVALID_FRAMEBUFFER_OPERATION";
case GL_OUT_OF_MEMORY:
return "GL_OUT_OF_MEMORY";
#ifndef __EMSCRIPTEN__
case GL_STACK_UNDERFLOW:
return "GL_STACK_UNDERFLOW";
case GL_STACK_OVERFLOW:
return "GL_STACK_OVERFLOW";
#endif
default:
return "UNKNOWN";
}
}
void check_opengl_error(string const& file, string const& function, int line)
{
GLenum error = glGetError();
if (error != GL_NO_ERROR)
{
string msg = "OpenGL ERROR detected\n"
"\tFile " + file + "\n"
"\tFunction " + function + "\n"
"\tLine " + to_string(line) + "\n"
"\tOpenGL Error: " + opengl_error_to_string(error);
cout << msg << endl;
}
}
// Files
vector<string> ListFiles(const string& folder, const string& ext)
{
vector<string> files;
for (const auto& entry : filesystem::directory_iterator(folder))
{
if (entry.path().extension() == ext)
files.push_back(entry.path().string());
}
return files;
}
// Strings
string wstring_to_utf8(const wstring& wstr)
{
if (wstr.empty()) return string();
int size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), (int)wstr.size(), NULL, 0, NULL, NULL);
string strTo(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
return strTo;
}
wstring utf8_to_wstring(const string& str)
{
if (str.empty()) return wstring();
int size_needed = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), NULL, 0);
wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), &wstrTo[0], size_needed);
return wstrTo;
}
// Conversions
float ms_to_knot(float speedMS)
{
return speedMS * 3600.0f / 1852.0f;
}
float knot_to_ms(float speedKnots)
{
return speedKnots * 1852.0f / 3600.0f;
}
float wind_to_dirdeg(vec2 windVector)
{
// Extraire x et z du vecteur de vent
float x = -windVector.x; // Inverser x pour échanger Est et Ouest
float z = windVector.y;
// Calculer l'angle en radians
float angleRad = atan(x, z);
// Convertir en degrés et ajuster pour le référentiel souhaité
float angleDeg = mod(degrees(angleRad) + 180.0, 360.0);
return angleDeg;
}
vec2 wind_from_speeddir(float directionDEG, float speedKN)
{
// Convertir la direction en radians
float directionRad = radians(directionDEG);
// Calculer les composantes x et y du vecteur
float x = knot_to_ms(speedKN) * sin(directionRad);
float y = knot_to_ms(-speedKN) * cos(directionRad);
// Retourner le vecteur vent
return vec2(x, y);
}
// Interpolations
quat RotationBetweenVectors(vec3 A, vec3 B)
{
A = glm::normalize(A);
B = glm::normalize(B);
float cosTheta = glm::dot(A, B);
vec3 rotationAxis;
if (cosTheta < -1 + 0.001f)
{
// Les vecteurs pointent dans des directions opposées
rotationAxis = glm::cross(vec3(0.0f, 0.0f, 1.0f), A);
if (glm::length2(rotationAxis) < 0.01f)
rotationAxis = glm::cross(vec3(1.0f, 0.0f, 0.0f), A);
rotationAxis = glm::normalize(rotationAxis);
return glm::angleAxis(glm::radians(180.0f), rotationAxis);
}
rotationAxis = glm::cross(A, B);
float s = sqrt((1 + cosTheta) * 2);
float invs = 1 / s;
return glm::quat(
s * 0.5f,
rotationAxis.x * invs,
rotationAxis.y * invs,
rotationAxis.z * invs
);
}
float Sign(float value)
{
if (value > 0.0f)
return 1.0f;
else if (value < 0.0f)
return -1.0f;
else
return 0.0f;
}
double InterpolateAValue(const double start_1, const double end_1, const double start_2, const double end_2, double value_between_start_1_and_end_1)
{
// Normaliser la valeur entre start_1 et end_1
double normalized = (value_between_start_1_and_end_1 - start_1) / (end_1 - start_1);
// Interpoler vers l'intervalle [start_2, end_2]
return start_2 + normalized * (end_2 - start_2);
}
bool IsInRect(vec4& rect, vec2& point)
{
return (point.x >= rect.x && point.x <= rect.x + rect.z && point.y >= rect.y && point.y <= rect.y + rect.w);
}
bool IsInCircle(const vec3& circle, const vec2& point)
{
float dx = point.x - circle.x;
float dy = point.y - circle.y;
return (dx * dx + dy * dy) <= (circle.z * circle.z);
}
bool IntersectionOfSegments(const vec2& p1, const vec2& p2, const vec2& p3, const vec2& p4, vec2& p)
{
float denom = (p1.x - p2.x) * (p3.y - p4.y) - (p1.y - p2.y) * (p3.x - p4.x);
if (denom == 0)
return 0; // the segments are parallel
float t = ((p1.x - p3.x) * (p3.y - p4.y) - (p1.y - p3.y) * (p3.x - p4.x)) / denom;
float u = -((p1.x - p2.x) * (p1.y - p3.y) - (p1.y - p2.y) * (p1.x - p3.x)) / denom;
if (t >= 0 && t <= 1 && u >= 0 && u <= 1)
{
p.x = p1.x + t * (p2.x - p1.x);
p.y = p1.y + t * (p2.y - p1.y);
return true;
}
else
return false; // the segments do not intersect
}
bool IntersectionOfSegments(const vec2& p1, const vec2& p2, const vec2& p3, const vec2& p4)
{
float denom = (p1.x - p2.x) * (p3.y - p4.y) - (p1.y - p2.y) * (p3.x - p4.x);
if (denom == 0)
return 0; // the segments are parallel
float t = ((p1.x - p3.x) * (p3.y - p4.y) - (p1.y - p3.y) * (p3.x - p4.x)) / denom;
float u = -((p1.x - p2.x) * (p1.y - p3.y) - (p1.y - p2.y) * (p1.x - p3.x)) / denom;
if (t >= 0 && t <= 1 && u >= 0 && u <= 1)
return true;
else
return false; // the segments do not intersect
}
// Geography
const float EARTH_RADIUS = 6371000.0; // Mean radius of the Earth in meters
const vec2 REFERENCE_POINT(-2.94097114, 47.38162231); // Houat
float lon_to_opengl(float lon)
{
float dLon = glm::radians(lon - REFERENCE_POINT.x);
return EARTH_RADIUS * dLon * cos(glm::radians(REFERENCE_POINT.y));
}
float lat_to_opengl(float lat)
{
float dLat = glm::radians(lat - REFERENCE_POINT.y);
return -EARTH_RADIUS * dLat;
}
vec3 lonlat_to_opengl(float lon, float lat)
{
float dLon = glm::radians(lon - REFERENCE_POINT.x);
float dLat = glm::radians(lat - REFERENCE_POINT.y);
float x = EARTH_RADIUS * dLon * cos(glm::radians(REFERENCE_POINT.y));
float z = -EARTH_RADIUS * dLat;
return vec3(x, 0.0f, z);
}
vec2 opengl_to_lonlat(float x, float z)
{
float lon = REFERENCE_POINT.x + glm::degrees(x / (EARTH_RADIUS * cos(glm::radians(REFERENCE_POINT.y))));
float lat = REFERENCE_POINT.y - glm::degrees(z / EARTH_RADIUS);
return vec2(lon, lat);
}
float get_angle_from_north(vec3 dir)
{
// Vector representing North (negative Z axis)
vec3 north(0.0f, 0.0f, -1.0f);
// Projection of the direction onto the XZ plane
vec3 directionXZ(dir.x, 0.0f, dir.z);
// Normalization of the projected vector
directionXZ = normalize(directionXZ);
// Calculating the angle between the projected direction and North
float North = glm::orientedAngle(north, directionXZ, vec3(0.0f, 1.0f, 0.0f));
// Converting angle to degrees
North = degrees(North);
North = 360.0f - North;
// Adjusting the angle to always be positive (0-360)
while (North < 0)
North += 360.0f;
while (North > 360.0f)
North -= 360.0f;
return North;
}
float get_yaw_from_hdg(float hdgDeg)
{
float deg_Yaw = fmod(450.0f - hdgDeg, 360.0f);
if (deg_Yaw < 0.0f)
deg_Yaw += 360.0f;
return glm::radians(deg_Yaw);
}
float get_hdg_from_yaw(float yawRad)
{
float yaw_deg = glm::degrees(yawRad);
float hdg = fmod(450.0f - yaw_deg, 360.0f);
if (hdg < 0.0f)
hdg += 360.0f;
return hdg;
}
// Colors
vec3 color_255_to_1(vec3 v)
{
return vec3((float)v.x / 255.0f, (float)v.y / 255.0f, (float)v.z / 255.0f);
}
void rgb_to_hsl(const vec3& rgb, float& h, float& s, float& l)
{
float r = rgb.r / 255.0f;
float g = rgb.g / 255.0f;
float b = rgb.b / 255.0f;
float max = std::max(std::max(r, g), b);
float min = std::min(std::min(r, g), b);
l = (max + min) / 2.0f;
if (max == min) {
h = s = 0.0f;
return;
}
float d = max - min;
s = (l > 0.5f) ? d / (2.0f - max - min) : d / (max + min);
if (max == r)
h = (g - b) / d + (g < b ? 6.0f : 0.0f);
else if (max == g)
h = (b - r) / d + 2.0f;
else
h = (r - g) / d + 4.0f;
h /= 6.0f;
}
// Save client area to image file (png)
wstring GetNextAvailableCaptureName(const wstring& folderPath)
{
WIN32_FIND_DATAW ffd;
HANDLE hFind = INVALID_HANDLE_VALUE;
wstring searchPath = folderPath + L"\\SimShip - Capture *.png";
vector<int> numbers;
// Lister les fichiers correspondant au motif
hFind = FindFirstFileW(searchPath.c_str(), &ffd);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
wstring filename(ffd.cFileName);
// Extraire le numéro
size_t pos = filename.rfind(L"Capture ");
if (pos != wstring::npos)
{
pos += 8; // "Capture "
wstring numStr = filename.substr(pos, filename.size() - pos - 4); // -4 pour .png
try
{
int num = std::stoi(numStr);
numbers.push_back(num);
}
catch (...) {}
}
}
} while (FindNextFileW(hFind, &ffd) != 0);
FindClose(hFind);
}
// Trouver le plus grand numéro existant
int nextNum = 1;
if (!numbers.empty())
{
std::sort(numbers.begin(), numbers.end());
nextNum = numbers.back() + 1;
}
// Générer le nom de fichier
wstringstream ss;
ss << L"SimShip - Capture " << std::setw(2) << std::setfill(L'0') << nextNum << L".png";
return ss.str();
}
void SaveHBITMAP(HBITMAP bitmap, HDC hDC, wchar_t* filename)
{
BITMAP bmp;
PBITMAPINFO pbmi;
WORD cClrBits;
HANDLE hf; // file handle
BITMAPFILEHEADER hdr; // bitmap file-header
PBITMAPINFOHEADER pbih; // bitmap info-header
LPBYTE lpBits; // memory pointer
DWORD dwTotal; // total count of bytes
DWORD cb; // incremental count of bytes
BYTE* hp; // byte pointer
DWORD dwTmp;
// Create the bitmapinfo header information
if (!GetObject(bitmap, sizeof(BITMAP), (LPSTR)&bmp))
{
perror("Could not retrieve bitmap info");
return;
}
// Convert the color format to a count of bits.
cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel);
if (cClrBits == 1)
cClrBits = 1;
else if (cClrBits <= 4)
cClrBits = 4;
else if (cClrBits <= 8)
cClrBits = 8;
else if (cClrBits <= 16)
cClrBits = 16;
else if (cClrBits <= 24)
cClrBits = 24;
else cClrBits = 32;
// Allocate memory for the BITMAPINFO structure.
if (cClrBits != 24)
pbmi = (PBITMAPINFO)LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * (1 << cClrBits));
else
pbmi = (PBITMAPINFO)LocalAlloc(LPTR, sizeof(BITMAPINFOHEADER));
// Initialize the fields in the BITMAPINFO structure.
pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth = bmp.bmWidth;
pbmi->bmiHeader.biHeight = bmp.bmHeight;
pbmi->bmiHeader.biPlanes = bmp.bmPlanes;
pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel;
if (cClrBits < 24)
pbmi->bmiHeader.biClrUsed = (1 << cClrBits);
// If the bitmap is not compressed, set the BI_RGB flag.
pbmi->bmiHeader.biCompression = BI_RGB;
// Compute the number of bytes in the array of color indices and store the result in biSizeImage.
pbmi->bmiHeader.biSizeImage = (pbmi->bmiHeader.biWidth + 7) / 8 * pbmi->bmiHeader.biHeight * cClrBits;
// Set biClrImportant to 0, indicating that all of the device colors are important.
pbmi->bmiHeader.biClrImportant = 0;
// Now open file and save the data
pbih = (PBITMAPINFOHEADER)pbmi;
lpBits = (LPBYTE)GlobalAlloc(GMEM_FIXED, pbih->biSizeImage);
if (!lpBits)
{
perror("SaveHBITMAP::Could not allocate memory");
return;
}
// Retrieve the color table (RGBQUAD array) and the bits
if (!GetDIBits(hDC, HBITMAP(bitmap), 0, (WORD)pbih->biHeight, lpBits, pbmi, DIB_RGB_COLORS))
{
perror("SaveHBITMAP::GetDIB error");
return;
}
// Create the .BMP file.
hf = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, (DWORD)0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL);
if (hf == INVALID_HANDLE_VALUE)
{
perror("Could not create file for writing");
return;
}
hdr.bfType = 0x4d42; // 0x42 = "B" 0x4d = "M"
// Compute the size of the entire file
hdr.bfSize = (DWORD)(sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof(RGBQUAD) + pbih->biSizeImage);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
// Compute the offset to the array of color indices
hdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + pbih->biSize + pbih->biClrUsed * sizeof(RGBQUAD);
// Copy the BITMAPFILEHEADER into the .BMP file
if (!WriteFile(hf, (LPVOID)&hdr, sizeof(BITMAPFILEHEADER), (LPDWORD)&dwTmp, NULL))
{
perror("Could not write in to file");
return;
}
// Copy the BITMAPINFOHEADER and RGBQUAD array into the file
if (!WriteFile(hf, (LPVOID)pbih, sizeof(BITMAPINFOHEADER) + pbih->biClrUsed * sizeof(RGBQUAD), (LPDWORD)&dwTmp, (NULL)))
{
perror("Could not write in to file");
return;
}
// Copy the array of color indices into the .BMP file
dwTotal = cb = pbih->biSizeImage;
hp = lpBits;
if (!WriteFile(hf, (LPSTR)hp, (int)cb, (LPDWORD)&dwTmp, NULL))
{
perror("Could not write in to file");
return;
}
// Close the .BMP file
if (!CloseHandle(hf))
{
perror("Could not close file");
return;
}
// Free memory
GlobalFree((HGLOBAL)lpBits);
}
wstring SaveClientArea(HWND hwnd)
{
// Get a compatible DC into the client area
HDC hDC = GetDC(hwnd);
HDC hTargetDC = CreateCompatibleDC(hDC);
RECT rect = { 0 };
GetClientRect(hwnd, &rect);
HBITMAP hBitmap = CreateCompatibleBitmap(hDC, rect.right - rect.left, rect.bottom - rect.top);
SelectObject(hTargetDC, hBitmap);
PrintWindow(hwnd, hTargetDC, PW_CLIENTONLY);
wstring name = GetNextAvailableCaptureName(L"Outputs");
name = L"Outputs/" + name;
SaveHBITMAP(hBitmap, hTargetDC, const_cast<wchar_t*>(name.c_str()));
DeleteObject(hBitmap);
ReleaseDC(hwnd, hDC);
DeleteDC(hTargetDC);
return name;
}