-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.cpp
1108 lines (953 loc) · 35.9 KB
/
screen.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
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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cstddef>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <cassert>
#include <stdnoreturn.h>
#include <unistd.h>
#include <sys/param.h>
using namespace std;
#include "screen.hpp"
#include "palette.hpp"
#define INNER_STRINGIZE(x) #x
#define STRINGIZE(x) INNER_STRINGIZE(x)
#define checkGlErrors(cont) \
(_checkGlErrors(cont, __FILE__ ":" STRINGIZE(__LINE__)))
noreturn void die(void) {
glfwTerminate();
exit(-1);
}
const char *glErrorString(GLenum err) {
switch (err) {
case GL_INVALID_ENUM:
return "Invalid enum";
case GL_INVALID_VALUE:
return "Invalud value";
case GL_INVALID_OPERATION:
return "Invalid operation";
case GL_OUT_OF_MEMORY:
return "Out of memory";
case GL_INVALID_FRAMEBUFFER_OPERATION:
return "Invalid framebuffer operation";
default:
return "Unrecognized error";
}
}
void _checkGlErrors(int continue_after_err,
const char *errloc) {
GLenum err = GL_NO_ERROR;
while ((err = glGetError()) != GL_NO_ERROR) {
const char *errMsg = glErrorString(err);
cerr << errloc
<< ": GL error: " << errMsg
<< " (" << hex << err << ")\n";
if (!continue_after_err) {
die();
}
}
}
Screen::Screen(ScrollType st)
: scrollType(st)
{
// Bit of a hack here: initializing the window will change the
// working directory for some reason, so store it and change it back
char cwd[MAXPATHLEN];
if (!getcwd(cwd, MAXPATHLEN)) {
cerr << "Couldn't get working directory\n";
die();
}
if (initWindow(&window) < 0) {
cerr << "Couldn't create window\n";
die();
}
if (chdir(cwd) != 0) {
cerr << "Couldn't reset working directory\n";
die();
}
// initWindow also does this check, but let's be safe and make sure
// the window made it out of initWindow okay
if (glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR) < 3) {
cerr << "Screen: Error: GL major version too low\n";
die();
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
// bind a vertex array
GLuint va;
glGenVertexArrays(1, &va);
checkGlErrors(0);
glBindVertexArray(va);
checkGlErrors(0);
initShaders();
glGenBuffers(1, &bgVbo);
glGenBuffers(1, &spriteVbo);
glGenTextures(1, &bgPtabName);
glGenTextures(1, &spritePtabName);
checkGlErrors(0);
lastBgPalette = -1;
lastSpritePalette = -1;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
checkGlErrors(0);
// things to zero-initialize: local palettes, pattern tables, tile
// indices, palette indices
// There is a more c++ style way to get the array lengths here. Eh.
memset(bgPalettes, 0, LOCAL_PALETTES_LENGTH * sizeof(float));
memset(spritePalettes, 0, LOCAL_PALETTES_LENGTH * sizeof(float));
memset(oam, 0, OAM_SIZE);
vector<float> zeroPtab(PATTERN_TABLE_LENGTH, 0);
setBgPatternTable(zeroPtab);
setSpritePatternTable(zeroPtab);
// set up state
setupFrame();
// there's probably a cleaner c++ way to do this
tileIndices.resize(tileColumns());
paletteIndices.resize(tileColumns());
for (int i = 0; i < tileColumns(); i++) {
tileIndices[i].resize(tileRows());
paletteIndices[i].resize(tileRows());
}
setUniversalBg(0);
spriteVertices.reserve(OAM_ENTRIES * 6);
}
ntab_coord Screen::tileRows() {
switch(scrollType) {
case SCROLL_VERTICAL:
return VISIBLE_TILE_ROWS * 2;
case SCROLL_HORIZONTAL:
return VISIBLE_TILE_ROWS;
default:
cerr << "tileRows: invalid scroll type\n";
die();
}
}
ntab_coord Screen::tileColumns() {
switch(scrollType) {
case SCROLL_VERTICAL:
return VISIBLE_TILE_COLUMNS;
case SCROLL_HORIZONTAL:
return VISIBLE_TILE_COLUMNS * 2;
default:
cerr << "tileColumns: invalid scroll type\n";
die();
}
}
scroll_coord Screen::scrollWidth() {
return (scroll_coord) (tileColumns() * 8);
}
scroll_coord Screen::scrollHeight() {
return (scroll_coord) (tileRows() * 8);
}
GLint safeGetAttribLocation(GLuint program, const GLchar *name) {
GLint loc = glGetAttribLocation(program, name);
checkGlErrors(0);
if (loc < 0) {
cerr << "Couldn't get program attribute " << name << "\n";
die();
}
return loc;
}
GLint safeGetUniformLocation(GLuint program, const GLchar *name) {
GLint loc = glGetUniformLocation(program, name);
checkGlErrors(0);
if (loc < 0) {
cerr << "Couldn't get program uniform " << name << "\n";
die();
}
return loc;
}
// doesn't return an error message, just exits the program on error
void Screen::initShaders(void) {
// get shaders
ifstream vertFile(VERTEX_SHADER_FILE);
if (!vertFile) {
cerr << "Couldn't open vertex shader file\n";
die();
}
stringstream vertBuffer;
vertBuffer << vertFile.rdbuf();
// This approach makes the string turn into the empty string for some reason
// const char *vertSrc = vertBuffer.str().c_str();
string vertStr = vertBuffer.str();
const char *vertSrc = vertStr.c_str();
// TODO error-check and make sure the file isn't too big
int vertSrcLen = (int) vertStr.length();
ifstream fragFile(FRAGMENT_SHADER_FILE);
if (!fragFile) {
cerr << "Couldn't open fragment shader file\n";
die();
}
stringstream fragBuffer;
fragBuffer << fragFile.rdbuf();
string fragStr = fragBuffer.str();
const char *fragSrc = fragStr.c_str();
int fragSrcLen = (int) fragStr.length();
// compile shaders
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertSrc, &vertSrcLen);
glCompileShader(vertexShader);
// check compilation
GLint status;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
if (!status) {
cerr << "Error compiling vertex shader:\n";
GLint logLen = 0;
glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &logLen);
if (logLen > 1) {
GLchar *log = (GLchar*) malloc(logLen);
GLint readLogLen = 0;
glGetShaderInfoLog(vertexShader, logLen, &readLogLen, log);
// FIXME should probably only print logLen chars here just to be safe
cout << log;
} else {
cerr << "Couldn't get log message\n";
}
die();
}
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragSrc, &fragSrcLen);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &status);
if (!status) {
cerr << "Error compiling fragment shader:\n";
GLint logLen = 0;
glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &logLen);
if (logLen > 1) {
GLchar *log = (GLchar*) malloc(logLen);
GLint readLogLen = 0;
glGetShaderInfoLog(fragmentShader, logLen, &readLogLen, log);
// FIXME should probably only print logLen chars here just to be safe
cout << log;
} else {
cerr << "Couldn't get log message\n";
}
die();
}
// link shader program
shader = glCreateProgram();
glAttachShader(shader, vertexShader);
glAttachShader(shader, fragmentShader);
glLinkProgram(shader);
glGetProgramiv(shader, GL_LINK_STATUS, &status);
if (!status) {
cerr << "Error linking shader:\n";
GLint logLen = 0;
glGetProgramiv(shader, GL_INFO_LOG_LENGTH, &logLen);
if (logLen > 1) {
GLchar *log = (GLchar*) malloc(logLen);
GLint readLogLen = 0;
glGetProgramInfoLog(shader, logLen, &readLogLen, log);
// FIXME should probably only print logLen chars here just to be safe
cout << log;
} else {
cerr << "Couldn't get log message\n";
}
die();
}
glUseProgram(shader);
checkGlErrors(0);
// shader attribute/uniform pointers
posAttrib = safeGetAttribLocation(shader, "pos");
priorityAttrib = safeGetAttribLocation(shader, "priority");
tileAttrib = safeGetAttribLocation(shader, "tile");
uvAttrib = safeGetAttribLocation(shader, "v_uv");
paletteNAttrib = safeGetAttribLocation(shader, "v_palette_n");
patternTableUniform = safeGetUniformLocation(shader, "patternTable");
localPalettesUniform = safeGetUniformLocation(shader, "localPalettes");
}
void Screen::setupFrame() {
// It'll be python's job to set up a scroll region starting at
// pixel-space (0,0). We won't do it here.
scrollChanges.clear();
}
void Screen::setUniversalBg(int bg) {
// TODO check that this is a valid index into PALETTE
universalBg = bg;
}
void Screen::setBgPalettes(vector<float> localPaletteInput) {
assert(localPaletteInput.size() == LOCAL_PALETTES_LENGTH);
memcpy(bgPalettes, localPaletteInput.data(),
LOCAL_PALETTES_LENGTH * sizeof(float));
}
// Assumes that the input is exactly LOCAL_PALETTES_LENGTH long
void Screen::setBgPalettes(float *localPaletteInput) {
memcpy(bgPalettes, localPaletteInput,
LOCAL_PALETTES_LENGTH * sizeof(float));
}
void Screen::setSpritePalettes(vector<float> localPaletteInput) {
assert(localPaletteInput.size() == LOCAL_PALETTES_LENGTH);
memcpy(spritePalettes, localPaletteInput.data(),
LOCAL_PALETTES_LENGTH * sizeof(float));
}
// Assumes that the input is exactly LOCAL_PALETTES_LENGTH long
void Screen::setSpritePalettes(float *localPaletteInput) {
memcpy(spritePalettes, localPaletteInput,
LOCAL_PALETTES_LENGTH * sizeof(float));
}
void Screen::setTileIndices(vector<vector<unsigned char> > tiles) {
tileIndices = tiles;
}
void Screen::setTileIndices(unsigned char *tiles, unsigned int len) {
// this is probably slower than it needs to be, but let's just make
// it work for now
assert(len == tileColumns() * tileRows());
tileIndices.resize(tileColumns());
for (int x = 0; x < tileColumns(); x++) {
tileIndices[x].resize(tileRows());
for (int y = 0; y < tileRows(); y++) {
tileIndices[x][y] = tiles[(x * tileRows()) + y];
}
}
}
void Screen::setPaletteIndices(vector<vector<unsigned char> > palettes) {
paletteIndices = palettes;
}
void Screen::setPaletteIndices(unsigned char *indices, unsigned int len) {
// this is probably slower than it needs to be, but let's just make
// it work for now
assert(len == tileColumns() * tileRows());
paletteIndices.resize(tileColumns());
for (int x = 0; x < tileColumns(); x++) {
paletteIndices[x].resize(tileRows());
for (int y = 0; y < tileRows(); y++) {
paletteIndices[x][y] = indices[(x * tileRows()) + y];
}
}
}
// assume the size is equal to 8*8*PATTERN_TABLE_TILES (TODO fix magic number)
void Screen::setBgPatternTable(float *bgPtabInput) {
glBindBuffer(GL_ARRAY_BUFFER, bgVbo);
glActiveTexture(BG_PATTERN_TABLE_TEXTURE);
glBindTexture(GL_TEXTURE_2D, bgPtabName);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, 8*PATTERN_TABLE_TILES, 8,
0, GL_RED, GL_FLOAT, bgPtabInput);
checkGlErrors(0);
}
void Screen::setBgPatternTable(vector<float> bgPtabInput) {
/* Note: according to stackoverflow, glTexImage2D allows the memory
to be freed after the call, so I don't have to worry about
copying the input over to somewhere stable. I can't find the part
of the API that actually says that, but for now I'll trust it.
http://stackoverflow.com/questions/26499361/opengl-what-does-glteximage2d-do
*/
// TODO fix magic number
assert(bgPtabInput.size() == 8*8*PATTERN_TABLE_TILES);
setBgPatternTable(bgPtabInput.data());
}
void Screen::setSpritePatternTable(float *spritePtabInput) {
glBindBuffer(GL_ARRAY_BUFFER, spriteVbo);
glActiveTexture(SPRITE_PATTERN_TABLE_TEXTURE);
glBindTexture(GL_TEXTURE_2D, spritePtabName);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, 8*PATTERN_TABLE_TILES, 8,
0, GL_RED, GL_FLOAT, spritePtabInput);
checkGlErrors(0);
}
void Screen::setSpritePatternTable(vector<float> spritePtabInput) {
assert(spritePtabInput.size() == 8*8*PATTERN_TABLE_TILES);
setSpritePatternTable(spritePtabInput.data());
}
void Screen::setOam(unsigned char *oamBytes) {
// Not bothering to write the struct interface, because we'll only
// actually use this by taking byte-array input from the NES.
memcpy(oam, oamBytes, OAM_SIZE);
}
void Screen::setMask(unsigned char m) {
maskState = m;
}
/* Record the start of a scroll region according to its scroll-space
* offset and pixel-space start coordinates.
*
* The first scroll region of a frame must start at pixel space (0,0).
* Each subsequent scroll regions must start later than the last one,
* in lexicographic order on pixel-space y and x coordinates. If the
* pixel-space coordinates are the same as the last scroll region,
* this doesn't create a new scroll region but instead updates the
* last one.
*/
void Screen::startScrollRegion(scroll_coord x_offset, scroll_coord y_offset,
pixel_coord x_start, pixel_coord y_top) {
assert(x_start < VISIBLE_COLUMNS);
assert(y_top < VISIBLE_SCANLINES);
if (!scrollChanges.empty() &&
(y_top == scrollChanges.back().ps_y_top) &&
(x_start == scrollChanges.back().ps_x_start)) {
// We're updating an old scroll region
scrollChanges.back().ss_x_offset = x_offset;
scrollChanges.back().ss_y_offset = y_offset;
} else {
// We're starting a new scroll region
if (scrollChanges.empty()) {
// First scroll region needs to start at pixel space (0,0)
assert(y_top == 0);
assert(x_start == 0);
} else {
// Enforce lexicographic ordering of scroll regions
assert((y_top > scrollChanges.back().ps_y_top) ||
((y_top == scrollChanges.back().ps_y_top) &&
(x_start > scrollChanges.back().ps_x_start)));
}
scrollChanges.push_back({x_offset, y_offset, x_start, y_top});
}
}
void Screen::setScrollType(ScrollType st) {
cerr << "Changing scroll types is not implemented\n";
die();
}
void Screen::drawToBuffer() {
// State we need by the time we finish this:
// For all:
// - universalBg needs to be set to the universal background palette index
// For background:
// - bgPalettes needs to be set to a buffer with the local palettes
// - BG_PATTERN_TABLE_TEXTURE needs to be populated with the background pattern table
// - we need data for the actual tiles (tileIndices and paletteIndices from the python, set by the ppu)
// (so maybe keep the ppu setting that, and then move it from python to c++)
// - scroll coordinates need to be set (currently assuming they're consistent in a frame)
// For sprites:
// - spritePalettes needs to be set to a buffer with the local palettes
// - SPRITE_PATTERN_TABLE_TEXTURE needs to be poulated with the sprite pattern table
// - oam needs to be set to the OAM contents
assert(universalBg < N_PALETTES);
// Do not confuse universalBgPalette with bgPalettes!
// universalBgPalette refers to the single background color, and
// bgPalettes refers to the set of background tiles that make up a
// lot of our rendering.
unsigned char *universalBgPalette = PALETTE[universalBg];
glClearColor(((float) universalBgPalette[0]) / 255.0,
((float) universalBgPalette[1]) / 255.0,
((float) universalBgPalette[2]) / 255.0,
1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
if (maskState & MASK_MASK_BKG) {
drawBg();
}
if (maskState & MASK_MASK_SPRITE) {
drawSprites();
}
// Drawing is done. Now set up the next frame.
// TODO: This isn't the right place to do the work that this
// function does. It currently depends on the scroll position at the
// start of the frame.
setupFrame();
}
int Screen::draw() {
glfwSwapBuffers(window);
glfwPollEvents();
if (glfwWindowShouldClose(window)) {
glfwTerminate();
return 1;
}
return 0;
}
/*
* So for accurate behavior, our background drawing will actually have
* to work something like this:
*
* - First of all, we throw away our assumption that we have a fixed
* number of tiles we're drawing to opengl. That won't be a problem,
* just track things in a vector.
*
* - We simulate scanning across the image, pixel by pixel. (Of
* course, we'll optimize this so we don't actually spend time
* thinking about every pixel on the screen)
*
* - For every pixel, if we haven't drawn a tile to cover it, we'll
* draw the appropriate tile. Depending on the scroll coordinates,
* the top-left of the tile will either be exactly on that pixel, or
* up to 7 tiles above and/or to the left of it.
*
* - We will also track a list of changes to the scroll variables.
* When we hit a change, we'll have to render new tiles for every
* subsequent pixel. This may involve rendering only part of a tile,
* in case e.g. we start drawing halfway down a tile. We can do that
* by drawing a rectangle and changing the uv coordinates
* correspondingly. For every change we hit, we'll increment a
* z-coordinate so that the new tiles overlap the old ones.
*/
void Screen::drawVertices(
vector<glVertex> &vertices,
GLuint vbo,
GLuint ptabName,
GLenum ptabTex,
int ptabTexId,
float *palettes
) {
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// From python implementation comments:
// We need to do this here (anytime before the draw call) and I
// don't understand why. The order is important for some reason.
glActiveTexture(ptabTex);
glBindTexture(GL_TEXTURE_2D, ptabName);
// do we need to call these again? unclear. python code did it but
// I don't know why.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
checkGlErrors(0);
int stride = sizeof(glVertex);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glVertex),
vertices.data(), GL_DYNAMIC_DRAW);
checkGlErrors(0);
glVertexAttribPointer(posAttrib, 2, GL_PIXEL_COORD, GL_FALSE, stride,
(const GLvoid *) offsetof(glVertex, x_pos));
glEnableVertexAttribArray(posAttrib);
checkGlErrors(0);
glVertexAttribPointer(priorityAttrib, 1, GL_FLOAT, GL_FALSE, stride,
(const GLvoid *) offsetof(glVertex, priority));
glEnableVertexAttribArray(priorityAttrib);
checkGlErrors(0);
glVertexAttribPointer(tileAttrib, 1, GL_PTAB_TILE_COORD, GL_FALSE, stride,
(const GLvoid *) offsetof(glVertex, tile));
glEnableVertexAttribArray(tileAttrib);
checkGlErrors(0);
glVertexAttribPointer(uvAttrib, 2, GL_PTAB_UV_COORD, GL_FALSE, stride,
(const GLvoid *) offsetof(glVertex, u));
glEnableVertexAttribArray(uvAttrib);
checkGlErrors(0);
glVertexAttribPointer(paletteNAttrib, 1, GL_UNSIGNED_BYTE, GL_FALSE, stride,
(const GLvoid *) offsetof(glVertex, palette));
glEnableVertexAttribArray(paletteNAttrib);
checkGlErrors(0);
glUniform1i(patternTableUniform, ptabTexId);
checkGlErrors(0);
// FIXME magic number 16
glUniform4fv(localPalettesUniform, 16, palettes);
checkGlErrors(0);
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
checkGlErrors(0);
}
void Screen::drawBg() {
/*
* Over the course of rendering, we'll need to transform between
* several different coordinate spaces. I'll give them some names
* here.
*
* /Pattern table space/ represents the contents of the NES's
* pattern tables, which store the images to be displayed for each
* tile. We represent pattern tables with three dimensions: the tile
* coordinate distinguishes between entries in a pattern table, and
* the u and v coordinates correspond to the horizontal and vertical
* location within a tile. The fragment shader does the job of
* converting coordinates in this space to actual colors.
*
* /Pixel space/ represents the NES's screen in pixel units. It
* ranges from (x = 0, y = 0) to (x = 256, y = 240).
*
* /Scroll space/ represents the image before scrolling is applied.
* Each of its dimensions is either equal to the corresponding pixel
* space dimension or double the corresponding dimension. Scroll
* space is toroidal: scrolling off the left of scroll space takes a
* tile to the right, and scrolling off the bottom takes it to the
* top.
*
* A /scroll region/ is a mapping between a region of pixel space
* and a region of scroll space. It's specified by three
* coordinates: a start coordinate in pixel space, an end coordinate
* in pixel space, and an offset coordinate in scroll space. We have
* (ssCoord = psCoord + ssOffset). Scroll regions aren't
* rectangular: they represent regions of time as the PPU sweeps
* across pixels, so they wrap around scanlines. For example, if a
* scroll region started 3/4 of the way through scanline 1 and ended
* 1/4 of the way through scanline 3, it would contain the last 1/4
* of scanline 1, all of scanline 2, and the first 1/4 of scanline
* 3:
*
* ..................++++++
* ++++++++++++++++++++++++
* ++++++..................
*
* /Nametable space/ is scroll space, but represented in tiles
* instead of pixels.
*
* /GL space/ is the coordinate space of the screen as OpenGL
* represents it after the vertex shader. The vertex shader is in
* charge of converting from pixel space to GL space; we don't need
* to worry about it anywhere else.
*
*
*
* So the way background rendering works is as follows:
*
* 1. Divide pixel space into scroll regions.
*
* 2. For each scroll region, look at the corresponding area in
* scroll space, and look at the tile boundaries, between the
* corresponding tiles in nametable space. Create one pixel-space
* rectangle in the vertex vector (six vertices) for each tile. Clip
* the rectangles on the top and left; don't bother for the bottom
* and right. For the tile containing the start of the scroll
* region, we may need to make two rectangles. Each vertex in these
* rectangles has u and v coordinates, corresponding to its location
* in the unclipped tile, and a tile coordinate from the tile's
* contents in nametable space. So each vertex has a pixel-space
* coordinate and a pattern-table-space coordinate.
*
* 3. The vertex shader sees the pixel-space coordinates of the
* polygons comprising the tiles, and maps them to GL space.
*
* 4. The fragment shader sees the pattern-table-space coordinates
* and maps them to actual colors (with the help of palettes).
*
*/
// For this function, I'll be using hungarian notation with prefix
// ss to represent screen-space coords, ps to represent pixel-space
// coords, and nts to represent nametable-space coords.
// TODO: actually do something with the z coord here. We need to
// increase it as we change scroll regions, pass it into the shader,
// and make the necessary changes for the shader to use it.
float priority = 0.0;
// Clear our vertex collection, but request that its memory not be
// deallocated. In practice, it looks like memory won't be
// deallocated under most compilers anyway, but (according to
// cplusplus.com) the spec doesn't guarantee either way.
bgVertices.reserve(bgVertices.size());
bgVertices.clear();
// The first element of the scroll changes vector needs to contain
// the initial scroll state of the frame. Assert that there is a
// first element and that it does in fact start at the beginning of
// the screen.
assert(scrollChanges.size() > 0);
assert(scrollChanges.at(0).ps_x_start == 0);
assert(scrollChanges.at(0).ps_y_top == 0);
for (vector<scrollChange>::iterator it = scrollChanges.begin();
it != scrollChanges.end();
it++) {
// pixel-space boundary coordinates of scroll region
pixel_coord psRegionXStart = it->ps_x_start;
pixel_coord psRegionYTop = it->ps_y_top;
pixel_coord psRegionXEnd, psRegionYBottom;
if (it+1 == scrollChanges.end()) {
psRegionYBottom = VISIBLE_SCANLINES - 1;
psRegionXEnd = VISIBLE_COLUMNS - 1;
} else {
if ((it+1)->ps_x_start > 0) {
psRegionYBottom = (it+1)->ps_y_top;
psRegionXEnd = (it+1)->ps_x_start - 1;
} else {
psRegionYBottom = (it+1)->ps_y_top - 1;
psRegionXEnd = VISIBLE_COLUMNS - 1;
}
// While we're here, check to make sure the scroll regions are
// strictly increasing over pixel space, lexicographically
assert(((it+1)->ps_y_top > it->ps_y_top)
||
(((it+1)->ps_y_top == it->ps_y_top)
&&
((it+1)->ps_x_start > it->ps_x_start)));
}
// We'd also better still be on the screen. This puts bounds on
// the start coordinates; between that and asserting that start
// coordinates are strictly increasing (lexicographically), we
// have a bound on how long this loop is allowed to continue, so
// we'll catch errors that'd make it go very long.
assert(psRegionXStart < VISIBLE_COLUMNS);
assert(psRegionYTop < VISIBLE_SCANLINES);
assert(psRegionXEnd < VISIBLE_COLUMNS);
assert(psRegionYBottom < VISIBLE_SCANLINES);
// scroll-space offset coordinates of scroll region
scroll_coord ssScrollX = it->ss_x_offset;
scroll_coord ssScrollY = it->ss_y_offset;
scroll_coord ssFineScrollX = ssScrollX % 8;
scroll_coord ssFineScrollY = ssScrollY % 8;
// Iterate through every tile represented in our scroll region.
// psTileTop and psTileLeft represent the top-left coordinates of
// the tile /before/ clipping.
// Start at the pixel-space boundaries of the scroll region and
// subtract the fine scroll. This will put us at the boundary of
// the first tile to appear on the screen.
for (pixel_coord psTileTop =
psRegionYTop - (pixel_coord) ssFineScrollY;
psTileTop < psRegionYBottom;
psTileTop += 8) {
scroll_coord ssTileTop =
(psTileTop + ssScrollY) % scrollHeight();
assert(0 <= ssTileTop < scrollHeight());
// Use div to make sure we do truncating division, no matter
// what type we've defined scroll_coord as. We can't just divide
// ssTileTop by 8 because ssTileTop will not be tile-aligned if
// we're at the top of the region and the region top is not
// aligned.
ntab_coord ntsTileY = (ntab_coord) div(ssTileTop, 8).quot;
// The x-coordinate starts similarly to the y-coordinate, except
// that it always starts at the left of the screen (and goes to
// the right).
for (pixel_coord psTileLeft =
- (pixel_coord) ssFineScrollX;
psTileLeft < VISIBLE_COLUMNS;
psTileLeft += 8) {
int ssTileLeft =
(psTileLeft + ssScrollX) % scrollWidth();
assert(0 <= ssTileLeft < scrollWidth());
// FIXME: similar to the y boundary, this will be misaligned
// if we're at the region top and our x-start is misaligned.
assert(ssTileLeft % 8 == 0);
ntab_coord ntsTileX = (ntab_coord) (ssTileLeft / 8);
/* TODO: clip the top and left of the tile, according to the
* following rules:
*
* - If we're above the top of the scroll region, and entirely
* to the left (i.e. our pixel-space x is >= the start
* coord's x), then clip y to the start coord's y, and clip
* v accordingly.
*
* - If we're above /or at/ the top, and entirely to the right
* (our rightmost pixel space x is < the start coord's x),
* then clip y to the start coord's y plus 1, and clip v
* accordingly.
*
* - If we're above or at the top, and our x boundaries
* straddle the start, then we actually need to spilt this
* into two rectangles.
*
*/
// Calculate rectangle boundaries. Top and left may not be
// tile-aligned, but bottom and right must be.
pixel_coord psRectLeft = psTileLeft;
pixel_coord psRectRight = psTileLeft + 8;
pixel_coord psRectTop = psTileTop;
pixel_coord psRectBottom = psTileTop + 8 - (pixel_coord) (ssTileLeft % 8);
// bottom-right of uv coordinates is always 1,1. Top-right is
// clipped if rectangle is clipped.
ptab_tile_coord tile = tileIndices[ntsTileX][ntsTileY];
ptab_uv_coord u_left = 0.0 + (psRectLeft - psTileLeft) / 8.0;
ptab_uv_coord u_right = 1.0;
ptab_uv_coord v_top = 0.0 + (psRectTop - psTileTop) / 8.0;
ptab_uv_coord v_bottom = 1.0;
int palette_index = paletteIndices[ntsTileX][ntsTileY];
appendRect(
bgVertices,
psRectLeft, psRectRight, psRectTop, psRectBottom,
priority, // TODO we don't do anything with priority so far
tile, u_left, u_right, v_top, v_bottom,
palette_index);
} // loop over x coordinates
} // loop over y coordinates
} // loop over scrollChanges
// We've set up the bgVertices vector, so now send over all those
// vertices to opengl.
drawVertices(bgVertices,
bgVbo, bgPtabName, BG_PATTERN_TABLE_TEXTURE,
BG_PATTERN_TABLE_TEXID, bgPalettes);
}
void Screen::drawSprites() {
glBindBuffer(GL_ARRAY_BUFFER, spriteVbo);
glActiveTexture(SPRITE_PATTERN_TABLE_TEXTURE);
glBindTexture(GL_TEXTURE_2D, spritePtabName);
// still don't remember why/whether these glTexParameteri calls
// are necessary
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
spriteVertices.resize(0);
for (int oam_i = 0; oam_i < OAM_ENTRIES; oam_i++) {
// TODO deal with maximum sprites per scanline
oamEntry sprite = oam[oam_i];
if (sprite.y_minus_one >= 0xef) {
// The sprite is wholly off the screen; ignore it
continue;
}
// preceding check ensures this won't overflow
unsigned char spritetop = sprite.y_minus_one + 1;
pixel_coord x_left = sprite.x;
pixel_coord x_right = sprite.x + 8;
pixel_coord y_top = sprite.y_minus_one + 1;
pixel_coord y_bottom = y_top + 8;
ptab_uv_coord u_left =
(sprite.attributes & OAM_FLIP_HORIZONTAL ? 1 : 0);
ptab_uv_coord u_right = 1 - u_left;
ptab_uv_coord v_top =
(sprite.attributes & OAM_FLIP_VERTICAL ? 1 : 0);
ptab_uv_coord v_bottom = 1 - v_top;
ptab_tile_coord tile = sprite.tile;
unsigned char palette_index = (sprite.attributes & OAM_PALETTE);
if (DONKEY_KONG_BIG_HEAD_MODE) {
// The main head tiles seem to be the even-numbered tiles in
// the first 4 rows of 16 tiles each. (There are more for
// various special states but I'll ignore them.)
if ((tile < 0x40) && ((tile % 2) == 0)) {
// I forgot to check for overflow here but the result is
// hilarious so I'm gonna leave it in
y_top += DONKEY_KONG_BIG_HEAD_INCREASE;
// The back-of-head tiles seem to be at multiples of 4, and
// the front-of-head tiles are at 2 plus a multiple of 4.
// They're all facing to the right.
int is_head_front = (tile % 4);
// Stretch front head to the right and back head to the
// left, unless it's horizontally mirrored.
if ((!!is_head_front) != (!!(sprite.attributes & OAM_FLIP_HORIZONTAL))) {
x_right += DONKEY_KONG_BIG_HEAD_INCREASE;
} else {
if (x_left <= DONKEY_KONG_BIG_HEAD_INCREASE) {
x_left = 0;
} else {
x_left -= DONKEY_KONG_BIG_HEAD_INCREASE;
}
}
}
}
appendRect(
spriteVertices,
x_left, x_right,
y_top, y_bottom,
0.1, // TODO priority
tile,
u_left, u_right,
v_top, v_bottom,
palette_index);
}
drawVertices(spriteVertices,
spriteVbo, spritePtabName,
SPRITE_PATTERN_TABLE_TEXTURE,
SPRITE_PATTERN_TABLE_TEXID,
spritePalettes);
}
void Screen::appendRect(
vector<glVertex> &vertices,
pixel_coord x_left, pixel_coord x_right,
pixel_coord y_top, pixel_coord y_bottom,
float priority,
ptab_tile_coord tile,
ptab_uv_coord u_left, ptab_uv_coord u_right,
ptab_uv_coord v_top, ptab_uv_coord v_bottom,
unsigned char palette_index
) {
glVertex bottomLeft =
{x_left, y_bottom, priority,
tile, u_left, v_bottom, palette_index};
glVertex bottomRight =
{x_right, y_bottom, priority,
tile, u_right, v_bottom, palette_index};
glVertex topLeft =
{x_left, y_top, priority,
tile, u_left, v_top, palette_index};
glVertex topRight =
{x_right, y_top, priority,
tile, u_right, v_top, palette_index};
// first triangle
vertices.push_back(bottomLeft);
vertices.push_back(bottomRight);
vertices.push_back(topRight);
// second triangle
vertices.push_back(bottomLeft);
vertices.push_back(topRight);
vertices.push_back(topLeft);
}
// Polls for keys, returns their state in a one-byte bitfield (masks
// defined in header)
unsigned char Screen::pollKeys() {
// Currently, we're polling events here and in draw(). Is that a
// problem? Not sure.
glfwPollEvents();
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
exit(0);
}
unsigned char out = 0;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
out |= KEY_MASK_A;
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
out |= KEY_MASK_B;
}
if (glfwGetKey(window, GLFW_KEY_BACKSLASH) == GLFW_PRESS) {
out |= KEY_MASK_SELECT;
}
if (glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_PRESS) {
out |= KEY_MASK_START;
}
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
out |= KEY_MASK_UP;
}
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
out |= KEY_MASK_DOWN;
}
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
out |= KEY_MASK_LEFT;
}
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
out |= KEY_MASK_RIGHT;
}
return out;