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

Author Topic: Beginners Quest  (Read 3783 times)

0 Members and 1 Guest are viewing this topic.

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Beginners Quest
« on: September 09, 2008, 01:24:33 pm »
well i want to draw a shape, and when a certain key is pressed i wanna rotate it 90°

how would this look like ?

do not laugh bout my tries ^^ :

Code: [Select]
while (App.IsOpened())
    {
        // Process events


/*
        // Draw predefined shapes
        App.Draw(Shape::Line(10, 10, 710, 100, 15, Color::Red));
        App.Draw(Shape::Circle(200, 200, 100, sf::Color::Yellow, 10, sf::Color::Blue));
        App.Draw(Shape::Rectangle(350, 200, 600, 350, sf::Color::Green));
*/
        // Build a custom convex shape
        Shape Polygon;
        Polygon.AddPoint(0, -50,  sf::Color(255, 0, 0),     sf::Color(0, 128, 128));
        Polygon.AddPoint(50, 0,   sf::Color(255, 85, 85),   sf::Color(0, 128, 128));
        Polygon.AddPoint(50, 50,  sf::Color(255, 170, 170), sf::Color(0, 128, 128));
        Polygon.AddPoint(0, 100,  sf::Color(255, 255, 255), sf::Color(0, 128, 128));
        Polygon.AddPoint(-50, 50, sf::Color(255, 170, 170), sf::Color(0, 128, 128));
        Polygon.AddPoint(-50, 0,  sf::Color(255, 85, 85),   sf::Color(0, 128, 128));

        // Define an outline width
        Polygon.SetOutlineWidth(10);

        // Disable filling and enable the outline
        Polygon.EnableFill(true);
        Polygon.EnableOutline(true);

        // We can still use the functions common to all SFML drawable objects
        Polygon.SetColor(Color(255, 255, 255, 255));
        Polygon.Move(300, 300);
        Polygon.Scale(3, 2);
       // Polygon.Rotate(90);

        // Draw it
        App.Draw(Polygon);

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

         Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == Event.Closed)
                App.Close();
            if ((Event.Type == Event.KeyPressed) && (Event.Key.Code == Key::Escape))
            {
                App.SetActive();
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                Polygon.Rotate(90);
                App.Draw(Polygon);
                App.Display();
            }

        }
    }

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Beginners Quest
« Reply #1 on: September 09, 2008, 01:37:46 pm »
There are two problems :
- You're doing your initializations in the game loop instead of just once at program startup
- You do useless stuff when reacting to a keypress event; you just have to rotate your shape

Code: [Select]
// Build a custom convex shape
Shape Polygon;
Polygon.AddPoint(0, -50,  sf::Color(255, 0, 0),     sf::Color(0, 128, 128));
Polygon.AddPoint(50, 0,   sf::Color(255, 85, 85),   sf::Color(0, 128, 128));
Polygon.AddPoint(50, 50,  sf::Color(255, 170, 170), sf::Color(0, 128, 128));
Polygon.AddPoint(0, 100,  sf::Color(255, 255, 255), sf::Color(0, 128, 128));
Polygon.AddPoint(-50, 50, sf::Color(255, 170, 170), sf::Color(0, 128, 128));
Polygon.AddPoint(-50, 0,  sf::Color(255, 85, 85),   sf::Color(0, 128, 128));

// Define an outline width
Polygon.SetOutlineWidth(10);

// Disable filling and enable the outline
Polygon.EnableFill(true);
Polygon.EnableOutline(true);

// We can still use the functions common to all SFML drawable objects
Polygon.SetColor(Color(255, 255, 255, 255));
Polygon.Move(300, 300);
Polygon.Scale(3, 2);

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

        if ((Event.Type == Event.KeyPressed) && (Event.Key.Code == Key::Escape))
            Polygon.Rotate(90);
    }

    // Draw it
    App.Draw(Polygon);

    // Finally, display the rendered frame on screen
    App.Display();
}
Laurent Gomila - SFML developer

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Beginners Quest
« Reply #2 on: September 09, 2008, 02:40:03 pm »
what i dont understand yet, is why the polygon is drawn...

while (App.IsOpened())
{

    Event Event;
    while (App.GetEvent(Event))
    {

    }
    App.Draw(Polygon);
    App.Display();
}

i enter first while, thats ok as long as my program is opened - check for events .. then react on events...

is there some time when no events come in? otherwise the 2nd while loop could never be left, or is there any logical mistake in my head?

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Beginners Quest
« Reply #3 on: September 09, 2008, 02:49:39 pm »
and another problem: now i want the polygon to move when the mouse is moved is , what is wrong with this:

        if (Event.Type == Event.MouseMoved)
        {
            Polygon.Move( Event.MouseMove.X,Event.MouseMove.Y);
        }

zarka

  • Jr. Member
  • **
  • Posts: 81
    • View Profile
Beginners Quest
« Reply #4 on: September 09, 2008, 04:51:10 pm »
Quote from: "ravenheart"

is there some time when no events come in? otherwise the 2nd while loop could never be left, or is there any logical mistake in my head?


Code: [Select]

    Event Event;
    while (App.GetEvent(Event))
    {

    }
    App.Draw(Polygon);
    App.Display();


if there are no events (or all events that occurred this frame have been processed) App.GetEvent(Event) will return false meaning the loop will exit. which will execute your draw calls.
Quote from: "ravenheart"

and another problem: now i want the polygon to move when the mouse is moved is , what is wrong with this:

if (Event.Type == Event.MouseMoved)
{
Polygon.Move( Event.MouseMove.X,Event.MouseMove.Y);
}


Polygon.Move uses relative positions while Event.MouseMove.X is an absolute position try using Polygon.SetPosition insteed (it works with absolute positions)[/quote]
//Zzzarka

 

anything