Graphics.h 13 KB

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