Graphics.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /**
  2. * Copyright (c) 2006-2015 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #ifndef LOVE_GRAPHICS_GRAPHICS_H
  21. #define LOVE_GRAPHICS_GRAPHICS_H
  22. // LOVE
  23. #include "common/Module.h"
  24. #include "common/StringMap.h"
  25. #include "Color.h"
  26. // C++
  27. #include <string>
  28. namespace love
  29. {
  30. namespace graphics
  31. {
  32. /**
  33. * Globally sets whether gamma correction is enabled. Ideally this should be set
  34. * prior to using any Graphics module function.
  35. **/
  36. void setGammaCorrect(bool gammacorrect);
  37. /**
  38. * Gets whether global gamma correction is enabled.
  39. **/
  40. bool isGammaCorrect();
  41. /**
  42. * Gamma-corrects a color (converts it from sRGB to linear RGB, if
  43. * gamma correction is enabled.)
  44. * The color's components are expected to be in the range of [0, 1].
  45. **/
  46. void gammaCorrectColor(Colorf &c);
  47. /**
  48. * Un-gamma-corrects a color (converts it from linear RGB to sRGB, if
  49. * gamma correction is enabled.)
  50. * The color's components are expected to be in the range of [0, 1].
  51. **/
  52. void unGammaCorrectColor(Colorf &c);
  53. class Graphics : public Module
  54. {
  55. public:
  56. enum DrawMode
  57. {
  58. DRAW_LINE,
  59. DRAW_FILL,
  60. DRAW_MAX_ENUM
  61. };
  62. enum ArcMode
  63. {
  64. ARC_OPEN,
  65. ARC_CLOSED,
  66. ARC_PIE,
  67. ARC_MAX_ENUM
  68. };
  69. enum BlendMode
  70. {
  71. BLEND_ALPHA,
  72. BLEND_ADD,
  73. BLEND_SUBTRACT,
  74. BLEND_MULTIPLY,
  75. BLEND_LIGHTEN,
  76. BLEND_DARKEN,
  77. BLEND_SCREEN,
  78. BLEND_REPLACE,
  79. BLEND_MAX_ENUM
  80. };
  81. enum BlendAlpha
  82. {
  83. BLENDALPHA_MULTIPLY,
  84. BLENDALPHA_PREMULTIPLIED,
  85. BLENDALPHA_MAX_ENUM
  86. };
  87. enum LineStyle
  88. {
  89. LINE_ROUGH,
  90. LINE_SMOOTH,
  91. LINE_MAX_ENUM
  92. };
  93. enum LineJoin
  94. {
  95. LINE_JOIN_NONE,
  96. LINE_JOIN_MITER,
  97. LINE_JOIN_BEVEL,
  98. LINE_JOIN_MAX_ENUM
  99. };
  100. enum StencilAction
  101. {
  102. STENCIL_REPLACE,
  103. STENCIL_INCREMENT,
  104. STENCIL_DECREMENT,
  105. STENCIL_INCREMENT_WRAP,
  106. STENCIL_DECREMENT_WRAP,
  107. STENCIL_INVERT,
  108. STENCIL_MAX_ENUM
  109. };
  110. enum CompareMode
  111. {
  112. COMPARE_LESS,
  113. COMPARE_LEQUAL,
  114. COMPARE_EQUAL,
  115. COMPARE_GEQUAL,
  116. COMPARE_GREATER,
  117. COMPARE_NOTEQUAL,
  118. COMPARE_ALWAYS,
  119. COMPARE_MAX_ENUM
  120. };
  121. enum Support
  122. {
  123. SUPPORT_MULTI_CANVAS_FORMATS,
  124. SUPPORT_CLAMP_ZERO,
  125. SUPPORT_LIGHTEN,
  126. SUPPORT_MAX_ENUM
  127. };
  128. enum Renderer
  129. {
  130. RENDERER_OPENGL = 0,
  131. RENDERER_OPENGLES,
  132. RENDERER_MAX_ENUM
  133. };
  134. enum SystemLimit
  135. {
  136. LIMIT_POINT_SIZE,
  137. LIMIT_TEXTURE_SIZE,
  138. LIMIT_MULTI_CANVAS,
  139. LIMIT_CANVAS_MSAA,
  140. LIMIT_MAX_ENUM
  141. };
  142. enum StackType
  143. {
  144. STACK_ALL,
  145. STACK_TRANSFORM,
  146. STACK_MAX_ENUM
  147. };
  148. enum StatType
  149. {
  150. STAT_DRAW_CALLS,
  151. STAT_CANVAS_SWITCHES,
  152. STAT_CANVASES,
  153. STAT_IMAGES,
  154. STAT_FONTS,
  155. STAT_TEXTURE_MEMORY,
  156. STAT_MAX_ENUM
  157. };
  158. struct RendererInfo
  159. {
  160. std::string name;
  161. std::string version;
  162. std::string vendor;
  163. std::string device;
  164. };
  165. struct Stats
  166. {
  167. int drawCalls;
  168. int canvasSwitches;
  169. int canvases;
  170. int images;
  171. int fonts;
  172. size_t textureMemory;
  173. };
  174. struct ColorMask
  175. {
  176. bool r, g, b, a;
  177. ColorMask()
  178. : r(true), g(true), b(true), a(true)
  179. {}
  180. ColorMask(bool _r, bool _g, bool _b, bool _a)
  181. : r(_r), g(_g), b(_b), a(_a)
  182. {}
  183. bool operator == (const ColorMask &m) const
  184. {
  185. return r == m.r && g == m.g && b == m.b && a == m.a;
  186. }
  187. bool operator != (const ColorMask &m) const
  188. {
  189. return !(operator == (m));
  190. }
  191. };
  192. struct ScissorRect
  193. {
  194. int x, y;
  195. int w, h;
  196. bool operator == (const ScissorRect &rhs) const
  197. {
  198. return x == rhs.x && y == rhs.y && w == rhs.w && h == rhs.h;
  199. }
  200. };
  201. virtual ~Graphics();
  202. // Implements Module.
  203. virtual ModuleType getModuleType() const { return M_GRAPHICS; }
  204. /**
  205. * Sets the current graphics display viewport dimensions.
  206. **/
  207. virtual void setViewportSize(int width, int height) = 0;
  208. /**
  209. * Sets the current graphics display viewport and initializes the renderer.
  210. * @param width The viewport width.
  211. * @param height The viewport height.
  212. **/
  213. virtual bool setMode(int width, int height) = 0;
  214. /**
  215. * Un-sets the current graphics display mode (uninitializing objects if
  216. * necessary.)
  217. **/
  218. virtual void unSetMode() = 0;
  219. /**
  220. * Sets whether the module is active (internal use only.)
  221. **/
  222. virtual void setActive(bool active) = 0;
  223. /**
  224. * Gets whether the module is active. Graphics module methods are only
  225. * guaranteed to work when it is active. Calling them otherwise may cause
  226. * the program to crash (or worse.)
  227. * Normally the module will always be active as long as a window exists, it
  228. * may be different on some platforms (especially mobile ones.)
  229. **/
  230. virtual bool isActive() const = 0;
  231. static bool getConstant(const char *in, DrawMode &out);
  232. static bool getConstant(DrawMode in, const char *&out);
  233. static bool getConstant(const char *in, ArcMode &out);
  234. static bool getConstant(ArcMode in, const char *&out);
  235. static bool getConstant(const char *in, BlendMode &out);
  236. static bool getConstant(BlendMode in, const char *&out);
  237. static bool getConstant(const char *in, BlendAlpha &out);
  238. static bool getConstant(BlendAlpha in, const char *&out);
  239. static bool getConstant(const char *in, LineStyle &out);
  240. static bool getConstant(LineStyle in, const char *&out);
  241. static bool getConstant(const char *in, LineJoin &out);
  242. static bool getConstant(LineJoin in, const char *&out);
  243. static bool getConstant(const char *in, StencilAction &out);
  244. static bool getConstant(StencilAction in, const char *&out);
  245. static bool getConstant(const char *in, CompareMode &out);
  246. static bool getConstant(CompareMode in, const char *&out);
  247. static bool getConstant(const char *in, Support &out);
  248. static bool getConstant(Support in, const char *&out);
  249. static bool getConstant(const char *in, SystemLimit &out);
  250. static bool getConstant(SystemLimit in, const char *&out);
  251. static bool getConstant(const char *in, StackType &out);
  252. static bool getConstant(StackType in, const char *&out);
  253. static bool getConstant(const char *in, StatType &out);
  254. static bool getConstant(StatType in, const char *&out);
  255. private:
  256. static StringMap<DrawMode, DRAW_MAX_ENUM>::Entry drawModeEntries[];
  257. static StringMap<DrawMode, DRAW_MAX_ENUM> drawModes;
  258. static StringMap<ArcMode, ARC_MAX_ENUM>::Entry arcModeEntries[];
  259. static StringMap<ArcMode, ARC_MAX_ENUM> arcModes;
  260. static StringMap<BlendMode, BLEND_MAX_ENUM>::Entry blendModeEntries[];
  261. static StringMap<BlendMode, BLEND_MAX_ENUM> blendModes;
  262. static StringMap<BlendAlpha, BLENDALPHA_MAX_ENUM>::Entry blendAlphaEntries[];
  263. static StringMap<BlendAlpha, BLENDALPHA_MAX_ENUM> blendAlphaModes;
  264. static StringMap<LineStyle, LINE_MAX_ENUM>::Entry lineStyleEntries[];
  265. static StringMap<LineStyle, LINE_MAX_ENUM> lineStyles;
  266. static StringMap<LineJoin, LINE_JOIN_MAX_ENUM>::Entry lineJoinEntries[];
  267. static StringMap<LineJoin, LINE_JOIN_MAX_ENUM> lineJoins;
  268. static StringMap<StencilAction, STENCIL_MAX_ENUM>::Entry stencilActionEntries[];
  269. static StringMap<StencilAction, STENCIL_MAX_ENUM> stencilActions;
  270. static StringMap<CompareMode, COMPARE_MAX_ENUM>::Entry compareModeEntries[];
  271. static StringMap<CompareMode, COMPARE_MAX_ENUM> compareModes;
  272. static StringMap<Support, SUPPORT_MAX_ENUM>::Entry supportEntries[];
  273. static StringMap<Support, SUPPORT_MAX_ENUM> support;
  274. static StringMap<SystemLimit, LIMIT_MAX_ENUM>::Entry systemLimitEntries[];
  275. static StringMap<SystemLimit, LIMIT_MAX_ENUM> systemLimits;
  276. static StringMap<StackType, STACK_MAX_ENUM>::Entry stackTypeEntries[];
  277. static StringMap<StackType, STACK_MAX_ENUM> stackTypes;
  278. static StringMap<StatType, STAT_MAX_ENUM>::Entry statTypeEntries[];
  279. static StringMap<StatType, STAT_MAX_ENUM> statTypes;
  280. }; // Graphics
  281. } // graphics
  282. } // love
  283. #endif // LOVE_GRAPHICS_GRAPHICS_H