-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen.h
479 lines (428 loc) · 20.5 KB
/
screen.h
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
// tile map
#define BG_TILE_SIZE_H 8
#define BG_TILE_SIZE_W 8
#define tbt BG_TILE_SIZE_H*BG_TILE_SIZE_W
#define htbt BG_TILE_SIZE_H*(BG_TILE_SIZE_W/2)
void inline bgTileLine2();
void inline bgTileLine();
void write_command_16(uint16_t data);
void write_data_16(uint16_t data);
int ease(int t, int b, int c, int d){
return ((t*c)/d)+b;
}
/*
______ __ _______
| __ \.-----.-----.--| |.-----.----. | __|.----.----.-----.-----.-----.
| <| -__| | _ || -__| _| |__ || __| _| -__| -__| |
|___|__||_____|__|__|_____||_____|__| |_______||____|__| |_____|_____|__|__|
*/
void loadPalette(const uint16_t* p, int numColours) {
for (int i=0;i<numColours;i++)
Pokitto::Display::palette[i] = p[i];
Pokitto::Display::paletteptr = Pokitto::Display::palette;
}
void clearPalette(int numColours) {
for (int i=0;i<numColours;i++)
Pokitto::Display::palette[i] = 0;
Pokitto::Display::paletteptr = Pokitto::Display::palette;
}
uint16_t alphaBlendRGB565(uint32_t fg, uint32_t bg, uint8_t alpha) {
fg = (fg | fg << 16) & 0x07e0f81f;
bg = (bg | bg << 16) & 0x07e0f81f;
bg += (fg - bg) * alpha >> 5;
bg &= 0x07e0f81f;
return (uint16_t)(bg | bg >> 16);
}
void GUILine(std::uint8_t* line, std::uint32_t y, bool skip){
if(skip)return;
if(y<bg.screenTop || y>=bg.screenBottom){
return;
}
uint32_t x = 0;
uint32_t tileIndex = 2+(y/8) * 27;
uint32_t lineOffset;
uint32_t alpha;
uint32_t temp;
auto &tileRef = guiFont[0];
#define invisible 1
auto scanLine = &Pokitto::Display::palette[16]; // start 32 pixels in because of underflow
for(int d=0; d<27; d++){
lineOffset = 2 + ((y%8)*4) + guiBG[tileIndex++]*34;
for(int c=0; c<4; c++){
temp = tileRef[lineOffset]>>4;
if(temp==0){
// scanLine[x] = 0;
}else{
if(temp==invisible){alpha = 16;}else{alpha=32;}
scanLine[x] = alphaBlendRGB565(guiFont_pal[temp], scanLine[x], alpha);
}
x++;
temp = tileRef[lineOffset++]&15;
if(temp==0){
// scanLine[x] = 0;
}else{
if(temp==invisible){alpha = 16;}else{alpha=32;}
scanLine[x] = alphaBlendRGB565(guiFont_pal[temp], scanLine[x], alpha);
}
x++;
}
}
}
void spriteFill(std::uint8_t* line, std::uint32_t y, bool skip){
if(renderMenuLayer==true){
GUILine(line, y, skip);
}
if(spriteLine[y]==0) return;
#define width 16
auto scanLine = &Pokitto::Display::palette[16]; // start 32 pixels in because of underflow
#define width 16
#define halfWidth 8
int y2 = y;
spriteLine[y]=0;
if(spriteCount>=0){
for(int32_t spriteIndex = 1; spriteIndex<=spriteCount; spriteIndex++){
auto &sprite = sprites[spriteIndex];
if(sprite.bit==1){
if(y >= sprite.y && y < sprite.y + sprite.imageData[1]){
if(sprite.x>-width && sprite.x<PROJ_LCDWIDTH){
if(sprite.hFlip){
uint32_t so = 2+(2 * (y2-sprite.y));
auto si = &sprite.imageData[so+3];
auto sl = &scanLine[sprite.x];
auto st = &scanLine[sprite.x + sprite.imageData[0]];
auto palette = sprite.paletteData;
auto pixel = *si;
#define midLoop()\
if(((pixel=*si)>>7)&1) *sl = palette[(pixel>>7)&1];\
sl++;\
if((pixel>>6)&1) *sl = palette[(pixel>>6)&1];\
sl++;\
if((pixel>>5)&1) *sl = palette[(pixel>>5)&1];\
sl++;\
if((pixel>>4)&1) *sl = palette[(pixel>>4)&1];\
sl++;\
if((pixel>>3)&1) *sl = palette[(pixel>>3)&1];\
sl++;\
if((pixel>>2)&1) *sl = palette[(pixel>>2)&1];\
sl++;\
if((pixel>>1)&1) *sl = palette[(pixel>>1)&1];\
sl++;\
if(pixel&1) *sl = palette[pixel&1];\
si--; sl++;
while(sl < st)
midLoop();
}else{
uint32_t so = 2+(2 * (y2-sprite.y));
auto si = &sprite.imageData[so];
auto sl = &scanLine[sprite.x];
int sw = sprite.imageData[0];
auto palette = sprite.paletteData;
auto pixel = *si;
#define midLoop()\
if(((pixel=*si)>>7)&1) *sl = palette[(pixel>>7)&1];\
sl++;\
if((pixel>>6)&1) *sl = palette[(pixel>>6)&1];\
sl++;\
if((pixel>>5)&1) *sl = palette[(pixel>>5)&1];\
sl++;\
if((pixel>>4)&1) *sl = palette[(pixel>>4)&1];\
sl++;\
if((pixel>>3)&1) *sl = palette[(pixel>>3)&1];\
sl++;\
if((pixel>>2)&1) *sl = palette[(pixel>>2)&1];\
sl++;\
if((pixel>>1)&1) *sl = palette[(pixel>>1)&1];\
sl++;\
if(pixel&1) *sl = palette[pixel&1];\
si++; sl++;
midLoop();
}
} // if X
} // if Y
} // 1bpp
if(sprite.bit==2){
if((int)y >= sprite.y && (int)y < sprite.y + sprite.imageData[1]){
if(sprite.x>-width && sprite.x<PROJ_LCDWIDTH){
if(sprite.hFlip){
uint32_t so = 2+(2 * (y2-sprite.y));
auto si = &sprite.imageData[so+3];
auto sl = &scanLine[sprite.x];
auto palette = sprite.paletteData;
auto pixel = *si;
#define midLoop()\
if(((pixel=*si)>>6)&3) *sl = palette[(pixel>>6)&3];\
sl++;\
if((pixel>>4)&3) *sl = palette[(pixel>>4)&3];\
sl++;\
if((pixel>>2)&3) *sl = palette[(pixel>>2)&3];\
sl++;\
if((pixel)&3) *sl = palette[(pixel)&3];\
*si--; sl++;
midLoop(); midLoop();
}else{
uint32_t so = 2+(2 * (y2-sprite.y));
auto si = &sprite.imageData[so];
auto sl = &scanLine[sprite.x];
auto palette = sprite.paletteData;
auto pixel = *si;
#define midLoop()\
if(((pixel=*si)>>6)&3) *sl = palette[(pixel>>6)&3];\
sl++;\
if((pixel>>4)&3) *sl = palette[(pixel>>4)&3];\
sl++;\
if((pixel>>2)&3) *sl = palette[(pixel>>2)&3];\
sl++;\
if((pixel)&3) *sl = palette[(pixel)&3];\
*si++; sl++;
midLoop(); midLoop();
}
} // if X
} // if Y
} // 2bpp
if(sprite.bit==4){
if((int)y >= sprite.y && (int)y < sprite.y + sprite.imageData[1]){
if(sprite.x>-width && sprite.x<PROJ_LCDWIDTH){
if(sprite.hFlip){
uint32_t so = 2+(halfWidth * (y2-sprite.y));
auto si = &sprite.imageData[so+7];
auto sl = &scanLine[sprite.x];
auto palette = sprite.paletteData;
#define midLoop()\
if(auto pixel = *si&15) *sl = palette[pixel];\
sl++;\
if(auto pixel = *si>>4) *sl = palette[pixel];\
si--; sl++;
midLoop(); midLoop(); midLoop(); midLoop();
midLoop(); midLoop(); midLoop(); midLoop();
}else{
uint32_t so = 2+(halfWidth * (y2-sprite.y));
auto si = &sprite.imageData[so];
auto sl = &scanLine[sprite.x];
auto palette = sprite.paletteData;
#define midLoop()\
if(auto pixel = *si>>4) *sl = palette[pixel];\
sl++;\
if(auto pixel = *si&15) *sl = palette[pixel];\
si++; sl++;
midLoop(); midLoop(); midLoop(); midLoop();
midLoop(); midLoop(); midLoop(); midLoop();
}
} // if X
} // if Y
} // 4bpp
if(sprite.bit==8){
if((int)y >= sprite.y && (int)y < sprite.y + sprite.imageData[1]){
if(sprite.x>-width && sprite.x<PROJ_LCDWIDTH){
if(sprite.hFlip){
uint32_t so = 2+(width * (y2-sprite.y));
auto si = &sprite.imageData[so+15];
auto sl = &scanLine[sprite.x];
auto palette = sprite.paletteData;
#define midLoop()\
if(auto pixel = *si) *sl = palette[pixel];\
si--; sl++;
midLoop(); midLoop(); midLoop(); midLoop();
midLoop(); midLoop(); midLoop(); midLoop();
midLoop(); midLoop(); midLoop(); midLoop();
midLoop(); midLoop(); midLoop(); midLoop();
}else{
uint32_t so = 2+(width * (y2-sprite.y));
auto si = &sprite.imageData[so];
auto sl = &scanLine[sprite.x];
auto palette = sprite.paletteData;
#define midLoop()\
if(auto pixel = *si) *sl = palette[pixel];\
si++; sl++;
midLoop(); midLoop(); midLoop(); midLoop();
midLoop(); midLoop(); midLoop(); midLoop();
midLoop(); midLoop(); midLoop(); midLoop();
midLoop(); midLoop(); midLoop(); midLoop();
}
} // if X
} // if Y
} // 8bpp
} // for spriteCount
} // sprite count >1
// }
return;
}
/*
inline void wiggleFill(std::uint8_t* line, std::uint32_t y, bool skip){
if(y<bg.screenTop+1 || y>=bg.screenBottom){
return;
}
int t=wiggle[y+offsetAngle];
if(t>=0){
memcpy(&line[0], &line[t], 220-t);
memset(&line[220-t], 255, t);
}else{
for(int p=220; p>-t; p--){
line[p] = line[t+p];
}
memset(&line[0], 255, -t);
}
return;
}
*/
void myBGFiller(std::uint8_t* line, std::uint32_t y, bool skip){
if(y==bg.screenTop){
for(uint32_t x=0; x<220; ++x){
line[x]=x+16;
}
}
if(y==0 || y==bg.screenBottom){
gamePalette.rgb[0]=0;
for(uint32_t x=0; x<220; ++x){
line[x]=0;
}
}
#define TILEWIDTH 8
#define TILEHEIGHT 8
if(y<bg.screenTop || y>=bg.screenBottom+1){
return;
}
gamePalette.rgb[0] = 0xF81F; //gamePalette.hpal[hline[y]];
int32_t x = -(bg.windowX%TILEWIDTH)+16;
uint32_t tileIndex = (bg.windowX/TILEWIDTH) + ((y+bg.windowY)/TILEWIDTH) * bg.miniMap[0];
uint32_t jStart = ((y+bg.windowY)%TILEWIDTH) *TILEWIDTH; // current line in current tile
uint32_t lineOffset;
uint32_t thisTile;
auto lineP = &Pokitto::Display::palette[0];
auto mainPal = &gamePalette.rgb[0];
#define bgTileLine()\
thisTile = bg.miniMap[2+tileIndex++]&0x83FF;\
lineOffset = ((thisTile&1023)*tbt) + jStart;\
lineP = &Pokitto::Display::palette[x];\
x+=8;\
if(thisTile&32768){\
auto tilesP = &tiles[7 + lineOffset];\
*lineP++ = mainPal[*tilesP--];\
*lineP++ = mainPal[*tilesP--];\
*lineP++ = mainPal[*tilesP--];\
*lineP++ = mainPal[*tilesP--];\
*lineP++ = mainPal[*tilesP--];\
*lineP++ = mainPal[*tilesP--];\
*lineP++ = mainPal[*tilesP--];\
*lineP++ = mainPal[*tilesP--];\
}else{\
auto tilesP = &tiles[lineOffset];\
*lineP++ = mainPal[*tilesP++];\
*lineP++ = mainPal[*tilesP++];\
*lineP++ = mainPal[*tilesP++];\
*lineP++ = mainPal[*tilesP++];\
*lineP++ = mainPal[*tilesP++];\
*lineP++ = mainPal[*tilesP++];\
*lineP++ = mainPal[*tilesP++];\
*lineP++ = mainPal[*tilesP++];\
}
// unrolling this loop got an extra 10fps
bgTileLine(); bgTileLine(); bgTileLine(); bgTileLine(); bgTileLine();
bgTileLine(); bgTileLine(); bgTileLine(); bgTileLine(); bgTileLine();
bgTileLine(); bgTileLine(); bgTileLine(); bgTileLine(); bgTileLine();
bgTileLine(); bgTileLine(); bgTileLine(); bgTileLine(); bgTileLine();
bgTileLine(); bgTileLine(); bgTileLine(); bgTileLine(); bgTileLine();
bgTileLine(); bgTileLine(); bgTileLine(); bgTileLine();
}
// background scenery
void myBGFiller2(std::uint8_t* line, std::uint32_t y, bool skip){
if(y<bg.screenTop || y>bg.screenBottom){
return;
}
int32_t stX = -(bg.midwindowX&7)+16;
int x = stX;
uint32_t tileIndex = (bg.midwindowX>>3) + ((y+bg.midwindowY)>>3) * bg.midminiMap[0];
uint32_t jStart = ((y+bg.midwindowY)&7) <<3; // current line in current tile
uint32_t lineOffset;
uint32_t thisTile;
auto lineP = &Pokitto::Display::palette[0];
gamePalette.rgb[0] = gamePalette.hpal[hline[y]];
uint32_t py = y/8;
uint32_t multiplied = (32*py) + 450; // the 450 is to select a different part of the sprite
auto offsetedPal = pal + doorPalOffset;
auto spriteSource = sprite_door + multiplied;
auto tilesP = &tiles[lineOffset];
#define bgTileLine2()\
thisTile = bg.midminiMap[2+tileIndex++]&0x83FF;\
lineOffset = ((thisTile&1023)*tbt) + jStart;\
lineP = &Pokitto::Display::palette[x];\
x+=8;\
tilesP = &tiles[lineOffset];\
if(*lineP==0xF81F) *lineP = offsetedPal[spriteSource[plasmaScreen[x]]];\
*lineP++;\
if(*lineP==0xF81F) *lineP = offsetedPal[spriteSource[plasmaScreen[x]]];\
*lineP++;\
if(*lineP==0xF81F) *lineP = offsetedPal[spriteSource[plasmaScreen[x]]];\
*lineP++;\
if(*lineP==0xF81F) *lineP = offsetedPal[spriteSource[plasmaScreen[x]]];\
*lineP++;\
if(*lineP==0xF81F) *lineP = offsetedPal[spriteSource[plasmaScreen[x]]];\
*lineP++;\
if(*lineP==0xF81F) *lineP = offsetedPal[spriteSource[plasmaScreen[x]]];\
*lineP++;\
if(*lineP==0xF81F) *lineP = offsetedPal[spriteSource[plasmaScreen[x]]];\
*lineP++;\
if(*lineP==0xF81F) *lineP = offsetedPal[spriteSource[plasmaScreen[x]]];\
*lineP++;
#define bgTileLine3()\
thisTile = bg.midminiMap[2+tileIndex++]&0x83FF;\
lineOffset = ((thisTile&1023)*tbt) + jStart;\
lineP = &Pokitto::Display::palette[x];\
x+=8;\
if(thisTile&32768){\
auto tilesP = &tiles[7 + lineOffset];\
if(*lineP==0xF81F) *lineP = gamePalette.rgb[*tilesP--];\
*lineP++;\
if(*lineP==0xF81F) *lineP = gamePalette.rgb[*tilesP--];\
*lineP++;\
if(*lineP==0xF81F) *lineP = gamePalette.rgb[*tilesP--];\
*lineP++;\
if(*lineP==0xF81F) *lineP = gamePalette.rgb[*tilesP--];\
*lineP++;\
if(*lineP==0xF81F) *lineP = gamePalette.rgb[*tilesP--];\
*lineP++;\
if(*lineP==0xF81F) *lineP = gamePalette.rgb[*tilesP--];\
*lineP++;\
if(*lineP==0xF81F) *lineP = gamePalette.rgb[*tilesP--];\
*lineP++;\
if(*lineP==0xF81F) *lineP = gamePalette.rgb[*tilesP--];\
*lineP++;\
}else{\
auto tilesP = &tiles[lineOffset];\
if(*lineP==0xF81F) *lineP = gamePalette.rgb[*tilesP++];\
*lineP++;\
if(*lineP==0xF81F) *lineP = gamePalette.rgb[*tilesP++];\
*lineP++;\
if(*lineP==0xF81F) *lineP = gamePalette.rgb[*tilesP++];\
*lineP++;\
if(*lineP==0xF81F) *lineP = gamePalette.rgb[*tilesP++];\
*lineP++;\
if(*lineP==0xF81F) *lineP = gamePalette.rgb[*tilesP++];\
*lineP++;\
if(*lineP==0xF81F) *lineP = gamePalette.rgb[*tilesP++];\
*lineP++;\
if(*lineP==0xF81F) *lineP = gamePalette.rgb[*tilesP++];\
*lineP++;\
if(*lineP==0xF81F) *lineP = gamePalette.rgb[*tilesP++];\
*lineP++;\
}
if(bg.totalGemsCollected == bg.totalGemsToCollect){
// unrolling this loop got an extra 10fps
bgTileLine2(); bgTileLine2(); bgTileLine2(); bgTileLine2();
bgTileLine2(); bgTileLine2(); bgTileLine2(); bgTileLine2();
bgTileLine2(); bgTileLine2(); bgTileLine2(); bgTileLine2();
bgTileLine2(); bgTileLine2(); bgTileLine2(); bgTileLine2();
bgTileLine2(); bgTileLine2(); bgTileLine2(); bgTileLine2();
bgTileLine2(); bgTileLine2(); bgTileLine2(); bgTileLine2();
bgTileLine2(); bgTileLine2(); bgTileLine2(); bgTileLine2();
bgTileLine2();
}else{
bgTileLine3(); bgTileLine3(); bgTileLine3(); bgTileLine3();
bgTileLine3(); bgTileLine3(); bgTileLine3(); bgTileLine3();
bgTileLine3(); bgTileLine3(); bgTileLine3(); bgTileLine3();
bgTileLine3(); bgTileLine3(); bgTileLine3(); bgTileLine3();
bgTileLine3(); bgTileLine3(); bgTileLine3(); bgTileLine3();
bgTileLine3(); bgTileLine3(); bgTileLine3(); bgTileLine3();
bgTileLine3(); bgTileLine3(); bgTileLine3(); bgTileLine3();
bgTileLine3();
}
}