2 * Copyright © 2010-2013 Team XBMC
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "angelscript.h"
25 #define GRID_WIDTH (32)
26 #define GRID_HEIGHT (24)
28 #define NUM_INDICES (1584)
30 #define TEX_WIDTH ( 1024 )
31 #define TEX_HEIGHT ( 512 )
35 m_iCurrentTexture = 0;
37 m_tex1 = Renderer::CreateTexture(TEX_WIDTH, TEX_HEIGHT);
38 m_tex2 = Renderer::CreateTexture(TEX_WIDTH, TEX_HEIGHT);
44 Renderer::GetDevice()->CreateIndexBuffer( NUM_INDICES * 2, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_iBuffer, NULL );
45 Renderer::GetDevice()->CreateVertexBuffer( GRID_WIDTH * GRID_HEIGHT * sizeof( PosColNormalUVVertex ),
46 D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, 0, D3DPOOL_DEFAULT,
49 PosColNormalUVVertex* v;
50 m_vBuffer->Lock( 0, 0, (void**)&v, 0 );
52 for (int y = 0; y < GRID_HEIGHT; y++)
54 for (int x = 0; x < GRID_WIDTH; x ++)
56 // 0-(32 / 2) / (32 / 2) = -1
57 // 15-(32 / 2) / (32 / 2) = -1
58 v->Coord.x = ((float)x - ((GRID_WIDTH -1 ) / 2.0f)) / ((GRID_WIDTH -1 ) / 2.0f);
59 v->Coord.y = -(((float)y - ((GRID_HEIGHT -1 ) / 2.0f)) / ((GRID_HEIGHT -1 ) / 2.0f));
60 // v->Diffuse= 0xffffffff;
67 // v->u = ((((float)x - ((GRID_WIDTH -1 ) / 2.0f)) / ((GRID_WIDTH -1 ) / 2.0f) * 256 + 256) * (1.0f / 512)) + (0.5f / 512);
68 // v->v = ((((float)y - ((GRID_HEIGHT -1 ) / 2.0f)) / ((GRID_HEIGHT -1 ) / 2.0f) * 256 + 256) * (1.0f / 512)) + (0.5f / 512);
69 v->u = ((float)x / (GRID_WIDTH -1));// + (0.5f / TEX_SIZE);
70 v->v = ((float)y / (GRID_HEIGHT -1));// + (0.5f / TEX_SIZE) ;//+ (5 / 512.0f);
77 //---------------------------------------------------------------------------
79 unsigned short* pIndices;
80 m_iBuffer->Lock( 0, 0, (void**)&pIndices, 0 );
82 unsigned short iIndex = 0;
84 for (int a = 0; a < GRID_HEIGHT; ++a)
86 for (int i = 0; i < GRID_WIDTH; ++i, pIndices += 2, ++iIndex )
88 pIndices[ 0 ] = iIndex + GRID_WIDTH;
89 pIndices[ 1 ] = iIndex;
93 // connect two strips by inserting two degenerate triangles
94 if (GRID_WIDTH - 2 > a)
96 pIndices[0] = iIndex - 1;
97 pIndices[1] = iIndex + GRID_WIDTH;
104 m_numIndices = numIndices - 2;
111 if ( m_vertices != NULL )
116 m_vBuffer->Release();
117 m_iBuffer->Release();
122 __inline float MapCol( float a )
124 // static float speed = 1.02f;
125 float res = powf(1.02f, a);
133 __inline float Clamp( float a )
147 void Map::SetValues(unsigned int x, unsigned int y, float uOffset, float vOffset, float r, float g, float b)
149 int index = ((y )* GRID_WIDTH) + x;
150 if( index >= GRID_HEIGHT * GRID_WIDTH )
155 if( m_vertices == NULL )
157 m_vBuffer->Lock( 0, 0, (void**)&m_vertices, 0 );
160 PosColNormalUVVertex* v = &m_vertices[ index ];
162 static const float invWidth = 1.0f / ( GRID_WIDTH - 1 );
163 static const float invHeight = 1.0f / ( GRID_HEIGHT - 1 );
167 v->u = ((float)x * invWidth ) + (0.5f / TEX_WIDTH) + ( ( uOffset ) * invWidth );
168 v->v = ((float)y * invHeight ) + (0.5f / TEX_HEIGHT) + ( ( vOffset ) * invHeight );
169 /* unsigned char* col = (unsigned char*)&v->Diffuse;
171 col[2] = int(MapCol(r) * 255);
172 col[1] = int(MapCol(g) * 255);
173 col[0] = int(MapCol(b) * 255);
175 v->FDiffuse.x = MapCol(r);
176 v->FDiffuse.y = MapCol(g);
177 v->FDiffuse.z = MapCol(b);
178 v->FDiffuse.w = 1.0f;
182 v->u = ((float)x * invWidth ) + (0.5f / TEX_WIDTH) + ((uOffset));
183 v->v = ((float)y * invHeight ) + (0.5f / TEX_HEIGHT) + ((vOffset));
184 // unsigned char* col = (unsigned char*)&v->Diffuse;
186 // col[2] = int(Clamp(r) * 255);
187 // col[1] = int(Clamp(g) * 255);
188 // col[0] = int(Clamp(b) * 255);
198 if ( m_vertices != NULL )
204 if ( m_iCurrentTexture == 0 )
206 Renderer::SetRenderTarget(m_tex1);
207 Renderer::SetTexture(m_tex2);
212 Renderer::SetRenderTarget(m_tex2);
213 Renderer::SetTexture(m_tex1);
217 Renderer::PushMatrix();
218 Renderer::SetIdentity();
219 float oldAspect = Renderer::GetAspect();
220 Renderer::SetAspect(0);
221 Renderer::Translate(0, 0, 2.414f);
223 DiffuseUVVertexShader* pShader = &DiffuseUVVertexShader::StaticType;
224 Renderer::CommitTransforms( pShader );
225 Renderer::CommitTextureState();
226 Renderer::GetDevice()->SetVertexShader( pShader->GetVertexShader() );
227 Renderer::GetDevice()->SetVertexDeclaration( g_pPosColUVDeclaration );
229 Renderer::GetDevice()->SetStreamSource( 0,
232 sizeof( PosColNormalUVVertex ) );
234 Renderer::GetDevice()->SetIndices( m_iBuffer );
236 Renderer::GetDevice()->DrawIndexedPrimitive( D3DPT_TRIANGLESTRIP, 0, 0, GRID_WIDTH * GRID_HEIGHT, 0, 1514 );
238 Renderer::GetDevice()->SetStreamSource( 0,
243 Renderer::GetDevice()->SetIndices( NULL );
245 Renderer::SetTexture(NULL);
246 Renderer::PopMatrix();
247 Renderer::SetAspect(oldAspect);
249 m_iCurrentTexture = 1 - m_iCurrentTexture;
253 //-----------------------------------------------------------------------------
258 /* The class constructor is initializing the reference counter to 1*/
262 void Map::RegisterScriptInterface( asIScriptEngine* pScriptEngine )
269 // Registering the reference type
270 r = pScriptEngine->RegisterObjectType("Map", 0, asOBJ_REF); assert(r >= 0);
272 // Registering the factory behaviour
273 r = pScriptEngine->RegisterObjectBehaviour("Map", asBEHAVE_FACTORY, "Map@ f()", asFUNCTION(Map_Factory), asCALL_CDECL); assert( r >= 0 );
275 // Registering the addref/release behaviours
276 r = pScriptEngine->RegisterObjectBehaviour("Map", asBEHAVE_ADDREF, "void f()", asMETHOD(Map, AddRef), asCALL_THISCALL); assert(r >= 0);
277 r = pScriptEngine->RegisterObjectBehaviour("Map", asBEHAVE_RELEASE, "void f()", asMETHOD(Map, Release), asCALL_THISCALL); assert(r >= 0);
278 r = pScriptEngine->RegisterObjectMethod("Map", "void Render()", asMETHOD(Map, Render), asCALL_THISCALL); assert(r >= 0);
279 r = pScriptEngine->RegisterObjectMethod("Map", "void SetValues(int, int, float, float, float, float, float)", asMETHOD(Map, SetValues), asCALL_THISCALL); assert(r >= 0);
280 r = pScriptEngine->RegisterObjectMethod("Map", "void SetTimed()", asMETHOD(Map, SetTimed), asCALL_THISCALL); assert(r >= 0);
282 r = pScriptEngine->RegisterObjectBehaviour("Map", asBEHAVE_IMPLICIT_REF_CAST, "EffectBase@ f()", asFUNCTION((refCast<Map,EffectBase>)), asCALL_CDECL_OBJLAST); assert( r >= 0 );