FreeWRL/FreeX3D  3.0.0
RasterFont.c
1 /*
2 
3 */
4 
5 /****************************************************************************
6  This file is part of the FreeWRL/FreeX3D Distribution.
7 
8  Copyright 2009 CRC Canada. (http://www.crc.gc.ca)
9 
10  FreeWRL/FreeX3D is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Lesser Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  FreeWRL/FreeX3D is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with FreeWRL/FreeX3D. If not, see <http://www.gnu.org/licenses/>.
22 ****************************************************************************/
23 
24 
25 #include <config.h>
26 #include <system.h>
27 #include <display.h>
28 #include <internal.h>
29 
30 #include "../vrml_parser/Structs.h"
31 #include "opengl/RasterFont.h"
32 #include "opengl/OpenGL_Utils.h"
33 
34 #include <stdarg.h>
35 
36 #include "../main/headers.h"
37 #include "../input/EAIHelpers.h"
38 #include "../scenegraph/Component_Text.h"
39 //static struct X3D_Text myText;
40 //static struct X3D_FontStyle myFont;
42 //
43 //
44 //static bool rf_initialized = FALSE;
45 //
46 //static int xf_color = xf_white;
47 //static vec4f_t xf_colors[3] = {
48 // { 1.0f, 1.0f, 1.0f, 1.0f },
49 // { 0.0f, 0.0f, 0.0f, 1.0f },
50 // { 0.5f, 0.5f, 0.5f, 1.0f }
51 //};
52 static vec4f_t static_xf_colors[] = {
53  { 1.0f, 1.0f, 1.0f, 1.0f },
54  { 0.0f, 0.0f, 0.0f, 1.0f },
55  { 0.5f, 0.5f, 0.5f, 1.0f }
56  };
57 typedef struct pRasterFont{
58  struct X3D_Text myText;
59  struct X3D_FontStyle myFont;
60  //struct Uni_String myString;
61  bool rf_initialized;//= FALSE;
62  int xf_color;// = xf_white;
63  vec4f_t xf_colors[3]; /* = {
64  { 1.0f, 1.0f, 1.0f, 1.0f },
65  { 0.0f, 0.0f, 0.0f, 1.0f },
66  { 0.5f, 0.5f, 0.5f, 1.0f }
67  };*/
68 
69 }* ppRasterFont;
70 void *RasterFont_constructor(){
71  void *v = malloc(sizeof(struct pRasterFont));
72  memset(v,0,sizeof(struct pRasterFont));
73  return v;
74 }
75 void RasterFont_init(struct tRasterFont *t){
76  //public
77  //private
78  t->prv = RasterFont_constructor();
79  {
80  ppRasterFont p = (ppRasterFont)t->prv;
81  //p->myText;
82  //p->myFont;
83  //p->myString;
84 
85 
86  p->rf_initialized = FALSE;
87 
88  p->xf_color = xf_white;
89  memcpy(p->xf_colors,static_xf_colors,sizeof(static_xf_colors));
90  //{
91  // { 1.0f, 1.0f, 1.0f, 1.0f },
92  // { 0.0f, 0.0f, 0.0f, 1.0f },
93  // { 0.5f, 0.5f, 0.5f, 1.0f }
94  //};
95 
96  }
97 }
98 
99 void rf_print(const char *text)
100 {
101  ppRasterFont p = (ppRasterFont)gglobal()->RasterFont.prv;
102  /* has text changed? */
103  p->myText.string.p[0]->touched = 0;
104  verify_Uni_String (p->myText.string.p[0],(char *)text);
105  if (p->myText.string.p[0]->touched > 0) {
106  /* mark the FontStyle and Text node that things have changed */
107  p->myText._change++;
108  p->myFont._change++;
109  }
110 
111  render_Text (&p->myText);
112 }
113 
114 
115 void rf_printf(int x, int y, const char *format, ...)
116 {
117 #if defined(IPHONE) || defined(_ANDROID ) || defined(GLES2)
118 //printf ("skipping the rf_printf\n");
119 #else
120  va_list ap;
121  char xfont_buffer[5000];
122  ppRasterFont p = (ppRasterFont)gglobal()->RasterFont.prv;
123 
124  if (!p->rf_initialized) {
125  ERROR_MSG("xfont not initialized !!! initializing with defaults (fixed white)\n");
126  if (!rf_xfont_init("fixed")) {
127  return;
128  }
129  rf_xfont_set_color(xf_white);
130  }
131 
132  va_start(ap, format);
133  vsprintf(xfont_buffer, format, ap);
134  va_end(ap);
135 
136  FW_GL_RASTERPOS2I(x, y);
137 
138 #ifdef HAVE_TO_REIMPLEMENT
139  //JAS - this is one case where the new full-time shaders do not work.
140  FW_GL_COLOR4FV(p->xf_colors[p->xf_color]);
141 #endif
142 
143  rf_print(xfont_buffer);
144 #endif
145 }
146 
147 void rf_layer2D()
148 {
149 
150 #ifdef GL_ES_VERSION_2_0
151 //printf ("skipping the push attrib\n");
152 #else
153  FW_GL_PUSH_ATTRIB(GL_ENABLE_BIT);
154 #endif
155  glDisable(GL_DEPTH_TEST);
156  glDisable(GL_CULL_FACE);
157 
158 
159  // On assume être en MODELVIEW
160  FW_GL_MATRIX_MODE(GL_PROJECTION);
161  FW_GL_PUSH_MATRIX();
162  FW_GL_LOAD_IDENTITY();
163 
164  FW_GL_ORTHO(0.0, (GLfloat) gglobal()->display.screenWidth, // we need a viewport variable: glc.viewport[2],
165  0.0, (GLfloat) gglobal()->display.screenHeight, // glc.viewport[3],
166  -1, 1);
167  // Faire un FW_GL_POP_MATRIX après ...
168  FW_GL_MATRIX_MODE(GL_MODELVIEW);
169  FW_GL_PUSH_MATRIX();
170  FW_GL_LOAD_IDENTITY();
171  FW_GL_TRANSLATE_F(0.375f, 0.375f, 0.0f);
172 }
173 
174 void rf_leave_layer2D()
175 {
176 #ifdef GL_ES_VERSION_2_0
177 //printf ("skipping the popattribhte\n");
178 #else
179  FW_GL_POP_ATTRIB();
180 #endif
181 
182  FW_GL_MATRIX_MODE(GL_PROJECTION);
183  FW_GL_POP_MATRIX();
184  FW_GL_MATRIX_MODE(GL_MODELVIEW);
185  FW_GL_POP_MATRIX();
186 }
187 
188 int rf_xfont_init(const char *fontname)
189 {
190  /* create a new text node, but DO NOT call one of the createNewX3DNode interface, because we only
191  want a holder here, this is NOT a scenegraph node. */
192  ppRasterFont p = (ppRasterFont)gglobal()->RasterFont.prv;
193 
194  /* write zeroes here - we do not want any pointers, parents, etc, etc. */
195  bzero (&p->myText,sizeof (struct X3D_Text));
196 
197  p->myText._nodeType=NODE_Text;
198  p->myText.fontStyle = NULL;
199  p->myText.solid = TRUE;
200  p->myText.__rendersub = 0;
201  p->myText.origin.c[0] = 0;p->myText.origin.c[1] = 0;p->myText.origin.c[2] = 0;;
202 
203  /* give this 1 string */
204  p->myText.string.p = MALLOC (struct Uni_String **, sizeof(struct Uni_String)*1);p->myText.string.p[0] = newASCIIString("Initial String for Status Line");p->myText.string.n=1; ;
205 
206 
207  p->myText.textBounds.c[0] = 0;p->myText.textBounds.c[1] = 0;;
208  p->myText.length.n=0; p->myText.length.p=0;
209  p->myText.maxExtent = 0;
210  p->myText.lineBounds.n=0; p->myText.lineBounds.p=0;
211  p->myText.metadata = NULL;
212  p->myText._defaultContainer = FIELDNAMES_geometry;
213 
214  /* create a new FontStyle node here and link it in */
215  bzero (&p->myFont, sizeof (struct X3D_FontStyle));
216 
217  p->myFont._nodeType = NODE_FontStyle; /* needed for scenegraph structure in make_Text */
218  p->myFont.language = newASCIIString("");
219  p->myFont.leftToRight = TRUE;
220  p->myFont.topToBottom = TRUE;
221  p->myFont.style = newASCIIString("PLAIN");
222  p->myFont.size = 20.0f;
223  p->myFont.justify.p = MALLOC (struct Uni_String **, sizeof(struct Uni_String)*1);p->myFont.justify.p[0] = newASCIIString("BEGIN");p->myFont.justify.n=1; ;
224  p->myFont.metadata = NULL;
225  p->myFont.spacing = 1;
226  p->myFont.horizontal = TRUE;
227  /* p->myFont.family.p = MALLOC (struct Uni_String **, sizeof(struct Uni_String)*1);p->myFont.family.p[0] = newASCIIString("SERIF");p->myFont.family.n=1; */
228  p->myFont.family.p = MALLOC (struct Uni_String **, sizeof(struct Uni_String)*1);p->myFont.family.p[0] = newASCIIString("TYPEWRITER");p->myFont.family.n=1; ;
229  p->myFont._defaultContainer = FIELDNAMES_fontStyle;
230 
231  p->myText.fontStyle = X3D_NODE(&p->myFont);
232  p->rf_initialized = TRUE;
233  return TRUE;
234 }
235 
236 void rf_xfont_set_color(e_xfont_color_t index)
237 {
238  ppRasterFont p = (ppRasterFont)gglobal()->RasterFont.prv;
239 
240  ASSERT(index < e_xfont_color_max);
241  p->xf_color = index;
242 }
243 
244 void rf_xfont_set_usercolor(vec4f_t color)
245 {
246  ppRasterFont p = (ppRasterFont)gglobal()->RasterFont.prv;
247 
248  p->xf_colors[xf_user][0] = color[0];
249  p->xf_colors[xf_user][1] = color[1];
250  p->xf_colors[xf_user][2] = color[2];
251  p->xf_colors[xf_user][3] = color[3];
252 }