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:
@@ -25,6 +25,7 @@
|
||||
|
||||
#include "Graphics.h"
|
||||
#include "window/sdl/Window.h"
|
||||
#include "font/Font.h"
|
||||
#include "Polyline.h"
|
||||
|
||||
// C++
|
||||
@@ -70,6 +71,7 @@ Graphics::~Graphics()
|
||||
{
|
||||
// We do this manually so the love objects get released before the window.
|
||||
states.clear();
|
||||
defaultFont.set(nullptr);
|
||||
|
||||
if (Shader::defaultShader)
|
||||
{
|
||||
@@ -170,6 +172,29 @@ void Graphics::restoreStateChecked(const DisplayState &s)
|
||||
setDefaultMipmapFilter(s.defaultMipmapFilter, s.defaultMipmapSharpness);
|
||||
}
|
||||
|
||||
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())
|
||||
{
|
||||
font::Font *fontmodule = Module::getInstance<font::Font>(M_FONT);
|
||||
if (!fontmodule)
|
||||
throw love::Exception("Font module has not been loaded.");
|
||||
|
||||
StrongRef<font::Rasterizer> r(fontmodule->newTrueTypeRasterizer(12));
|
||||
r->release();
|
||||
|
||||
defaultFont.set(newFont(r.get()));
|
||||
defaultFont->release();
|
||||
}
|
||||
|
||||
states.back().font.set(defaultFont.get());
|
||||
}
|
||||
|
||||
void Graphics::setViewportSize(int width, int height)
|
||||
{
|
||||
this->width = width;
|
||||
@@ -704,18 +729,16 @@ Color Graphics::getBackgroundColor() const
|
||||
|
||||
void Graphics::setFont(Font *font)
|
||||
{
|
||||
// Hack: the Lua-facing love.graphics.print function will set the current
|
||||
// font if needed, but only on its first call... we want to make sure a nil
|
||||
// font is never accidentally set (e.g. via love.graphics.reset.)
|
||||
if (font == nullptr)
|
||||
return;
|
||||
// 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);
|
||||
}
|
||||
|
||||
Font *Graphics::getFont() const
|
||||
Font *Graphics::getFont()
|
||||
{
|
||||
checkSetDefaultFont();
|
||||
return states.back().font.get();
|
||||
}
|
||||
|
||||
@@ -953,6 +976,8 @@ bool Graphics::isWireframe() const
|
||||
|
||||
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)
|
||||
{
|
||||
checkSetDefaultFont();
|
||||
|
||||
DisplayState &state = states.back();
|
||||
|
||||
if (state.font.get() != nullptr)
|
||||
@@ -961,6 +986,8 @@ void Graphics::print(const std::string &str, float x, float y , float angle, flo
|
||||
|
||||
void Graphics::printf(const std::string &str, float x, float y, float wrap, Font::AlignMode align, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
|
||||
{
|
||||
checkSetDefaultFont();
|
||||
|
||||
DisplayState &state = states.back();
|
||||
|
||||
if (state.font.get() != nullptr)
|
||||
|
||||
@@ -182,15 +182,8 @@ public:
|
||||
**/
|
||||
Color getBackgroundColor() const;
|
||||
|
||||
/**
|
||||
* Sets the current font.
|
||||
* @param font A Font object.
|
||||
**/
|
||||
void setFont(Font *font);
|
||||
/**
|
||||
* Gets the current Font, or nil if none.
|
||||
**/
|
||||
Font *getFont() const;
|
||||
Font *getFont();
|
||||
|
||||
void setShader(Shader *shader);
|
||||
void setShader();
|
||||
@@ -466,8 +459,12 @@ private:
|
||||
void restoreState(const DisplayState &s);
|
||||
void restoreStateChecked(const DisplayState &s);
|
||||
|
||||
void checkSetDefaultFont();
|
||||
|
||||
love::window::Window *currentWindow;
|
||||
|
||||
StrongRef<Font> defaultFont;
|
||||
|
||||
std::vector<double> pixel_size_stack; // stores current size of a pixel (needed for line drawing)
|
||||
|
||||
int width;
|
||||
|
||||
@@ -132,8 +132,6 @@ void OpenGL::setupContext()
|
||||
initMaxValues();
|
||||
createDefaultTexture();
|
||||
|
||||
state.lastPseudoInstanceID = -1;
|
||||
|
||||
// Invalidate the cached matrices by setting some elements to NaN.
|
||||
float nan = std::numeric_limits<float>::quiet_NaN();
|
||||
state.lastProjectionMatrix.setTranslation(nan, nan);
|
||||
@@ -276,14 +274,6 @@ void OpenGL::prepareDraw()
|
||||
// love-provided uniforms.
|
||||
shader->checkSetScreenParams();
|
||||
|
||||
// Make sure the Instance ID variable is up-to-date when
|
||||
// pseudo-instancing is used.
|
||||
if (state.lastPseudoInstanceID != 0 && shader->hasVertexAttrib(ATTRIB_PSEUDO_INSTANCE_ID))
|
||||
{
|
||||
glVertexAttrib1f((GLuint) ATTRIB_PSEUDO_INSTANCE_ID, 0.0f);
|
||||
state.lastPseudoInstanceID = 0;
|
||||
}
|
||||
|
||||
// We need to make sure antialiased Canvases are properly resolved
|
||||
// before sampling from their textures in a shader.
|
||||
// This is kind of a big hack. :(
|
||||
@@ -346,58 +336,6 @@ void OpenGL::drawElements(GLenum mode, GLsizei count, GLenum type, const void *i
|
||||
++stats.drawCalls;
|
||||
}
|
||||
|
||||
void OpenGL::drawArraysInstanced(GLenum mode, GLint first, GLsizei count, GLsizei primcount)
|
||||
{
|
||||
Shader *shader = Shader::current;
|
||||
|
||||
if (GLAD_ARB_draw_instanced)
|
||||
glDrawArraysInstancedARB(mode, first, count, primcount);
|
||||
else
|
||||
{
|
||||
bool shaderHasID = shader && shader->hasVertexAttrib(ATTRIB_PSEUDO_INSTANCE_ID);
|
||||
|
||||
// Pseudo-instancing fallback.
|
||||
for (int i = 0; i < primcount; i++)
|
||||
{
|
||||
if (shaderHasID)
|
||||
glVertexAttrib1f((GLuint) ATTRIB_PSEUDO_INSTANCE_ID, (GLfloat) i);
|
||||
|
||||
glDrawArrays(mode, first, count);
|
||||
}
|
||||
|
||||
if (shaderHasID)
|
||||
state.lastPseudoInstanceID = primcount - 1;
|
||||
}
|
||||
|
||||
++stats.drawCalls;
|
||||
}
|
||||
|
||||
void OpenGL::drawElementsInstanced(GLenum mode, GLsizei count, GLenum type, const void *indices, GLsizei primcount)
|
||||
{
|
||||
Shader *shader = Shader::current;
|
||||
|
||||
if (GLAD_ARB_draw_instanced)
|
||||
glDrawElementsInstancedARB(mode, count, type, indices, primcount);
|
||||
else
|
||||
{
|
||||
bool shaderHasID = shader && shader->hasVertexAttrib(ATTRIB_PSEUDO_INSTANCE_ID);
|
||||
|
||||
// Pseudo-instancing fallback.
|
||||
for (int i = 0; i < primcount; i++)
|
||||
{
|
||||
if (shaderHasID)
|
||||
glVertexAttrib1f((GLuint) ATTRIB_PSEUDO_INSTANCE_ID, (GLfloat) i);
|
||||
|
||||
glDrawElements(mode, count, type, indices);
|
||||
}
|
||||
|
||||
if (shaderHasID)
|
||||
state.lastPseudoInstanceID = primcount - 1;
|
||||
}
|
||||
|
||||
++stats.drawCalls;
|
||||
}
|
||||
|
||||
void OpenGL::setColor(const Color &c)
|
||||
{
|
||||
GLfloat glc[] = {c.r / 255.0f, c.g / 255.0f, c.b / 255.0f, c.a / 255.0f};
|
||||
|
||||
@@ -187,13 +187,6 @@ public:
|
||||
void drawArrays(GLenum mode, GLint first, GLsizei count);
|
||||
void drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices);
|
||||
|
||||
/**
|
||||
* glDrawArraysInstanced and glDrawElementsInstanced with pseudo-instancing
|
||||
* fallbacks. They also increment the draw-call counter (once per call).
|
||||
**/
|
||||
void drawArraysInstanced(GLenum mode, GLint first, GLsizei count, GLsizei primcount);
|
||||
void drawElementsInstanced(GLenum mode, GLsizei count, GLenum type, const void *indices, GLsizei primcount);
|
||||
|
||||
/**
|
||||
* Sets the current constant color.
|
||||
**/
|
||||
@@ -369,9 +362,6 @@ private:
|
||||
|
||||
BlendState blend;
|
||||
|
||||
// The last ID value used for pseudo-instancing.
|
||||
int lastPseudoInstanceID;
|
||||
|
||||
Matrix lastProjectionMatrix;
|
||||
Matrix lastTransformMatrix;
|
||||
|
||||
|
||||
@@ -655,6 +655,14 @@ int w_getBackgroundColor(lua_State *L)
|
||||
return 4;
|
||||
}
|
||||
|
||||
int w_setNewFont(lua_State *L)
|
||||
{
|
||||
int ret = w_newFont(L);
|
||||
Font *font = luax_checktype<Font>(L, -1, "Font", GRAPHICS_FONT_T);
|
||||
instance()->setFont(font);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int w_setFont(lua_State *L)
|
||||
{
|
||||
Font *font = luax_checktype<Font>(L, 1, "Font", GRAPHICS_FONT_T);
|
||||
@@ -664,10 +672,8 @@ int w_setFont(lua_State *L)
|
||||
|
||||
int w_getFont(lua_State *L)
|
||||
{
|
||||
Font *f = instance()->getFont();
|
||||
|
||||
if (f == 0)
|
||||
return 0;
|
||||
Font *f = nullptr;
|
||||
luax_catchexcept(L, [&](){ f = instance()->getFont(); });
|
||||
|
||||
luax_pushtype(L, "Font", GRAPHICS_FONT_T, f);
|
||||
return 1;
|
||||
@@ -1461,6 +1467,7 @@ static const luaL_Reg functions[] =
|
||||
{ "setBackgroundColor", w_setBackgroundColor },
|
||||
{ "getBackgroundColor", w_getBackgroundColor },
|
||||
|
||||
{ "setNewFont", w_setNewFont },
|
||||
{ "setFont", w_setFont },
|
||||
{ "getFont", w_getFont },
|
||||
|
||||
|
||||
@@ -66,6 +66,7 @@ int w_setColor(lua_State *L);
|
||||
int w_getColor(lua_State *L);
|
||||
int w_setBackgroundColor(lua_State *L);
|
||||
int w_getBackgroundColor(lua_State *L);
|
||||
int w_setNewFont(lua_State *L);
|
||||
int w_setFont(lua_State *L);
|
||||
int w_getFont(lua_State *L);
|
||||
int w_setColorMask(lua_State *L);
|
||||
|
||||
Reference in New Issue
Block a user