forked from erkkah/tigr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tigr.h
356 lines (286 loc) · 11.8 KB
/
tigr.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
// TIGR - TIny GRaphics Library - v3.1
// ^^ ^^
//
// rawr.
/*
This is free and unencumbered software released into the public domain.
Our intent is that anyone is free to copy and use this software,
for any purpose, in any form, and by any means.
The authors dedicate any and all copyright interest in the software
to the public domain, at their own expense for the betterment of mankind.
The software is provided "as is", without any kind of warranty, including
any implied warranty. If it breaks, you get to keep both pieces.
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
// Compiler configuration.
#ifdef _MSC_VER
#define TIGR_INLINE static __forceinline
#else
#define TIGR_INLINE static inline
#endif
// Bitmaps ----------------------------------------------------------------
// This struct contains one pixel.
typedef struct {
unsigned char r, g, b, a;
} TPixel;
// Window flags.
#define TIGR_FIXED 0 // window's bitmap is a fixed size (default)
#define TIGR_AUTO 1 // window's bitmap is scaled with the window
#define TIGR_2X 2 // always enforce (at least) 2X pixel scale
#define TIGR_3X 4 // always enforce (at least) 3X pixel scale
#define TIGR_4X 8 // always enforce (at least) 4X pixel scale
#define TIGR_RETINA 16 // enable retina support on OS X
#define TIGR_NOCURSOR 32 // hide cursor
#define TIGR_FULLSCREEN 64 // start in full-screen mode
// A Tigr bitmap.
typedef struct Tigr {
int w, h; // width/height (unscaled)
int cx, cy, cw, ch; // clip rect
TPixel *pix; // pixel data
void *handle; // OS window handle, NULL for off-screen bitmaps.
int blitMode; // Target bitmap blit mode
} Tigr;
// Creates a new empty window with a given bitmap size.
//
// Title is UTF-8.
//
// In TIGR_FIXED mode, the window is made as large as possible to contain an integer-scaled
// version of the bitmap while still fitting on the screen. Resizing the window will adapt
// the scale in integer steps to fit the bitmap.
//
// In TIGR_AUTO mode, the initial window size is set to the bitmap size times the pixel
// scale. Resizing the window will resize the bitmap using the specified scale.
// For example, in forced 2X mode, the window will be twice as wide (and high) as the bitmap.
//
// Turning on TIGR_RETINA mode will request full backing resolution on OSX, meaning that
// the effective window size might be integer scaled to a larger size. In TIGR_AUTO mode,
// this means that the Tigr bitmap will change size if the window is moved between
// retina and non-retina screens.
//
Tigr *tigrWindow(int w, int h, const char *title, int flags);
// Creates an empty off-screen bitmap.
Tigr *tigrBitmap(int w, int h);
// Deletes a window/bitmap.
void tigrFree(Tigr *bmp);
// Returns non-zero if the user requested to close a window.
int tigrClosed(Tigr *bmp);
// Displays a window's contents on-screen and updates input.
void tigrUpdate(Tigr *bmp);
// Called before doing direct OpenGL calls and before tigrUpdate.
// Returns non-zero if OpenGL is available.
int tigrBeginOpenGL(Tigr *bmp);
// Sets post shader for a window.
// This replaces the built-in post-FX shader.
void tigrSetPostShader(Tigr *bmp, const char* code, int size);
// Sets post-FX properties for a window.
//
// The built-in post-FX shader uses the following parameters:
// p1: hblur - use bilinear filtering along the x-axis (pixels)
// p2: vblur - use bilinear filtering along the y-axis (pixels)
// p3: scanlines - CRT scanlines effect (0-1)
// p4: contrast - contrast boost (1 = no change, 2 = 2X contrast, etc)
void tigrSetPostFX(Tigr *bmp, float p1, float p2, float p3, float p4);
// Drawing ----------------------------------------------------------------
// Helper for reading pixels.
// For high performance, just access bmp->pix directly.
TPixel tigrGet(Tigr *bmp, int x, int y);
// Plots a pixel.
// Clips and blends.
// For high performance, just access bmp->pix directly.
void tigrPlot(Tigr *bmp, int x, int y, TPixel pix);
// Clears a bitmap to a color.
// No blending, no clipping.
void tigrClear(Tigr *bmp, TPixel color);
// Fills a rectangular area.
// No blending, no clipping.
void tigrFill(Tigr *bmp, int x, int y, int w, int h, TPixel color);
// Draws a line.
// Start pixel is drawn, end pixel is not.
// Clips and blends.
void tigrLine(Tigr *bmp, int x0, int y0, int x1, int y1, TPixel color);
// Draws an empty rectangle.
// Drawing a 1x1 rectangle yields the same result as calling tigrPlot.
// Clips and blends.
void tigrRect(Tigr *bmp, int x, int y, int w, int h, TPixel color);
// Fills a rectangle.
// Fills the inside of the specified rectangular area.
// Calling tigrRect followed by tigrFillRect using the same arguments
// causes no overdrawing.
// Clips and blends.
void tigrFillRect(Tigr *bmp, int x, int y, int w, int h, TPixel color);
// Draws a circle.
// Drawing a zero radius circle yields the same result as calling tigrPlot.
// Drawing a circle with radius one draws a circle three pixels wide.
// Clips and blends.
void tigrCircle(Tigr *bmp, int x, int y, int r, TPixel color);
// Fills a circle.
// Fills the inside of the specified circle.
// Calling tigrCircle followed by tigrFillCircle using the same arguments
// causes no overdrawing.
// Filling a circle with zero radius has no effect.
// Clips and blends.
void tigrFillCircle(Tigr *bmp, int x, int y, int r, TPixel color);
// Sets clip rect.
// Set to (0, 0, -1, -1) to reset clipping to full bitmap.
void tigrClip(Tigr *bmp, int cx, int cy, int cw, int ch);
// Copies bitmap data.
// dx/dy = dest co-ordinates
// sx/sy = source co-ordinates
// w/h = width/height
//
// RGBAdest = RGBAsrc
// Clips, does not blend.
void tigrBlit(Tigr *dest, Tigr *src, int dx, int dy, int sx, int sy, int w, int h);
// Same as tigrBlit, but alpha blends the source bitmap with the
// target using per pixel alpha and the specified global alpha.
//
// Ablend = Asrc * alpha
// RGBdest = RGBsrc * Ablend + RGBdest * (1 - Ablend)
//
// Blit mode == TIGR_KEEP_ALPHA:
// Adest = Adest
//
// Blit mode == TIGR_BLEND_ALPHA:
// Adest = Asrc * Ablend + Adest * (1 - Ablend)
// Clips and blends.
void tigrBlitAlpha(Tigr *dest, Tigr *src, int dx, int dy, int sx, int sy, int w, int h, float alpha);
// Same as tigrBlit, but tints the source bitmap with a color
// and alpha blends the resulting source with the destination.
//
// Rblend = Rsrc * Rtint
// Gblend = Gsrc * Gtint
// Bblend = Bsrc * Btint
// Ablend = Asrc * Atint
//
// RGBdest = RGBblend * Ablend + RGBdest * (1 - Ablend)
//
// Blit mode == TIGR_KEEP_ALPHA:
// Adest = Adest
//
// Blit mode == TIGR_BLEND_ALPHA:
// Adest = Ablend * Ablend + Adest * (1 - Ablend)
// Clips and blends.
void tigrBlitTint(Tigr *dest, Tigr *src, int dx, int dy, int sx, int sy, int w, int h, TPixel tint);
enum TIGRBlitMode {
TIGR_KEEP_ALPHA = 0, // Keep destination alpha value
TIGR_BLEND_ALPHA = 1, // Blend destination alpha (default)
};
// Set destination bitmap blend mode for blit operations.
void tigrBlitMode(Tigr *dest, int mode);
// Helper for making colors.
TIGR_INLINE TPixel tigrRGB(unsigned char r, unsigned char g, unsigned char b)
{
TPixel p; p.r = r; p.g = g; p.b = b; p.a = 0xff; return p;
}
// Helper for making colors.
TIGR_INLINE TPixel tigrRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
TPixel p; p.r = r; p.g = g; p.b = b; p.a = a; return p;
}
// Font printing ----------------------------------------------------------
typedef struct {
int code, x, y, w, h;
} TigrGlyph;
typedef struct {
Tigr *bitmap;
int numGlyphs;
TigrGlyph *glyphs;
} TigrFont;
typedef enum {
TCP_ASCII = 0,
TCP_1252 = 1252,
TCP_UTF32 = 12001
} TCodepage;
// Loads a font.
//
// Codepages:
//
// TCP_ASCII - Regular 7-bit ASCII
// TCP_1252 - Windows 1252
// TCP_UTF32 - Unicode subset
//
// For ASCII and 1252, the font bitmap should contain all characters
// for the given codepage, excluding the first 32 control codes.
//
// For UTF32 - the font bitmap contains a subset of Unicode characters
// and must be in the format generated by tigrFont for UTF32.
//
TigrFont *tigrLoadFont(Tigr *bitmap, int codepage);
// Frees a font.
void tigrFreeFont(TigrFont *font);
// Prints UTF-8 text onto a bitmap.
// NOTE:
// This uses the target bitmap blit mode.
// See tigrBlitTint for details.
void tigrPrint(Tigr *dest, TigrFont *font, int x, int y, TPixel color, const char *text, ...);
// Returns the width/height of a string.
int tigrTextWidth(TigrFont *font, const char *text);
int tigrTextHeight(TigrFont *font, const char *text);
// The built-in font.
extern TigrFont *tfont;
// User Input -------------------------------------------------------------
// Key scancodes. For letters/numbers, use ASCII ('A'-'Z' and '0'-'9').
typedef enum {
TK_PAD0=128,TK_PAD1,TK_PAD2,TK_PAD3,TK_PAD4,TK_PAD5,TK_PAD6,TK_PAD7,TK_PAD8,TK_PAD9,
TK_PADMUL,TK_PADADD,TK_PADENTER,TK_PADSUB,TK_PADDOT,TK_PADDIV,
TK_F1,TK_F2,TK_F3,TK_F4,TK_F5,TK_F6,TK_F7,TK_F8,TK_F9,TK_F10,TK_F11,TK_F12,
TK_BACKSPACE,TK_TAB,TK_RETURN,TK_SHIFT,TK_CONTROL,TK_ALT,TK_PAUSE,TK_CAPSLOCK,
TK_ESCAPE,TK_SPACE,TK_PAGEUP,TK_PAGEDN,TK_END,TK_HOME,TK_LEFT,TK_UP,TK_RIGHT,TK_DOWN,
TK_INSERT,TK_DELETE,TK_LWIN,TK_RWIN,TK_NUMLOCK,TK_SCROLL,TK_LSHIFT,TK_RSHIFT,
TK_LCONTROL,TK_RCONTROL,TK_LALT,TK_RALT,TK_SEMICOLON,TK_EQUALS,TK_COMMA,TK_MINUS,
TK_DOT,TK_SLASH,TK_BACKTICK,TK_LSQUARE,TK_BACKSLASH,TK_RSQUARE,TK_TICK
} TKey;
// Returns mouse input for a window.
void tigrMouse(Tigr *bmp, int *x, int *y, int *buttons);
typedef struct {
int x;
int y;
} TigrTouchPoint;
// Reads touch input for a window.
// Returns number of touch points read.
int tigrTouch(Tigr *bmp, TigrTouchPoint* points, int maxPoints);
// Reads the keyboard for a window.
// Returns non-zero if a key is pressed/held.
// tigrKeyDown tests for the initial press, tigrKeyHeld repeats each frame.
int tigrKeyDown(Tigr *bmp, int key);
int tigrKeyHeld(Tigr *bmp, int key);
// Reads character input for a window.
// Returns the Unicode value of the last key pressed, or 0 if none.
int tigrReadChar(Tigr *bmp);
// Show / hide virtual keyboard.
// (Only available on iOS / Android)
void tigrShowKeyboard(int show);
// Bitmap I/O -------------------------------------------------------------
// Loads a PNG, from either a file or memory. (fileName is UTF-8)
// On error, returns NULL and sets errno.
Tigr *tigrLoadImage(const char *fileName);
Tigr *tigrLoadImageMem(const void *data, int length);
// Saves a PNG to a file. (fileName is UTF-8)
// On error, returns zero and sets errno.
int tigrSaveImage(const char *fileName, Tigr *bmp);
// Helpers ----------------------------------------------------------------
// Returns the amount of time elapsed since tigrTime was last called,
// or zero on the first call.
float tigrTime(void);
// Displays an error message and quits. (UTF-8)
// 'bmp' can be NULL.
void tigrError(Tigr *bmp, const char *message, ...);
// Reads an entire file into memory. (fileName is UTF-8)
// Free it yourself after with 'free'.
// On error, returns NULL and sets errno.
// TIGR will automatically append a NUL terminator byte
// to the end (not included in the length)
void *tigrReadFile(const char *fileName, int *length);
// Decompresses DEFLATEd zip/zlib data into a buffer.
// Returns non-zero on success.
int tigrInflate(void *out, unsigned outlen, const void *in, unsigned inlen);
// Decodes a single UTF8 codepoint and returns the next pointer.
const char *tigrDecodeUTF8(const char *text, int *cp);
// Encodes a single UTF8 codepoint and returns the next pointer.
char *tigrEncodeUTF8(char *text, int cp);
#ifdef __cplusplus
}
#endif