tigr.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // TIGR - TIny GRaphics Library - v3.2
  2. // ^^ ^^
  3. //
  4. // rawr.
  5. /*
  6. This is free and unencumbered software released into the public domain.
  7. Our intent is that anyone is free to copy and use this software,
  8. for any purpose, in any form, and by any means.
  9. The authors dedicate any and all copyright interest in the software
  10. to the public domain, at their own expense for the betterment of mankind.
  11. The software is provided "as is", without any kind of warranty, including
  12. any implied warranty. If it breaks, you get to keep both pieces.
  13. */
  14. #pragma once
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. // Compiler configuration.
  19. #ifdef _MSC_VER
  20. #define TIGR_INLINE static __forceinline
  21. #else
  22. #define TIGR_INLINE static inline
  23. #endif
  24. // Bitmaps ----------------------------------------------------------------
  25. // This struct contains one pixel.
  26. typedef struct {
  27. unsigned char r, g, b, a;
  28. } TPixel;
  29. // Window flags.
  30. #define TIGR_FIXED 0 // window's bitmap is a fixed size (default)
  31. #define TIGR_AUTO 1 // window's bitmap is scaled with the window
  32. #define TIGR_2X 2 // always enforce (at least) 2X pixel scale
  33. #define TIGR_3X 4 // always enforce (at least) 3X pixel scale
  34. #define TIGR_4X 8 // always enforce (at least) 4X pixel scale
  35. #define TIGR_RETINA 16 // enable retina support on OS X
  36. #define TIGR_NOCURSOR 32 // hide cursor
  37. #define TIGR_FULLSCREEN 64 // start in full-screen mode
  38. // A Tigr bitmap.
  39. typedef struct Tigr {
  40. int w, h; // width/height (unscaled)
  41. int cx, cy, cw, ch; // clip rect
  42. TPixel *pix; // pixel data
  43. void *handle; // OS window handle, NULL for off-screen bitmaps.
  44. int blitMode; // Target bitmap blit mode
  45. } Tigr;
  46. // Creates a new empty window with a given bitmap size.
  47. //
  48. // Title is UTF-8.
  49. //
  50. // In TIGR_FIXED mode, the window is made as large as possible to contain an integer-scaled
  51. // version of the bitmap while still fitting on the screen. Resizing the window will adapt
  52. // the scale in integer steps to fit the bitmap.
  53. //
  54. // In TIGR_AUTO mode, the initial window size is set to the bitmap size times the pixel
  55. // scale. Resizing the window will resize the bitmap using the specified scale.
  56. // For example, in forced 2X mode, the window will be twice as wide (and high) as the bitmap.
  57. //
  58. // Turning on TIGR_RETINA mode will request full backing resolution on OSX, meaning that
  59. // the effective window size might be integer scaled to a larger size. In TIGR_AUTO mode,
  60. // this means that the Tigr bitmap will change size if the window is moved between
  61. // retina and non-retina screens.
  62. //
  63. Tigr *tigrWindow(int w, int h, const char *title, int flags);
  64. // Creates an empty off-screen bitmap.
  65. Tigr *tigrBitmap(int w, int h);
  66. // Deletes a window/bitmap.
  67. void tigrFree(Tigr *bmp);
  68. // Returns non-zero if the user requested to close a window.
  69. int tigrClosed(Tigr *bmp);
  70. // Displays a window's contents on-screen and updates input.
  71. void tigrUpdate(Tigr *bmp);
  72. // Called before doing direct OpenGL calls and before tigrUpdate.
  73. // Returns non-zero if OpenGL is available.
  74. int tigrBeginOpenGL(Tigr *bmp);
  75. // Sets post shader for a window.
  76. // This replaces the built-in post-FX shader.
  77. void tigrSetPostShader(Tigr *bmp, const char* code, int size);
  78. // Sets post-FX properties for a window.
  79. //
  80. // The built-in post-FX shader uses the following parameters:
  81. // p1: hblur - use bilinear filtering along the x-axis (pixels)
  82. // p2: vblur - use bilinear filtering along the y-axis (pixels)
  83. // p3: scanlines - CRT scanlines effect (0-1)
  84. // p4: contrast - contrast boost (1 = no change, 2 = 2X contrast, etc)
  85. void tigrSetPostFX(Tigr *bmp, float p1, float p2, float p3, float p4);
  86. // Drawing ----------------------------------------------------------------
  87. // Helper for reading pixels.
  88. // For high performance, just access bmp->pix directly.
  89. TPixel tigrGet(Tigr *bmp, int x, int y);
  90. // Plots a pixel.
  91. // Clips and blends.
  92. // For high performance, just access bmp->pix directly.
  93. void tigrPlot(Tigr *bmp, int x, int y, TPixel pix);
  94. // Clears a bitmap to a color.
  95. // No blending, no clipping.
  96. void tigrClear(Tigr *bmp, TPixel color);
  97. // Fills a rectangular area.
  98. // No blending, no clipping.
  99. void tigrFill(Tigr *bmp, int x, int y, int w, int h, TPixel color);
  100. // Draws a line.
  101. // Start pixel is drawn, end pixel is not.
  102. // Clips and blends.
  103. void tigrLine(Tigr *bmp, int x0, int y0, int x1, int y1, TPixel color);
  104. // Draws an empty rectangle.
  105. // Drawing a 1x1 rectangle yields the same result as calling tigrPlot.
  106. // Clips and blends.
  107. void tigrRect(Tigr *bmp, int x, int y, int w, int h, TPixel color);
  108. // Fills a rectangle.
  109. // Fills the inside of the specified rectangular area.
  110. // Calling tigrRect followed by tigrFillRect using the same arguments
  111. // causes no overdrawing.
  112. // Clips and blends.
  113. void tigrFillRect(Tigr *bmp, int x, int y, int w, int h, TPixel color);
  114. // Draws a circle.
  115. // Drawing a zero radius circle yields the same result as calling tigrPlot.
  116. // Drawing a circle with radius one draws a circle three pixels wide.
  117. // Clips and blends.
  118. void tigrCircle(Tigr *bmp, int x, int y, int r, TPixel color);
  119. // Fills a circle.
  120. // Fills the inside of the specified circle.
  121. // Calling tigrCircle followed by tigrFillCircle using the same arguments
  122. // causes no overdrawing.
  123. // Filling a circle with zero radius has no effect.
  124. // Clips and blends.
  125. void tigrFillCircle(Tigr *bmp, int x, int y, int r, TPixel color);
  126. // Sets clip rect.
  127. // Set to (0, 0, -1, -1) to reset clipping to full bitmap.
  128. void tigrClip(Tigr *bmp, int cx, int cy, int cw, int ch);
  129. // Copies bitmap data.
  130. // dx/dy = dest co-ordinates
  131. // sx/sy = source co-ordinates
  132. // w/h = width/height
  133. //
  134. // RGBAdest = RGBAsrc
  135. // Clips, does not blend.
  136. void tigrBlit(Tigr *dest, Tigr *src, int dx, int dy, int sx, int sy, int w, int h);
  137. // Same as tigrBlit, but alpha blends the source bitmap with the
  138. // target using per pixel alpha and the specified global alpha.
  139. //
  140. // Ablend = Asrc * alpha
  141. // RGBdest = RGBsrc * Ablend + RGBdest * (1 - Ablend)
  142. //
  143. // Blit mode == TIGR_KEEP_ALPHA:
  144. // Adest = Adest
  145. //
  146. // Blit mode == TIGR_BLEND_ALPHA:
  147. // Adest = Asrc * Ablend + Adest * (1 - Ablend)
  148. // Clips and blends.
  149. void tigrBlitAlpha(Tigr *dest, Tigr *src, int dx, int dy, int sx, int sy, int w, int h, float alpha);
  150. // Same as tigrBlit, but tints the source bitmap with a color
  151. // and alpha blends the resulting source with the destination.
  152. //
  153. // Rblend = Rsrc * Rtint
  154. // Gblend = Gsrc * Gtint
  155. // Bblend = Bsrc * Btint
  156. // Ablend = Asrc * Atint
  157. //
  158. // RGBdest = RGBblend * Ablend + RGBdest * (1 - Ablend)
  159. //
  160. // Blit mode == TIGR_KEEP_ALPHA:
  161. // Adest = Adest
  162. //
  163. // Blit mode == TIGR_BLEND_ALPHA:
  164. // Adest = Ablend * Ablend + Adest * (1 - Ablend)
  165. // Clips and blends.
  166. void tigrBlitTint(Tigr *dest, Tigr *src, int dx, int dy, int sx, int sy, int w, int h, TPixel tint);
  167. enum TIGRBlitMode {
  168. TIGR_KEEP_ALPHA = 0, // Keep destination alpha value
  169. TIGR_BLEND_ALPHA = 1, // Blend destination alpha (default)
  170. };
  171. // Set destination bitmap blend mode for blit operations.
  172. void tigrBlitMode(Tigr *dest, int mode);
  173. // Helper for making colors.
  174. TIGR_INLINE TPixel tigrRGB(unsigned char r, unsigned char g, unsigned char b)
  175. {
  176. TPixel p; p.r = r; p.g = g; p.b = b; p.a = 0xff; return p;
  177. }
  178. // Helper for making colors.
  179. TIGR_INLINE TPixel tigrRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
  180. {
  181. TPixel p; p.r = r; p.g = g; p.b = b; p.a = a; return p;
  182. }
  183. // Font printing ----------------------------------------------------------
  184. typedef struct {
  185. int code, x, y, w, h;
  186. } TigrGlyph;
  187. typedef struct {
  188. Tigr *bitmap;
  189. int numGlyphs;
  190. TigrGlyph *glyphs;
  191. } TigrFont;
  192. typedef enum {
  193. TCP_ASCII = 0,
  194. TCP_1252 = 1252,
  195. TCP_UTF32 = 12001
  196. } TCodepage;
  197. // Loads a font from a bitmap font sheet.
  198. // The loaded font takes ownership of the provided bitmap.
  199. //
  200. // Codepages:
  201. //
  202. // TCP_ASCII - Regular 7-bit ASCII
  203. // TCP_1252 - Windows 1252
  204. // TCP_UTF32 - Unicode subset
  205. //
  206. // For ASCII and 1252, the font bitmap should contain all characters
  207. // for the given codepage, excluding the first 32 control codes.
  208. //
  209. // For UTF32 - the font bitmap contains a subset of Unicode characters
  210. // and must be in the format generated by tigrFont for UTF32.
  211. //
  212. TigrFont *tigrLoadFont(Tigr *bitmap, int codepage);
  213. // Frees a font and associated font sheet.
  214. void tigrFreeFont(TigrFont *font);
  215. // Prints UTF-8 text onto a bitmap.
  216. // NOTE:
  217. // This uses the target bitmap blit mode.
  218. // See tigrBlitTint for details.
  219. void tigrPrint(Tigr *dest, TigrFont *font, int x, int y, TPixel color, const char *text, ...);
  220. // Returns the width/height of a string.
  221. int tigrTextWidth(TigrFont *font, const char *text);
  222. int tigrTextHeight(TigrFont *font, const char *text);
  223. // The built-in font.
  224. extern TigrFont *tfont;
  225. // User Input -------------------------------------------------------------
  226. // Key scancodes. For letters/numbers, use ASCII ('A'-'Z' and '0'-'9').
  227. typedef enum {
  228. TK_PAD0=128,TK_PAD1,TK_PAD2,TK_PAD3,TK_PAD4,TK_PAD5,TK_PAD6,TK_PAD7,TK_PAD8,TK_PAD9,
  229. TK_PADMUL,TK_PADADD,TK_PADENTER,TK_PADSUB,TK_PADDOT,TK_PADDIV,
  230. TK_F1,TK_F2,TK_F3,TK_F4,TK_F5,TK_F6,TK_F7,TK_F8,TK_F9,TK_F10,TK_F11,TK_F12,
  231. TK_BACKSPACE,TK_TAB,TK_RETURN,TK_SHIFT,TK_CONTROL,TK_ALT,TK_PAUSE,TK_CAPSLOCK,
  232. TK_ESCAPE,TK_SPACE,TK_PAGEUP,TK_PAGEDN,TK_END,TK_HOME,TK_LEFT,TK_UP,TK_RIGHT,TK_DOWN,
  233. TK_INSERT,TK_DELETE,TK_LWIN,TK_RWIN,TK_NUMLOCK,TK_SCROLL,TK_LSHIFT,TK_RSHIFT,
  234. TK_LCONTROL,TK_RCONTROL,TK_LALT,TK_RALT,TK_SEMICOLON,TK_EQUALS,TK_COMMA,TK_MINUS,
  235. TK_DOT,TK_SLASH,TK_BACKTICK,TK_LSQUARE,TK_BACKSLASH,TK_RSQUARE,TK_TICK
  236. } TKey;
  237. // Returns mouse input for a window.
  238. // The value set to "buttons" is a bit set where bits 0, 1 and 2
  239. // corresponds to the left, right and middle buttons.
  240. // A set bit indicates that a button is held.
  241. void tigrMouse(Tigr *bmp, int *x, int *y, int *buttons);
  242. typedef struct {
  243. int x;
  244. int y;
  245. } TigrTouchPoint;
  246. // Reads touch input for a window.
  247. // Returns number of touch points read.
  248. int tigrTouch(Tigr *bmp, TigrTouchPoint* points, int maxPoints);
  249. // Reads the delta of the scroll "wheel" in somewhat platform neutral
  250. // units where 1.0 corresponds to a "notch". The actual correlation between
  251. // physical movement and this number varies between platforms, input methods
  252. // and settings.
  253. void tigrScrollWheel(Tigr* bmp, float* x, float *y);
  254. // Reads the keyboard for a window.
  255. // Returns non-zero if a key is pressed/held.
  256. // tigrKeyDown tests for the initial press, tigrKeyHeld repeats each frame.
  257. int tigrKeyDown(Tigr *bmp, int key);
  258. int tigrKeyHeld(Tigr *bmp, int key);
  259. // Reads character input for a window.
  260. // Returns the Unicode value of the last key pressed, or 0 if none.
  261. int tigrReadChar(Tigr *bmp);
  262. // Show / hide virtual keyboard.
  263. // (Only available on iOS / Android)
  264. void tigrShowKeyboard(int show);
  265. // Bitmap I/O -------------------------------------------------------------
  266. // Loads a PNG, from either a file or memory. (fileName is UTF-8)
  267. // On error, returns NULL and sets errno.
  268. Tigr *tigrLoadImage(const char *fileName);
  269. Tigr *tigrLoadImageMem(const void *data, int length);
  270. // Saves a PNG to a file. (fileName is UTF-8)
  271. // On error, returns zero and sets errno.
  272. int tigrSaveImage(const char *fileName, Tigr *bmp);
  273. // Helpers ----------------------------------------------------------------
  274. // Returns the amount of time elapsed since tigrTime was last called,
  275. // or zero on the first call.
  276. float tigrTime(void);
  277. // Displays an error message and quits. (UTF-8)
  278. // 'bmp' can be NULL.
  279. void tigrError(Tigr *bmp, const char *message, ...);
  280. // Reads an entire file into memory. (fileName is UTF-8)
  281. // Free it yourself after with 'free'.
  282. // On error, returns NULL and sets errno.
  283. // TIGR will automatically append a NUL terminator byte
  284. // to the end (not included in the length)
  285. void *tigrReadFile(const char *fileName, int *length);
  286. // Decompresses DEFLATEd zip/zlib data into a buffer.
  287. // Returns non-zero on success.
  288. int tigrInflate(void *out, unsigned outlen, const void *in, unsigned inlen);
  289. // Decodes a single UTF8 codepoint and returns the next pointer.
  290. const char *tigrDecodeUTF8(const char *text, int *cp);
  291. // Encodes a single UTF8 codepoint and returns the next pointer.
  292. char *tigrEncodeUTF8(char *text, int cp);
  293. #ifdef __cplusplus
  294. }
  295. #endif