Skip to content

Commit

Permalink
Refactor|Fonts|Client: Completed initial C++ translation of (composit…
Browse files Browse the repository at this point in the history
…e) bitmap fonts

Todo: Cleanup.
  • Loading branch information
danij-deng committed Nov 10, 2013
1 parent c3ff0e8 commit 677f260
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 147 deletions.
55 changes: 30 additions & 25 deletions doomsday/client/include/resource/bitmapfont.h
Expand Up @@ -27,15 +27,16 @@
#include <de/size.h>
#include <de/str.h>

// Data for a character.
typedef struct {
RectRaw geometry;
Point2Raw coords[4];
} bitmapfont_char_t;

class bitmapfont_t : public font_t
{
public:
// Data for a character.
struct bitmapfont_char_t
{
RectRaw geometry;
Point2Raw coords[4];
};

/// Absolute file path to the archived version of this font (if any).
ddstring_t _filePath;

Expand All @@ -48,9 +49,23 @@ class bitmapfont_t : public font_t
/// Character map.
bitmapfont_char_t _chars[MAX_CHARS];

public:
bitmapfont_t(fontid_t bindId);
~bitmapfont_t();

static bitmapfont_t *fromFile(fontid_t bindId, char const *resourcePath);

void rebuildFromFile(char const *resourcePath);
void setFilePath(char const *filePath);

/// @return GL-texture name.
DGLuint textureGLName() const;
Size2Raw const *textureSize() const;
int textureHeight() const;
int textureWidth() const;

void charCoords(unsigned char ch, Point2Raw coords[4]);

void glInit();
void glDeinit();

Expand All @@ -59,16 +74,6 @@ class bitmapfont_t : public font_t
int charHeight(unsigned char ch);
};

void BitmapFont_SetFilePath(font_t *font, char const *filePath);

/// @return GL-texture name.
DGLuint BitmapFont_GLTextureName(font_t const *font);
Size2Raw const *BitmapFont_TextureSize(font_t const *font);
int BitmapFont_TextureHeight(font_t const *font);
int BitmapFont_TextureWidth(font_t const *font);

void BitmapFont_CharCoords(font_t *font, unsigned char ch, Point2Raw coords[4]);

class bitmapcompositefont_t : public font_t
{
public:
Expand Down Expand Up @@ -106,6 +111,15 @@ class bitmapcompositefont_t : public font_t
*/
void rebuildFromDef(ded_compositefont_t *def);

patchid_t charPatch(unsigned char ch);
void charSetPatch(unsigned char ch, char const *encodedPatchName);

de::Texture::Variant *charTexture(unsigned char ch);

uint8_t charBorder(unsigned char chr);

void charCoords(unsigned char chr, Point2Raw coords[4]);

void glInit();
void glDeinit();

Expand All @@ -114,13 +128,4 @@ class bitmapcompositefont_t : public font_t
int charHeight(unsigned char ch);
};

patchid_t BitmapCompositeFont_CharPatch(font_t *font, unsigned char ch);
void BitmapCompositeFont_CharSetPatch(font_t *font, unsigned char ch, char const *encodedPatchName);

de::Texture::Variant *BitmapCompositeFont_CharTexture(font_t *font, unsigned char ch);

uint8_t BitmapCompositeFont_CharBorder(font_t *font, unsigned char chr);

void BitmapCompositeFont_CharCoords(font_t *font, unsigned char chr, Point2Raw coords[4]);

#endif /* LIBDENG_BITMAPFONT_H */
4 changes: 0 additions & 4 deletions doomsday/client/include/resource/font.h
Expand Up @@ -101,8 +101,4 @@ class font_t
void charSize(Size2Raw *size, unsigned char ch);
};

font_t *Font_FromFile(fontid_t bindId, char const *resourcePath);

void Font_RebuildFromFile(font_t *font, char const *resourcePath);

#endif // CLIENT_RESOURCE_FONT_H
61 changes: 32 additions & 29 deletions doomsday/client/src/render/rend_font.cpp
Expand Up @@ -581,20 +581,21 @@ static void textFragmentDrawer(const char* fragment, int x, int y, int alignFlag
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDisable(GL_TEXTURE_2D);
}
if(font->type() == FT_BITMAP && 0 != BitmapFont_GLTextureName(font))
if(bitmapfont_t *bmapFont = font->maybeAs<bitmapfont_t>())
{
GL_BindTextureUnmanaged(BitmapFont_GLTextureName(font), gl::ClampToEdge,
gl::ClampToEdge, filterUI? gl::Linear : gl::Nearest);
if(bmapFont->textureGLName())
{
GL_BindTextureUnmanaged(bmapFont->textureGLName(), gl::ClampToEdge,
gl::ClampToEdge, filterUI? gl::Linear : gl::Nearest);

glMatrixMode(GL_TEXTURE);
glPushMatrix();
glLoadIdentity();
glScalef(1.f / BitmapFont_TextureWidth(font),
1.f / BitmapFont_TextureHeight(font), 1.f);
glMatrixMode(GL_TEXTURE);
glPushMatrix();
glLoadIdentity();
glScalef(1.f / bmapFont->textureWidth(), 1.f / bmapFont->textureHeight(), 1.f);
}
}

{ int pass;
for(pass = (noShadow? 1 : 0); pass < (noCharacter && noGlitter? 1 : 2); ++pass)
for(int pass = (noShadow? 1 : 0); pass < (noCharacter && noGlitter? 1 : 2); ++pass)
{
count = initialCount;
ch = fragment;
Expand Down Expand Up @@ -731,13 +732,16 @@ static void textFragmentDrawer(const char* fragment, int x, int y, int alignFlag

cx += w + sat->tracking;
}
}}
}

// Restore previous GL-state.
if(font->type() == FT_BITMAP && 0 != BitmapFont_GLTextureName(font))
if(bitmapfont_t *bmapFont = font->maybeAs<bitmapfont_t>())
{
glMatrixMode(GL_TEXTURE);
glPopMatrix();
if(bmapFont->textureGLName())
{
glMatrixMode(GL_TEXTURE);
glPopMatrix();
}
}
if(renderWireframe > 1)
{
Expand Down Expand Up @@ -767,35 +771,34 @@ static void drawChar(unsigned char ch, int posX, int posY, font_t *font,
glMatrixMode(GL_MODELVIEW);
glTranslatef(x, y, 0);

switch(font->type())
if(bitmapfont_t *bmapFont = font->maybeAs<bitmapfont_t>())
{
case FT_BITMAP:
/// @todo Filtering should be determined at a higher level.
/// @todo We should not need to re-bind this texture here.
GL_BindTextureUnmanaged(BitmapFont_GLTextureName(font), gl::ClampToEdge,
GL_BindTextureUnmanaged(bmapFont->textureGLName(), gl::ClampToEdge,
gl::ClampToEdge, filterUI? gl::Linear : gl::Nearest);

std::memcpy(&geometry, font->charGeometry(ch), sizeof(geometry));
BitmapFont_CharCoords(font, ch, coords);
break;

case FT_BITMAPCOMPOSITE: {
uint8_t const border = BitmapCompositeFont_CharBorder(font, ch);
std::memcpy(&geometry, bmapFont->charGeometry(ch), sizeof(geometry));
bmapFont->charCoords(ch, coords);
}
else if(bitmapcompositefont_t *compFont = font->maybeAs<bitmapcompositefont_t>())
{
uint8_t const border = compFont->charBorder(ch);

GL_BindTexture(BitmapCompositeFont_CharTexture(font, ch));
GL_BindTexture(compFont->charTexture(ch));

std::memcpy(&geometry, font->charGeometry(ch), sizeof(geometry));
std::memcpy(&geometry, compFont->charGeometry(ch), sizeof(geometry));
if(border)
{
geometry.origin.x -= border;
geometry.origin.y -= border;
geometry.size.width += border*2;
geometry.size.height += border*2;
}
BitmapCompositeFont_CharCoords(font, ch, coords);
break; }

default:
compFont->charCoords(ch, coords);
}
else
{
Con_Error("FR_DrawChar: Invalid font type %i.", (int) font->type());
exit(1); // Unreachable.
}
Expand Down

0 comments on commit 677f260

Please sign in to comment.