-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathOpenGL.cpp
More file actions
812 lines (692 loc) · 22.6 KB
/
OpenGL.cpp
File metadata and controls
812 lines (692 loc) · 22.6 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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
// Copyright 2015 by Joseph Forgion
// This file is part of VCC (Virtual Color Computer).
//
// VCC (Virtual Color Computer) is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// VCC (Virtual Color Computer) is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with VCC. If not, see <http://www.gnu.org/licenses/>.
//
// 2025/04/10 - Craig Allsop - Add OpenGL rendering.
//
#include "OpenGL.h"
#include "BuildConfig.h"
#include <Windows.h>
#include <CommCtrl.h>
#include <gl/GL.h>
#include "GL/glext.h"
#include "GL/wglext.h"
#include <vcc/util/logger.h>
#if USE_OPENGL
#if USE_DEBUG_LINES
#include <vector>
#endif
typedef HGLRC(WINAPI* wglprocCreateContextAttribsARB)(HDC, HGLRC, const int*);
typedef BOOL(WINAPI* wglprocChoosePixelFormatARB)(HDC, const int*, const FLOAT*, UINT, int*, UINT*);
typedef BOOL(WINAPI* wglprocSwapIntervalEXT)(int interval);
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32")
static const LPCSTR ViewClass = "VCC_ViewWnd";
static const LPCSTR OpenGLClass = "VCC_OpenGLWnd";
namespace VCC
{
//
// Private wrapper on result code for logging errors
//
static int Result(int code)
{
if (code != OpenGL::OK)
{
char message[256];
snprintf(message,64,"OpenGL error %d\nCheck OpenGL support",code);
MessageBox(nullptr,message,"Error",0);
// FIXME: This is needed and should not be commented out. Wrap it conditional
// either here or in the debug log functions.
//PrintLogC("OpenGL Error: %d\n", code);
}
return code;
}
//
// Impliementation detail of OpenGL interface.
//
struct OpenGL::Detail
{
const int SubClassId = 1;
const int GLSubClassId = 2;
const int SurfaceWidth = 640; // coco surface size
const int SurfaceHeight = 480;
const float PalFreq = 60.0f;
const float NtscFreq = 50.0f;
bool isInitialized; // if already initialized
HWND hWnd; // window handle of OpenGL window
HDC hDC; // its context
HGLRC hRC;
int width; // width of OpenGL window
int height; // height of OpenGL window
int statusHeight; // height of status bar
bool aspect; // true if aspect preserved, otherwise fit
bool ntsc; // use ntsc 50hz, otherwise 60hz
Pixel* pixels; // cpu side surface buffer
GLuint texId; // OpenGL texture on gpu
wglprocSwapIntervalEXT wglSwapIntervalEXT;
ISystemState* state;
#if USE_DEBUG_LINES
struct Line { float x1; float y1; float x2; float y2; Pixel color; };
std::vector<Line> debugLines;
#endif // USE_DEBUG_LINES
OpenGL::Detail(ISystemState *state)
: isInitialized(false), hWnd(nullptr), hDC(nullptr), hRC(nullptr)
, width(0), height(0), statusHeight(0), aspect(true), ntsc(false)
, pixels(nullptr), texId(0), wglSwapIntervalEXT(nullptr), state(state)
{
}
// intercepted parent window messages
static LRESULT __stdcall SubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
switch (uMsg)
{
case WM_ERASEBKGND:
return 1;
case WM_SIZE:
{
auto detail = reinterpret_cast<OpenGL::Detail*>(dwRefData);
detail->Resize(LOWORD(lParam), HIWORD(lParam));
break;
}
case WM_NCDESTROY:
RemoveWindowSubclass(hWnd, &SubclassProc, uIdSubclass);
break;
};
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
static LRESULT __stdcall GLSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
switch (uMsg)
{
case WM_ERASEBKGND:
return 1;
// prevent child window taking mouse messages
case WM_NCHITTEST:
return HTTRANSPARENT;
case WM_NCDESTROY:
RemoveWindowSubclass(hWnd, &SubclassProc, uIdSubclass);
break;
};
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
void Resize(int width, int height)
{
// windows callback with client size, leave space for status bar.
height -= statusHeight;
this->width = width;
this->height = height;
if (hWnd) SetWindowPos(hWnd, nullptr, 0, 0, width, height, SWP_NOCOPYBITS);
}
int Setup(void* hwnd, int width, int height, int statusHeight)
{
if (isInitialized)
return Result(ERR_INITIALIZED);
HWND hTempWnd = nullptr;
HDC hTempDC = nullptr;
HGLRC hTempRC = nullptr;
auto hInstance = GetModuleHandle(nullptr);
auto cleanupTempWindow = [&](int err)
{
if (hTempRC) { wglMakeCurrent(nullptr, nullptr); wglDeleteContext(hTempRC); }
if (hTempDC) ReleaseDC(hTempWnd, hTempDC);
if (hTempWnd) DestroyWindow(hTempWnd);
UnregisterClass(ViewClass, hInstance);
return err;
};
auto cleanupWindow = [&](int err)
{
Cleanup();
return cleanupTempWindow(err);
};
this->width = width;
this->height = height;
this->statusHeight = statusHeight;
WNDCLASS wc;
memset(&wc, 0, sizeof(wc));
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = DefWindowProc;
wc.hInstance = GetModuleHandle(nullptr);
wc.lpszClassName = ViewClass;
if (!RegisterClass(&wc))
return ERR_TMPREGISTERCLASS;
// Create a temporary context to get address of wgl extensions.
hTempWnd = CreateWindowEx(WS_EX_APPWINDOW, ViewClass, "Simple", WS_VISIBLE | WS_POPUP | WS_MAXIMIZE,
0, 0, 0, 0, nullptr, nullptr, hInstance, nullptr);
if (!hTempWnd)
return Result(ERR_TMPCREATEWINDOW);
hTempDC = GetDC(hTempWnd);
if (!hTempDC)
return Result(cleanupTempWindow(ERR_TMPGETDC));
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;
pfd.cColorBits = 24;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.iLayerType = PFD_MAIN_PLANE;
int iPF;
if (!(iPF = ChoosePixelFormat(hTempDC, &pfd)))
return Result(cleanupTempWindow(ERR_TMPCHOOSEFORMAT));
if (!SetPixelFormat(hTempDC, iPF, &pfd))
return Result(cleanupTempWindow(ERR_TMPSETPIXELFORMAT));
if (!(hTempRC = wglCreateContext(hTempDC)))
return Result(cleanupTempWindow(ERR_TMPCREATECONTEXT));
if (!wglMakeCurrent(hTempDC, hTempRC))
return Result(cleanupTempWindow(ERR_TMPMAKECONTEXT));
// Function pointers returned by wglGetProcAddress are tied to the render context they were obtained with.
wglprocCreateContextAttribsARB wglCreateContextAttribsARB = (wglprocCreateContextAttribsARB)wglGetProcAddress("wglCreateContextAttribsARB");
wglprocChoosePixelFormatARB wglChoosePixelFormatARB = (wglprocChoosePixelFormatARB)wglGetProcAddress("wglChoosePixelFormatARB");
wglSwapIntervalEXT = (wglprocSwapIntervalEXT)wglGetProcAddress("wglSwapIntervalEXT");
if (wglChoosePixelFormatARB && wglCreateContextAttribsARB && wglSwapIntervalEXT)
{
WNDCLASS wc = { 0 };
wc.lpfnWndProc = DefWindowProc;
wc.hInstance = hInstance;
wc.hbrBackground = nullptr;
wc.lpszClassName = OpenGLClass;
wc.style = CS_OWNDC;
if (!RegisterClass(&wc))
return Result(cleanupTempWindow(ERR_REGISTERCLASS));
SetWindowSubclass((HWND)hwnd, &SubclassProc, SubClassId, (DWORD_PTR)this);
hWnd = CreateWindowEx(0, OpenGLClass, nullptr, WS_VISIBLE | WS_CHILD, 0, 0,
width, height, (HWND)hwnd, 0, hInstance, this);
if (!hWnd)
{
UnregisterClass(OpenGLClass, hInstance);
return Result(cleanupTempWindow(ERR_CREATEWINDOW));
}
SetWindowSubclass((HWND)hWnd, &GLSubclassProc, GLSubClassId, (DWORD_PTR)this);
hDC = GetDC(hWnd);
if (!hDC)
return Result(cleanupWindow(ERR_GETDC));
int attribs[] = {
WGL_DRAW_TO_WINDOW_ARB, TRUE,
WGL_DOUBLE_BUFFER_ARB, TRUE,
WGL_SUPPORT_OPENGL_ARB, TRUE,
WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
WGL_COLOR_BITS_ARB, 24,
WGL_RED_BITS_ARB, 8,
WGL_GREEN_BITS_ARB, 8,
WGL_BLUE_BITS_ARB, 8,
WGL_DEPTH_BITS_ARB, 0,
WGL_STENCIL_BITS_ARB, 0,
0, 0
};
UINT numFormats;
BOOL success = wglChoosePixelFormatARB(hDC, attribs, nullptr, 1, &iPF, &numFormats);
DescribePixelFormat(hDC, iPF, sizeof(pfd), &pfd);
if (!(success && numFormats >= 1 && SetPixelFormat(hDC, iPF, &pfd)))
return Result(cleanupWindow(ERR_SETPIXELFORMAT));
// request OpenGl 3.0
int contextAttribs[] =
{
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
WGL_CONTEXT_MINOR_VERSION_ARB, 0,
0, 0
};
hRC = wglCreateContextAttribsARB(hDC, nullptr, contextAttribs);
if (!hRC)
return Result(cleanupWindow(ERR_GLVERSION));
// release temporary window
cleanupTempWindow(OK);
}
else return Result(ERR_MISSINGAPIS);
// the off screen surface
pixels = new Pixel[SurfaceWidth * SurfaceHeight];
memset(pixels, 0, SurfaceWidth * SurfaceHeight * sizeof(Pixel));
// create texture for rendering surface
wglMakeCurrent(hDC, hRC);
glViewport(0, 0, width, height);
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SurfaceWidth, SurfaceHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, pixels);
wglMakeCurrent(nullptr, nullptr);
isInitialized = true;
return Result(OK);
}
int Render()
{
if (!isInitialized) return Result(ERR_NOTINITIALIZED);
wglMakeCurrent(hDC, hRC);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, (float)width, (float)height, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// select surface texture
glBindTexture(GL_TEXTURE_2D, texId);
glEnable(GL_TEXTURE_2D);
// update it
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, SurfaceWidth, SurfaceHeight, GL_BGRA, GL_UNSIGNED_BYTE, pixels);
Rect area;
GetDisplayArea(&area);
// aliases - don't panic
const auto& x = area.x;
const auto& y = area.y;
const auto& w = area.w;
const auto& h = area.h;
// draw main surface in the middle
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(x, y, 0);
glTexCoord2f(1, 0); glVertex3f(x + w, y, 0);
glTexCoord2f(1, 1); glVertex3f(x + w, y + h, 0);
glTexCoord2f(0, 1); glVertex3f(x, y + h, 0);
glEnd();
#if USE_BLACKEDGES
// Switch to black color for edges
glDisable(GL_TEXTURE_2D);
#define glTexCoord2f(a,b) glColor3f(0,0,0)
#endif // USE_BLACKEDGES
glBegin(GL_QUADS);
if (x > 0)
{
// fill left/right edge
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(0, 0); glVertex3f(x, 0, 0);
glTexCoord2f(0, 1); glVertex3f(x, h, 0);
glTexCoord2f(0, 1); glVertex3f(0, h, 0);
glTexCoord2f(1, 0); glVertex3f(x + w, y, 0);
glTexCoord2f(1, 0); glVertex3f((float)width, y, 0);
glTexCoord2f(1, 1); glVertex3f((float)width, y + h, 0);
glTexCoord2f(1, 1); glVertex3f(x + w, y + h, 0);
}
else if (y > 0)
{
// fill top/bottom edge
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(1, 0); glVertex3f(w, 0, 0);
glTexCoord2f(1, 0); glVertex3f(w, y, 0);
glTexCoord2f(0, 0); glVertex3f(0, y, 0);
glTexCoord2f(0, 1); glVertex3f(0, y + h, 0);
glTexCoord2f(1, 1); glVertex3f(w, y + h, 0);
glTexCoord2f(1, 1); glVertex3f(w, (float)height, 0);
glTexCoord2f(0, 1); glVertex3f(0, (float)height, 0);
}
#if USE_BLACKEDGES
#undef glTexCoord2f
#endif // USE_BLACKEDGES
glEnd();
#if USE_DEBUG_LINES
// draw some debug lines
if (debugLines.size())
{
Pixel lastColor(debugLines[0].color);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_LINES);
glColor4ub(lastColor.r, lastColor.g, lastColor.b, lastColor.a);
for (size_t i = 0; i < debugLines.size(); ++i)
{
if (lastColor != debugLines[i].color)
{
lastColor = debugLines[i].color;
glColor4ub(lastColor.r, lastColor.g, lastColor.b, lastColor.a);
}
glVertex3f(debugLines[i].x1, debugLines[i].y1, 0);
glVertex3f(debugLines[i].x2, debugLines[i].y2, 0);
}
glEnd();
glDisable(GL_BLEND);
debugLines.clear();
}
#endif // USE_DEBUG_LINES
wglMakeCurrent(nullptr, nullptr);
return Result(OK);
}
int Cleanup()
{
if (!isInitialized) return Result(ERR_NOTINITIALIZED);
if (pixels) delete pixels;
if (hDC) ReleaseDC(hWnd, hDC);
if (hWnd) DestroyWindow(hWnd);
UnregisterClass(OpenGLClass, GetModuleHandle(nullptr));
isInitialized = false;
return Result(OK);
}
int GetSurface(Pixel** pixels) const
{
if (!isInitialized) return Result(ERR_NOTINITIALIZED);
*pixels = this->pixels;
return Result(OK);
}
int SetOption(int option, bool enabled)
{
if (!isInitialized) return Result(ERR_NOTINITIALIZED);
switch (option)
{
case OPT_FLAG_ASPECT: aspect = enabled; break;
case OPT_FLAG_NTSC: ntsc = enabled; break;
case OPT_FLAG_RESIZEABLE: break;
default: return Result(ERR_BADOPTION);
}
return Result(OK);
}
int GetRect(int rectOption, Rect* rect)
{
if (!isInitialized) return Result(ERR_NOTINITIALIZED);
switch (rectOption)
{
case OPT_RECT_DISPLAY: GetDisplayArea(rect); break;
case OPT_RECT_RENDER: GetRenderArea(rect); break;
case OPT_RECT_SURFACE: GetSurfaceArea(rect); break;
default: return Result(ERR_BADOPTION);
}
return Result(OK);
}
int RenderText(const OpenGLFont* font, float x, float y, float size, const char* text) const
{
if (!isInitialized) return Result(ERR_NOTINITIALIZED);
int length = strlen(text);
wglMakeCurrent(hDC, hRC);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, (float)width, (float)height, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, font->texture);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glColor3f(1, 1, 1);
for (int i = 0; i < length; ++i)
{
auto ch = text[i];
if (ch < font->start || ch >= font->end) continue;
const auto glyph = font->glyphs + ch - font->start;
if (ch != ' ')
{
const auto &ad = glyph->atlasDimensions;
const auto& d = glyph->dimensions;
float u1 = ad.l / font->textureWidth;
float v1 = ad.b / font->textureHeight;
float u2 = ad.r / font->textureWidth;
float v2 = ad.t / font->textureHeight;
glTexCoord2f(u1, v1); glVertex3f(x + size * d.l, y + size * d.b, 0);
glTexCoord2f(u2, v1); glVertex3f(x + size * d.r, y + size * d.b, 0);
glTexCoord2f(u2, v2); glVertex3f(x + size * d.r, y + size * d.t, 0);
glTexCoord2f(u1, v2); glVertex3f(x + size * d.l, y + size * d.t, 0);
}
x += glyph->advance * size;
}
glEnd();
wglMakeCurrent(nullptr, nullptr);
return Result(OK);
}
int Present() const
{
if (!isInitialized) return Result(ERR_NOTINITIALIZED);
wglMakeCurrent(hDC, hRC);
// present
glFlush();
// don't sync with monitor, if monitor is 30hz we want vcc
// running at 60 still, refresh is controlled in throttle.
wglSwapIntervalEXT(0);
SwapBuffers(hDC);
wglMakeCurrent(nullptr, nullptr);
return Result(OK);
}
int LoadFont(const OpenGLFont** outFont, int bitmapRes, const OpenGLFontGlyph* glyphs, int start, int end) const
{
*outFont = nullptr;
if (!isInitialized) return Result(ERR_NOTINITIALIZED);
HBITMAP hBitmap = nullptr;
HDC dcBitmap = nullptr;
COLORREF* bitmapPixels = nullptr;
auto cleanup = [&](int code)
{
if (hBitmap) DeleteObject(hBitmap);
if (dcBitmap) ReleaseDC(nullptr, dcBitmap);
if (bitmapPixels) delete bitmapPixels;
return code;
};
// load bitmap resource
auto hInstance = GetModuleHandle(nullptr);
hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(bitmapRes));
if (!hBitmap) return Result(cleanup(ERR_FONTHBITMAP));
dcBitmap = CreateCompatibleDC(nullptr);
if (!dcBitmap) return Result(cleanup(ERR_FONTDC));
SelectObject(dcBitmap, hBitmap);
BITMAP bm;
::GetObject(hBitmap, sizeof(bm), &bm);
BITMAPINFO bmpInfo;
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = bm.bmWidth;
bmpInfo.bmiHeader.biHeight = -bm.bmHeight;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 24;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biSizeImage = 0;
// get bitmap data
bitmapPixels = new COLORREF[bm.bmWidth * bm.bmHeight];
if (GetDIBits(dcBitmap, hBitmap, 0, bm.bmHeight, bitmapPixels, &bmpInfo, DIB_RGB_COLORS) == 0)
return Result(cleanup(ERR_FONTGETDIBITS));
// upload texture
GLuint textureId = 0;
wglMakeCurrent(hDC, hRC);
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bm.bmWidth, bm.bmHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, bitmapPixels);
if (glGetError()) return Result(cleanup(ERR_FONTTEXIMAGE2D));
wglMakeCurrent(nullptr, nullptr);
// allocate font
OpenGLFont* font = new OpenGLFont();
font->glyphs = glyphs;
font->start = start;
font->end = end;
font->texture = textureId;
font->textureWidth = (float)bm.bmWidth;
font->textureHeight = (float)bm.bmHeight;
*outFont = font;
return Result(cleanup(OK));
}
#if USE_DEBUG_LINES
void DebugDrawLine(float x1, float y1, float x2, float y2, Pixel color)
{
Line line = { x1, y1, x2, y2, color };
debugLines.push_back(line);
}
#endif // USE_DEBUG_LINES
int RenderBox(float x, float y, float w, float h, Pixel color, bool filled) const
{
if (!isInitialized) return Result(ERR_NOTINITIALIZED);
float x2 = x + w;
float y2 = y + h;
wglMakeCurrent(hDC, hRC);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, (float)width, (float)height, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3ub(color.r, color.g, color.b);
if (filled)
{
glBegin(GL_QUADS);
glVertex3f(x, y, 0);
glVertex3f(x + w, y, 0);
glVertex3f(x + w, y + h, 0);
glVertex3f(x, y + h, 0);
glEnd();
}
else
{
glBegin(GL_LINES);
glVertex3f(x, y, 0);
glVertex3f(x + w, y, 0);
glVertex3f(x + w, y, 0);
glVertex3f(x + w, y + h, 0);
glVertex3f(x + w, y + h, 0);
glVertex3f(x, y + h, 0);
glVertex3f(x, y + h, 0);
glVertex3f(x, y, 0);
glEnd();
}
wglMakeCurrent(nullptr, nullptr);
return Result(OK);
}
int LockSurface()
{
if (!isInitialized) return Result(ERR_NOTINITIALIZED);
state->SetSurface(pixels, 3, DefaultWidth);
return Result(OK);
}
private:
// returns the box where display should be rendered
void GetDisplayArea(Rect* area) const
{
area->w = (float)width;
area->h = (float)height;
// scale to fit display with aspect kept
if (aspect)
{
// keep aspect ratio, ntsc = 50hz
float freqScale = ntsc ? PalFreq / NtscFreq : 1.0f;
float dw = (float)SurfaceWidth;
float dh = (float)SurfaceHeight * freqScale;
float rx = area->w / dw;
float ry = area->h / dh;
float r = rx > ry ? ry : rx;
area->w = r * dw;
area->h = r * dh;
}
// center on display
area->x = (width - area->w) / 2.0f;
area->y = (height - area->h) / 2.0f;
}
void GetRenderArea(Rect* area) const
{
area->x = area->y = 0;
area->w = (float)width;
area->h = (float)height;
}
void GetSurfaceArea(Rect* area) const
{
area->x = area->y = 0;
area->w = (float)SurfaceWidth;
area->h = (float)SurfaceHeight;
}
};
//////////////////////////////////////////////////////////////////////////////////////////////////
//
// Forwarding functions to implementation.
//
int OpenGL::GetSurface(Pixel** pixels)
{
if (detail)
return detail->GetSurface(pixels);
return Result(ERR_NOTINITIALIZED);
}
int OpenGL::Setup(void* hwnd, int width, int height, int statusHeight, bool fullscreen)
{
detail = new Detail(state);
return detail->Setup(hwnd, width, height, statusHeight);
}
int OpenGL::Render()
{
if (detail)
return detail->Render();
return Result(ERR_NOTINITIALIZED);
}
int OpenGL::Cleanup()
{
if (detail)
{
int result = detail->Cleanup();
delete detail;
detail = nullptr;
return result;
}
return Result(ERR_NOTINITIALIZED);
}
int OpenGL::Present()
{
if (detail)
return detail->Present();
return Result(ERR_NOTINITIALIZED);
}
int OpenGL::SetOption(int option, bool enabled)
{
if (detail)
return detail->SetOption(option, enabled);
return Result(ERR_NOTINITIALIZED);
}
int OpenGL::RenderBox(float x, float y, float w, float h, Pixel color, bool filled)
{
if (detail)
return detail->RenderBox(x,y,w,h,color,filled);
return Result(ERR_NOTINITIALIZED);
}
int OpenGL::GetRect(int rectOption, Rect* area)
{
if (detail)
return detail->GetRect(rectOption, area);
return Result(ERR_NOTINITIALIZED);
}
int OpenGL::RenderText(const OpenGLFont* font, float x, float y, float size, const char* text)
{
if (detail)
return detail->RenderText(font, x, y, size, text);
return Result(ERR_NOTINITIALIZED);
}
int OpenGL::LoadFont(const OpenGLFont** outFont, int bitmapRes, const OpenGLFontGlyph* glyphs, int start, int end)
{
if (detail)
return detail->LoadFont(outFont, bitmapRes, glyphs, start, end);
return Result(ERR_NOTINITIALIZED);
}
int OpenGL::LockSurface()
{
if (detail)
return detail->LockSurface();
return Result(ERR_NOTINITIALIZED);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void OpenGL::DebugDrawLine(float x1, float y1, float x2, float y2, Pixel color)
{
#if USE_DEBUG_LINES
if (detail)
detail->DebugDrawLine(x1, y1, x2, y2, color);
#endif // USE_DEBUG_LINES
}
void OpenGL::DebugDrawBox(float x, float y, float w, float h, Pixel color)
{
#if USE_DEBUG_LINES
if (detail)
{
float x2 = x + w;
float y2 = y + h;
detail->DebugDrawLine(x, y, x2, y, color);
detail->DebugDrawLine(x2, y, x2, y2, color);
detail->DebugDrawLine(x2, y2, x, y2, color);
detail->DebugDrawLine(x, y2, x, y, color);
}
#endif // USE_DEBUG_LINES
}
}
#endif // USE_OPENGL