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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Azurium

Pages: [1]
1
Window / Re: event.mouseMove.x always 0?
« on: March 28, 2013, 11:15:07 pm »
Thanks! That solved the problem.  But I must say, it's a bit confusing to me the fact that you need separate classes for getting a mouse position.

2
Window / event.mouseMove.x always 0?
« on: March 28, 2013, 10:45:29 pm »
Hello everyone! I'm new to SFML - I tried to learn Allegro in the past but I lost interest. Nowadays my curiosity for programming has been reignited and I've been in search for a graphics library. After going through Allegro, SDL and SFML, I finally settled for SFML ( at least for now).

I once saw an interesting animation effect in Processing. It's called easing. It simulates a decelerated motion towards a point. I tried to adapt this to my first program in SFML, as I'm learning new features.

The problem? event.mouseMove.x always seems to return 0, so the rectangle sticks to the left side of the screen! Also, if that wasn't enough, it seems that event.mouseMove.y returns the y position! (I've added the 'cout' statements to watch the behavior).

Here's the code:

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
 
int main()
{
    float x = 200; float y = 200; float easing = 0.01; float currentPosX = x+10, currentPosY = y+10;
    bool mousePressed = 0; float dx =2 , dy= 2; // variables used for the easing part of the code
    sf::RectangleShape rectangl; rectangl.setSize(sf::Vector2f(100,100)); rectangl.setOutlineColor(sf::Color::Yellow); rectangl.setOutlineThickness(4); rectangl.setPosition(x, y);
    rectangl.setFillColor(sf::Color::Black);
    sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
 
    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            switch(event.type)
            {
                case sf::Event::Closed:
                window.close();
                break;
 
                case sf::Event::MouseButtonPressed:
                mousePressed = 1;
                                currentPosX = event.mouseMove.x; std::cout<<" IN CASE mouseMove.x: "<<event.mouseMove.x<<"\n";
                currentPosY = event.mouseMove.y;
                break;
 
                case sf::Event::KeyPressed:
                if(event.key.code == sf::Keyboard::S)
                {
                    x+=5; y+=5;
                    rectangl.setPosition( x, y );
                } // verifying if setting a new position works
                break;
            }
                }
 
            if(mousePressed || (abs(dx) > 1) || (abs(dy) > 1)) // if the mouse moved, apply the following "easing" animation ( smooth, decelerated movement)
            {
               // if(mousePressed)  // <-- seems like I don't need this condition( I don't have to get the mouse position every loop, so I moved these in the switch block)
            //    {
                 //   currentPosX = event.mouseMove.x;
                 //   currentPosY = event.mouseMove.y;
              //  }
 
                std::cout<<"Clicked at X: "<<currentPosX<<", Clicked at Y: "<<currentPosY<<" \n";
                                std::cout<<"Current X: "<<x<<", Current Y: "<<x<<" \n";
                                system("CLS");
                dx = currentPosX - x;
               
                if ( abs(dx)>1)
                {
                    x += easing*dx;
                }
 
                dy = currentPosY - y;
                if ( abs(dy)>1)
                {
                    y += easing*dy;
                }
 
                /*if((abs(dx)<1)&&(abs(dx)<1))*/
                mousePressed = 0;
            }
            rectangl.setPosition(x, y);
       
        window.draw(rectangl);
 
        window.display();
 
        window.clear();
    }
    return 0;
}



Pages: [1]
anything