mirror of
https://github.com/love2d/love.git
synced 2026-08-16 00:02:12 +02:00
Merged default into minor
--HG-- branch : minor
This commit is contained in:
@@ -44,20 +44,15 @@ namespace opengl
|
||||
{
|
||||
|
||||
Graphics::Graphics()
|
||||
: currentFont(0)
|
||||
, lineStyle(LINE_SMOOTH)
|
||||
, lineJoin(LINE_JOIN_MITER)
|
||||
, lineWidth(1)
|
||||
, matrixLimit(0)
|
||||
, userMatrices(0)
|
||||
, colorMask()
|
||||
, width(0)
|
||||
: width(0)
|
||||
, height(0)
|
||||
, created(false)
|
||||
, activeStencil(false)
|
||||
, savedState()
|
||||
, displayedMinReqWarning(false)
|
||||
{
|
||||
states.reserve(10);
|
||||
states.push_back(DisplayState());
|
||||
|
||||
currentWindow = love::window::sdl::Window::createSingleton();
|
||||
|
||||
int w, h;
|
||||
@@ -71,8 +66,8 @@ Graphics::Graphics()
|
||||
|
||||
Graphics::~Graphics()
|
||||
{
|
||||
if (currentFont != 0)
|
||||
currentFont->release();
|
||||
// We do this manually so the love objects get released before the window.
|
||||
states.clear();
|
||||
|
||||
currentWindow->release();
|
||||
}
|
||||
@@ -82,50 +77,86 @@ const char *Graphics::getName() const
|
||||
return "love.graphics.opengl";
|
||||
}
|
||||
|
||||
DisplayState Graphics::saveState()
|
||||
{
|
||||
DisplayState s;
|
||||
|
||||
s.color = getColor();
|
||||
s.backgroundColor = getBackgroundColor();
|
||||
|
||||
s.blendMode = getBlendMode();
|
||||
//get line style
|
||||
s.lineStyle = getLineStyle();
|
||||
s.lineJoin = getLineJoin();
|
||||
//get the point size
|
||||
glGetFloatv(GL_POINT_SIZE, &s.pointSize);
|
||||
//get scissor status
|
||||
s.scissor = (glIsEnabled(GL_SCISSOR_TEST) == GL_TRUE);
|
||||
//do we have scissor, if so, store the box
|
||||
if (s.scissor)
|
||||
s.scissorBox = gl.getScissor();
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
s.colorMask[i] = colorMask[i];
|
||||
|
||||
wireframe = isWireframe();
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
void Graphics::restoreState(const DisplayState &s)
|
||||
{
|
||||
setColor(s.color);
|
||||
setBackgroundColor(s.backgroundColor);
|
||||
|
||||
setBlendMode(s.blendMode);
|
||||
setLineWidth(lineWidth);
|
||||
|
||||
setLineWidth(s.lineWidth);
|
||||
setLineStyle(s.lineStyle);
|
||||
setLineJoin(s.lineJoin);
|
||||
|
||||
setPointSize(s.pointSize);
|
||||
|
||||
if (s.scissor)
|
||||
setScissor(s.scissorBox.x, s.scissorBox.y, s.scissorBox.w, s.scissorBox.h);
|
||||
else
|
||||
setScissor();
|
||||
setColorMask(s.colorMask[0], s.colorMask[1], s.colorMask[2], s.colorMask[3]);
|
||||
|
||||
setFont(s.font.get());
|
||||
setShader(s.shader.get());
|
||||
setCanvas(s.canvases);
|
||||
|
||||
setColorMask(s.colorMask);
|
||||
setWireframe(s.wireframe);
|
||||
}
|
||||
|
||||
void Graphics::restoreStateChecked(const DisplayState &s)
|
||||
{
|
||||
const DisplayState &cur = states.back();
|
||||
|
||||
if (*(uint32 *) &s.color.r != *(uint32 *) &cur.color.r)
|
||||
setColor(s.color);
|
||||
|
||||
if (*(uint32 *) &s.backgroundColor.r != *(uint32 *) &cur.backgroundColor.r)
|
||||
setBackgroundColor(s.backgroundColor);
|
||||
|
||||
if (s.blendMode != cur.blendMode)
|
||||
setBlendMode(s.blendMode);
|
||||
|
||||
// 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.scissorBox == cur.scissorBox)))
|
||||
{
|
||||
if (s.scissor)
|
||||
setScissor(s.scissorBox.x, s.scissorBox.y, s.scissorBox.w, s.scissorBox.h);
|
||||
else
|
||||
setScissor();
|
||||
}
|
||||
|
||||
setFont(s.font.get());
|
||||
setShader(s.shader.get());
|
||||
|
||||
for (size_t i = 0; i < s.canvases.size() && i < cur.canvases.size(); i++)
|
||||
{
|
||||
if (s.canvases[i].get() != cur.canvases[i].get())
|
||||
{
|
||||
setCanvas(s.canvases);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
if (s.colorMask[i] != cur.colorMask[i])
|
||||
{
|
||||
setColorMask(s.colorMask);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (s.wireframe != cur.wireframe)
|
||||
setWireframe(s.wireframe);
|
||||
}
|
||||
|
||||
void Graphics::setViewportSize(int width, int height)
|
||||
{
|
||||
this->width = width;
|
||||
@@ -136,8 +167,8 @@ void Graphics::setViewportSize(int width, int height)
|
||||
|
||||
// We want to affect the main screen, not any Canvas that's currently active
|
||||
// (not that any *should* be active when this is called.)
|
||||
Canvas *c = Canvas::current;
|
||||
Canvas::bindDefaultCanvas();
|
||||
std::vector<Canvas *> canvases = getCanvas();
|
||||
setCanvas();
|
||||
|
||||
// Set the viewport to top-left corner.
|
||||
gl.setViewport(OpenGL::Viewport(0, 0, width, height));
|
||||
@@ -146,18 +177,11 @@ void Graphics::setViewportSize(int width, int height)
|
||||
// made aware of the new system viewport size.
|
||||
Canvas::systemViewport = gl.getViewport();
|
||||
|
||||
// Reset the projection matrix
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
|
||||
// Set up orthographic view (no depth)
|
||||
glOrtho(0.0, width, height, 0.0, -1.0, 1.0);
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
// Set up the projection matrix
|
||||
gl.matrices.projection.back() = Matrix::ortho(0.0, width, height, 0.0);
|
||||
|
||||
// Restore the previously active Canvas.
|
||||
if (c != nullptr)
|
||||
c->startGrab(c->getAttachedCanvases());
|
||||
setCanvas(canvases);
|
||||
}
|
||||
|
||||
bool Graphics::setMode(int width, int height, bool &sRGB)
|
||||
@@ -202,7 +226,8 @@ bool Graphics::setMode(int width, int height, bool &sRGB)
|
||||
glEnable(GL_BLEND);
|
||||
|
||||
// Enable all color component writes.
|
||||
setColorMask(true, true, true, true);
|
||||
bool colormask[] = {true, true, true, true};
|
||||
setColorMask(colormask);
|
||||
|
||||
// Auto-generated mipmaps should be the best quality possible
|
||||
glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST);
|
||||
@@ -211,28 +236,9 @@ bool Graphics::setMode(int width, int height, bool &sRGB)
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
gl.setTextureUnit(0);
|
||||
|
||||
// Reset modelview matrix
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
|
||||
// Set pixel row alignment
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
|
||||
// Reload all volatile objects.
|
||||
if (!Volatile::loadAll())
|
||||
std::cerr << "Could not reload all volatile objects." << std::endl;
|
||||
|
||||
// Restore the display state.
|
||||
restoreState(savedState);
|
||||
pixel_size_stack.clear();
|
||||
pixel_size_stack.reserve(5);
|
||||
pixel_size_stack.push_back(1);
|
||||
|
||||
// Get the maximum number of matrices
|
||||
// subtract a few to give the engine some room.
|
||||
glGetIntegerv(GL_MAX_MODELVIEW_STACK_DEPTH, &matrixLimit);
|
||||
matrixLimit -= 5;
|
||||
|
||||
// Set whether drawing converts input from linear -> sRGB colorspace.
|
||||
if (GLEE_VERSION_3_0 || GLEE_ARB_framebuffer_sRGB || GLEE_EXT_framebuffer_sRGB)
|
||||
{
|
||||
@@ -258,6 +264,17 @@ bool Graphics::setMode(int width, int height, bool &sRGB)
|
||||
|
||||
setDebug(enabledebug);
|
||||
|
||||
// Reload all volatile objects.
|
||||
if (!Volatile::loadAll())
|
||||
std::cerr << "Could not reload all volatile objects." << std::endl;
|
||||
|
||||
// Restore the graphics state.
|
||||
restoreState(states.back());
|
||||
|
||||
pixel_size_stack.clear();
|
||||
pixel_size_stack.reserve(5);
|
||||
pixel_size_stack.push_back(1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -266,9 +283,6 @@ void Graphics::unSetMode()
|
||||
if (!isCreated())
|
||||
return;
|
||||
|
||||
// Window re-creation may destroy the GL context, so we must save the state.
|
||||
savedState = saveState();
|
||||
|
||||
// Unload all volatile objects. These must be reloaded after the display
|
||||
// mode change.
|
||||
Volatile::unloadAll();
|
||||
@@ -337,8 +351,6 @@ void Graphics::reset()
|
||||
{
|
||||
DisplayState s;
|
||||
discardStencil();
|
||||
Canvas::bindDefaultCanvas();
|
||||
Shader::detach();
|
||||
origin();
|
||||
restoreState(s);
|
||||
}
|
||||
@@ -370,13 +382,18 @@ bool Graphics::isCreated() const
|
||||
|
||||
void Graphics::setScissor(int x, int y, int width, int height)
|
||||
{
|
||||
OpenGL::Viewport box(x, y, width, height);
|
||||
|
||||
states.back().scissor = true;
|
||||
glEnable(GL_SCISSOR_TEST);
|
||||
// OpenGL's reversed y-coordinate is compensated for in OpenGL::setScissor.
|
||||
gl.setScissor(OpenGL::Viewport(x, y, width, height));
|
||||
gl.setScissor(box);
|
||||
states.back().scissorBox = box;
|
||||
}
|
||||
|
||||
void Graphics::setScissor()
|
||||
{
|
||||
states.back().scissor = false;
|
||||
glDisable(GL_SCISSOR_TEST);
|
||||
}
|
||||
|
||||
@@ -389,7 +406,7 @@ bool Graphics::getScissor(int &x, int &y, int &width, int &height) const
|
||||
width = scissor.w;
|
||||
height = scissor.h;
|
||||
|
||||
return glIsEnabled(GL_SCISSOR_TEST) == GL_TRUE;
|
||||
return states.back().scissor;
|
||||
}
|
||||
|
||||
void Graphics::defineStencil()
|
||||
@@ -413,7 +430,7 @@ void Graphics::useStencil(bool invert)
|
||||
{
|
||||
glStencilFunc(GL_EQUAL, (GLint)(!invert), 1); // invert ? 0 : 1
|
||||
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
|
||||
setColorMask(colorMask[0], colorMask[1], colorMask[2], colorMask[3]);
|
||||
setColorMask(states.back().colorMask);
|
||||
}
|
||||
|
||||
void Graphics::discardStencil()
|
||||
@@ -421,7 +438,7 @@ void Graphics::discardStencil()
|
||||
if (!activeStencil)
|
||||
return;
|
||||
|
||||
setColorMask(colorMask[0], colorMask[1], colorMask[2], colorMask[3]);
|
||||
setColorMask(states.back().colorMask);
|
||||
glDisable(GL_STENCIL_TEST);
|
||||
activeStencil = false;
|
||||
}
|
||||
@@ -580,119 +597,186 @@ Mesh *Graphics::newMesh(int vertexcount, Mesh::DrawMode mode)
|
||||
void Graphics::setColor(const Color &c)
|
||||
{
|
||||
gl.setColor(c);
|
||||
states.back().color = c;
|
||||
}
|
||||
|
||||
Color Graphics::getColor() const
|
||||
{
|
||||
return gl.getColor();
|
||||
return states.back().color;
|
||||
}
|
||||
|
||||
void Graphics::setBackgroundColor(const Color &c)
|
||||
{
|
||||
gl.setClearColor(c);
|
||||
states.back().backgroundColor = c;
|
||||
}
|
||||
|
||||
Color Graphics::getBackgroundColor() const
|
||||
{
|
||||
return gl.getClearColor();
|
||||
return states.back().backgroundColor;
|
||||
}
|
||||
|
||||
void Graphics::setFont(Font *font)
|
||||
{
|
||||
Object::AutoRelease fontrelease(currentFont);
|
||||
|
||||
currentFont = font;
|
||||
|
||||
if (font != 0)
|
||||
currentFont->retain();
|
||||
DisplayState &state = states.back();
|
||||
state.font.set(font);
|
||||
}
|
||||
|
||||
Font *Graphics::getFont() const
|
||||
{
|
||||
return currentFont;
|
||||
return states.back().font.get();
|
||||
}
|
||||
|
||||
void Graphics::setColorMask(bool r, bool g, bool b, bool a)
|
||||
void Graphics::setShader(Shader *shader)
|
||||
{
|
||||
colorMask[0] = r;
|
||||
colorMask[1] = g;
|
||||
colorMask[2] = b;
|
||||
colorMask[3] = a;
|
||||
if (shader == nullptr)
|
||||
return setShader();
|
||||
|
||||
glColorMask((GLboolean) r, (GLboolean) g, (GLboolean) b, (GLboolean) a);
|
||||
DisplayState &state = states.back();
|
||||
|
||||
shader->attach();
|
||||
|
||||
state.shader.set(shader);
|
||||
}
|
||||
|
||||
void Graphics::setShader()
|
||||
{
|
||||
DisplayState &state = states.back();
|
||||
|
||||
Shader::detach();
|
||||
|
||||
state.shader.set(nullptr);
|
||||
}
|
||||
|
||||
Shader *Graphics::getShader() const
|
||||
{
|
||||
return states.back().shader.get();
|
||||
}
|
||||
|
||||
void Graphics::setCanvas(Canvas *canvas)
|
||||
{
|
||||
if (canvas == nullptr)
|
||||
return setCanvas();
|
||||
|
||||
DisplayState &state = states.back();
|
||||
|
||||
canvas->startGrab();
|
||||
|
||||
std::vector<Object::StrongRef<Canvas>> canvasref;
|
||||
canvasref.push_back(canvas);
|
||||
|
||||
std::swap(state.canvases, canvasref);
|
||||
}
|
||||
|
||||
void Graphics::setCanvas(const std::vector<Canvas *> &canvases)
|
||||
{
|
||||
if (canvases.size() == 0)
|
||||
return setCanvas();
|
||||
else if (canvases.size() == 1)
|
||||
return setCanvas(canvases[0]);
|
||||
|
||||
DisplayState &state = states.back();
|
||||
|
||||
auto attachments = std::vector<Canvas *>(canvases.begin() + 1, canvases.end());
|
||||
canvases[0]->startGrab(attachments);
|
||||
|
||||
std::vector<Object::StrongRef<Canvas>> canvasrefs;
|
||||
canvasrefs.reserve(canvases.size());
|
||||
|
||||
for (Canvas *c : canvases)
|
||||
canvasrefs.push_back(c);
|
||||
|
||||
std::swap(state.canvases, canvasrefs);
|
||||
}
|
||||
|
||||
void Graphics::setCanvas(const std::vector<Object::StrongRef<Canvas>> &canvases)
|
||||
{
|
||||
std::vector<Canvas *> canvaslist;
|
||||
canvaslist.reserve(canvases.size());
|
||||
|
||||
for (const Object::StrongRef<Canvas> &c : canvases)
|
||||
canvaslist.push_back(c.get());
|
||||
|
||||
return setCanvas(canvaslist);
|
||||
}
|
||||
|
||||
void Graphics::setCanvas()
|
||||
{
|
||||
DisplayState &state = states.back();
|
||||
|
||||
if (Canvas::current != nullptr)
|
||||
Canvas::current->stopGrab();
|
||||
|
||||
state.canvases.clear();
|
||||
}
|
||||
|
||||
std::vector<Canvas *> Graphics::getCanvas() const
|
||||
{
|
||||
std::vector<Canvas *> canvases;
|
||||
canvases.reserve(states.back().canvases.size());
|
||||
|
||||
for (const Object::StrongRef<Canvas> &c : states.back().canvases)
|
||||
canvases.push_back(c.get());
|
||||
|
||||
return canvases;
|
||||
}
|
||||
|
||||
void Graphics::setColorMask(const bool mask[4])
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
states.back().colorMask[i] = mask[i];
|
||||
|
||||
glColorMask(mask[0], mask[1], mask[2], mask[3]);
|
||||
}
|
||||
|
||||
const bool *Graphics::getColorMask() const
|
||||
{
|
||||
return colorMask;
|
||||
return states.back().colorMask;
|
||||
}
|
||||
|
||||
void Graphics::setBlendMode(Graphics::BlendMode mode)
|
||||
{
|
||||
OpenGL::BlendState state = {GL_ONE, GL_ONE, GL_ZERO, GL_ZERO, GL_FUNC_ADD};
|
||||
OpenGL::BlendState blend = {GL_ONE, GL_ONE, GL_ZERO, GL_ZERO, GL_FUNC_ADD};
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
case BLEND_ALPHA:
|
||||
state.srcRGB = GL_SRC_ALPHA;
|
||||
state.srcA = GL_ONE;
|
||||
state.dstRGB = state.dstA = GL_ONE_MINUS_SRC_ALPHA;
|
||||
blend.srcRGB = GL_SRC_ALPHA;
|
||||
blend.srcA = GL_ONE;
|
||||
blend.dstRGB = blend.dstA = GL_ONE_MINUS_SRC_ALPHA;
|
||||
break;
|
||||
case BLEND_MULTIPLY:
|
||||
state.srcRGB = state.srcA = GL_DST_COLOR;
|
||||
state.dstRGB = state.dstA = GL_ZERO;
|
||||
blend.srcRGB = blend.srcA = GL_DST_COLOR;
|
||||
blend.dstRGB = blend.dstA = GL_ZERO;
|
||||
break;
|
||||
case BLEND_PREMULTIPLIED:
|
||||
state.srcRGB = state.srcA = GL_ONE;
|
||||
state.dstRGB = state.dstA = GL_ONE_MINUS_SRC_ALPHA;
|
||||
blend.srcRGB = blend.srcA = GL_ONE;
|
||||
blend.dstRGB = blend.dstA = GL_ONE_MINUS_SRC_ALPHA;
|
||||
break;
|
||||
case BLEND_SUBTRACT:
|
||||
state.func = GL_FUNC_REVERSE_SUBTRACT;
|
||||
blend.func = GL_FUNC_REVERSE_SUBTRACT;
|
||||
case BLEND_ADD:
|
||||
state.srcRGB = state.srcA = GL_SRC_ALPHA;
|
||||
state.dstRGB = state.dstA = GL_ONE;
|
||||
blend.srcRGB = blend.srcA = GL_SRC_ALPHA;
|
||||
blend.dstRGB = blend.dstA = GL_ONE;
|
||||
break;
|
||||
case BLEND_SCREEN:
|
||||
state.srcRGB = state.srcA = GL_ONE;
|
||||
state.dstRGB = state.dstA = GL_ONE_MINUS_SRC_COLOR;
|
||||
blend.srcRGB = blend.srcA = GL_ONE;
|
||||
blend.dstRGB = blend.dstA = GL_ONE_MINUS_SRC_COLOR;
|
||||
break;
|
||||
case BLEND_REPLACE:
|
||||
default:
|
||||
state.srcRGB = state.srcA = GL_ONE;
|
||||
state.dstRGB = state.dstA = GL_ZERO;
|
||||
blend.srcRGB = blend.srcA = GL_ONE;
|
||||
blend.dstRGB = blend.dstA = GL_ZERO;
|
||||
break;
|
||||
}
|
||||
|
||||
gl.setBlendState(state);
|
||||
gl.setBlendState(blend);
|
||||
states.back().blendMode = mode;
|
||||
}
|
||||
|
||||
Graphics::BlendMode Graphics::getBlendMode() const
|
||||
{
|
||||
OpenGL::BlendState state = gl.getBlendState();
|
||||
|
||||
if (state.func == GL_FUNC_REVERSE_SUBTRACT) // && src == GL_SRC_ALPHA && dst == GL_ONE
|
||||
return BLEND_SUBTRACT;
|
||||
// Everything else has equation == GL_FUNC_ADD.
|
||||
else if (state.srcRGB == state.srcA && state.dstRGB == state.dstA)
|
||||
{
|
||||
if (state.srcRGB == GL_SRC_ALPHA && state.dstRGB == GL_ONE)
|
||||
return BLEND_ADD;
|
||||
else if (state.srcRGB == GL_SRC_ALPHA && state.dstRGB == GL_ONE_MINUS_SRC_ALPHA)
|
||||
return BLEND_ALPHA; // alpha blend mode fallback for very old OpenGL versions.
|
||||
else if (state.srcRGB == GL_DST_COLOR && state.dstRGB == GL_ZERO)
|
||||
return BLEND_MULTIPLY;
|
||||
else if (state.srcRGB == GL_ONE && state.dstRGB == GL_ONE_MINUS_SRC_ALPHA)
|
||||
return BLEND_PREMULTIPLIED;
|
||||
else if (state.srcRGB == GL_ONE && state.dstRGB == GL_ONE_MINUS_SRC_COLOR)
|
||||
return BLEND_SCREEN;
|
||||
else if (state.srcRGB == GL_ONE && state.dstRGB == GL_ZERO)
|
||||
return BLEND_REPLACE;
|
||||
}
|
||||
else if (state.srcRGB == GL_SRC_ALPHA && state.srcA == GL_ONE &&
|
||||
state.dstRGB == GL_ONE_MINUS_SRC_ALPHA && state.dstA == GL_ONE_MINUS_SRC_ALPHA)
|
||||
return BLEND_ALPHA;
|
||||
|
||||
throw Exception("Unknown blend mode");
|
||||
return states.back().blendMode;
|
||||
}
|
||||
|
||||
void Graphics::setDefaultFilter(const Texture::Filter &f)
|
||||
@@ -719,66 +803,69 @@ void Graphics::getDefaultMipmapFilter(Texture::FilterMode *filter, float *sharpn
|
||||
|
||||
void Graphics::setLineWidth(float width)
|
||||
{
|
||||
lineWidth = width;
|
||||
states.back().lineWidth = width;
|
||||
}
|
||||
|
||||
void Graphics::setLineStyle(Graphics::LineStyle style)
|
||||
{
|
||||
lineStyle = style;
|
||||
states.back().lineStyle = style;
|
||||
}
|
||||
|
||||
void Graphics::setLineJoin(Graphics::LineJoin join)
|
||||
{
|
||||
lineJoin = join;
|
||||
states.back().lineJoin = join;
|
||||
}
|
||||
|
||||
float Graphics::getLineWidth() const
|
||||
{
|
||||
return lineWidth;
|
||||
return states.back().lineWidth;
|
||||
}
|
||||
|
||||
Graphics::LineStyle Graphics::getLineStyle() const
|
||||
{
|
||||
return lineStyle;
|
||||
return states.back().lineStyle;
|
||||
}
|
||||
|
||||
Graphics::LineJoin Graphics::getLineJoin() const
|
||||
{
|
||||
return lineJoin;
|
||||
return states.back().lineJoin;
|
||||
}
|
||||
|
||||
void Graphics::setPointSize(float size)
|
||||
{
|
||||
glPointSize((GLfloat)size);
|
||||
glPointSize(size);
|
||||
states.back().pointSize = size;
|
||||
}
|
||||
|
||||
float Graphics::getPointSize() const
|
||||
{
|
||||
GLfloat size;
|
||||
glGetFloatv(GL_POINT_SIZE, &size);
|
||||
return (float)size;
|
||||
return states.back().pointSize;
|
||||
}
|
||||
|
||||
void Graphics::setWireframe(bool enable)
|
||||
{
|
||||
wireframe = enable;
|
||||
glPolygonMode(GL_FRONT_AND_BACK, enable ? GL_LINE : GL_FILL);
|
||||
states.back().wireframe = enable;
|
||||
}
|
||||
|
||||
bool Graphics::isWireframe() const
|
||||
{
|
||||
return wireframe;
|
||||
return states.back().wireframe;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (currentFont != nullptr)
|
||||
currentFont->print(str, x, y, 0.0, angle, sx, sy, ox, oy, kx, ky);
|
||||
DisplayState &state = states.back();
|
||||
|
||||
if (state.font.get() != nullptr)
|
||||
state.font->print(str, x, y, 0.0, angle, sx, sy, ox, oy, kx, ky);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (currentFont == nullptr)
|
||||
DisplayState &state = states.back();
|
||||
|
||||
if (state.font.get() == nullptr)
|
||||
return;
|
||||
|
||||
if (wrap < 0.0f)
|
||||
@@ -790,59 +877,49 @@ void Graphics::printf(const std::string &str, float x, float y, float wrap, Alig
|
||||
// wrappedlines indicates which lines were automatically wrapped. It's
|
||||
// guaranteed to have the same number of elements as lines_to_draw.
|
||||
vector<bool> wrappedlines;
|
||||
vector<string> lines_to_draw = currentFont->getWrap(str, wrap, 0, &wrappedlines);
|
||||
|
||||
glPushMatrix();
|
||||
vector<string> lines_to_draw = state.font->getWrap(str, wrap, 0, &wrappedlines);
|
||||
|
||||
static Matrix t;
|
||||
t.setTransformation(ceilf(x), ceilf(y), angle, sx, sy, ox, oy, kx, ky);
|
||||
glMultMatrixf((const GLfloat *)t.getElements());
|
||||
|
||||
OpenGL::TempTransform transform(gl);
|
||||
transform.get() *= t;
|
||||
|
||||
x = y = 0.0f;
|
||||
|
||||
try
|
||||
{
|
||||
// now for the actual printing
|
||||
vector<string>::const_iterator line_iter, line_end = lines_to_draw.end();
|
||||
float extra_spacing = 0.0f;
|
||||
int num_spaces = 0;
|
||||
int i = 0;
|
||||
// now for the actual printing
|
||||
vector<string>::const_iterator line_iter, line_end = lines_to_draw.end();
|
||||
float extra_spacing = 0.0f;
|
||||
int num_spaces = 0;
|
||||
int i = 0;
|
||||
|
||||
for (line_iter = lines_to_draw.begin(); line_iter != line_end; ++line_iter)
|
||||
for (line_iter = lines_to_draw.begin(); line_iter != line_end; ++line_iter)
|
||||
{
|
||||
float width = static_cast<float>(state.font->getWidth(*line_iter));
|
||||
switch (align)
|
||||
{
|
||||
float width = static_cast<float>(currentFont->getWidth(*line_iter));
|
||||
switch (align)
|
||||
{
|
||||
case ALIGN_RIGHT:
|
||||
currentFont->print(*line_iter, ceilf(x + (wrap - width)), ceilf(y), 0.0f);
|
||||
break;
|
||||
case ALIGN_CENTER:
|
||||
currentFont->print(*line_iter, ceilf(x + (wrap - width) / 2), ceilf(y), 0.0f);
|
||||
break;
|
||||
case ALIGN_JUSTIFY:
|
||||
num_spaces = std::count(line_iter->begin(), line_iter->end(), ' ');
|
||||
if (wrappedlines[i] && num_spaces >= 1)
|
||||
extra_spacing = (wrap - width) / float(num_spaces);
|
||||
else
|
||||
extra_spacing = 0.0f;
|
||||
currentFont->print(*line_iter, ceilf(x), ceilf(y), extra_spacing);
|
||||
break;
|
||||
case ALIGN_LEFT:
|
||||
default:
|
||||
currentFont->print(*line_iter, ceilf(x), ceilf(y), 0.0f);
|
||||
break;
|
||||
}
|
||||
y += currentFont->getHeight() * currentFont->getLineHeight();
|
||||
i++;
|
||||
case ALIGN_RIGHT:
|
||||
state.font->print(*line_iter, ceilf(x + (wrap - width)), ceilf(y), 0.0f);
|
||||
break;
|
||||
case ALIGN_CENTER:
|
||||
state.font->print(*line_iter, ceilf(x + (wrap - width) / 2), ceilf(y), 0.0f);
|
||||
break;
|
||||
case ALIGN_JUSTIFY:
|
||||
num_spaces = std::count(line_iter->begin(), line_iter->end(), ' ');
|
||||
if (wrappedlines[i] && num_spaces >= 1)
|
||||
extra_spacing = (wrap - width) / float(num_spaces);
|
||||
else
|
||||
extra_spacing = 0.0f;
|
||||
state.font->print(*line_iter, ceilf(x), ceilf(y), extra_spacing);
|
||||
break;
|
||||
case ALIGN_LEFT:
|
||||
default:
|
||||
state.font->print(*line_iter, ceilf(x), ceilf(y), 0.0f);
|
||||
break;
|
||||
}
|
||||
y += state.font->getHeight() * state.font->getLineHeight();
|
||||
i++;
|
||||
}
|
||||
catch (love::Exception &)
|
||||
{
|
||||
glPopMatrix();
|
||||
throw;
|
||||
}
|
||||
|
||||
glPopMatrix();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -860,22 +937,24 @@ void Graphics::point(float x, float y)
|
||||
|
||||
void Graphics::polyline(const float *coords, size_t count)
|
||||
{
|
||||
if (lineJoin == LINE_JOIN_NONE)
|
||||
DisplayState &state = states.back();
|
||||
|
||||
if (state.lineJoin == LINE_JOIN_NONE)
|
||||
{
|
||||
NoneJoinPolyline line;
|
||||
line.render(coords, count, lineWidth * .5f, float(pixel_size_stack.back()), lineStyle == LINE_SMOOTH);
|
||||
line.render(coords, count, state.lineWidth * .5f, float(pixel_size_stack.back()), state.lineStyle == LINE_SMOOTH);
|
||||
line.draw();
|
||||
}
|
||||
else if (lineJoin == LINE_JOIN_BEVEL)
|
||||
else if (state.lineJoin == LINE_JOIN_BEVEL)
|
||||
{
|
||||
BevelJoinPolyline line;
|
||||
line.render(coords, count, lineWidth * .5f, float(pixel_size_stack.back()), lineStyle == LINE_SMOOTH);
|
||||
line.render(coords, count, state.lineWidth * .5f, float(pixel_size_stack.back()), state.lineStyle == LINE_SMOOTH);
|
||||
line.draw();
|
||||
}
|
||||
else // LINE_JOIN_MITER
|
||||
{
|
||||
MiterJoinPolyline line;
|
||||
line.render(coords, count, lineWidth * .5f, float(pixel_size_stack.back()), lineStyle == LINE_SMOOTH);
|
||||
line.render(coords, count, state.lineWidth * .5f, float(pixel_size_stack.back()), state.lineStyle == LINE_SMOOTH);
|
||||
line.draw();
|
||||
}
|
||||
}
|
||||
@@ -982,9 +1061,8 @@ love::image::ImageData *Graphics::newScreenshot(love::image::Image *image, bool
|
||||
{
|
||||
// Temporarily unbind the currently active canvas (glReadPixels reads the
|
||||
// active framebuffer, not the main one.)
|
||||
Canvas *curcanvas = Canvas::current;
|
||||
if (curcanvas)
|
||||
Canvas::bindDefaultCanvas();
|
||||
std::vector<Canvas *> canvases = getCanvas();
|
||||
setCanvas();
|
||||
|
||||
int w = getWidth();
|
||||
int h = getHeight();
|
||||
@@ -1005,10 +1083,7 @@ love::image::ImageData *Graphics::newScreenshot(love::image::Image *image, bool
|
||||
{
|
||||
delete[] pixels;
|
||||
delete[] screenshot;
|
||||
|
||||
if (curcanvas)
|
||||
curcanvas->startGrab(curcanvas->getAttachedCanvases());
|
||||
|
||||
setCanvas(canvases);
|
||||
throw love::Exception("Out of memory.");
|
||||
}
|
||||
|
||||
@@ -1040,16 +1115,12 @@ love::image::ImageData *Graphics::newScreenshot(love::image::Image *image, bool
|
||||
catch (love::Exception &)
|
||||
{
|
||||
delete[] screenshot;
|
||||
|
||||
if (curcanvas)
|
||||
curcanvas->startGrab(curcanvas->getAttachedCanvases());
|
||||
|
||||
setCanvas(canvases);
|
||||
throw;
|
||||
}
|
||||
|
||||
// Re-bind the active canvas, if necessary.
|
||||
if (curcanvas)
|
||||
curcanvas->startGrab(curcanvas->getAttachedCanvases());
|
||||
setCanvas(canvases);
|
||||
|
||||
return img;
|
||||
}
|
||||
@@ -1130,53 +1201,140 @@ bool Graphics::isSupported(Support feature) const
|
||||
}
|
||||
}
|
||||
|
||||
void Graphics::push()
|
||||
void Graphics::push(StackType type)
|
||||
{
|
||||
if (userMatrices == matrixLimit)
|
||||
throw Exception("Maximum stack depth reached. (More pushes than pops?)");
|
||||
glPushMatrix();
|
||||
++userMatrices;
|
||||
if (stackTypes.size() == MAX_USER_STACK_DEPTH)
|
||||
throw Exception("Maximum stack depth reached (more pushes than pops?)");
|
||||
|
||||
gl.pushTransform();
|
||||
|
||||
pixel_size_stack.push_back(pixel_size_stack.back());
|
||||
|
||||
if (type == STACK_ALL)
|
||||
states.push_back(states.back());
|
||||
|
||||
stackTypes.push_back(type);
|
||||
}
|
||||
|
||||
void Graphics::pop()
|
||||
{
|
||||
if (userMatrices < 1)
|
||||
throw Exception("Minimum stack depth reached. (More pops than pushes?)");
|
||||
glPopMatrix();
|
||||
--userMatrices;
|
||||
if (stackTypes.size() < 1)
|
||||
throw Exception("Minimum stack depth reached (more pops than pushes?)");
|
||||
|
||||
gl.popTransform();
|
||||
pixel_size_stack.pop_back();
|
||||
|
||||
if (stackTypes.back() == STACK_ALL)
|
||||
{
|
||||
DisplayState &newstate = states[states.size() - 2];
|
||||
|
||||
// Hack: the Lua-facing love.graphics.print function will set the current
|
||||
// font if needed, but only on its first call... we always want a font.
|
||||
if (newstate.font.get() == nullptr)
|
||||
newstate.font.set(states.back().font.get());
|
||||
|
||||
restoreStateChecked(newstate);
|
||||
|
||||
// The last two states in the stack should be equal now.
|
||||
states.pop_back();
|
||||
}
|
||||
|
||||
stackTypes.pop_back();
|
||||
}
|
||||
|
||||
void Graphics::rotate(float r)
|
||||
{
|
||||
glRotatef(LOVE_TODEG(r), 0, 0, 1);
|
||||
gl.getTransform().rotate(r);
|
||||
}
|
||||
|
||||
void Graphics::scale(float x, float y)
|
||||
{
|
||||
glScalef(x, y, 1);
|
||||
gl.getTransform().scale(x, y);
|
||||
pixel_size_stack.back() *= 2. / (fabs(x) + fabs(y));
|
||||
}
|
||||
|
||||
void Graphics::translate(float x, float y)
|
||||
{
|
||||
glTranslatef(x, y, 0);
|
||||
gl.getTransform().translate(x, y);
|
||||
}
|
||||
|
||||
void Graphics::shear(float kx, float ky)
|
||||
{
|
||||
Matrix t;
|
||||
t.setShear(kx, ky);
|
||||
glMultMatrixf((const GLfloat *)t.getElements());
|
||||
gl.getTransform().setShear(kx, ky);
|
||||
}
|
||||
|
||||
void Graphics::origin()
|
||||
{
|
||||
glLoadIdentity();
|
||||
gl.getTransform().setIdentity();
|
||||
pixel_size_stack.back() = 1;
|
||||
}
|
||||
|
||||
Graphics::DisplayState::DisplayState()
|
||||
: color(255, 255, 255, 255)
|
||||
, backgroundColor(0, 0, 0, 255)
|
||||
, blendMode(BLEND_ALPHA)
|
||||
, lineWidth(1.0f)
|
||||
, lineStyle(LINE_SMOOTH)
|
||||
, lineJoin(LINE_JOIN_MITER)
|
||||
, pointSize(1.0f)
|
||||
, scissor(false)
|
||||
, scissorBox()
|
||||
, font(nullptr)
|
||||
, shader(nullptr)
|
||||
, wireframe(false)
|
||||
{
|
||||
// We should just directly initialize the array in the initializer list, but
|
||||
// that feature of C++11 is broken in Visual Studio 2013...
|
||||
colorMask[0] = colorMask[1] = colorMask[2] = colorMask[3] = true;
|
||||
}
|
||||
|
||||
Graphics::DisplayState::DisplayState(const DisplayState &other)
|
||||
: color(other.color)
|
||||
, backgroundColor(other.backgroundColor)
|
||||
, blendMode(other.blendMode)
|
||||
, lineWidth(other.lineWidth)
|
||||
, lineStyle(other.lineStyle)
|
||||
, lineJoin(other.lineJoin)
|
||||
, pointSize(other.pointSize)
|
||||
, scissor(other.scissor)
|
||||
, scissorBox(other.scissorBox)
|
||||
, font(other.font)
|
||||
, shader(other.shader)
|
||||
, canvases(other.canvases)
|
||||
, wireframe(other.wireframe)
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
colorMask[i] = other.colorMask[i];
|
||||
}
|
||||
|
||||
Graphics::DisplayState::~DisplayState()
|
||||
{
|
||||
}
|
||||
|
||||
Graphics::DisplayState &Graphics::DisplayState::operator = (const DisplayState &other)
|
||||
{
|
||||
color = other.color;
|
||||
backgroundColor = other.backgroundColor;
|
||||
blendMode = other.blendMode;
|
||||
lineWidth = other.lineWidth;
|
||||
lineStyle = other.lineStyle;
|
||||
lineJoin = other.lineJoin;
|
||||
pointSize = other.pointSize;
|
||||
scissor = other.scissor;
|
||||
scissorBox = other.scissorBox;
|
||||
|
||||
font = other.font;
|
||||
shader = other.shader;
|
||||
canvases = other.canvases;
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
colorMask[i] = other.colorMask[i];
|
||||
|
||||
wireframe = other.wireframe;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
} // opengl
|
||||
} // graphics
|
||||
} // love
|
||||
|
||||
Reference in New Issue
Block a user