-
Notifications
You must be signed in to change notification settings - Fork 0
/
MDRender copy.c
599 lines (504 loc) · 17.2 KB
/
MDRender copy.c
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
#include "HydrogenEngine.h"
#include "HE_Utils.h"
#include "SDL_image.h"
#include <stdio.h>
static SDL_Renderer* hwrender;
static SDL_Surface* framebuffer;
static SDL_Texture* fb_texture;
static HE_HInterrupt hblank;
void HE_RenderInit(void)
{
hwrender = SDL_CreateRenderer(HE_GetWindow(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
framebuffer = HE_CreateSurface(HE_FB_WIDTH, HE_FB_HEIGHT, HE_SURFACEFLAG_NONE);
fb_texture = SDL_CreateTexture(hwrender,
SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING,
HE_FB_WIDTH, HE_FB_HEIGHT);
HE_ResetHBlank();
}
void HE_RenderQuit(void)
{
SDL_FreeSurface(framebuffer);
SDL_DestroyTexture(fb_texture);
SDL_DestroyRenderer(hwrender);
}
static void setup_surface(SDL_Surface* surface, int flags)
{
SDL_SetSurfacePalette(surface, HE_GetDefaultColorPalette());
if (flags & HE_SURFACEFLAG_TRANSPARENT)
SDL_SetColorKey(surface, SDL_TRUE, 0);
HE_SetSurfaceFlags(surface, flags);
}
SDL_Surface* HE_CreateSurface(int w, int h, int flags)
{
SDL_Surface* surface = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 8, 0, 0, 0, 0);
setup_surface(surface, flags);
return surface;
}
Uint8* HE_GetSurfacePixels(SDL_Surface* surface)
{
return (Uint8*)surface->pixels;
}
void HE_FillSurface(SDL_Surface* surface, int palid, int colorid)
{
Uint8 color = HE_MapColorInternal(palid, colorid);
memset(surface->pixels, color, surface->pitch * surface->h);
}
void HE_ClearSurface(SDL_Surface* surface)
{
HE_FillSurface(surface, 0, 0);
}
static SDL_bool colors_equal(const SDL_Color* a, const SDL_Color* b)
{
return a->r == b->r
&& a->g == b->g
&& a->b == b->b;
}
static int find_color(const SDL_Color* color, int palid_start, int palid_end)
{
if (color->a == 0)
return 0;
SDL_Color* palette = HE_GetColorPalette()->colors;
int color_start = HE_MapColorInternal(palid_start, 0) + 1;
int color_end = HE_MapColorInternal(palid_end, HE_PALETTE_COLORS - 1);
for (int i = color_start; i <= color_end; i++)
{
// Skip the transparent colors
if ((i % HE_PALETTE_COLORS) == 0)
continue;
if (colors_equal(palette + i, color))
return i;
}
return 0;
}
SDL_Surface* HE_ConvertSurface(SDL_Surface* surface, int palid_start, int palid_end)
{
if (surface->format->BytesPerPixel != 4)
{
// TODO
printf("Can't convert this format\n");
return NULL;
}
SDL_Surface* mdsurface = HE_CreateSurface(surface->w, surface->h, HE_SURFACEFLAG_TRANSPARENT);
Uint8* pixels = surface->pixels;
Uint8* mdpixels = HE_GetSurfacePixels(mdsurface);
SDL_PixelFormat* pixel_format = surface->format;
int BytesPerPixel = surface->format->BytesPerPixel;
for (int i = 0; i < surface->h; i++)
{
for (int j = 0; j < surface->w; j++)
{
SDL_Color color;
Uint32 pixel = *(Uint32*)(pixels + j * BytesPerPixel);
SDL_GetRGBA(pixel, pixel_format, &color.r, &color.g, &color.b, &color.a);
mdpixels[j] = find_color(&color, palid_start, palid_end);
}
mdpixels += mdsurface->pitch;
pixels += surface->pitch;
}
return mdsurface;
}
SDL_Surface* HE_LoadSurfaceRW(SDL_RWops* rw, SDL_bool free_rwops, int palid_start, int palid_end)
{
SDL_Surface* rgb = IMG_Load_RW(rw, free_rwops);
SDL_Surface* result = HE_ConvertSurface(rgb, palid_start, palid_end);
SDL_FreeSurface(rgb);
return result;
}
SDL_Surface* HE_LoadSurface(const char* filename, int palid_start, int palid_end)
{
return HE_LoadSurfaceRW(SDL_RWFromFile(filename, "rb"), SDL_TRUE, palid_start, palid_end);
}
void HE_SaveSurface(SDL_Surface* surface, const char* filename)
{
const char* filename_test = filename + strlen(filename) - 4;
if (strlen(filename) < 5)
return;
if (!strcmp(filename_test, ".png"))
IMG_SavePNG(surface, filename);
else if (!strcmp(filename_test, ".bmp"))
SDL_SaveBMP(surface, filename);
}
// Rendering
void HE_PreTextureRender(void)
{
SDL_SetRenderDrawColor(hwrender, 0, 0, 0, 255);
SDL_RenderClear(hwrender);
}
void HE_PostTextureRender(void)
{
SDL_RenderPresent(hwrender);
}
SDL_Surface* HE_GetFramebuffer(void)
{
return framebuffer;
}
SDL_Surface* HE_LockFBTexture(void)
{
SDL_Surface* fb_surface;
SDL_LockTextureToSurface(fb_texture, NULL, &fb_surface);
return fb_surface;
}
void HE_UnlockFBTexture(void)
{
SDL_UnlockTexture(fb_texture);
}
void HE_RenderFBToSurface(SDL_Surface* surface)
{
if (surface->format->format != SDL_PIXELFORMAT_ABGR8888)
{
// TODO
printf("HE_RenderFBToSurface: Invalid format\n");
return;
}
Uint32* pixels = surface->pixels;
Uint8* fb_pixels = framebuffer->pixels;
for (int i = 0; i < framebuffer->h; i++)
{
hblank(i);
Uint32* colors = (Uint32*)HE_GetColorPalette()->colors;
for (int j = 0; j < framebuffer->w; j++)
{
Uint8 colorid = fb_pixels[j];
Uint32 color = colors[colorid];
pixels[j] = color;
}
pixels += surface->pitch / 4;
fb_pixels += framebuffer->pitch;
}
}
void HE_RenderFBTextureToScreen()
{
int w, h;
SDL_GetWindowSize(HE_GetWindow(), &w, &h);
float fb_zoom_factor = HE_GetFBZoom();
SDL_Rect rect = {
w / 2 - framebuffer->w * fb_zoom_factor / 2,
h / 2 - framebuffer->h * fb_zoom_factor / 2,
framebuffer->w * fb_zoom_factor,
framebuffer->h * fb_zoom_factor,
};
SDL_RenderCopy(hwrender, fb_texture, NULL, &rect);
}
void HE_RenderSurfaceEx(SDL_Surface* src, SDL_Rect* srcrect, int x, int y, double zoomx, double zoomy, double angle)
{
int flags = HE_GetSurfaceFlags(src);
SDL_Surface* surface = src;
if (srcrect)
surface = HE_ClipSurface(src, srcrect);
SDL_bool need_scalerotate = HE_NeedScaleRotate(zoomx, zoomy, angle);
SDL_Surface* rotozoom = surface;
if (need_scalerotate)
{
rotozoom = HE_ScaleRotateSurface(surface, zoomx, zoomy, angle);
setup_surface(rotozoom, flags);
}
// If a clipped surface was created and if we don't need it
// in case it was scaled/rotated, free it
// (don't free it if it wasn't rotated, we will draw it below)
if (srcrect && need_scalerotate) SDL_FreeSurface(surface);
SDL_Rect rect = { x - rotozoom->w / 2, y - rotozoom->h / 2, rotozoom->w, rotozoom->h };
SDL_BlitSurface(rotozoom, NULL, framebuffer, &rect);
// If the clipped surface was scaled/rotated, flip the modified surface
// (the clipped surface was freed above)
// If the clipped surface wasn't scaled/rotated, free the clipped surface
// that we don't free above (rotozoom == surface)
if (srcrect || need_scalerotate)
SDL_FreeSurface(rotozoom);
}
void HE_RenderSurfaceAngle(SDL_Surface* src, SDL_Rect* srcrect, int x, int y, double angle)
{
HE_RenderSurfaceEx(src, srcrect, x, y, 1.0, 1.0, angle);
}
void HE_RenderSurface(SDL_Surface* src, SDL_Rect* srcrect, int x, int y)
{
HE_RenderSurfaceEx(src, srcrect, x, y, 1.0, 1.0, 0.0);
}
void HE_RenderSurfaceDeform(SDL_Surface* src, int xleft, int ytop, int* hdeform, int deformsize)
{
Uint8* pixels = src->pixels;
Uint8* fbpixels = framebuffer->pixels;
/* Find the intersection of the source surface and the framebuffer */
int xstart = SDL_max(xleft, 0);
int xend = SDL_min((xleft + src->w), framebuffer->w);
int ystart = SDL_max(ytop, 0);
int yend = SDL_min((ytop + src->h), framebuffer->h);
/* If the surface is offset above the framebuffer, skip that many rows. */
if (ytop < 0)
pixels += src->pitch * -ytop;
/* Set the initial framebuffer pointer to the top edge of the intersection. */
fbpixels += framebuffer->pitch * ystart;
/* For each row in the intersection: */
for (int i = ystart; i < yend; i++)
{
/* Calculate the deformation amount for this row */
int y = i - ytop;
int deform = y < deformsize ? hdeform[y] : 0;
/* For each pixel in the intersection, copy it over if it's nonzero. */
for (int j = xstart; j < xend; j++)
{
Uint8 pixel = pixels[HE_UtilsOffset(j - xleft, src->w, deform)];
if (pixel)
fbpixels[j] = pixel;
}
/* Advance to the next row in both source and framebuffer. */
pixels += src->pitch;
fbpixels += framebuffer->pitch;
}
}
// TODO: comment the difference between this function and HE_RenderSurfaceDeform
void HE_RenderSurfaceDeform2(SDL_Surface* src, int xleft, int ytop, int* hdeform, int deformsize)
{
Uint8* pixels = src->pixels;
Uint8* fbpixels = framebuffer->pixels;
// Offset destination pointer to the first row to write to
fbpixels += framebuffer->pitch * ytop;
// Iterate over each row of the source surface
for (int y = 0; y < src->h; y++)
{
// If the current row is outside the framebuffer,
// break out of the loop
if (y + ytop >= framebuffer->h)
break;
// Get the horizontal deformation amount for this row
int deform = y < deformsize ? hdeform[y] : 0;
// Iterate over each column of the current row
for (int x = 0; x < src->w; x++)
{
// Calculate the destination index in the framebuffer
int dest = xleft + x + deform;
Uint8 pixel = pixels[x];
// If the destination index is outside the framebuffer or
// the pixel is transparent (% HE_PALETTE_COLORS == 0),
// skip this pixel
if (dest < 0 || dest >= framebuffer->w || pixel % HE_PALETTE_COLORS == 0)
continue;
fbpixels[dest] = pixel;
}
fbpixels += framebuffer->pitch;
pixels += src->pitch;
}
}
HE_Spritesheet* HE_CreateSpritesheet(int sprite_w, int sprite_h, SDL_Surface* spritesheet_surface, SDL_bool take_ownership)
{
HE_Spritesheet* spritesheet = (HE_Spritesheet*)malloc(sizeof(*spritesheet));
spritesheet->surface = spritesheet_surface;
spritesheet->surface_ownership = take_ownership;
spritesheet->sprite_w = sprite_w;
spritesheet->sprite_h = sprite_h;
spritesheet->sprites_h = spritesheet_surface->w / sprite_w;
spritesheet->sprites_v = spritesheet_surface->h / sprite_h;
return spritesheet;
}
void HE_FreeSpritesheet(HE_Spritesheet* spritesheet)
{
if (spritesheet->surface_ownership)
SDL_FreeSurface(spritesheet->surface);
free(spritesheet);
}
HE_Spritesheet* HE_LoadSpritesheetHE_RW(int sprite_w, int sprite_h,
SDL_RWops* tiles, SDL_RWops* mappings, SDL_RWops* dplc, int palid)
{
int sprite_count = HE_GetSpriteMappingsCount(mappings);
int sprites_h = 10;
int sprites_v = sprite_count / sprites_h + 1;
SDL_Surface* spritesheet_surface = HE_CreateSurface(
sprite_w * sprites_h, sprite_h * sprites_v,
HE_SURFACEFLAG_TRANSPARENT);
int sprite = 0;
for (int i = 0; i < sprites_v; i++)
{
for (int j = 0; j < sprites_h; j++)
{
if (sprite >= sprite_count)
break;
HE_DrawSpriteMappings(tiles, mappings, dplc, sprite, palid,
sprite_w * j + (sprite_w / 2), sprite_h * i + (sprite_h / 2), spritesheet_surface);
sprite++;
}
}
HE_Spritesheet* spritesheet = HE_CreateSpritesheet(sprite_w, sprite_h, spritesheet_surface, SDL_TRUE);
return spritesheet;
}
HE_Spritesheet* HE_LoadSpritesheetMD(int sprite_w, int sprite_h,
const char* tiles_file, const char* mappings_file, const char* dplc_file, int palid)
{
SDL_RWops* tiles = SDL_RWFromFile(tiles_file, "rb");
SDL_RWops* mappings = SDL_RWFromFile(mappings_file, "rb");
SDL_RWops* dplc = SDL_RWFromFile(dplc_file, "rb");
HE_Spritesheet* spritesheet = HE_LoadSpritesheetHE_RW(sprite_w, sprite_h,
tiles, mappings, dplc, palid);
SDL_RWclose(tiles);
SDL_RWclose(mappings);
SDL_RWclose(dplc);
return spritesheet;
}
HE_Spritesheet* HE_LoadSpritesheet(int sprite_w, int sprite_h, const char* filename, int palid_start, int palid_end)
{
return HE_CreateSpritesheet(sprite_w, sprite_h, HE_LoadSurface(filename, palid_start, palid_end), SDL_TRUE);
}
void HE_SaveSpritesheet(HE_Spritesheet* spritesheet, const char* filename)
{
HE_SaveSurface(spritesheet->surface, filename);
}
SDL_Rect HE_GetSpriteRect(HE_Spritesheet* spritesheet, int sprite)
{
int sprite_h = sprite % spritesheet->sprites_h;
int sprite_v = sprite / spritesheet->sprites_h;
SDL_Rect out = {
sprite_h * spritesheet->sprite_w,
sprite_v * spritesheet->sprite_h,
spritesheet->sprite_w,
spritesheet->sprite_h,
};
return out;
}
void HE_RenderSpritesheetEx(HE_Spritesheet* spritesheet, int sprite, int x, int y, double zoomx, double zoomy, double angle)
{
SDL_Rect rect = HE_GetSpriteRect(spritesheet, sprite);
HE_RenderSurfaceEx(spritesheet->surface, &rect, x, y, zoomx, zoomy, angle);
}
void HE_RenderSpritesheetAngle(HE_Spritesheet* spritesheet, int sprite, int x, int y, double angle)
{
HE_RenderSpritesheetEx(spritesheet, sprite, x, y, 1.0, 1.0, angle);
}
void HE_RenderSpritesheet(HE_Spritesheet* spritesheet, int sprite, int x, int y)
{
HE_RenderSpritesheetEx(spritesheet, sprite, x, y, 1.0, 1.0, 0.0);
}
static void default_hblank(int y) {}
void HE_SetHBlank(HE_HInterrupt h_int)
{
hblank = h_int;
}
void HE_ResetHBlank(void)
{
HE_SetHBlank(default_hblank);
}
// Mega Drive tile rendering
static void draw_tile_hline(SDL_RWops* tiles, Uint8* pixels, int palid, int xleft, int flipped)
{
if (!flipped)
{
for (int j = 0; j < 8 / 2; j++)
{
Uint8 byte = SDL_ReadU8(tiles);
pixels[xleft + j * 2] = HE_MapColor(palid, (byte & 0xF0) / 0x10);
pixels[xleft + j * 2 + 1] = HE_MapColor(palid, byte & 0xF);
}
}
else
{
for (int j = 8 / 2 - 1; j >= 0; j--)
{
Uint8 byte = SDL_ReadU8(tiles);
pixels[xleft + j * 2 + 1] = HE_MapColor(palid, (byte & 0xF0) / 0x10);
pixels[xleft + j * 2] = HE_MapColor(palid, byte & 0xF);
}
}
}
void HE_DrawTile(SDL_RWops* tiles, int tileid, int palid, int flags, SDL_Surface* dst, int xleft, int ytop)
{
Uint8* pixels = HE_GetSurfacePixels(dst);
pixels += dst->pitch * ytop;
SDL_RWseek(tiles, tileid * 0x20, RW_SEEK_SET);
if ((flags & HE_TILE_YFLIP) == 0)
{
for (int i = 0; i < 8; i++)
{
draw_tile_hline(tiles, pixels, palid, xleft, flags & HE_TILE_XFLIP);
pixels += dst->pitch;
}
}
else
{
pixels += dst->pitch * 7;
for (int i = 7; i >= 0; i--)
{
draw_tile_hline(tiles, pixels, palid, xleft, flags & HE_TILE_XFLIP);
pixels -= dst->pitch;
}
}
}
static void draw_mappings_vline(SDL_RWops* tiles,
int tilesh, int pal, int map_flags, int xleft, int tilex, int ytop, int tile,
SDL_Surface* dst)
{
if ((map_flags & HE_TILE_YFLIP) == 0)
{
for (int h = 0; h < tilesh; h++)
{
HE_DrawTile(tiles, tile, pal, map_flags, dst, xleft + tilex * 8, ytop + h * 8);
tile++;
}
}
else
{
for (int h = tilesh - 1; h >= 0; h--)
{
HE_DrawTile(tiles, tile, pal, map_flags, dst, xleft + tilex * 8, ytop + h * 8);
tile++;
}
}
}
// TODO: comments
void HE_DrawSpriteMappings(
SDL_RWops* tiles, SDL_RWops* map, SDL_RWops* dplc,
int spriteid, int palid,
int xoffset, int yoffset,
SDL_Surface* dst)
{
SDL_RWseek(map, 2 * spriteid, RW_SEEK_SET);
SDL_RWseek(dplc, 2 * spriteid, RW_SEEK_SET);
int map_offset = SDL_ReadBE16(map);
SDL_RWseek(map, map_offset, RW_SEEK_SET);
int mappings_count = SDL_ReadU8(map);
int dplc_offset = SDL_ReadBE16(dplc);
// +1 to skip the DPLC count for the sprite
SDL_RWseek(dplc, dplc_offset, RW_SEEK_SET);
int dplc_count = SDL_ReadU8(dplc);
int dplc_tile = -1;
if (dplc_count == 1)
dplc_tile = SDL_ReadBE16(dplc) & 0xFFF;
for (int i = 0; i < mappings_count; i++)
{
int ytop = (Sint8)SDL_ReadU8(map) + yoffset;
Uint8 temp1 = SDL_ReadU8(map);
int tilesw = (temp1 & 0xC) / 0x4 + 1;
int tilesh = (temp1 & 0x3) + 1;
Uint16 temp2 = SDL_ReadBE16(map);
int pal = (temp2 & 0x6000) / 0x2000 + palid;
int map_flags = (temp2 & 0x1800) / 0x800;
int xleft = (Sint8)SDL_ReadU8(map) + xoffset;
int tile;
if (dplc_count > 1)
{
Uint16 dplc_value = SDL_ReadBE16(dplc);
tile = dplc_value & 0xFFF;
}
else
{
tile = dplc_tile;
}
if ((map_flags & HE_TILE_XFLIP) == 0)
{
for (int j = 0; j < tilesw; j++)
{
draw_mappings_vline(tiles, tilesh, pal, map_flags, xleft, j, ytop, tile, dst);
tile += tilesh;
}
}
else
{
for (int j = tilesw - 1; j >= 0; j--)
{
draw_mappings_vline(tiles, tilesh, pal, map_flags, xleft, j, ytop, tile, dst);
tile += tilesh;
}
}
}
}
int HE_GetSpriteMappingsCount(SDL_RWops* map)
{
SDL_RWseek(map, 0, RW_SEEK_SET);
return SDL_ReadBE16(map) / 2;
}