Simple SFML problem here

Hi all,
I know that this problem is a bit simple but I still havn't fixed it. Now what I'm trying to do is to draw a circle not by hard-coding but based on mouse coordinates using the Circle(); function.

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
//aim:
//----------------------header files---------------------------------------------
#include <iostream>
#include <cstdlib>
#include <SFML/Graphics.hpp>
//----------------------namespaces-----------------------------------------------
using namespace std;
//------------globals/functions'n'classes-prototypes-----------------------------


//-------------------------code--------------------------------------------------

int main()
{
    sf::RenderWindow winMainWindow;
    sf::Shape mwCircle1;

    winMainWindow.Create(sf::VideoMode(800,600,32),"Prog 5 - Main Window");
    while(winMainWindow.IsOpened())
    {
        static sf::Event eventMainWindow;
        static const sf::Input& inputMainWindow=winMainWindow.GetInput();
        static bool bIsDrawing=false;
        static int circle1X, circle1Y, circle1Radius, circle1Temp;

        while(winMainWindow.GetEvent(eventMainWindow))
        {
            if(eventMainWindow.Type==sf::Event::Closed)
            {
                winMainWindow.Close();
            }
            if(eventMainWindow.Type==sf::Event::MouseButtonPressed &&
               eventMainWindow.MouseButton.Button==sf::Mouse::Left && !bIsDrawing)
            {
                bIsDrawing=true;
                circle1X=eventMainWindow.MouseButton.X;
                circle1Y=eventMainWindow.MouseButton.Y;
                circle1Temp=circle1X;
                circle1Radius=0;
            }
            if(bIsDrawing)
            {
                circle1X=inputMainWindow.GetMouseX();
                circle1Radius=(circle1X-circle1Temp);
                mwCircle1.Circle(circle1X,circle1Y,circle1Radius,sf::Color(255,0,255));
                winMainWindow.Clear();
                winMainWindow.Draw(mwCircle1);
            }
            if(eventMainWindow.Type==sf::Event::MouseButtonReleased &&
               eventMainWindow.MouseButton.Button==sf::Mouse::Left)
            {
                bIsDrawing=false;
            }
        }

        mwCircle1.Circle(100,100,100,sf::Color(255,0,255));  //this is just to test the function but it still doesn't work?!?
        winMainWindow.Draw(mwCircle1);

        winMainWindow.Display();
    }

    return 0;
}


Aceix.
Last edited on
The thing is everyrhing compiles fine and no runtime errors, but the circle does not show or draw in the window?

Please help,
Aceix.
sf::Shape::Circle is a static function that returns a sf::Shape.

Try something like the following (I don't have 1.6 installed, so you may need to make some adjustments -- you should upgrade to 2.0)

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
///aim:
//----------------------header files---------------------------------------------
#include <iostream>
#include <cstdlib>
#include <SFML/Graphics.hpp>
//----------------------namespaces-----------------------------------------------
using namespace std;
//------------globals/functions'n'classes-prototypes-----------------------------


//-------------------------code--------------------------------------------------

int main()
{
    sf::RenderWindow winMainWindow;
    winMainWindow.Create(sf::VideoMode(800,600,32),"Prog 5 - Main Window");

    while(winMainWindow.IsOpened())
    {
        // no reason for these to be static
        sf::Event eventMainWindow;
        bool bIsDrawing=false;
        int circle1X, circle1Y, circle1Radius;

        while(winMainWindow.GetEvent(eventMainWindow))
        {
            switch( eventMainWindow.Type )
            {
            case sf::Event::Closed:
                winMainWindow.Close();
                break;

            case sf::Event::MouseButtonPressed:
                if ( eventMainWindow.MouseButton.Button == sf::Mouse::Left && !bIsDrawing )
                {
                    bIsDrawing = true ;
                    circle1X = eventMainWindow.MouseButton.X ;
                    circle1Y = eventMainWindow.MouseButton.Y ;
                    circle1Radius = 0 ;
                }
                break ;

            case sf::Event::MouseButtonReleased:
                if ( eventMainWindow.MouseButton.Button == sf::Mouse::Left )
                    bIsDrawing = false ;
                break ;

            case sf::Event::MouseMoved:
                if ( bIsDrawing )
                {
                    int mouseX = eventMainWindow.MouseMove.X ;
                    int mouseY = eventMainWindow.MouseMove.Y ;
                    circle1Radius = sqrt((circle1X-mouseX)*(circle1X-mouseX)+
                                         (circle1Y-mouseY)*(circle1Y-mouseY)) ;
                }
                break ;

            }
        }

        winMainWindow.Clear() ;        
        winMainWindow.Draw(sf::Shape::Circle(circle1X, circle1Y, 
            circle1Radius, sf::Color(255,0,255))) ;
        winMainWindow.Display();
    }

    return 0;
}
Last edited on
Thanks,
Working on it.

Aceix.
Topic archived. No new replies allowed.