File: PostFX.cpp

package info (click to toggle)
libsfml 1.6%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 12,756 kB
  • ctags: 38,139
  • sloc: cpp: 27,830; ansic: 3,963; makefile: 483
file content (184 lines) | stat: -rw-r--r-- 6,683 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <map>

void DisplayError();


////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
    // Check that the system can use post effects
    if (sf::PostFX::CanUsePostFX() == false)
    {
        DisplayError();
        return EXIT_SUCCESS;
    }

    // Create the main window
    sf::RenderWindow App(sf::VideoMode(800, 600), "SFML PostFX");

    // Load a cute background image to display :)
    sf::Image BackgroundImage;
    if (!BackgroundImage.LoadFromFile("datas/post-fx/background.jpg"))
        return EXIT_FAILURE;
    sf::Sprite Background(BackgroundImage);

    // Load the text font
    sf::Font Cheeseburger;
    if (!Cheeseburger.LoadFromFile("datas/post-fx/cheeseburger.ttf"))
        return EXIT_FAILURE;

    // Load the image needed for the wave effect
    sf::Image WaveImage;
    if (!WaveImage.LoadFromFile("datas/post-fx/wave.jpg"))
        return EXIT_FAILURE;

    // Load all effects
    std::map<std::string, sf::PostFX> Effects;
    if (!Effects["nothing"].LoadFromFile("datas/post-fx/nothing.sfx"))   return EXIT_FAILURE;
    if (!Effects["blur"].LoadFromFile("datas/post-fx/blur.sfx"))         return EXIT_FAILURE;
    if (!Effects["colorize"].LoadFromFile("datas/post-fx/colorize.sfx")) return EXIT_FAILURE;
    if (!Effects["fisheye"].LoadFromFile("datas/post-fx/fisheye.sfx"))   return EXIT_FAILURE;
    if (!Effects["wave"].LoadFromFile("datas/post-fx/wave.sfx"))         return EXIT_FAILURE;
    std::map<std::string, sf::PostFX>::iterator CurrentEffect = Effects.find("nothing");

    // Do specific initializations
    Effects["nothing"].SetTexture("framebuffer", NULL);
    Effects["blur"].SetTexture("framebuffer", NULL);
    Effects["blur"].SetParameter("offset", 0.f);
    Effects["colorize"].SetTexture("framebuffer", NULL);
    Effects["colorize"].SetParameter("color", 1.f, 1.f, 1.f);
    Effects["fisheye"].SetTexture("framebuffer", NULL);
    Effects["wave"].SetTexture("framebuffer", NULL);
    Effects["wave"].SetTexture("wave", &WaveImage);

    // Define a string for displaying current effect description
    sf::String CurFXStr;
    CurFXStr.SetText("Current effect is \"" + CurrentEffect->first + "\"");
    CurFXStr.SetFont(Cheeseburger);
    CurFXStr.SetPosition(20.f, 0.f);

    // Define a string for displaying help
    sf::String InfoStr;
    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");
    InfoStr.SetFont(Cheeseburger);
    InfoStr.SetPosition(20.f, 460.f);
    InfoStr.SetColor(sf::Color(200, 100, 150));

    // Start the game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();

            if (Event.Type == sf::Event::KeyPressed)
            {
                // Escape key : exit
                if (Event.Key.Code == sf::Key::Escape)
                    App.Close();

                // Add key : next effect
                if (Event.Key.Code == sf::Key::Add)
                {
                    CurrentEffect++;
                    if (CurrentEffect == Effects.end())
                        CurrentEffect = Effects.begin();
                    CurFXStr.SetText("Current effect is \"" + CurrentEffect->first + "\"");
                }

                // Subtract key : previous effect
                if (Event.Key.Code == sf::Key::Subtract)
                {
                    if (CurrentEffect == Effects.begin())
                        CurrentEffect = Effects.end();
                    CurrentEffect--;
                    CurFXStr.SetText("Current effect is \"" + CurrentEffect->first + "\"");
                }
            }
        }

        // Get the mouse position in the range [0, 1]
        float X = App.GetInput().GetMouseX() / static_cast<float>(App.GetWidth());
        float Y = App.GetInput().GetMouseY() / static_cast<float>(App.GetHeight());

        // Update the current effect
        if      (CurrentEffect->first == "blur")     CurrentEffect->second.SetParameter("offset", X * Y * 0.1f);
        else if (CurrentEffect->first == "colorize") CurrentEffect->second.SetParameter("color", 0.3f, X, Y);
        else if (CurrentEffect->first == "fisheye")  CurrentEffect->second.SetParameter("mouse", X, 1.f - Y);
        else if (CurrentEffect->first == "wave")     CurrentEffect->second.SetParameter("offset", X, Y);

        // Clear the window
        App.Clear();

        // Draw background and apply the post-fx
        App.Draw(Background);
        App.Draw(CurrentEffect->second);

        // Draw interface strings
        App.Draw(CurFXStr);
        App.Draw(InfoStr);

        // Finally, display the rendered frame on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}


////////////////////////////////////////////////////////////
/// Fonction called when the post-effects are not supported ;
/// Display an error message and wait until the user exits
///
////////////////////////////////////////////////////////////
void DisplayError()
{
    // Create the main window
    sf::RenderWindow App(sf::VideoMode(800, 600), "SFML PostFX");

    // Define a string for displaying the error message
    sf::String ErrorStr("Sorry, your system doesn't support post-effects");
    ErrorStr.SetPosition(100.f, 250.f);
    ErrorStr.SetColor(sf::Color(200, 100, 150));

    // Start the game loop
    bool Running = true;
    while (Running)
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                Running = false;

            // Escape key : exit
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                Running = false;
        }

        // Clear the window
        App.Clear();

        // Draw the error message
        App.Draw(ErrorStr);

        // Finally, display the rendered frame on screen
        App.Display();
    }
}