Line data Source code
1 : #include <GL/glut.h>
2 : #include "TextGameObject.h"
3 : #include "constants.h"
4 : #include "api.h"
5 :
6 : //------------------------------------------------------------------------------
7 0 : TextGameObject::TextGameObject() : Object() {
8 0 : text = "";
9 0 : color[0] = 0.0f; color[1] = 0.0f; color[2] = 0.0f; color[3] = 1.0f;
10 0 : width = 10.0f;
11 0 : fadeRate = 0.0f;
12 0 : }
13 :
14 : //------------------------------------------------------------------------------
15 0 : void TextGameObject::draw(Object* camera) {
16 0 : if (isInView_) {
17 0 : glEnable(GL_BLEND);
18 :
19 0 : glPushMatrix();
20 0 : glColor4fv(color);
21 0 : glLineWidth(width);
22 0 : glTranslatef(position_.x, position_.y, position_.z );
23 0 : glScalef (scale_.x, scale_.y, 0.0f);
24 :
25 0 : for (int i = 0; i < text.length(); i++) {
26 0 : glutStrokeCharacter(GLUT_STROKE_ROMAN, text[i]);
27 0 : }
28 0 : glPopMatrix();
29 :
30 0 : glDisable(GL_BLEND);
31 0 : }
32 0 : }
33 :
34 : //------------------------------------------------------------------------------
35 0 : void TextGameObject::animate(const float &timeElapsed, Object* camera) {
36 0 : Object::animate(timeElapsed, camera);
37 :
38 0 : color[3] += timeElapsed * fadeRate;
39 :
40 0 : if (color[3] > 1.0f)
41 0 : color[3] = 1.0f;
42 0 : else if (color[3] < 0.0f)
43 0 : color[3] = 0.0f;
44 0 : }
45 :
46 0 : void TextGameObject::buildLuaObjectTable(lua_State *L) {
47 0 : Object::buildLuaObjectTable(L);
48 :
49 0 : lua_pushstring(L, "text");
50 0 : lua_pushstring(L, text.c_str());
51 0 : lua_rawset(L, -3);
52 :
53 0 : lua_pushstring(L, "color");
54 0 : returnArray(L, color, 4);
55 0 : lua_rawset(L, -3);
56 :
57 0 : lua_pushstring(L, "width");
58 0 : lua_pushnumber(L, width);
59 0 : lua_rawset(L, -3);
60 :
61 0 : lua_pushstring(L, "fade_rate");
62 0 : lua_pushnumber(L, fadeRate);
63 0 : lua_rawset(L, -3);
64 0 : }
65 :
66 0 : void TextGameObject::transferLuaObjectTable(lua_State *L) {
67 0 : Object::transferLuaObjectTable(L);
68 :
69 0 : int index = -1;
70 0 : lua_pushstring(L, "width");
71 0 : lua_gettable(L, 1);
72 0 : if (lua_isnumber(L, index)) {
73 0 : width = lua_tonumber(L, -1);
74 0 : }
75 0 : lua_pop(L, 1);
76 :
77 0 : index = -1;
78 0 : lua_pushstring(L, "fade_rate");
79 0 : lua_gettable(L, 1);
80 0 : if (lua_isnumber(L, index)) {
81 0 : fadeRate = lua_tonumber(L, -1);
82 0 : }
83 0 : lua_pop(L, 1);
84 :
85 0 : index = -1;
86 0 : lua_pushstring(L, "color");
87 0 : lua_gettable(L, 1);
88 0 : loadArray(L, color, 4, -1);
89 0 : lua_pop(L, 1);
90 : /*
91 : index = -1;
92 : lua_pushstring(L, "text");
93 : lua_gettable(L, 1);
94 : text = string(lua_tostring(L, -1));
95 : lua_pop(L, 1);
96 : */
97 0 : }
98 :
99 : //------------------------------------------------------------------------------
100 0 : void TextGameObject::render_string(void* font, const char* string) {
101 : // Renders a bitmap font string
102 0 : char* p = (char*) string;
103 0 : while (*p != '\0') glutBitmapCharacter(font, *p++);
104 0 : }
105 :
106 : //------------------------------------------------------------------------------
107 0 : void TextGameObject::displayText(
108 : const char *text, const float &x, const float &y, const float &z,
109 : const float &scaleX, const float &scaleY
110 : ) {
111 : // Renders a stroke font string
112 0 : glPushMatrix();
113 0 : glTranslatef (x, y, z);
114 0 : glScalef (scaleX, scaleY, 0.0f);
115 0 : for (const char* p = text; *p; p++)
116 0 : glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
117 0 : glPopMatrix();
118 0 : }
|