Graphics.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  1. /**
  2. * Copyright (c) 2006-2015 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. #include <cstdio>
  35. namespace love
  36. {
  37. namespace graphics
  38. {
  39. namespace opengl
  40. {
  41. Graphics::Graphics()
  42. : width(0)
  43. , height(0)
  44. , created(false)
  45. , writingToStencil(false)
  46. {
  47. gl = OpenGL();
  48. states.reserve(10);
  49. states.push_back(DisplayState());
  50. currentWindow = love::window::sdl::Window::createSingleton();
  51. int w, h;
  52. love::window::WindowSettings wsettings;
  53. currentWindow->getWindow(w, h, wsettings);
  54. if (currentWindow->isCreated())
  55. setMode(w, h, wsettings.sRGB);
  56. }
  57. Graphics::~Graphics()
  58. {
  59. // We do this manually so the love objects get released before the window.
  60. states.clear();
  61. if (Shader::defaultShader)
  62. {
  63. Shader::defaultShader->release();
  64. Shader::defaultShader = nullptr;
  65. }
  66. currentWindow->release();
  67. }
  68. const char *Graphics::getName() const
  69. {
  70. return "love.graphics.opengl";
  71. }
  72. void Graphics::restoreState(const DisplayState &s)
  73. {
  74. setColor(s.color);
  75. setBackgroundColor(s.backgroundColor);
  76. setBlendMode(s.blendMode);
  77. setLineWidth(s.lineWidth);
  78. setLineStyle(s.lineStyle);
  79. setLineJoin(s.lineJoin);
  80. setPointSize(s.pointSize);
  81. if (s.scissor)
  82. setScissor(s.scissorBox.x, s.scissorBox.y, s.scissorBox.w, s.scissorBox.h);
  83. else
  84. setScissor();
  85. setStencilTest(s.stencilTest, s.stencilInvert);
  86. setFont(s.font.get());
  87. setShader(s.shader.get());
  88. setCanvas(s.canvases);
  89. setColorMask(s.colorMask);
  90. setWireframe(s.wireframe);
  91. setDefaultFilter(s.defaultFilter);
  92. setDefaultMipmapFilter(s.defaultMipmapFilter, s.defaultMipmapSharpness);
  93. }
  94. void Graphics::restoreStateChecked(const DisplayState &s)
  95. {
  96. const DisplayState &cur = states.back();
  97. if (*(uint32 *) &s.color.r != *(uint32 *) &cur.color.r)
  98. setColor(s.color);
  99. if (*(uint32 *) &s.backgroundColor.r != *(uint32 *) &cur.backgroundColor.r)
  100. setBackgroundColor(s.backgroundColor);
  101. if (s.blendMode != cur.blendMode)
  102. setBlendMode(s.blendMode);
  103. // These are just simple assignments.
  104. setLineWidth(s.lineWidth);
  105. setLineStyle(s.lineStyle);
  106. setLineJoin(s.lineJoin);
  107. if (s.pointSize != cur.pointSize)
  108. setPointSize(s.pointSize);
  109. if (s.scissor != cur.scissor || (s.scissor && !(s.scissorBox == cur.scissorBox)))
  110. {
  111. if (s.scissor)
  112. setScissor(s.scissorBox.x, s.scissorBox.y, s.scissorBox.w, s.scissorBox.h);
  113. else
  114. setScissor();
  115. }
  116. if (s.stencilTest != cur.stencilTest || s.stencilInvert != cur.stencilInvert)
  117. setStencilTest(s.stencilTest, s.stencilInvert);
  118. setFont(s.font.get());
  119. setShader(s.shader.get());
  120. for (size_t i = 0; i < s.canvases.size() && i < cur.canvases.size(); i++)
  121. {
  122. if (s.canvases[i].get() != cur.canvases[i].get())
  123. {
  124. setCanvas(s.canvases);
  125. break;
  126. }
  127. }
  128. for (int i = 0; i < 4; i++)
  129. {
  130. if (s.colorMask[i] != cur.colorMask[i])
  131. {
  132. setColorMask(s.colorMask);
  133. break;
  134. }
  135. }
  136. if (s.wireframe != cur.wireframe)
  137. setWireframe(s.wireframe);
  138. setDefaultFilter(s.defaultFilter);
  139. setDefaultMipmapFilter(s.defaultMipmapFilter, s.defaultMipmapSharpness);
  140. }
  141. void Graphics::setViewportSize(int width, int height)
  142. {
  143. this->width = width;
  144. this->height = height;
  145. if (!isCreated())
  146. return;
  147. // We want to affect the main screen, not any Canvas that's currently active
  148. // (not that any *should* be active when this is called.)
  149. std::vector<Object::StrongRef<Canvas>> canvases = states.back().canvases;
  150. setCanvas();
  151. // Set the viewport to top-left corner.
  152. gl.setViewport(OpenGL::Viewport(0, 0, width, height));
  153. // If a canvas was bound before this function was called, it needs to be
  154. // made aware of the new system viewport size.
  155. Canvas::systemViewport = gl.getViewport();
  156. // Set up the projection matrix
  157. gl.matrices.projection.back() = Matrix::ortho(0.0, width, height, 0.0);
  158. // Restore the previously active Canvas.
  159. setCanvas(canvases);
  160. }
  161. bool Graphics::setMode(int width, int height, bool &sRGB)
  162. {
  163. this->width = width;
  164. this->height = height;
  165. // Okay, setup OpenGL.
  166. gl.initContext();
  167. gl.setupContext();
  168. created = true;
  169. setViewportSize(width, height);
  170. // Make sure antialiasing works when set elsewhere
  171. glEnable(GL_MULTISAMPLE);
  172. // Enable blending
  173. glEnable(GL_BLEND);
  174. // Auto-generated mipmaps should be the best quality possible
  175. glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST);
  176. // Enable textures
  177. glEnable(GL_TEXTURE_2D);
  178. gl.setTextureUnit(0);
  179. // Set pixel row alignment
  180. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  181. // Set whether drawing converts input from linear -> sRGB colorspace.
  182. if (GLAD_VERSION_3_0 || GLAD_ARB_framebuffer_sRGB || GLAD_EXT_framebuffer_sRGB
  183. || GLAD_ES_VERSION_3_0 || GLAD_EXT_sRGB)
  184. {
  185. if (GLAD_VERSION_1_0 || GLAD_EXT_sRGB_write_control)
  186. {
  187. if (sRGB)
  188. glEnable(GL_FRAMEBUFFER_SRGB);
  189. else
  190. glDisable(GL_FRAMEBUFFER_SRGB);
  191. }
  192. }
  193. else
  194. sRGB = false;
  195. Canvas::screenHasSRGB = sRGB;
  196. bool enabledebug = false;
  197. if (GLAD_VERSION_3_0)
  198. {
  199. // Enable OpenGL's debug output if a debug context has been created.
  200. GLint flags = 0;
  201. glGetIntegerv(GL_CONTEXT_FLAGS, &flags);
  202. enabledebug = (flags & GL_CONTEXT_FLAG_DEBUG_BIT) != 0;
  203. }
  204. setDebug(enabledebug);
  205. // Reload all volatile objects.
  206. if (!Volatile::loadAll())
  207. ::printf("Could not reload all volatile objects.\n");
  208. // Restore the graphics state.
  209. restoreState(states.back());
  210. pixel_size_stack.clear();
  211. pixel_size_stack.reserve(5);
  212. pixel_size_stack.push_back(1);
  213. // We always need a default shader.
  214. if (!Shader::defaultShader)
  215. {
  216. Renderer renderer = GLAD_ES_VERSION_2_0 ? RENDERER_OPENGLES : RENDERER_OPENGL;
  217. Shader::defaultShader = newShader(Shader::defaultCode[renderer]);
  218. }
  219. if (!getShader())
  220. setShader(Shader::defaultShader);
  221. return true;
  222. }
  223. void Graphics::unSetMode()
  224. {
  225. if (!isCreated())
  226. return;
  227. // Unload all volatile objects. These must be reloaded after the display
  228. // mode change.
  229. Volatile::unloadAll();
  230. gl.deInitContext();
  231. created = false;
  232. }
  233. static void APIENTRY debugCB(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei /*len*/, const GLchar *msg, const GLvoid* /*usr*/)
  234. {
  235. // Human-readable strings for the debug info.
  236. const char *sourceStr = OpenGL::debugSourceString(source);
  237. const char *typeStr = OpenGL::debugTypeString(type);
  238. const char *severityStr = OpenGL::debugSeverityString(severity);
  239. const char *fmt = "OpenGL: %s [source=%s, type=%s, severity=%s, id=%d]\n";
  240. printf(fmt, msg, sourceStr, typeStr, severityStr, id);
  241. }
  242. void Graphics::setDebug(bool enable)
  243. {
  244. // Make sure debug output is supported. The AMD ext. is a bit different
  245. // so we don't make use of it, since AMD drivers now support KHR_debug.
  246. if (!(GLAD_VERSION_4_3 || GLAD_KHR_debug || GLAD_ARB_debug_output))
  247. return;
  248. // TODO: We don't support GL_KHR_debug in GLES yet.
  249. if (GLAD_ES_VERSION_2_0)
  250. return;
  251. // Ugly hack to reduce code duplication.
  252. if (GLAD_ARB_debug_output && !(GLAD_VERSION_4_3 || GLAD_KHR_debug))
  253. {
  254. fp_glDebugMessageCallback = (pfn_glDebugMessageCallback) fp_glDebugMessageCallbackARB;
  255. fp_glDebugMessageControl = (pfn_glDebugMessageControl) fp_glDebugMessageControlARB;
  256. }
  257. if (!enable)
  258. {
  259. // Disable the debug callback function.
  260. glDebugMessageCallback(nullptr, nullptr);
  261. // We can disable debug output entirely with KHR_debug.
  262. if (GLAD_VERSION_4_3 || GLAD_KHR_debug)
  263. glDisable(GL_DEBUG_OUTPUT);
  264. return;
  265. }
  266. // We don't want asynchronous debug output.
  267. glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
  268. glDebugMessageCallback(debugCB, nullptr);
  269. // Initially, enable everything.
  270. glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, 0, GL_TRUE);
  271. // Disable messages about deprecated OpenGL functionality.
  272. glDebugMessageControl(GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR, GL_DONT_CARE, 0, 0, GL_FALSE);
  273. glDebugMessageControl(GL_DEBUG_SOURCE_SHADER_COMPILER, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR, GL_DONT_CARE, 0, 0, GL_FALSE);
  274. if (GLAD_VERSION_4_3 || GLAD_KHR_debug)
  275. glEnable(GL_DEBUG_OUTPUT);
  276. ::printf("OpenGL debug output enabled (LOVE_GRAPHICS_DEBUG=1)\n");
  277. }
  278. void Graphics::reset()
  279. {
  280. DisplayState s;
  281. drawToStencilBuffer(false);
  282. restoreState(s);
  283. origin();
  284. }
  285. void Graphics::clear(ClearType type)
  286. {
  287. GLbitfield mask = 0;
  288. switch (type)
  289. {
  290. case CLEAR_ALL:
  291. default:
  292. mask = GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
  293. break;
  294. case CLEAR_STENCIL:
  295. mask = GL_STENCIL_BUFFER_BIT;
  296. break;
  297. }
  298. glClear(mask);
  299. }
  300. void Graphics::present()
  301. {
  302. // Make sure we don't have a canvas active.
  303. std::vector<Object::StrongRef<Canvas>> canvases = states.back().canvases;
  304. setCanvas();
  305. if (GLAD_ES_VERSION_3_0 || GLAD_EXT_discard_framebuffer)
  306. {
  307. GLenum attachments[] = {GL_STENCIL, GL_DEPTH};
  308. if (gl.getDefaultFBO() != 0)
  309. {
  310. // A non-zero FBO needs different attachment enums.
  311. attachments[0] = GL_STENCIL_ATTACHMENT;
  312. attachments[1] = GL_DEPTH_ATTACHMENT;
  313. }
  314. // Hint for the driver that it doesn't need to save these buffers.
  315. if (GLAD_ES_VERSION_3_0)
  316. glInvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
  317. else if (GLAD_EXT_discard_framebuffer)
  318. glDiscardFramebufferEXT(GL_FRAMEBUFFER, 2, attachments);
  319. }
  320. currentWindow->swapBuffers();
  321. // Restore the currently active canvas, if there is one.
  322. setCanvas(canvases);
  323. // Reset the per-frame stat counts.
  324. gl.stats.drawCalls = 0;
  325. Canvas::switchCount = 0;
  326. }
  327. int Graphics::getWidth() const
  328. {
  329. return width;
  330. }
  331. int Graphics::getHeight() const
  332. {
  333. return height;
  334. }
  335. bool Graphics::isCreated() const
  336. {
  337. return created;
  338. }
  339. void Graphics::setScissor(int x, int y, int width, int height)
  340. {
  341. OpenGL::Viewport box(x, y, width, height);
  342. states.back().scissor = true;
  343. glEnable(GL_SCISSOR_TEST);
  344. // OpenGL's reversed y-coordinate is compensated for in OpenGL::setScissor.
  345. gl.setScissor(box);
  346. states.back().scissorBox = box;
  347. }
  348. void Graphics::setScissor()
  349. {
  350. states.back().scissor = false;
  351. glDisable(GL_SCISSOR_TEST);
  352. }
  353. bool Graphics::getScissor(int &x, int &y, int &width, int &height) const
  354. {
  355. OpenGL::Viewport scissor = gl.getScissor();
  356. x = scissor.x;
  357. y = scissor.y;
  358. width = scissor.w;
  359. height = scissor.h;
  360. return states.back().scissor;
  361. }
  362. void Graphics::drawToStencilBuffer(bool enable)
  363. {
  364. if (writingToStencil == enable)
  365. return;
  366. writingToStencil = enable;
  367. if (!enable)
  368. {
  369. const DisplayState &state = states.back();
  370. // Revert the color write mask.
  371. setColorMask(state.colorMask);
  372. // Use the user-set stencil test state when writes are disabled.
  373. setStencilTest(state.stencilTest, state.stencilInvert);
  374. return;
  375. }
  376. // Make sure the active canvas has a stencil buffer.
  377. if (Canvas::current)
  378. Canvas::current->checkCreateStencil();
  379. // Disable color writes but don't save the state for it.
  380. glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  381. // The stencil test must be enabled in order to write to the stencil buffer.
  382. glEnable(GL_STENCIL_TEST);
  383. glStencilFunc(GL_ALWAYS, 1, 1);
  384. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  385. }
  386. void Graphics::setStencilTest(bool enable, bool invert)
  387. {
  388. DisplayState &state = states.back();
  389. state.stencilTest = enable;
  390. state.stencilInvert = invert;
  391. if (writingToStencil)
  392. return;
  393. if (!enable)
  394. {
  395. glDisable(GL_STENCIL_TEST);
  396. return;
  397. }
  398. // Make sure the active canvas has a stencil buffer.
  399. if (Canvas::current)
  400. Canvas::current->checkCreateStencil();
  401. glEnable(GL_STENCIL_TEST);
  402. glStencilFunc(GL_EQUAL, invert ? 0 : 1, 1);
  403. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  404. }
  405. void Graphics::getStencilTest(bool &enable, bool &invert)
  406. {
  407. const DisplayState &state = states.back();
  408. enable = state.stencilTest;
  409. invert = state.stencilInvert;
  410. }
  411. Image *Graphics::newImage(love::image::ImageData *data, const Image::Flags &flags)
  412. {
  413. // Create the image.
  414. Image *image = new Image(data, flags);
  415. if (!isCreated())
  416. return image;
  417. bool success = false;
  418. try
  419. {
  420. success = image->load();
  421. }
  422. catch(love::Exception &)
  423. {
  424. image->release();
  425. throw;
  426. }
  427. if (!success)
  428. {
  429. image->release();
  430. return nullptr;
  431. }
  432. return image;
  433. }
  434. Image *Graphics::newImage(love::image::CompressedData *cdata, const Image::Flags &flags)
  435. {
  436. // Create the image.
  437. Image *image = new Image(cdata, flags);
  438. if (!isCreated())
  439. return image;
  440. bool success = false;
  441. try
  442. {
  443. success = image->load();
  444. }
  445. catch(love::Exception &)
  446. {
  447. image->release();
  448. throw;
  449. }
  450. if (!success)
  451. {
  452. image->release();
  453. return nullptr;
  454. }
  455. return image;
  456. }
  457. Quad *Graphics::newQuad(Quad::Viewport v, float sw, float sh)
  458. {
  459. return new Quad(v, sw, sh);
  460. }
  461. Font *Graphics::newFont(love::font::Rasterizer *r, const Texture::Filter &filter)
  462. {
  463. return new Font(r, filter);
  464. }
  465. SpriteBatch *Graphics::newSpriteBatch(Texture *texture, int size, int usage)
  466. {
  467. return new SpriteBatch(texture, size, usage);
  468. }
  469. ParticleSystem *Graphics::newParticleSystem(Texture *texture, int size)
  470. {
  471. return new ParticleSystem(texture, size);
  472. }
  473. Canvas *Graphics::newCanvas(int width, int height, Canvas::Format format, int msaa)
  474. {
  475. if (!Canvas::isFormatSupported(format))
  476. {
  477. const char *fstr = "rgba8";
  478. Canvas::getConstant(format, fstr);
  479. throw love::Exception("The %s canvas format is not supported by your OpenGL implementation.", fstr);
  480. }
  481. if (width > gl.getMaxTextureSize())
  482. throw Exception("Cannot create canvas: width of %d pixels is too large for this system.", width);
  483. else if (height > gl.getMaxTextureSize())
  484. throw Exception("Cannot create canvas: height of %d pixels is too large for this system.", height);
  485. while (GL_NO_ERROR != glGetError())
  486. /* clear opengl error flag */;
  487. Canvas *canvas = new Canvas(width, height, format, msaa);
  488. GLenum err = canvas->getStatus();
  489. // everything ok, return canvas (early out)
  490. if (err == GL_FRAMEBUFFER_COMPLETE)
  491. return canvas;
  492. // create error message
  493. std::stringstream error_string;
  494. error_string << "Cannot create canvas: ";
  495. switch (err)
  496. {
  497. case GL_FRAMEBUFFER_UNSUPPORTED:
  498. error_string << "Not supported by your OpenGL implementation.";
  499. break;
  500. // remaining error codes are highly unlikely:
  501. case GL_FRAMEBUFFER_UNDEFINED:
  502. case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
  503. case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
  504. case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
  505. case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
  506. case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
  507. error_string << "Error in implementation. Possible fix: Make canvas width and height powers of two.";
  508. break;
  509. default:
  510. // my intel hda card wrongly returns 0 to glCheckFramebufferStatus() but sets
  511. // no error flag. I think it meant to return GL_FRAMEBUFFER_UNSUPPORTED, but who
  512. // knows.
  513. if (glGetError() == GL_NO_ERROR)
  514. error_string << "May not be supported by your OpenGL implementation.";
  515. // the remaining error is an indication of a serious fuckup since it should
  516. // only be returned if glCheckFramebufferStatus() was called with the wrong
  517. // arguments.
  518. else
  519. error_string << "Cannot create canvas: Aliens did it (OpenGL error code: " << glGetError() << ")";
  520. }
  521. canvas->release();
  522. throw Exception("%s", error_string.str().c_str());
  523. return nullptr; // never reached
  524. }
  525. Shader *Graphics::newShader(const Shader::ShaderSource &source)
  526. {
  527. return new Shader(source);
  528. }
  529. Mesh *Graphics::newMesh(const std::vector<Vertex> &vertices, Mesh::DrawMode mode)
  530. {
  531. return new Mesh(vertices, mode);
  532. }
  533. Mesh *Graphics::newMesh(int vertexcount, Mesh::DrawMode mode)
  534. {
  535. return new Mesh(vertexcount, mode);
  536. }
  537. void Graphics::setColor(const Color &c)
  538. {
  539. gl.setColor(c);
  540. states.back().color = c;
  541. }
  542. Color Graphics::getColor() const
  543. {
  544. return states.back().color;
  545. }
  546. void Graphics::setBackgroundColor(const Color &c)
  547. {
  548. gl.setClearColor(c);
  549. states.back().backgroundColor = c;
  550. }
  551. Color Graphics::getBackgroundColor() const
  552. {
  553. return states.back().backgroundColor;
  554. }
  555. void Graphics::setFont(Font *font)
  556. {
  557. // Hack: the Lua-facing love.graphics.print function will set the current
  558. // font if needed, but only on its first call... we want to make sure a nil
  559. // font is never accidentally set (e.g. via love.graphics.reset.)
  560. if (font == nullptr)
  561. return;
  562. DisplayState &state = states.back();
  563. state.font.set(font);
  564. }
  565. Font *Graphics::getFont() const
  566. {
  567. return states.back().font.get();
  568. }
  569. void Graphics::setShader(Shader *shader)
  570. {
  571. if (shader == nullptr)
  572. return setShader();
  573. DisplayState &state = states.back();
  574. shader->attach();
  575. state.shader.set(shader);
  576. }
  577. void Graphics::setShader()
  578. {
  579. DisplayState &state = states.back();
  580. Shader::detach();
  581. state.shader.set(nullptr);
  582. }
  583. Shader *Graphics::getShader() const
  584. {
  585. return states.back().shader.get();
  586. }
  587. void Graphics::setCanvas(Canvas *canvas)
  588. {
  589. if (canvas == nullptr)
  590. return setCanvas();
  591. DisplayState &state = states.back();
  592. canvas->startGrab();
  593. std::vector<Object::StrongRef<Canvas>> canvasref;
  594. canvasref.push_back(canvas);
  595. std::swap(state.canvases, canvasref);
  596. }
  597. void Graphics::setCanvas(const std::vector<Canvas *> &canvases)
  598. {
  599. if (canvases.size() == 0)
  600. return setCanvas();
  601. else if (canvases.size() == 1)
  602. return setCanvas(canvases[0]);
  603. DisplayState &state = states.back();
  604. auto attachments = std::vector<Canvas *>(canvases.begin() + 1, canvases.end());
  605. canvases[0]->startGrab(attachments);
  606. std::vector<Object::StrongRef<Canvas>> canvasrefs;
  607. canvasrefs.reserve(canvases.size());
  608. for (Canvas *c : canvases)
  609. canvasrefs.push_back(c);
  610. std::swap(state.canvases, canvasrefs);
  611. }
  612. void Graphics::setCanvas(const std::vector<Object::StrongRef<Canvas>> &canvases)
  613. {
  614. std::vector<Canvas *> canvaslist;
  615. canvaslist.reserve(canvases.size());
  616. for (const Object::StrongRef<Canvas> &c : canvases)
  617. canvaslist.push_back(c.get());
  618. return setCanvas(canvaslist);
  619. }
  620. void Graphics::setCanvas()
  621. {
  622. DisplayState &state = states.back();
  623. if (Canvas::current != nullptr)
  624. Canvas::current->stopGrab();
  625. state.canvases.clear();
  626. }
  627. std::vector<Canvas *> Graphics::getCanvas() const
  628. {
  629. std::vector<Canvas *> canvases;
  630. canvases.reserve(states.back().canvases.size());
  631. for (const Object::StrongRef<Canvas> &c : states.back().canvases)
  632. canvases.push_back(c.get());
  633. return canvases;
  634. }
  635. void Graphics::setColorMask(const bool mask[4])
  636. {
  637. for (int i = 0; i < 4; i++)
  638. states.back().colorMask[i] = mask[i];
  639. glColorMask(mask[0], mask[1], mask[2], mask[3]);
  640. }
  641. const bool *Graphics::getColorMask() const
  642. {
  643. return states.back().colorMask;
  644. }
  645. void Graphics::setBlendMode(Graphics::BlendMode mode)
  646. {
  647. OpenGL::BlendState blend = {GL_ONE, GL_ONE, GL_ZERO, GL_ZERO, GL_FUNC_ADD};
  648. switch (mode)
  649. {
  650. case BLEND_ALPHA:
  651. blend.srcRGB = GL_SRC_ALPHA;
  652. blend.srcA = GL_ONE;
  653. blend.dstRGB = blend.dstA = GL_ONE_MINUS_SRC_ALPHA;
  654. break;
  655. case BLEND_MULTIPLY:
  656. blend.srcRGB = blend.srcA = GL_DST_COLOR;
  657. blend.dstRGB = blend.dstA = GL_ZERO;
  658. break;
  659. case BLEND_PREMULTIPLIED:
  660. blend.srcRGB = blend.srcA = GL_ONE;
  661. blend.dstRGB = blend.dstA = GL_ONE_MINUS_SRC_ALPHA;
  662. break;
  663. case BLEND_SUBTRACT:
  664. blend.func = GL_FUNC_REVERSE_SUBTRACT;
  665. case BLEND_ADD:
  666. blend.srcRGB = blend.srcA = GL_SRC_ALPHA;
  667. blend.dstRGB = blend.dstA = GL_ONE;
  668. break;
  669. case BLEND_SCREEN:
  670. blend.srcRGB = blend.srcA = GL_ONE;
  671. blend.dstRGB = blend.dstA = GL_ONE_MINUS_SRC_COLOR;
  672. break;
  673. case BLEND_REPLACE:
  674. default:
  675. blend.srcRGB = blend.srcA = GL_ONE;
  676. blend.dstRGB = blend.dstA = GL_ZERO;
  677. break;
  678. }
  679. gl.setBlendState(blend);
  680. states.back().blendMode = mode;
  681. }
  682. Graphics::BlendMode Graphics::getBlendMode() const
  683. {
  684. return states.back().blendMode;
  685. }
  686. void Graphics::setDefaultFilter(const Texture::Filter &f)
  687. {
  688. Texture::setDefaultFilter(f);
  689. states.back().defaultFilter = f;
  690. }
  691. const Texture::Filter &Graphics::getDefaultFilter() const
  692. {
  693. return Texture::getDefaultFilter();
  694. }
  695. void Graphics::setDefaultMipmapFilter(Texture::FilterMode filter, float sharpness)
  696. {
  697. Image::setDefaultMipmapFilter(filter);
  698. Image::setDefaultMipmapSharpness(sharpness);
  699. states.back().defaultMipmapFilter = filter;
  700. states.back().defaultMipmapSharpness = sharpness;
  701. }
  702. void Graphics::getDefaultMipmapFilter(Texture::FilterMode *filter, float *sharpness) const
  703. {
  704. *filter = Image::getDefaultMipmapFilter();
  705. *sharpness = Image::getDefaultMipmapSharpness();
  706. }
  707. void Graphics::setLineWidth(float width)
  708. {
  709. states.back().lineWidth = width;
  710. }
  711. void Graphics::setLineStyle(Graphics::LineStyle style)
  712. {
  713. states.back().lineStyle = style;
  714. }
  715. void Graphics::setLineJoin(Graphics::LineJoin join)
  716. {
  717. states.back().lineJoin = join;
  718. }
  719. float Graphics::getLineWidth() const
  720. {
  721. return states.back().lineWidth;
  722. }
  723. Graphics::LineStyle Graphics::getLineStyle() const
  724. {
  725. return states.back().lineStyle;
  726. }
  727. Graphics::LineJoin Graphics::getLineJoin() const
  728. {
  729. return states.back().lineJoin;
  730. }
  731. void Graphics::setPointSize(float size)
  732. {
  733. gl.setPointSize(size);
  734. states.back().pointSize = size;
  735. }
  736. float Graphics::getPointSize() const
  737. {
  738. return states.back().pointSize;
  739. }
  740. void Graphics::setWireframe(bool enable)
  741. {
  742. // Not supported in OpenGL ES.
  743. if (GLAD_ES_VERSION_2_0)
  744. return;
  745. glPolygonMode(GL_FRONT_AND_BACK, enable ? GL_LINE : GL_FILL);
  746. states.back().wireframe = enable;
  747. }
  748. bool Graphics::isWireframe() const
  749. {
  750. return states.back().wireframe;
  751. }
  752. 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)
  753. {
  754. DisplayState &state = states.back();
  755. if (state.font.get() != nullptr)
  756. state.font->print(str, x, y, 0.0, angle, sx, sy, ox, oy, kx, ky);
  757. }
  758. 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)
  759. {
  760. DisplayState &state = states.back();
  761. if (state.font.get() == nullptr)
  762. return;
  763. if (wrap < 0.0f)
  764. throw love::Exception("Horizontal wrap limit cannot be negative.");
  765. using std::string;
  766. using std::vector;
  767. // wrappedlines indicates which lines were automatically wrapped. It's
  768. // guaranteed to have the same number of elements as lines_to_draw.
  769. vector<bool> wrappedlines;
  770. vector<string> lines_to_draw = state.font->getWrap(str, wrap, 0, &wrappedlines);
  771. static Matrix t;
  772. t.setTransformation(ceilf(x), ceilf(y), angle, sx, sy, ox, oy, kx, ky);
  773. OpenGL::TempTransform transform(gl);
  774. transform.get() *= t;
  775. x = y = 0.0f;
  776. // now for the actual printing
  777. vector<string>::const_iterator line_iter, line_end = lines_to_draw.end();
  778. float extra_spacing = 0.0f;
  779. int num_spaces = 0;
  780. int i = 0;
  781. for (line_iter = lines_to_draw.begin(); line_iter != line_end; ++line_iter)
  782. {
  783. float width = static_cast<float>(state.font->getWidth(*line_iter));
  784. switch (align)
  785. {
  786. case ALIGN_RIGHT:
  787. state.font->print(*line_iter, ceilf(x + (wrap - width)), ceilf(y), 0.0f);
  788. break;
  789. case ALIGN_CENTER:
  790. state.font->print(*line_iter, ceilf(x + (wrap - width) / 2), ceilf(y), 0.0f);
  791. break;
  792. case ALIGN_JUSTIFY:
  793. num_spaces = std::count(line_iter->begin(), line_iter->end(), ' ');
  794. if (wrappedlines[i] && num_spaces >= 1)
  795. extra_spacing = (wrap - width) / float(num_spaces);
  796. else
  797. extra_spacing = 0.0f;
  798. state.font->print(*line_iter, ceilf(x), ceilf(y), extra_spacing);
  799. break;
  800. case ALIGN_LEFT:
  801. default:
  802. state.font->print(*line_iter, ceilf(x), ceilf(y), 0.0f);
  803. break;
  804. }
  805. y += state.font->getHeight() * state.font->getLineHeight();
  806. i++;
  807. }
  808. }
  809. /**
  810. * Primitives
  811. **/
  812. void Graphics::point(float x, float y)
  813. {
  814. GLfloat coord[] = {x, y};
  815. gl.prepareDraw();
  816. gl.bindTexture(gl.getDefaultTexture());
  817. glEnableVertexAttribArray(ATTRIB_POS);
  818. glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, 0, coord);
  819. gl.drawArrays(GL_POINTS, 0, 1);
  820. glDisableVertexAttribArray(ATTRIB_POS);
  821. }
  822. void Graphics::polyline(const float *coords, size_t count)
  823. {
  824. DisplayState &state = states.back();
  825. if (state.lineJoin == LINE_JOIN_NONE)
  826. {
  827. NoneJoinPolyline line;
  828. line.render(coords, count, state.lineWidth * .5f, float(pixel_size_stack.back()), state.lineStyle == LINE_SMOOTH);
  829. line.draw();
  830. }
  831. else if (state.lineJoin == LINE_JOIN_BEVEL)
  832. {
  833. BevelJoinPolyline line;
  834. line.render(coords, count, state.lineWidth * .5f, float(pixel_size_stack.back()), state.lineStyle == LINE_SMOOTH);
  835. line.draw();
  836. }
  837. else // LINE_JOIN_MITER
  838. {
  839. MiterJoinPolyline line;
  840. line.render(coords, count, state.lineWidth * .5f, float(pixel_size_stack.back()), state.lineStyle == LINE_SMOOTH);
  841. line.draw();
  842. }
  843. }
  844. void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h)
  845. {
  846. float coords[] = {x,y, x,y+h, x+w,y+h, x+w,y, x,y};
  847. polygon(mode, coords, 5 * 2);
  848. }
  849. void Graphics::circle(DrawMode mode, float x, float y, float radius, int points)
  850. {
  851. float two_pi = static_cast<float>(LOVE_M_PI * 2);
  852. if (points <= 0) points = 1;
  853. float angle_shift = (two_pi / points);
  854. float phi = .0f;
  855. float *coords = new float[2 * (points + 1)];
  856. for (int i = 0; i < points; ++i, phi += angle_shift)
  857. {
  858. coords[2*i] = x + radius * cosf(phi);
  859. coords[2*i+1] = y + radius * sinf(phi);
  860. }
  861. coords[2*points] = coords[0];
  862. coords[2*points+1] = coords[1];
  863. polygon(mode, coords, (points + 1) * 2);
  864. delete[] coords;
  865. }
  866. void Graphics::arc(DrawMode mode, float x, float y, float radius, float angle1, float angle2, int points)
  867. {
  868. // Nothing to display with no points or equal angles. (Or is there with line mode?)
  869. if (points <= 0 || angle1 == angle2)
  870. return;
  871. // Oh, you want to draw a circle?
  872. if (fabs(angle1 - angle2) >= 2.0f * (float) LOVE_M_PI)
  873. {
  874. circle(mode, x, y, radius, points);
  875. return;
  876. }
  877. float angle_shift = (angle2 - angle1) / points;
  878. // Bail on precision issues.
  879. if (angle_shift == 0.0)
  880. return;
  881. float phi = angle1;
  882. int num_coords = (points + 3) * 2;
  883. float *coords = new float[num_coords];
  884. coords[0] = coords[num_coords - 2] = x;
  885. coords[1] = coords[num_coords - 1] = y;
  886. for (int i = 0; i <= points; ++i, phi += angle_shift)
  887. {
  888. coords[2 * (i+1)] = x + radius * cosf(phi);
  889. coords[2 * (i+1) + 1] = y + radius * sinf(phi);
  890. }
  891. // GL_POLYGON can only fill-draw convex polygons, so we need to do stuff manually here
  892. if (mode == DRAW_LINE)
  893. {
  894. polyline(coords, num_coords); // Artifacts at sharp angles if set to looping.
  895. }
  896. else
  897. {
  898. gl.prepareDraw();
  899. gl.bindTexture(gl.getDefaultTexture());
  900. glEnableVertexAttribArray(ATTRIB_POS);
  901. glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, 0, coords);
  902. gl.drawArrays(GL_TRIANGLE_FAN, 0, points + 2);
  903. glDisableVertexAttribArray(ATTRIB_POS);
  904. }
  905. delete[] coords;
  906. }
  907. /// @param mode the draw mode
  908. /// @param coords the coordinate array
  909. /// @param count the number of coordinates/size of the array
  910. void Graphics::polygon(DrawMode mode, const float *coords, size_t count)
  911. {
  912. // coords is an array of a closed loop of vertices, i.e.
  913. // coords[count-2] = coords[0], coords[count-1] = coords[1]
  914. if (mode == DRAW_LINE)
  915. {
  916. polyline(coords, count);
  917. }
  918. else
  919. {
  920. gl.prepareDraw();
  921. gl.bindTexture(gl.getDefaultTexture());
  922. glEnableVertexAttribArray(ATTRIB_POS);
  923. glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, 0, coords);
  924. gl.drawArrays(GL_TRIANGLE_FAN, 0, count/2-1); // opengl will close the polygon for us
  925. glDisableVertexAttribArray(ATTRIB_POS);
  926. }
  927. }
  928. love::image::ImageData *Graphics::newScreenshot(love::image::Image *image, bool copyAlpha)
  929. {
  930. // Temporarily unbind the currently active canvas (glReadPixels reads the
  931. // active framebuffer, not the main one.)
  932. std::vector<Object::StrongRef<Canvas>> canvases = states.back().canvases;
  933. setCanvas();
  934. int w = getWidth();
  935. int h = getHeight();
  936. int row = 4 * w;
  937. int size = row * h;
  938. GLubyte *pixels = nullptr;
  939. GLubyte *screenshot = nullptr;
  940. try
  941. {
  942. pixels = new GLubyte[size];
  943. screenshot = new GLubyte[size];
  944. }
  945. catch (std::exception &)
  946. {
  947. delete[] pixels;
  948. delete[] screenshot;
  949. setCanvas(canvases);
  950. throw love::Exception("Out of memory.");
  951. }
  952. glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
  953. if (!copyAlpha)
  954. {
  955. // Replace alpha values with full opacity.
  956. for (int i = 3; i < size; i += 4)
  957. pixels[i] = 255;
  958. }
  959. // OpenGL sucks and reads pixels from the lower-left. Let's fix that.
  960. GLubyte *src = pixels - row;
  961. GLubyte *dst = screenshot + size;
  962. for (int i = 0; i < h; ++i)
  963. memcpy(dst-=row, src+=row, row);
  964. delete[] pixels;
  965. love::image::ImageData *img = nullptr;
  966. try
  967. {
  968. // Tell the new ImageData that it owns the screenshot data, so we don't
  969. // need to delete it here.
  970. img = image->newImageData(w, h, (void *) screenshot, true);
  971. }
  972. catch (love::Exception &)
  973. {
  974. delete[] screenshot;
  975. setCanvas(canvases);
  976. throw;
  977. }
  978. // Re-bind the active canvas, if necessary.
  979. setCanvas(canvases);
  980. return img;
  981. }
  982. Graphics::RendererInfo Graphics::getRendererInfo() const
  983. {
  984. RendererInfo info;
  985. if (GLAD_ES_VERSION_2_0)
  986. info.name = "OpenGL ES";
  987. else
  988. info.name = "OpenGL";
  989. const char *str = (const char *) glGetString(GL_VERSION);
  990. if (str)
  991. info.version = str;
  992. else
  993. throw love::Exception("Cannot retrieve renderer version information.");
  994. str = (const char *) glGetString(GL_VENDOR);
  995. if (str)
  996. info.vendor = str;
  997. else
  998. throw love::Exception("Cannot retrieve renderer vendor information.");
  999. str = (const char *) glGetString(GL_RENDERER);
  1000. if (str)
  1001. info.device = str;
  1002. else
  1003. throw love::Exception("Cannot retrieve renderer device information.");
  1004. return info;
  1005. }
  1006. Graphics::Stats Graphics::getStats() const
  1007. {
  1008. Stats stats;
  1009. stats.drawCalls = gl.stats.drawCalls;
  1010. stats.canvasSwitches = Canvas::switchCount;
  1011. stats.canvases = Canvas::canvasCount;
  1012. stats.images = Image::imageCount;
  1013. stats.fonts = Font::fontCount;
  1014. stats.textureMemory = gl.stats.textureMemory;
  1015. return stats;
  1016. }
  1017. double Graphics::getSystemLimit(SystemLimit limittype) const
  1018. {
  1019. double limit = 0.0;
  1020. switch (limittype)
  1021. {
  1022. case Graphics::LIMIT_POINT_SIZE:
  1023. {
  1024. GLfloat limits[2];
  1025. glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, limits);
  1026. limit = limits[1];
  1027. }
  1028. break;
  1029. case Graphics::LIMIT_TEXTURE_SIZE:
  1030. limit = (double) gl.getMaxTextureSize();
  1031. break;
  1032. case Graphics::LIMIT_MULTI_CANVAS:
  1033. limit = (double) gl.getMaxRenderTargets();
  1034. break;
  1035. case Graphics::LIMIT_CANVAS_MSAA:
  1036. if (GLAD_VERSION_3_0 || GLAD_ARB_framebuffer_object
  1037. || GLAD_EXT_framebuffer_multisample)
  1038. {
  1039. GLint intlimit = 0;
  1040. glGetIntegerv(GL_MAX_SAMPLES, &intlimit);
  1041. limit = (double) intlimit;
  1042. }
  1043. break;
  1044. default:
  1045. break;
  1046. }
  1047. return limit;
  1048. }
  1049. bool Graphics::isSupported(Support feature) const
  1050. {
  1051. switch (feature)
  1052. {
  1053. case SUPPORT_MULTI_CANVAS:
  1054. return Canvas::isMultiCanvasSupported();
  1055. case SUPPORT_SRGB:
  1056. // sRGB support for the screen is guaranteed if it's supported as a
  1057. // Canvas format.
  1058. return Canvas::isFormatSupported(Canvas::FORMAT_SRGB);
  1059. default:
  1060. return false;
  1061. }
  1062. }
  1063. void Graphics::push(StackType type)
  1064. {
  1065. if (stackTypes.size() == MAX_USER_STACK_DEPTH)
  1066. throw Exception("Maximum stack depth reached (more pushes than pops?)");
  1067. gl.pushTransform();
  1068. pixel_size_stack.push_back(pixel_size_stack.back());
  1069. if (type == STACK_ALL)
  1070. states.push_back(states.back());
  1071. stackTypes.push_back(type);
  1072. }
  1073. void Graphics::pop()
  1074. {
  1075. if (stackTypes.size() < 1)
  1076. throw Exception("Minimum stack depth reached (more pops than pushes?)");
  1077. gl.popTransform();
  1078. pixel_size_stack.pop_back();
  1079. if (stackTypes.back() == STACK_ALL)
  1080. {
  1081. DisplayState &newstate = states[states.size() - 2];
  1082. restoreStateChecked(newstate);
  1083. // The last two states in the stack should be equal now.
  1084. states.pop_back();
  1085. }
  1086. stackTypes.pop_back();
  1087. }
  1088. void Graphics::rotate(float r)
  1089. {
  1090. gl.getTransform().rotate(r);
  1091. }
  1092. void Graphics::scale(float x, float y)
  1093. {
  1094. gl.getTransform().scale(x, y);
  1095. pixel_size_stack.back() *= 2. / (fabs(x) + fabs(y));
  1096. }
  1097. void Graphics::translate(float x, float y)
  1098. {
  1099. gl.getTransform().translate(x, y);
  1100. }
  1101. void Graphics::shear(float kx, float ky)
  1102. {
  1103. gl.getTransform().setShear(kx, ky);
  1104. }
  1105. void Graphics::origin()
  1106. {
  1107. gl.getTransform().setIdentity();
  1108. pixel_size_stack.back() = 1;
  1109. }
  1110. Graphics::DisplayState::DisplayState()
  1111. : color(255, 255, 255, 255)
  1112. , backgroundColor(0, 0, 0, 255)
  1113. , blendMode(BLEND_ALPHA)
  1114. , lineWidth(1.0f)
  1115. , lineStyle(LINE_SMOOTH)
  1116. , lineJoin(LINE_JOIN_MITER)
  1117. , pointSize(1.0f)
  1118. , scissor(false)
  1119. , scissorBox()
  1120. , stencilTest(false)
  1121. , stencilInvert(false)
  1122. , font(nullptr)
  1123. , shader(nullptr)
  1124. , wireframe(false)
  1125. , defaultFilter()
  1126. , defaultMipmapFilter(Texture::FILTER_NEAREST)
  1127. , defaultMipmapSharpness(0.0f)
  1128. {
  1129. // We should just directly initialize the array in the initializer list, but
  1130. // that feature of C++11 is broken in Visual Studio 2013...
  1131. colorMask[0] = colorMask[1] = colorMask[2] = colorMask[3] = true;
  1132. }
  1133. Graphics::DisplayState::DisplayState(const DisplayState &other)
  1134. : color(other.color)
  1135. , backgroundColor(other.backgroundColor)
  1136. , blendMode(other.blendMode)
  1137. , lineWidth(other.lineWidth)
  1138. , lineStyle(other.lineStyle)
  1139. , lineJoin(other.lineJoin)
  1140. , pointSize(other.pointSize)
  1141. , scissor(other.scissor)
  1142. , scissorBox(other.scissorBox)
  1143. , stencilTest(other.stencilTest)
  1144. , stencilInvert(other.stencilInvert)
  1145. , font(other.font)
  1146. , shader(other.shader)
  1147. , canvases(other.canvases)
  1148. , wireframe(other.wireframe)
  1149. , defaultFilter(other.defaultFilter)
  1150. , defaultMipmapFilter(other.defaultMipmapFilter)
  1151. , defaultMipmapSharpness(other.defaultMipmapSharpness)
  1152. {
  1153. for (int i = 0; i < 4; i++)
  1154. colorMask[i] = other.colorMask[i];
  1155. }
  1156. Graphics::DisplayState::~DisplayState()
  1157. {
  1158. }
  1159. Graphics::DisplayState &Graphics::DisplayState::operator = (const DisplayState &other)
  1160. {
  1161. color = other.color;
  1162. backgroundColor = other.backgroundColor;
  1163. blendMode = other.blendMode;
  1164. lineWidth = other.lineWidth;
  1165. lineStyle = other.lineStyle;
  1166. lineJoin = other.lineJoin;
  1167. pointSize = other.pointSize;
  1168. scissor = other.scissor;
  1169. scissorBox = other.scissorBox;
  1170. stencilTest = other.stencilTest;
  1171. stencilInvert = other.stencilInvert;
  1172. font = other.font;
  1173. shader = other.shader;
  1174. canvases = other.canvases;
  1175. for (int i = 0; i < 4; i++)
  1176. colorMask[i] = other.colorMask[i];
  1177. wireframe = other.wireframe;
  1178. defaultFilter = other.defaultFilter;
  1179. defaultMipmapFilter = other.defaultMipmapFilter;
  1180. defaultMipmapSharpness = other.defaultMipmapSharpness;
  1181. return *this;
  1182. }
  1183. } // opengl
  1184. } // graphics
  1185. } // love