-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOGfxDisplayGL2.cpp
776 lines (686 loc) · 25.1 KB
/
IOGfxDisplayGL2.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
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
/**
* Graphics primitives with OpenGL (ES) 2
* Copyright (C) 2015 Sylvain Beucler
* This file is part of GNU FreeDink
* GNU FreeDink is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
* GNU FreeDink 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
* Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public
* License along with GNU FreeDink. If not, see
* <https://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "IOGfxDisplayGL2.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <string.h>
#include "paths.h"
#include "log.h"
#include "IOGfxGLFuncs.h"
#include "IOGfxSurfaceGL2.h"
#include "gfx_palette.h"
#include "ImageLoader.h"
#include "debug_imgui.h"
#include "imgui_impl_opengl3.h"
#include "imgui_impl_sdl2.h"
#include "imgui.h"
IOGfxDisplayGL2::IOGfxDisplayGL2(int w, int h, bool truecolor, Uint32 flags)
: IOGfxDisplay(w, h, truecolor, flags | SDL_WINDOW_OPENGL), glcontext(NULL),
gl(NULL), screen(NULL) {
palette = -1;
vboSpriteVertices = -1, vboSpriteTexcoords = -1,
vboCroppedSpriteTexcoords = -1;
blit = NULL;
fliprgb = NULL;
flippal = NULL;
fillRect = NULL;
}
IOGfxDisplayGL2::~IOGfxDisplayGL2() {
close();
}
bool IOGfxDisplayGL2::open() {
if (!IOGfxDisplay::open())
return false;
if (!createOpenGLContext())
return false;
if (!createSpriteVertices())
return false;
if (!createSpriteTexcoords())
return false;
if (!createCroppedSpriteTexcoords())
return false;
if (!createPrograms())
return false;
if (!createPalette())
return false;
return true;
}
void IOGfxDisplayGL2::close() {
//ImGui Shutdown
ImGui_ImplOpenGL3_Shutdown();
if (gl != NULL) {
gl->DeleteProgram(blit->program);
gl->DeleteProgram(fillRect->program);
gl->DeleteProgram(flippal->program);
delete blit;
delete fillRect;
delete flippal;
gl->DeleteBuffers(1, &vboSpriteVertices);
gl->DeleteBuffers(1, &vboSpriteTexcoords);
gl->DeleteBuffers(1, &vboCroppedSpriteTexcoords);
gl->Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |
GL_STENCIL_BUFFER_BIT);
delete gl;
gl = NULL;
}
if (glcontext != NULL)
SDL_GL_DeleteContext(glcontext);
glcontext = NULL;
IOGfxDisplay::close();
}
bool IOGfxDisplayGL2::createOpenGLContext() {
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
glcontext = SDL_GL_CreateContext(window);
if (glcontext == NULL) {
log_error("Could not create OpenGL context: %s", SDL_GetError());
return false;
}
if (SDL_GL_MakeCurrent(window, glcontext) < 0) {
SDL_GL_DeleteContext(glcontext);
log_error("Could not make OpenGL context current: %s", SDL_GetError());
return false;
}
// Let FramerateManager handle frame delay
// TODO: drop the extra SwapWindow/RenderPresent during speed mode
// TODO: ideally we should set to 1 to get VSync and avoid tearing, if achievable
// Commented out for emscripten to avoid "emscripten_set_main_loop_timing: Cannot set timing mode for main loop since a main loop does not exist! Call emscripten_set_main_loop first to set one up." warning
#ifndef __EMSCRIPTEN__
SDL_GL_SetSwapInterval(0);
#endif
gl = new IOGfxGLFuncs();
gl->Enable(GL_BLEND);
//gl->Enable(GL_DEPTH_TEST);
gl->BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// buffer for glReadPixels, defaults to GL_BACK in double buffered mode
// This functions is not present in GLES2
//gl->ReadBuffer(GL_FRONT);
const char* v = "#version 120\n";
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.Fonts->AddFontFromFileTTF(paths_pkgdatafile("Roboto-Medium.ttf"), 15);
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
io.WantCaptureMouse = true;
//io.WantCaptureKeyboard = true;
ImGui::StyleColorsDark();
ImGui_ImplSDL2_InitForOpenGL(window, glcontext);
ImGui_ImplOpenGL3_Init(v);
return true;
}
bool IOGfxDisplayGL2::createSpriteVertices() {
GLfloat spriteVertices[] = {
0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1,
};
gl->GenBuffers(1, &vboSpriteVertices);
gl->BindBuffer(GL_ARRAY_BUFFER, vboSpriteVertices);
gl->BufferData(GL_ARRAY_BUFFER, sizeof(spriteVertices), spriteVertices,
GL_STATIC_DRAW);
return true;
}
bool IOGfxDisplayGL2::createSpriteTexcoords() {
GLfloat spriteTexcoords[] = {
0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0,
};
gl->GenBuffers(1, &vboSpriteTexcoords);
gl->BindBuffer(GL_ARRAY_BUFFER, vboSpriteTexcoords);
gl->BufferData(GL_ARRAY_BUFFER, sizeof(spriteTexcoords), spriteTexcoords,
GL_STATIC_DRAW);
return true;
}
bool IOGfxDisplayGL2::createCroppedSpriteTexcoords() {
gl->GenBuffers(1, &vboCroppedSpriteTexcoords);
return true;
}
/**
* Compile vertex or fragment shader.
* 'name' is provided for debugging purposes only
*/
GLuint IOGfxDisplayGL2::createShader(const char* name, const char* source,
GLenum type) {
GLuint res = gl->CreateShader(type);
// GLSL version
std::string version;
int profile;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &profile);
if (profile == SDL_GL_CONTEXT_PROFILE_ES)
version = "#version 100\n"; // OpenGL ES 2.0
else
version = "#version 120\n"; // OpenGL 2.1
// GLES2 precision specifiers
std::string precision = "#ifdef GL_ES \n"
"# ifdef GL_FRAGMENT_PRECISION_HIGH \n"
" precision highp float; \n"
"# else \n"
" precision mediump float; \n"
"# endif \n"
"#else \n"
// Ignore unsupported precision specifiers
"# define lowp \n"
"# define mediump \n"
"# define highp \n"
"#endif \n";
// Concat instead of giving glShaderSource multiple strings.
// At least one setup complained that "#version 120" was an incomplete shader
// (VirtualBox 5.1 + Windows 7 guest) - so not taking chances.
std::string fullsource = version + precision + source;
const GLchar* sources[] = {fullsource.c_str()};
gl->ShaderSource(res, 1, sources, NULL);
gl->CompileShader(res);
GLint compile_ok = GL_FALSE;
gl->GetShaderiv(res, GL_COMPILE_STATUS, &compile_ok);
if (compile_ok == GL_FALSE) {
log_error("when compiling %s %s shader:", name,
(type == GL_VERTEX_SHADER) ? "vertex" : "fragment");
infoLog(res);
gl->DeleteShader(res);
return 0;
}
return res;
}
void IOGfxDisplayGL2::infoLog(GLuint object) {
GLint log_length = 0;
if (gl->IsShader(object)) {
gl->GetShaderiv(object, GL_INFO_LOG_LENGTH, &log_length);
} else if (gl->IsProgram(object)) {
gl->GetProgramiv(object, GL_INFO_LOG_LENGTH, &log_length);
} else {
log_error("logShaderInfoLog: Not a shader or a program");
return;
}
char* log = (char*)malloc(log_length);
if (gl->IsShader(object))
gl->GetShaderInfoLog(object, log_length, NULL, log);
else if (gl->IsProgram(object))
gl->GetProgramInfoLog(object, log_length, NULL, log);
log_error("%s\n", log);
free(log);
}
bool IOGfxDisplayGL2::createPrograms() {
blit = new IOGfxGLProg();
blit->vshader = "attribute vec4 v_coord; \n"
"attribute vec2 v_texcoord; \n"
"varying vec2 f_texcoord; \n"
"uniform mat4 mvp; \n"
" \n"
"void main(void) { \n"
" gl_Position = mvp * v_coord; \n"
" f_texcoord = v_texcoord; \n"
"} \n";
blit->fshader = "varying vec2 f_texcoord; \n"
"uniform sampler2D texture; \n"
"uniform vec3 colorkey; \n"
" \n"
"void main(void) { \n"
" vec4 f = texture2D(texture, f_texcoord); \n"
" // don't need float comparison abs(a-b)<.001? \n"
" if (f.r == colorkey.r \n"
" && f.g == colorkey.g \n"
" && f.b == colorkey.b) \n"
" f.a = 0.0; \n"
" gl_FragColor = f; \n"
"} \n";
blit->program = createProgram("blit", blit->vshader, blit->fshader);
if ((blit->attributes["v_coord"] =
getAttribLocation(blit->program, "v_coord")) == -1)
return false;
if ((blit->attributes["v_texcoord"] =
getAttribLocation(blit->program, "v_texcoord")) == -1)
return false;
if ((blit->uniforms["mvp"] = getUniformLocation(blit->program, "mvp")) ==
-1)
return false;
if ((blit->uniforms["texture"] =
getUniformLocation(blit->program, "texture")) == -1)
return false;
if ((blit->uniforms["colorkey"] =
getUniformLocation(blit->program, "colorkey")) == -1)
return false;
fillRect = new IOGfxGLProg();
fillRect->vshader = "attribute vec4 v_coord; \n"
"uniform mat4 mvp; \n"
" \n"
"void main(void) { \n"
" gl_Position = mvp * v_coord; \n"
"} \n";
fillRect->fshader = "uniform vec4 color; \n"
" \n"
"void main(void) { \n"
" gl_FragColor = color; \n"
"} \n";
fillRect->program =
createProgram("fillRect", fillRect->vshader, fillRect->fshader);
if ((fillRect->attributes["v_coord"] =
getAttribLocation(fillRect->program, "v_coord")) == -1)
return false;
if ((fillRect->uniforms["mvp"] =
getUniformLocation(fillRect->program, "mvp")) == -1)
return false;
if ((fillRect->uniforms["color"] =
getUniformLocation(fillRect->program, "color")) == -1)
return false;
fliprgb = new IOGfxGLProg();
fliprgb->vshader = "attribute vec4 v_coord; \n"
"attribute vec2 v_texcoord; \n"
"varying vec2 f_texcoord; \n"
"uniform mat4 mvp; \n"
" \n"
"void main(void) { \n"
" gl_Position = mvp * v_coord; \n"
" f_texcoord = v_texcoord; \n"
"} \n";
fliprgb->fshader = "varying vec2 f_texcoord; \n"
"uniform sampler2D texture; \n"
"uniform float brightness; \n"
" \n"
"void main(void) { \n"
" vec4 f = texture2D(texture, f_texcoord); \n"
" if (f == vec4(1.0,1.0,1.0, 1.0)) \n"
" gl_FragColor = f; \n"
" else \n"
" gl_FragColor = vec4(f.rgb * brightness, 1.0);\n"
"} \n";
fliprgb->program =
createProgram("pflip", fliprgb->vshader, fliprgb->fshader);
if ((fliprgb->attributes["v_coord"] =
getAttribLocation(fliprgb->program, "v_coord")) == -1)
return false;
if ((fliprgb->attributes["v_texcoord"] =
getAttribLocation(fliprgb->program, "v_texcoord")) == -1)
return false;
if ((fliprgb->uniforms["mvp"] =
getUniformLocation(fliprgb->program, "mvp")) == -1)
return false;
if ((fliprgb->uniforms["texture"] =
getUniformLocation(fliprgb->program, "texture")) == -1)
return false;
if ((fliprgb->uniforms["brightness"] =
getUniformLocation(fliprgb->program, "brightness")) == -1)
return false;
/* Palette emulation */
flippal = new IOGfxGLProg();
flippal->vshader = "attribute vec4 v_coord; \n"
"attribute vec2 v_texcoord; \n"
"varying vec2 f_texcoord; \n"
"uniform mat4 mvp; \n"
" \n"
"void main(void) { \n"
" gl_Position = mvp * v_coord; \n"
" f_texcoord = v_texcoord; \n"
"} \n";
flippal->fshader =
"varying vec2 f_texcoord; \n"
"uniform sampler2D texture; \n"
"uniform sampler2D palette; \n"
" \n"
"void main(void) { \n"
" vec4 index = texture2D(texture, f_texcoord); \n"
" gl_FragColor = texture2D(palette, vec2(index.r, 0.5)); \n"
"} \n";
flippal->program =
createProgram("i2rgb", flippal->vshader, flippal->fshader);
if ((flippal->attributes["v_coord"] =
getAttribLocation(flippal->program, "v_coord")) == -1)
return false;
if ((flippal->attributes["v_texcoord"] =
getAttribLocation(flippal->program, "v_texcoord")) == -1)
return false;
if ((flippal->uniforms["mvp"] =
getUniformLocation(flippal->program, "mvp")) == -1)
return false;
if ((flippal->uniforms["texture"] =
getUniformLocation(flippal->program, "texture")) == -1)
return false;
if ((flippal->uniforms["palette"] =
getUniformLocation(flippal->program, "palette")) == -1)
return false;
if (blit->program == 0 || fliprgb->program == 0 || fillRect->program == 0 ||
flippal->program == 0)
return false;
return true;
}
GLuint IOGfxDisplayGL2::createProgram(const char* name,
const char* vertexShaderSource,
const char* fragmentShaderSource) {
GLuint vs, fs;
if ((vs = createShader(name, vertexShaderSource, GL_VERTEX_SHADER)) == 0)
return false;
if ((fs = createShader(name, fragmentShaderSource, GL_FRAGMENT_SHADER)) ==
0)
return false;
GLuint program = gl->CreateProgram();
if (program == 0) {
log_error("Could not allocate program");
return 0;
}
gl->AttachShader(program, vs);
gl->AttachShader(program, fs);
GLint link_ok = GL_FALSE;
gl->LinkProgram(program);
gl->GetProgramiv(program, GL_LINK_STATUS, &link_ok);
if (!link_ok) {
infoLog(program);
return 0;
}
return program;
}
GLint IOGfxDisplayGL2::getAttribLocation(GLuint program, const char* name) {
GLint attribute = gl->GetAttribLocation(program, name);
if (attribute == -1)
log_error("Could not locate attribute %s", name);
return attribute;
}
GLint IOGfxDisplayGL2::getUniformLocation(GLuint program, const char* name) {
GLint uniform = gl->GetUniformLocation(program, name);
if (uniform == -1)
log_error("Could not locate uniform %s", name);
return uniform;
}
bool IOGfxDisplayGL2::createPalette() {
gl->GenTextures(1, &palette);
gl->BindTexture(GL_TEXTURE_2D, palette);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
updatePalette();
return true; // I hereby thank OpenGL error reporting..
}
void IOGfxDisplayGL2::updatePalette() {
SDL_Color pal_phys[256];
gfx_palette_get_phys(pal_phys);
gl->BindTexture(GL_TEXTURE_2D, palette);
gl->TexImage2D(GL_TEXTURE_2D, // target
0, // level, 0 = base, no minimap,
GL_RGBA, // internalformat
256, // width
1, // height
0, // border, always 0 in OpenGL ES
GL_RGBA, // format
GL_UNSIGNED_BYTE, // type
pal_phys);
}
void IOGfxDisplayGL2::logOpenGLInfo() {
int major, minor, profile;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &profile);
const char* profile_str = "";
if (profile & SDL_GL_CONTEXT_PROFILE_CORE)
profile_str = "CORE";
if (profile & SDL_GL_CONTEXT_PROFILE_COMPATIBILITY)
profile_str = "COMPATIBILITY";
if (profile & SDL_GL_CONTEXT_PROFILE_ES)
profile_str = "ES";
log_info("OpenGL %d.%d %s", major, minor, profile_str);
int doublebuffer;
SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuffer);
log_info("Double buffered: %d", doublebuffer);
#ifndef __EMSCRIPTEN__
// Error: WebGL warning: getParameter: parameter: invalid enum value READ_BUFFER
int read_buffer;
gl->GetIntegerv(GL_READ_BUFFER, &read_buffer);
if (read_buffer == GL_FRONT)
log_info("Read buffer: GL_FRONT");
else if (read_buffer == GL_BACK)
log_info("Read buffer: GL_BACK");
else
log_info("Read buffer: 0x%04X", read_buffer);
#endif
}
void IOGfxDisplayGL2::logDisplayInfo() {
log_info("FreeDink graphics mode: IOGfxDisplayGL2");
IOGfxDisplay::logDisplayInfo();
logOpenGLInfo();
}
void IOGfxDisplayGL2::clear() {
gl->BindFramebuffer(GL_FRAMEBUFFER, 0);
gl->ClearColor(0, 0, 0, 1);
gl->Clear(GL_COLOR_BUFFER_BIT);
}
SDL_Surface* IOGfxDisplayGL2::screenshot(SDL_Rect* rect) {
gl->BindFramebuffer(GL_FRAMEBUFFER, 0);
// assume 4-bytes alignment
SDL_Surface* image =
SDL_CreateRGBSurface(0, rect->w, rect->h, 32,
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff
#else
0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
#endif
);
unsigned char* pixels = (unsigned char*)image->pixels;
gl->ReadPixels(rect->x, h - rect->h - rect->y, rect->w, rect->h, GL_RGBA,
GL_UNSIGNED_BYTE, pixels);
// Flip vertically
unsigned int length = rect->w * 4;
unsigned char* src = pixels + (rect->h - 1) * length;
unsigned char* tmp = new unsigned char[length];
unsigned char* dst = pixels;
unsigned int rows = rect->h / 2;
while (rows--) {
memcpy(tmp, dst, length);
memcpy(dst, src, length);
memcpy(src, tmp, length);
dst += length;
src -= length;
}
delete[] tmp;
return image;
}
void IOGfxDisplayGL2::setVertexAttrib(IOGfxGLProg* prog, GLuint attribLocation,
GLuint vbo, GLint size) {
gl->EnableVertexAttribArray(attribLocation);
// Describe our vertices array to OpenGL (it can't guess its format automatically)
gl->BindBuffer(GL_ARRAY_BUFFER, vbo);
gl->VertexAttribPointer(
attribLocation, // attribute
size, // number of elements per vertex, e.g. (x,y,z,t)
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no extra data between each position
0 // offset of first element
);
}
void IOGfxDisplayGL2::flip(IOGfxSurface* backbuffer, SDL_Rect* dstrect,
bool interpolation, bool hwflip) {
//ImGui Frame start
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
dbg_imgui();
if (backbuffer == NULL)
SDL_SetError("IOGfxDisplayGL2::flip: passed a NULL surface");
IOGfxSurfaceGL2* surf = dynamic_cast<IOGfxSurfaceGL2*>(backbuffer);
GLuint texture = surf->texture;
IOGfxGLProg* prog = truecolor ? fliprgb : flippal;
gl->UseProgram(prog->program);
setVertexAttrib(prog, prog->attributes["v_coord"], vboSpriteVertices, 4);
setVertexAttrib(prog, prog->attributes["v_texcoord"], vboSpriteTexcoords,
2);
gl->ActiveTexture(GL_TEXTURE0);
gl->Uniform1i(prog->uniforms["texture"], /*GL_TEXTURE*/ 0);
gl->BindTexture(GL_TEXTURE_2D, texture);
// GL_LINEAR's slight blur is closer to 90's CRT monitors
// GL_NEAREST is clean-pixellated and harder on the eyes
if (truecolor && interpolation) {
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
} else {
// but GL_LINEAR messes palette emulation
// TODO: implement 2-step flip: 1) i2rgb 2) interpolation
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
if (!truecolor) {
gl->ActiveTexture(GL_TEXTURE1);
gl->Uniform1i(prog->uniforms["palette"], /*GL_TEXTURE*/ 1);
gl->BindTexture(GL_TEXTURE_2D, palette);
updatePalette();
}
if (truecolor)
gl->Uniform1f(prog->uniforms["brightness"], brightness / 256.0);
// Y-inversed projection for top-left origin and top-bottom textures
// Beware that rotation is reversed too
glm::mat4 projection = glm::ortho(0.0f, 1.0f * w, 1.0f * h, 0.0f);
glm::mat4 m_transform =
glm::translate(glm::mat4(1.0f),
glm::vec3(dstrect->x, dstrect->y, 0)) *
glm::scale(glm::mat4(1.0f), glm::vec3(dstrect->w, dstrect->h, 0));
glm::mat4 mvp = projection * m_transform; // * view * model * anim;
gl->UniformMatrix4fv(prog->uniforms["mvp"], 1, GL_FALSE,
glm::value_ptr(mvp));
/* Push each element in buffer_vertices to the vertex shader */
gl->BindFramebuffer(GL_FRAMEBUFFER, 0);
gl->DrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// gl->DisableVertexAttribArray(prog->attributes["v_coord"]);
// gl->DisableVertexAttribArray(blit->attributes["v_texcoord"]);
//Popout window
if (debug_gamewindow) {
ImGuiWindowFlags windowflags = ImGuiWindowFlags_AlwaysAutoResize;
ImGui::Begin("Editor Display", &debug_gamewindow, windowflags);
if (ImGui::Button("1x"))
edit_edscale = 1; ImGui::SameLine();
if (ImGui::Button("2x"))
edit_edscale = 2;
ImGui::Image((void*)(intptr_t)texture,ImVec2((int)640*edit_edscale,480*edit_edscale));
ImGui::End();
}
if (hwflip) {
//ImGui Render
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(window);
}
}
void IOGfxDisplayGL2::onSizeChange(int w, int h) {
this->w = w;
this->h = h;
gl->Viewport(0, 0, w, h);
}
IOGfxSurface* IOGfxDisplayGL2::upload(SDL_Surface* surf) {
GLuint texture = -1;
gl->GenTextures(1, &texture);
gl->BindTexture(GL_TEXTURE_2D, texture);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
log_trace("surf->w=%d", surf->w);
log_trace("surf->h=%d", surf->h);
log_trace("surf->format->bits=%d", surf->format->BitsPerPixel);
// Save transparency color
SDL_Color colorkey = {0, 0, 0, 0};
Uint32 key;
if (SDL_GetColorKey(surf, &key) == -1) {
colorkey.a = SDL_ALPHA_OPAQUE; // no colorkey
} else {
if (truecolor)
SDL_GetRGBA(key, surf->format, &colorkey.r, &colorkey.g,
&colorkey.b, &colorkey.a);
else
colorkey.r = colorkey.g = colorkey.b = key;
colorkey.a = SDL_ALPHA_TRANSPARENT;
}
if (truecolor) {
// Dink images get alpha disabled, so use 24-bit for simplicity
if (surf->format->BitsPerPixel != 24 ||
surf->format->format != SDL_PIXELFORMAT_RGB888) {
// don't black out transparent pixels, keep original color
SDL_SetColorKey(surf, SDL_FALSE, 0);
SDL_PixelFormat fmt;
fmt.BitsPerPixel = 24;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
fmt.Rmask = 0xFF000000;
fmt.Gmask = 0x00FF0000;
fmt.Bmask = 0x0000FF00;
fmt.Amask = 0x00000000;
#else
fmt.Rmask = 0x000000FF;
fmt.Gmask = 0x0000FF00;
fmt.Bmask = 0x00FF0000;
fmt.Amask = 0x00000000;
#endif
SDL_Surface* surf24 = SDL_CreateRGBSurface(
0, surf->w, surf->h, fmt.BitsPerPixel, fmt.Rmask, fmt.Gmask,
fmt.Bmask, fmt.Amask);
SDL_BlitSurface(surf, NULL, surf24, NULL);
SDL_FreeSurface(surf);
surf = surf24;
}
gl->TexImage2D(GL_TEXTURE_2D, // target
0, // level, 0 = base, no minimap,
GL_RGB, // internalformat
surf->w, // width
surf->h, // height
0, // border, always 0 in OpenGL ES
GL_RGB, // format
GL_UNSIGNED_BYTE, // type
surf->pixels);
} else {
if (surf->format->BitsPerPixel != 8) {
log_error("IOGfxDisplayGL2::upload: format is not indexed");
SDL_FreeSurface(surf);
return NULL;
}
gl->TexImage2D(GL_TEXTURE_2D, // target
0, // level, 0 = base, no minimap,
GL_LUMINANCE, // internalformat
surf->w, // width
surf->h, // height
0, // border, always 0 in OpenGL ES
GL_LUMINANCE, // format
GL_UNSIGNED_BYTE, // type
surf->pixels);
}
int w = surf->w;
int h = surf->h;
SDL_FreeSurface(surf);
return new IOGfxSurfaceGL2(this, texture, w, h, colorkey);
}
IOGfxSurface* IOGfxDisplayGL2::allocBuffer(int surfW, int surfH) {
GLuint texture = -1;
gl->GenTextures(1, &texture);
gl->BindTexture(GL_TEXTURE_2D, texture);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// No color key
SDL_Color colorkey = {0, 0, 0, SDL_ALPHA_OPAQUE};
// Blank texture
// RGB even in indexed mode because some setup (Android) don't allow GL_LUMINANCE as framebuffer
unsigned char* pixels =
new unsigned char[(surfW + (4 - surfW % 4)) * 4 * surfH];
gl->TexImage2D(GL_TEXTURE_2D, // target
0, // level, 0 = base, no minimap,
GL_RGB, // internalformat
surfW, // width
surfH, // height
0, // border, always 0 in OpenGL ES
GL_RGB, // format
GL_UNSIGNED_BYTE, // type
pixels);
delete[] pixels;
return new IOGfxSurfaceGL2(this, texture, surfW, surfH, colorkey);
}