Graphics.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. /**
  2. * Copyright (c) 2006-2014 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. // LOVE
  21. #include "common/config.h"
  22. #include "common/math.h"
  23. #include "common/Vector.h"
  24. #include "Graphics.h"
  25. #include "window/sdl/Window.h"
  26. #include "Polyline.h"
  27. // C++
  28. #include <vector>
  29. #include <sstream>
  30. #include <algorithm>
  31. #include <iterator>
  32. // C
  33. #include <cmath>
  34. namespace love
  35. {
  36. namespace graphics
  37. {
  38. namespace opengl
  39. {
  40. Graphics::Graphics()
  41. : currentFont(0)
  42. , lineStyle(LINE_SMOOTH)
  43. , lineWidth(1)
  44. , matrixLimit(0)
  45. , userMatrices(0)
  46. , colorMask()
  47. , width(0)
  48. , height(0)
  49. , created(false)
  50. , activeStencil(false)
  51. , savedState()
  52. , displayedMinReqWarning(false)
  53. {
  54. currentWindow = love::window::sdl::Window::createSingleton();
  55. int w, h;
  56. love::window::WindowSettings wsettings;
  57. currentWindow->getWindow(w, h, wsettings);
  58. if (currentWindow->isCreated())
  59. setMode(w, h, wsettings.sRGB);
  60. }
  61. Graphics::~Graphics()
  62. {
  63. if (currentFont != 0)
  64. currentFont->release();
  65. currentWindow->release();
  66. }
  67. const char *Graphics::getName() const
  68. {
  69. return "love.graphics.opengl";
  70. }
  71. DisplayState Graphics::saveState()
  72. {
  73. DisplayState s;
  74. s.color = getColor();
  75. s.backgroundColor = getBackgroundColor();
  76. s.blendMode = getBlendMode();
  77. //get line style
  78. s.lineStyle = lineStyle;
  79. //get the point size
  80. glGetFloatv(GL_POINT_SIZE, &s.pointSize);
  81. //get scissor status
  82. s.scissor = (glIsEnabled(GL_SCISSOR_TEST) == GL_TRUE);
  83. //do we have scissor, if so, store the box
  84. if (s.scissor)
  85. s.scissorBox = gl.getScissor();
  86. for (int i = 0; i < 4; i++)
  87. s.colorMask[i] = colorMask[i];
  88. wireframe = isWireframe();
  89. return s;
  90. }
  91. void Graphics::restoreState(const DisplayState &s)
  92. {
  93. setColor(s.color);
  94. setBackgroundColor(s.backgroundColor);
  95. setBlendMode(s.blendMode);
  96. setLineWidth(lineWidth);
  97. setLineStyle(s.lineStyle);
  98. setPointSize(s.pointSize);
  99. if (s.scissor)
  100. setScissor(s.scissorBox.x, s.scissorBox.y, s.scissorBox.w, s.scissorBox.h);
  101. else
  102. setScissor();
  103. setColorMask(s.colorMask[0], s.colorMask[1], s.colorMask[2], s.colorMask[3]);
  104. setWireframe(s.wireframe);
  105. }
  106. void Graphics::setViewportSize(int width, int height)
  107. {
  108. this->width = width;
  109. this->height = height;
  110. if (!isCreated())
  111. return;
  112. // We want to affect the main screen, not any Canvas that's currently active
  113. // (not that any *should* be active when this is called.)
  114. Canvas *c = Canvas::current;
  115. Canvas::bindDefaultCanvas();
  116. // Set the viewport to top-left corner.
  117. gl.setViewport(OpenGL::Viewport(0, 0, width, height));
  118. // If a canvas was bound before this function was called, it needs to be
  119. // made aware of the new system viewport size.
  120. Canvas::systemViewport = gl.getViewport();
  121. // Reset the projection matrix
  122. glMatrixMode(GL_PROJECTION);
  123. glLoadIdentity();
  124. // Set up orthographic view (no depth)
  125. glOrtho(0.0, width, height, 0.0, -1.0, 1.0);
  126. glMatrixMode(GL_MODELVIEW);
  127. // Restore the previously active Canvas.
  128. if (c != nullptr)
  129. c->startGrab(c->getAttachedCanvases());
  130. }
  131. bool Graphics::setMode(int width, int height, bool &sRGB)
  132. {
  133. this->width = width;
  134. this->height = height;
  135. gl.initContext();
  136. // Does the system meet LOVE's minimum requirements for graphics?
  137. if (!(GLEE_VERSION_2_0 && Shader::isSupported() && Canvas::isSupported())
  138. && !displayedMinReqWarning)
  139. {
  140. love::window::MessageBoxType type = love::window::MESSAGEBOX_ERROR;
  141. const char *title = "Minimum system requirements not met!";
  142. std::string message;
  143. message += "Detected OpenGL version: ";
  144. message += (const char *) glGetString(GL_VERSION);
  145. message += "\nRequired OpenGL version: 2.1."; // -ish
  146. message += "\nThe program may crash or have graphical issues.";
  147. ::printf("%s\n%s\n", title, message.c_str());
  148. currentWindow->showMessageBox(type, title, message.c_str());
  149. // We should only show the message once, instead of after every setMode.
  150. displayedMinReqWarning = true;
  151. }
  152. // Okay, setup OpenGL.
  153. gl.setupContext();
  154. created = true;
  155. setViewportSize(width, height);
  156. // Make sure antialiasing works when set elsewhere
  157. glEnable(GL_MULTISAMPLE);
  158. // Enable blending
  159. glEnable(GL_BLEND);
  160. // Enable all color component writes.
  161. setColorMask(true, true, true, true);
  162. // Enable line/point smoothing.
  163. setLineStyle(LINE_SMOOTH);
  164. glEnable(GL_POINT_SMOOTH);
  165. glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
  166. // Auto-generated mipmaps should be the best quality possible
  167. glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST);
  168. // Enable textures
  169. glEnable(GL_TEXTURE_2D);
  170. gl.setTextureUnit(0);
  171. // Reset modelview matrix
  172. glMatrixMode(GL_MODELVIEW);
  173. glLoadIdentity();
  174. // Set pixel row alignment
  175. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  176. // Reload all volatile objects.
  177. if (!Volatile::loadAll())
  178. std::cerr << "Could not reload all volatile objects." << std::endl;
  179. // Restore the display state.
  180. restoreState(savedState);
  181. pixel_size_stack.clear();
  182. pixel_size_stack.reserve(5);
  183. pixel_size_stack.push_back(1);
  184. // Get the maximum number of matrices
  185. // subtract a few to give the engine some room.
  186. glGetIntegerv(GL_MAX_MODELVIEW_STACK_DEPTH, &matrixLimit);
  187. matrixLimit -= 5;
  188. // Set whether drawing converts input from linear -> sRGB colorspace.
  189. if (GLEE_VERSION_3_0 || GLEE_ARB_framebuffer_sRGB || GLEE_EXT_framebuffer_sRGB)
  190. {
  191. if (sRGB)
  192. glEnable(GL_FRAMEBUFFER_SRGB);
  193. else
  194. glDisable(GL_FRAMEBUFFER_SRGB);
  195. }
  196. else
  197. sRGB = false;
  198. Canvas::screenHasSRGB = sRGB;
  199. bool enabledebug = false;
  200. if (GLEE_VERSION_3_0)
  201. {
  202. // Enable OpenGL's debug output if a debug context has been created.
  203. GLint flags = 0;
  204. glGetIntegerv(GL_CONTEXT_FLAGS, &flags);
  205. enabledebug = (flags & GL_CONTEXT_FLAG_DEBUG_BIT) != 0;
  206. }
  207. setDebug(enabledebug);
  208. return true;
  209. }
  210. void Graphics::unSetMode()
  211. {
  212. if (!isCreated())
  213. return;
  214. // Window re-creation may destroy the GL context, so we must save the state.
  215. savedState = saveState();
  216. // Unload all volatile objects. These must be reloaded after the display
  217. // mode change.
  218. Volatile::unloadAll();
  219. gl.deInitContext();
  220. created = false;
  221. }
  222. static void APIENTRY debugCB(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei /*len*/, const GLchar *msg, GLvoid* /*usr*/)
  223. {
  224. // Human-readable strings for the debug info.
  225. const char *sourceStr = OpenGL::debugSourceString(source);
  226. const char *typeStr = OpenGL::debugTypeString(type);
  227. const char *severityStr = OpenGL::debugSeverityString(severity);
  228. const char *fmt = "OpenGL: %s [source=%s, type=%s, severity=%s, id=%d]\n";
  229. printf(fmt, msg, sourceStr, typeStr, severityStr, id);
  230. }
  231. void Graphics::setDebug(bool enable)
  232. {
  233. // Make sure debug output is supported. The AMD ext. is a bit different
  234. // so we don't make use of it, since AMD drivers now support KHR_debug.
  235. if (!(GLEE_VERSION_4_3 || GLEE_KHR_debug || GLEE_ARB_debug_output))
  236. return;
  237. // Ugly hack to reduce code duplication.
  238. if (GLEE_ARB_debug_output && !(GLEE_VERSION_4_3 || GLEE_KHR_debug))
  239. {
  240. glDebugMessageCallback = (GLEEPFNGLDEBUGMESSAGECALLBACKPROC) glDebugMessageCallbackARB;
  241. glDebugMessageControl = (GLEEPFNGLDEBUGMESSAGECONTROLPROC) glDebugMessageControlARB;
  242. }
  243. if (!enable)
  244. {
  245. // Disable the debug callback function.
  246. glDebugMessageCallback(nullptr, nullptr);
  247. // We can disable debug output entirely with KHR_debug.
  248. if (GLEE_VERSION_4_3 || GLEE_KHR_debug)
  249. glDisable(GL_DEBUG_OUTPUT);
  250. return;
  251. }
  252. // We don't want asynchronous debug output.
  253. glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
  254. glDebugMessageCallback(debugCB, nullptr);
  255. // Initially, enable everything.
  256. glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, 0, GL_TRUE);
  257. // Disable messages about deprecated OpenGL functionality.
  258. glDebugMessageControl(GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR, GL_DONT_CARE, 0, 0, GL_FALSE);
  259. glDebugMessageControl(GL_DEBUG_SOURCE_SHADER_COMPILER, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR, GL_DONT_CARE, 0, 0, GL_FALSE);
  260. if (GLEE_VERSION_4_3 || GLEE_KHR_debug)
  261. glEnable(GL_DEBUG_OUTPUT);
  262. ::printf("OpenGL debug output enabled (LOVE_GRAPHICS_DEBUG=1)\n");
  263. }
  264. void Graphics::reset()
  265. {
  266. DisplayState s;
  267. discardStencil();
  268. Canvas::bindDefaultCanvas();
  269. Shader::detach();
  270. origin();
  271. restoreState(s);
  272. }
  273. void Graphics::clear()
  274. {
  275. glClear(GL_COLOR_BUFFER_BIT);
  276. }
  277. void Graphics::present()
  278. {
  279. currentWindow->swapBuffers();
  280. }
  281. int Graphics::getWidth() const
  282. {
  283. return width;
  284. }
  285. int Graphics::getHeight() const
  286. {
  287. return height;
  288. }
  289. bool Graphics::isCreated() const
  290. {
  291. return created;
  292. }
  293. void Graphics::setScissor(int x, int y, int width, int height)
  294. {
  295. glEnable(GL_SCISSOR_TEST);
  296. // OpenGL's reversed y-coordinate is compensated for in OpenGL::setScissor.
  297. gl.setScissor(OpenGL::Viewport(x, y, width, height));
  298. }
  299. void Graphics::setScissor()
  300. {
  301. glDisable(GL_SCISSOR_TEST);
  302. }
  303. bool Graphics::getScissor(int &x, int &y, int &width, int &height) const
  304. {
  305. OpenGL::Viewport scissor = gl.getScissor();
  306. x = scissor.x;
  307. y = scissor.y;
  308. width = scissor.w;
  309. height = scissor.h;
  310. return glIsEnabled(GL_SCISSOR_TEST) == GL_TRUE;
  311. }
  312. void Graphics::defineStencil()
  313. {
  314. // Make sure the active canvas has a stencil buffer.
  315. if (Canvas::current)
  316. Canvas::current->checkCreateStencil();
  317. // Disable color writes but don't save the mask values.
  318. glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  319. glClear(GL_STENCIL_BUFFER_BIT);
  320. glEnable(GL_STENCIL_TEST);
  321. glStencilFunc(GL_ALWAYS, 1, 1);
  322. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  323. activeStencil = true;
  324. }
  325. void Graphics::useStencil(bool invert)
  326. {
  327. glStencilFunc(GL_EQUAL, (GLint)(!invert), 1); // invert ? 0 : 1
  328. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  329. setColorMask(colorMask[0], colorMask[1], colorMask[2], colorMask[3]);
  330. }
  331. void Graphics::discardStencil()
  332. {
  333. if (!activeStencil)
  334. return;
  335. setColorMask(colorMask[0], colorMask[1], colorMask[2], colorMask[3]);
  336. glDisable(GL_STENCIL_TEST);
  337. activeStencil = false;
  338. }
  339. Image *Graphics::newImage(love::image::ImageData *data, Image::Format format)
  340. {
  341. // Create the image.
  342. Image *image = new Image(data, format);
  343. if (!isCreated())
  344. return image;
  345. bool success = false;
  346. try
  347. {
  348. success = image->load();
  349. }
  350. catch(love::Exception &)
  351. {
  352. image->release();
  353. throw;
  354. }
  355. if (!success)
  356. {
  357. image->release();
  358. return 0;
  359. }
  360. return image;
  361. }
  362. Image *Graphics::newImage(love::image::CompressedData *cdata, Image::Format format)
  363. {
  364. // Create the image.
  365. Image *image = new Image(cdata, format);
  366. if (!isCreated())
  367. return image;
  368. bool success = false;
  369. try
  370. {
  371. success = image->load();
  372. }
  373. catch(love::Exception &)
  374. {
  375. image->release();
  376. throw;
  377. }
  378. if (!success)
  379. {
  380. image->release();
  381. return 0;
  382. }
  383. return image;
  384. }
  385. Quad *Graphics::newQuad(Quad::Viewport v, float sw, float sh)
  386. {
  387. return new Quad(v, sw, sh);
  388. }
  389. Font *Graphics::newFont(love::font::Rasterizer *r, const Texture::Filter &filter)
  390. {
  391. return new Font(r, filter);
  392. }
  393. SpriteBatch *Graphics::newSpriteBatch(Texture *texture, int size, int usage)
  394. {
  395. return new SpriteBatch(texture, size, usage);
  396. }
  397. ParticleSystem *Graphics::newParticleSystem(Texture *texture, int size)
  398. {
  399. return new ParticleSystem(texture, size);
  400. }
  401. Canvas *Graphics::newCanvas(int width, int height, Canvas::Format format, int fsaa)
  402. {
  403. if (!Canvas::isFormatSupported(format))
  404. {
  405. const char *fstr = "rgba8";
  406. Canvas::getConstant(format, fstr);
  407. throw love::Exception("The %s canvas format is not supported by your OpenGL implementation.", fstr);
  408. }
  409. if (width > gl.getMaxTextureSize())
  410. throw Exception("Cannot create canvas: width of %d pixels is too large for this system.", width);
  411. else if (height > gl.getMaxTextureSize())
  412. throw Exception("Cannot create canvas: height of %d pixels is too large for this system.", height);
  413. while (GL_NO_ERROR != glGetError())
  414. /* clear opengl error flag */;
  415. Canvas *canvas = new Canvas(width, height, format, fsaa);
  416. GLenum err = canvas->getStatus();
  417. // everything ok, return canvas (early out)
  418. if (err == GL_FRAMEBUFFER_COMPLETE)
  419. return canvas;
  420. // create error message
  421. std::stringstream error_string;
  422. error_string << "Cannot create canvas: ";
  423. switch (err)
  424. {
  425. case GL_FRAMEBUFFER_UNSUPPORTED:
  426. error_string << "Not supported by your OpenGL implementation.";
  427. break;
  428. // remaining error codes are highly unlikely:
  429. case GL_FRAMEBUFFER_UNDEFINED:
  430. case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
  431. case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
  432. case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
  433. case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
  434. case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
  435. error_string << "Error in implementation. Possible fix: Make canvas width and height powers of two.";
  436. break;
  437. default:
  438. // my intel hda card wrongly returns 0 to glCheckFramebufferStatus() but sets
  439. // no error flag. I think it meant to return GL_FRAMEBUFFER_UNSUPPORTED, but who
  440. // knows.
  441. if (glGetError() == GL_NO_ERROR)
  442. error_string << "May not be supported by your OpenGL implementation.";
  443. // the remaining error is an indication of a serious fuckup since it should
  444. // only be returned if glCheckFramebufferStatus() was called with the wrong
  445. // arguments.
  446. else
  447. error_string << "Cannot create canvas: Aliens did it (OpenGL error code: " << glGetError() << ")";
  448. }
  449. canvas->release();
  450. throw Exception("%s", error_string.str().c_str());
  451. return nullptr; // never reached
  452. }
  453. Shader *Graphics::newShader(const Shader::ShaderSources &sources)
  454. {
  455. return new Shader(sources);
  456. }
  457. Mesh *Graphics::newMesh(const std::vector<Vertex> &vertices, Mesh::DrawMode mode)
  458. {
  459. return new Mesh(vertices, mode);
  460. }
  461. Mesh *Graphics::newMesh(int vertexcount, Mesh::DrawMode mode)
  462. {
  463. return new Mesh(vertexcount, mode);
  464. }
  465. void Graphics::setColor(const Color &c)
  466. {
  467. gl.setColor(c);
  468. }
  469. Color Graphics::getColor() const
  470. {
  471. return gl.getColor();
  472. }
  473. void Graphics::setBackgroundColor(const Color &c)
  474. {
  475. gl.setClearColor(c);
  476. }
  477. Color Graphics::getBackgroundColor() const
  478. {
  479. return gl.getClearColor();
  480. }
  481. void Graphics::setFont(Font *font)
  482. {
  483. Object::AutoRelease fontrelease(currentFont);
  484. currentFont = font;
  485. if (font != 0)
  486. currentFont->retain();
  487. }
  488. Font *Graphics::getFont() const
  489. {
  490. return currentFont;
  491. }
  492. void Graphics::setColorMask(bool r, bool g, bool b, bool a)
  493. {
  494. colorMask[0] = r;
  495. colorMask[1] = g;
  496. colorMask[2] = b;
  497. colorMask[3] = a;
  498. glColorMask((GLboolean) r, (GLboolean) g, (GLboolean) b, (GLboolean) a);
  499. }
  500. const bool *Graphics::getColorMask() const
  501. {
  502. return colorMask;
  503. }
  504. void Graphics::setBlendMode(Graphics::BlendMode mode)
  505. {
  506. OpenGL::BlendState state = {GL_ONE, GL_ONE, GL_ZERO, GL_ZERO, GL_FUNC_ADD};
  507. switch (mode)
  508. {
  509. case BLEND_ALPHA:
  510. state.srcRGB = GL_SRC_ALPHA;
  511. state.srcA = GL_ONE;
  512. state.dstRGB = state.dstA = GL_ONE_MINUS_SRC_ALPHA;
  513. break;
  514. case BLEND_MULTIPLY:
  515. state.srcRGB = state.srcA = GL_DST_COLOR;
  516. state.dstRGB = state.dstA = GL_ZERO;
  517. break;
  518. case BLEND_PREMULTIPLIED:
  519. state.srcRGB = state.srcA = GL_ONE;
  520. state.dstRGB = state.dstA = GL_ONE_MINUS_SRC_ALPHA;
  521. break;
  522. case BLEND_SUBTRACT:
  523. state.func = GL_FUNC_REVERSE_SUBTRACT;
  524. case BLEND_ADD:
  525. state.srcRGB = state.srcA = GL_SRC_ALPHA;
  526. state.dstRGB = state.dstA = GL_ONE;
  527. break;
  528. case BLEND_SCREEN:
  529. state.srcRGB = state.srcA = GL_ONE;
  530. state.dstRGB = state.dstA = GL_ONE_MINUS_SRC_COLOR;
  531. break;
  532. case BLEND_REPLACE:
  533. default:
  534. state.srcRGB = state.srcA = GL_ONE;
  535. state.dstRGB = state.dstA = GL_ZERO;
  536. break;
  537. }
  538. gl.setBlendState(state);
  539. }
  540. Graphics::BlendMode Graphics::getBlendMode() const
  541. {
  542. OpenGL::BlendState state = gl.getBlendState();
  543. if (state.func == GL_FUNC_REVERSE_SUBTRACT) // && src == GL_SRC_ALPHA && dst == GL_ONE
  544. return BLEND_SUBTRACT;
  545. // Everything else has equation == GL_FUNC_ADD.
  546. else if (state.srcRGB == state.srcA && state.dstRGB == state.dstA)
  547. {
  548. if (state.srcRGB == GL_SRC_ALPHA && state.dstRGB == GL_ONE)
  549. return BLEND_ADD;
  550. else if (state.srcRGB == GL_SRC_ALPHA && state.dstRGB == GL_ONE_MINUS_SRC_ALPHA)
  551. return BLEND_ALPHA; // alpha blend mode fallback for very old OpenGL versions.
  552. else if (state.srcRGB == GL_DST_COLOR && state.dstRGB == GL_ZERO)
  553. return BLEND_MULTIPLY;
  554. else if (state.srcRGB == GL_ONE && state.dstRGB == GL_ONE_MINUS_SRC_ALPHA)
  555. return BLEND_PREMULTIPLIED;
  556. else if (state.srcRGB == GL_ONE && state.dstRGB == GL_ONE_MINUS_SRC_COLOR)
  557. return BLEND_SCREEN;
  558. else if (state.srcRGB == GL_ONE && state.dstRGB == GL_ZERO)
  559. return BLEND_REPLACE;
  560. }
  561. else if (state.srcRGB == GL_SRC_ALPHA && state.srcA == GL_ONE &&
  562. state.dstRGB == GL_ONE_MINUS_SRC_ALPHA && state.dstA == GL_ONE_MINUS_SRC_ALPHA)
  563. return BLEND_ALPHA;
  564. throw Exception("Unknown blend mode");
  565. }
  566. void Graphics::setDefaultFilter(const Texture::Filter &f)
  567. {
  568. Texture::setDefaultFilter(f);
  569. }
  570. const Texture::Filter &Graphics::getDefaultFilter() const
  571. {
  572. return Texture::getDefaultFilter();
  573. }
  574. void Graphics::setDefaultMipmapFilter(Texture::FilterMode filter, float sharpness)
  575. {
  576. Image::setDefaultMipmapFilter(filter);
  577. Image::setDefaultMipmapSharpness(sharpness);
  578. }
  579. void Graphics::getDefaultMipmapFilter(Texture::FilterMode *filter, float *sharpness) const
  580. {
  581. *filter = Image::getDefaultMipmapFilter();
  582. *sharpness = Image::getDefaultMipmapSharpness();
  583. }
  584. void Graphics::setLineWidth(float width)
  585. {
  586. lineWidth = width;
  587. }
  588. void Graphics::setLineStyle(Graphics::LineStyle style)
  589. {
  590. lineStyle = style;
  591. }
  592. void Graphics::setLineJoin(Graphics::LineJoin join)
  593. {
  594. lineJoin = join;
  595. }
  596. float Graphics::getLineWidth() const
  597. {
  598. return lineWidth;
  599. }
  600. Graphics::LineStyle Graphics::getLineStyle() const
  601. {
  602. return lineStyle;
  603. }
  604. Graphics::LineJoin Graphics::getLineJoin() const
  605. {
  606. return lineJoin;
  607. }
  608. void Graphics::setPointSize(float size)
  609. {
  610. glPointSize((GLfloat)size);
  611. }
  612. float Graphics::getPointSize() const
  613. {
  614. GLfloat size;
  615. glGetFloatv(GL_POINT_SIZE, &size);
  616. return (float)size;
  617. }
  618. void Graphics::setWireframe(bool enable)
  619. {
  620. wireframe = enable;
  621. glPolygonMode(GL_FRONT_AND_BACK, enable ? GL_LINE : GL_FILL);
  622. }
  623. bool Graphics::isWireframe() const
  624. {
  625. return wireframe;
  626. }
  627. void Graphics::print(const std::string &str, float x, float y , float angle, float sx, float sy, float ox, float oy, float kx, float ky)
  628. {
  629. if (currentFont != nullptr)
  630. currentFont->print(str, x, y, 0.0, angle, sx, sy, ox, oy, kx, ky);
  631. }
  632. void Graphics::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)
  633. {
  634. if (currentFont == nullptr)
  635. return;
  636. if (wrap < 0.0f)
  637. throw love::Exception("Horizontal wrap limit cannot be negative.");
  638. using std::string;
  639. using std::vector;
  640. // wrappedlines indicates which lines were automatically wrapped. It's
  641. // guaranteed to have the same number of elements as lines_to_draw.
  642. vector<bool> wrappedlines;
  643. vector<string> lines_to_draw = currentFont->getWrap(str, wrap, 0, &wrappedlines);
  644. glPushMatrix();
  645. static Matrix t;
  646. t.setTransformation(ceilf(x), ceilf(y), angle, sx, sy, ox, oy, kx, ky);
  647. glMultMatrixf((const GLfloat *)t.getElements());
  648. x = y = 0.0f;
  649. try
  650. {
  651. // now for the actual printing
  652. vector<string>::const_iterator line_iter, line_end = lines_to_draw.end();
  653. float extra_spacing = 0.0f;
  654. int num_spaces = 0;
  655. int i = 0;
  656. for (line_iter = lines_to_draw.begin(); line_iter != line_end; ++line_iter)
  657. {
  658. float width = static_cast<float>(currentFont->getWidth(*line_iter));
  659. switch (align)
  660. {
  661. case ALIGN_RIGHT:
  662. currentFont->print(*line_iter, ceilf(x + (wrap - width)), ceilf(y), 0.0f);
  663. break;
  664. case ALIGN_CENTER:
  665. currentFont->print(*line_iter, ceilf(x + (wrap - width) / 2), ceilf(y), 0.0f);
  666. break;
  667. case ALIGN_JUSTIFY:
  668. num_spaces = std::count(line_iter->begin(), line_iter->end(), ' ');
  669. if (wrappedlines[i] && num_spaces >= 1)
  670. extra_spacing = (wrap - width) / float(num_spaces);
  671. else
  672. extra_spacing = 0.0f;
  673. currentFont->print(*line_iter, ceilf(x), ceilf(y), extra_spacing);
  674. break;
  675. case ALIGN_LEFT:
  676. default:
  677. currentFont->print(*line_iter, ceilf(x), ceilf(y), 0.0f);
  678. break;
  679. }
  680. y += currentFont->getHeight() * currentFont->getLineHeight();
  681. i++;
  682. }
  683. }
  684. catch (love::Exception &)
  685. {
  686. glPopMatrix();
  687. throw;
  688. }
  689. glPopMatrix();
  690. }
  691. /**
  692. * Primitives
  693. **/
  694. void Graphics::point(float x, float y)
  695. {
  696. gl.prepareDraw();
  697. gl.bindTexture(0);
  698. glBegin(GL_POINTS);
  699. glVertex2f(x, y);
  700. glEnd();
  701. }
  702. void Graphics::polyline(const float *coords, size_t count)
  703. {
  704. if (lineJoin == LINE_JOIN_NONE)
  705. {
  706. NoneJoinPolyline line;
  707. line.render(coords, count, lineWidth * .5f, float(pixel_size_stack.back()), lineStyle == LINE_SMOOTH);
  708. line.draw();
  709. }
  710. else if (lineJoin == LINE_JOIN_BEVEL)
  711. {
  712. BevelJoinPolyline line;
  713. line.render(coords, count, lineWidth * .5f, float(pixel_size_stack.back()), lineStyle == LINE_SMOOTH);
  714. line.draw();
  715. }
  716. else // LINE_JOIN_MITER
  717. {
  718. MiterJoinPolyline line;
  719. line.render(coords, count, lineWidth * .5f, float(pixel_size_stack.back()), lineStyle == LINE_SMOOTH);
  720. line.draw();
  721. }
  722. }
  723. void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h)
  724. {
  725. float coords[] = {x,y, x,y+h, x+w,y+h, x+w,y, x,y};
  726. polygon(mode, coords, 5 * 2);
  727. }
  728. void Graphics::circle(DrawMode mode, float x, float y, float radius, int points)
  729. {
  730. float two_pi = static_cast<float>(LOVE_M_PI * 2);
  731. if (points <= 0) points = 1;
  732. float angle_shift = (two_pi / points);
  733. float phi = .0f;
  734. float *coords = new float[2 * (points + 1)];
  735. for (int i = 0; i < points; ++i, phi += angle_shift)
  736. {
  737. coords[2*i] = x + radius * cosf(phi);
  738. coords[2*i+1] = y + radius * sinf(phi);
  739. }
  740. coords[2*points] = coords[0];
  741. coords[2*points+1] = coords[1];
  742. polygon(mode, coords, (points + 1) * 2);
  743. delete[] coords;
  744. }
  745. void Graphics::arc(DrawMode mode, float x, float y, float radius, float angle1, float angle2, int points)
  746. {
  747. // Nothing to display with no points or equal angles. (Or is there with line mode?)
  748. if (points <= 0 || angle1 == angle2)
  749. return;
  750. // Oh, you want to draw a circle?
  751. if (fabs(angle1 - angle2) >= 2.0f * (float) LOVE_M_PI)
  752. {
  753. circle(mode, x, y, radius, points);
  754. return;
  755. }
  756. float angle_shift = (angle2 - angle1) / points;
  757. // Bail on precision issues.
  758. if (angle_shift == 0.0)
  759. return;
  760. float phi = angle1;
  761. int num_coords = (points + 3) * 2;
  762. float *coords = new float[num_coords];
  763. coords[0] = coords[num_coords - 2] = x;
  764. coords[1] = coords[num_coords - 1] = y;
  765. for (int i = 0; i <= points; ++i, phi += angle_shift)
  766. {
  767. coords[2 * (i+1)] = x + radius * cosf(phi);
  768. coords[2 * (i+1) + 1] = y + radius * sinf(phi);
  769. }
  770. // GL_POLYGON can only fill-draw convex polygons, so we need to do stuff manually here
  771. if (mode == DRAW_LINE)
  772. {
  773. polyline(coords, num_coords); // Artifacts at sharp angles if set to looping.
  774. }
  775. else
  776. {
  777. gl.prepareDraw();
  778. gl.bindTexture(0);
  779. glEnableClientState(GL_VERTEX_ARRAY);
  780. glVertexPointer(2, GL_FLOAT, 0, (const GLvoid *) coords);
  781. glDrawArrays(GL_TRIANGLE_FAN, 0, points + 2);
  782. glDisableClientState(GL_VERTEX_ARRAY);
  783. }
  784. delete[] coords;
  785. }
  786. /// @param mode the draw mode
  787. /// @param coords the coordinate array
  788. /// @param count the number of coordinates/size of the array
  789. void Graphics::polygon(DrawMode mode, const float *coords, size_t count)
  790. {
  791. // coords is an array of a closed loop of vertices, i.e.
  792. // coords[count-2] = coords[0], coords[count-1] = coords[1]
  793. if (mode == DRAW_LINE)
  794. {
  795. polyline(coords, count);
  796. }
  797. else
  798. {
  799. gl.prepareDraw();
  800. gl.bindTexture(0);
  801. glEnableClientState(GL_VERTEX_ARRAY);
  802. glVertexPointer(2, GL_FLOAT, 0, (const GLvoid *)coords);
  803. glDrawArrays(GL_POLYGON, 0, count/2-1); // opengl will close the polygon for us
  804. glDisableClientState(GL_VERTEX_ARRAY);
  805. }
  806. }
  807. love::image::ImageData *Graphics::newScreenshot(love::image::Image *image, bool copyAlpha)
  808. {
  809. // Temporarily unbind the currently active canvas (glReadPixels reads the
  810. // active framebuffer, not the main one.)
  811. Canvas *curcanvas = Canvas::current;
  812. if (curcanvas)
  813. Canvas::bindDefaultCanvas();
  814. int w = getWidth();
  815. int h = getHeight();
  816. int row = 4*w;
  817. int size = row*h;
  818. GLubyte *pixels = 0;
  819. GLubyte *screenshot = 0;
  820. try
  821. {
  822. pixels = new GLubyte[size];
  823. screenshot = new GLubyte[size];
  824. }
  825. catch (std::exception &)
  826. {
  827. delete[] pixels;
  828. delete[] screenshot;
  829. if (curcanvas)
  830. curcanvas->startGrab(curcanvas->getAttachedCanvases());
  831. throw love::Exception("Out of memory.");
  832. }
  833. glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
  834. if (!copyAlpha)
  835. {
  836. // Replace alpha values with full opacity.
  837. for (int i = 3; i < size; i += 4)
  838. pixels[i] = 255;
  839. }
  840. // OpenGL sucks and reads pixels from the lower-left. Let's fix that.
  841. GLubyte *src = pixels - row, *dst = screenshot + size;
  842. for (int i = 0; i < h; ++i)
  843. memcpy(dst-=row, src+=row, row);
  844. delete[] pixels;
  845. love::image::ImageData *img = 0;
  846. try
  847. {
  848. // Tell the new ImageData that it owns the screenshot data, so we don't
  849. // need to delete it here.
  850. img = image->newImageData(w, h, (void *) screenshot, true);
  851. }
  852. catch (love::Exception &)
  853. {
  854. delete[] screenshot;
  855. if (curcanvas)
  856. curcanvas->startGrab(curcanvas->getAttachedCanvases());
  857. throw;
  858. }
  859. // Re-bind the active canvas, if necessary.
  860. if (curcanvas)
  861. curcanvas->startGrab(curcanvas->getAttachedCanvases());
  862. return img;
  863. }
  864. std::string Graphics::getRendererInfo(Graphics::RendererInfo infotype) const
  865. {
  866. const char *infostr = 0;
  867. switch (infotype)
  868. {
  869. case Graphics::RENDERER_INFO_NAME:
  870. default:
  871. infostr = "OpenGL";
  872. break;
  873. case Graphics::RENDERER_INFO_VERSION:
  874. infostr = (const char *) glGetString(GL_VERSION);
  875. break;
  876. case Graphics::RENDERER_INFO_VENDOR:
  877. infostr = (const char *) glGetString(GL_VENDOR);
  878. break;
  879. case Graphics::RENDERER_INFO_DEVICE:
  880. infostr = (const char *) glGetString(GL_RENDERER);
  881. break;
  882. }
  883. if (!infostr)
  884. throw love::Exception("Cannot retrieve renderer information.");
  885. return std::string(infostr);
  886. }
  887. double Graphics::getSystemLimit(SystemLimit limittype) const
  888. {
  889. double limit = 0.0;
  890. switch (limittype)
  891. {
  892. case Graphics::LIMIT_POINT_SIZE:
  893. {
  894. GLfloat limits[2];
  895. glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, limits);
  896. limit = limits[1];
  897. }
  898. break;
  899. case Graphics::LIMIT_TEXTURE_SIZE:
  900. limit = (double) gl.getMaxTextureSize();
  901. break;
  902. case Graphics::LIMIT_MULTI_CANVAS:
  903. limit = (double) gl.getMaxRenderTargets();
  904. break;
  905. case Graphics::LIMIT_CANVAS_FSAA:
  906. if (GLEE_VERSION_3_0 || GLEE_ARB_framebuffer_object
  907. || GLEE_EXT_framebuffer_multisample)
  908. {
  909. GLint intlimit = 0;
  910. glGetIntegerv(GL_MAX_SAMPLES, &intlimit);
  911. limit = (double) intlimit;
  912. }
  913. break;
  914. default:
  915. break;
  916. }
  917. return limit;
  918. }
  919. void Graphics::push()
  920. {
  921. if (userMatrices == matrixLimit)
  922. throw Exception("Maximum stack depth reached. (More pushes than pops?)");
  923. glPushMatrix();
  924. ++userMatrices;
  925. pixel_size_stack.push_back(pixel_size_stack.back());
  926. }
  927. void Graphics::pop()
  928. {
  929. if (userMatrices < 1)
  930. throw Exception("Minimum stack depth reached. (More pops than pushes?)");
  931. glPopMatrix();
  932. --userMatrices;
  933. pixel_size_stack.pop_back();
  934. }
  935. void Graphics::rotate(float r)
  936. {
  937. glRotatef(LOVE_TODEG(r), 0, 0, 1);
  938. }
  939. void Graphics::scale(float x, float y)
  940. {
  941. glScalef(x, y, 1);
  942. pixel_size_stack.back() *= 2. / (fabs(x) + fabs(y));
  943. }
  944. void Graphics::translate(float x, float y)
  945. {
  946. glTranslatef(x, y, 0);
  947. }
  948. void Graphics::shear(float kx, float ky)
  949. {
  950. Matrix t;
  951. t.setShear(kx, ky);
  952. glMultMatrixf((const GLfloat *)t.getElements());
  953. }
  954. void Graphics::origin()
  955. {
  956. glLoadIdentity();
  957. pixel_size_stack.back() = 1;
  958. }
  959. } // opengl
  960. } // graphics
  961. } // love