Graphics.h 11 KB

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