Graphics.cpp 34 KB

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