did a bunch of other stuff, sorry about the monolithic changeset but the diffs should be understandable enough

This commit is contained in:
Bill Meltsner
2010-04-15 10:19:01 -04:00
parent 17d7770eec
commit f8f812a719
18 changed files with 338 additions and 237 deletions
+59 -23
View File
@@ -1,48 +1,84 @@
/**
* Copyright (c) 2006-2010 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.
/**
* Copyright (c) 2006-2010 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 "Font.h"
#include <common/math.h>
#include <math.h>
namespace love
{
namespace graphics
{
namespace opengl
{
Font::Font(int size)
: size(size), lineHeight(1), mSpacing(1)
Font::Font(love::font::FontData * data)
: height(data->getHeight()), lineHeight(1.25)
{
glyphs = new Glyph*[MAX_CHARS];
for(unsigned int i = 0; i < MAX_CHARS; i++)
{
widths[i] = 0;
spacing[i] = 0;
glyphs[i] = new Glyph(data->getGlyphData(i));
glyphs[i]->load();
widths[i] = data->getGlyphData(i)->getWidth();
spacing[i] = data->getGlyphData(i)->getAdvance();
bearingX[i] = data->getGlyphData(i)->getBearingX();
bearingY[i] = data->getGlyphData(i)->getBearingY();
}
}
Font::~Font()
{
delete[] glyphs;
}
float Font::getHeight() const
{
return (float)size;
return height;
}
void Font::print(std::string text, float x, float y) const
{
print(text, x, y, 0.0f, 1.0f, 1.0f);
}
void Font::print(std::string text, float x, float y, float angle, float sx, float sy) const
{
glPushMatrix();
glTranslatef(ceil(x), ceil(y+getHeight()), 0.0f);
glRotatef(LOVE_TODEG(angle), 0, 0, 1.0f);
glScalef(sx, sy, 1.0f);
int s = 0;
for (unsigned int i = 0; i < text.size(); i++) {
int g = (int)text[i];
glyphs[g]->draw(bearingX[g] + s, -bearingY[g], 0, 1, 1, 0, 0);
s += spacing[g];
}
glPopMatrix();
}
void Font::print(char character, float x, float y) const
{
glyphs[character]->draw(x, y+getHeight(), 0, 1, 1, 0, 0);
}
float Font::getWidth(const std::string & line) const
+21 -18
View File
@@ -26,7 +26,8 @@
// LOVE
#include <common/Object.h>
#include <graphics/Volatile.h>
#include <font/FontData.h>
#include "Glyph.h"
namespace love
{
@@ -34,33 +35,35 @@ namespace graphics
{
namespace opengl
{
class Font : public Object, public Volatile
class Font : public Object
{
protected:
private:
int size;
int height;
float lineHeight;
float mSpacing; // modifies the spacing by multiplying it with this value
Glyph ** glyphs;
public:
static const unsigned int MAX_CHARS = 256;
// The widths of each character.
int widths[MAX_CHARS];
// The spacing of each character.
int spacing[MAX_CHARS];
// The X-bearing of each character.
int bearingX[MAX_CHARS];
// The Y-bearing of each character.
int bearingY[MAX_CHARS];
/**
* Default constructor.
*
* @param file The file containing the OpenGLFont data.
* @param size The size of the OpenGLFont.
* @param data The font data to construct from.
**/
Font(int size);
Font(love::font::FontData * data);
virtual ~Font();
virtual bool load() = 0;
virtual void unload() = 0;
/**
* Prints the text at the designated position.
*
@@ -68,7 +71,7 @@ namespace opengl
* @param x The x-coordinate.
* @param y The y-coordinate.
**/
virtual void print(std::string text, float x, float y) const = 0;
void print(std::string text, float x, float y) const;
/**
* Prints the text at the designated position with rotation and scaling.
@@ -78,7 +81,7 @@ namespace opengl
* @param y The y-coordinate.
* @param angle The amount of rotation.
**/
virtual void print(std::string text, float x, float y, float angle, float sx, float sy) const = 0;
void print(std::string text, float x, float y, float angle, float sx, float sy) const;
/**
* Prints the character at the designated position.
@@ -87,27 +90,27 @@ namespace opengl
* @param x The x-coordinate.
* @param y The y-coordinate.
**/
virtual void print(char character, float x, float y) const = 0;
void print(char character, float x, float y) const;
/**
* Returns the height of the font.
**/
virtual float getHeight() const;
float getHeight() const;
/**
* Returns the width of the passed string.
*
* @param line A line of text.
**/
virtual float getWidth(const std::string & line) const;
virtual float getWidth(const char * line) const;
float getWidth(const std::string & line) const;
float getWidth(const char * line) const;
/**
* Returns the width of the passed character.
*
* @param character A character.
**/
virtual float getWidth(const char character) const;
float getWidth(const char character) const;
/**
* Sets the line height (which should be a number to multiply the font size by,
@@ -119,7 +122,7 @@ namespace opengl
/**
* Returns the line height.
**/
virtual float getLineHeight() const;
float getLineHeight() const;
/**
* Sets the spacing modifier (changes the spacing between the characters the
+14 -2
View File
@@ -34,7 +34,6 @@ namespace opengl
: data(data), width((float)data->getWidth()), height((float)data->getHeight()), texture(0)
{
data->retain();
data->getWidth();
memset(vertices, 255, sizeof(vertex)*4);
@@ -94,6 +93,9 @@ namespace opengl
bool Glyph::loadVolatile()
{
GLint format = GL_RGBA;
if (data->getFormat() == love::font::GlyphData::FORMAT_LUMINOSITY_ALPHA) format = GL_LUMINANCE_ALPHA;
glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
@@ -108,7 +110,7 @@ namespace opengl
(GLsizei)width,
(GLsizei)height,
0,
GL_RGBA,
format,
GL_UNSIGNED_BYTE,
data->getData());
@@ -124,6 +126,16 @@ namespace opengl
texture = 0;
}
}
float Glyph::getWidth() const
{
return width;
}
float Glyph::getHeight() const
{
return height;
}
} // opengl
} // graphics
+22 -19
View File
@@ -1,21 +1,21 @@
/**
* Copyright (c) 2006-2010 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.
/**
* Copyright (c) 2006-2010 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.
**/
#ifndef LOVE_GRAPHICS_OPENGL_GLYPH_H
@@ -62,7 +62,10 @@ namespace opengl
// Implements Volatile.
bool loadVolatile();
void unloadVolatile();
void unloadVolatile();
float getWidth() const;
float getHeight() const;
void draw(float x, float y, float angle, float sx, float sy, float ox, float oy) const;
+3 -26
View File
@@ -410,26 +410,12 @@ namespace opengl
return new Quad(v, sw, sh);
}
Font * Graphics::newFont(Data * data, int size)
Font * Graphics::newFont(love::font::FontData * data)
{
Font * font = new TrueTypeFont(data, size);
Font * font = new Font(data);
// Load it and check for errors.
if(!font->load())
{
delete font;
return 0;
}
return font;
}
Font * Graphics::newImageFont(Image * image, const char * glyphs, float)
{
Font * font = new ImageFont(image, std::string(glyphs));
// Load it and check for errors.
if(!font->load())
if(!font)
{
delete font;
return 0;
@@ -497,15 +483,6 @@ namespace opengl
currentFont->retain();
}
void Graphics::setFont( Data * data, int size )
{
if(currentFont != 0)
currentFont->release();
currentFont = new TrueTypeFont(data, size);
currentFont->load();
}
Font * Graphics::getFont()
{
return currentFont;
+4 -18
View File
@@ -33,12 +33,13 @@
// LOVE
#include <graphics/Graphics.h>
#include <font/FontData.h>
#include <image/Image.h>
#include <image/ImageData.h>
#include "Font.h"
#include "Image.h"
#include "TrueTypeFont.h"
#include "ImageFont.h"
#include "Quad.h"
#include "SpriteBatch.h"
#include "ParticleSystem.h"
@@ -248,12 +249,7 @@ namespace opengl
/**
* Creates a Font object.
**/
Font * newFont(Data * data, int size);
/**
* Creates an ImageFont object.
**/
Font * newImageFont(Image * image, const char * glyphs, float spacing = 1);
Font * newFont(love::font::FontData * data);
SpriteBatch * newSpriteBatch(Image * image, int size, int usage);
@@ -285,16 +281,6 @@ namespace opengl
* @parm font A Font object.
**/
void setFont(Font * font);
/**
* Sets a default font. The font is
* loaded and sent to the GPU every time this is called,
* so no over-using.
* @param data Data
* @param size The size of the font.
**/
void setFont(Data * data, int size = 12);
/**
* Gets the current Font, or nil if none.
**/
+1 -1
View File
@@ -160,7 +160,7 @@ namespace opengl
glPushMatrix();
glTranslatef(ceil(x), ceil(y), 0.0f);
glRotatef(LOVE_TORAD(angle), 0, 0, 1.0f);
glRotatef(LOVE_TODEG(angle), 0, 0, 1.0f);
glScalef(sx, sy, 1.0f);
GLuint TrueTypeFont = list;
+87 -67
View File
@@ -21,6 +21,8 @@
#include "wrap_Graphics.h"
#include <image/ImageData.h>
#include <font/Rasterizer.h>
#include <font/FontData.h>
#include <scripts/graphics.lua.h>
@@ -197,69 +199,69 @@ namespace opengl
int w_newFont1(lua_State * L)
{
Data * d = 0;
// Convert to File, if necessary.
if(lua_isstring(L, 1))
luax_convobj(L, 1, "filesystem", "newFile");
if(luax_istype(L, 1, FILESYSTEM_FILE_T))
{
// Check the value.
love::filesystem::File * file = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T);
try {
d = file->read();
} catch (Exception & e) {
return luaL_error(L, e.what());
}
// Convert to Data, if necessary.
if(luax_istype(L, 1, FILESYSTEM_FILE_T)) {
love::filesystem::File * f = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T);
Data * d = f->read();
lua_pop(L, 1); // get rid of the file
luax_newtype(L, "Data", DATA_T, (void*)d);
}
else if(luax_istype(L, 1, DATA_T))
{
d = luax_checktype<Data>(L, 1, "Data", DATA_T);
} else { // This is not the type you're looking for.
return luaL_error(L, "love.graphics.newFont() requires a string, File, or font data as argument #1");
// Convert to Rasterizer, if necessary.
if(luax_istype(L, 1, DATA_T) && !luax_istype(L, 1, FONT_FONT_DATA_T)) {
int idxs[] = {1, 2};
luax_convobj(L, idxs, 2, "font", "newRasterizer");
}
// Second optional parameter can be a number:
int size = luaL_optint(L, 2, 12);
Font * font;
try {
font = instance->newFont(d, size);
} catch (Exception & e) {
return luaL_error(L, e.what());
}
// Convert to FontData, if necessary.
if(luax_istype(L, 1, FONT_RASTERIZER_T))
luax_convobj(L, 1, "font", "newFontData");
love::font::FontData * data = luax_checktype<love::font::FontData>(L, 1, "FontData", FONT_FONT_DATA_T);
// Create the font.
Font * font = instance->newFont(data);
if(font == 0)
return luaL_error(L, "Could not load the font");
return luaL_error(L, "Could not load font.");
// Push the type.
luax_newtype(L, "Font", GRAPHICS_FONT_T, (void*)font);
return 1;
}
int w_newImageFont(lua_State * L)
{
// Convert to File, if necessary.
if(lua_isstring(L, 1))
luax_convobj(L, 1, "filesystem", "newFile");
// Convert to Image, if necessary.
if(luax_istype(L, 1, FILESYSTEM_FILE_T))
luax_convobj(L, 1, "graphics", "newImage");
// Check the value.
Image * image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
const char * glyphs = luaL_checkstring(L, 2);
Font * font = instance->newImageFont(image, glyphs);
// Convert to ImageData if necessary.
if(lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T) || luax_istype(L, 1, DATA_T))
luax_convobj(L, 1, "image", "newImageData");
// Convert to Rasterizer if necessary.
if(luax_istype(L, 1, IMAGE_IMAGE_DATA_T)) {
int idxs[] = {1, 2};
luax_convobj(L, idxs, 2, "font", "newRasterizer");
}
// Convert to FontData, if necessary.
if(luax_istype(L, 1, FONT_RASTERIZER_T))
luax_convobj(L, 1, "font", "newFontData");
love::font::FontData * data = luax_checktype<love::font::FontData>(L, 1, "FontData", FONT_FONT_DATA_T);
// Create the font.
Font * font = instance->newFont(data);
if(font == 0)
return luaL_error(L, "Could not load the font");
return luaL_error(L, "Could not load font.");
// Push the type.
luax_newtype(L, "Font", GRAPHICS_FONT_T, (void*)font);
return 1;
}
@@ -329,25 +331,43 @@ namespace opengl
// The second parameter is an optional int.
int size = luaL_optint(L, 2, 12);
// If the first parameter is a string, convert it to a file.
if(lua_isstring(L, 1))
luax_convobj(L, 1, "filesystem", "newFile");
// If the first parameter is a File, use another setFont function.
if(luax_istype(L, 1, FILESYSTEM_FILE_T))
{
love::filesystem::File * file = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T);
instance->setFont(file->read(), size);
return 0;
Font * font;
// If the first parameter isn't a Font, create a new one
if (!luax_istype(L, 1, GRAPHICS_FONT_T)) {
lua_pushinteger(L, size); // push the size
lua_insert(L, 2); // move it to its proper place
// Convert to File, if necessary.
if(lua_isstring(L, 1))
luax_convobj(L, 1, "filesystem", "newFile");
// Convert to Data, if necessary.
if(luax_istype(L, 1, FILESYSTEM_FILE_T)) {
love::filesystem::File * f = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T);
Data * d = f->read();
lua_pop(L, 1); // get rid of the file
luax_newtype(L, "Data", DATA_T, (void*)d);
}
// Convert to Rasterizer, if necessary.
if(luax_istype(L, 1, DATA_T) && !luax_istype(L, 1, FONT_FONT_DATA_T)) {
int idxs[] = {1, 2};
luax_convobj(L, idxs, 2, "font", "newRasterizer");
}
// Convert to FontData, if necessary.
if(luax_istype(L, 1, FONT_RASTERIZER_T))
luax_convobj(L, 1, "font", "newFontData");
love::font::FontData * data = luax_checktype<love::font::FontData>(L, 1, "FontData", FONT_FONT_DATA_T);
// Create the font.
font = instance->newFont(data);
if(font == 0)
return luaL_error(L, "Could not load font.");
}
else if(luax_istype(L, 1, DATA_T))
{
Data * data = luax_checktype<Data>(L, 1, "Data", DATA_T);
instance->setFont(data, size);
return 0;
}
Font * font = luax_checktype<Font>(L, 1, "Font", GRAPHICS_FONT_T);
else font = luax_checktype<Font>(L, 1, "Font", GRAPHICS_FONT_T);
instance->setFont(font);
return 0;
}