Graphics.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /**
  2. * Copyright (c) 2006-2011 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. // SDL
  26. #include <SDL.h>
  27. #include "GLee.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 "Font.h"
  34. #include "Image.h"
  35. #include "Quad.h"
  36. #include "SpriteBatch.h"
  37. #include "ParticleSystem.h"
  38. #include "Framebuffer.h"
  39. namespace love
  40. {
  41. namespace graphics
  42. {
  43. namespace opengl
  44. {
  45. struct DisplayMode
  46. {
  47. int width, height; // The size of the screen.
  48. int colorDepth; // The color depth of the display mode.
  49. bool fullscreen; // Fullscreen (true), or windowed (false).
  50. bool vsync; // Vsync enabled (true), or disabled (false).
  51. int fsaa; // 0 for no FSAA, otherwise 1, 2 or 4.
  52. };
  53. // During display mode changing, certain
  54. // variables about the OpenGL context are
  55. // lost.
  56. struct DisplayState
  57. {
  58. // Colors.
  59. Color color;
  60. Color backgroundColor;
  61. // Blend and color modes.
  62. Graphics::BlendMode blendMode;
  63. Graphics::ColorMode colorMode;
  64. // Line.
  65. Graphics::LineStyle lineStyle;
  66. // Point.
  67. float pointSize;
  68. Graphics::PointStyle pointStyle;
  69. // Scissor.
  70. bool scissor;
  71. GLint scissorBox[4];
  72. // Window info.
  73. std::string caption;
  74. bool mouseVisible;
  75. // Default values.
  76. DisplayState()
  77. {
  78. color.set(255,255,255,255);
  79. backgroundColor.r = 0;
  80. backgroundColor.g = 0;
  81. backgroundColor.b = 0;
  82. backgroundColor.a = 255;
  83. blendMode = Graphics::BLEND_ALPHA;
  84. colorMode = Graphics::COLOR_MODULATE;
  85. lineStyle = Graphics::LINE_SMOOTH;
  86. pointSize = 1.0f;
  87. pointStyle = Graphics::POINT_SMOOTH;
  88. scissor = false;
  89. caption = "";
  90. mouseVisible = true;
  91. }
  92. };
  93. class Graphics : public love::graphics::Graphics
  94. {
  95. private:
  96. Font * currentFont;
  97. DisplayMode currentMode;
  98. float lineWidth;
  99. public:
  100. Graphics();
  101. virtual ~Graphics();
  102. // Implements Module.
  103. const char * getName() const;
  104. /**
  105. * Checks whether a display mode is supported or not. Note
  106. * that fullscreen is assumed, because windowed modes are
  107. * generally supported regardless of size.
  108. * @param width The window width.
  109. * @param height The window height.
  110. **/
  111. bool checkMode(int width, int height, bool fullscreen);
  112. DisplayState saveState();
  113. void restoreState(const DisplayState & s);
  114. /**
  115. * Sets the current display mode.
  116. * @param width The window width.
  117. * @param height The window height.
  118. * @param fullscreen True if fullscreen, false otherwise.
  119. * @param vsync True if we should wait for vsync, false otherwise.
  120. * @param fsaa Number of full scene anti-aliasing buffer, or 0 for disabled.
  121. **/
  122. bool setMode(int width, int height, bool fullscreen, bool vsync, int fsaa);
  123. /**
  124. * Toggles fullscreen. Note that this also needs to reload the
  125. * entire OpenGL context.
  126. **/
  127. bool toggleFullscreen();
  128. /**
  129. * Resets the current color, background color,
  130. * line style, and so forth. (This will be called
  131. * when the game reloads.
  132. **/
  133. void reset();
  134. /**
  135. * Clears the screen.
  136. **/
  137. void clear();
  138. /**
  139. * Flips buffers. (Rendered geometry is
  140. * presented on screen).
  141. **/
  142. void present();
  143. /**
  144. * Sets the window's icon.
  145. **/
  146. void setIcon(Image * image);
  147. /**
  148. * Sets the window's caption.
  149. **/
  150. void setCaption(const char * caption);
  151. int getCaption(lua_State * L);
  152. /**
  153. * Gets the width of the current display mode.
  154. **/
  155. int getWidth();
  156. /**
  157. * Gets the height of the current display mode.
  158. **/
  159. int getHeight();
  160. /**
  161. * True if some display mode is set.
  162. **/
  163. bool isCreated();
  164. /**
  165. * This native Lua function gets available modes
  166. * from SDL and returns them as a table on the following format:
  167. *
  168. * {
  169. * { width = 800, height = 600 },
  170. * { width = 1024, height = 768 },
  171. * ...
  172. * }
  173. *
  174. * Only fullscreen modes are returned here, as all
  175. * window sizes are supported (normally).
  176. **/
  177. int getModes(lua_State * L);
  178. /**
  179. * Scissor defines a box such that everything outside that box is discared and not drawn.
  180. * Scissoring is automatically enabled.
  181. * @param x The x-coordinate of the top-left corner.
  182. * @param y The y-coordinate of the top-left corner.
  183. * @param width The width of the box.
  184. * @param height The height of the box.
  185. **/
  186. void setScissor(int x, int y, int width, int height);
  187. /**
  188. * Clears any scissor that has been created.
  189. **/
  190. void setScissor();
  191. /**
  192. * This native Lua function gets the current scissor box in the order of:
  193. * x, y, width, height
  194. **/
  195. int getScissor(lua_State * L);
  196. /**
  197. * Creates an Image object with padding and/or optimization.
  198. **/
  199. Image * newImage(love::filesystem::File * file);
  200. Image * newImage(love::image::ImageData * data);
  201. /**
  202. * Creates a Frame
  203. **/
  204. Quad * newQuad(int x, int y, int w, int h, int sw, int sh);
  205. /**
  206. * Creates a Font object.
  207. **/
  208. Font * newFont(love::font::Rasterizer * data, const Image::Filter& filter = Image::Filter());
  209. SpriteBatch * newSpriteBatch(Image * image, int size, int usage);
  210. ParticleSystem * newParticleSystem(Image * image, int size);
  211. Framebuffer * newFramebuffer(int width, int height);
  212. /**
  213. * Sets the foreground color.
  214. **/
  215. void setColor(const Color& c);
  216. /**
  217. * Gets current color.
  218. **/
  219. Color getColor();
  220. /**
  221. * Sets the background Color.
  222. **/
  223. void setBackgroundColor(const Color& c);
  224. /**
  225. * Gets the current background color.
  226. * @param c Array of size 3 (r,g,b).
  227. **/
  228. Color getBackgroundColor();
  229. /**
  230. * Sets the current font.
  231. * @parm font A Font object.
  232. **/
  233. void setFont(Font * font);
  234. /**
  235. * Gets the current Font, or nil if none.
  236. **/
  237. Font * getFont();
  238. /**
  239. * Sets the current blend mode.
  240. **/
  241. void setBlendMode(BlendMode mode);
  242. /**
  243. * Sets the current color mode.
  244. **/
  245. void setColorMode (ColorMode mode);
  246. /**
  247. * Gets the current blend mode.
  248. **/
  249. BlendMode getBlendMode();
  250. /**
  251. * Gets the current color mode.
  252. **/
  253. ColorMode getColorMode();
  254. /**
  255. * Sets the line width.
  256. * @param width The new width of the line.
  257. **/
  258. void setLineWidth(float width);
  259. /**
  260. * Sets the line style.
  261. * @param style LINE_ROUGH or LINE_SMOOTH.
  262. **/
  263. void setLineStyle(LineStyle style);
  264. /**
  265. * Sets the type of line used to draw primitives.
  266. * A shorthand for setLineWidth and setLineStyle.
  267. **/
  268. void setLine(float width, LineStyle style);
  269. /**
  270. * Gets the line width.
  271. **/
  272. float getLineWidth();
  273. /**
  274. * Gets the line style.
  275. **/
  276. LineStyle getLineStyle();
  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. * Shorthand for setPointSize and setPointStyle.
  288. **/
  289. void setPoint(float size, PointStyle style);
  290. /**
  291. * Gets the point size.
  292. **/
  293. float getPointSize();
  294. /**
  295. * Gets the point style.
  296. **/
  297. PointStyle getPointStyle();
  298. /**
  299. * Gets the maximum point size supported.
  300. * This may vary from computer to computer.
  301. **/
  302. int getMaxPointSize();
  303. /**
  304. * Draws text at the specified coordinates, with rotation and
  305. * scaling along both axes.
  306. * @param x The x-coordinate.
  307. * @param y The y-coordinate.
  308. * @param angle The amount of rotation.
  309. * @param sx The scale factor along the x-axis. (1 = normal).
  310. * @param sy The scale factor along the y-axis. (1 = normal).
  311. **/
  312. void print(const char * str, float x, float y , float angle = 0.0f, float sx = 1.0f, float sy = 1.0f);
  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. **/
  322. void printf(const char * str, float x, float y, float wrap, AlignMode align);
  323. /**
  324. * Draws a point at (x,y).
  325. * @param x Point along x-axis.
  326. * @param y Point along y-axis.
  327. **/
  328. void point(float x, float y);
  329. /**
  330. * Draws a series of lines connecting the given vertices.
  331. * @param coords Vertex components (x1, y1, x2, y2, etc.)
  332. * @param count Coord array size
  333. * @param looping Wether the line is joining itself
  334. **/
  335. void polyline(const float* coords, size_t count, bool looping = false);
  336. /**
  337. * Draws a triangle using the three coordinates passed.
  338. * @param mode The mode of drawing (line/filled).
  339. * @param x1 First x-coordinate.
  340. * @param y1 First y-coordinate.
  341. * @param x2 Second x-coordinate.
  342. * @param y2 Second y-coordinate.
  343. * @param x3 Third x-coordinate.
  344. * @param y3 Third y-coordinate.
  345. **/
  346. void triangle(DrawMode mode, float x1, float y1, float x2, float y2, float x3, float y3);
  347. /**
  348. * Draws a rectangle.
  349. * @param x Position along x-axis for top-left corner.
  350. * @param y Position along y-axis for top-left corner.
  351. * @param w The width of the rectangle.
  352. * @param h The height of the rectangle.
  353. **/
  354. void rectangle(DrawMode mode, float x, float y, float w, float h);
  355. /**
  356. * Draws a quadrilateral using the four coordinates passed.
  357. * @param mode The mode of drawing (line/filled).
  358. * @param x1 First x-coordinate.
  359. * @param y1 First y-coordinate.
  360. * @param x2 Second x-coordinate.
  361. * @param y2 Second y-coordinate.
  362. * @param x3 Third x-coordinate.
  363. * @param y3 Third y-coordinate.
  364. * @param x4 Fourth x-coordinate.
  365. * @param y4 Fourth y-coordinate.
  366. **/
  367. void quad(DrawMode mode, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4);
  368. /**
  369. * Draws a circle using the specified arguments.
  370. * @param mode The mode of drawing (line/filled).
  371. * @param x X-coordinate.
  372. * @param y Y-coordinate.
  373. * @param radius Radius of the circle.
  374. * @param points Amount of points to use to draw the circle.
  375. **/
  376. void circle(DrawMode mode, float x, float y, float radius, int points = 10);
  377. void arc(DrawMode mode, float x, float y, float radius, float angle1, float angle2, int points = 10);
  378. /**
  379. * Draws a polygon with an arbitrary number of vertices.
  380. * @param type The type of drawing (line/filled).
  381. * @param coords Vertex components (x1, y1, x2, y2, etc.)
  382. * @param count Coord array size
  383. **/
  384. void polygon(DrawMode mode, const float* coords, size_t count);
  385. /**
  386. * Creates a screenshot of the view and saves it to the default folder.
  387. * @param file The file to write the screenshot to.
  388. **/
  389. love::image::ImageData * newScreenshot(love::image::Image * image);
  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 drawTest(Image * image, float x, float y, float a, float sx, float sy, float ox, float oy);
  396. bool hasFocus();
  397. }; // Graphics
  398. } // opengl
  399. } // graphics
  400. } // love
  401. #endif // LOVE_GRAPHICS_OPENGL_GRAPHICS_H