Files
itgmania212121/stepmania/src/Background.cpp
T

493 lines
13 KiB
C++
Raw Normal View History

2003-02-16 04:01:45 +00:00
#include "global.h"
/*
-----------------------------------------------------------------------------
2002-08-03 22:05:04 +00:00
Class: Background
Desc: Background behind arrows while dancing
2002-05-19 01:59:48 +00:00
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
2002-08-03 22:05:04 +00:00
Chris Danford
-----------------------------------------------------------------------------
*/
#include "Background.h"
#include "RageUtil.h"
2002-05-01 19:14:55 +00:00
#include "GameConstantsAndTypes.h"
2002-02-05 05:33:33 +00:00
#include "RageBitmapTexture.h"
2002-07-27 19:29:51 +00:00
#include "RageException.h"
#include "RageTimer.h"
2003-03-24 20:57:19 +00:00
#include "RageLog.h"
#include "RageTextureManager.h"
#include "GameState.h"
#include "ThemeManager.h"
#include "PrefsManager.h"
#include "NoteTypes.h"
#include <math.h> // for fmodf
2003-06-27 08:06:22 +00:00
#include "DancingCharacters.h"
2003-07-22 07:47:27 +00:00
#include "arch/arch.h"
2002-07-11 19:02:26 +00:00
const float FADE_SECONDS = 1.0f;
2002-08-28 22:42:40 +00:00
#define LEFT_EDGE THEME->GetMetricI("Background","LeftEdge")
#define TOP_EDGE THEME->GetMetricI("Background","TopEdge")
#define RIGHT_EDGE THEME->GetMetricI("Background","RightEdge")
#define BOTTOM_EDGE THEME->GetMetricI("Background","BottomEdge")
2002-10-31 05:52:12 +00:00
#define RECT_BACKGROUND RectI(LEFT_EDGE,TOP_EDGE,RIGHT_EDGE,BOTTOM_EDGE)
2002-07-11 19:02:26 +00:00
const CString STATIC_BACKGROUND = "static background";
2003-05-28 03:35:08 +00:00
CString RandomBackground(int num) { return ssprintf("__random%i", num); }
Background::Background()
{
2002-07-11 19:02:26 +00:00
m_bInDanger = false;
m_iCurBGChangeIndex = -1;
2003-04-13 00:44:50 +00:00
m_pCurrentBGA = NULL;
m_pFadingBGA = NULL;
m_fSecsLeftInFade = 0;
m_BGADanger.LoadFromAniDir( THEME->GetPathToB("ScreenGameplay danger") );
2002-06-24 22:20:51 +00:00
m_quadBGBrightness.StretchTo( RECT_BACKGROUND );
m_quadBGBrightness.SetDiffuse( RageColor(0,0,0,1-PREFSMAN->m_fBGBrightness) );
2002-07-31 22:37:58 +00:00
2002-10-31 05:52:12 +00:00
m_quadBorder[0].StretchTo( RectI(SCREEN_LEFT,SCREEN_TOP,LEFT_EDGE,SCREEN_BOTTOM) );
m_quadBorder[0].SetDiffuse( RageColor(0,0,0,1) );
2002-10-31 05:52:12 +00:00
m_quadBorder[1].StretchTo( RectI(LEFT_EDGE,SCREEN_TOP,RIGHT_EDGE,TOP_EDGE) );
m_quadBorder[1].SetDiffuse( RageColor(0,0,0,1) );
2002-10-31 05:52:12 +00:00
m_quadBorder[2].StretchTo( RectI(RIGHT_EDGE,SCREEN_TOP,SCREEN_RIGHT,SCREEN_BOTTOM) );
m_quadBorder[2].SetDiffuse( RageColor(0,0,0,1) );
2002-10-31 05:52:12 +00:00
m_quadBorder[3].StretchTo( RectI(LEFT_EDGE,BOTTOM_EDGE,RIGHT_EDGE,SCREEN_BOTTOM) );
m_quadBorder[3].SetDiffuse( RageColor(0,0,0,1) );
2003-06-27 08:06:22 +00:00
bool bOneOrMoreChars = false;
for( int p=0; p<NUM_PLAYERS; p++ )
2003-06-28 01:28:18 +00:00
if( GAMESTATE->IsPlayerEnabled(p) )
bOneOrMoreChars = true;
2003-06-27 08:06:22 +00:00
if( bOneOrMoreChars )
m_pDancingCharacters = new DancingCharacters;
else
m_pDancingCharacters = NULL;
2002-07-11 19:02:26 +00:00
}
Background::~Background()
{
Unload();
2003-06-27 09:05:51 +00:00
delete m_pDancingCharacters;
}
void Background::Unload()
2002-07-11 19:02:26 +00:00
{
for( map<CString,BGAnimation*>::iterator iter = m_BGAnimations.begin();
iter != m_BGAnimations.end();
iter++ )
delete iter->second;
2002-10-24 20:15:24 +00:00
m_BGAnimations.clear();
2002-08-28 22:42:40 +00:00
m_aBGChanges.clear();
2003-04-13 00:44:50 +00:00
m_pCurrentBGA = NULL;
m_pFadingBGA = NULL;
2002-06-23 02:57:51 +00:00
}
void Background::LoadFromAniDir( CString sAniDir )
{
Unload();
if( PREFSMAN->m_fBGBrightness == 0 )
return;
2002-09-29 05:06:18 +00:00
BGAnimation* pTempBGA;
pTempBGA = new BGAnimation;
pTempBGA->LoadFromAniDir( sAniDir );
m_BGAnimations[STATIC_BACKGROUND] = pTempBGA;
}
BGAnimation *Background::CreateSongBGA(const Song *pSong, CString sBGName) const
2002-10-30 19:55:19 +00:00
{
BGAnimation *pTempBGA;
// Using aniseg.m_sBGName, search for the corresponding animation.
// Look in this order: movies in song dir, BGAnims in song dir
// movies in RandomMovies dir, BGAnims in BGAnimsDir.
CStringArray asFiles;
2002-12-28 16:07:19 +00:00
// Look for BGAnims in the song dir
GetDirListing( pSong->GetSongDir()+sBGName, asFiles, true, true );
if( !asFiles.empty() )
2002-10-30 19:55:19 +00:00
{
pTempBGA = new BGAnimation;
pTempBGA->LoadFromAniDir( asFiles[0] );
2002-10-30 19:55:19 +00:00
return pTempBGA;
}
2003-04-13 23:22:27 +00:00
// Look for BG movies or static graphics in the song dir
GetDirListing( pSong->GetSongDir()+sBGName, asFiles, false, true );
if( !asFiles.empty() )
2002-10-30 19:55:19 +00:00
{
pTempBGA = new BGAnimation;
2003-04-13 23:22:27 +00:00
CString sThrowAway, sExt;
splitrelpath( asFiles[0], sThrowAway, sThrowAway, sExt );
if( sExt.CompareNoCase("avi")==0 ||
sExt.CompareNoCase("mpg")==0 ||
sExt.CompareNoCase("mpeg")==0 )
pTempBGA->LoadFromMovie( asFiles[0] );
2003-04-13 23:22:27 +00:00
else
pTempBGA->LoadFromStaticGraphic( asFiles[0] );
2002-10-30 19:55:19 +00:00
return pTempBGA;
}
// Look for movies in the RandomMovies dir
GetDirListing( RANDOMMOVIES_DIR+sBGName, asFiles, false, true );
if( !asFiles.empty() )
2002-10-30 19:55:19 +00:00
{
pTempBGA = new BGAnimation;
pTempBGA->LoadFromMovie( asFiles[0] );
2002-10-30 19:55:19 +00:00
return pTempBGA;
}
// Look for BGAnims in the BGAnims dir
GetDirListing( BG_ANIMS_DIR+sBGName, asFiles, true, true );
if( !asFiles.empty() )
2002-10-30 19:55:19 +00:00
{
pTempBGA = new BGAnimation;
pTempBGA->LoadFromAniDir( asFiles[0] );
2002-10-30 19:55:19 +00:00
return pTempBGA;
}
// Look for BGAnims in the BGAnims dir
GetDirListing( VISUALIZATIONS_DIR+sBGName, asFiles, false, true );
if( !asFiles.empty() )
2002-10-30 19:55:19 +00:00
{
pTempBGA = new BGAnimation;
pTempBGA->LoadFromVisualization( asFiles[0] );
2002-10-30 19:55:19 +00:00
return pTempBGA;
}
// There is no background by this name.
2002-10-30 19:55:19 +00:00
return NULL;
}
BGAnimation* Background::CreateRandomBGA() const
{
switch( PREFSMAN->m_BackgroundMode )
{
default:
ASSERT(0);
// fall through
case PrefsManager::BGMODE_OFF:
return NULL;
case PrefsManager::BGMODE_ANIMATIONS:
{
CStringArray arrayPossibleAnims;
GetDirListing( BG_ANIMS_DIR+"*", arrayPossibleAnims, true, true );
// strip out "cvs" and "danger
int i;
for( i=arrayPossibleAnims.size()-1; i>=0; i-- )
if( 0==stricmp(arrayPossibleAnims[i].Right(3),"cvs") || 0==stricmp(arrayPossibleAnims[i].Right(3),"danger") )
arrayPossibleAnims.erase(arrayPossibleAnims.begin()+i,
arrayPossibleAnims.begin()+i+1);
if( arrayPossibleAnims.empty() )
return NULL;
unsigned index = rand() % arrayPossibleAnims.size();
BGAnimation *pTempBGA = new BGAnimation;
pTempBGA->LoadFromAniDir( arrayPossibleAnims[index] );
return pTempBGA;
}
case PrefsManager::BGMODE_MOVIEVIS:
{
CStringArray arrayPossibleMovies;
GetDirListing( VISUALIZATIONS_DIR + "*.avi", arrayPossibleMovies, false, true );
GetDirListing( VISUALIZATIONS_DIR + "*.mpg", arrayPossibleMovies, false, true );
GetDirListing( VISUALIZATIONS_DIR + "*.mpeg", arrayPossibleMovies, false, true );
if( arrayPossibleMovies.empty() )
return NULL;
unsigned index = rand() % arrayPossibleMovies.size();
BGAnimation* pTempBGA = new BGAnimation;
pTempBGA->LoadFromVisualization( arrayPossibleMovies[index] );
return pTempBGA;
}
break;
case PrefsManager::BGMODE_RANDOMMOVIES:
{
CStringArray arrayPossibleMovies;
GetDirListing( RANDOMMOVIES_DIR + "*.avi", arrayPossibleMovies, false, true );
GetDirListing( RANDOMMOVIES_DIR + "*.mpg", arrayPossibleMovies, false, true );
GetDirListing( RANDOMMOVIES_DIR + "*.mpeg", arrayPossibleMovies, false, true );
if( arrayPossibleMovies.empty() )
return NULL;
unsigned index = rand() % arrayPossibleMovies.size();
BGAnimation *pTempBGA = new BGAnimation;
pTempBGA->LoadFromMovie( arrayPossibleMovies[index] );
return pTempBGA;
}
break;
}
}
2002-08-22 09:31:32 +00:00
void Background::LoadFromSong( Song* pSong )
{
2002-08-21 04:11:21 +00:00
Unload();
2002-07-11 19:02:26 +00:00
if( PREFSMAN->m_fBGBrightness == 0 )
return;
TEXTUREMAN->DisableOddDimensionWarning();
2002-12-17 06:25:03 +00:00
const float fXZoom = RECT_BACKGROUND.GetWidth() / (float)SCREEN_WIDTH;
const float fYZoom = RECT_BACKGROUND.GetHeight() / (float)SCREEN_HEIGHT;
2002-08-28 22:42:40 +00:00
CString sSongBGPath = pSong && pSong->HasBackground() ? pSong->GetBackgroundPath() : THEME->GetPathToG("Common fallback background");
// Load the static background that will before notes start and after notes end
2002-10-25 07:35:07 +00:00
{
BGAnimation *pTempBGA = new BGAnimation;
pTempBGA->LoadFromStaticGraphic( sSongBGPath );
m_BGAnimations[STATIC_BACKGROUND] = pTempBGA;
2002-10-25 07:35:07 +00:00
}
// Load random backgrounds
bool bLoadedAnyRandomBackgrounds = false;
{
for( int i=0; i<PREFSMAN->m_iNumBackgrounds; i++ )
{
2003-05-28 03:35:08 +00:00
CString sBGName = RandomBackground(i);
BGAnimation *pTempBGA = CreateRandomBGA();
if( pTempBGA )
{
m_BGAnimations[sBGName] = pTempBGA;
bLoadedAnyRandomBackgrounds = true;
}
}
}
// start off showing the static song background
m_aBGChanges.push_back( BackgroundChange(-10000,STATIC_BACKGROUND) );
if( pSong->HasBGChanges() )
{
// Load all song-specified backgrounds
for( unsigned i=0; i<pSong->m_BackgroundChanges.size(); i++ )
2002-07-11 19:02:26 +00:00
{
BackgroundChange change = pSong->m_BackgroundChanges[i];
CString sBGName = change.m_sBGName;
bool bIsAlreadyLoaded = m_BGAnimations.find(sBGName) != m_BGAnimations.end();
if( !bIsAlreadyLoaded )
2002-10-25 07:35:07 +00:00
{
BGAnimation *pTempBGA = CreateSongBGA(pSong, sBGName);
if( pTempBGA )
m_BGAnimations[sBGName] = pTempBGA;
else // the background was not found. Use a random one instead
if( bLoadedAnyRandomBackgrounds )
sBGName = RandomBackground( rand()%PREFSMAN->m_iNumBackgrounds );
else
sBGName = STATIC_BACKGROUND;
2002-10-25 07:35:07 +00:00
}
m_aBGChanges.push_back( change );
}
}
else // pSong doesn't have an animation plan
{
const float fFirstBeat = pSong->m_fFirstBeat;
const float fLastBeat = pSong->m_fLastBeat;
2003-04-20 00:49:43 +00:00
// const int iSequenceLength = 8;
// const int iSequence[iSequenceLength] = {0,1,0,1,2,3,2,3};
2003-04-14 22:12:54 +00:00
int ctr=0;
// change BG every 4 bars
for( float f=fFirstBeat; f<fLastBeat; f+=BEATS_PER_MEASURE*4 )
{
if( bLoadedAnyRandomBackgrounds )
{
2003-05-28 03:35:08 +00:00
CString sBGName = RandomBackground( ctr );
// Don't fade. It causes frame rate dip, especially on
// slower machines.
bool bFade = false;
//bool bFade = PREFSMAN->m_BackgroundMode==PrefsManager::BGMODE_RANDOMMOVIES ||
// PREFSMAN->m_BackgroundMode==PrefsManager::BGMODE_MOVIEVIS;
2003-04-14 07:11:04 +00:00
m_aBGChanges.push_back( BackgroundChange(f,sBGName,1.f,bFade) );
ctr = (ctr+1)%PREFSMAN->m_iNumBackgrounds;
}
}
// change BG every BPM change that is at the beginning of a measure
for( unsigned i=0; i<pSong->m_BPMSegments.size(); i++ )
{
const BPMSegment& bpmseg = pSong->m_BPMSegments[i];
if( fmodf(bpmseg.m_fStartBeat, (float)BEATS_PER_MEASURE) != 0 )
continue; // skip
if( bpmseg.m_fStartBeat < fFirstBeat || bpmseg.m_fStartBeat > fLastBeat )
continue; // skip
if( bLoadedAnyRandomBackgrounds )
{
CString sBGName = RandomBackground( rand()%PREFSMAN->m_iNumBackgrounds );
m_aBGChanges.push_back( BackgroundChange(bpmseg.m_fStartBeat,sBGName) );
}
}
}
// end showing the static song background
m_aBGChanges.push_back( BackgroundChange(pSong->m_fLastBeat,STATIC_BACKGROUND) );
// sort segments
SortBackgroundChangesArray( m_aBGChanges );
// scale all rates by the current music rate
for( unsigned i=0; i<m_aBGChanges.size(); i++ )
m_aBGChanges[i].m_fRate *= GAMESTATE->m_SongOptions.m_fMusicRate;
for( map<CString,BGAnimation*>::iterator iter = m_BGAnimations.begin();
iter != m_BGAnimations.end();
iter++ )
2002-08-28 22:42:40 +00:00
{
iter->second->SetXY( (float)LEFT_EDGE, (float)TOP_EDGE );
iter->second->SetZoomX( fXZoom );
iter->second->SetZoomY( fYZoom );
2002-08-28 22:42:40 +00:00
}
m_BGADanger.SetXY( (float)LEFT_EDGE, (float)TOP_EDGE );
m_BGADanger.SetZoomX( fXZoom );
m_BGADanger.SetZoomY( fYZoom );
TEXTUREMAN->EnableOddDimensionWarning();
}
2002-07-11 19:02:26 +00:00
void Background::Update( float fDeltaTime )
{
ActorFrame::Update( fDeltaTime );
if( IsDangerVisible() )
2002-06-24 22:20:51 +00:00
{
m_BGADanger.Update( fDeltaTime );
2002-06-24 22:20:51 +00:00
}
else
2002-07-11 19:02:26 +00:00
{
2003-02-07 23:49:38 +00:00
if( GAMESTATE->m_fMusicSeconds == GameState::MUSIC_SECONDS_INVALID )
return; /* hasn't been updated yet */
2003-04-13 00:44:50 +00:00
if( m_aBGChanges.size() == 0 )
return;
/* Only update BGAnimations if we're not in the middle of a stop. */
if( !GAMESTATE->m_bFreeze )
{
// Find the BGSegment we're in
int i;
int size = (int)(m_aBGChanges.size()) - 1;
for( i=0; i<size; i++ )
if( GAMESTATE->m_fSongBeat < m_aBGChanges[i+1].m_fStartBeat )
break;
2002-06-24 22:20:51 +00:00
if( i != m_iCurBGChangeIndex )
{
LOG->Trace( "old bga %d -> new bga %d, %f, %f", i, m_iCurBGChangeIndex, m_aBGChanges[i].m_fStartBeat, GAMESTATE->m_fSongBeat );
2002-06-24 22:20:51 +00:00
m_iCurBGChangeIndex = i;
const BackgroundChange& change = m_aBGChanges[i];
BGAnimation* pOld = m_pCurrentBGA;
if( change.m_bFadeLast )
m_pFadingBGA = m_pCurrentBGA;
else
m_pFadingBGA = NULL;
m_pCurrentBGA = m_BGAnimations[ change.m_sBGName ];
if( pOld != m_pCurrentBGA )
{
if( pOld )
pOld->LosingFocus();
if( m_pCurrentBGA )
m_pCurrentBGA->GainingFocus( change.m_fRate, change.m_bRewindMovie, change.m_bLoop );
}
m_fSecsLeftInFade = m_pFadingBGA!=NULL ? FADE_SECONDS : 0;
}
2002-07-31 22:37:58 +00:00
if( m_pCurrentBGA )
m_pCurrentBGA->Update( fDeltaTime );
if( m_pFadingBGA )
{
m_pFadingBGA->Update( fDeltaTime );
m_fSecsLeftInFade -= fDeltaTime;
float fPercentOpaque = m_fSecsLeftInFade / FADE_SECONDS;
m_pFadingBGA->SetDiffuse( RageColor(1,1,1,fPercentOpaque) );
if( fPercentOpaque <= 0 )
m_pFadingBGA = NULL;
}
}
}
2003-06-27 08:06:22 +00:00
if( m_pDancingCharacters )
m_pDancingCharacters->Update( fDeltaTime );
m_quadBGBrightness.Update( fDeltaTime );
}
2002-05-19 01:59:48 +00:00
void Background::DrawPrimitives()
{
if( PREFSMAN->m_fBGBrightness == 0 )
return;
2002-05-19 01:59:48 +00:00
ActorFrame::DrawPrimitives();
if( IsDangerVisible() )
{
m_BGADanger.Draw();
}
else
2003-04-13 00:44:50 +00:00
{
if( m_pCurrentBGA )
m_pCurrentBGA->Draw();
if( m_pFadingBGA )
m_pFadingBGA->Draw();
}
2003-06-27 08:06:22 +00:00
if( m_pDancingCharacters )
m_pDancingCharacters->Draw();
m_quadBGBrightness.Draw();
for( int i=0; i<4; i++ )
m_quadBorder[i].Draw();
}
bool Background::IsDangerVisible()
2002-06-24 22:20:51 +00:00
{
2002-12-19 23:07:20 +00:00
return m_bInDanger && PREFSMAN->m_bShowDanger && (RageTimer::GetTimeSinceStart() - (int)RageTimer::GetTimeSinceStart()) < 0.5f;
2002-07-11 19:02:26 +00:00
}
void Background::FadeIn()
{
m_quadBGBrightness.BeginTweening( 0.5f );
2003-04-12 06:16:12 +00:00
m_quadBGBrightness.SetDiffuse( RageColor(0,0,0,1-PREFSMAN->m_fBGBrightness) );
2002-07-11 19:02:26 +00:00
}
void Background::FadeOut()
{
m_quadBGBrightness.BeginTweening( 0.5f );
2003-04-12 06:16:12 +00:00
m_quadBGBrightness.SetDiffuse( RageColor(0,0,0,1-0.5f) );
2002-07-11 19:02:26 +00:00
2002-06-24 22:20:51 +00:00
}