Graphics.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. // OpenGL
  26. #include "OpenGL.h"
  27. // LOVE
  28. #include "graphics/Graphics.h"
  29. #include "graphics/Color.h"
  30. #include "image/Image.h"
  31. #include "image/ImageData.h"
  32. #include "window/Window.h"
  33. #include "video/VideoStream.h"
  34. #include "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. namespace graphics
  48. {
  49. namespace opengl
  50. {
  51. class Graphics : public love::graphics::Graphics
  52. {
  53. public:
  54. struct OptionalColorf
  55. {
  56. float r, g, b, a;
  57. bool enabled;
  58. };
  59. Graphics();
  60. virtual ~Graphics();
  61. // Implements Module.
  62. const char *getName() const;
  63. virtual void setViewportSize(int width, int height);
  64. virtual bool setMode(int width, int height);
  65. virtual void unSetMode();
  66. virtual void setActive(bool active);
  67. virtual bool isActive() const;
  68. void setDebug(bool enable);
  69. /**
  70. * Resets the current color, background color,
  71. * line style, and so forth. (This will be called
  72. * when the game reloads.
  73. **/
  74. void reset();
  75. /**
  76. * Clears the screen to a specific color.
  77. **/
  78. void clear(Colorf c);
  79. /**
  80. * Clears each active canvas to a different color.
  81. **/
  82. void clear(const std::vector<OptionalColorf> &colors);
  83. /**
  84. * Discards the contents of the screen.
  85. **/
  86. void discard(const std::vector<bool> &colorbuffers, bool stencil);
  87. /**
  88. * Flips buffers. (Rendered geometry is presented on screen).
  89. **/
  90. void present();
  91. /**
  92. * Gets the width of the current graphics viewport.
  93. **/
  94. int getWidth() const;
  95. /**
  96. * Gets the height of the current graphics viewport.
  97. **/
  98. int getHeight() const;
  99. /**
  100. * True if a graphics viewport is set.
  101. **/
  102. bool isCreated() const;
  103. /**
  104. * Scissor defines a box such that everything outside that box is discarded and not drawn.
  105. * Scissoring is automatically enabled.
  106. * @param x The x-coordinate of the top-left corner.
  107. * @param y The y-coordinate of the top-left corner.
  108. * @param width The width of the box.
  109. * @param height The height of the box.
  110. **/
  111. void setScissor(int x, int y, int width, int height);
  112. void intersectScissor(int x, int y, int width, int height);
  113. /**
  114. * Clears any scissor that has been created.
  115. **/
  116. void setScissor();
  117. /**
  118. * Gets the current scissor box.
  119. * @return Whether the scissor is enabled.
  120. */
  121. bool getScissor(int &x, int &y, int &width, int &height) const;
  122. /**
  123. * Enables or disables drawing to the stencil buffer. When enabled, the
  124. * color buffer is disabled.
  125. **/
  126. void drawToStencilBuffer(StencilAction action, int value);
  127. void stopDrawToStencilBuffer();
  128. /**
  129. * Sets whether stencil testing is enabled.
  130. **/
  131. void setStencilTest(CompareMode compare, int value);
  132. void setStencilTest();
  133. void getStencilTest(CompareMode &compare, int &value);
  134. /**
  135. * Clear the stencil buffer in the active Canvas(es.)
  136. **/
  137. void clearStencil();
  138. /**
  139. * Creates an Image object with padding and/or optimization.
  140. **/
  141. Image *newImage(const std::vector<love::image::ImageData *> &data, const Image::Flags &flags);
  142. Image *newImage(const std::vector<love::image::CompressedImageData *> &cdata, const Image::Flags &flags);
  143. Quad *newQuad(Quad::Viewport v, double sw, double sh);
  144. /**
  145. * Creates a Font object.
  146. **/
  147. Font *newFont(love::font::Rasterizer *data, const Texture::Filter &filter = Texture::getDefaultFilter());
  148. SpriteBatch *newSpriteBatch(Texture *texture, int size, Mesh::Usage usage);
  149. ParticleSystem *newParticleSystem(Texture *texture, int size);
  150. Canvas *newCanvas(int width, int height, Canvas::Format format = Canvas::FORMAT_NORMAL, int msaa = 0);
  151. Shader *newShader(const Shader::ShaderSource &source);
  152. Mesh *newMesh(const std::vector<Vertex> &vertices, Mesh::DrawMode drawmode, Mesh::Usage usage);
  153. Mesh *newMesh(int vertexcount, Mesh::DrawMode drawmode, Mesh::Usage usage);
  154. Mesh *newMesh(const std::vector<Mesh::AttribFormat> &vertexformat, int vertexcount, Mesh::DrawMode drawmode, Mesh::Usage usage);
  155. Mesh *newMesh(const std::vector<Mesh::AttribFormat> &vertexformat, const void *data, size_t datasize, Mesh::DrawMode drawmode, Mesh::Usage usage);
  156. Text *newText(Font *font, const std::vector<Font::ColoredString> &text = {});
  157. Video *newVideo(love::video::VideoStream *stream);
  158. bool isGammaCorrect() const;
  159. /**
  160. * Sets the foreground color.
  161. * @param c The new foreground color.
  162. **/
  163. void setColor(Colorf c);
  164. /**
  165. * Gets current color.
  166. **/
  167. Colorf getColor() const;
  168. /**
  169. * Sets the background Color.
  170. **/
  171. void setBackgroundColor(Colorf c);
  172. /**
  173. * Gets the current background color.
  174. **/
  175. Colorf getBackgroundColor() const;
  176. void setFont(Font *font);
  177. Font *getFont();
  178. void setShader(Shader *shader);
  179. void setShader();
  180. Shader *getShader() const;
  181. void setCanvas(Canvas *canvas);
  182. void setCanvas(const std::vector<Canvas *> &canvases);
  183. void setCanvas(const std::vector<StrongRef<Canvas>> &canvases);
  184. void setCanvas();
  185. std::vector<Canvas *> getCanvas() const;
  186. /**
  187. * Sets the enabled color components when rendering.
  188. **/
  189. void setColorMask(ColorMask mask);
  190. /**
  191. * Gets the current color mask.
  192. **/
  193. ColorMask getColorMask() const;
  194. /**
  195. * Sets the current blend mode.
  196. **/
  197. void setBlendMode(BlendMode mode, BlendAlpha alphamode);
  198. /**
  199. * Gets the current blend mode.
  200. **/
  201. BlendMode getBlendMode(BlendAlpha &alphamode) const;
  202. /**
  203. * Sets the default filter for images, canvases, and fonts.
  204. **/
  205. void setDefaultFilter(const Texture::Filter &f);
  206. /**
  207. * Gets the default filter for images, canvases, and fonts.
  208. **/
  209. const Texture::Filter &getDefaultFilter() const;
  210. /**
  211. * Default Image mipmap filter mode and sharpness values.
  212. **/
  213. void setDefaultMipmapFilter(Texture::FilterMode filter, float sharpness);
  214. void getDefaultMipmapFilter(Texture::FilterMode *filter, float *sharpness) const;
  215. /**
  216. * Sets the line width.
  217. * @param width The new width of the line.
  218. **/
  219. void setLineWidth(float width);
  220. /**
  221. * Sets the line style.
  222. * @param style LINE_ROUGH or LINE_SMOOTH.
  223. **/
  224. void setLineStyle(LineStyle style);
  225. /**
  226. * Sets the line style.
  227. * @param style LINE_ROUGH or LINE_SMOOTH.
  228. **/
  229. void setLineJoin(LineJoin style);
  230. /**
  231. * Gets the line width.
  232. **/
  233. float getLineWidth() const;
  234. /**
  235. * Gets the line style.
  236. **/
  237. LineStyle getLineStyle() const;
  238. /**
  239. * Gets the line style.
  240. **/
  241. LineJoin getLineJoin() const;
  242. /**
  243. * Sets the size of points.
  244. **/
  245. void setPointSize(float size);
  246. /**
  247. * Gets the point size.
  248. **/
  249. float getPointSize() const;
  250. /**
  251. * Sets whether graphics will be drawn as wireframe lines instead of filled
  252. * triangles (has no effect for drawn points.)
  253. * This should only be used as a debugging tool. The wireframe lines do not
  254. * behave the same as regular love.graphics lines.
  255. **/
  256. void setWireframe(bool enable);
  257. /**
  258. * Gets whether wireframe drawing mode is enabled.
  259. **/
  260. bool isWireframe() const;
  261. /**
  262. * Draws text at the specified coordinates, with rotation and
  263. * scaling along both axes.
  264. * @param x The x-coordinate.
  265. * @param y The y-coordinate.
  266. * @param angle The amount of rotation.
  267. * @param sx The scale factor along the x-axis. (1 = normal).
  268. * @param sy The scale factor along the y-axis. (1 = normal).
  269. * @param ox The origin offset along the x-axis.
  270. * @param oy The origin offset along the y-axis.
  271. * @param kx Shear along the x-axis.
  272. * @param ky Shear along the y-axis.
  273. **/
  274. void print(const std::vector<Font::ColoredString> &str, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky);
  275. /**
  276. * Draw formatted text on screen at the specified coordinates.
  277. *
  278. * @param str A string of text.
  279. * @param x The x-coordinate.
  280. * @param y The y-coordinate.
  281. * @param wrap The maximum width of the text area.
  282. * @param align Where to align the text.
  283. * @param angle The amount of rotation.
  284. * @param sx The scale factor along the x-axis. (1 = normal).
  285. * @param sy The scale factor along the y-axis. (1 = normal).
  286. * @param ox The origin offset along the x-axis.
  287. * @param oy The origin offset along the y-axis.
  288. * @param kx Shear along the x-axis.
  289. * @param ky Shear along the y-axis.
  290. **/
  291. void printf(const std::vector<Font::ColoredString> &str, float x, float y, float wrap, Font::AlignMode align, float angle, float sx, float sy, float ox, float oy, float kx, float ky);
  292. /**
  293. * Draws a point at (x,y).
  294. * @param x Point along x-axis.
  295. * @param y Point along y-axis.
  296. **/
  297. void points(const float *coords, const uint8 *colors, size_t numpoints);
  298. /**
  299. * Draws a series of lines connecting the given vertices.
  300. * @param coords Vertex components (x1, y1, ..., xn, yn). If x1,y1 == xn,yn the line will be drawn closed.
  301. * @param count Number of items in the array, i.e. count = 2 * n
  302. **/
  303. void polyline(const float *coords, size_t count);
  304. /**
  305. * Draws a rectangle.
  306. * @param x Position along x-axis for top-left corner.
  307. * @param y Position along y-axis for top-left corner.
  308. * @param w The width of the rectangle.
  309. * @param h The height of the rectangle.
  310. **/
  311. void rectangle(DrawMode mode, float x, float y, float w, float h);
  312. /**
  313. * Variant of rectangle that draws a rounded rectangle.
  314. * @param mode The mode of drawing (line/filled).
  315. * @param x X-coordinate of top-left corner
  316. * @param y Y-coordinate of top-left corner
  317. * @param w The width of the rectangle.
  318. * @param h The height of the rectangle.
  319. * @param rx The radius of the corners on the x axis
  320. * @param ry The radius of the corners on the y axis
  321. * @param points The number of points to use per corner
  322. **/
  323. void rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry, int points = 10);
  324. /**
  325. * Draws a circle using the specified arguments.
  326. * @param mode The mode of drawing (line/filled).
  327. * @param x X-coordinate.
  328. * @param y Y-coordinate.
  329. * @param radius Radius of the circle.
  330. * @param points Number of points to use to draw the circle.
  331. **/
  332. void circle(DrawMode mode, float x, float y, float radius, int points = 10);
  333. /**
  334. * Draws an ellipse using the specified arguments.
  335. * @param mode The mode of drawing (line/filled).
  336. * @param x X-coordinate of center
  337. * @param y Y-coordinate of center
  338. * @param a Radius in x-direction
  339. * @param b Radius in y-direction
  340. * @param points Number of points to use to draw the circle.
  341. **/
  342. void ellipse(DrawMode mode, float x, float y, float a, float b, int points = 10);
  343. /**
  344. * Draws an arc using the specified arguments.
  345. * @param drawmode The mode of drawing (line/filled).
  346. * @param arcmode The type of arc.
  347. * @param x X-coordinate.
  348. * @param y Y-coordinate.
  349. * @param radius Radius of the arc.
  350. * @param angle1 The angle at which the arc begins.
  351. * @param angle2 The angle at which the arc terminates.
  352. * @param points Number of points to use to draw the arc.
  353. **/
  354. void arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2, int points = 10);
  355. /**
  356. * Draws a polygon with an arbitrary number of vertices.
  357. * @param mode The type of drawing (line/filled).
  358. * @param coords Vertex components (x1, y1, x2, y2, etc.)
  359. * @param count Coord array size
  360. **/
  361. void polygon(DrawMode mode, const float *coords, size_t count);
  362. /**
  363. * Creates a screenshot of the view and saves it to the default folder.
  364. * @param image The love.image module.
  365. * @param copyAlpha If the alpha channel should be copied or set to full opacity (255).
  366. **/
  367. love::image::ImageData *newScreenshot(love::image::Image *image, bool copyAlpha = true);
  368. /**
  369. * Returns system-dependent renderer information.
  370. * Returned strings can vary greatly between systems! Do not rely on it for
  371. * anything!
  372. **/
  373. RendererInfo getRendererInfo() const;
  374. /**
  375. * Returns performance-related statistics.
  376. **/
  377. Stats getStats() const;
  378. /**
  379. * Gets the system-dependent numeric limit for the specified parameter.
  380. **/
  381. double getSystemLimit(SystemLimit limittype) const;
  382. /**
  383. * Gets whether a graphics feature is supported on this system.
  384. **/
  385. bool isSupported(Support feature) const;
  386. void push(StackType type = STACK_TRANSFORM);
  387. void pop();
  388. void rotate(float r);
  389. void scale(float x, float y = 1.0f);
  390. void translate(float x, float y);
  391. void shear(float kx, float ky);
  392. void origin();
  393. private:
  394. struct DisplayState
  395. {
  396. Colorf color = Colorf(255.0, 255.0, 255.0, 255.0);
  397. Colorf backgroundColor = Colorf(0.0, 0.0, 0.0, 255.0);
  398. BlendMode blendMode = BLEND_ALPHA;
  399. BlendAlpha blendAlphaMode = BLENDALPHA_MULTIPLY;
  400. float lineWidth = 1.0f;
  401. LineStyle lineStyle = LINE_SMOOTH;
  402. LineJoin lineJoin = LINE_JOIN_MITER;
  403. float pointSize = 1.0f;
  404. bool scissor = false;
  405. ScissorRect scissorRect = ScissorRect();
  406. // Stencil.
  407. CompareMode stencilCompare = COMPARE_ALWAYS;
  408. int stencilTestValue = 0;
  409. StrongRef<Font> font;
  410. StrongRef<Shader> shader;
  411. std::vector<StrongRef<Canvas>> canvases;
  412. ColorMask colorMask = ColorMask(true, true, true, true);
  413. bool wireframe = false;
  414. Texture::Filter defaultFilter = Texture::Filter();
  415. Texture::FilterMode defaultMipmapFilter = Texture::FILTER_NEAREST;
  416. float defaultMipmapSharpness = 0.0f;
  417. };
  418. void restoreState(const DisplayState &s);
  419. void restoreStateChecked(const DisplayState &s);
  420. void checkSetDefaultFont();
  421. StrongRef<love::window::Window> currentWindow;
  422. StrongRef<Font> defaultFont;
  423. std::vector<double> pixelSizeStack; // stores current size of a pixel (needed for line drawing)
  424. QuadIndices *quadIndices;
  425. int width;
  426. int height;
  427. bool created;
  428. bool active;
  429. bool writingToStencil;
  430. std::vector<DisplayState> states;
  431. std::vector<StackType> stackTypes; // Keeps track of the pushed stack types.
  432. static const size_t MAX_USER_STACK_DEPTH = 64;
  433. }; // Graphics
  434. } // opengl
  435. } // graphics
  436. } // love
  437. #endif // LOVE_GRAPHICS_OPENGL_GRAPHICS_H