ScreenHelloWorld.cpp

Go to the documentation of this file.
00001 #include "global.h"
00002 #include "ScreenHelloWorld.h"
00003 #include "MercuryTheme.h"
00004 #include "MercuryLog.h"
00005 #include "MercuryUtil.h"
00006 #include "MercuryDisplay.h"
00007 #include "MercuryInput.h"
00008 
00009 #define     MC_INPUTMAPPED      1       //convention: MESSAGE CODE
00010 #define     MC_INPUTUNMAPPED    2   
00011 
00012 KeyMappingWithCode( button_a, "0-97" );
00013 
00014 KeyMappingWithCode( button_b, "0-98" );
00015 KeyMappingWithCode( button_c, "0-99" );
00016 KeyMappingWithCode( button_d, "0-100" );
00017 
00018 #include "MercuryObjectFactory.h"
00019 REGISTER_SCREEN_CLASS( ScreenHelloWorld )
00020 
00021 void ScreenHelloWorld::Init()
00022 {
00023     MercuryScreen::Init();
00024 
00025     m_held = false;
00026 
00027     m_sprHello.SetName( "HelloSprite" );
00028     m_sprHello.Init();
00029     m_sprHello.LoadImage( THEME.GetMetricS( GetName(), "HelloImage" ) );
00030     m_sprHello.Tweening.AddCommand(THEME.GetMetricS( GetName(), m_sprHello.GetName()+"OnCommand" ) );
00031     m_sprHello.SetRotX(180);
00032     m_sprHello.SetScale( MercuryPoint(0.3, 0.3, 1) );
00033     AddPerspObject( &m_sprHello );
00034     m_timeElasped = 0;
00035 
00036     Tweening.AddCommand(THEME.GetMetricS( GetName(), "GenericParticle" ) , MercuryTweenState::GLOBAL, "GenericParticle" ) ;
00037     Tweening.AddCommand(THEME.GetMetricS( GetName(), "Firework" ), MercuryTweenState::GLOBAL, "Firework" );
00038 
00039     m_camera.Init();
00040     AddObject(&m_camera);
00041 
00042     LOG.Info( THEME.GetMetricS( GetName(), "InfoComment" ) );
00043 
00044     //Setup a light
00045     m_light.Init();
00046     m_light.SetY(100);
00047     m_light.SetX(-100);
00048     m_light.SetZ(110);
00049     m_light.SetAttenuation(0.001f, Attenuation::LINEAR);
00050 //  m_light.SetStatic(true);
00051     DISPLAY->AddLight(&m_light);
00052 
00053     unsigned int numImg = THEME.GetMetricI( GetName(), "NumberImages" );
00054     m_imagePaths.resize( numImg );
00055     for (unsigned int i = 0; i < numImg; ++i)
00056         m_imagePaths[i] = THEME.GetMetricS( GetName(), ssprintf("Image%d",i) );
00057 
00058     m_delay = THEME.GetMetricF( GetName(), "ShellDelay" );
00059     m_maxPoints = THEME.GetMetricI( GetName(), "MaxPointsInShell" );
00060     m_minPoints = THEME.GetMetricI( GetName(), "MinPointsInShell" );
00061 
00062     this->RegisterMessage( MC_INPUTMAPPED, "mappedinput" );
00063 }
00064 
00065 void ScreenHelloWorld::Update( const float dTime )
00066 {
00067     m_timeElasped += dTime;
00068     if (m_timeElasped >= m_delay)
00069     {
00070         m_timeElasped = 0;
00071         float fCenterX = float( rand()%640 );
00072         float fCenterY = float( rand()%480 );
00073 
00074         float fRadius = float( rand()%100 )+100.0f;
00075         
00076         bool    bMulticolor = false;
00077 
00078         if ( ( rand() % 3 ) == 0 )
00079             bMulticolor = true;
00080 
00081         int iParticles = (rand()%(m_maxPoints-m_minPoints))+m_minPoints;
00082         
00083         float ColorR = float(rand()%100)/100.0f;
00084         float ColorB = float(rand()%100)/100.0f;
00085         float ColorG = float(rand()%100)/100.0f;
00086 
00087         int ModType = rand()%m_imagePaths.size();
00088         float RandScale = (50 + (rand()%100))/150.0f;
00089 
00090         MercuryParticleField* seed = (MercuryParticleField*)Spawn( "MercuryParticleField", "Firework", ORTHOGRAPHIC );
00091         seed->Tweening.AddCommand( THEME.GetMetricS( GetName(), "Firework" ) );
00092 
00093         seed->LoadImage( m_imagePaths[ModType] );
00094 
00095         for ( int i = 0; i < iParticles; i++ )
00096         {
00097             float fLifetime = float( rand()%100 )/100.0f+0.3f;
00098             MercuryObject* c = seed->SpawnParticle();
00099 
00100             c->SetScale( MercuryPoint(RandScale, RandScale, 1.0) );         
00101             
00102             c->SetZ(0);
00103             c->SetY( fCenterY );
00104             c->SetX( fCenterX );
00105 
00106             if ( bMulticolor )
00107             {
00108                 ColorR = float(rand()%100)/100.0f;
00109                 ColorG = float(rand()%100)/100.0f;
00110                 ColorB = float(rand()%100)/100.0f;
00111             }
00112 
00113             float fAngle = float(rand()%200)/200.0f*6.283f;
00114             float fActRadius = powf((float(rand()%200)/200.0f),0.25);
00115             float fNewX = fActRadius*fRadius * COS( fAngle )+fCenterX;
00116             float fNewY = fActRadius*fRadius * SIN( fAngle )+fCenterY;
00117 
00118 //          BENCHMARK.Start( "TWEENING" );
00119             PStack KArgs;
00120             KArgs.PushItemBack( PSElement( ColorR ) );
00121             KArgs.PushItemBack( PSElement( ColorG ) );
00122             KArgs.PushItemBack( PSElement( ColorB ) );
00123             KArgs.PushItemBack( PSElement( fLifetime ) );
00124             KArgs.PushItemBack( PSElement( fNewX ) );
00125             KArgs.PushItemBack( PSElement( fNewY ) );
00126             int rotz = (rand()%1000) - 500;
00127             KArgs.PushItemBack( PSElement( rotz ) );
00128             c->Tweening.ExecuteCommand( "GenericParticle", KArgs );
00129 //          BENCHMARK.End( "TWEENING" );
00130         }
00131     }
00132 
00133     if (m_held)
00134     {
00135         int x,y;
00136         INPUTMAN->GetCursorPosition( x, y );
00137 //      m_camera.Pitch(-(m_y - y)/3.0f);
00138 //      m_camera.Yaw(-(m_x - x)/3.0f);
00139         m_x = x;
00140         m_y = y;
00141     }
00142 
00143 //  if (m_camera.GetAxisRecalc())
00144     {
00145         if (INPUTMAN->IsButtonDown(button_b))
00146             m_camera.MoveX(-.1f);
00147 
00148         if (INPUTMAN->IsButtonDown(button_c))
00149             m_camera.MoveX(.1f);
00150     }
00151 
00152     MercuryScreen::Update( dTime );
00153 }
00154 
00155 void ScreenHelloWorld::Message( int Message, PStack & data, const MString & name )
00156 {
00157     switch ( Message ) 
00158     {
00159     case MC_INPUTMAPPED:
00160         {
00161             InputMessageStruct c(data);
00162             if ( c.code == button_a )
00163             {
00164                 if ( c.type == IET_DOWN )
00165                 {
00166                     m_held = true;
00167                     INPUTMAN->GetCursorPosition( m_x, m_y );
00168                 }
00169                 if ( c.type == IET_RELEASE )
00170                 {
00171                     m_held = false;
00172                 }
00173             }
00174             if ( c.code == button_d )
00175             {
00176 //              if ( c.type == IET_DOWN )
00177 //                  m_camera.SetAxisRecalc(!m_camera.GetAxisRecalc());
00178             }
00179         }
00180     }
00181 }
00182 
00183 /* 
00184  * Copyright (c) 2005-2006, Charles Lohr
00185  * All rights reserved.
00186  *
00187  * Redistribution and use in source and binary forms, with or
00188  * without modification, are permitted provided that the following
00189  * conditions are met:
00190  *  -   Redistributions of source code must retain the above
00191  *      copyright notice, this list of conditions and the following disclaimer.
00192  *  -   Redistributions in binary form must reproduce the above copyright
00193  *      notice, this list of conditions and the following disclaimer in
00194  *      the documentation and/or other materials provided with the distribution.
00195  *  -   Neither the name of the <ORGANIZATION> nor the names of its
00196  *      contributors may be used to endorse or promote products derived from
00197  *      this software without specific prior written permission.
00198  *
00199  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00200  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00201  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00202  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00203  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00204  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00205  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00206  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00207  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00208  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00209  */

Hosted by SourceForge.net Logo