JNR
laLevel__draw_anim.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 //
31 // FILE: laLevel.cpp
32 //
33 // This class represents the entire level; Each level is
34 // composed of several sequential segments, arragned along the X
35 // axis; Segments are organized as a list of laSecment objects
36 //
37 // The level object manages this list, as well as dynamic object
38 // and the tileset object (laTitleset)
39 //
40 // Copyright (C) 2007-2013 Atanas Laskov, <latanas@gmail.com>
41 //
42 #include "stdafx.h"
43 #include "Core-Level-JR.h"
44 #include "laStagesJR.h"
45 
46 void laLevel::drawSky(laRenderer *r) {
47  double dOldFilter = r->modeBlendFilter(0); // get rid of the default GUI alpha filter for the sky
48  _pTileSet->getSky()->draw(r);
49  r->modeBlendFilter(dOldFilter);
50 }
51 
52 void laLevel::drawInterface(laRenderer *r, laPoint3 ptBasePos) {
53  ERRORLEVEL_BEGIN;
54 
55  for(unsigned i=0; i<_nObjCnt; i++)
56  _arObjects[i]->drawInterface(r, ptBasePos);
57 
58  ERRORLEVEL_END;
59 }
60 
61 void laLevel::draw(laRenderer *r, laSegment* pfirst, unsigned nSegments)
62 {
63  ERRORLEVEL_BEGIN;
64  PROFILE_REN(laLevel_draw);
65  ASSERT(pfirst, "Nil first segment");
66  ASSERT(nSegments, "Zero semgnets to draw, must be at least one");
67  static laSegment *ps;
68  static unsigned n;
69 
70  r->transPush();
71  _setup_scene(r);
72  getTS()->getBackgroundElement()->drawGeometry(r);
73  _scroll_view(r);
74 
75  // Pass 1 - Background, Terrain, Object geometry
76  //
77  ps = pfirst; n = nSegments;
78  _draw_pass_1( r );
79 
80  laPoint3 ofs1( (ps->_nSegmentIndex)*M_UNIT*M_SEGW, 0, 0 ), ofs( M_UNIT*M_SEGW, 0, 0 );
81  r->transPush();
82  r->transTranslate( ofs1 );
83 
84  do{
85  getTS()->getElement( ps->getBackgroundElement() )->drawGeometry(r, 0);
86  ps->drawTerrain(r);
87  r->transTranslate( ofs );
88  ps = ps->next();
89  } while( ps && (--n) );
90 
91  r->transPop();
92 
93  // Pass 1a - Object geometry
94  // NOTE: Fog and vtex should be disabled; Some of the light setup could be different
95  //
96  ps = pfirst; n = nSegments;
97  _draw_pass_1a( r );
98 
99  do{
100  ps->drawObjects(r);
101  ps = ps->next();
102  } while( ps && (--n) );
103 
104  // Pass 2 - Fx, shadows, etc. pransparent stuff
105  //
106  ps = pfirst; n = nSegments;
107  _draw_pass_2( r );
108 
109  do{
110  // NOTE: Can't really Translate segments here, because objects are also drawn in the same call
111  // and they are not positioned on segment boundaries
112  //
113  laPoint3 pos( (ps->_nSegmentIndex)*M_UNIT*M_SEGW, 0, 0 );
114  r->transPush();
115  r->transTranslate(pos);
116  getTS()->getElement( ps->getBackgroundElement() )->drawFx(r);
117  r->transPop();
118  ps->drawFx(r, pos);
119  ps = ps->next();
120  } while( ps && (--n) );
121 
122  r->transPop();
123  getTS()->getBackgroundElement()->drawFx(r);
124 
125  ERRORLEVEL_END;
126 }
127 
128 void laLevel::_setup_scene(laRenderer *r)
129 {
130  // Directional light
131  //
132  r->lightMakeSunlight(0,_pTileSet->getLightElement()->getPoint("light-vector"));
133 
134  r->lightAmbient(0, _pTileSet->getLightElement()->getColor("light-ambient") );
135  r->lightDiffuse(0, _pTileSet->getLightElement()->getColor("light-diffuse") );
136  r->lightSpecular(0, _pTileSet->getLightElement()->getColor("light-specular") );
137 
138  // Secondary light source follows the player
139  // NOTE: Pre-renderend terrain cant use this?
140  //
141  /*double dZOfs = terrainZOffset(getPlayer()->getPosition().x() / M_UNIT ) + 4*M_UNIT;
142  double dAng = terrainAngle(getPlayer()->getPosition().x() / M_UNIT );
143 
144  laPoint3 o(0, -1*M_UNIT, dZOfs);
145  laPoint3 v(-M_UNIT*sin(dAng), 1*M_UNIT, -M_UNIT*cos(dAng));
146  r->lightMakeProjector(1, o,v, 360);
147 
148  r->lightAmbient(1, laColor(0,0,0));
149  r->lightDiffuse(1, _pTileSet->getLightElement()->getColor("light-spotlight") );
150  r->lightSpecular(1, _pTileSet->getLightElement()->getColor("light-spotlight") );
151  r->lightAttenuation(1, 4*M_UNIT);
152  r->drawLine( o, o + v );*/
153 }
154 
155 // Pass 1 - Terrain
156 //
157 void laLevel::_draw_pass_1( laRenderer *r )
158 {
159  PROFILE_REN(laLevel__draw_pass_1);
160 
161  r->modeLight(M_TRUE);
162 
163  // Set a blend filter for mobs and other objects that have transparent areas in the texture
164  // This prevents some blending problems
165  // NOTE: This is off for modeWorld by default
166  //
167  r->modeBlendFilter(0.15);
168 
169  // Setup noise/defect texture and style to white
170  //
171  r->vtex(_pNoiseTex, getTS()->getBackgroundElement()->getDouble("terrain-noise-scale"));
172  r->styleSet( laColor(255,255,255,255) );
173  r->modeOutline( laOutline(M_FALSE) );
174 
175  // Fog
176  //
177  laElement *pef = _pTileSet->getFogElement();
178 
179  r->modeFog(M_TRUE);
180  r->fogColor( pef->getColor("fog-color") );
181  r->fogRange( pef->getDouble("foreground-fog-near")*M_UNIT ,pef->getDouble("foreground-fog-far")*M_UNIT);
182 }
183 
184 // Pass 1a - Objects
185 //
186 void laLevel::_draw_pass_1a( laRenderer *r )
187 {
188  PROFILE_REN(laLevel__draw_pass_1a);
189 
190  r->modeFog(M_FALSE);
191  r->vtex(NULL);
192  r->lightAmbient(0, getTS()->getLightElement()->getColor("light-ambient-obj")); // NOTE: Obj are brigter
193 }
194 
195 void laLevel::_draw_pass_2( laRenderer *r )
196 {
197  PROFILE_REN(laLevel__draw_pass_2);
198 
199  r->modeLight(M_FALSE);
200  r->modeOutline( laOutline(M_FALSE) );
201 
202  // Blend iflter is off by default for drawing the Fx
203  // NOTE: Particle Fx set and restore a custom value where needed
204  //
205  double dOldFIlter = r->modeBlendFilter(0);
206  //r->modeBlendFilter(dOldFIlter);
207 }
208 
209 // Animate a segment range
210 //
211 //void laLevel::animate(laTimer &t, unsigned index, unsigned nleft, unsigned nright)
212 void laLevel::animate(laTimer &t, laSegment* pfirst, unsigned nSegments)
213 {
214  ERRORLEVEL_BEGIN;
215  PROFILE_ANIM(laLevel_animate);
216  ASSERT(pfirst, "Nil first segment");
217  ASSERT(nSegments, "Zero semgnets to animate, must be at least one");
218 
219  laSegment *ps = pfirst;
220 
221  do{
222  ps->animate(t);
223  ps = ps->next();
224  } while( ps && (--nSegments) );
225 
226  // Animate the tileset
227  getTS()->animate(t);
228 
229  ERRORLEVEL_END;
230 }
virtual void modeFog(M_BOOL bOn)=0
Enable/disable fox.
#define M_UNIT
Unit of 1 meter.
Base Class for Tileset Elements.
Definition: laElement.h:48
Object outline properties.
Definition: laRenderer.h:66
Terrain Semgnet.
Definition: laSegment.h:48
virtual void modeOutline(const laOutline &outline)
Set outline parameters.
Definition: laRenderer.h:174
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
virtual void modeLight(M_BOOL bOn)=0
Enable/disable lighting.
virtual double modeBlendFilter(const double &dMinVisisbleAlpha)=0
Set a "blend filter" (cutting out elements with opacity lower than dMinVisisbleAlpha) ...