Filter class by Nothingness0x.
The purpose of the Filter class is to apply filters to your windows. Handy during a transition to decrease brightness slowly or quickly until complete darkness, adn then re put it to normal, or to simulate the nightfall for example.
Notice that I'm using during this test a completly black image of the size of my window (800×600). Using an image with another color to produce another kind of effect is obviously expectable. (For example create a flash effet with a white filter).
I apologize for the comment in a bad english as I'm not a native english speaker, I hope you will have no problem to understand. Here's the code :
#ifndef FILTER_H_INCLUDED #define FILTER_H_INCLUDED class Filter{ private : sf::Sprite m_filter; sf::Clock m_clockFilter; float m_filterDelay; //The delay bewteen two alpha removing float m_alphaChanger; //Number of alpha decrease or increase at each frame (1 : Slow, 2.5 : Medium, 5 : Fast) float m_alphaColor; //Current Alpha color of the filter bool m_filterIn; //To know if we have to do a fade out or a fade in bool m_runFade; //Activate the fade public : Filter(); Filter::Filter(sf::Image &imageFilter,float newAlphaChanger); //newAlphaChanger is the speed of the filter application ~Filter(); void runFilter(); //Do a fade out or a fade in using the filter sf::Sprite getFilter()const; //Display the filter float getAlphaColor() const; //Return the current alpha color of the filter float getAlphaChanger() const; //Return the time between two alpha changing void setFilterDelay(float newDelay); void setFilter(sf::Image &newFilter); //Change the filter void setAlphaColor(float newAlphaColor); //Change manually alpha color if you want a constent filter void setAlphaChanger(float newAlphaChanger); //To change the rapidity of the fade in or the fade out void changeFade(bool fade); //Change a fade in to a fade out or a fade out to a fade in }; #endif // FILTER_H_INCLUDED
#include <SFML/System.hpp> #include <SFML/Window.hpp> #include <SFML/Graphics.hpp> #include"filter.h" Filter::Filter() { } Filter::Filter(sf::Image &imageFilter,float newAlphaChanger) { m_filter.SetImage(imageFilter); m_filterDelay=0.01; m_alphaChanger=newAlphaChanger; m_runFade=false; m_alphaColor=0; m_filter.SetColor(sf::Color(255,255,255,m_alphaColor)); } Filter::~Filter() { } void Filter::runFilter() { if((m_filterIn) && (m_runFade)) { if((m_alphaColor<=255) && (m_clockFilter.GetElapsedTime()>m_filterDelay)) { m_filter.SetColor(sf::Color(255,255,255,m_alphaColor+=m_alphaChanger)); m_clockFilter.Reset(); } if(m_alphaColor>255) { m_filter.SetColor(sf::Color(255,255,255,m_alphaColor=255)); m_runFade=false; } } else if((!m_filterIn) && (m_runFade)) { if((m_alphaColor>=0) && (m_clockFilter.GetElapsedTime()>m_filterDelay)) { m_filter.SetColor(sf::Color(255,255,255,m_alphaColor-=m_alphaChanger)); m_clockFilter.Reset(); } if(m_alphaColor<0) { m_filter.SetColor(sf::Color(255,255,255,m_alphaColor=0)); m_runFade=false; } } } sf::Sprite Filter::getFilter()const { return m_filter; } float Filter::getAlphaColor() const { return m_alphaColor; } float Filter::getAlphaChanger() const { return m_alphaChanger; } void Filter::setFilterDelay(float newDelay) { m_filterDelay=newDelay; } void Filter::setFilter(sf::Image &newFilter) { m_filter.SetImage(newFilter); } void Filter::setAlphaColor(float newAlphaColor) { m_filter.SetColor(sf::Color(255,255,255,newAlphaColor)); } void Filter::setAlphaChanger(float newAlphaChanger) { m_alphaChanger=newAlphaChanger; } void Filter::changeFade(bool fade) { m_runFade=true; if(fade) { m_alphaColor=0; m_filter.SetColor(sf::Color(255,255,255,m_alphaColor)); m_filterIn=true; } else { m_alphaColor=255; m_filter.SetColor(sf::Color(255,255,255,m_alphaColor)); m_filterIn=false; } }
#include <SFML/System.hpp> #include <SFML/Window.hpp> #include <SFML/Graphics.hpp> #include "filter.h" int main() { sf::RenderWindow App(sf::VideoMode(800 , 600, 32), "Filter"); sf::Image imageFilter; if (!imageFilter.LoadFromFile("images/filter/blackfilter.png")) { // Error } Filter gameFilter(imageFilter,1); while(App.IsOpened()) { sf::Event Event; while (App.GetEvent(Event)) { if (Event.Type == sf::Event::Closed) App.Close(); if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::A)) gameFilter.changeFade(true); if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Z)) gameFilter.changeFade(false); } App.Clear(sf::Color::Red); gameFilter.runFilter(); App.Draw(gameFilter.getFilter()); App.Display(); } return 0; }
I voluntarily paint the windows background with entire red color to observe the filter application by pressing A to put it and 2 to remove it. There's of course a lot of another utilisations possible instead of using events, but it was to make a quick demonstration of the utility of the class.
Just remind I loaded a black image of the size of my window (800×600) done easily on paint software. I don't provide the used image here because it's easy for you to get the same.