-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtigr.h
230 lines (172 loc) · 7.53 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
// TIGR - TIny GRaphics Library - v1.3
// ^^ ^^
//
// 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
// Graphics configuration.
#ifdef _WIN32
#define TIGR_GAPI_D3D9
#else
#define TIGR_GAPI_GL
#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 b, g, r, a;
} TPixel;
// Windows flags.
#define TIGR_FIXED 0 // window's bitmap is a fixed size (default)
#define TIGR_AUTO 1 // window's bitmap will automatically resize after each tigrUpdate
#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
// A Tigr bitmap.
typedef struct Tigr {
int w, h; // width/height (unscaled)
TPixel *pix; // pixel data
void *handle; // OS window handle, NULL for off-screen bitmaps.
} Tigr;
// Creates a new empty window. (title is UTF-8)
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.
void tigrUpdate(Tigr *bmp);
// Sets post-FX properties for a window.
// hblur/vblur = whether to use bilinear filtering along that axis (boolean)
// scanlines = CRT scanlines effect (0-1)
// contrast = contrast boost (1 = no change, 2 = 2X contrast, etc)
void tigrSetPostFX(Tigr *bmp, int hblur, int vblur, float scanlines, float contrast);
// Drawing ----------------------------------------------------------------
// Helper for reading/writing pixels.
// For high performance, just write to bmp->pix yourself.
TPixel tigrGet(Tigr *bmp, int x, int y);
void tigrPlot(Tigr *bmp, int x, int y, TPixel pix);
// Clears a bitmap to a color.
void tigrClear(Tigr *bmp, TPixel color);
// Fills in a solid rectangle.
void tigrFill(Tigr *bmp, int x, int y, int w, int h, TPixel color);
// Draws an empty rectangle. (exclusive co-ords)
void tigrRect(Tigr *bmp, int x, int y, int w, int h, TPixel color);
// Draws a line.
void tigrLine(Tigr *bmp, int x0, int y0, int x1, int y1, TPixel color);
// Copies bitmap data.
// dx/dy = dest co-ordinates
// sx/sy = source co-ordinates
// w/h = width/height
void tigrBlit(Tigr *dest, Tigr *src, int dx, int dy, int sx, int sy, int w, int h);
// Same as tigrBlit, but blends with the bitmap alpha channel,
// and uses the 'alpha' variable to fade out.
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.
void tigrBlitTint(Tigr *dest, Tigr *src, int dx, int dy, int sx, int sy, int w, int h, TPixel tint);
// 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;
// Loads a font. The font bitmap should contain all characters
// for the given codepage, excluding the first 32 control codes.
// Supported codepages:
// 0 - Regular 7-bit ASCII
// 1252 - Windows 1252
TigrFont *tigrLoadFont(Tigr *bitmap, int codepage);
// Frees a font.
void tigrFreeFont(TigrFont *font);
// Prints UTF-8 text onto a bitmap.
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);
// 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);
// 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();
// 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