1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     demo_TextsRenderData.cpp
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 46365 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "demo/RenderData/demo_TextsRenderData.h"
17 
18 namespace demo
19 {
20 
21 /* ------------------------------------------------------------------------
22          TextsRenderData class  member function
23    ------------------------------------------------------------------------ */
24 
TextsRenderData(void)25     TextsRenderData::TextsRenderData(void) :
26     TrianglesRenderData(),
27     m_PackedTextLength(0), m_MaxTextLength(MAX_TEXT_LENGTH),
28     m_FontSize(8.0f),
29     m_FontBaseSize(FONT_BASE_SIZE),
30     m_FontOffsetS(FONT_OFFSET_S),
31     m_FontOffsetT(FONT_OFFSET_T)
32     {
33         m_Color[0] = 1.0f;
34         m_Color[1] = 1.0f;
35         m_Color[2] = 1.0f;
36         m_Color[3] = 1.0f;
37     }
38 
~TextsRenderData(void)39     TextsRenderData::~TextsRenderData(void)
40     {
41         Finalize();
42     }
43 
Initialize(void)44     void TextsRenderData::Initialize(void)
45     {
46     }
47 
Finalize(void)48     void TextsRenderData::Finalize(void)
49     {
50         ClearPackedTextLength();
51         TrianglesRenderData::Finalize();
52     }
53 
InitializeVertexBuffers(const u32 vertexAttributes,const GLenum triangleType,const u32 maxTextLength)54     void TextsRenderData::InitializeVertexBuffers(const u32 vertexAttributes, const GLenum triangleType,
55         const u32 maxTextLength)
56     {
57         SetVertexAttributes(vertexAttributes);
58         SetTriangleType(triangleType);
59 
60         ClearPackedTextLength();
61         SetMaxTextLength(maxTextLength);
62     }
63 
SetMaxTextLength(const u32 maxTextLength)64     void TextsRenderData::SetMaxTextLength(const u32 maxTextLength)
65     {
66         m_PackedTextLength = 0;
67         m_MaxTextLength = maxTextLength;
68 
69         u32 verticesNum = 4 * m_MaxTextLength;
70         u32 trianglesNum = 2 * m_MaxTextLength;
71         SetVerticesNum(verticesNum);
72         SetTrianglesNum(trianglesNum);
73 
74         CreateArrays();
75         SetPackedTextLength(0);
76     }
77 
AddText(const f32 windowCoordinateX,const f32 windowCoordinateY,const char * text,const u32 textLength,const f32 normalizedDeviceCoordinateZ)78     void TextsRenderData::AddText(const f32 windowCoordinateX, const f32 windowCoordinateY,
79         const char *text, const u32 textLength, const f32 normalizedDeviceCoordinateZ)
80     {
81         if ( ( m_PackedTextLength + textLength) >= m_MaxTextLength )
82         {
83             return;
84         }
85 
86         for (u32 i = 0; i < textLength; ++i)
87         {
88             u32 index4 = 4 * (i + m_PackedTextLength);
89             u32 index2 = 2 * (i + m_PackedTextLength);
90 
91             s32 fontW = text[i] - 0x20;
92             f32 fontS = static_cast<f32>(fontW % m_FontBaseSize) * m_FontOffsetS;
93             f32 fontT = static_cast<f32>(fontW / m_FontBaseSize) * m_FontOffsetT;
94             f32 offset = static_cast<f32>(i) * m_FontSize;
95 
96             // POSITION
97             f32 normalizedDeviceCoordinateX = 0.0f;
98             f32 normalizedDeviceCoordinateY = 0.0f;
99             GetNormalizedDeviceCoordinateXY(windowCoordinateX + offset, windowCoordinateY + m_FontSize,
100                 normalizedDeviceCoordinateX, normalizedDeviceCoordinateY);
101             SetPosition(index4,
102                 normalizedDeviceCoordinateX, normalizedDeviceCoordinateY, normalizedDeviceCoordinateZ, 1.0f);
103 
104             GetNormalizedDeviceCoordinateXY(windowCoordinateX + offset, windowCoordinateY,
105                 normalizedDeviceCoordinateX, normalizedDeviceCoordinateY);
106             SetPosition(index4 + 1,
107                 normalizedDeviceCoordinateX, normalizedDeviceCoordinateY, normalizedDeviceCoordinateZ, 1.0f);
108 
109             GetNormalizedDeviceCoordinateXY(windowCoordinateX + offset + m_FontSize, windowCoordinateY + m_FontSize,
110                 normalizedDeviceCoordinateX, normalizedDeviceCoordinateY);
111             SetPosition(index4 + 2,
112                 normalizedDeviceCoordinateX, normalizedDeviceCoordinateY, normalizedDeviceCoordinateZ, 1.0f);
113 
114             GetNormalizedDeviceCoordinateXY(windowCoordinateX + offset + m_FontSize, windowCoordinateY,
115                 normalizedDeviceCoordinateX, normalizedDeviceCoordinateY);
116             SetPosition(index4 + 3,
117                 normalizedDeviceCoordinateX, normalizedDeviceCoordinateY, normalizedDeviceCoordinateZ, 1.0f);
118 
119             // COLOR
120             for (u32 j = 0; j < 4; j++)
121             {
122                 SetColor(index4 + j,
123                     m_Color[0], m_Color[1], m_Color[2], m_Color[3]);
124             }
125 
126             // TEXCOORD
127             f32 s = fontS + m_FontOffsetS;
128             f32 t = fontT + m_FontOffsetT;
129             SetTexcoord(index4,
130                 s, t, 0.0f);
131 
132             s = fontS + m_FontOffsetS;
133             t = fontT;
134             SetTexcoord(index4 + 1,
135                 s, t, 0.0f);
136 
137             s = fontS;
138             t = fontT + m_FontOffsetT;
139             SetTexcoord(index4 + 2,
140                 s, t, 0.0f);
141 
142             s = fontS;
143             t = fontT;
144             SetTexcoord(index4 + 3,
145                 s, t, 0.0f);
146 
147             // INDEX
148             SetIndex(index2,
149                 index4, index4 + 1, index4 + 2);
150 
151             SetIndex(index2 + 1,
152                 index4 + 2, index4 + 1, index4 + 3);
153         }
154 
155         m_PackedTextLength += textLength;
156         SetPackedTextLength(m_PackedTextLength);
157     }
158 
ClearPackedTextLength(void)159     void TextsRenderData::ClearPackedTextLength(void)
160     {
161         SetPackedTextLength(0);
162     }
163 
SetPackedTextLength(const u32 packedTextLength)164     void TextsRenderData::SetPackedTextLength(const u32 packedTextLength)
165     {
166         m_PackedTextLength = packedTextLength;
167         SetPackedVerticesNum(m_PackedTextLength * 4);
168         SetPackedTrianglesNum(m_PackedTextLength * 2);
169     }
170 
GetPackedTextLength(void) const171     u32 TextsRenderData::GetPackedTextLength(void) const
172     {
173         return m_PackedTextLength;
174     }
175 
SetFontSize(const f32 fontSize)176     void TextsRenderData::SetFontSize(const f32 fontSize)
177     {
178         m_FontSize = fontSize;
179     }
180 
SetFontColor(const f32 red,const f32 green,const f32 blue,const f32 alpha)181     void TextsRenderData::SetFontColor(const f32 red, const f32 green,
182         const f32 blue, const f32 alpha)
183     {
184         m_Color[0] = red;
185         m_Color[1] = green;
186         m_Color[2] = blue;
187         m_Color[3] = alpha;
188     }
189 
190 }
191