1 //SDL_inprint (c) driedfruit https://github.com/driedfruit/SDL_inprint
2 // modified for use in FBNeo
3 
4 #include "burner.h"
5 
6 #define inline_font_width     128
7 #define inline_font_height    64
8 static unsigned char inline_font_bits[] = {
9    0x01, 0x66, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
10    0xff, 0xff, 0xff, 0x39, 0xa5, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
11    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xc3, 0xff, 0xff, 0xff, 0xff, 0xff,
12    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x39, 0x18, 0xff, 0xff,
13    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x39,
14    0x18, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
15    0xff, 0xff, 0x19, 0xc3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
16    0xff, 0xff, 0xff, 0xff, 0xff, 0x98, 0xa5, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
17    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x66, 0xff, 0xff, 0xff,
18    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xbf,
19    0xe7, 0x99, 0x01, 0x83, 0xff, 0xe7, 0xe7, 0xe7, 0xff, 0xff, 0xff, 0xff, 0xff,
20    0xff, 0xf8, 0x8f, 0xc3, 0x99, 0x24, 0x39, 0xff, 0xc3, 0xc3, 0xe7, 0xe7, 0xf3,
21    0xff, 0xff, 0xff, 0xff, 0xe0, 0x83, 0x81, 0x99, 0x24, 0xe3, 0xff, 0x81, 0x81,
22    0xe7, 0xcf, 0xf9, 0xff, 0xff, 0xff, 0xff, 0x80, 0x80, 0xe7, 0x99, 0x21, 0xc9,
23    0xff, 0xe7, 0xe7, 0xe7, 0x80, 0x80, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x83, 0xe7,
24    0x99, 0x27, 0xc9, 0x81, 0x81, 0xe7, 0x81, 0xcf, 0xf9, 0xff, 0xff, 0xff, 0xff,
25    0xf8, 0x8f, 0x81, 0xff, 0x27, 0xe3, 0x81, 0xc3, 0xe7, 0xc3, 0xe7, 0xf3, 0xff,
26    0xff, 0xff, 0xff, 0xfe, 0xbf, 0xc3, 0x99, 0x27, 0xcc, 0x81, 0xe7, 0xe7, 0xe7,
27    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe7, 0xff, 0xff, 0xe1, 0xff,
28    0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf3, 0xc9, 0xc9,
29    0xf3, 0xff, 0xe3, 0xf9, 0xe7, 0xf9, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9f, 0xff,
30    0xe1, 0xc9, 0xc9, 0xc1, 0x9c, 0xc9, 0xf9, 0xf3, 0xf3, 0x99, 0xf3, 0xff, 0xff,
31    0xff, 0xcf, 0xff, 0xe1, 0xc9, 0x80, 0xfc, 0xcc, 0xe3, 0xfc, 0xf9, 0xe7, 0xc3,
32    0xf3, 0xff, 0xff, 0xff, 0xe7, 0xff, 0xf3, 0xff, 0xc9, 0xe1, 0xe7, 0x91, 0xff,
33    0xf9, 0xe7, 0x00, 0xc0, 0xff, 0xc0, 0xff, 0xf3, 0xff, 0xf3, 0xff, 0x80, 0xcf,
34    0xf3, 0xc4, 0xff, 0xf9, 0xe7, 0xc3, 0xf3, 0xff, 0xff, 0xff, 0xf9, 0xff, 0xff,
35    0xff, 0xc9, 0xe0, 0x99, 0xcc, 0xff, 0xf3, 0xf3, 0x99, 0xf3, 0xf3, 0xff, 0xf3,
36    0xfc, 0xff, 0xf3, 0xff, 0xc9, 0xf3, 0x9c, 0x91, 0xff, 0xe7, 0xf9, 0xff, 0xff,
37    0xf3, 0xff, 0xf3, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
38    0xff, 0xff, 0xff, 0xf9, 0xff, 0xff, 0xff, 0xc1, 0xf3, 0xe1, 0xe1, 0xc7, 0xc0,
39    0xe3, 0xc0, 0xe1, 0xe1, 0xff, 0xff, 0xe7, 0xff, 0xf9, 0xe1, 0x9c, 0xf1, 0xcc,
40    0xcc, 0xc3, 0xfc, 0xf9, 0xcc, 0xcc, 0xcc, 0xf3, 0xf3, 0xf3, 0xff, 0xf3, 0xcc,
41    0x8c, 0xf3, 0xcf, 0xcf, 0xc9, 0xe0, 0xfc, 0xcf, 0xcc, 0xcc, 0xf3, 0xf3, 0xf9,
42    0xc0, 0xe7, 0xcf, 0x84, 0xf3, 0xe3, 0xe3, 0xcc, 0xcf, 0xe0, 0xe7, 0xe1, 0xc1,
43    0xff, 0xff, 0xfc, 0xff, 0xcf, 0xe7, 0x90, 0xf3, 0xf9, 0xcf, 0x80, 0xcf, 0xcc,
44    0xf3, 0xcc, 0xcf, 0xff, 0xff, 0xf9, 0xff, 0xe7, 0xf3, 0x98, 0xf3, 0xcc, 0xcc,
45    0xcf, 0xcc, 0xcc, 0xf3, 0xcc, 0xe7, 0xf3, 0xf3, 0xf3, 0xc0, 0xf3, 0xff, 0xc1,
46    0xc0, 0xc0, 0xe1, 0x87, 0xe1, 0xe1, 0xf3, 0xe1, 0xf1, 0xf3, 0xf3, 0xe7, 0xff,
47    0xf9, 0xf3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
48    0xf9, 0xff, 0xff, 0xff, 0xff, 0xc1, 0xf3, 0xc0, 0xc3, 0xe0, 0x80, 0x80, 0xc3,
49    0xcc, 0xe1, 0x87, 0x98, 0xf0, 0x9c, 0x9c, 0xe3, 0x9c, 0xe1, 0x99, 0x99, 0xc9,
50    0xb9, 0xb9, 0x99, 0xcc, 0xf3, 0xcf, 0x99, 0xf9, 0x88, 0x98, 0xc9, 0x84, 0xcc,
51    0x99, 0xfc, 0x99, 0xe9, 0xe9, 0xfc, 0xcc, 0xf3, 0xcf, 0xc9, 0xf9, 0x80, 0x90,
52    0x9c, 0x84, 0xcc, 0xc1, 0xfc, 0x99, 0xe1, 0xe1, 0xfc, 0xc0, 0xf3, 0xcf, 0xe1,
53    0xf9, 0x80, 0x84, 0x9c, 0x84, 0xc0, 0x99, 0xfc, 0x99, 0xe9, 0xe9, 0x8c, 0xcc,
54    0xf3, 0xcc, 0xc9, 0xb9, 0x94, 0x8c, 0x9c, 0xfc, 0xcc, 0x99, 0x99, 0xc9, 0xb9,
55    0xf9, 0x99, 0xcc, 0xf3, 0xcc, 0x99, 0x99, 0x9c, 0x9c, 0xc9, 0xe1, 0xcc, 0xc0,
56    0xc3, 0xe0, 0x80, 0xf0, 0x83, 0xcc, 0xe1, 0xe1, 0x98, 0x80, 0x9c, 0x9c, 0xe3,
57    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
58    0xff, 0xff, 0xff, 0xc0, 0xe1, 0xc0, 0xe1, 0xc0, 0xcc, 0xcc, 0x9c, 0x9c, 0xcc,
59    0x80, 0xe1, 0xfc, 0xe1, 0xf7, 0xff, 0x99, 0xcc, 0x99, 0xcc, 0xd2, 0xcc, 0xcc,
60    0x9c, 0x9c, 0xcc, 0x9c, 0xf9, 0xf9, 0xe7, 0xe3, 0xff, 0x99, 0xcc, 0x99, 0xf8,
61    0xf3, 0xcc, 0xcc, 0x9c, 0xc9, 0xcc, 0xce, 0xf9, 0xf3, 0xe7, 0xc9, 0xff, 0xc1,
62    0xcc, 0xc1, 0xf1, 0xf3, 0xcc, 0xcc, 0x94, 0xe3, 0xe1, 0xe7, 0xf9, 0xe7, 0xe7,
63    0x9c, 0xff, 0xf9, 0xc4, 0xc9, 0xc7, 0xf3, 0xcc, 0xcc, 0x80, 0xe3, 0xf3, 0xb3,
64    0xf9, 0xcf, 0xe7, 0xff, 0xff, 0xf9, 0xe1, 0x99, 0xcc, 0xf3, 0xcc, 0xe1, 0x88,
65    0xc9, 0xf3, 0x99, 0xf9, 0x9f, 0xe7, 0xff, 0xff, 0xf0, 0xc7, 0x98, 0xe1, 0xe1,
66    0xc0, 0xf3, 0x9c, 0x9c, 0xe1, 0x80, 0xe1, 0xbf, 0xe1, 0xff, 0xff, 0xff, 0xff,
67    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
68    0x00, 0xf3, 0xff, 0xf8, 0xff, 0xc7, 0xff, 0xe3, 0xff, 0xf8, 0xf3, 0xcf, 0xf8,
69    0xf1, 0xff, 0xff, 0xff, 0xf3, 0xff, 0xf9, 0xff, 0xcf, 0xff, 0xc9, 0xff, 0xf9,
70    0xff, 0xff, 0xf9, 0xf3, 0xff, 0xff, 0xff, 0xe7, 0xe1, 0xf9, 0xe1, 0xcf, 0xe1,
71    0xf9, 0x91, 0xc9, 0xf1, 0xcf, 0x99, 0xf3, 0xcc, 0xe0, 0xe1, 0xff, 0xcf, 0xc1,
72    0xcc, 0xc1, 0xcc, 0xf0, 0xcc, 0x91, 0xf3, 0xcf, 0xc9, 0xf3, 0x80, 0xcc, 0xcc,
73    0xff, 0xc1, 0x99, 0xfc, 0xcc, 0xc0, 0xf9, 0xcc, 0x99, 0xf3, 0xcf, 0xe1, 0xf3,
74    0x80, 0xcc, 0xcc, 0xff, 0xcc, 0x99, 0xcc, 0xcc, 0xfc, 0xf9, 0xc1, 0x99, 0xf3,
75    0xcc, 0xc9, 0xf3, 0x94, 0xcc, 0xcc, 0xff, 0x91, 0xc4, 0xe1, 0x91, 0xe1, 0xf0,
76    0xcf, 0x98, 0xe1, 0xcc, 0x98, 0xe1, 0x9c, 0xcc, 0xe1, 0xff, 0xff, 0xff, 0xff,
77    0xff, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xe1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
78    0xff, 0xff, 0xff, 0xf7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0xe7, 0xf8,
79    0x91, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
80    0xf3, 0xe7, 0xf3, 0xc4, 0xf7, 0xc4, 0x91, 0xc4, 0xc1, 0xc1, 0xcc, 0xcc, 0x9c,
81    0x9c, 0xcc, 0xc0, 0xf3, 0xe7, 0xf3, 0xff, 0xe3, 0x99, 0xcc, 0x91, 0xfc, 0xf3,
82    0xcc, 0xcc, 0x94, 0xc9, 0xcc, 0xe6, 0xf8, 0xff, 0xc7, 0xff, 0xc9, 0x99, 0xcc,
83    0x99, 0xe1, 0xf3, 0xcc, 0xcc, 0x80, 0xe3, 0xcc, 0xf3, 0xf3, 0xe7, 0xf3, 0xff,
84    0x9c, 0xc1, 0xc1, 0xf9, 0xcf, 0xd3, 0xcc, 0xe1, 0x80, 0xc9, 0xc1, 0xd9, 0xf3,
85    0xe7, 0xf3, 0xff, 0x9c, 0xf9, 0xcf, 0xf0, 0xe0, 0xe7, 0x91, 0xf3, 0xc9, 0x9c,
86    0xcf, 0xc0, 0xc7, 0xe7, 0xf8, 0xff, 0x80, 0xf0, 0x87, 0xff, 0xff, 0xff, 0xff,
87    0xff, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
88 };
89 
90 #define CHARACTERS_PER_ROW       16 /* I like 16 x 8 fontsets. */
91 #define CHARACTERS_PER_COLUMN    8  /* 128 x 1 is another popular format. */
92 
93 static SDL_Renderer* selected_renderer = NULL;
94 static SDL_Texture* inline_font = NULL;
95 static SDL_Texture* selected_font = NULL;
96 static Uint16        selected_font_w, selected_font_h;
97 
prepare_inline_font()98 void prepare_inline_font()
99 {
100 	Uint32* pix_ptr, tmp;
101 	int          i, len, j;
102 	SDL_Surface* surface;
103 	Uint32       colors[2];
104 
105 	selected_font_w = inline_font_width;
106 	selected_font_h = inline_font_height;
107 
108 	if (inline_font != NULL)
109 	{
110 		selected_font = inline_font; return;
111 	}
112 
113 	surface = SDL_CreateRGBSurface(0, inline_font_width, inline_font_height, 32,
114 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
115 		0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff
116 #else
117 		0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
118 #endif
119 	);
120 	colors[0] = SDL_MapRGBA(surface->format, 0xFF, 0xFF, 0xFF, 0xFF);
121 	colors[1] = SDL_MapRGBA(surface->format, 0x00, 0x00, 0x00, 0x00 /* or 0xFF, to have bg-color */);
122 
123 	/* Get pointer to pixels and array length */
124 	pix_ptr = (Uint32*)surface->pixels;
125 	len = surface->h * surface->w / 8;
126 
127 	/* Copy */
128 	for (i = 0; i < len; i++)
129 	{
130 		tmp = (Uint8)inline_font_bits[i];
131 		for (j = 0; j < 8; j++)
132 		{
133 			Uint8 mask = (0x01 << j);
134 			pix_ptr[i * 8 + j] = colors[(tmp & mask) >> j];
135 		}
136 	}
137 
138 	inline_font = SDL_CreateTextureFromSurface(selected_renderer, surface);
139 	SDL_FreeSurface(surface);
140 
141 	selected_font = inline_font;
142 }
143 
kill_inline_font(void)144 void kill_inline_font(void)
145 {
146 	SDL_DestroyTexture(inline_font); inline_font = NULL;
147 }
148 
inrenderer(SDL_Renderer * renderer)149 void inrenderer(SDL_Renderer* renderer)
150 {
151 	selected_renderer = renderer;
152 }
153 
infont(SDL_Texture * font)154 void infont(SDL_Texture* font)
155 {
156 	Uint32 format;
157 	int    access;
158 	int    w, h;
159 
160 	if (font == NULL)
161 	{
162 		prepare_inline_font(); return;
163 	}
164 
165 	SDL_QueryTexture(font, &format, &access, &w, &h);
166 
167 	selected_font = font;
168 	selected_font_w = w;
169 	selected_font_h = h;
170 }
171 
incolor1(SDL_Color * color)172 void incolor1(SDL_Color* color)
173 {
174 	SDL_SetTextureColorMod(selected_font, color->r, color->g, color->b);
175 }
176 
incolor(Uint32 fore,Uint32 unused)177 void incolor(Uint32 fore, Uint32 unused) /* Color must be in 0x00RRGGBB format ! */
178 {
179 	SDL_Color pal[1];
180 
181 	pal[0].r = (Uint8)((fore & 0x00FF0000) >> 16);
182 	pal[0].g = (Uint8)((fore & 0x0000FF00) >> 8);
183 	pal[0].b = (Uint8)((fore & 0x000000FF));
184 	SDL_SetTextureColorMod(selected_font, pal[0].r, pal[0].g, pal[0].b);
185 }
186 
inprint(SDL_Renderer * dst,const char * str,Uint32 x,Uint32 y)187 void inprint(SDL_Renderer* dst, const char* str, Uint32 x, Uint32 y)
188 {
189 	SDL_Rect s_rect;
190 	SDL_Rect d_rect;
191 
192 	if (str == NULL)
193 	{
194 		return;
195 	}
196 
197 	d_rect.x = x;
198 	d_rect.y = y;
199 	s_rect.w = selected_font_w / CHARACTERS_PER_ROW;
200 	s_rect.h = selected_font_h / CHARACTERS_PER_COLUMN;
201 	d_rect.w = s_rect.w;
202 	d_rect.h = s_rect.h;
203 
204 	if (dst == NULL)
205 	{
206 		dst = selected_renderer;
207 	}
208 
209 	for (; *str; str++)
210 	{
211 		int id = (int)*str;
212 #if (CHARACTERS_PER_COLUMN != 1)
213 		int row = id / CHARACTERS_PER_ROW;
214 		int col = id % CHARACTERS_PER_ROW;
215 		s_rect.x = col * s_rect.w;
216 		s_rect.y = row * s_rect.h;
217 #else
218 		s_rect.x = id * s_rect.w;
219 		s_rect.y = 0;
220 #endif
221 		if (id == '\n')
222 		{
223 			d_rect.x = x;
224 			d_rect.y += s_rect.h;
225 			continue;
226 		}
227 		SDL_RenderCopy(dst, selected_font, &s_rect, &d_rect);
228 		d_rect.x += s_rect.w;
229 	}
230 }
231 
inprint_shadowed(SDL_Renderer * dst,const char * str,Uint32 x,Uint32 y)232 void inprint_shadowed(SDL_Renderer* dst, const char* str, Uint32 x, Uint32 y)
233 {
234 	SDL_Color pal[1];
235 
236 	SDL_GetTextureColorMod(selected_font, &pal[0].r, &pal[0].g, &pal[0].b);
237 	incolor(0x000000, 0);
238 	inprint(dst, str, x + 2, y + 2);
239 	SDL_SetTextureColorMod(selected_font, pal[0].r, pal[0].g, pal[0].b);
240 	inprint(dst, str, x, y);
241 }
242 
get_inline_font(void)243 SDL_Texture* get_inline_font(void)
244 {
245 	return selected_font;
246 }
247