12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367 |
- /**
- * Copyright (c) 2006-2017 LOVE Development Team
- *
- * This software is provided 'as-is', without any express or implied
- * warranty. In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- * claim that you wrote the original software. If you use this software
- * in a product, an acknowledgment in the product documentation would be
- * appreciated but is not required.
- * 2. Altered source versions must be plainly marked as such, and must not be
- * misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- **/
- // LOVE
- #include "Graphics.h"
- #include "math/MathModule.h"
- #include "Polyline.h"
- #include "font/Font.h"
- #include "window/Window.h"
- // C++
- #include <algorithm>
- namespace love
- {
- namespace graphics
- {
- static bool gammaCorrect = false;
- void setGammaCorrect(bool gammacorrect)
- {
- gammaCorrect = gammacorrect;
- }
- bool isGammaCorrect()
- {
- return gammaCorrect;
- }
- void gammaCorrectColor(Colorf &c)
- {
- if (isGammaCorrect())
- {
- c.r = math::gammaToLinear(c.r);
- c.g = math::gammaToLinear(c.g);
- c.b = math::gammaToLinear(c.b);
- }
- }
- Colorf gammaCorrectColor(const Colorf &c)
- {
- Colorf r = c;
- gammaCorrectColor(r);
- return r;
- }
- void unGammaCorrectColor(Colorf &c)
- {
- if (isGammaCorrect())
- {
- c.r = math::linearToGamma(c.r);
- c.g = math::linearToGamma(c.g);
- c.b = math::linearToGamma(c.b);
- }
- }
- Colorf unGammaCorrectColor(const Colorf &c)
- {
- Colorf r = c;
- unGammaCorrectColor(r);
- return r;
- }
- love::Type Graphics::type("graphics", &Module::type);
- Shader::ShaderSource Graphics::defaultShaderCode[Shader::LANGUAGE_MAX_ENUM][2];
- Shader::ShaderSource Graphics::defaultVideoShaderCode[Shader::LANGUAGE_MAX_ENUM][2];
- Graphics::Graphics()
- : width(0)
- , height(0)
- , pixelWidth(0)
- , pixelHeight(0)
- , created(false)
- , active(true)
- , writingToStencil(false)
- , streamBufferState()
- , projectionMatrix()
- , canvasSwitchCount(0)
- {
- transformStack.reserve(16);
- transformStack.push_back(Matrix4());
- pixelScaleStack.reserve(16);
- pixelScaleStack.push_back(1);
- if (!Shader::initialize())
- throw love::Exception("Shader support failed to initialize!");
- }
- Graphics::~Graphics()
- {
- states.clear();
- defaultFont.set(nullptr);
- if (Shader::defaultShader)
- {
- Shader::defaultShader->release();
- Shader::defaultShader = nullptr;
- }
- if (Shader::defaultVideoShader)
- {
- Shader::defaultVideoShader->release();
- Shader::defaultVideoShader = nullptr;
- }
- delete streamBufferState.vb[0];
- delete streamBufferState.vb[1];
- delete streamBufferState.indexBuffer;
- Shader::deinitialize();
- }
- Quad *Graphics::newQuad(Quad::Viewport v, double sw, double sh)
- {
- return new Quad(v, sw, sh);
- }
- bool Graphics::validateShader(bool gles, const Shader::ShaderSource &source, std::string &err)
- {
- return Shader::validate(this, gles, source, true, err);
- }
- int Graphics::getWidth() const
- {
- return width;
- }
- int Graphics::getHeight() const
- {
- return height;
- }
- int Graphics::getPixelWidth() const
- {
- return pixelWidth;
- }
- int Graphics::getPixelHeight() const
- {
- return pixelHeight;
- }
- double Graphics::getCurrentPixelDensity() const
- {
- if (states.back().canvases.size() > 0)
- {
- love::graphics::Canvas *c = states.back().canvases[0];
- return (double) c->getPixelHeight() / (double) c->getHeight();
- }
- return getScreenPixelDensity();
- }
- double Graphics::getScreenPixelDensity() const
- {
- return (double) getPixelHeight() / (double) getHeight();
- }
- bool Graphics::isCreated() const
- {
- return created;
- }
- bool Graphics::isActive() const
- {
- // The graphics module is only completely 'active' if there's a window, a
- // context, and the active variable is set.
- auto window = getInstance<love::window::Window>(M_WINDOW);
- return active && isCreated() && window != nullptr && window->isOpen();
- }
- void Graphics::reset()
- {
- DisplayState s;
- stopDrawToStencilBuffer();
- restoreState(s);
- origin();
- }
- /**
- * State functions.
- **/
- void Graphics::restoreState(const DisplayState &s)
- {
- setColor(s.color);
- setBackgroundColor(s.backgroundColor);
- setBlendMode(s.blendMode, s.blendAlphaMode);
- setLineWidth(s.lineWidth);
- setLineStyle(s.lineStyle);
- setLineJoin(s.lineJoin);
- setPointSize(s.pointSize);
- if (s.scissor)
- setScissor(s.scissorRect);
- else
- setScissor();
- setStencilTest(s.stencilCompare, s.stencilTestValue);
- setFont(s.font.get());
- setShader(s.shader.get());
- setCanvas(s.canvases);
- setColorMask(s.colorMask);
- setWireframe(s.wireframe);
- setDefaultFilter(s.defaultFilter);
- setDefaultMipmapFilter(s.defaultMipmapFilter, s.defaultMipmapSharpness);
- }
- void Graphics::restoreStateChecked(const DisplayState &s)
- {
- const DisplayState &cur = states.back();
- if (s.color != cur.color)
- setColor(s.color);
- setBackgroundColor(s.backgroundColor);
- if (s.blendMode != cur.blendMode || s.blendAlphaMode != cur.blendAlphaMode)
- setBlendMode(s.blendMode, s.blendAlphaMode);
- // These are just simple assignments.
- setLineWidth(s.lineWidth);
- setLineStyle(s.lineStyle);
- setLineJoin(s.lineJoin);
- if (s.pointSize != cur.pointSize)
- setPointSize(s.pointSize);
- if (s.scissor != cur.scissor || (s.scissor && !(s.scissorRect == cur.scissorRect)))
- {
- if (s.scissor)
- setScissor(s.scissorRect);
- else
- setScissor();
- }
- if (s.stencilCompare != cur.stencilCompare || s.stencilTestValue != cur.stencilTestValue)
- setStencilTest(s.stencilCompare, s.stencilTestValue);
- setFont(s.font.get());
- setShader(s.shader.get());
- bool canvaseschanged = s.canvases.size() != cur.canvases.size();
- if (!canvaseschanged)
- {
- for (size_t i = 0; i < s.canvases.size() && i < cur.canvases.size(); i++)
- {
- if (s.canvases[i].get() != cur.canvases[i].get())
- {
- canvaseschanged = true;
- break;
- }
- }
- }
- if (canvaseschanged)
- setCanvas(s.canvases);
- if (s.colorMask != cur.colorMask)
- setColorMask(s.colorMask);
- if (s.wireframe != cur.wireframe)
- setWireframe(s.wireframe);
- setDefaultFilter(s.defaultFilter);
- setDefaultMipmapFilter(s.defaultMipmapFilter, s.defaultMipmapSharpness);
- }
- Colorf Graphics::getColor() const
- {
- return states.back().color;
- }
- void Graphics::setBackgroundColor(Colorf c)
- {
- states.back().backgroundColor = c;
- }
- Colorf Graphics::getBackgroundColor() const
- {
- return states.back().backgroundColor;
- }
- void Graphics::checkSetDefaultFont()
- {
- // We don't create or set the default Font if an existing font is in use.
- if (states.back().font.get() != nullptr)
- return;
- // Create a new default font if we don't have one yet.
- if (!defaultFont.get())
- {
- auto fontmodule = Module::getInstance<font::Font>(M_FONT);
- if (!fontmodule)
- throw love::Exception("Font module has not been loaded.");
- auto hinting = font::TrueTypeRasterizer::HINTING_NORMAL;
- StrongRef<font::Rasterizer> r(fontmodule->newTrueTypeRasterizer(12, hinting), Acquire::NORETAIN);
- defaultFont.set(newFont(r.get()), Acquire::NORETAIN);
- }
- states.back().font.set(defaultFont.get());
- }
- void Graphics::setFont(love::graphics::Font *font)
- {
- // We don't need to set a default font here if null is passed in, since we
- // only care about the default font in getFont and print.
- DisplayState &state = states.back();
- state.font.set(font);
- }
- love::graphics::Font *Graphics::getFont()
- {
- checkSetDefaultFont();
- return states.back().font.get();
- }
- void Graphics::setShader(love::graphics::Shader *shader)
- {
- if (shader == nullptr)
- return setShader();
- flushStreamDraws();
- shader->attach();
- states.back().shader.set(shader);
- }
- void Graphics::setShader()
- {
- flushStreamDraws();
- Shader::attachDefault();
- states.back().shader.set(nullptr);
- }
- love::graphics::Shader *Graphics::getShader() const
- {
- return states.back().shader.get();
- }
- void Graphics::setCanvas(Canvas *canvas)
- {
- if (canvas == nullptr)
- return setCanvas();
- std::vector<Canvas *> canvases = {canvas};
- setCanvas(canvases);
- }
- void Graphics::setCanvas(const std::vector<StrongRef<Canvas>> &canvases)
- {
- std::vector<Canvas *> canvaslist;
- canvaslist.reserve(canvases.size());
- for (const StrongRef<Canvas> &c : canvases)
- canvaslist.push_back(c.get());
- return setCanvas(canvaslist);
- }
- std::vector<Canvas *> Graphics::getCanvas() const
- {
- std::vector<Canvas *> canvases;
- canvases.reserve(states.back().canvases.size());
- for (const StrongRef<Canvas> &c : states.back().canvases)
- canvases.push_back(c.get());
- return canvases;
- }
- bool Graphics::isCanvasActive() const
- {
- return !states.back().canvases.empty();
- }
- bool Graphics::isCanvasActive(love::graphics::Canvas *canvas) const
- {
- for (const auto &c : states.back().canvases)
- {
- if (c.get() == canvas)
- return true;
- }
- return false;
- }
- void Graphics::intersectScissor(const Rect &rect)
- {
- Rect currect = states.back().scissorRect;
- if (!states.back().scissor)
- {
- currect.x = 0;
- currect.y = 0;
- currect.w = std::numeric_limits<int>::max();
- currect.h = std::numeric_limits<int>::max();
- }
- int x1 = std::max(currect.x, rect.x);
- int y1 = std::max(currect.y, rect.y);
- int x2 = std::min(currect.x + currect.w, rect.x + rect.w);
- int y2 = std::min(currect.y + currect.h, rect.y + rect.h);
- Rect newrect = {x1, y1, std::max(0, x2 - x1), std::max(0, y2 - y1)};
- setScissor(newrect);
- }
- bool Graphics::getScissor(Rect &rect) const
- {
- const DisplayState &state = states.back();
- rect = state.scissorRect;
- return state.scissor;
- }
- void Graphics::getStencilTest(CompareMode &compare, int &value)
- {
- const DisplayState &state = states.back();
- compare = state.stencilCompare;
- value = state.stencilTestValue;
- }
- Graphics::ColorMask Graphics::getColorMask() const
- {
- return states.back().colorMask;
- }
- Graphics::BlendMode Graphics::getBlendMode(BlendAlpha &alphamode) const
- {
- alphamode = states.back().blendAlphaMode;
- return states.back().blendMode;
- }
- void Graphics::setDefaultFilter(const Texture::Filter &f)
- {
- Texture::defaultFilter = f;
- states.back().defaultFilter = f;
- }
- const Texture::Filter &Graphics::getDefaultFilter() const
- {
- return Texture::defaultFilter;
- }
- void Graphics::setDefaultMipmapFilter(Texture::FilterMode filter, float sharpness)
- {
- Texture::defaultMipmapFilter = filter;
- Texture::defaultMipmapSharpness = sharpness;
- states.back().defaultMipmapFilter = filter;
- states.back().defaultMipmapSharpness = sharpness;
- }
- void Graphics::getDefaultMipmapFilter(Texture::FilterMode *filter, float *sharpness) const
- {
- *filter = Texture::defaultMipmapFilter;
- *sharpness = Texture::defaultMipmapSharpness;
- }
- void Graphics::setLineWidth(float width)
- {
- states.back().lineWidth = width;
- }
- void Graphics::setLineStyle(Graphics::LineStyle style)
- {
- states.back().lineStyle = style;
- }
- void Graphics::setLineJoin(Graphics::LineJoin join)
- {
- states.back().lineJoin = join;
- }
- float Graphics::getLineWidth() const
- {
- return states.back().lineWidth;
- }
- Graphics::LineStyle Graphics::getLineStyle() const
- {
- return states.back().lineStyle;
- }
- Graphics::LineJoin Graphics::getLineJoin() const
- {
- return states.back().lineJoin;
- }
- float Graphics::getPointSize() const
- {
- return states.back().pointSize;
- }
- bool Graphics::isWireframe() const
- {
- return states.back().wireframe;
- }
- void Graphics::captureScreenshot(const ScreenshotInfo &info)
- {
- pendingScreenshotCallbacks.push_back(info);
- }
- Graphics::StreamVertexData Graphics::requestStreamDraw(const StreamDrawRequest &req)
- {
- using namespace vertex;
- StreamBufferState &state = streamBufferState;
- bool shouldflush = false;
- bool shouldresize = false;
- if (req.primitiveMode != state.primitiveMode
- || req.formats[0] != state.formats[0] || req.formats[1] != state.formats[1]
- || ((req.indexMode != TriangleIndexMode::NONE) != (state.indexCount > 0))
- || req.texture != state.texture || req.textureHandle != state.textureHandle)
- {
- shouldflush = true;
- }
- int totalvertices = state.vertexCount + req.vertexCount;
- // We only support uint16 index buffers for now.
- if (totalvertices > LOVE_UINT16_MAX && req.indexMode != TriangleIndexMode::NONE)
- shouldflush = true;
- int reqIndexCount = getIndexCount(req.indexMode, req.vertexCount);
- size_t reqIndexSize = reqIndexCount * sizeof(uint16);
- size_t newdatasizes[2] = {0, 0};
- size_t buffersizes[3] = {0, 0, 0};
- for (int i = 0; i < 2; i++)
- {
- if (req.formats[i] == CommonFormat::NONE)
- continue;
- size_t stride = getFormatStride(req.formats[i]);
- size_t datasize = stride * totalvertices;
- if (state.vbMap[i].data != nullptr && datasize > state.vbMap[i].size)
- shouldflush = true;
- if (datasize > state.vb[i]->getSize())
- {
- buffersizes[i] = std::max(datasize, state.vb[i]->getSize() * 2);
- shouldresize = true;
- }
- newdatasizes[i] = stride * req.vertexCount;
- }
- if (req.indexMode != TriangleIndexMode::NONE)
- {
- size_t datasize = (state.indexCount + reqIndexCount) * sizeof(uint16);
- if (state.indexBufferMap.data != nullptr && datasize > state.indexBufferMap.size)
- shouldflush = true;
- if (datasize > state.indexBuffer->getSize())
- {
- buffersizes[2] = std::max(datasize, state.indexBuffer->getSize() * 2);
- shouldresize = true;
- }
- }
- if (shouldflush || shouldresize)
- {
- flushStreamDraws();
- state.primitiveMode = req.primitiveMode;
- state.formats[0] = req.formats[0];
- state.formats[1] = req.formats[1];
- state.texture = req.texture;
- state.textureHandle = req.textureHandle;
- }
- if (shouldresize)
- {
- for (int i = 0; i < 2; i++)
- {
- if (state.vb[i]->getSize() < buffersizes[i])
- {
- delete state.vb[i];
- state.vb[i] = newStreamBuffer(BUFFER_VERTEX, buffersizes[i]);
- }
- }
- if (state.indexBuffer->getSize() < buffersizes[2])
- {
- delete state.indexBuffer;
- state.indexBuffer = newStreamBuffer(BUFFER_INDEX, buffersizes[2]);
- }
- }
- if (req.indexMode != TriangleIndexMode::NONE)
- {
- if (state.indexBufferMap.data == nullptr)
- state.indexBufferMap = state.indexBuffer->map(reqIndexSize);
- uint16 *indices = (uint16 *) state.indexBufferMap.data;
- fillIndices(req.indexMode, state.vertexCount, req.vertexCount, indices);
- state.indexBufferMap.data += reqIndexSize;
- }
- StreamVertexData d;
- for (int i = 0; i < 2; i++)
- {
- if (newdatasizes[i] > 0)
- {
- if (state.vbMap[i].data == nullptr)
- state.vbMap[i] = state.vb[i]->map(newdatasizes[i]);
- d.stream[i] = state.vbMap[i].data;
- state.vbMap[i].data += newdatasizes[i];
- }
- }
- state.vertexCount += req.vertexCount;
- state.indexCount += reqIndexCount;
- return d;
- }
- /**
- * Drawing
- **/
- void Graphics::print(const std::vector<Font::ColoredString> &str, const Matrix4 &m)
- {
- checkSetDefaultFont();
- DisplayState &state = states.back();
- if (state.font.get() != nullptr)
- state.font->print(this, str, m, state.color);
- }
- void Graphics::printf(const std::vector<Font::ColoredString> &str, float wrap, Font::AlignMode align, const Matrix4 &m)
- {
- checkSetDefaultFont();
- DisplayState &state = states.back();
- if (state.font.get() != nullptr)
- state.font->printf(this, str, wrap, align, m, state.color);
- }
- void Graphics::draw(Drawable *drawable, const Matrix4 &m)
- {
- drawable->draw(this, m);
- }
- void Graphics::draw(Texture *texture, Quad *quad, const Matrix4 &m)
- {
- texture->drawq(this, quad, m);
- }
- /**
- * Primitives (points, shapes, lines).
- **/
- void Graphics::points(const float *coords, const Colorf *colors, size_t numpoints)
- {
- StreamDrawRequest req;
- req.primitiveMode = vertex::PrimitiveMode::POINTS;
- req.formats[0] = vertex::CommonFormat::XYf;
- if (colors)
- req.formats[1] = vertex::CommonFormat::RGBAub;
- req.vertexCount = (int) numpoints;
- StreamVertexData data = requestStreamDraw(req);
- const Matrix4 &t = getTransform();
- t.transform((Vector *) data.stream[0], (const Vector *) coords, req.vertexCount);
- Color *colordata = (Color *) data.stream[1];
- if (colors)
- {
- Colorf nc = getColor();
- gammaCorrectColor(nc);
- if (isGammaCorrect())
- {
- for (int i = 0; i < req.vertexCount; i++)
- {
- Colorf ci = colors[i];
- gammaCorrectColor(ci);
- ci *= nc;
- unGammaCorrectColor(ci);
- colordata[i] = toColor(ci);
- }
- }
- else
- {
- for (int i = 0; i < req.vertexCount; i++)
- colordata[i] = toColor(nc * colors[i]);
- }
- }
- else
- {
- Color c = toColor(getColor());
- for (int i = 0; i < req.vertexCount; i++)
- colordata[i] = c;
- }
- }
- int Graphics::calculateEllipsePoints(float rx, float ry) const
- {
- int points = (int) sqrtf(((rx + ry) / 2.0f) * 20.0f * (float) pixelScaleStack.back());
- return std::max(points, 8);
- }
- void Graphics::polyline(const float *coords, size_t count)
- {
- float halfwidth = getLineWidth() * 0.5f;
- LineJoin linejoin = getLineJoin();
- LineStyle linestyle = getLineStyle();
- float pixelsize = 1.0f / std::max((float) pixelScaleStack.back(), 0.000001f);
- if (linejoin == LINE_JOIN_NONE)
- {
- NoneJoinPolyline line;
- line.render(coords, count, halfwidth, pixelsize, linestyle == LINE_SMOOTH);
- line.draw(this);
- }
- else if (linejoin == LINE_JOIN_BEVEL)
- {
- BevelJoinPolyline line;
- line.render(coords, count, halfwidth, pixelsize, linestyle == LINE_SMOOTH);
- line.draw(this);
- }
- else if (linejoin == LINE_JOIN_MITER)
- {
- MiterJoinPolyline line;
- line.render(coords, count, halfwidth, pixelsize, linestyle == LINE_SMOOTH);
- line.draw(this);
- }
- }
- void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h)
- {
- float coords[] = {x,y, x,y+h, x+w,y+h, x+w,y, x,y};
- polygon(mode, coords, 5 * 2);
- }
- void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry, int points)
- {
- if (rx == 0 || ry == 0)
- {
- rectangle(mode, x, y, w, h);
- return;
- }
- // Radius values that are more than half the rectangle's size aren't handled
- // correctly (for now)...
- if (w >= 0.02f)
- rx = std::min(rx, w / 2.0f - 0.01f);
- if (h >= 0.02f)
- ry = std::min(ry, h / 2.0f - 0.01f);
- points = std::max(points / 4, 1);
- const float half_pi = static_cast<float>(LOVE_M_PI / 2);
- float angle_shift = half_pi / ((float) points + 1.0f);
- int num_coords = (points + 2) * 8;
- float *coords = getScratchBuffer<float>(num_coords + 2);
- float phi = .0f;
- for (int i = 0; i <= points + 2; ++i, phi += angle_shift)
- {
- coords[2 * i + 0] = x + rx * (1 - cosf(phi));
- coords[2 * i + 1] = y + ry * (1 - sinf(phi));
- }
- phi = half_pi;
- for (int i = points + 2; i <= 2 * (points + 2); ++i, phi += angle_shift)
- {
- coords[2 * i + 0] = x + w - rx * (1 + cosf(phi));
- coords[2 * i + 1] = y + ry * (1 - sinf(phi));
- }
- phi = 2 * half_pi;
- for (int i = 2 * (points + 2); i <= 3 * (points + 2); ++i, phi += angle_shift)
- {
- coords[2 * i + 0] = x + w - rx * (1 + cosf(phi));
- coords[2 * i + 1] = y + h - ry * (1 + sinf(phi));
- }
- phi = 3 * half_pi;
- for (int i = 3 * (points + 2); i <= 4 * (points + 2); ++i, phi += angle_shift)
- {
- coords[2 * i + 0] = x + rx * (1 - cosf(phi));
- coords[2 * i + 1] = y + h - ry * (1 + sinf(phi));
- }
- coords[num_coords + 0] = coords[0];
- coords[num_coords + 1] = coords[1];
- polygon(mode, coords, num_coords + 2);
- }
- void Graphics::rectangle(DrawMode mode, float x, float y, float w, float h, float rx, float ry)
- {
- rectangle(mode, x, y, w, h, rx, ry, calculateEllipsePoints(rx, ry));
- }
- void Graphics::circle(DrawMode mode, float x, float y, float radius, int points)
- {
- ellipse(mode, x, y, radius, radius, points);
- }
- void Graphics::circle(DrawMode mode, float x, float y, float radius)
- {
- ellipse(mode, x, y, radius, radius);
- }
- void Graphics::ellipse(DrawMode mode, float x, float y, float a, float b, int points)
- {
- float two_pi = (float) (LOVE_M_PI * 2);
- if (points <= 0) points = 1;
- float angle_shift = (two_pi / points);
- float phi = .0f;
- float *coords = getScratchBuffer<float>(2 * (points + 1));
- for (int i = 0; i < points; ++i, phi += angle_shift)
- {
- coords[2*i+0] = x + a * cosf(phi);
- coords[2*i+1] = y + b * sinf(phi);
- }
- coords[2*points+0] = coords[0];
- coords[2*points+1] = coords[1];
- polygon(mode, coords, (points + 1) * 2);
- }
- void Graphics::ellipse(DrawMode mode, float x, float y, float a, float b)
- {
- ellipse(mode, x, y, a, b, calculateEllipsePoints(a, b));
- }
- void Graphics::arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2, int points)
- {
- // Nothing to display with no points or equal angles. (Or is there with line mode?)
- if (points <= 0 || angle1 == angle2)
- return;
- // Oh, you want to draw a circle?
- if (fabs(angle1 - angle2) >= 2.0f * (float) LOVE_M_PI)
- {
- circle(drawmode, x, y, radius, points);
- return;
- }
- float angle_shift = (angle2 - angle1) / points;
- // Bail on precision issues.
- if (angle_shift == 0.0)
- return;
- // Prevent the connecting line from being drawn if a closed line arc has a
- // small angle. Avoids some visual issues when connected lines are at sharp
- // angles, due to the miter line join drawing code.
- if (drawmode == DRAW_LINE && arcmode == ARC_CLOSED && fabsf(angle1 - angle2) < LOVE_TORAD(4))
- arcmode = ARC_OPEN;
- // Quick fix for the last part of a filled open arc not being drawn (because
- // polygon(DRAW_FILL, ...) doesn't work without a closed loop of vertices.)
- if (drawmode == DRAW_FILL && arcmode == ARC_OPEN)
- arcmode = ARC_CLOSED;
- float phi = angle1;
- float *coords = nullptr;
- int num_coords = 0;
- const auto createPoints = [&](float *coordinates)
- {
- for (int i = 0; i <= points; ++i, phi += angle_shift)
- {
- coordinates[2 * i + 0] = x + radius * cosf(phi);
- coordinates[2 * i + 1] = y + radius * sinf(phi);
- }
- };
- if (arcmode == ARC_PIE)
- {
- num_coords = (points + 3) * 2;
- coords = getScratchBuffer<float>(num_coords);
- coords[0] = coords[num_coords - 2] = x;
- coords[1] = coords[num_coords - 1] = y;
- createPoints(coords + 2);
- }
- else if (arcmode == ARC_OPEN)
- {
- num_coords = (points + 1) * 2;
- coords = getScratchBuffer<float>(num_coords);
- createPoints(coords);
- }
- else // ARC_CLOSED
- {
- num_coords = (points + 2) * 2;
- coords = getScratchBuffer<float>(num_coords);
- createPoints(coords);
- // Connect the ends of the arc.
- coords[num_coords - 2] = coords[0];
- coords[num_coords - 1] = coords[1];
- }
- polygon(drawmode, coords, num_coords);
- }
- void Graphics::arc(DrawMode drawmode, ArcMode arcmode, float x, float y, float radius, float angle1, float angle2)
- {
- float points = (float) calculateEllipsePoints(radius, radius);
- // The amount of points is based on the fraction of the circle created by the arc.
- float angle = fabsf(angle1 - angle2);
- if (angle < 2.0f * (float) LOVE_M_PI)
- points *= angle / (2.0f * (float) LOVE_M_PI);
- arc(drawmode, arcmode, x, y, radius, angle1, angle2, (int) (points + 0.5f));
- }
- /// @param mode the draw mode
- /// @param coords the coordinate array
- /// @param count the number of coordinates/size of the array
- void Graphics::polygon(DrawMode mode, const float *coords, size_t count)
- {
- // coords is an array of a closed loop of vertices, i.e.
- // coords[count-2] = coords[0], coords[count-1] = coords[1]
- if (mode == DRAW_LINE)
- {
- polyline(coords, count);
- }
- else
- {
- StreamDrawRequest req;
- req.formats[0] = vertex::CommonFormat::XYf;
- req.formats[1] = vertex::CommonFormat::RGBAub;
- req.indexMode = vertex::TriangleIndexMode::FAN;
- req.vertexCount = (int)count/2 - 1;
- StreamVertexData data = requestStreamDraw(req);
-
- const Matrix4 &t = getTransform();
- t.transform((Vector *) data.stream[0], (const Vector *) coords, req.vertexCount);
-
- Color c = toColor(getColor());
- Color *colordata = (Color *) data.stream[1];
- for (int i = 0; i < req.vertexCount; i++)
- colordata[i] = c;
- }
- }
- void Graphics::push(StackType type)
- {
- if (stackTypeStack.size() == MAX_USER_STACK_DEPTH)
- throw Exception("Maximum stack depth reached (more pushes than pops?)");
- pushTransform();
- pixelScaleStack.push_back(pixelScaleStack.back());
- if (type == STACK_ALL)
- states.push_back(states.back());
- stackTypeStack.push_back(type);
- }
- void Graphics::pop()
- {
- if (stackTypeStack.size() < 1)
- throw Exception("Minimum stack depth reached (more pops than pushes?)");
- popTransform();
- pixelScaleStack.pop_back();
- if (stackTypeStack.back() == STACK_ALL)
- {
- DisplayState &newstate = states[states.size() - 2];
- restoreStateChecked(newstate);
- // The last two states in the stack should be equal now.
- states.pop_back();
- }
-
- stackTypeStack.pop_back();
- }
- /**
- * Transform and stack functions.
- **/
- const Matrix4 &Graphics::getTransform() const
- {
- return transformStack.back();
- }
- const Matrix4 &Graphics::getProjection() const
- {
- return projectionMatrix;
- }
- void Graphics::pushTransform()
- {
- transformStack.push_back(transformStack.back());
- }
- void Graphics::pushIdentityTransform()
- {
- transformStack.push_back(Matrix4());
- }
- void Graphics::popTransform()
- {
- transformStack.pop_back();
- }
- void Graphics::rotate(float r)
- {
- transformStack.back().rotate(r);
- }
- void Graphics::scale(float x, float y)
- {
- transformStack.back().scale(x, y);
- pixelScaleStack.back() *= (fabs(x) + fabs(y)) / 2.0;
- }
- void Graphics::translate(float x, float y)
- {
- transformStack.back().translate(x, y);
- }
- void Graphics::shear(float kx, float ky)
- {
- transformStack.back().shear(kx, ky);
- }
- void Graphics::origin()
- {
- transformStack.back().setIdentity();
- pixelScaleStack.back() = 1;
- }
- void Graphics::applyTransform(love::math::Transform *transform)
- {
- Matrix4 &m = transformStack.back();
- m *= transform->getMatrix();
- float sx, sy;
- m.getApproximateScale(sx, sy);
- pixelScaleStack.back() = (sx + sy) / 2.0;
- }
- void Graphics::replaceTransform(love::math::Transform *transform)
- {
- const Matrix4 &m = transform->getMatrix();
- transformStack.back() = m;
- float sx, sy;
- m.getApproximateScale(sx, sy);
- pixelScaleStack.back() = (sx + sy) / 2.0;
- }
- Vector Graphics::transformPoint(Vector point)
- {
- Vector p;
- transformStack.back().transform(&p, &point, 1);
- return p;
- }
- Vector Graphics::inverseTransformPoint(Vector point)
- {
- Vector p;
- // TODO: We should probably cache the inverse transform so we don't have to
- // re-calculate it every time this is called.
- transformStack.back().inverse().transform(&p, &point, 1);
- return p;
- }
- const Shader::ShaderSource &Graphics::getCurrentDefaultShaderCode() const
- {
- return defaultShaderCode[getShaderLanguageTarget()][isGammaCorrect() ? 1 : 0];
- }
- /**
- * Constants.
- **/
- bool Graphics::getConstant(const char *in, DrawMode &out)
- {
- return drawModes.find(in, out);
- }
- bool Graphics::getConstant(DrawMode in, const char *&out)
- {
- return drawModes.find(in, out);
- }
- bool Graphics::getConstant(const char *in, ArcMode &out)
- {
- return arcModes.find(in, out);
- }
- bool Graphics::getConstant(ArcMode in, const char *&out)
- {
- return arcModes.find(in, out);
- }
- bool Graphics::getConstant(const char *in, BlendMode &out)
- {
- return blendModes.find(in, out);
- }
- bool Graphics::getConstant(BlendMode in, const char *&out)
- {
- return blendModes.find(in, out);
- }
- bool Graphics::getConstant(const char *in, BlendAlpha &out)
- {
- return blendAlphaModes.find(in, out);
- }
- bool Graphics::getConstant(BlendAlpha in, const char *&out)
- {
- return blendAlphaModes.find(in, out);
- }
- bool Graphics::getConstant(const char *in, LineStyle &out)
- {
- return lineStyles.find(in, out);
- }
- bool Graphics::getConstant(LineStyle in, const char *&out)
- {
- return lineStyles.find(in, out);
- }
- bool Graphics::getConstant(const char *in, LineJoin &out)
- {
- return lineJoins.find(in, out);
- }
- bool Graphics::getConstant(LineJoin in, const char *&out)
- {
- return lineJoins.find(in, out);
- }
- bool Graphics::getConstant(const char *in, StencilAction &out)
- {
- return stencilActions.find(in, out);
- }
- bool Graphics::getConstant(StencilAction in, const char *&out)
- {
- return stencilActions.find(in, out);
- }
- bool Graphics::getConstant(const char *in, CompareMode &out)
- {
- return compareModes.find(in, out);
- }
- bool Graphics::getConstant(CompareMode in, const char *&out)
- {
- return compareModes.find(in, out);
- }
- bool Graphics::getConstant(const char *in, Feature &out)
- {
- return features.find(in, out);
- }
- bool Graphics::getConstant(Feature in, const char *&out)
- {
- return features.find(in, out);
- }
- bool Graphics::getConstant(const char *in, SystemLimit &out)
- {
- return systemLimits.find(in, out);
- }
- bool Graphics::getConstant(SystemLimit in, const char *&out)
- {
- return systemLimits.find(in, out);
- }
- bool Graphics::getConstant(const char *in, StackType &out)
- {
- return stackTypes.find(in, out);
- }
- bool Graphics::getConstant(StackType in, const char *&out)
- {
- return stackTypes.find(in, out);
- }
- StringMap<Graphics::DrawMode, Graphics::DRAW_MAX_ENUM>::Entry Graphics::drawModeEntries[] =
- {
- { "line", DRAW_LINE },
- { "fill", DRAW_FILL },
- };
- StringMap<Graphics::DrawMode, Graphics::DRAW_MAX_ENUM> Graphics::drawModes(Graphics::drawModeEntries, sizeof(Graphics::drawModeEntries));
- StringMap<Graphics::ArcMode, Graphics::ARC_MAX_ENUM>::Entry Graphics::arcModeEntries[] =
- {
- { "open", ARC_OPEN },
- { "closed", ARC_CLOSED },
- { "pie", ARC_PIE },
- };
- StringMap<Graphics::ArcMode, Graphics::ARC_MAX_ENUM> Graphics::arcModes(Graphics::arcModeEntries, sizeof(Graphics::arcModeEntries));
- StringMap<Graphics::BlendMode, Graphics::BLEND_MAX_ENUM>::Entry Graphics::blendModeEntries[] =
- {
- { "alpha", BLEND_ALPHA },
- { "add", BLEND_ADD },
- { "subtract", BLEND_SUBTRACT },
- { "multiply", BLEND_MULTIPLY },
- { "lighten", BLEND_LIGHTEN },
- { "darken", BLEND_DARKEN },
- { "screen", BLEND_SCREEN },
- { "replace", BLEND_REPLACE },
- { "none", BLEND_NONE },
- };
- StringMap<Graphics::BlendMode, Graphics::BLEND_MAX_ENUM> Graphics::blendModes(Graphics::blendModeEntries, sizeof(Graphics::blendModeEntries));
- StringMap<Graphics::BlendAlpha, Graphics::BLENDALPHA_MAX_ENUM>::Entry Graphics::blendAlphaEntries[] =
- {
- { "alphamultiply", BLENDALPHA_MULTIPLY },
- { "premultiplied", BLENDALPHA_PREMULTIPLIED },
- };
- StringMap<Graphics::BlendAlpha, Graphics::BLENDALPHA_MAX_ENUM> Graphics::blendAlphaModes(Graphics::blendAlphaEntries, sizeof(Graphics::blendAlphaEntries));
- StringMap<Graphics::LineStyle, Graphics::LINE_MAX_ENUM>::Entry Graphics::lineStyleEntries[] =
- {
- { "smooth", LINE_SMOOTH },
- { "rough", LINE_ROUGH }
- };
- StringMap<Graphics::LineStyle, Graphics::LINE_MAX_ENUM> Graphics::lineStyles(Graphics::lineStyleEntries, sizeof(Graphics::lineStyleEntries));
- StringMap<Graphics::LineJoin, Graphics::LINE_JOIN_MAX_ENUM>::Entry Graphics::lineJoinEntries[] =
- {
- { "none", LINE_JOIN_NONE },
- { "miter", LINE_JOIN_MITER },
- { "bevel", LINE_JOIN_BEVEL }
- };
- StringMap<Graphics::LineJoin, Graphics::LINE_JOIN_MAX_ENUM> Graphics::lineJoins(Graphics::lineJoinEntries, sizeof(Graphics::lineJoinEntries));
- StringMap<Graphics::StencilAction, Graphics::STENCIL_MAX_ENUM>::Entry Graphics::stencilActionEntries[] =
- {
- { "replace", STENCIL_REPLACE },
- { "increment", STENCIL_INCREMENT },
- { "decrement", STENCIL_DECREMENT },
- { "incrementwrap", STENCIL_INCREMENT_WRAP },
- { "decrementwrap", STENCIL_DECREMENT_WRAP },
- { "invert", STENCIL_INVERT },
- };
- StringMap<Graphics::StencilAction, Graphics::STENCIL_MAX_ENUM> Graphics::stencilActions(Graphics::stencilActionEntries, sizeof(Graphics::stencilActionEntries));
- StringMap<Graphics::CompareMode, Graphics::COMPARE_MAX_ENUM>::Entry Graphics::compareModeEntries[] =
- {
- { "less", COMPARE_LESS },
- { "lequal", COMPARE_LEQUAL },
- { "equal", COMPARE_EQUAL },
- { "gequal", COMPARE_GEQUAL },
- { "greater", COMPARE_GREATER },
- { "notequal", COMPARE_NOTEQUAL },
- { "always", COMPARE_ALWAYS },
- };
- StringMap<Graphics::CompareMode, Graphics::COMPARE_MAX_ENUM> Graphics::compareModes(Graphics::compareModeEntries, sizeof(Graphics::compareModeEntries));
- StringMap<Graphics::Feature, Graphics::FEATURE_MAX_ENUM>::Entry Graphics::featureEntries[] =
- {
- { "multicanvasformats", FEATURE_MULTI_CANVAS_FORMATS },
- { "clampzero", FEATURE_CLAMP_ZERO },
- { "lighten", FEATURE_LIGHTEN },
- { "fullnpot", FEATURE_FULL_NPOT },
- { "pixelshaderhighp", FEATURE_PIXEL_SHADER_HIGHP },
- { "glsl3", FEATURE_GLSL3 },
- { "instancing", FEATURE_INSTANCING },
- };
- StringMap<Graphics::Feature, Graphics::FEATURE_MAX_ENUM> Graphics::features(Graphics::featureEntries, sizeof(Graphics::featureEntries));
- StringMap<Graphics::SystemLimit, Graphics::LIMIT_MAX_ENUM>::Entry Graphics::systemLimitEntries[] =
- {
- { "pointsize", LIMIT_POINT_SIZE },
- { "texturesize", LIMIT_TEXTURE_SIZE },
- { "multicanvas", LIMIT_MULTI_CANVAS },
- { "canvasmsaa", LIMIT_CANVAS_MSAA },
- { "anisotropy", LIMIT_ANISOTROPY },
- };
- StringMap<Graphics::SystemLimit, Graphics::LIMIT_MAX_ENUM> Graphics::systemLimits(Graphics::systemLimitEntries, sizeof(Graphics::systemLimitEntries));
- StringMap<Graphics::StackType, Graphics::STACK_MAX_ENUM>::Entry Graphics::stackTypeEntries[] =
- {
- { "all", STACK_ALL },
- { "transform", STACK_TRANSFORM },
- };
- StringMap<Graphics::StackType, Graphics::STACK_MAX_ENUM> Graphics::stackTypes(Graphics::stackTypeEntries, sizeof(Graphics::stackTypeEntries));
- } // graphics
- } // love
|