Graphics.cpp 35 KB

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