source: trunk/src/text.cpp @ 664

Last change on this file since 664 was 664, checked in by sam, 10 years ago

Get rid of float3, float4, int3 etc. in favour of GLSL types.

  • Property svn:keywords set to Id
File size: 1.9 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 <cstdio>
16#include <cstdlib> /* free() */
17#include <cstring> /* strdup() */
18
19#include "core.h"
20
21/*
22 * Text implementation class
23 */
24
25class TextData
26{
27    friend class Text;
28
29private:
30    int font, align, length;
31    char *text;
32    vec3i pos;
33};
34
35/*
36 * Public Text class
37 */
38
39Text::Text(char const *text, char const *font)
40  : data(new TextData())
41{
42    data->font = Forge::Register(font);
43    data->text = text ? strdup(text) : NULL;
44    data->length = text ? strlen(text) : 0;
45    data->pos = vec3i(0, 0, 0);
46
47    drawgroup = DRAWGROUP_HUD;
48}
49
50void Text::SetText(char const *text)
51{
52    if (data->text)
53        free(data->text);
54    data->text = text ? strdup(text) : NULL;
55    data->length = text ? strlen(text) : 0;
56}
57
58void Text::SetInt(int val)
59{
60    if (data->text)
61        free(data->text);
62    char text[128];
63    sprintf(text, "%i", val);
64    data->text = strdup(text);
65    data->length = strlen(text);
66}
67
68void Text::SetPos(vec3i pos)
69{
70    data->pos = pos;
71}
72
73void Text::SetAlign(int align)
74{
75    data->align = align;
76}
77
78void Text::TickDraw(float deltams)
79{
80    Entity::TickDraw(deltams);
81
82    if (data->text)
83    {
84        Font *font = Forge::GetFont(data->font);
85        vec3i delta = 0;
86        if (data->align == ALIGN_RIGHT)
87            delta.x -= data->length * font->GetSize().x;
88        else if (data->align == ALIGN_CENTER)
89            delta.x -= data->length * font->GetSize().x / 2;
90        font->Print(data->pos + delta, data->text);
91    }
92}
93
94Text::~Text()
95{
96    if (data->text)
97        free(data->text);
98    Forge::Deregister(data->font);
99    delete data;
100}
101
Note: See TracBrowser for help on using the repository browser.