JNR
fxMessages.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 // Add a new message
34 //
35 void fxMessages::add(double dSizeMultiplier, laColor rgb, char* str, ...)
36 {
37  // Format text
38  //
39  va_list ap;
40  char text[256];
41 
42  if (!str) return;
43  va_start(ap, str);
44  vsnprintf(text, 255, str, ap);
45  va_end(ap);
46 
47  // Format message
48  //
49  _laFxMessage msg(text, rgb, dSizeMultiplier);
50 
51  if( !_ttDelay.isElapsed() )
52  {
53  msg.bDelay = M_TRUE;
54  }
55  _ttDelay.reset();
56 
57  // See if there is already an expired message
58  //
59  std::vector<_laFxMessage>::iterator i;
60 
61  for(i=_vMessages.begin(); i!=_vMessages.end(); i++)
62  {
63  double prog = (i->dPassedTime)/_dDispalcementTime;
64 
65  if( (prog < 0.5) && (!strcmp(i->str, text)))
66  {
67  // Duplicate message
68  //
69  i->nx++;
70  return;
71  }
72  else if( prog > 1 )
73  {
74  // Reset the expired message
75  //
76  strcpy(i->str, msg.str);
77  i->dPassedTime = msg.dPassedTime;
78  i->rgbColor = msg.rgbColor;
79  i->dSizeMultiplier = msg.dSizeMultiplier;
80  i->bDelay = msg.bDelay;
81  i->nx = 1;
82  return;
83  }
84  }
85 
86  // Allocate memory for a new message
87  _vMessages.push_back( msg );
88 
89 }
90 
91 // Animate messages
92 //
93 void fxMessages::animate(laTimer &t)
94 {
95  M_BOOL bOneUnblocked = M_FALSE;
96 
97  // Try to unblcok one message
98  std::vector<_laFxMessage>::iterator i;
99  for(i=_vMessages.begin(); i!=_vMessages.end(); i++)
100  {
101  if( (i->bDelay) && (i->dPassedTime >= _ttDelay.getElapseTime()) )
102  {
103  bOneUnblocked = M_TRUE;
104  i->bDelay = M_FALSE;
105  i->dPassedTime = 0;
106  break;
107  }
108  }
109 
110  // Animate the rest
111  for(i=_vMessages.begin(); i!=_vMessages.end(); i++)
112  {
113 
114  if(bOneUnblocked && (i->bDelay)) i->dPassedTime = 0; // the rest wll have to wait a full period;
115  else i->dPassedTime += t.delta(); // reduce delay eriod
116  }
117 
118  _ttDelay.animate(t);
119 }
120 
121 // Draw active messages
122 //
123 void fxMessages::draw(laRenderer *r, laPoint3 pos)
124 {
125  PROFILE_REN(fxMessages_draw);
126 
127  // Setup font
128  if(!_pFont) _pFont = r->font("plain");
129  _pFont->ctlAlignH(_nAlign);
130 
131  // Messages are always on top
132  r->modeDepthBuffer(M_FALSE);
133 
134  // Iterate
135  std::vector<_laFxMessage>::iterator i;
136  for(i=_vMessages.begin(); i!=_vMessages.end(); i++)
137  {
138  // Skip expired messages
139  double prog = (i->dPassedTime)/_dDispalcementTime;
140  if( (prog > 1) || (i->bDelay) ) continue;
141 
142  laPoint3 pt = pos - laPoint3(0, prog*_dDisplacementDY, prog*_dDisplacementDY);
143 
144  // Draw text
145  r->styleSet( laColor(i->rgbColor.r(), i->rgbColor.g(), i->rgbColor.b(), (1-prog)*255) );
146  _pFont->ctlSize(_dFontSize*(i->dSizeMultiplier));
147 
148  if(i->nx > 1) _pFont->draw(pt, "%s (x%d)", i->str, i->nx);
149  else _pFont->draw(pt, "%s", i->str);
150  }
151 
152  //r->styleSet( laColor(255, 255, 255, 255) );
153  //_pFont->draw(pos, "ssdsd" /*"%s", i->str*/);
154 
155 
156  r->modeDepthBuffer(M_TRUE);
157 }
laFont * font(char *strName)
Get a font renderer.
Definition: laRenderer.h:192
virtual void modeDepthBuffer(M_BOOL bOn)=0
Enable/disable depth buffer.
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98