Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Porting an openFrameworks graphic example  (Read 3277 times)

0 Members and 1 Guest are viewing this topic.

maidis

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • SFML Geliştirme
Porting an openFrameworks graphic example
« on: December 14, 2010, 12:47:21 am »
Hi,

I'm porting advancedGraphicsExample from openFrameworks [1] to SFML for learning something. I splited example to three parts:

- sfml-lissajous
- sfml-pixel-ocean
- sfml-rgb-circles

But i can't make last one yet. Is there any easy way to link three circles together to rotate without using any gl* stuffs.

sfml-lissajous.cpp
Code: [Select]
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <iostream>

////////////////////////////////////////////////////////////
/// Entry point of application
/// Ported from openFrameworks advancedGraphicsExample
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
    // Create main window
    sf::RenderWindow App(sf::VideoMode(800, 600), "SFML Lissajous");

    // Limit to 60 frames per second
    App.SetFramerateLimit(60);

    // Define and initialize some variables
    float x, y, spin, spinPct, prevMY, prevMX = 0;
    bool bFirstMouseMove = 1;

    // Start 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();

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

            // MouseMove event : adjust spinPct
            if (Event.Type == sf::Event::MouseMoved)
            {
                // Update spinPct by the distance the mouse moved in x and y.
                // We use abs so it always spins in the same direction

                // We use the "bFirstMouseMove" flag so that we calculate only
                // after we have the first prevMY and prevMX stored;
                if (bFirstMouseMove == false)
                    {
                    spinPct += (float)abs(y - prevMY) * 0.03;
                    spinPct += (float)abs(x - prevMX) * 0.03;
                    } else bFirstMouseMove = false;

                // Store the x and y so we can get the prev value
                // next time the mouse is moved
                prevMY = y;
                prevMX = x;
            }
        }

        // Reduce the spinPct by a small amount so that the spinning eventually stops
        spinPct *= 0.99f;

        // Update the spin -which is storing the total rotation- by spinPct
        spin += spinPct;

        // Lets make the curves out of a series of points
        for(int i = 0; i < 800; i++)
        {
            // Lets use the mouse x and y position to affect the x and y paramters of the curve.
            // These values are quite large, so we scale them down by 0.0001
            float xPct = (float)(i * App.GetInput().GetMouseX()) * 0.0001;
            float yPct = (float)(i * App.GetInput().GetMouseY()) * 0.0001;

            // Lets also use the spin to transform the curve over time
            xPct += spin * 0.002;
            yPct += spin * 0.003;

            // Lets feed these two values to sin and cos functions and multiply
            // by how large we want it to be. Because the sin function is producing
            // -1 to 1 results the total width in this case will be double what we specify.
            x =  800.0  * sin(xPct) * 0.5;
            y =  600.0  * cos(yPct) * 0.5;

            // We draw the rects as small 2 pixel by 2 pixel squares and
            // add the position we want them to be osicalting around
            sf::Shape Rect2 = sf::Shape::Rectangle(0, 0, 2, 2, sf::Color::Green);
            Rect2.Move(x + 400, y + 300);
            App.Draw(Rect2);
        }

        // Display current Lissajous curve and then clear screen
        App.Display();
        App.Clear();
    }

    return EXIT_SUCCESS;
}



sfml-pixel-ocean.cpp
Code: [Select]
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <iostream>

////////////////////////////////////////////////////////////
/// Entry point of application
/// Ported from openFrameworks advancedGraphicsExample
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
    // Create main window
    sf::RenderWindow App(sf::VideoMode(800, 600), "SFML Pixel Ocean");

    // Limit to 60 frames per second
    App.SetFramerateLimit(60);

    // Define and initialize some variables
    float counter, k = 0.0;

    // Start 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();

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

        // Add 0.029 to our counter
        counter = counter + 0.029f;

        // Lets make some 8bit looking waves out of rectangles
        for(int i = 0; i < App.GetView().GetSize().x; i += 50)
        {
            // Lets get a unique height for our 'wave' using sine
            float height = sin(counter + k);

            // sin produces -1 to 1 values, lets add 1 to make sure the height is always positive
            height += 1.0;

            // Now it is going from 0 to 2 but we want it to be taller than that.
            // Lets make it go from 0 - 100 by multiplying 50
            height *= 50;

            // Lets draw it!
            sf::Shape Rect3 = sf::Shape::Rectangle(0, 0, 50, -height, sf::Color(0, 90, 170, 128));
            Rect3.Move(i, App.GetView().GetSize().y);
            Rect3.SetBlendMode(sf::Blend::Alpha);
            App.Draw(Rect3);

            // This variable makes sure that each rect has a unique height otherwise
            // they would all be moving up and down at the same time
            k+=0.7;
        }

        // This is doing it again but for a different color
        k = 0;

        for(int i = 0; i < App.GetView().GetSize().x; i+= 50)
        {
            sf::Shape Rect4 = sf::Shape::Rectangle(0, 0, 50, -50 * (sin(1.4 * counter - k) + 1.0), sf::Color(0, 120, 190, 128));
            Rect4.Move(i + 5, App.GetView().GetSize().y);
            Rect4.SetBlendMode(sf::Blend::Alpha);
            App.Draw(Rect4);
            k += 0.4;
        }

        // Display waves and then clear screen
        App.Display();
        App.Clear();
    }

    return EXIT_SUCCESS;
}



Screenshots from original example and mines [2, 3].

[1] https://github.com/openframeworks/openFrameworks/blob/master/apps/examples/advancedGraphicsExample/src/testApp.cpp
[2] http://imgur.com/TQhat
[3] http://imgur.com/w8NdD

 

anything