Back to index

libsfml  1.6+dfsg2
PostFX.cpp
Go to the documentation of this file.
00001 
00003 // Headers
00005 #include <SFML/Graphics.hpp>
00006 #include <map>
00007 
00008 void DisplayError();
00009 
00010 
00017 int main()
00018 {
00019     // Check that the system can use post effects
00020     if (sf::PostFX::CanUsePostFX() == false)
00021     {
00022         DisplayError();
00023         return EXIT_SUCCESS;
00024     }
00025 
00026     // Create the main window
00027     sf::RenderWindow App(sf::VideoMode(800, 600), "SFML PostFX");
00028 
00029     // Load a cute background image to display :)
00030     sf::Image BackgroundImage;
00031     if (!BackgroundImage.LoadFromFile("datas/post-fx/background.jpg"))
00032         return EXIT_FAILURE;
00033     sf::Sprite Background(BackgroundImage);
00034 
00035     // Load the text font
00036     sf::Font Cheeseburger;
00037     if (!Cheeseburger.LoadFromFile("datas/post-fx/cheeseburger.ttf"))
00038         return EXIT_FAILURE;
00039 
00040     // Load the image needed for the wave effect
00041     sf::Image WaveImage;
00042     if (!WaveImage.LoadFromFile("datas/post-fx/wave.jpg"))
00043         return EXIT_FAILURE;
00044 
00045     // Load all effects
00046     std::map<std::string, sf::PostFX> Effects;
00047     if (!Effects["nothing"].LoadFromFile("datas/post-fx/nothing.sfx"))   return EXIT_FAILURE;
00048     if (!Effects["blur"].LoadFromFile("datas/post-fx/blur.sfx"))         return EXIT_FAILURE;
00049     if (!Effects["colorize"].LoadFromFile("datas/post-fx/colorize.sfx")) return EXIT_FAILURE;
00050     if (!Effects["fisheye"].LoadFromFile("datas/post-fx/fisheye.sfx"))   return EXIT_FAILURE;
00051     if (!Effects["wave"].LoadFromFile("datas/post-fx/wave.sfx"))         return EXIT_FAILURE;
00052     std::map<std::string, sf::PostFX>::iterator CurrentEffect = Effects.find("nothing");
00053 
00054     // Do specific initializations
00055     Effects["nothing"].SetTexture("framebuffer", NULL);
00056     Effects["blur"].SetTexture("framebuffer", NULL);
00057     Effects["blur"].SetParameter("offset", 0.f);
00058     Effects["colorize"].SetTexture("framebuffer", NULL);
00059     Effects["colorize"].SetParameter("color", 1.f, 1.f, 1.f);
00060     Effects["fisheye"].SetTexture("framebuffer", NULL);
00061     Effects["wave"].SetTexture("framebuffer", NULL);
00062     Effects["wave"].SetTexture("wave", &WaveImage);
00063 
00064     // Define a string for displaying current effect description
00065     sf::String CurFXStr;
00066     CurFXStr.SetText("Current effect is \"" + CurrentEffect->first + "\"");
00067     CurFXStr.SetFont(Cheeseburger);
00068     CurFXStr.SetPosition(20.f, 0.f);
00069 
00070     // Define a string for displaying help
00071     sf::String InfoStr;
00072     InfoStr.SetText("Move your mouse to change the effect parameters\nPress numpad + and - to change effect\nWarning : some effects may not work\ndepending on your graphics card");
00073     InfoStr.SetFont(Cheeseburger);
00074     InfoStr.SetPosition(20.f, 460.f);
00075     InfoStr.SetColor(sf::Color(200, 100, 150));
00076 
00077     // Start the game loop
00078     while (App.IsOpened())
00079     {
00080         // Process events
00081         sf::Event Event;
00082         while (App.GetEvent(Event))
00083         {
00084             // Close window : exit
00085             if (Event.Type == sf::Event::Closed)
00086                 App.Close();
00087 
00088             if (Event.Type == sf::Event::KeyPressed)
00089             {
00090                 // Escape key : exit
00091                 if (Event.Key.Code == sf::Key::Escape)
00092                     App.Close();
00093 
00094                 // Add key : next effect
00095                 if (Event.Key.Code == sf::Key::Add)
00096                 {
00097                     CurrentEffect++;
00098                     if (CurrentEffect == Effects.end())
00099                         CurrentEffect = Effects.begin();
00100                     CurFXStr.SetText("Current effect is \"" + CurrentEffect->first + "\"");
00101                 }
00102 
00103                 // Subtract key : previous effect
00104                 if (Event.Key.Code == sf::Key::Subtract)
00105                 {
00106                     if (CurrentEffect == Effects.begin())
00107                         CurrentEffect = Effects.end();
00108                     CurrentEffect--;
00109                     CurFXStr.SetText("Current effect is \"" + CurrentEffect->first + "\"");
00110                 }
00111             }
00112         }
00113 
00114         // Get the mouse position in the range [0, 1]
00115         float X = App.GetInput().GetMouseX() / static_cast<float>(App.GetWidth());
00116         float Y = App.GetInput().GetMouseY() / static_cast<float>(App.GetHeight());
00117 
00118         // Update the current effect
00119         if      (CurrentEffect->first == "blur")     CurrentEffect->second.SetParameter("offset", X * Y * 0.1f);
00120         else if (CurrentEffect->first == "colorize") CurrentEffect->second.SetParameter("color", 0.3f, X, Y);
00121         else if (CurrentEffect->first == "fisheye")  CurrentEffect->second.SetParameter("mouse", X, 1.f - Y);
00122         else if (CurrentEffect->first == "wave")     CurrentEffect->second.SetParameter("offset", X, Y);
00123 
00124         // Clear the window
00125         App.Clear();
00126 
00127         // Draw background and apply the post-fx
00128         App.Draw(Background);
00129         App.Draw(CurrentEffect->second);
00130 
00131         // Draw interface strings
00132         App.Draw(CurFXStr);
00133         App.Draw(InfoStr);
00134 
00135         // Finally, display the rendered frame on screen
00136         App.Display();
00137     }
00138 
00139     return EXIT_SUCCESS;
00140 }
00141 
00142 
00148 void DisplayError()
00149 {
00150     // Create the main window
00151     sf::RenderWindow App(sf::VideoMode(800, 600), "SFML PostFX");
00152 
00153     // Define a string for displaying the error message
00154     sf::String ErrorStr("Sorry, your system doesn't support post-effects");
00155     ErrorStr.SetPosition(100.f, 250.f);
00156     ErrorStr.SetColor(sf::Color(200, 100, 150));
00157 
00158     // Start the game loop
00159     bool Running = true;
00160     while (Running)
00161     {
00162         // Process events
00163         sf::Event Event;
00164         while (App.GetEvent(Event))
00165         {
00166             // Close window : exit
00167             if (Event.Type == sf::Event::Closed)
00168                 Running = false;
00169 
00170             // Escape key : exit
00171             if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
00172                 Running = false;
00173         }
00174 
00175         // Clear the window
00176         App.Clear();
00177 
00178         // Draw the error message
00179         App.Draw(ErrorStr);
00180 
00181         // Finally, display the rendered frame on screen
00182         App.Display();
00183     }
00184 }