Graphics.h 12 KB

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