Skip to content

Commit 78c783b

Browse files
author
rude
committed
Fixed ImageFont.
1 parent 806d085 commit 78c783b

File tree

4 files changed

+67
-85
lines changed

4 files changed

+67
-85
lines changed

changes.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
LOVE 0.6.2
22
----------
33

4+
* Fixed a bug causing ImageFonts to cut off some pixels.
5+
* Fixed a bug where filled rectangles were too small.
46
* Fixed a bug in Image:setFilter where it would switch the parameters.
57
* Fixed a bug in ImageRasterizer where it wasn't using the data.
68
* Image filter and wrap modes now use string constants as well.

src/modules/graphics/opengl/ImageFont.cpp

Lines changed: 57 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@
2020

2121
#include "ImageFont.h"
2222

23+
#include <common/math.h>
2324
#include <SDL_opengl.h>
2425

25-
using std::string;
26-
2726
namespace love
2827
{
2928
namespace graphics
3029
{
3130
namespace opengl
3231
{
33-
ImageFont::ImageFont(Image * image, std::string glyphs)
32+
ImageFont::ImageFont(Image * image, const std::string& glyphs)
3433
: Font(0), image(image), glyphs(glyphs)
3534

3635
{
@@ -43,7 +42,7 @@ namespace opengl
4342
image->release();
4443
}
4544

46-
void ImageFont::print(string text, float x, float y) const
45+
void ImageFont::print(std::string text, float x, float y) const
4746
{
4847
glPushMatrix();
4948
glTranslatef(x, y, 0.0f);
@@ -58,7 +57,7 @@ namespace opengl
5857
glPushMatrix();
5958

6059
glTranslatef(x, y, 0.0f);
61-
glRotatef(angle * 57.29578f, 0, 0, 1.0f);
60+
glRotatef(LOVE_TORAD(angle), 0, 0, 1.0f);
6261
glScalef(sx, sy, 1.0f);
6362

6463
GLuint OpenGLFont = list;
@@ -92,73 +91,57 @@ namespace opengl
9291
{
9392
love::image::pixel * pixels = (love::image::pixel *)(image->getData()->getData());
9493

94+
unsigned imgw = (unsigned)image->getWidth();
95+
unsigned imgh = (unsigned)image->getHeight();
96+
unsigned imgs = imgw*imgh;
97+
9598
// Reading texture data begins
96-
size = (int)image->getHeight();
99+
size = imgh;
97100

98-
for(unsigned int i = 0; i < MAX_CHARS; i++) positions[i] = -1;
101+
for(unsigned int i = 0; i < MAX_CHARS; i++)
102+
positions[i] = -1;
99103

100104
love::image::pixel spacer = pixels[0];
101-
unsigned int current = 0;
102-
int width = 0;
103-
int space = 0;
104-
105-
// Finds out where the first character starts
106-
int firstchar = 0;
107-
for(int i = 0; i != (int)image->getWidth(); i++)
105+
106+
unsigned num = glyphs.size();
107+
108+
unsigned start = 0;
109+
unsigned end = 0;
110+
unsigned space = 0;
111+
112+
for(unsigned i = 0; i < num; ++i)
108113
{
109-
if(spacer.r == pixels[i].r && spacer.g == pixels[i].g && spacer.b == pixels[i].b && spacer.a == pixels[i].a)
110-
continue;
111-
else
112-
{
113-
firstchar = i;
114+
if(i >= MAX_CHARS)
114115
break;
115-
}
116-
}
117116

118-
for(int i = firstchar; i != (int)image->getWidth(); i++)
119-
{
120-
if(spacer.r == pixels[i].r && spacer.g == pixels[i].g && spacer.b == pixels[i].b && spacer.a == pixels[i].a)
121-
{
122-
if(width != 0) // this means we have found the end of our current character
123-
{
124-
if((unsigned int)glyphs[current] > MAX_CHARS)
125-
printf("Error reading texture font: Character '%c' is out of range.", glyphs[current]);
126-
else
127-
{
128-
widths[(int)glyphs[current]] = width - 1;
129-
positions[(int)glyphs[current]] = i - width;
130-
}
131-
132-
width = 0;
133-
//space++; // start counting the spacing
134-
}
135-
space++;
136-
}
137-
else
138-
{
139-
if(space != 0) // this means we have found the end of our spacing
140-
{
141-
if((unsigned int)spacing[current] > MAX_CHARS)
142-
printf("Error reading image font: Character '%c' is out of range.", glyphs[current]);
143-
else
144-
spacing[(int)glyphs[current]] = space;
145-
146-
current++;
147-
if(current == glyphs.size())
148-
i = (int)image->getWidth() - 1; // just to end it when the last character is found
149-
150-
space = 0;
151-
//width++; // start counting the width
152-
}
153-
width++;
154-
}
117+
start = end;
118+
119+
// Finds out where the first character starts
120+
while(start < imgw && equal(pixels[start], spacer))
121+
++start;
122+
123+
if(i > 0)
124+
spacing[glyphs[i - 1]] = (start > end) ? (start - end) : 0;
125+
126+
end = start;
127+
128+
// Find where glyph ends.
129+
while(end < imgw && !equal(pixels[end], spacer))
130+
++end;
131+
132+
if(start >= end)
133+
break;
134+
135+
unsigned c = glyphs[i];
136+
137+
positions[c] = start;
138+
widths[c] = (end - start);
155139
}
156-
// Reading image data ends
157140

158141
// Replace spacer color with an empty pixel
159-
for(int i = 0; i < (int)(image->getWidth() * image->getHeight()); i++)
142+
for(unsigned int i = 0; i < imgs; ++i)
160143
{
161-
if(spacer.r == pixels[i].r && spacer.g == pixels[i].g && spacer.b == pixels[i].b && spacer.a == pixels[i].a)
144+
if(equal(pixels[i], spacer))
162145
{
163146
pixels[i].r = 0;
164147
pixels[i].g = 0;
@@ -176,26 +159,14 @@ namespace opengl
176159

177160
if(positions[i] != -1)
178161
{
162+
Quad::Viewport v;
163+
v.x = positions[i];
164+
v.y = 0;
165+
v.w = widths[i];
166+
v.h = imgh;
167+
Quad q(v, imgw, imgh);
179168

180-
float x = (float)positions[i] + 1;
181-
float y = 1.0;
182-
float w = (float)widths[i];
183-
float h = (float)size+1;
184-
185-
image->bind();
186-
187-
float xTex = x/(float)image->getWidth();
188-
float yTex = y/(float)image->getHeight();
189-
190-
float wTex = w/(float)image->getWidth();
191-
float hTex = h/(float)image->getHeight();
192-
193-
glBegin(GL_QUADS);
194-
glTexCoord2f(xTex,yTex); glVertex2f(0,0);
195-
glTexCoord2f(xTex,yTex+hTex); glVertex2f(0,h);
196-
glTexCoord2f(xTex+wTex,yTex+hTex); glVertex2f(w,h);
197-
glTexCoord2f(xTex+wTex,yTex); glVertex2f(w,0);
198-
glEnd();
169+
image->drawq(&q, 0, 0, 0, 1, 1, 0, 0);
199170

200171
glTranslatef((float)widths[i] + ((float)spacing[i] * mSpacing), 0, 0);
201172
}
@@ -213,7 +184,12 @@ namespace opengl
213184
glDeleteLists(list, MAX_CHARS);
214185
}
215186

216-
inline int ImageFont::next_p2(int num)
187+
bool ImageFont::equal(const love::image::pixel& a, const love::image::pixel& b)
188+
{
189+
return (a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a);
190+
}
191+
192+
int ImageFont::next_p2(int num)
217193
{
218194
int powered = 2;
219195
while(powered < num) powered <<= 1;

src/modules/graphics/opengl/ImageFont.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ namespace opengl
6464
* @param file The image file.
6565
* @param glyphs A list of the characters as they appear in the image.
6666
**/
67-
ImageFont(Image * image, std::string glyphs);
67+
ImageFont(Image * image, const std::string& glyphs);
6868

6969
/**
7070
* Calls unload().
@@ -87,12 +87,17 @@ namespace opengl
8787

8888
protected:
8989

90+
/**
91+
* Checks whether two pixels are equal.
92+
**/
93+
bool equal(const love::image::pixel& a, const love::image::pixel& b);
94+
9095
/**
9196
* Returns the closest number to num which is a power of two.
9297
*
9398
* @param num The number to be 2powered.
9499
**/
95-
inline int next_p2(int num);
100+
int next_p2(int num);
96101

97102
}; // ImageFont
98103

src/modules/image/ImageData.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace image
3232
struct pixel
3333
{
3434
// Red, green, blue, alpha.
35-
unsigned char r, g, b, a;
35+
unsigned char r, g, b, a;
3636
};
3737

3838
/**
@@ -95,7 +95,6 @@ namespace image
9595
**/
9696
virtual pixel getPixel(int x, int y) const = 0;
9797

98-
9998
}; // ImageData
10099

101100
} // image

0 commit comments

Comments
 (0)