-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.h
376 lines (336 loc) · 12.5 KB
/
graphics.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
#ifndef GRAPHICS_H
#define GRAPHICS_H
#include <ft2build.h>
#include FT_FREETYPE_H
#include <psptypes.h>
#define PSP_LINE_SIZE 512
#define SCREEN_WIDTH 480
#define SCREEN_HEIGHT 272
typedef u32 Color;
#define A(color) ((u8)(color >> 24 & 0xFF))
#define B(color) ((u8)(color >> 16 & 0xFF))
#define G(color) ((u8)(color >> 8 & 0xFF))
#define R(color) ((u8)(color & 0xFF))
typedef struct
{
int textureWidth; // the real width of data, 2^n with n>=0
int textureHeight; // the real height of data, 2^n with n>=0
int imageWidth; // the image width
int imageHeight;
Color* data;
} Image;
/**
* Load a PNG or JPEG image (depends on the filename suffix).
*
* @pre filename != NULL
* @param filename - filename of the PNG image to load
* @return pointer to a new allocated Image struct, or NULL on failure
*/
extern Image* loadImage(const char* filename);
/**
* Load a PNG image.
*
* @pre filename != NULL
* @param filename - filename of the PNG image to load
* @return pointer to a new allocated Image struct, or NULL on failure
*/
extern Image* loadPngImage(const char* filename);
/**
* Load a JPEG image.
*
* @pre filename != NULL
* @param filename - filename of the JPEG image to load
* @return pointer to a new allocated Image struct, or NULL on failure
*/
extern Image* loadJpegImage(const char* filename);
/**
* Load a PNG or JPEG image from in-memory data (with auto detection of the format).
*
* @pre data != NULL && len >0
* @param data - the in-memory bytes of the image
* @param len - the number of valid bytes at the data pointer
* @return pointer to a new allocated Image struct, or NULL on failure
*/
extern Image* loadImageFromMemory(const unsigned char* data, int len);
/**
* Blit a rectangle part of an image to another image.
*
* @pre source != NULL && destination != NULL &&
* sx >= 0 && sy >= 0 &&
* width > 0 && height > 0 &&
* sx + width <= source->width && sy + height <= source->height &&
* dx + width <= destination->width && dy + height <= destination->height
* @param sx - left position of rectangle in source image
* @param sy - top position of rectangle in source image
* @param width - width of rectangle in source image
* @param height - height of rectangle in source image
* @param source - pointer to Image struct of the source image
* @param dx - left target position in destination image
* @param dy - top target position in destination image
* @param destination - pointer to Image struct of the destination image
*/
extern void blitImageToImage(int sx, int sy, int width, int height, Image* source, int dx, int dy, Image* destination);
/**
* Blit a rectangle part of an image to screen.
*
* @pre source != NULL && destination != NULL &&
* sx >= 0 && sy >= 0 &&
* width > 0 && height > 0 &&
* sx + width <= source->width && sy + height <= source->height &&
* dx + width <= SCREEN_WIDTH && dy + height <= SCREEN_HEIGHT
* @param sx - left position of rectangle in source image
* @param sy - top position of rectangle in source image
* @param width - width of rectangle in source image
* @param height - height of rectangle in source image
* @param source - pointer to Image struct of the source image
* @param dx - left target position in destination image
* @param dy - top target position in destination image
*/
extern void blitImageToScreen(int sx, int sy, int width, int height, Image* source, int dx, int dy);
/**
* Blit a rectangle part of an image to another image without alpha pixels in source image.
*
* @pre source != NULL && destination != NULL &&
* sx >= 0 && sy >= 0 &&
* width > 0 && height > 0 &&
* sx + width <= source->width && sy + height <= source->height &&
* dx + width <= destination->width && dy + height <= destination->height
* @param sx - left position of rectangle in source image
* @param sy - top position of rectangle in source image
* @param width - width of rectangle in source image
* @param height - height of rectangle in source image
* @param source - pointer to Image struct of the source image
* @param dx - left target position in destination image
* @param dy - top target position in destination image
* @param destination - pointer to Image struct of the destination image
*/
extern void blitAlphaImageToImage(int sx, int sy, int width, int height, Image* source, int dx, int dy, Image* destination);
/**
* Blit a rectangle part of an image to screen without alpha pixels in source image.
*
* @pre source != NULL && destination != NULL &&
* sx >= 0 && sy >= 0 &&
* width > 0 && height > 0 &&
* sx + width <= source->width && sy + height <= source->height &&
* dx + width <= SCREEN_WIDTH && dy + height <= SCREEN_HEIGHT
* @param sx - left position of rectangle in source image
* @param sy - top position of rectangle in source image
* @param width - width of rectangle in source image
* @param height - height of rectangle in source image
* @param source - pointer to Image struct of the source image
* @param dx - left target position in destination image
* @param dy - top target position in destination image
*/
extern void blitAlphaImageToScreen(int sx, int sy, int width, int height, Image* source, int dx, int dy);
extern void blitCardImageToScreen(int sx, int sy, int width, int height, Image* source, int dx, int dy);
extern void blitAlphaImageToScreenScaled(int sx, int sy, int width, int height, Image* source, int dx, int dy, int endx, int endy);
/**
* Create an empty image.
*
* @pre width > 0 && height > 0 && width <= 512 && height <= 512
* @param width - width of the new image
* @param height - height of the new image
* @return pointer to a new allocated Image struct, all pixels initialized to color 0, or NULL on failure
*/
extern Image* createImage(int width, int height);
/**
* Frees an allocated image.
*
* @pre image != null
* @param image a pointer to an image struct
*/
extern void freeImage(Image* image);
/**
* Initialize all pixels of an image with a color.
*
* @pre image != NULL
* @param color - new color for the pixels
* @param image - image to clear
*/
extern void clearImage(Color color, Image* image);
/**
* Initialize all pixels of the screen with a color.
*
* @param color - new color for the pixels
*/
extern void clearScreen(Color color);
/**
* Fill a rectangle of an image with a color.
*
* @pre image != NULL
* @param color - new color for the pixels
* @param x0 - left position of rectangle in image
* @param y0 - top position of rectangle in image
* @param width - width of rectangle in image
* @param height - height of rectangle in image
* @param image - image
*/
extern void fillImageRect(Color color, int x0, int y0, int width, int height, Image* image);
/**
* Fill a rectangle of an image with a color.
*
* @pre image != NULL
* @param color - new color for the pixels
* @param x0 - left position of rectangle in image
* @param y0 - top position of rectangle in image
* @param width - width of rectangle in image
* @param height - height of rectangle in image
*/
extern void fillScreenRect(Color color, int x0, int y0, int width, int height);
/**
* Set a pixel on screen to the specified color.
*
* @pre x >= 0 && x < SCREEN_WIDTH && y >= 0 && y < SCREEN_HEIGHT
* @param color - new color for the pixels
* @param x - left position of the pixel
* @param y - top position of the pixel
*/
extern void putPixelScreen(Color color, int x, int y);
/**
* Set a pixel in an image to the specified color.
*
* @pre x >= 0 && x < image->imageWidth && y >= 0 && y < image->imageHeight && image != NULL
* @param color - new color for the pixels
* @param x - left position of the pixel
* @param y - top position of the pixel
*/
extern void putPixelImage(Color color, int x, int y, Image* image);
/**
* Get the color of a pixel on screen.
*
* @pre x >= 0 && x < SCREEN_WIDTH && y >= 0 && y < SCREEN_HEIGHT
* @param x - left position of the pixel
* @param y - top position of the pixel
* @return the color of the pixel
*/
extern Color getPixelScreen(int x, int y);
/**
* Get the color of a pixel of an image.
*
* @pre x >= 0 && x < image->imageWidth && y >= 0 && y < image->imageHeight && image != NULL
* @param x - left position of the pixel
* @param y - top position of the pixel
* @return the color of the pixel
*/
extern Color getPixelImage(int x, int y, Image* image);
/**
* Print a text (pixels out of the screen or image are clipped).
*
* @param x - left position of text
* @param y - top position of text
* @param text - the text to print
* @param color - text color
*/
extern void printTextScreen(int x, int y, const char* text, Color color);
/**
* Print a text (pixels out of the screen or image are clipped).
*
* @param x - left position of text
* @param y - top position of text
* @param text - the text to print
* @param color - text color
* @param image - image
*/
extern void printTextImage(int x, int y, const char* text, Color color, Image* image);
/**
* Print a text, which was rendered to a bitmap with Freetype.
*
* @param x - left position of text
* @param y - top position of text
* @param text - the text to print
* @param color - text color
* @param image - image
*/
extern void fontPrintTextImage(FT_Bitmap* bitmap, int x, int y, Color color, Image* image);
/**
* Print a text, which was rendered to a bitmap with Freetype.
*
* @param x - left position of text
* @param y - top position of text
* @param text - the text to print
* @param color - text color
*/
extern void fontPrintTextScreen(FT_Bitmap* bitmap, int x, int y, Color color);
/**
* Save an image or the screen in PNG or JPEG format (depends on the filename suffix).
*
* @pre filename != NULL
* @param filename - filename of the PNG image
* @param data - start of Color type pixel data (can be getVramDisplayBuffer())
* @param width - logical width of the image or SCREEN_WIDTH
* @param height - height of the image or SCREEN_HEIGHT
* @param lineSize - physical width of the image or PSP_LINE_SIZE
* @param saveAlpha - if 0, image is saved without alpha channel
*/
extern void saveImage(const char* filename, Color* data, int width, int height, int lineSize, int saveAlpha);
/**
* Save an image or the screen in PNG format.
*
* @pre filename != NULL
* @param filename - filename of the PNG image
* @param data - start of Color type pixel data (can be getVramDisplayBuffer())
* @param width - logical width of the image or SCREEN_WIDTH
* @param height - height of the image or SCREEN_HEIGHT
* @param lineSize - physical width of the image or PSP_LINE_SIZE
* @param saveAlpha - if 0, image is saved without alpha channel
*/
extern void savePngImage(const char* filename, Color* data, int width, int height, int lineSize, int saveAlpha);
/**
* Save an image or the screen in JPEG format.
*
* @pre filename != NULL
* @param filename - filename of the JPEG image
* @param data - start of Color type pixel data (can be getVramDisplayBuffer())
* @param width - logical width of the image or SCREEN_WIDTH
* @param height - height of the image or SCREEN_HEIGHT
* @param lineSize - physical width of the image or PSP_LINE_SIZE
*/
extern void saveJpegImage(const char* filename, Color* data, int width, int height, int lineSize);
/**
* Exchange display buffer and drawing buffer.
*/
extern void flipScreen();
/**
* Initialize the graphics.
*/
extern void initGraphics();
/**
* Disable graphics, used for debug text output.
*/
extern void disableGraphics();
/**
* Draw a line to screen.
*
* @pre x0 >= 0 && x0 < SCREEN_WIDTH && y0 >= 0 && y0 < SCREEN_HEIGHT &&
* x1 >= 0 && x1 < SCREEN_WIDTH && y1 >= 0 && y1 < SCREEN_HEIGHT
* @param x0 - x line start position
* @param y0 - y line start position
* @param x1 - x line end position
* @param y1 - y line end position
*/
void drawLineScreen(int x0, int y0, int x1, int y1, Color color);
/**
* Draw a line to screen.
*
* @pre x0 >= 0 && x0 < image->imageWidth && y0 >= 0 && y0 < image->imageHeight &&
* x1 >= 0 && x1 < image->imageWidth && y1 >= 0 && y1 < image->imageHeight
* @param x0 - x line start position
* @param y0 - y line start position
* @param x1 - x line end position
* @param y1 - y line end position
*/
extern void drawLineImage(int x0, int y0, int x1, int y1, Color color, Image* image);
/**
* Get the current draw buffer for fast unchecked access.
*
* @return the start address of the current draw buffer
*/
extern Color* getVramDrawBuffer();
/**
* Get the current display buffer for fast unchecked access.
*
* @return the start address of the current display buffer
*/
extern Color* getVramDisplayBuffer();
extern void guStart();
#endif