#include "StdAfx.h" #include "T2WeatherFilter.h" T2WeatherFilter::T2WeatherFilter() { mCurrentWeather = WeatherCode_0; mPhase = 2; SetWeather(WeatherCode_0); } T2WeatherFilter::~T2WeatherFilter() { } void T2WeatherFilter::SetWeather(WeatherCode weather) { mLastChange = GetTickCount(); mTimeSinceLastChange = mLastChange; mNextWeather = weather; mLightningTime = -1; } void T2WeatherFilter::ColorFilter(RGBQUAD* rgb) { if (mCurrentWeather == WeatherCode_1) RainyFilter(mTimeSinceLastChange, rgb); else if (mCurrentWeather == WeatherCode_2) SnowyFilter(mTimeSinceLastChange, rgb); else ClearFilter(mTimeSinceLastChange, rgb); } void T2WeatherFilter::ClearFilter(unsigned long time, RGBQUAD* rgb) { } void T2WeatherFilter::RainyFilter(unsigned long time, RGBQUAD* rgb) { if (mLightningTime <= time && time <= (mLightningTime + 200)) { RGBQUAD c; c.rgbRed = 240; c.rgbGreen = 240; c.rgbBlue = 128; ColorBlend(40, rgb, &c); } else { RGBQUAD c; c.rgbRed = (rgb->rgbRed * 500) / 1000; c.rgbGreen = (rgb->rgbGreen * 500) / 1000; c.rgbBlue = (rgb->rgbBlue * 600) / 1000; ColorFade(time, rgb, &c); } } void T2WeatherFilter::SnowyFilter(unsigned long time, RGBQUAD* rgb) { RGBQUAD c; c.rgbRed = ((rgb->rgbRed + ((255 - rgb->rgbRed) * 500) / 1000) * 800) / 1000; c.rgbGreen = ((rgb->rgbGreen + ((255 - rgb->rgbGreen) * 500) / 1000) * 800) / 1000; c.rgbBlue = ((rgb->rgbBlue + ((255 - rgb->rgbBlue) * 500) / 1000) * 800) / 1000; ColorFade(time, rgb, &c); } void T2WeatherFilter::ColorFade(unsigned long time, RGBQUAD* a, RGBQUAD* b) { if ((mPhase == 1) && (time <= 2500)) { a->rgbRed = (a->rgbRed * (2500 - time) + b->rgbRed * time) / 2500; a->rgbGreen = (a->rgbGreen * (2500 - time) + b->rgbGreen * time) / 2500; a->rgbBlue = (a->rgbBlue * (2500 - time) + b->rgbBlue * time) / 2500; } else if ((mPhase == 3) && (time <= 2500)) { a->rgbRed = (b->rgbRed * (2500 - time) + a->rgbRed * time) / 2500; a->rgbGreen = (b->rgbGreen * (2500 - time) + a->rgbGreen * time) / 2500; a->rgbBlue = (b->rgbBlue * (2500 - time) + a->rgbBlue * time) / 2500; } else { *a = *b; } } void T2WeatherFilter::SkyColorFilter(RGBQUAD* rgb) { if (mCurrentWeather == WeatherCode_1) RainySkyFilter(mTimeSinceLastChange, rgb); else if (mCurrentWeather == WeatherCode_2) SnowySkyFilter(mTimeSinceLastChange, rgb); else ClearSkyFilter(mTimeSinceLastChange, rgb); } void T2WeatherFilter::ClearSkyFilter(unsigned long time, RGBQUAD* rgb) { } void T2WeatherFilter::RainySkyFilter(unsigned long time, RGBQUAD* rgb) { if (time >= 1800 && time <= 1900) { RGBQUAD c; c.rgbRed = 240; c.rgbGreen = 240; c.rgbBlue = 128; ColorBlend(15, rgb, &c); mLightningTime = rand() % 15000 + 8000; } else if (mLightningTime <= time && time <= (mLightningTime + 200)) { RGBQUAD c; c.rgbRed = 240; c.rgbGreen = 240; c.rgbBlue = 128; ColorBlend(30, rgb, &c); mLightningTime += rand() % 15000 + 8000; } else { int x = ((rgb->rgbRed * 33) + (rgb->rgbGreen * 42) + (rgb->rgbBlue * 25)) / 100; RGBQUAD c; c.rgbRed = (x * 128) / 255; c.rgbGreen = (x * 128) / 255; c.rgbBlue = (x * 140) / 255; ColorFade(time, rgb, &c); } } void T2WeatherFilter::SnowySkyFilter(unsigned long time, RGBQUAD* rgb) { #pragma var_order(g, r, z, b, c) int z = ((rgb->rgbRed * 33) + (rgb->rgbGreen * 42) + (rgb->rgbBlue * 25)) / 100; int r = (((z * 180) / 255) & 255) + 80; int g = (((z * 180) / 255) & 255) + 80; int b = (((z * 180) / 255) & 255) + 80; RGBQUAD c; c.rgbRed = (r <= 255) ? r : 255; c.rgbGreen = (g <= 255) ? g : 255; c.rgbBlue = (b <= 255) ? b : 255; ColorFade(time, rgb, &c); } void T2WeatherFilter::ColorBlend(int factor, RGBQUAD* a, RGBQUAD* b) { a->rgbRed = (a->rgbRed * (100 - factor) + b->rgbRed * factor) / 100; a->rgbGreen = (a->rgbGreen * (100 - factor) + b->rgbGreen * factor) / 100; a->rgbBlue = (a->rgbBlue * (100 - factor) + b->rgbBlue * factor) / 100; } void T2WeatherFilter::Tick() { mTimeSinceLastChange = GetTickCount() - mLastChange; if ((mPhase == 2) && (mCurrentWeather != mNextWeather)) { mPhase = 3; mLastChange = GetTickCount(); } if ((mPhase == 1) && ((mTimeSinceLastChange >= 2500) || (mCurrentWeather == WeatherCode_0))) { mPhase = 2; mLastChange = GetTickCount(); } if ((mPhase == 3) && ((mTimeSinceLastChange >= 2500) || (mCurrentWeather == WeatherCode_0))) { mPhase = 1; mCurrentWeather = mNextWeather; mLastChange = GetTickCount(); } }