Graphics.h 13 KB

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