JNR
fxAnimatedText.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 #include "stdafx.h"
31 #include "Core.h"
32 
33 fxAnimatedText::fxAnimatedText(void)
34 {
35  _ttLineAnim.parameters(1.5);
36  _ttLineAnim.enable();
37 
38  _ang = 0;
39  _dir = 1;
40 
41  _bFX_Slide = _bFX_Ramble = _bFX_Chaos = M_FALSE;
42 }
43 
44 fxAnimatedText::~fxAnimatedText(void)
45 {
46 }
47 
48 // Animate messages
49 void fxAnimatedText::animate(laTimer &t)
50 {
51  _ttLineAnim.animate(t);
52 
53  if( _ttLineAnim.isElapsed() && (_nVisible<_vLines.size()) )
54  {
55  _nVisible++;
56  _ttLineAnim.reset();
57  }
58 
59  for(unsigned i=0; i<_nVisible; i++)
60  {
61  if(_bFX_Slide)
62  {
63  if( _vLines[i].dBias > 0 ) _vLines[i].dBias -= t.delta()*0.8;
64  else _vLines[i].dBias = 0;
65  }
66  }
67 
68  //if( _ang>6 ) _dir = -1;
69  //if( _ang< -6 ) _dir = +1;
70  _ang += /*_dir **/ t.delta();
71 
72  //double d = _ttLineAnim.reminder();//( /*0.1 +*/ ((double)_vLines.size()-_nVisible )/_vLines.size());
73 }
74 
75 // Draw active messages at their current displacemnts
76 void fxAnimatedText::draw(laRenderer *r, laPoint3 pos)
77 {
78  PROFILE_REN(fxAnimatedText_draw);
79 
80  double sz;
81  double alpha, ofx, ofy;
82 
83  _pFont->ctlAlignH(_align);
84  _pFont->ctlAlignV(M_AT);
85 
86  for(unsigned i=0; i<_nVisible; i++)
87  {
88  // last visible line? ( one that's in the process of showing up )
89  if(i+1 == _nVisible)
90  {
91  alpha = _color[3]- _ttLineAnim.reminder()*_color.a();
92  sz = _dFontSz - (_dFontSz*0.1)*_ttLineAnim.reminder();
93  }
94  else { alpha = _color.a(); sz = _dFontSz; }
95 
96  ofx = ofy = 0;
97 
98  if(_bFX_Slide)
99  {
100  ofx += sin( _ang*_dFX_Slide_speedx ) * (_vLines[i].dBias) * _dFX_Slide_rangex;
101  ofy += sin( _ang*_dFX_Slide_speedy ) * (_vLines[i].dBias) * _dFX_Slide_rangey;
102  }
103 
104  if(_bFX_Ramble)
105  {
106  ofx += sin( _ang*_dFX_Ramble_speedx + (_vLines[i].dRandBias-0.5)*90 ) * _dFX_Ramble_rangex;
107  ofy += sin( _ang*_dFX_Ramble_speedy + (_vLines[i].dRandBias-0.5)*90 ) * _dFX_Ramble_rangey;
108  }
109 
110  _pFont->ctlSize(sz);
111 
112  r->transPush();
113  r->transTranslate( pos + laPoint3(ofx, ofy + i*( _dFontSz * _dLineSpacing )) );
114  if(_bFX_Chaos) r->transRotate( _vLines[i].dRandBias*_dFX_Chaos_arange - (_dFX_Chaos_arange/2.0), laPoint3(0,0,1) );
115 
116  //Drop shadow
117  r->styleSet( laColor(_color.r()*0.4, _color.g()*0.4, _color.b()*0.4, alpha) );
118  _pFont->draw( laPoint3(2,2,0), _vLines[i].strText );
119 
120  //Text line
121  r->styleSet( laColor(_color.r(), _color.g(), _color.b(), alpha) );
122  _pFont->draw( laPoint3(), _vLines[i].strText );
123  r->transPop();
124  }
125 
126 }
127 
128 //Load or reload intro file
129 //
130 void fxAnimatedText::load(laFileParser *fp)
131 {
132  _vLines.clear();
133 
134  while(1)
135  {
136  char strSection[32];
137  fp->readSectionSeparator(strSection);
138 
139  if(!strcmp(strSection, "End")) break;
140 
141  _vLines.push_back( _laIntroTextLine(fp) );
142 
143  //_vLines[_vLines.size()-1].dBias = 1;
144  //_vLines[_vLines.size()-1].dRandBias = (rand()%1000)/1000.0;
145  }
146 
147  reset();
148 }
149 
150 //Locate and replace in the dialogue
151 //This can be used to insert game variables
152 void fxAnimatedText::replace(char* strFind, char* strFormat, ...)
153 {
154  va_list ap;
155  char strTarget[M_ANIMETED_TEXT_MAX_LINE];
156 
157  if (!strFormat) return;
158 
159  va_start(ap, strFormat);
160  vsnprintf(strTarget, M_ANIMETED_TEXT_MAX_LINE-1, strFormat, ap);
161  va_end(ap);
162 
163  for(unsigned i=0; i<_vLines.size(); i++)
164  {
165  char* ps;
166 
167  while( ps = strstr(_vLines[i].strText, strFind) )
168  {
169  //MSG(strTarget);
170  char temp[M_ANIMETED_TEXT_MAX_LINE];
171  unsigned n = (unsigned)(ps - _vLines[i].strText);
172 
173  strncpy(temp, _vLines[i].strText, n);
174  temp[n] = 0;
175 
176  strncat(temp, strTarget, M_ANIMETED_TEXT_MAX_LINE-1);
177  strncat(temp, _vLines[i].strText + n + strlen(strFind), M_ANIMETED_TEXT_MAX_LINE-1);
178 
179  strncpy(_vLines[i].strText, temp, M_ANIMETED_TEXT_MAX_LINE-1);
180  }
181  }
182 }
#define M_AT
Text align top.
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
File Parser.
Definition: laFileParser.h:41