source: trunk/src/font.cpp @ 639

Last change on this file since 639 was 639, checked in by sam, 9 years ago

Make font objects use the TileSet system and fix the text objects' Z offset.
Fixes tickets #19 and #24.

  • Property svn:keywords set to Id
File size: 1.5 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <string.h>
16#include <stdio.h>
17
18#include "core.h"
19
20/*
21 * Font implementation class
22 */
23
24class FontData
25{
26    friend class Font;
27
28private:
29    char *name;
30    int tiler;
31    int2 size;
32};
33
34/*
35 * Public Font class
36 */
37
38Font::Font(char const *path)
39  : data(new FontData())
40{
41    data->name = (char *)malloc(7 + strlen(path) + 1);
42    sprintf(data->name, "<font> %s", path);
43
44    data->tiler = Tiler::Register(path, 0, 16, 1.0f);
45    data->size = Tiler::GetSize(data->tiler);
46
47    drawgroup = DRAWGROUP_BEFORE;
48}
49
50Font::~Font()
51{
52    Tiler::Deregister(data->tiler);
53    free(data->name);
54    delete data;
55}
56
57void Font::TickDraw(float deltams)
58{
59    Entity::TickDraw(deltams);
60}
61
62char const *Font::GetName()
63{
64    return data->name;
65}
66
67void Font::Print(int3 pos, char const *str)
68{
69    Scene *scene = Scene::GetDefault();
70
71    while (*str)
72    {
73        uint32_t ch = (uint8_t)*str++;
74
75        if (ch != ' ')
76            scene->AddTile((data->tiler << 16) | (ch & 255),
77                           pos.x, pos.y, pos.z, 0);
78
79        pos.x += data->size.x;
80    }
81}
82
83int2 Font::GetSize() const
84{
85    return data->size;
86}
87
Note: See TracBrowser for help on using the repository browser.