Graphics.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /**
  2. * Copyright (c) 2006-2014 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 <iostream>
  24. #include <stack>
  25. #include <vector>
  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 "window/Window.h"
  34. #include "Font.h"
  35. #include "Image.h"
  36. #include "graphics/Quad.h"
  37. #include "SpriteBatch.h"
  38. #include "ParticleSystem.h"
  39. #include "Canvas.h"
  40. #include "Shader.h"
  41. #include "Mesh.h"
  42. namespace love
  43. {
  44. namespace graphics
  45. {
  46. namespace opengl
  47. {
  48. class Graphics : public love::graphics::Graphics
  49. {
  50. public:
  51. Graphics();
  52. virtual ~Graphics();
  53. // Implements Module.
  54. const char *getName() const;
  55. virtual void setViewportSize(int width, int height);
  56. virtual bool setMode(int width, int height, bool &sRGB);
  57. virtual void unSetMode();
  58. void setDebug(bool enable);
  59. /**
  60. * Resets the current color, background color,
  61. * line style, and so forth. (This will be called
  62. * when the game reloads.
  63. **/
  64. void reset();
  65. /**
  66. * Clears the screen.
  67. **/
  68. void clear();
  69. /**
  70. * Flips buffers. (Rendered geometry is presented on screen).
  71. **/
  72. void present();
  73. /**
  74. * Gets the width of the current graphics viewport.
  75. **/
  76. int getWidth() const;
  77. /**
  78. * Gets the height of the current graphics viewport.
  79. **/
  80. int getHeight() const;
  81. /**
  82. * True if a graphics viewport is set.
  83. **/
  84. bool isCreated() const;
  85. /**
  86. * Scissor defines a box such that everything outside that box is discarded and not drawn.
  87. * Scissoring is automatically enabled.
  88. * @param x The x-coordinate of the top-left corner.
  89. * @param y The y-coordinate of the top-left corner.
  90. * @param width The width of the box.
  91. * @param height The height of the box.
  92. **/
  93. void setScissor(int x, int y, int width, int height);
  94. /**
  95. * Clears any scissor that has been created.
  96. **/
  97. void setScissor();
  98. /**
  99. * Gets the current scissor box.
  100. * @return Whether the scissor is enabled.
  101. */
  102. bool getScissor(int &x, int &y, int &width, int &height) const;
  103. /**
  104. * Enables the stencil buffer and set stencil function to fill it
  105. */
  106. void defineStencil();
  107. /**
  108. * Set stencil function to mask the following drawing calls using
  109. * the current stencil buffer
  110. * @param invert Invert the mask, i.e. draw everywhere expect where
  111. * the mask is defined.
  112. */
  113. void useStencil(bool invert = false);
  114. /**
  115. * Disables the stencil buffer
  116. */
  117. void discardStencil();
  118. /**
  119. * Creates an Image object with padding and/or optimization.
  120. **/
  121. Image *newImage(love::image::ImageData *data, const Image::Flags &flags);
  122. Image *newImage(love::image::CompressedData *cdata, const Image::Flags &flags);
  123. Quad *newQuad(Quad::Viewport v, float sw, float sh);
  124. /**
  125. * Creates a Font object.
  126. **/
  127. Font *newFont(love::font::Rasterizer *data, const Texture::Filter &filter = Texture::getDefaultFilter());
  128. SpriteBatch *newSpriteBatch(Texture *texture, int size, int usage);
  129. ParticleSystem *newParticleSystem(Texture *texture, int size);
  130. Canvas *newCanvas(int width, int height, Canvas::Format format = Canvas::FORMAT_NORMAL, int msaa = 0);
  131. Shader *newShader(const Shader::ShaderSources &sources);
  132. Mesh *newMesh(const std::vector<Vertex> &vertices, Mesh::DrawMode mode = Mesh::DRAW_MODE_FAN);
  133. Mesh *newMesh(int vertexcount, Mesh::DrawMode mode = Mesh::DRAW_MODE_FAN);
  134. /**
  135. * Sets the foreground color.
  136. * @param c The new foreground color.
  137. **/
  138. void setColor(const Color &c);
  139. /**
  140. * Gets current color.
  141. **/
  142. Color getColor() const;
  143. /**
  144. * Sets the background Color.
  145. **/
  146. void setBackgroundColor(const Color &c);
  147. /**
  148. * Gets the current background color.
  149. **/
  150. Color getBackgroundColor() const;
  151. /**
  152. * Sets the current font.
  153. * @param font A Font object.
  154. **/
  155. void setFont(Font *font);
  156. /**
  157. * Gets the current Font, or nil if none.
  158. **/
  159. Font *getFont() const;
  160. void setShader(Shader *shader);
  161. void setShader();
  162. Shader *getShader() const;
  163. void setCanvas(Canvas *canvas);
  164. void setCanvas(const std::vector<Canvas *> &canvases);
  165. void setCanvas(const std::vector<Object::StrongRef<Canvas>> &canvases);
  166. void setCanvas();
  167. std::vector<Canvas *> getCanvas() const;
  168. /**
  169. * Sets the enabled color components when rendering.
  170. **/
  171. void setColorMask(const bool mask[4]);
  172. /**
  173. * Gets the current color mask.
  174. * Returns an array of 4 booleans representing the mask.
  175. **/
  176. const bool *getColorMask() const;
  177. /**
  178. * Sets the current blend mode.
  179. **/
  180. void setBlendMode(BlendMode mode);
  181. /**
  182. * Gets the current blend mode.
  183. **/
  184. BlendMode getBlendMode() const;
  185. /**
  186. * Sets the default filter for images, canvases, and fonts.
  187. **/
  188. void setDefaultFilter(const Texture::Filter &f);
  189. /**
  190. * Gets the default filter for images, canvases, and fonts.
  191. **/
  192. const Texture::Filter &getDefaultFilter() const;
  193. /**
  194. * Default Image mipmap filter mode and sharpness values.
  195. **/
  196. void setDefaultMipmapFilter(Texture::FilterMode filter, float sharpness);
  197. void getDefaultMipmapFilter(Texture::FilterMode *filter, float *sharpness) const;
  198. /**
  199. * Sets the line width.
  200. * @param width The new width of the line.
  201. **/
  202. void setLineWidth(float width);
  203. /**
  204. * Sets the line style.
  205. * @param style LINE_ROUGH or LINE_SMOOTH.
  206. **/
  207. void setLineStyle(LineStyle style);
  208. /**
  209. * Sets the line style.
  210. * @param style LINE_ROUGH or LINE_SMOOTH.
  211. **/
  212. void setLineJoin(LineJoin style);
  213. /**
  214. * Gets the line width.
  215. **/
  216. float getLineWidth() const;
  217. /**
  218. * Gets the line style.
  219. **/
  220. LineStyle getLineStyle() const;
  221. /**
  222. * Gets the line style.
  223. **/
  224. LineJoin getLineJoin() const;
  225. /**
  226. * Sets the size of points.
  227. **/
  228. void setPointSize(float size);
  229. /**
  230. * Gets the point size.
  231. **/
  232. float getPointSize() const;
  233. /**
  234. * Sets whether graphics will be drawn as wireframe lines instead of filled
  235. * triangles (has no effect for drawn points.)
  236. * This should only be used as a debugging tool. The wireframe lines do not
  237. * behave the same as regular love.graphics lines.
  238. **/
  239. void setWireframe(bool enable);
  240. /**
  241. * Gets whether wireframe drawing mode is enabled.
  242. **/
  243. bool isWireframe() const;
  244. /**
  245. * Draws text at the specified coordinates, with rotation and
  246. * scaling along both axes.
  247. * @param x The x-coordinate.
  248. * @param y The y-coordinate.
  249. * @param angle The amount of rotation.
  250. * @param sx The scale factor along the x-axis. (1 = normal).
  251. * @param sy The scale factor along the y-axis. (1 = normal).
  252. * @param ox The origin offset along the x-axis.
  253. * @param oy The origin offset along the y-axis.
  254. * @param kx Shear along the x-axis.
  255. * @param ky Shear along the y-axis.
  256. **/
  257. void print(const std::string &str, float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky);
  258. /**
  259. * Draw formatted text on screen at the specified coordinates.
  260. *
  261. * @param str A string of text.
  262. * @param x The x-coordinate.
  263. * @param y The y-coordinate.
  264. * @param wrap The maximum width of the text area.
  265. * @param align Where to align the text.
  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 printf(const std::string &str, float x, float y, float wrap, AlignMode align, float angle, float sx, float sy, float ox, float oy, float kx, float ky);
  275. /**
  276. * Draws a point at (x,y).
  277. * @param x Point along x-axis.
  278. * @param y Point along y-axis.
  279. **/
  280. void point(float x, float y);
  281. /**
  282. * Draws a series of lines connecting the given vertices.
  283. * @param coords Vertex components (x1, y1, ..., xn, yn). If x1,y1 == xn,yn the line will be drawn closed.
  284. * @param count Number of items in the array, i.e. count = 2 * n
  285. **/
  286. void polyline(const float *coords, size_t count);
  287. /**
  288. * Draws a rectangle.
  289. * @param x Position along x-axis for top-left corner.
  290. * @param y Position along y-axis for top-left corner.
  291. * @param w The width of the rectangle.
  292. * @param h The height of the rectangle.
  293. **/
  294. void rectangle(DrawMode mode, float x, float y, float w, float h);
  295. /**
  296. * Draws a circle using the specified arguments.
  297. * @param mode The mode of drawing (line/filled).
  298. * @param x X-coordinate.
  299. * @param y Y-coordinate.
  300. * @param radius Radius of the circle.
  301. * @param points Number of points to use to draw the circle.
  302. **/
  303. void circle(DrawMode mode, float x, float y, float radius, int points = 10);
  304. /**
  305. * Draws an arc using the specified arguments.
  306. * @param mode The mode of drawing (line/filled).
  307. * @param x X-coordinate.
  308. * @param y Y-coordinate.
  309. * @param radius Radius of the arc.
  310. * @param angle1 The angle at which the arc begins.
  311. * @param angle2 The angle at which the arc terminates.
  312. * @param points Number of points to use to draw the arc.
  313. **/
  314. void arc(DrawMode mode, float x, float y, float radius, float angle1, float angle2, int points = 10);
  315. /**
  316. * Draws a polygon with an arbitrary number of vertices.
  317. * @param mode The type of drawing (line/filled).
  318. * @param coords Vertex components (x1, y1, x2, y2, etc.)
  319. * @param count Coord array size
  320. **/
  321. void polygon(DrawMode mode, const float *coords, size_t count);
  322. /**
  323. * Creates a screenshot of the view and saves it to the default folder.
  324. * @param image The love.image module.
  325. * @param copyAlpha If the alpha channel should be copied or set to full opacity (255).
  326. **/
  327. love::image::ImageData *newScreenshot(love::image::Image *image, bool copyAlpha = true);
  328. /**
  329. * Returns system-dependent renderer information.
  330. * Returned string s can vary greatly between systems! Do not rely on it for
  331. * anything!
  332. **/
  333. RendererInfo getRendererInfo() const;
  334. /**
  335. * Gets the system-dependent numeric limit for the specified parameter.
  336. **/
  337. double getSystemLimit(SystemLimit limittype) const;
  338. /**
  339. * Gets whether a graphics feature is supported on this system.
  340. **/
  341. bool isSupported(Support feature) const;
  342. void push(StackType type = STACK_TRANSFORM);
  343. void pop();
  344. void rotate(float r);
  345. void scale(float x, float y = 1.0f);
  346. void translate(float x, float y);
  347. void shear(float kx, float ky);
  348. void origin();
  349. private:
  350. struct DisplayState
  351. {
  352. // Colors.
  353. Color color;
  354. Color backgroundColor;
  355. // Blend mode.
  356. BlendMode blendMode;
  357. // Line.
  358. float lineWidth;
  359. LineStyle lineStyle;
  360. LineJoin lineJoin;
  361. // Point.
  362. float pointSize;
  363. // Scissor.
  364. bool scissor;
  365. OpenGL::Viewport scissorBox;
  366. Object::StrongRef<Font> font;
  367. Object::StrongRef<Shader> shader;
  368. std::vector<Object::StrongRef<Canvas>> canvases;
  369. // Color mask.
  370. bool colorMask[4];
  371. bool wireframe;
  372. DisplayState();
  373. DisplayState(const DisplayState &other);
  374. ~DisplayState();
  375. DisplayState &operator = (const DisplayState &other);
  376. };
  377. void restoreState(const DisplayState &s);
  378. void restoreStateChecked(const DisplayState &s);
  379. love::window::Window *currentWindow;
  380. std::vector<double> pixel_size_stack; // stores current size of a pixel (needed for line drawing)
  381. int width;
  382. int height;
  383. bool created;
  384. bool activeStencil;
  385. std::vector<DisplayState> states;
  386. std::vector<StackType> stackTypes; // Keeps track of the pushed stack types.
  387. static const size_t MAX_USER_STACK_DEPTH = 64;
  388. bool displayedMinReqWarning;
  389. }; // Graphics
  390. } // opengl
  391. } // graphics
  392. } // love
  393. #endif // LOVE_GRAPHICS_OPENGL_GRAPHICS_H