source: trunk/src/text.cpp @ 2506

Last change on this file since 2506 was 2506, checked in by sam, 7 years ago

base: start removing occurrences of NULL on our long journey to nullptr.

  • Property svn:keywords set to Id
File size: 2.0 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, length;
36    char *text;
37    vec3 pos;
38};
39
40/*
41 * Public Text class
42 */
43
44Text::Text(char const *text, char const *font)
45  : data(new TextData())
46{
47    data->font = Forge::Register(font);
48    data->text = text ? strdup(text) : nullptr;
49    data->length = text ? strlen(text) : 0;
50    data->pos = vec3(0, 0, 0);
51
52    m_drawgroup = DRAWGROUP_HUD;
53}
54
55void Text::SetText(char const *text)
56{
57    if (data->text)
58        free(data->text);
59    data->text = text ? strdup(text) : nullptr;
60    data->length = text ? strlen(text) : 0;
61}
62
63void Text::SetInt(int val)
64{
65    if (data->text)
66        free(data->text);
67    char text[128];
68    sprintf(text, "%i", val);
69    data->text = strdup(text);
70    data->length = strlen(text);
71}
72
73void Text::SetPos(vec3 pos)
74{
75    data->pos = pos;
76}
77
78
79vec3 Text::GetPos()
80{
81    return (vec3)data->pos;
82}
83
84void Text::SetAlign(int align)
85{
86    data->align = align;
87}
88
89void Text::TickDraw(float seconds)
90{
91    Entity::TickDraw(seconds);
92
93    if (data->text)
94    {
95        Font *font = Forge::GetFont(data->font);
96        vec3 delta(0.0f);
97        if (data->align == ALIGN_RIGHT)
98            delta.x -= data->length * font->GetSize().x;
99        else if (data->align == ALIGN_CENTER)
100            delta.x -= data->length * font->GetSize().x / 2;
101        font->Print(data->pos + delta, data->text);
102    }
103}
104
105Text::~Text()
106{
107    if (data->text)
108        free(data->text);
109    Forge::Deregister(data->font);
110    delete data;
111}
112
113} /* namespace lol */
114
Note: See TracBrowser for help on using the repository browser.