Add the ability to have formally deprecated functions.

Functions which are deprecated will print out a message and show up in a small dialog on-screen, when they're first called. Deprecation output is disabled in fused mode by default, and can be modified with love.setDeprecationOutput(enable).

--HG--
branch : minor
This commit is contained in:
Alex Szpakowski
2017-08-27 13:46:31 -03:00
parent 324ef8f690
commit de28c6e6a8
16 changed files with 542 additions and 10 deletions
+136
View File
@@ -0,0 +1,136 @@
/**
* 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.
**/
#include "common/config.h"
#include "Deprecations.h"
#include "Graphics.h"
#include "Font.h"
#include "common/deprecation.h"
#include "timer/Timer.h"
#include <algorithm>
namespace love
{
namespace graphics
{
Deprecations::Deprecations()
: currentDeprecationCount(0)
, lastUpdatedTime(0.0)
{
}
Deprecations::~Deprecations()
{
}
void Deprecations::draw(Graphics *gfx)
{
GetDeprecated deprecations;
if (deprecations.all.empty())
return;
int total = (int) deprecations.all.size();
if (total != currentDeprecationCount)
{
currentDeprecationCount = total;
lastUpdatedTime = timer::Timer::getTime();
}
double showTime = 20.0;
double fadeTime = 1.0;
double delta = timer::Timer::getTime() - lastUpdatedTime;
float alpha = 1.0f;
if (delta > (showTime - fadeTime))
alpha = (float) (1.0 - (delta - showTime - fadeTime) / fadeTime);
if (alpha <= 0.0f)
return;
if (font.get() == nullptr)
{
auto hinting = font::TrueTypeRasterizer::HINTING_NORMAL;
if (!isGammaCorrect() && gfx->getScreenPixelDensity() <= 1.0)
hinting = font::TrueTypeRasterizer::HINTING_LIGHT;
font.set(gfx->newDefaultFont(9, hinting), Acquire::NORETAIN);
}
gfx->flushStreamDraws();
gfx->push(Graphics::STACK_ALL);
gfx->reset();
int maxcount = 4;
int remaining = std::max(0, total - maxcount);
std::vector<Font::ColoredString> strings;
Colorf white(1, 1, 1, 1);
// Grab the newest deprecation notices first.
for (int i = total - 1; i >= remaining; --i)
{
if (!strings.empty())
strings.back().str += "\n";
const DeprecationInfo *info = deprecations.all[i];
strings.push_back({getDeprecationNotice(*info, true), white});
}
if (remaining > 0)
strings.push_back({"\n(And " + std::to_string(remaining) + " more)", white});
int padding = 5;
int width = 600;
for (const auto &coloredstr : strings)
width = std::max(width, font->getWidth(coloredstr.str) + padding * 2);
float wraplimit = std::min(gfx->getWidth(), width - padding * 2);
std::vector<std::string> wrappedlines;
font->getWrap(strings, wraplimit, wrappedlines);
int linecount = std::min((int) wrappedlines.size(), maxcount);
int height = font->getHeight() * linecount + padding * 2;
int x = 0;
int y = std::max(gfx->getHeight() - height, 0);
gfx->setColor(Colorf(0, 0, 0, 0.85 * alpha));
gfx->rectangle(Graphics::DRAW_FILL, x, y, width, height);
gfx->setColor(Colorf(1, 0.9, 0.8, 1 * alpha));
gfx->setScissor({x, y, width, height});
Matrix4 textm(x + padding, y + padding, 0, 1, 1, 0, 0, 0, 0);
gfx->printf(strings, font.get(), wraplimit, Font::ALIGN_LEFT, textm);
gfx->pop();
}
} // graphics
} // love
+51
View File
@@ -0,0 +1,51 @@
/**
* 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.
**/
#pragma once
#include "common/Object.h"
namespace love
{
namespace graphics
{
class Graphics;
class Font;
class Deprecations
{
public:
Deprecations();
~Deprecations();
void draw(Graphics *gfx);
private:
int currentDeprecationCount;
double lastUpdatedTime;
StrongRef<Font> font;
}; // Deprecations
} // graphics
} // love
+12 -10
View File
@@ -27,6 +27,7 @@
#include "window/Window.h"
#include "Font.h"
#include "Video.h"
#include "common/deprecation.h"
// C++
#include <algorithm>
@@ -157,6 +158,16 @@ Font *Graphics::newFont(love::font::Rasterizer *data, const Texture::Filter &fil
return new Font(data, filter);
}
Font *Graphics::newDefaultFont(int size, font::TrueTypeRasterizer::Hinting hinting, const Texture::Filter &filter)
{
auto fontmodule = Module::getInstance<font::Font>(M_FONT);
if (!fontmodule)
throw love::Exception("Font module has not been loaded.");
StrongRef<font::Rasterizer> r(fontmodule->newTrueTypeRasterizer(size, hinting), Acquire::NORETAIN);
return newFont(r.get(), filter);
}
Video *Graphics::newVideo(love::video::VideoStream *stream, float pixeldensity)
{
return new Video(this, stream, pixeldensity);
@@ -357,16 +368,7 @@ void Graphics::checkSetDefaultFont()
// 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);
}
defaultFont.set(newDefaultFont(12, font::TrueTypeRasterizer::HINTING_NORMAL), Acquire::NORETAIN);
states.back().font.set(defaultFont.get());
}
+5
View File
@@ -38,9 +38,11 @@
#include "Quad.h"
#include "Mesh.h"
#include "Image.h"
#include "Deprecations.h"
#include "depthstencil.h"
#include "math/Transform.h"
#include "font/Rasterizer.h"
#include "font/Font.h"
#include "video/VideoStream.h"
// C++
@@ -369,6 +371,7 @@ public:
Quad *newQuad(Quad::Viewport v, double sw, double sh);
Font *newFont(love::font::Rasterizer *data, const Texture::Filter &filter = Texture::defaultFilter);
Font *newDefaultFont(int size, font::TrueTypeRasterizer::Hinting hinting, const Texture::Filter &filter = Texture::defaultFilter);
Video *newVideo(love::video::VideoStream *stream, float pixeldensity);
virtual SpriteBatch *newSpriteBatch(Texture *texture, int size, vertex::Usage usage) = 0;
@@ -905,6 +908,8 @@ protected:
Capabilities capabilities;
Deprecations deprecations;
static const size_t MAX_USER_STACK_DEPTH = 64;
private:
+2
View File
@@ -899,6 +899,8 @@ void Graphics::present(void *screenshotCallbackData)
if (isCanvasActive())
throw love::Exception("present cannot be called while a Canvas is active.");
deprecations.draw(this);
flushStreamDraws();
endPass();