#include "GlobalFunc.h" #include "T2DateTime.h" #include "T2DayLightFilter.h" #include "T2PaletteAnime.h" #include "T2PaletteAnimeDef.h" #include "T2TowerDoc.h" #include "T2TowerMainView.h" #include "T2WeatherFilter.h" #include "T2WorldDef.h" T2PaletteAnime::T2PaletteAnime(T2WorldDef* worldDef, T2DateTime* dateTime) { Init(); mWorldDef = worldDef; mWorldPlugin = (T2WorldPlugin *) mWorldDef->GetPlugin(); mPaletteAnimeDef = worldDef->GetPaletteAnimeDef(); mInterCTab = MakeInterCTab(); mDayLightFilter = new T2DayLightFilter; mWeatherFilter = new T2WeatherFilter; AdjustWindowColor(dateTime); } void T2PaletteAnime::Init() { mWorldDef = NULL; mWorldPlugin = NULL; mPaletteAnimeDef = NULL; mInterCTab = NULL; mPaletteUpdated = false; mSkyState = 0; mSkyCount = 0; mSECount = 0; mSEKind = 0; mSETimeNextTick = 0; mDayLightFilter = NULL; mWeatherFilter = NULL; } /*virtual*/ T2PaletteAnime::~T2PaletteAnime() { if (mInterCTab) free(mInterCTab); if (mDayLightFilter) delete mDayLightFilter; if (mWeatherFilter) delete mWeatherFilter; } LOGPALETTE* T2PaletteAnime::MakeInterCTab() { LOGPALETTE *pal = (LOGPALETTE *) malloc(sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * 256); for (int i = 0; i < 256; i++) { pal->palPalEntry[i].peRed = 0; pal->palPalEntry[i].peGreen = 0; pal->palPalEntry[i].peBlue = 0; if (i < mPaletteAnimeDef->GetNumOfAnimColor()) pal->palPalEntry[i].peFlags = PC_RESERVED; else pal->palPalEntry[i].peFlags = 0; } pal->palVersion = 0x300; // is there a GDI constant for this? pal->palNumEntries = 256; return pal; } BOOL T2PaletteAnime::IsSETime() { BOOL isSETime = false; DWORD ticks = TickCount(); if (mSETimeNextTick <= ticks) { mSETimeNextTick = ticks + 2; mSECount++; if (mSECount >= 60) mSECount = 0; isSETime = true; } return isSETime; } void T2PaletteAnime::ChangeSE() { mPaletteUpdated = mWorldDef->ChangeFastSEProc(this) == BOOL3_1; if ((mSECount % mPaletteAnimeDef->GetSEInterval()) == 0) { mSEKind++; if (mSEKind >= mPaletteAnimeDef->GetNumOfSEKind()) mSEKind = 0; int numOfSEColor = mPaletteAnimeDef->GetNumOfSEColor(); int interIndex = mPaletteAnimeDef->GetNumOfRefrectColor() + mPaletteAnimeDef->GetNumOfSkyColor(); int srcIndex = numOfSEColor * mSEKind; LOGPALETTE *theSEPalette = mPaletteAnimeDef->GetSEPalette(); for (int i = 0; i < numOfSEColor; i++) { mInterCTab->palPalEntry[interIndex + i].peRed = theSEPalette->palPalEntry[srcIndex + i].peRed; mInterCTab->palPalEntry[interIndex + i].peGreen = theSEPalette->palPalEntry[srcIndex + i].peGreen; mInterCTab->palPalEntry[interIndex + i].peBlue = theSEPalette->palPalEntry[srcIndex + i].peBlue; } mWorldDef->ChangeSEProc(this); mPaletteUpdated = true; } } void T2PaletteAnime::RotateDayLightPalette(unsigned int start, unsigned int count) const { RotatePalette(mPaletteAnimeDef->GetDayLightPalette(), start, count); RotatePalette(mInterCTab, start, count); } void T2PaletteAnime::RotatePalette(LOGPALETTE* pal, unsigned int start, unsigned int count) const { PALETTEENTRY theFirstEntry = pal->palPalEntry[start]; for (unsigned int i = start + 1; i < (start + count); i++) { PALETTEENTRY entry = pal->palPalEntry[i]; pal->palPalEntry[i - 1] = entry; } pal->palPalEntry[start + count - 1] = theFirstEntry; } void T2PaletteAnime::AdjustSkyState(T2DateTime* dateTime) { if (mWorldDef->AdjustSkyStateProc(this, dateTime) == BOOL3_NEG1) { mDayLightFilter->SetTime(0, (dateTime->GetRawMinutes() * 1000) / 1440); } } BOOL T2PaletteAnime::IsChangeSkyTime() { BOOL result = false; if (mSkyCount > 0) { mSkyCount--; if ((mSkyCount % 6) == 0) result = true; } return result; } void T2PaletteAnime::ChangeSkyColor() { BOOL3 result = mWorldDef->ChangeSkyColorProc(this); if (result == BOOL3_1) { mPaletteUpdated = true; } else if (result == BOOL3_NEG1) { int n; int numOfSkyCol = mPaletteAnimeDef->GetNumOfSkyColor(); int numOfRefrectCol = mPaletteAnimeDef->GetNumOfRefrectColor(); LOGPALETTE *dayLightPal = mPaletteAnimeDef->GetDayLightPalette(); for (n = 0; n < numOfSkyCol; n++) { RGBQUAD c; c.rgbRed = dayLightPal->palPalEntry[n].peRed; c.rgbGreen = dayLightPal->palPalEntry[n].peGreen; c.rgbBlue = dayLightPal->palPalEntry[n].peBlue; mDayLightFilter->SkyColor(&c); mInterCTab->palPalEntry[n].peRed = c.rgbRed; mInterCTab->palPalEntry[n].peGreen = c.rgbGreen; mInterCTab->palPalEntry[n].peBlue = c.rgbBlue; } for (n = 0; n < numOfRefrectCol; n++) { RGBQUAD c; c.rgbRed = dayLightPal->palPalEntry[n + numOfSkyCol].peRed; c.rgbGreen = dayLightPal->palPalEntry[n + numOfSkyCol].peGreen; c.rgbBlue = dayLightPal->palPalEntry[n + numOfSkyCol].peBlue; mDayLightFilter->RefractionColor(&c); mInterCTab->palPalEntry[n + numOfSkyCol].peRed = c.rgbRed; mInterCTab->palPalEntry[n + numOfSkyCol].peGreen = c.rgbGreen; mInterCTab->palPalEntry[n + numOfSkyCol].peBlue = c.rgbBlue; } mPaletteUpdated = true; } } void T2PaletteAnime::MinuteChanged(T2DateTime* dateTime) { unsigned int minute = dateTime->GetMinutes(); if (mWorldDef->UseOwnSkyPaletteFunction()) { if (minute == 1) AdjustSkyState(dateTime); if (IsChangeSkyTime()) ChangeSkyColor(); } else { if ((minute % 6) == 0) { AdjustSkyState(dateTime); ChangeSkyColor(); } } } void T2PaletteAnime::DoAnimatePalette() { if (mPaletteUpdated) { GetCurrentT2TowerDoc()->UpdatePalette( mPaletteAnimeDef->GetAnimStartIndex(), mPaletteAnimeDef->GetNumOfAnimColor(), mInterCTab->palPalEntry ); mPaletteUpdated = false; } } void T2PaletteAnime::ReplaceDayLightPalette(int id) { mPaletteAnimeDef->ReplaceDayLightPalette(mWorldDef->mModuleHandle, id); mPaletteUpdated = false; } void T2PaletteAnime::SetSkyState(int skyState, int skyCount) { if (mSkyState != skyState) GetCurrentT2TowerDoc()->GetMainView()->Invalidate(true); mSkyState = skyState; mSkyCount = skyCount; } void T2PaletteAnime::AdjustWindowColor(T2DateTime* dateTime) { AdjustSkyState(dateTime); ChangeSkyColor(); ChangeSE(); DoAnimatePalette(); }