The Game Engine  1
Text.cpp
Go to the documentation of this file.
1 #include "Text.h"
2 #include <iostream>
3 #include <string>
4 #include "Game.h"
5 #include "fns.h"
6 #include <GL/gl.h>
7 #include <stdexcept>
8 #include <map>
9 #include <fstream>
10 #include <yaml-cpp/yaml.h>
11 using namespace std;
12 
13 Text::Text(string filename, string sectionName, FontType t, int s, Color c, int w, int h, Point2D pos)
14  : WorldObject(filename),
15  mFont(Game::game()->getFont(t, s)),
16  color(c),
17  width(w),
18  height(h),
19  lines(vector<string>()),
20  startline(0)
21 {
22  mPos = pos;
23  filename="ArtAssets/Story/"+filename+".yaml";
24  ifstream fin(filename.c_str());
25 
26  if(!fin)
27  {
28  printf("Error opening %s\n\n",filename.c_str());
29  }
30 
31  YAML::Parser parser(fin);
32 
33  YAML::Node doc;
34 
35  while(parser.GetNextDocument(doc)) {
36  try{
37  const YAML::Node& section = doc[sectionName];
38  string lang = "en";
39  switch(t)
40  {
41  case English:
42  {
43  lang="en";
44  break;
45  }
46  case Japanese:
47  {
48  lang="jp";
49  break;
50  }
51  case JapaneseWithRuby:
52  {
53  lang="jprub";
54  break;
55  }
56  }
57 
58  const YAML::Node& items = section[lang];
59  string tmp;
60  foritr(it,items)
61  {
62  *it>>tmp;
63  lines.push_back( tmp.size() > 0 && tmp[tmp.size()-1]==10 ? tmp.substr(0,tmp.size()-1):tmp);
64  }
65  }
66  catch (exception e){
67  cout<<e.what()<<endl;
68  }
69  }
70  wrap();
71 }
72 
73 Text::Text(string name, string t, int s, Color c, int w, int h, Point2D pos)
74  : WorldObject(name),
75  mFont(Game::game()->getFont(Game::game()->Language, s)),
76  color(c),
77  width(w),
78  height(h),
79  lines(vector<string>()),
80  startline(0)
81 {
82  mPos = pos;
83  //here we see if the thing wil fit on one line if not we resize it
84  lines.push_back(t);
85  wrap();
86 }
87 
89 {
91 }
92 
93 void Text::draw()
94 {
95  int size = lines.size();
96  if(size>0){
97  int i= startline;
98  while(mFont->size > 0 && i < size && i*mFont->size < height){
100  /*vector<string> ss = splitString(tmp);
101  for(size_t i=0;i<ss.size();++i){
102  lines.push_back(ss[i]);
103  }*/
104  if(mFont->font){
105  SDL_Surface *initial;
106  SDL_Surface *intermediary;
107  int w,h;
108  Texture texture;
109 
110  /* Use SDL_TTF to render our txt */
111  if (!(initial = TTF_RenderUTF8_Blended(mFont->font, lines[i].c_str(), color.toSDLColor())))
112  {
113  fprintf(stderr, "TTF_RenderUTF8_Blended: %s\n", TTF_GetError());
114  return;
115  }
116 
117  /* Convert the rendered txt to a known format */
118  w = nextPow2(initial->w);
119  h = nextPow2(initial->h);
120 
121  intermediary = SDL_CreateRGBSurface(0, w, h, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
122  SDL_BlitSurface(initial, 0, intermediary, 0);
123 
124  glBlendFunc(GL_ONE, GL_ONE);
125 
126  /* Tell GL about our new texture */
127  glGenTextures(1, &texture.image);
128  glBindTexture(GL_TEXTURE_2D, texture.image);
129  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, intermediary->pixels );
130 
131  /* GL_NEAREST looks horrible, if scaled... */
132  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
133  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
134 
135  /* prepare to render our texture */
136  glBindTexture(GL_TEXTURE_2D, texture.image);
137  //this is always corrrect... the color is taken care of higher up for render
138  glColor3f(1.0f, 1.0f, 1.0f);
139 
140  /* Draw a quad at location */
141  glBegin(GL_QUADS);
142  glTexCoord2d(0.0f, 0.0f);
143  glVertex2d(mPos.x , mPos.y + i*mFont->size);
144  glTexCoord2d(1.0f, 0.0f);
145  glVertex2d(mPos.x + w, mPos.y + i*mFont->size);
146  glTexCoord2d(1.0f, 1.0f);
147  glVertex2d(mPos.x + w, mPos.y + h + i*mFont->size);
148  glTexCoord2d(0.0f, 1.0f);
149  glVertex2d(mPos.x , mPos.y + h + i*mFont->size);
150  glEnd();
151  glLoadIdentity();
152 
153  /* Bad things happen if we delete the texture before it finishes */
154  glFinish();
155 
156  /* Clean up */
157  SDL_FreeSurface(initial);
158  SDL_FreeSurface(intermediary);
159  glDeleteTextures(1, &texture.image);
160  }
161  lastline = i++;
162  }
163  }
164 }
165 
167 {
168  if(mUnused) return;
170 }
171 
172 void Text::setStartLine(unsigned int line){
173  if(line < lines.size())
174  startline=line;
175 }
176 
178  return lastline;
179 }
180 
182 void Text::wrap(){
183  //vector<string> text();
185  //foritr(line, lines){
186  // string txt = *line;
187  // int w,h;
188  // TTF_SizeUTF8(mFont->font,txt.c_str(), &w, &h);
189  // while(w>width){
190 
191  // }
192  // int a=0;
193  //}
194 }