source: trunk/src/font.cpp @ 787

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

tiler: Tiler::GetSize can now return a different value for different tiles.

  • Property svn:keywords set to Id
File size: 1.6 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 <cstring>
16#include <cstdio>
17
18#include "core.h"
19
20using namespace std;
21
22namespace lol
23{
24
25/*
26 * Font implementation class
27 */
28
29class FontData
30{
31    friend class Font;
32
33private:
34    char *name;
35    int tiler;
36    vec2i size;
37};
38
39/*
40 * Public Font class
41 */
42
43Font::Font(char const *path)
44  : data(new FontData())
45{
46    data->name = (char *)malloc(7 + strlen(path) + 1);
47    sprintf(data->name, "<font> %s", path);
48
49    data->tiler = Tiler::Register(path, 0, 16, 1.0f);
50    data->size = Tiler::GetSize(data->tiler, 0);
51
52    drawgroup = DRAWGROUP_BEFORE;
53}
54
55Font::~Font()
56{
57    Tiler::Deregister(data->tiler);
58    free(data->name);
59    delete data;
60}
61
62void Font::TickDraw(float deltams)
63{
64    Entity::TickDraw(deltams);
65}
66
67char const *Font::GetName()
68{
69    return data->name;
70}
71
72void Font::Print(vec3i pos, char const *str)
73{
74    Scene *scene = Scene::GetDefault();
75
76    while (*str)
77    {
78        uint32_t ch = (uint8_t)*str++;
79
80        if (ch != ' ')
81            scene->AddTile((data->tiler << 16) | (ch & 255),
82                           pos.x, pos.y, pos.z, 0);
83
84        pos.x += data->size.x;
85    }
86}
87
88vec2i Font::GetSize() const
89{
90    return data->size;
91}
92
93} /* namespace lol */
94
Note: See TracBrowser for help on using the repository browser.