Moved the default Font logic from the graphics.lua script to the Graphics and Font module C++ code. Moved love.graphics.setNewFont from the graphics.lua script to the Graphics module Lua wrapper code. Moved the Vera.ttf data from the graphics.lua script to its own file inside the Font module.

This commit is contained in:
Alex Szpakowski
2015-01-31 01:10:38 -04:00
parent 72f98b9c8f
commit 665cdc4ae7
13 changed files with 5593 additions and 7471 deletions
+36 -6
View File
@@ -25,6 +25,7 @@
#include "Graphics.h"
#include "window/sdl/Window.h"
#include "font/Font.h"
#include "Polyline.h"
// C++
@@ -37,6 +38,7 @@
#include <cmath>
#include <cstdio>
namespace love
{
namespace graphics
@@ -70,6 +72,7 @@ Graphics::~Graphics()
{
// We do this manually so the love objects get released before the window.
states.clear();
defaultFont.set(nullptr);
currentWindow->release();
}
@@ -163,6 +166,31 @@ 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->newRasterizer(12));
r->release();
defaultFont.set(newFont(r.get()));
defaultFont->release();
::printf("created default font\n");
}
states.back().font.set(defaultFont.get());
}
void Graphics::setViewportSize(int width, int height)
{
this->width = width;
@@ -612,18 +640,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();
}
@@ -883,6 +909,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)
@@ -891,6 +919,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, 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)
+5 -8
View File
@@ -190,15 +190,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();
@@ -482,8 +475,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;
+14 -5
View File
@@ -246,7 +246,9 @@ int w_newQuad(lua_State *L)
int w_newFont(lua_State *L)
{
// Convert to Rasterizer, if necessary.
// Convert to Rasterizer, if necessary. Note that lua_isstring returns true
// if the value is a number, which we rely on for the variant that uses the
// default Font rather than a font file.
if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T) || luax_istype(L, 1, FILESYSTEM_FILE_DATA_T))
{
int idxs[] = {1, 2};
@@ -634,6 +636,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);
@@ -643,10 +653,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;
@@ -1438,6 +1446,7 @@ static const luaL_Reg functions[] =
{ "setBackgroundColor", w_setBackgroundColor },
{ "getBackgroundColor", w_getBackgroundColor },
{ "setNewFont", w_setNewFont },
{ "setFont", w_setFont },
{ "getFont", w_getFont },
@@ -64,6 +64,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);