Graphics.h 12 KB

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