-
Notifications
You must be signed in to change notification settings - Fork 0
/
bg.c
145 lines (131 loc) · 2.85 KB
/
bg.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
#include "config.h"
#ifdef ENABLE_BG
#include <stdlib.h>
#include <string.h>
#include "display.h"
#include "image.h"
#include "bg.h"
static pixel * bg_start = (pixel *)(0x44000000 + 512 * 272 * PIXEL_BYTES * 2);
static bool have_bg = false;
static dword imgwidth, imgheight;
extern void bg_load(const char * filename, pixel bgcolor, t_fs_filetype ft, dword grayscale)
{
pixel * imgdata, * imgshow = NULL, * img_buf, * bg_buf, * bg_line, * bg_end, * bg_lineend;
dword width, height, w2, h2, left, top;
pixel bgc;
int result;
switch(ft)
{
case fs_filetype_png:
result = image_readpng(filename, &width, &height, &imgdata, &bgc);
break;
case fs_filetype_gif:
result = image_readgif(filename, &width, &height, &imgdata, &bgc);
break;
case fs_filetype_jpg:
result = image_readjpg(filename, &width, &height, &imgdata, &bgc);
break;
case fs_filetype_tga:
result = image_readtga(filename, &width, &height, &imgdata, &bgc);
break;
case fs_filetype_bmp:
result = image_readbmp(filename, &width, &height, &imgdata, &bgc);
break;
default:
result = -1;
}
if(result != 0)
return;
if(width > 480)
{
h2 = height * 480 / width;
if(h2 > 272)
{
h2 = 272;
w2 = width * 272 / height;
}
else
w2 = 480;
}
else if(height > 272)
{
h2 = 272;
w2 = width * 272 / height;
}
else
{
h2 = height;
w2 = width;
}
if(width != w2 || height != h2)
{
imgshow = (pixel *)malloc(sizeof(pixel) * w2 * h2);
if(imgshow == NULL)
{
free((void *)imgdata);
return;
}
image_zoom_bicubic(imgdata, width, height, imgshow, w2, h2);
}
else
imgshow = imgdata;
if(w2 < 480)
left = (480 - w2) / 2;
else
left = 0;
if(h2 < 272)
top = (272 - h2) / 2;
else
top = 0;
for(bg_buf = bg_start, bg_end = bg_start + 0x22000; bg_buf < bg_end; bg_buf ++)
* bg_buf = bgcolor;
img_buf = imgshow;
dword r = RGB_R(bgcolor), g = RGB_G(bgcolor), b = RGB_B(bgcolor);
for(bg_buf = bg_start + top * 512 + left, bg_end = bg_buf + h2 * 512; bg_buf < bg_end; bg_buf += 512)
for(bg_line = bg_buf, bg_lineend = bg_buf + w2; bg_line < bg_lineend; bg_line ++)
{
*bg_line = disp_grayscale(*img_buf, r, g, b, grayscale);
img_buf ++;
}
have_bg = true;
if(imgshow != imgdata)
free((void *)imgshow);
imgwidth = w2; imgheight = h2;
free((void *)imgdata);
}
extern bool bg_display()
{
if(have_bg)
{
memcpy(vram_start, bg_start, 512 * 272 * PIXEL_BYTES);
return true;
}
return false;
}
extern void bg_cancel()
{
have_bg = false;
}
static byte * _cache = NULL;
extern void bg_cache()
{
if(!have_bg)
return;
if(_cache != NULL)
free((void *)_cache);
_cache = malloc(512 * 272 * PIXEL_BYTES);
if(_cache == NULL)
return;
memcpy(_cache, bg_start, 512 * 272 * PIXEL_BYTES);
}
extern void bg_restore()
{
if(!have_bg)
return;
if(_cache == NULL)
return;
memcpy(bg_start, _cache, 512 * 272 * PIXEL_BYTES);
free((void *)_cache);
_cache = NULL;
}
#endif