Files
itgmania212121/stepmania/src/GraphDisplay.cpp
T

319 lines
8.9 KiB
C++
Raw Normal View History

2003-10-22 22:02:26 +00:00
#include "global.h"
#include "GraphDisplay.h"
#include "ThemeManager.h"
#include "RageTextureManager.h"
#include "RageDisplay.h"
2005-08-29 22:51:35 +00:00
#include "ActorUtil.h"
2003-10-22 22:02:26 +00:00
#include "RageUtil.h"
2005-08-29 22:51:35 +00:00
#include "RageLog.h"
#include "RageMath.h"
2003-12-22 23:27:54 +00:00
#include "StageStats.h"
2005-04-27 21:46:35 +00:00
#include "Foreach.h"
#include "song.h"
2005-08-29 22:51:35 +00:00
#include "XmlFile.h"
2003-10-22 22:02:26 +00:00
//#define DIVIDE_LINE_WIDTH THEME->GetMetricI(m_sName,"TexturedBottomHalf")
2005-08-29 22:51:35 +00:00
REGISTER_ACTOR_CLASS( GraphDisplay )
2003-10-22 22:02:26 +00:00
enum { VALUE_RESOLUTION=100 };
2005-08-29 22:51:35 +00:00
class GraphLine: public Actor
2003-10-22 22:02:26 +00:00
{
2005-08-29 22:51:35 +00:00
public:
enum { iSubdivisions = 4 };
enum { iCircleVertices = iSubdivisions+2 };
2005-08-29 22:51:35 +00:00
void DrawPrimitives()
{
Actor::SetGlobalRenderStates(); // set Actor-specified render states
2003-10-22 22:02:26 +00:00
2005-08-29 22:51:35 +00:00
DISPLAY->ClearAllTextures();
2003-10-22 22:02:26 +00:00
2005-08-29 22:51:35 +00:00
// Must call this after setting the texture or else texture
// parameters have no effect.
Actor::SetTextureRenderStates();
for( unsigned i = 0; i < m_Quads.size(); ++i )
m_Quads[i].c = this->m_pTempState->diffuse[0];
for( unsigned i = 0; i < m_pCircles.size(); ++i )
m_pCircles[i].c = this->m_pTempState->diffuse[0];
2005-08-29 22:51:35 +00:00
DISPLAY->DrawQuads( &m_Quads[0], m_Quads.size() );
int iFans = m_pCircles.size() / iCircleVertices;
for( int i = 0; i < iFans; ++i )
DISPLAY->DrawFan( &m_pCircles[0]+iCircleVertices*i, iCircleVertices );
}
static void MakeCircle( const RageSpriteVertex &v, RageSpriteVertex *pVerts, int iSubdivisions, float fRadius )
{
pVerts[0] = v;
for( int i = 0; i < iSubdivisions+1; ++i )
{
const float fRotation = float(i) / iSubdivisions * 2*PI;
const float fX = RageFastCos(fRotation) * fRadius;
const float fY = -RageFastSin(fRotation) * fRadius;
pVerts[1+i] = v;
pVerts[1+i].p.x += fX;
pVerts[1+i].p.y += fY;
}
}
void Set( const RageSpriteVertex *m_LineStrip, int iSize )
{
m_pCircles.resize( iSize * iCircleVertices );
for( int i = 0; i < iSize; ++i )
{
MakeCircle( m_LineStrip[i], &m_pCircles[0] + iCircleVertices*i, iSubdivisions, 1 );
}
int iNumLines = iSize-1;
m_Quads.resize( iNumLines * 4 );
2005-09-11 23:04:49 +00:00
for( int i = 0; i < iNumLines; ++i )
{
const RageSpriteVertex &p1 = m_LineStrip[i];
const RageSpriteVertex &p2 = m_LineStrip[i+1];
float opp = p2.p.x - p1.p.x;
float adj = p2.p.y - p1.p.y;
float hyp = powf(opp*opp + adj*adj, 0.5f);
float lsin = opp/hyp;
float lcos = adj/hyp;
RageSpriteVertex *v = &m_Quads[i*4];
v[0] = v[1] = p1;
v[2] = v[3] = p2;
int iLineWidth = 2;
float ydist = lsin * iLineWidth/2;
float xdist = lcos * iLineWidth/2;
v[0].p.x += xdist;
v[0].p.y -= ydist;
v[1].p.x -= xdist;
v[1].p.y += ydist;
v[2].p.x -= xdist;
v[2].p.y += ydist;
v[3].p.x += xdist;
v[3].p.y -= ydist;
}
2005-08-29 22:51:35 +00:00
}
private:
vector<RageSpriteVertex> m_Quads;
vector<RageSpriteVertex> m_pCircles;
2005-08-29 22:51:35 +00:00
};
2003-10-22 22:02:26 +00:00
2005-08-29 22:51:35 +00:00
class GraphBody: public Actor
2003-10-22 22:02:26 +00:00
{
2005-08-29 22:51:35 +00:00
public:
GraphBody()
{
for( int i = 0; i < 2*VALUE_RESOLUTION; ++i )
{
m_Slices[i].c = RageColor(1,1,1,1);
m_Slices[i].t = RageVector2( 0,0 );
}
}
void DrawPrimitives()
{
Actor::SetGlobalRenderStates(); // set Actor-specified render states
2003-10-22 22:02:26 +00:00
2005-08-29 22:51:35 +00:00
DISPLAY->ClearAllTextures();
// Must call this after setting the texture or else texture
// parameters have no effect.
Actor::SetTextureRenderStates();
DISPLAY->SetTextureModeModulate();
2006-09-13 03:11:38 +00:00
DISPLAY->DrawQuadStrip( m_Slices, ARRAYLEN(m_Slices) );
2005-08-29 22:51:35 +00:00
}
RageSpriteVertex m_Slices[2*VALUE_RESOLUTION];
};
GraphDisplay::GraphDisplay()
{
m_pGraphLine = new GraphLine;
m_pGraphBody = new GraphBody;
}
2005-04-27 21:46:35 +00:00
2005-08-29 22:51:35 +00:00
GraphDisplay::~GraphDisplay()
{
FOREACH( Actor*, m_vpSongBoundaries, p )
2005-04-27 21:46:35 +00:00
SAFE_DELETE( *p );
m_vpSongBoundaries.clear();
2005-08-29 22:51:35 +00:00
SAFE_DELETE( m_pGraphLine );
SAFE_DELETE( m_pGraphBody );
}
2005-04-27 21:46:35 +00:00
void GraphDisplay::LoadFromNode( const XNode* pNode )
2005-08-29 22:51:35 +00:00
{
ActorFrame::LoadFromNode( pNode );
2005-08-29 22:51:35 +00:00
const XNode *pChild = pNode->GetChild( "Body" );
if( pChild == NULL )
2006-10-15 05:37:35 +00:00
RageException::Throw( "%s: GraphDisplay: missing the node \"Body\"", ActorUtil::GetWhere(pNode).c_str() );
m_pGraphBody->LoadFromNode( pChild );
2005-08-29 22:51:35 +00:00
this->AddChild( m_pGraphBody );
pChild = pNode->GetChild( "Texture" );
if( pChild == NULL )
2006-10-15 05:37:35 +00:00
RageException::Throw( "%s: GraphDisplay: missing the node \"Texture\"", ActorUtil::GetWhere(pNode).c_str() );
m_sprTexture.LoadActorFromNode( pChild, this );
2005-08-29 22:51:35 +00:00
m_size.x = m_sprTexture->GetUnzoomedWidth();
m_size.y = m_sprTexture->GetUnzoomedHeight();
this->AddChild( m_sprTexture );
pChild = pNode->GetChild( "Line" );
if( pChild == NULL )
2006-10-15 05:37:35 +00:00
RageException::Throw( "%s: GraphDisplay: missing the node \"Line\"", ActorUtil::GetWhere(pNode).c_str() );
m_pGraphLine->LoadFromNode( pChild );
2005-08-29 22:51:35 +00:00
this->AddChild( m_pGraphLine );
pChild = pNode->GetChild( "SongBoundary" );
if( pChild == NULL )
2006-10-15 05:37:35 +00:00
RageException::Throw( "%s: GraphDisplay: missing the node \"SongBoundary\"", ActorUtil::GetWhere(pNode).c_str() );
m_sprSongBoundary.LoadActorFromNode( pChild, this );
2005-08-29 22:51:35 +00:00
pChild = pNode->GetChild( "Barely" );
if( pChild == NULL )
2006-10-15 05:37:35 +00:00
RageException::Throw( "%s: GraphDisplay: missing the node \"Barely\"", ActorUtil::GetWhere(pNode).c_str() );
m_sprJustBarely.LoadActorFromNode( pChild, this );
2003-10-22 22:02:26 +00:00
}
2005-08-29 22:51:35 +00:00
void GraphDisplay::LoadFromStageStats( const StageStats &ss, const PlayerStageStats &pss )
2003-10-22 22:02:26 +00:00
{
2005-04-27 21:46:35 +00:00
float fTotalStepSeconds = ss.GetTotalPossibleStepsSeconds();
2005-08-29 22:51:35 +00:00
m_Values.resize( VALUE_RESOLUTION );
pss.GetLifeRecord( &m_Values[0], VALUE_RESOLUTION, ss.GetTotalPossibleStepsSeconds() );
2006-09-13 03:11:38 +00:00
for( unsigned i=0; i<ARRAYLEN(m_Values); i++ )
2005-08-29 10:27:46 +00:00
CLAMP( m_Values[i], 0.f, 1.f );
2005-04-27 21:46:35 +00:00
2005-05-07 16:47:48 +00:00
//
// Show song boundaries
//
float fSec = 0;
FOREACH_CONST( Song*, ss.vpPossibleSongs, song )
{
if( song == ss.vpPossibleSongs.end()-1 )
continue;
fSec += (*song)->GetStepsSeconds();
2005-08-29 22:51:35 +00:00
Actor *p = m_sprSongBoundary->Copy();
2005-05-07 16:47:48 +00:00
m_vpSongBoundaries.push_back( p );
float fX = SCALE( fSec, 0, fTotalStepSeconds, m_quadVertices.left, m_quadVertices.right );
2005-08-29 22:51:35 +00:00
p->SetX( fX );
this->AddChild( p );
2005-05-07 16:47:48 +00:00
}
2005-08-29 22:51:35 +00:00
UpdateVerts();
2005-10-01 00:18:13 +00:00
if( !pss.bFailed )
2005-04-27 21:46:35 +00:00
{
//
// Search for the min life record to show "Just Barely!"
//
2005-04-27 21:46:35 +00:00
float fMinLifeSoFar = 1.0f;
2005-05-25 04:58:45 +00:00
int iMinLifeSoFarAt = 0;
2005-08-29 22:51:35 +00:00
for( int i = 0; i < VALUE_RESOLUTION; ++i )
2005-04-27 21:46:35 +00:00
{
2005-08-29 10:27:46 +00:00
float fLife = m_Values[i];
2005-04-27 21:46:35 +00:00
if( fLife < fMinLifeSoFar )
{
fMinLifeSoFar = fLife;
2005-05-25 04:58:45 +00:00
iMinLifeSoFarAt = i;
2005-04-27 21:46:35 +00:00
}
}
2005-05-25 04:58:45 +00:00
if( fMinLifeSoFar > 0.0f && fMinLifeSoFar < 0.1f )
2005-04-27 21:46:35 +00:00
{
2005-08-29 22:51:35 +00:00
float fX = SCALE( float(iMinLifeSoFarAt), 0.0f, float(VALUE_RESOLUTION-1), m_quadVertices.left, m_quadVertices.right );
m_sprJustBarely->SetX( fX );
2005-04-27 21:46:35 +00:00
}
else
{
m_sprJustBarely->SetHidden( true );
}
this->AddChild( m_sprJustBarely );
}
2003-10-22 22:02:26 +00:00
}
void GraphDisplay::UpdateVerts()
{
2005-08-29 22:51:35 +00:00
m_quadVertices.left = -m_size.x/2;
m_quadVertices.right = m_size.x/2;
m_quadVertices.top = -m_size.y/2;
m_quadVertices.bottom = m_size.y/2;
2003-10-22 22:02:26 +00:00
RageSpriteVertex LineStrip[VALUE_RESOLUTION];
2005-08-29 22:51:35 +00:00
for( int i = 0; i < VALUE_RESOLUTION; ++i )
2003-10-22 22:02:26 +00:00
{
2005-08-29 22:51:35 +00:00
const float fX = SCALE( float(i), 0.0f, float(VALUE_RESOLUTION-1), m_quadVertices.left, m_quadVertices.right );
const float fY = SCALE( m_Values[i], 0.0f, 1.0f, m_quadVertices.bottom, m_quadVertices.top );
2003-10-22 22:02:26 +00:00
m_pGraphBody->m_Slices[i*2+0].p = RageVector3( fX, m_quadVertices.top, 0 );
2005-08-29 22:51:35 +00:00
m_pGraphBody->m_Slices[i*2+1].p = RageVector3( fX, fY, 0 );
2003-10-22 22:02:26 +00:00
LineStrip[i].p = RageVector3( fX, fY, 0 );
LineStrip[i].c = RageColor( 1,1,1,1 );
LineStrip[i].t = RageVector2( 0,0 );
2003-10-22 22:02:26 +00:00
}
m_pGraphLine->Set( LineStrip, VALUE_RESOLUTION );
2003-10-22 22:02:26 +00:00
}
2005-08-29 22:51:35 +00:00
// lua start
#include "LuaBinding.h"
2005-02-27 08:31:41 +00:00
2005-08-29 22:51:35 +00:00
class LunaGraphDisplay: public Luna<GraphDisplay>
{
public:
static int LoadFromStats( T* p, lua_State *L )
{
StageStats *pStageStats = Luna<StageStats>::check( L, 1 );
PlayerStageStats *pPlayerStageStats = Luna<PlayerStageStats>::check( L, 2 );
p->LoadFromStageStats( *pStageStats, *pPlayerStageStats );
return 0;
}
2003-10-22 22:02:26 +00:00
2006-09-27 20:03:31 +00:00
LunaGraphDisplay()
2005-08-29 22:51:35 +00:00
{
2006-09-27 20:03:31 +00:00
ADD_METHOD( LoadFromStats );
2005-08-29 22:51:35 +00:00
}
};
2005-04-27 21:46:35 +00:00
2005-08-29 22:51:35 +00:00
LUA_REGISTER_DERIVED_CLASS( GraphDisplay, ActorFrame )
// lua end
2003-10-22 22:02:26 +00:00
2004-06-07 21:14:03 +00:00
/*
* (c) 2003 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/