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

Author Topic: Another annoying beginner´s question  (Read 3847 times)

0 Members and 1 Guest are viewing this topic.

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Another annoying beginner´s question
« on: September 12, 2008, 09:39:12 pm »
hi i decided to program a little pong clone,

now i want the ball to move, this works:

Code: [Select]
Ball.Move(50 * App.GetFrameTime(), 50 *App.GetFrameTime());

(it doesnt matter in what direction the ball moves yet , just an example)

the problem is that the ball flickers, it doesnt look good
what can i do to stop that
is doublebuffering a solution and if how does it work?

thank u guys, I LOVE SFML

marox

  • Full Member
  • ***
  • Posts: 178
    • View Profile
Another annoying beginner´s question
« Reply #1 on: September 12, 2008, 09:49:29 pm »
how do you limit your framelimit?
mon tout nouveau siteweb  : Creations

ravenheart

  • Full Member
  • ***
  • Posts: 148
    • View Profile
Another annoying beginner´s question
« Reply #2 on: September 12, 2008, 10:43:47 pm »
im a total beginner to make it easier for you, here my code:

Code: [Select]

#include <SFML/Graphics.hpp>
#include <iostream>

using namespace sf;

////////////////////////////////////////////////////////////
/// Entry point of application
////////////////////////////////////////////////////////////

int main()
{

// Create main window
    RenderWindow App;
    App.Create(sf::VideoMode(800, 600, 32), "SFML Window", sf::Style::Fullscreen);
// Cursor is not being shown
    ShowCursor(0);
// Build a custom convex shape
    Shape Polygon;
    {
     Polygon.AddPoint(0, 0,  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, 50,  sf::Color(255, 170, 170), sf::Color(0, 128, 128));

     Polygon.SetOutlineWidth(0);

     Polygon.EnableFill(true);
     Polygon.EnableOutline(true);

     Polygon.SetColor(Color(255, 255, 255, 255));
     Polygon.Scale(3, 1);
     Polygon.SetPosition(400,550);
    }

    Shape Ball;
    {
     Ball.AddPoint(0,0, sf::Color(255,255,255),sf::Color(255,255,255));
     Ball.AddPoint(10,0,  sf::Color(255,255,255),sf::Color(255,255,255));
     Ball.AddPoint(10,10, sf::Color(255,255,255),sf::Color(255,255,255));
     Ball.AddPoint(0,10,  sf::Color(255,255,255),sf::Color(255,255,255));
    }

    int movex = 50;
    int movey = 50;
    Vector2<float> ballvec;
    Vector2<float> shapevec;
    Clock Spieluhr;
    Clock Uhr;

    String Text;
    Text.SetFont(sf::Font::GetDefaultFont());
    Text.SetSize(20);



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

        // keybord-events
        if ((Event.Type == Event.KeyPressed) && (Event.Key.Code == Key::Escape))
        {
            App.Close();
        }
        if ((Event.Type == Event.KeyPressed) && (Event.Key.Code == Key::Num1))
        {
            Polygon.Rotate(15);
        }

        // mouse-events
        if (Event.Type == Event.MouseMoved)
        {
                Polygon.SetPosition(Event.MouseMove.X,550);
        }

    }

    // Manage Ball
    ballvec = Ball.GetPosition();
    shapevec = Polygon.GetPosition();
    if((ballvec.y + movey *App.GetFrameTime() >= 540) && (ballvec.x >= shapevec.x) && (ballvec.x <= shapevec.x +150) )
    {
        movey = -movey;
    }
    if(ballvec.y +movey *App.GetFrameTime() <= 0)
    {
        movey = -movey;
    }
    if(ballvec.x + movex *App.GetFrameTime() >= 790)
    {
        movex = -movex;
    }
    if(ballvec.x + movex *App.GetFrameTime() <= 0)
    {
        movex = -movex;
    }
    if(Uhr.GetElapsedTime() > 5.0)
    {
        if (movex > 0)
        {
            movex = movex + 50;
        }
        if (movex < 0)
        {
            movex = movex - 50;
        }
        if (movey > 0)
        {
            movey = movey + 50;
        }
        if (movey < 0)
        {
            movey = movey - 50;
        }
        Uhr.Reset();
    }
    Text.SetText("hallo");
    Ball.Move(movex * App.GetFrameTime(), movey * App.GetFrameTime());

    // Draw it
    App.Draw(Text);
    App.Draw(Ball);
    App.Draw(Polygon);

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

    return EXIT_SUCCESS;
}


its not perfect yet but i think its playable now;

can anyone tell me how to convert a float into String?

marox

  • Full Member
  • ***
  • Posts: 178
    • View Profile
Another annoying beginner´s question
« Reply #3 on: September 13, 2008, 12:02:06 am »
to convert float to string, use this:

Code: [Select]
#include <sstream>
template< T > string to_string(T value){
ostream oss;
oss << value;
return oss.str();
}

PS: it worls also with int, long, double ...
mon tout nouveau siteweb  : Creations

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Another annoying beginner´s question
« Reply #4 on: September 13, 2008, 08:20:40 am »
You should try App.SetVerticalSync(true);.