Graphics.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /**
  2. * Copyright (c) 2006-2016 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_OPENGL_GRAPHICS_H
  21. #define LOVE_GRAPHICS_OPENGL_GRAPHICS_H
  22. // STD
  23. #include <stack>
  24. #include <vector>
  25. #include <unordered_map>
  26. // OpenGL
  27. #include "OpenGL.h"
  28. // LOVE
  29. #include "graphics/Graphics.h"
  30. #include "graphics/Color.h"
  31. #include "image/Image.h"
  32. #include "image/ImageData.h"
  33. #include "video/VideoStream.h"
  34. #include "graphics/Font.h"
  35. #include "Image.h"
  36. #include "graphics/Quad.h"
  37. #include "graphics/Texture.h"
  38. #include "SpriteBatch.h"
  39. #include "ParticleSystem.h"
  40. #include "Canvas.h"
  41. #include "Shader.h"
  42. #include "Mesh.h"
  43. #include "Text.h"
  44. #include "Video.h"
  45. namespace love
  46. {
  47. class Reference;
  48. namespace graphics
  49. {
  50. namespace opengl
  51. {
  52. class Graphics final : public love::graphics::Graphics
  53. {
  54. public:
  55. typedef void (*ScreenshotCallback)(love::image::ImageData *i, Reference *ref, void *ud);
  56. struct ScreenshotInfo
  57. {
  58. ScreenshotCallback callback;
  59. Reference *ref;
  60. };
  61. Graphics();
  62. virtual ~Graphics();
  63. // Implements Module.
  64. const char *getName() const override;
  65. void setViewportSize(int width, int height, int pixelwidth, int pixelheight) override;
  66. bool setMode(int width, int height, int pixelwidth, int pixelheight) override;
  67. void unSetMode() override;
  68. void setActive(bool active) override;
  69. bool isActive() const override;
  70. void flushStreamDraws() override;
  71. /**
  72. * Resets the current color, background color,
  73. * line style, and so forth. (This will be called
  74. * when the game reloads.
  75. **/
  76. void reset();
  77. void clear(Colorf color);
  78. void clear(const std::vector<OptionalColorf> &colors);
  79. void discard(const std::vector<bool> &colorbuffers, bool depthstencil);
  80. bool isCanvasActive() const override;
  81. bool isCanvasActive(Canvas *canvas) const;
  82. /**
  83. * Flips buffers. (Rendered geometry is presented on screen).
  84. **/
  85. void present(void *screenshotCallbackData);
  86. int getWidth() const override;
  87. int getHeight() const override;
  88. int getPixelWidth() const;
  89. int getPixelHeight() const;
  90. double getCurrentPixelDensity() const;
  91. double getScreenPixelDensity() const;
  92. /**
  93. * True if a graphics viewport is set.
  94. **/
  95. bool isCreated() const;
  96. void setCanvas(Canvas *canvas);
  97. void setCanvas(const std::vector<Canvas *> &canvases);
  98. void setCanvas(const std::vector<StrongRef<Canvas>> &canvases);
  99. void setCanvas();
  100. std::vector<Canvas *> getCanvas() const;
  101. /**
  102. * Scissor defines a box such that everything outside that box is discarded and not drawn.
  103. * Scissoring is automatically enabled.
  104. * @param rect The rectangle defining the scissor area.
  105. **/
  106. void setScissor(const Rect &rect);
  107. void intersectScissor(const Rect &rect);
  108. /**
  109. * Clears any scissor that has been created.
  110. **/
  111. void setScissor();
  112. /**
  113. * Gets the current scissor box.
  114. * @return Whether the scissor is enabled.
  115. */
  116. bool getScissor(Rect &rect) const;
  117. /**
  118. * Enables or disables drawing to the stencil buffer. When enabled, the
  119. * color buffer is disabled.
  120. **/
  121. void drawToStencilBuffer(StencilAction action, int value);
  122. void stopDrawToStencilBuffer();
  123. /**
  124. * Sets whether stencil testing is enabled.
  125. **/
  126. void setStencilTest(CompareMode compare, int value);
  127. void setStencilTest();
  128. void getStencilTest(CompareMode &compare, int &value);
  129. /**
  130. * Clear the stencil buffer in the active Canvas(es.)
  131. **/
  132. void clearStencil();
  133. /**
  134. * Creates an Image object with padding and/or optimization.
  135. **/
  136. Image *newImage(const std::vector<love::image::ImageData *> &data, const Image::Settings &settings);
  137. Image *newImage(const std::vector<love::image::CompressedImageData *> &cdata, const Image::Settings &settings);
  138. Quad *newQuad(Quad::Viewport v, double sw, double sh);
  139. /**
  140. * Creates a Font object.
  141. **/
  142. graphics::Font *newFont(love::font::Rasterizer *data, const Texture::Filter &filter = Texture::getDefaultFilter());
  143. SpriteBatch *newSpriteBatch(Texture *texture, int size, Mesh::Usage usage);
  144. ParticleSystem *newParticleSystem(Texture *texture, int size);
  145. Canvas *newCanvas(int width, int height, const Canvas::Settings &settings);
  146. Shader *newShader(const Shader::ShaderSource &source);
  147. Mesh *newMesh(const std::vector<Vertex> &vertices, Mesh::DrawMode drawmode, Mesh::Usage usage);
  148. Mesh *newMesh(int vertexcount, Mesh::DrawMode drawmode, Mesh::Usage usage);
  149. Mesh *newMesh(const std::vector<Mesh::AttribFormat> &vertexformat, int vertexcount, Mesh::DrawMode drawmode, Mesh::Usage usage);
  150. Mesh *newMesh(const std::vector<Mesh::AttribFormat> &vertexformat, const void *data, size_t datasize, Mesh::DrawMode drawmode, Mesh::Usage usage);
  151. Text *newText(graphics::Font *font, const std::vector<Font::ColoredString> &text = {});
  152. Video *newVideo(love::video::VideoStream *stream, float pixeldensity);
  153. bool isGammaCorrect() const;
  154. /**
  155. * Sets the current constant color.
  156. **/
  157. void setColor(Colorf c);
  158. /**
  159. * Gets current color.
  160. **/
  161. Colorf getColor() const override;
  162. /**
  163. * Sets the background Color.
  164. **/
  165. void setBackgroundColor(Colorf c);
  166. /**
  167. * Gets the current background color.
  168. **/
  169. Colorf getBackgroundColor() const;
  170. void setFont(love::graphics::Font *font);
  171. love::graphics::Font *getFont();
  172. void setShader(Shader *shader);
  173. void setShader();
  174. Shader *getShader() const;
  175. /**
  176. * Sets the enabled color components when rendering.
  177. **/
  178. void setColorMask(ColorMask mask);
  179. /**
  180. * Gets the current color mask.
  181. **/
  182. ColorMask getColorMask() const;
  183. /**
  184. * Sets the current blend mode.
  185. **/
  186. void setBlendMode(BlendMode mode, BlendAlpha alphamode);
  187. /**
  188. * Gets the current blend mode.
  189. **/
  190. BlendMode getBlendMode(BlendAlpha &alphamode) const;
  191. /**
  192. * Sets the default filter for images, canvases, and fonts.
  193. **/
  194. void setDefaultFilter(const Texture::Filter &f);
  195. /**
  196. * Gets the default filter for images, canvases, and fonts.
  197. **/
  198. const Texture::Filter &getDefaultFilter() const;
  199. /**
  200. * Default Image mipmap filter mode and sharpness values.
  201. **/
  202. void setDefaultMipmapFilter(Texture::FilterMode filter, float sharpness);
  203. void getDefaultMipmapFilter(Texture::FilterMode *filter, float *sharpness) const;
  204. /**
  205. * Sets the line width.
  206. * @param width The new width of the line.
  207. **/
  208. void setLineWidth(float width);
  209. /**
  210. * Sets the line style.
  211. * @param style LINE_ROUGH or LINE_SMOOTH.
  212. **/
  213. void setLineStyle(LineStyle style);
  214. /**
  215. * Sets the line join mode.
  216. **/
  217. void setLineJoin(LineJoin style);
  218. float getLineWidth() const override;
  219. LineStyle getLineStyle() const override;
  220. LineJoin getLineJoin() const override;
  221. /**
  222. * Sets the size of points.
  223. **/
  224. void setPointSize(float size);
  225. /**
  226. * Gets the point size.
  227. **/
  228. float getPointSize() const;
  229. /**
  230. * Sets whether graphics will be drawn as wireframe lines instead of filled
  231. * triangles (has no effect for drawn points.)
  232. * This should only be used as a debugging tool. The wireframe lines do not
  233. * behave the same as regular love.graphics lines.
  234. **/
  235. void setWireframe(bool enable);
  236. /**
  237. * Gets whether wireframe drawing mode is enabled.
  238. **/
  239. bool isWireframe() const;
  240. /**
  241. * Draws text at the specified coordinates
  242. **/
  243. void print(const std::vector<Font::ColoredString> &str, const Matrix4 &m);
  244. /**
  245. * Draws formatted text on screen at the specified coordinates.
  246. **/
  247. void printf(const std::vector<Font::ColoredString> &str, float wrap, Font::AlignMode align, const Matrix4 &m);
  248. void captureScreenshot(const ScreenshotInfo &info);
  249. /**
  250. * Returns system-dependent renderer information.
  251. * Returned strings can vary greatly between systems! Do not rely on it for
  252. * anything!
  253. **/
  254. RendererInfo getRendererInfo() const;
  255. /**
  256. * Returns performance-related statistics.
  257. **/
  258. Stats getStats() const;
  259. double getSystemLimit(SystemLimit limittype) const override;
  260. /**
  261. * Gets whether a graphics feature is supported on this system.
  262. **/
  263. bool isSupported(Feature feature) const;
  264. void push(StackType type = STACK_TRANSFORM);
  265. void pop();
  266. private:
  267. struct DisplayState
  268. {
  269. Colorf color = Colorf(1.0, 1.0, 1.0, 1.0);
  270. Colorf gammaCorrectedColor = Colorf(1.0f, 1.0f, 1.0f, 1.0f);
  271. Colorf backgroundColor = Colorf(0.0, 0.0, 0.0, 1.0);
  272. BlendMode blendMode = BLEND_ALPHA;
  273. BlendAlpha blendAlphaMode = BLENDALPHA_MULTIPLY;
  274. float lineWidth = 1.0f;
  275. LineStyle lineStyle = LINE_SMOOTH;
  276. LineJoin lineJoin = LINE_JOIN_MITER;
  277. float pointSize = 1.0f;
  278. bool scissor = false;
  279. Rect scissorRect = Rect();
  280. // Stencil.
  281. CompareMode stencilCompare = COMPARE_ALWAYS;
  282. int stencilTestValue = 0;
  283. StrongRef<love::graphics::Font> font;
  284. StrongRef<Shader> shader;
  285. std::vector<StrongRef<Canvas>> canvases;
  286. ColorMask colorMask = ColorMask(true, true, true, true);
  287. bool wireframe = false;
  288. Texture::Filter defaultFilter = Texture::Filter();
  289. Texture::FilterMode defaultMipmapFilter = Texture::FILTER_NEAREST;
  290. float defaultMipmapSharpness = 0.0f;
  291. };
  292. struct CachedRenderbuffer
  293. {
  294. int w;
  295. int h;
  296. int samples;
  297. GLenum attachments[2];
  298. GLuint renderbuffer;
  299. };
  300. void restoreState(const DisplayState &s);
  301. void restoreStateChecked(const DisplayState &s);
  302. void endPass();
  303. void bindCachedFBO(const std::vector<Canvas *> &canvases);
  304. void discard(OpenGL::FramebufferTarget target, const std::vector<bool> &colorbuffers, bool depthstencil);
  305. GLuint attachCachedStencilBuffer(int w, int h, int samples);
  306. void setDebug(bool enable);
  307. void checkSetDefaultFont();
  308. StrongRef<love::graphics::Font> defaultFont;
  309. std::vector<ScreenshotInfo> pendingScreenshotCallbacks;
  310. std::unordered_map<uint32, GLuint> framebufferObjects;
  311. std::vector<CachedRenderbuffer> stencilBuffers;
  312. QuadIndices *quadIndices;
  313. int width;
  314. int height;
  315. int pixelWidth;
  316. int pixelHeight;
  317. bool created;
  318. bool active;
  319. bool writingToStencil;
  320. std::vector<DisplayState> states;
  321. std::vector<StackType> stackTypes; // Keeps track of the pushed stack types.
  322. int canvasSwitchCount;
  323. static const size_t MAX_USER_STACK_DEPTH = 64;
  324. }; // Graphics
  325. } // opengl
  326. } // graphics
  327. } // love
  328. #endif // LOVE_GRAPHICS_OPENGL_GRAPHICS_H