source: trunk/src/text.cpp @ 2820

Last change on this file since 2820 was 2820, checked in by sam, 8 years ago

core: Text constructor now wants a String object.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 1.7 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 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://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstdio>
16#include <cstdlib> /* free() */
17#include <cstring> /* strdup() */
18
19#include "core.h"
20
21using namespace std;
22
23namespace lol
24{
25
26/*
27 * Text implementation class
28 */
29
30class TextData
31{
32    friend class Text;
33
34private:
35    int font, align;
36    String m_text;
37    vec3 pos;
38};
39
40/*
41 * Public Text class
42 */
43
44Text::Text(String const &text, char const *font)
45  : data(new TextData())
46{
47    data->font = Forge::Register(font);
48    data->m_text = text;
49    data->pos = vec3(0, 0, 0);
50
51    m_drawgroup = DRAWGROUP_HUD;
52}
53
54void Text::SetText(String const &text)
55{
56    data->m_text = text;
57}
58
59void Text::SetInt(int val)
60{
61    data->m_text = String::Printf("%i", val);
62}
63
64void Text::SetPos(vec3 pos)
65{
66    data->pos = pos;
67}
68
69
70vec3 Text::GetPos()
71{
72    return (vec3)data->pos;
73}
74
75void Text::SetAlign(int align)
76{
77    data->align = align;
78}
79
80void Text::TickDraw(float seconds)
81{
82    Entity::TickDraw(seconds);
83
84    int length = data->m_text.Count();
85    if (length)
86    {
87        Font *font = Forge::GetFont(data->font);
88        vec3 delta(0.0f);
89        if (data->align == ALIGN_RIGHT)
90            delta.x -= length * font->GetSize().x;
91        else if (data->align == ALIGN_CENTER)
92            delta.x -= length * font->GetSize().x / 2;
93        font->Print(data->pos + delta, data->m_text.C());
94    }
95}
96
97Text::~Text()
98{
99    Forge::Deregister(data->font);
100    delete data;
101}
102
103} /* namespace lol */
104
Note: See TracBrowser for help on using the repository browser.