-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.cpp
388 lines (305 loc) · 11.8 KB
/
display.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
#include "display.h"
void Display::init(Memory* _memory)
{
memory = _memory;
int scale = 5;
window.create(sf::VideoMode(width, height), "ComradeTech Gameboy");
window.setSize(sf::Vector2u(width * scale, height * scale));
window.setKeyRepeatEnabled(false);
bg_array.create(160, 144, sf::Color(255, 0, 255));
window_array.create(160, 144, sf::Color(0, 0, 0, 0));
sprites_array.create(160, 144, sf::Color(0, 0, 0, 0)); // ïðîçðà÷íûé
shades_of_gray[0x0] = sf::Color(255, 255, 255); // 0x0 - Áåëûé
shades_of_gray[0x1] = sf::Color(198, 198, 198); // 0x1 - Ñâåòëî-ñåðûé
shades_of_gray[0x2] = sf::Color(127, 127, 127); // 0x2 - Òåìíî-ñåðûé
shades_of_gray[0x3] = sf::Color(0, 0, 0); // 0x3 - ×åðíûé/**/
}
void Display::render()
{
if (!is_lcd_enabled())
return;
window.clear(sf::Color::Transparent);
// î÷èñòêà ñóùåñòâóþùèõ äàííûõ ñïðàéòîâ è îêíà
sprites_array.create(160, 144, sf::Color::Transparent);
bool do_sprites = memory->LCDC.is_bit_set(BIT_1);
if (do_sprites)
render_sprites();
sf::Texture bg_texture;
sf::Texture window_texture;
sf::Texture sprites_texture;
window_texture.loadFromImage(window_array);
bg_texture.loadFromImage(bg_array);
sprites_texture.loadFromImage(sprites_array);
window_sprite.setTexture(window_texture);
bg_sprite.setTexture(bg_texture);
sprites_sprite.setTexture(sprites_texture);
window.draw(bg_sprite);
window.draw(window_sprite);
window.draw(sprites_sprite);
window.display();
}
void Display::clear_window()
{
window_array.create(160, 144, sf::Color::Transparent);
}
void Display::update_scanline(Byte current_scanline)
{
scanlines_rendered++;
bool do_background = memory->LCDC.is_bit_set(BIT_0);
bool do_window = memory->LCDC.is_bit_set(BIT_5);
if (do_background)
update_bg_scanline(current_scanline);
if (do_window)
update_window_scanline(current_scanline);
}
void Display::update_bg_scanline(Byte current_scanline)
{
bool bg_code_area = memory->LCDC.is_bit_set(BIT_3);
if (debug_enabled)
{
bg_code_area = force_bg_map;
}
Address tile_map_location = (bg_code_area) ? 0x9C00 : 0x9800;
Byte scroll_x = memory->SCX.get();
Byte scroll_y = memory->SCY.get();
//cout << hex << (int)scroll_x << " y: " << (int)scroll_y << endl;
Byte palette = memory->BGP.get();
// Äëÿ êàæäîãî ïèêñåëÿ â ñòðîêå 160x1:
// 1. Âû÷èñëÿåì, ãäå íàõîäèòñÿ ïèêñåëü â îáùåé êàðòå ôîíà 256x256, ñ ó÷åòîì ScrollX è Y
// 2. Ïîëó÷àåì èäåíòèôèêàòîð òàéëà, íà êîòîðîì íàõîäèòñÿ ýòîò ïèêñåëü
// 3. Ïîëó÷àåì öâåò ïèêñåëÿ íà îñíîâå êîîðäèíàò îòíîñèòåëüíî ñåòêè òàéëîâ 8x8
// 4. Îòîáðàæàåì ïèêñåëü â 160x144 âèäå äèñïëåÿ
int y = current_scanline;
// Èòåðèðóåì ñëåâà íàïðàâî ïî ýêðàíó îòîáðàæåíèÿ (x = 0 -> 160)
for (int x = 0; x < 160; x++)
{
// 1. Ïîëó÷àåì êîîðäèíàòû ïèêñåëÿ X,Y â îáùåé êàðòå ôîíà, ñ ó÷åòîì ScrollX è Y
int map_x = (int)scroll_x + x;
int map_y = (int)scroll_y + y;
// Êîððåêòèðóåì êîîðäèíàòû êàðòû, åñëè îíè âûõîäÿò çà ïðåäåëû îáëàñòè 256x256, ÷òîáû çàöèêëèòüñÿ
map_x = (map_x >= 256) ? map_x - 256 : map_x;
map_y = (map_y >= 256) ? map_y - 256 : map_y;
// 2. Ïîëó÷àåì èäåíòèôèêàòîð òàéëà, íà êîòîðîì íàõîäèòñÿ ýòîò ïèêñåëü
int tile_col = floor(map_x / 8);
int tile_row = floor(map_y / 8);
int tile_map_id = (tile_row * 32) + tile_col;
Address loc = tile_map_location + tile_map_id;
Byte tile_id = memory->read(loc);
// 3. Ïîëó÷àåì öâåò ïèêñåëÿ íà îñíîâå êîîðäèíàò îòíîñèòåëüíî ñåòêè òàéëîâ 8x8
// 4. Îòîáðàæàåì ïèêñåëü â 160x144 âèäå äèñïëåÿ
int tile_x_pixel = map_x % 8;
int tile_y_pixel = map_y % 8;
// Èíâåðòèðóåì ïèêñåëè X, ïîòîìó ÷òî îíè õðàíÿòñÿ çàäîì íàïåðåä
tile_x_pixel = abs(tile_x_pixel - 7);
update_bg_tile_pixel(palette, x, y, tile_x_pixel, tile_y_pixel, tile_id);
}
}
void Display::update_window_scanline(Byte current_scanline)
{
// Ïîëó÷àåì òåêóùóþ êàðòó òàéëîâ îêíà
bool window_code_area = memory->LCDC.is_bit_set(BIT_6);
Address tile_map_location = (window_code_area) ? 0x9C00 : 0x9800;
int window_x = (int)memory->WX.get();
int window_y = (int)memory->WY.get();
Byte palette = memory->BGP.get();
// Äëÿ êàæäîãî ïèêñåëÿ â ñòðîêå 160x1:
// 1. Âû÷èñëÿåì, ãäå íàõîäèòñÿ ïèêñåëü â îáùåé êàðòå òàéëîâ 256x256, ñ ó÷åòîì ScrollX è Y
// 2. Ïîëó÷àåì èäåíòèôèêàòîð òàéëà, íà êîòîðîì íàõîäèòñÿ ýòîò ïèêñåëü
// 3. Ïîëó÷àåì öâåò ïèêñåëÿ íà îñíîâå êîîðäèíàò îòíîñèòåëüíî ñåòêè òàéëîâ 8x8
// 4. Îòîáðàæàåì ïèêñåëü â 160x144 âèäå äèñïëåÿ
int y = (int)current_scanline;
// Èòåðèðóåì ñëåâà íàïðàâî ïî ýêðàíó îòîáðàæåíèÿ (x = 0 -> 160)
for (int x = 0; x < 160; x++)
{
// ÎÊÍÎ ÎÒÍÎÑÈÒÑß Ê ÝÊÐÀÍÓ
// Ñäâèãàåì X è Y ïèêñåëè íà îñíîâå çíà÷åíèÿ ðåãèñòðà îêíà
int display_x = x + window_x - 7;
int display_y = y;
// 1. Ïîëó÷àåì èäåíòèôèêàòîð òàéëà, íà êîòîðîì íàõîäèòñÿ ýòîò ïèêñåëü
int tile_col = floor(x / 8);
int tile_row = floor((y - window_y) / 8);
int tile_map_id = (tile_row * 32) + tile_col;
Address loc = tile_map_location + tile_map_id;
Byte tile_id = memory->read(loc);
// 2. Ïîëó÷àåì öâåò ïèêñåëÿ íà îñíîâå êîîðäèíàò îòíîñèòåëüíî ñåòêè òàéëîâ 8x8
// 3. Îòîáðàæàåì ïèêñåëü â 160x144 âèäå äèñïëåÿ
int tile_x_pixel = x % 8;
int tile_y_pixel = y % 8;
// Èíâåðòèðóåì ïèêñåëè X, ïîòîìó ÷òî îíè õðàíÿòñÿ çàäîì íàïåðåä
tile_x_pixel = abs(tile_x_pixel - 7);
if (current_scanline == 128)
bool breakpoint = true;
if (current_scanline < window_y)
{
window_array.setPixel(x, y, sf::Color::Transparent);
}
else
{
//window_array.setPixel(x, y, sf::Color::Blue);
update_window_tile_pixel(palette, display_x, display_y, tile_x_pixel, tile_y_pixel, tile_id);
}
}
}
void Display::update_bg_tile_pixel(Byte palette, int display_x, int display_y, int tile_x, int tile_y, Byte tile_id)
{
bool bg_char_selection = memory->LCDC.is_bit_set(BIT_4);
if (debug_enabled)
{
bg_char_selection = force_bg_map;
}
// Îïðåäåëÿåì, ãäå õðàíÿòñÿ òåêóùèå äàííûå õàðàêòåðà ôîíà
// åñëè âûáðàíà îáëàñòü=0, òî îáëàñòü ôîíà 0x8800-0x97FF è èäåíòèôèêàòîð òàéëà îïðåäåëÿåòñÿ êàê SIGNED -128 äî 127
// 0x9000 ïðåäñòàâëÿåò àäðåñ íóëåâîãî èäåíòèôèêàòîðà â ýòîì äèàïàçîíå
Address bg_data_location = (bg_char_selection) ? 0x8000 : 0x9000;
Address offset;
// 0x8000 - 0x8FFF áåççíàêîâûé
if (bg_char_selection)
{
offset = (tile_id * 16) + bg_data_location;
}
// 0x8800 - 0x97FF çíàêîâûé
else
{
Byte_Signed direction = (Byte_Signed)tile_id;
Byte_2 temp_offset = (bg_data_location)+(direction * 16);
offset = (Address)temp_offset;
}
Byte
high = memory->read(offset + (tile_y * 2) + 1),
low = memory->read(offset + (tile_y * 2));
sf::Color color = get_pixel_color(palette, low, high, tile_x, false);
bg_array.setPixel(display_x, display_y, color);
}
void Display::update_window_tile_pixel(Byte palette, int display_x, int display_y, int tile_x, int tile_y, Byte tile_id)
{
if (display_x >= 160 || display_x < 0)
return;
if (display_y >= 144 || display_y < 0)
return;
bool bg_char_selection = memory->LCDC.is_bit_set(BIT_4);
if (debug_enabled)
{
bg_char_selection = force_bg_map;
}
// Îïðåäåëÿåì, ãäå õðàíÿòñÿ òåêóùèå äàííûå õàðàêòåðà ôîíà
// åñëè âûáðàíà îáëàñòü=0, òî îáëàñòü ôîíà 0x8800-0x97FF è èäåíòèôèêàòîð òàéëà îïðåäåëÿåòñÿ êàê SIGNED -128 äî 127
// 0x9000 ïðåäñòàâëÿåò àäðåñ íóëåâîãî èäåíòèôèêàòîðà â ýòîì äèàïàçîíå
Address bg_data_location = (bg_char_selection) ? 0x8000 : 0x9000;
Address offset;
// 0x8000 - 0x8FFF áåççíàêîâûé
if (bg_char_selection)
{
offset = (tile_id * 16) + bg_data_location;
}
// 0x8800 - 0x97FF çíàêîâûé
else
{
Byte_Signed direction = (Byte_Signed)tile_id;
Byte_2 temp_offset = (bg_data_location)+(direction * 16);
offset = (Address)temp_offset;
}
Byte
high = memory->read(offset + (tile_y * 2) + 1),
low = memory->read(offset + (tile_y * 2));
sf::Color color = get_pixel_color(palette, low, high, tile_x, false);
window_array.setPixel(display_x, display_y, color);
}
void Display::render_sprites()
{
Address sprite_data_location = 0xFE00;
Byte palette_0 = memory->OBP0.get();
Byte palette_1 = memory->OBP1.get();
bool use_8x16_sprites = memory->LCDC.is_bit_set(BIT_2);
// 160 áàéò äàííûõ ñïðàéòîâ / 4 áàéòà íà ñïðàéò = ìàêñèìóì 40 ñïðàéòîâ äëÿ îòîáðàæåíèÿ
// Íà÷èíàåì ñ 39 -> ÷òîáû ñîõðàíèòü ïðàâèëüíûé ïðèîðèòåò
for (int sprite_id = 39; sprite_id >= 0; sprite_id--)
{
Address offset = sprite_data_location + (sprite_id * 4);
int y_pos = ((int)memory->read(offset)) - 16;
int x_pos = ((int)memory->read(offset + 1)) - 8;
Byte tile_id = memory->read(offset + 2);
Byte flags = memory->read(offset + 3);
bool use_palette_1 = is_bit_set(flags, BIT_4);
Byte sprite_palette = (use_palette_1) ? palette_1 : palette_0;
// Åñëè âêëþ÷åí 8x16 ðåæèì, òî øàáëîí òàéëà äëÿ âåðõíåãî - VAL & 0xFE
// Íèæíèé 8x8 òàéë - VAL | 0x1
if (use_8x16_sprites)
{
tile_id = tile_id & 0xFE;
Byte tile_id_bottom = tile_id | 0x01;
render_sprite_tile(sprite_palette, x_pos, y_pos, tile_id, flags);
render_sprite_tile(sprite_palette, x_pos, y_pos + 8, tile_id_bottom, flags);
}
else
{
render_sprite_tile(sprite_palette, x_pos, y_pos, tile_id, flags);
}
}
}
void Display::render_sprite_tile(Byte palette, int start_x, int start_y, Byte tile_id, Byte flags)
{
Address sprite_data_location = 0x8000;
bool priority = is_bit_set(flags, BIT_7);
bool mirror_y = is_bit_set(flags, BIT_6);
bool mirror_x = is_bit_set(flags, BIT_5);
// Åñëè óñòàíîâëåí â íîëü, òî ñïðàéò âñåãäà îòîáðàæàåòñÿ ïîâåðõ ôîíà
// Åñëè óñòàíîâëåí â 1, ñïðàéò ñêðûò çà ôîíîì è îêíîì
// åñëè öâåò ôîíà èëè îêíà íå áåëûé, îí âñå ðàâíî îòîáðàæàåòñÿ ïîâåðõ
bool hide_sprite = is_bit_set(flags, BIT_7);
for (int y = 0; y < 8; y++)
{
int offset = (tile_id * 16) + sprite_data_location;
Byte
high = memory->read(offset + (y * 2) + 1),
low = memory->read(offset + (y * 2));
for (int x = 0; x < 8; x++)
{
int pixel_x = (mirror_x) ? (start_x + x) : (start_x + 7 - x);
int pixel_y = (mirror_y) ? (start_y + 7 - y) : (start_y + y);
// ïðåäîòâðàùàåì îòðèñîâêó ïèêñåëåé çà ïðåäåëàìè ýêðàíà
sf::Vector2u bounds = sprites_array.getSize();
if (pixel_x < 0 || pixel_x >= bounds.x)
continue;
if (pixel_y < 0 || pixel_y >= bounds.y)
continue;
sf::Color color = get_pixel_color(palette, low, high, x, true);
// Åñëè öâåò â ôîíå/îêíå îòëè÷àåòñÿ îò áåëîãî, ñêðûòü ïèêñåëü ñïðàéòà
sf::Color bg_color = bg_array.getPixel(pixel_x, pixel_y);
if (priority)
{
if (bg_color != shades_of_gray[0x0])
{
continue;
//color = sf::Color::Transparent;
}
}
sprites_array.setPixel(pixel_x, pixel_y, color);
}
}
}
// Âîçâðàùàåò öâåò ïèêñåëÿ â X áèòå íà îñíîâå 2 ñîîòâåòñòâóþùèõ áàéòîâ ñòðîêè
sf::Color Display::get_pixel_color(Byte palette, Byte top, Byte bottom, int bit, bool is_sprite)
{
// Îïðåäåëÿåì, êàêèå öâåòà ïðèìåíÿòü ê êàæäîìó êîäó öâåòà íà îñíîâå äàííûõ ïàëèòðû
Byte color_3_shade = (palette >> 6); // èçâëåêàåì áèòû 7 è 6
Byte color_2_shade = (palette >> 4) & 0x03; // èçâëåêàåì áèòû 5 è 4
Byte color_1_shade = (palette >> 2) & 0x03; // èçâëåêàåì áèòû 3 è 2
Byte color_0_shade = (palette & 0x03); // èçâëåêàåì áèòû 1 è 0
// Ïîëó÷àåì êîä öâåòà èç äâóõ îïðåäåëÿþùèõ áàéòîâ
Byte first = (Byte)is_bit_set(top, bit);
Byte second = (Byte)is_bit_set(bottom, bit);
Byte color_code = (second << 1) | first;
sf::Color result;
switch (color_code)
{
case 0x0: return (is_sprite) ? sf::Color::Transparent : shades_of_gray[color_0_shade];
case 0x1: return shades_of_gray[color_1_shade];
case 0x2: return shades_of_gray[color_2_shade];
case 0x3: return shades_of_gray[color_3_shade];
default: return sf::Color(255, 0, 255); // öâåò îøèáêè
}
}
bool Display::is_lcd_enabled()
{
return memory->LCDC.is_bit_set(BIT_7);
}