FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
spearow/screen.cpp at master · mkbehr/spearow · GitHub
mkbehr
/
spearow
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
spearow
/
screen.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
680 lines (570 loc) · 18.8 KB
Breadcrumbs
spearow
/
screen.cpp
Copy path
File metadata and controls
680 lines (570 loc) · 18.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
<
cstddef
>
#
include
<
iostream
>
#
include
<
string
>
#
include
<
sstream
>
#
include
<
fstream
>
#
include
<
cassert
>
#
include
<
stdnoreturn.h
>
#
include
<
unistd.h
>
#
include
<
sys/param.h
>
#
include
"
screen.hpp
"
#
include
"
mem.hpp
"
const
char
*
PROGRAM_NAME
=
"
Spearow
"
;
const
char
*
VERTEX_SHADER_FILE
=
"
vertex.vert
"
;
const
char
*
FRAGMENT_SHADER_FILE
=
"
fragment.frag
"
;
#
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);
std::cerr << errloc
<<
"
: GL error:
"
<< errMsg
<<
"
(
"
<< std::hex << err <<
"
)
\n
"
;
if
(!continue_after_err) {
die
();
}
}
}
Screen::Screen
(
CPU
*c,
bool
vsyncParam,
bool
displayTiles)
: vsync(vsyncParam), cpu(c), tileWindow(
NULL
)
{
//
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
)) {
std::cerr <<
"
Couldn't get working directory
\n
"
;
die
();
}
if
(
initWindow
(&window) <
0
) {
std::cerr <<
"
Couldn't create window
\n
"
;
die
();
}
if
(
chdir
(cwd) !=
0
) {
std::cerr <<
"
Couldn't reset working directory
\n
"
;
die
();
}
//
bind a vertex array
glGenVertexArrays
(
1
, &bgVao);
checkGlErrors
(
0
);
glBindVertexArray
(bgVao);
checkGlErrors
(
0
);
initShaders
();
glGenBuffers
(
1
, &bgVbo);
glGenTextures
(
1
, &texName);
checkGlErrors
(
0
);
glfwSetInputMode
(window,
GLFW_STICKY_KEYS
,
1
);
glClearColor
(
0.0
,
0.0
,
0.0
,
0.0
);
glClear
(
GL_COLOR_BUFFER_BIT
|
GL_DEPTH_BUFFER_BIT
);
glfwSwapBuffers
(window);
glfwPollEvents
();
checkGlErrors
(
0
);
//
TODO: consider a call to glfwSwapInterval to disable vsync
if
(!vsync) {
glfwSwapInterval
(
0
);
}
if
(displayTiles) {
initTileWindow
();
}
}
void
Screen::draw
() {
//
switch contexts here, but don't bother unless we actually have
//
multiple contexts to switch between.
if
(tileWindow) {
glfwMakeContextCurrent
(window);
}
drawMainWindow
();
if
(tileWindow) {
glfwMakeContextCurrent
(tileWindow);
drawTileWindow
();
}
}
void
Screen::drawMainWindow
() {
//
start slow, make it work
float
pixels[
144
*
160
];
if
(cpu->
lcd_control
&
LCDC_BG_DISPLAY
) {
drawBackground
(pixels);
}
if
(cpu->
lcd_control
&
LCDC_WINDOW_DISPLAY
) {
drawWindow
(pixels);
}
if
(cpu->
lcd_control
&
LCDC_SPRITE_DISPLAY
) {
drawSprites
(pixels);
}
glBindVertexArray
(bgVao);
checkGlErrors
(
0
);
glBindBuffer
(
GL_ARRAY_BUFFER
, bgVbo);
glActiveTexture
(
GL_TEXTURE0
);
glBindTexture
(
GL_TEXTURE_2D
, texName);
glTexImage2D
(
GL_TEXTURE_2D
,
0
,
GL_RED
,
160
,
144
,
0
,
GL_RED
,
GL_FLOAT
, pixels);
checkGlErrors
(
0
);
glClearColor
(
0.0
,
0.0
,
0.0
,
0.0
);
glClear
(
GL_COLOR_BUFFER_BIT
|
GL_DEPTH_BUFFER_BIT
);
//
TODO what does this do again?
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_MIN_FILTER
,
GL_NEAREST
);
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_MAG_FILTER
,
GL_NEAREST
);
checkGlErrors
(
0
);
glActiveTexture
(
GL_TEXTURE0
);
checkGlErrors
(
0
);
glBindTexture
(
GL_TEXTURE_2D
, texName);
//
do we need this twice?
checkGlErrors
(
0
);
vertex vs[
6
];
vs[
0
] = {
0.0
,
0.0
};
vs[
1
] = {
0.0
,
1.0
};
vs[
2
] = {
1.0
,
1.0
};
vs[
3
] = {
0.0
,
0.0
};
vs[
4
] = {
1.0
,
1.0
};
vs[
5
] = {
1.0
,
0.0
};
int
stride =
sizeof
(vertex);
glBufferData
(
GL_ARRAY_BUFFER
,
6
* stride, vs,
GL_DYNAMIC_DRAW
);
checkGlErrors
(
0
);
glVertexAttribPointer
(posAttrib,
2
,
GL_FLOAT
,
GL_FALSE
, stride,
(
const
GLvoid *)
offsetof
(vertex, x));
glEnableVertexAttribArray
(posAttrib);
checkGlErrors
(
0
);
glUniform1i
(texUniform,
0
);
//
0 corresponds to GL_TEXTURE0
checkGlErrors
(
0
);
glDrawArrays
(
GL_TRIANGLES
,
0
,
6
);
checkGlErrors
(
0
);
glfwSwapBuffers
(window);
glfwPollEvents
();
if
(
glfwWindowShouldClose
(window)) {
die
();
}
}
void
Screen::drawBackground
(
float
*pixels) {
const
uint16_t
bg_base = (cpu->
lcd_control
&
LCDC_BG_CODE
)
?
0x9c00
:
0x9800
;
const
bool
tile_signed = !(cpu->
lcd_control
&
LCDC_BG_CHR
);
const
uint16_t
tile_base = tile_signed ?
0x9000
:
0x8000
;
for
(
int
screenY =
0
; screenY <
144
; screenY++) {
int
scrollspaceY = (screenY + cpu->
scroll_y
) %
256
;
int
tileY = scrollspaceY /
8
;
for
(
int
screenX =
0
; screenX <
160
; screenX++) {
int
scrollspaceX = (screenX + cpu->
scroll_x
) %
256
;
int
tileX = scrollspaceX /
8
;
int
block = tileY *
32
+ tileX;
uint8_t
tile_code =
gb_mem_ptr
(*cpu, bg_base + block).
read
();
int
tile_n = tile_signed ? (
int8_t
) tile_code : tile_code;
uint16_t
tile_addr = tile_base + tile_n *
16
;
uint8_t
low_byte =
gb_mem_ptr
(*cpu,
tile_addr + (scrollspaceY%
8
)*
2
).
read
();
uint8_t
high_byte =
gb_mem_ptr
(*cpu,
tile_addr + (scrollspaceY%
8
)*
2
+
1
).
read
();
int
out =
0
;
if
(low_byte & (
1
<<(
7
-(scrollspaceX%
8
)))) {
out +=
1
;
}
if
(high_byte & (
1
<<(
7
-(scrollspaceX%
8
)))) {
out +=
2
;
}
int
shade =
3
- ((cpu->
bg_palette
& (
3
<< (out*
2
))) >> (out*
2
));
pixels[screenX + (screenY*
160
)] = shade /
3.0
;
}
}
}
void
Screen::drawSprites
(
float
*pixels) {
const
uint16_t
tile_base =
0x8000
;
const
bool
big_sprites = !!(cpu->
lcd_control
&
LCDC_SPRITE_SIZE
);
//
TODO handle overlapping sprites
//
TODO handle sprite-per-scanline limitation
for
(
int
i =
0
; i <
OAM_N_SPRITES
; i++) {
uint16_t
sprite_base =
OAM_BASE
+ i *
SPRITE_SIZE
;
uint8_t
y =
gb_mem_ptr
(*cpu, sprite_base +
0
).
read
();
uint8_t
x =
gb_mem_ptr
(*cpu, sprite_base +
1
).
read
();
uint8_t
chr =
gb_mem_ptr
(*cpu, sprite_base +
2
).
read
();
if
(big_sprites) {
//
clear least-significant bit
chr &= ~
1
;
}
uint8_t
flags =
gb_mem_ptr
(*cpu, sprite_base +
3
).
read
();
uint16_t
tile_addr = tile_base + chr *
16
;
bool
priority = !!(flags &
SPRITE_PRIORITY
);
bool
flipVert = !!(flags &
SPRITE_FLIP_V
);
bool
flipHoriz = !!(flags &
SPRITE_FLIP_H
);
uint8_t
palette = (flags &
SPRITE_PALETTE
) ? cpu->
obj_palette_1
: cpu->
obj_palette_0
;
int
color = flags &
SPRITE_COLOR
;
int
top = y -
SPRITE_Y_OFFSET
;
int
left = x -
SPRITE_X_OFFSET
;
int
height = big_sprites ?
16
:
8
;
for
(
int
screenY = top; screenY < top + height; screenY++) {
if
((screenY <
0
) || (screenY >=
SCREEN_HEIGHT
)) {
continue
;
}
int
dy = flipVert ? (height -
1
) - (screenY - top) : screenY - top;
for
(
int
screenX = left; screenX < left +
8
; screenX++) {
if
((screenX <
0
) || (screenX >=
SCREEN_WIDTH
)) {
continue
;
}
int
dx = flipHoriz ? (
7
- (screenX - left)) : (screenX - left);
uint8_t
low_byte =
gb_mem_ptr
(*cpu, tile_addr + (dy%
8
)*
2
).
read
();
uint8_t
high_byte =
gb_mem_ptr
(*cpu, tile_addr + (dy%
8
)*
2
+
1
).
read
();
int
pixel =
0
;
if
(low_byte & (
1
<<(
7
-(dx%
8
)))) {
pixel +=
1
;
}
if
(high_byte & (
1
<<(
7
-(dx%
8
)))) {
pixel +=
2
;
}
if
(pixel) {
//
color 0 is transparent
int
shade =
3
- ((palette & (
3
<< (pixel*
2
))) >> (pixel*
2
));
//
TODO test priority and bg pixel
pixels[screenX + (screenY*
160
)] = shade /
3.0
;
}
}
}
}
}
void
Screen::drawWindow
(
float
*pixels) {
const
uint16_t
bg_base = (cpu->
lcd_control
&
LCDC_WINDOW_CODE
)
?
0x9c00
:
0x9800
;
const
bool
tile_signed = !(cpu->
lcd_control
&
LCDC_BG_CHR
);
const
uint16_t
tile_base = tile_signed ?
0x9000
:
0x8000
;
int
screenTop = cpu->
window_y
;
int
screenLeft = cpu->
window_x
-
7
;
if
((screenTop >=
SCREEN_HEIGHT
) ||
(screenLeft <
0
) ||
(screenLeft >=
SCREEN_WIDTH
)) {
return
;
}
for
(
int
y = screenTop; y <
SCREEN_HEIGHT
; y++) {
int
tileY = (y - screenTop) /
8
;
for
(
int
x = screenLeft; x <
SCREEN_WIDTH
; x++) {
int
tileX = (x - screenLeft) /
8
;
int
block = tileY *
32
+ tileX;
uint8_t
tile_code =
gb_mem_ptr
(*cpu, bg_base + block).
read
();
int
tile_n = tile_signed ? (
int8_t
) tile_code : tile_code;
uint16_t
tile_addr = tile_base + tile_n *
16
;
uint8_t
low_byte =
gb_mem_ptr
(*cpu, tile_addr + (y%
8
)*
2
).
read
();
uint8_t
high_byte =
gb_mem_ptr
(*cpu, tile_addr + (y%
8
)*
2
+
1
).
read
();
int
out =
0
;
if
(low_byte & (
1
<<(
7
-(x%
8
)))) {
out +=
1
;
}
if
(high_byte & (
1
<<(
7
-(x%
8
)))) {
out +=
2
;
}
//
window uses bg palette
int
shade =
3
- ((cpu->
bg_palette
& (
3
<< (out*
2
))) >> (out*
2
));
pixels[x + (y*
160
)] = shade /
3.0
;
}
}
}
void
Screen::drawTileWindow
() {
const
uint16_t
tile_base =
0x8000
;
const
uint16_t
n_tiles = (
0x9800
-
0x8000
) /
16
;
//
Display a 16*24 window of tiles
float
pixels[
16
*
8
*
24
*
8
];
for
(
int
y =
0
; y <
24
*
8
; y++) {
int
tileY = y /
8
;
for
(
int
x =
0
; x <
16
*
8
; x++) {
int
tileX = x /
8
;
int
tile_n = tileX + tileY *
16
;
assert
(tile_n < n_tiles);
uint16_t
tile_addr = tile_base + tile_n *
16
;
uint8_t
low_byte =
gb_mem_ptr
(*cpu, tile_addr + (y%
8
)*
2
).
read
();
uint8_t
high_byte =
gb_mem_ptr
(*cpu, tile_addr + (y%
8
)*
2
+
1
).
read
();
int
out =
0
;
if
(low_byte & (
1
<<(
7
-(x%
8
)))) {
out +=
1
;
}
if
(high_byte & (
1
<<(
7
-(x%
8
)))) {
out +=
2
;
}
pixels[x + (y*
16
*
8
)] = out /
3.0
;
}
}
glBindVertexArray
(tileVao);
checkGlErrors
(
0
);
glBindBuffer
(
GL_ARRAY_BUFFER
, tileVbo);
glActiveTexture
(
GL_TEXTURE1
);
glBindTexture
(
GL_TEXTURE_2D
, tileWindowTexName);
glTexImage2D
(
GL_TEXTURE_2D
,
0
,
GL_RED
,
16
*
8
,
24
*
8
,
0
,
GL_RED
,
GL_FLOAT
, pixels);
checkGlErrors
(
0
);
glClearColor
(
0.0
,
0.0
,
0.0
,
0.0
);
glClear
(
GL_COLOR_BUFFER_BIT
|
GL_DEPTH_BUFFER_BIT
);
//
TODO what does this do again?
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_MIN_FILTER
,
GL_NEAREST
);
glTexParameteri
(
GL_TEXTURE_2D
,
GL_TEXTURE_MAG_FILTER
,
GL_NEAREST
);
checkGlErrors
(
0
);
glActiveTexture
(
GL_TEXTURE1
);
checkGlErrors
(
0
);
glBindTexture
(
GL_TEXTURE_2D
, tileWindowTexName);
//
do we need this twice?
checkGlErrors
(
0
);
vertex vs[
6
];
vs[
0
] = {
0.0
,
0.0
};
vs[
1
] = {
0.0
,
1.0
};
vs[
2
] = {
1.0
,
1.0
};
vs[
3
] = {
0.0
,
0.0
};
vs[
4
] = {
1.0
,
1.0
};
vs[
5
] = {
1.0
,
0.0
};
int
stride =
sizeof
(vertex);
glBufferData
(
GL_ARRAY_BUFFER
,
6
* stride, vs,
GL_DYNAMIC_DRAW
);
checkGlErrors
(
0
);
glVertexAttribPointer
(posAttrib,
2
,
GL_FLOAT
,
GL_FALSE
, stride,
(
const
GLvoid *)
offsetof
(vertex, x));
checkGlErrors
(
0
);
glEnableVertexAttribArray
(posAttrib);
checkGlErrors
(
0
);
glUseProgram
(shader);
//
why do I need this here? it is a mystery
glUniform1i
(texUniform,
1
);
//
1 corresponds to GL_TEXTURE1
checkGlErrors
(
0
);
glDrawArrays
(
GL_TRIANGLES
,
0
,
6
);
checkGlErrors
(
0
);
glfwSwapBuffers
(tileWindow);
glfwPollEvents
();
if
(
glfwWindowShouldClose
(tileWindow)) {
die
();
}
}
int
initWindow
(GLFWwindow **window_p) {
/*
Initialize the library
*/
if
(!
glfwInit
()) {
std::cerr <<
"
glfwInit failed
\n
"
;
return
-
1
;
}
glfwWindowHint
(
GLFW_CONTEXT_VERSION_MAJOR
,
3
);
glfwWindowHint
(
GLFW_CONTEXT_VERSION_MINOR
,
2
);
glfwWindowHint
(
GLFW_OPENGL_FORWARD_COMPAT
,
GL_TRUE
);
glfwWindowHint
(
GLFW_OPENGL_PROFILE
,
GLFW_OPENGL_CORE_PROFILE
);
/*
Create a windowed mode window and its OpenGL context
*/
GLFWwindow *window;
window =
glfwCreateWindow
(
SCREEN_WIDTH
*
2
,
SCREEN_HEIGHT
*
2
,
PROGRAM_NAME
,
NULL
,
NULL
);
if
(!window)
{
std::cerr <<
"
glfwCreateWindow failed
\n
"
;
glfwTerminate
();
return
-
1
;
}
if
(
glfwGetWindowAttrib
(window,
GLFW_CONTEXT_VERSION_MAJOR
) <
3
) {
std::cerr <<
"
initWindow: Error: GL major version too low
\n
"
;
die
();
}
/*
Make the window's context current
*/
glfwMakeContextCurrent
(window);
*window_p = window;
return
0
;
}
void
Screen::initTileWindow
() {
//
TODO refactor this, initWindow, and constructor. code reuse is
//
weird here.
glfwWindowHint
(
GLFW_CONTEXT_VERSION_MAJOR
,
3
);
glfwWindowHint
(
GLFW_CONTEXT_VERSION_MINOR
,
2
);
glfwWindowHint
(
GLFW_OPENGL_FORWARD_COMPAT
,
GL_TRUE
);
glfwWindowHint
(
GLFW_OPENGL_PROFILE
,
GLFW_OPENGL_CORE_PROFILE
);
/*
Create a windowed mode window and its OpenGL context
*/
const
static
char
*tileWindowName =
"
Tiles
"
;
const
int
tileWindowWidth =
16
*
8
*
2
;
const
int
tileWindowHeight =
20
*
8
*
2
;
tileWindow =
glfwCreateWindow
(tileWindowWidth, tileWindowHeight,
tileWindowName,
NULL
,
//
share context with existing window
window);
if
(!tileWindow)
{
std::cerr <<
"
glfwCreateWindow failed
\n
"
;
glfwTerminate
();
}
if
(
glfwGetWindowAttrib
(tileWindow,
GLFW_CONTEXT_VERSION_MAJOR
) <
3
) {
std::cerr <<
"
initWindow: Error: GL major version too low
\n
"
;
die
();
}
//
move tile window so it doesn't overlap with main window
int
mainX, mainY;
glfwGetWindowPos
(window, &mainX, &mainY);
glfwSetWindowPos
(tileWindow, mainX - tileWindowWidth -
20
, mainY);
glfwMakeContextCurrent
(tileWindow);
if
(!vsync) {
glfwSwapInterval
(
0
);
}
glClearColor
(
0.0
,
0.0
,
0.0
,
0.0
);
glClear
(
GL_COLOR_BUFFER_BIT
|
GL_DEPTH_BUFFER_BIT
);
glfwSwapBuffers
(window);
glfwPollEvents
();
checkGlErrors
(
0
);
glGenVertexArrays
(
1
, &tileVao);
checkGlErrors
(
0
);
glBindVertexArray
(tileVao);
checkGlErrors
(
0
);
glGenBuffers
(
1
, &tileVbo);
glGenTextures
(
1
, &tileWindowTexName);
checkGlErrors
(
0
);
//
not sure exactly how much is shared between the windows - they
//
should use the same context, but it seems that we have to install
//
the shader program again
glUseProgram
(shader);
checkGlErrors
(
0
);
}
GLint
safeGetAttribLocation
(GLuint program,
const
GLchar *name) {
GLint loc =
glGetAttribLocation
(program, name);
checkGlErrors
(
0
);
if
(loc <
0
) {
std::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
) {
std::cerr <<
"
Couldn't get program uniform
"
<< name <<
"
\n
"
;
die
();
}
return
loc;
}
void
Screen::initShaders
(
void
) {
//
get shaders
std::ifstream
vertFile
(
VERTEX_SHADER_FILE
);
if
(!vertFile) {
std::cerr <<
"
Couldn't open vertex shader file
\n
"
;
die
();
}
std::stringstream vertBuffer;
vertBuffer << vertFile.
rdbuf
();
std::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
();
std::ifstream
fragFile
(
FRAGMENT_SHADER_FILE
);
if
(!fragFile) {
std::cerr <<
"
Couldn't open fragment shader file
\n
"
;
die
();
}
std::stringstream fragBuffer;
fragBuffer << fragFile.
rdbuf
();
std::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) {
std::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
std::cout << log;
}
else
{
std::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) {
std::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
std::cout << log;
}
else
{
std::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) {
std::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
std::cout << log;
}
else
{
std::cerr <<
"
Couldn't get log message
\n
"
;
}
die
();
}
glUseProgram
(shader);
checkGlErrors
(
0
);
//
shader attribute/uniform pointers
posAttrib =
safeGetAttribLocation
(shader,
"
pos
"
);
texUniform =
safeGetUniformLocation
(shader,
"
tex
"
);
}
uint8_t
Screen::getKeys
(
uint8_t
inputFlags) {
//
COMPAT: I'm not sure what happens when both JOYPAD_DIRECTIONS and
//
JOYPAD_BUTTONS bits are set low. This is just a guess.
//
COMPAT: Unclear what the top nibble of this should be. Guessing
//
it can be 0 for now.
uint8_t
out =
0xf
;
if
(!(inputFlags &
JOYPAD_DIRECTIONS
)) {
if
(
glfwGetKey
(window,
GLFW_KEY_RIGHT
) ==
GLFW_PRESS
) {
out &= ~
JOYPAD_RIGHT
;
}
if
(
glfwGetKey
(window,
GLFW_KEY_LEFT
) ==
GLFW_PRESS
) {
out &= ~
JOYPAD_LEFT
;
}
if
(
glfwGetKey
(window,
GLFW_KEY_UP
) ==
GLFW_PRESS
) {
out &= ~
JOYPAD_UP
;
}
if
(
glfwGetKey
(window,
GLFW_KEY_DOWN
) ==
GLFW_PRESS
) {
out &= ~
JOYPAD_DOWN
;
}
}
if
(!(inputFlags &
JOYPAD_BUTTONS
)) {
if
(
glfwGetKey
(window,
GLFW_KEY_S
) ==
GLFW_PRESS
) {
out &= ~
JOYPAD_A
;
}
if
(
glfwGetKey
(window,
GLFW_KEY_A
) ==
GLFW_PRESS
) {
out &= ~
JOYPAD_B
;
}
if
(
glfwGetKey
(window,
GLFW_KEY_BACKSLASH
) ==
GLFW_PRESS
) {
out &= ~
JOYPAD_SELECT
;
}
if
(
glfwGetKey
(window,
GLFW_KEY_ENTER
) ==
GLFW_PRESS
) {
out &= ~
JOYPAD_START
;
}
}
return
out;
}
Back
|
FazBrowse Home
|
New Git URL