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

Author Topic: Pong game doesn't work when i SetFramerateLimit  (Read 2923 times)

0 Members and 1 Guest are viewing this topic.

Hair_FTW

  • Newbie
  • *
  • Posts: 6
    • View Profile
Pong game doesn't work when i SetFramerateLimit
« on: September 16, 2011, 11:06:28 pm »
So i made a Pong game and it works fine (i know that the collision is really bad programmed and that it is bad programmed in general, but it works).

I use for movement sprite.Move(x, y*App.GetFrameTime()); and it works. I make the game "cout" the frame rate every loop and it runs at 300 at my computer.

The problem is that when i try to cap the framerate (i think it's the right term) using SetFrameRate the collision system doesn't work anymore. It works until the 100 fps but below that it desn't and i don't know why.

Here is the code:
Code: [Select]
///Headers///
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <string>
#include <sstream>

///Namespaces///
using namespace std;
using namespace sf;

int main ()
{
///Variables///
float framerate;
float balltime;
int score1 = 0;
int score2 = 0;
bool isballonscreen = false;
bool player1up, player1down, player2up, player2down;
float speed;
float moveballx, movebally;
int random;
stringstream score;
Clock cballtime;

///Window and Input///
RenderWindow Janela (VideoMode(800,600,32), "Pong!");
const sf::Input& Input = Janela.GetInput();
Janela.SetFramerateLimit(0);

///Shapes///
Shape Line = Shape::Line(400, 0, 400, 600, 3, Color::White);
Shape Player1 = Shape::Rectangle(20, 265, 40, 335, Color::White);
Shape Player2 = Shape::Rectangle(760, 265, 780, 335, Color::White);
Shape Ball = Shape::Circle(400, 300, 10, Color::White);

///SoundBuffers///
SoundBuffer Blop, Ping, Yeah;

if(!Blop.LoadFromFile("Blop.wav"))
{
   return EXIT_FAILURE;
}
if(!Ping.LoadFromFile("Ping.wav"))
{
    return EXIT_FAILURE;
}
if(!Yeah.LoadFromFile("Yeah.wav"))
{
    return EXIT_FAILURE;
}

///Sounds///
Sound blop, ping, yeah;
blop.SetBuffer(Blop);
ping.SetBuffer(Ping);
yeah.SetBuffer(Yeah);

///Strings///
String PressReturn ("            PONG!\nPress Return to Start", Font::GetDefaultFont(), 50);
PressReturn.Move (160.f, 130.f);

String Keys ("Player 1:                   Player 2:\n\nUp                             W\nDown                         S", Font::GetDefaultFont(), 35);
Keys.Move (160.f, 300.f);

String player1score ("0", Font::GetDefaultFont(), 45);
String player2score ("0", Font::GetDefaultFont(), 45);
player1score.Move (250.f, 30.f);
player2score.Move (550.f, 30.f);

///Start Loop///
while(1)
{
        Janela.Clear();
        Janela.Draw(Keys);
        Janela.Draw(PressReturn);
        Janela.Display();

        Event start;
        Janela.GetEvent(start);

        if(start.Type == Event::KeyPressed && start.Key.Code == Key::Return)
        break;

        if(start.Type == Event::Closed)
        {
            Janela.Close();
            return EXIT_SUCCESS;
        }

        if(start.Type == Event::KeyPressed && start.Key.Code == Key::Escape)
        {
            Janela.Close();
            return EXIT_SUCCESS;
        }
}

///Main Loop///
    while(Janela.IsOpened())
    {
        framerate = 1.f / Janela.GetFrameTime();
        balltime = cballtime.GetElapsedTime();

        ///Puts ball on Screen///
            while(isballonscreen == false)
            {
                cballtime.Reset();
                speed = 300.f;
                moveballx = 0.f;
                movebally = 0.f;
                Ball.SetPosition(0.f,0.f);

                random = Randomizer::Random(1, 4);

                        switch(random)
                        {
                            case 1:
                            moveballx = speed;
                            movebally = speed;
                            isballonscreen = true;
                            break;

                            case 2:
                            moveballx = speed;
                            movebally = -speed;
                            isballonscreen = true;
                            break;

                            case 3:
                            moveballx = -speed;
                            movebally = speed;
                            isballonscreen = true;
                            break;

                            case 4:
                            moveballx = -speed;
                            movebally = -speed;
                            isballonscreen = true;
                            break;
                        }

                }

        ///Speeds up Ball///
        if(balltime > 3)
        speed += 0.03f;

        ///Events///
        Event quit;
        Janela.GetEvent(quit);

        if(quit.Type == Event::Closed)
        {
            Janela.Close();
            return EXIT_SUCCESS;
        }

        if(quit.Type == Event::KeyPressed && quit.Key.Code == Key::Escape)
        {
            Janela.Close();
            return EXIT_SUCCESS;
        }

        player1up = Input.IsKeyDown(Key::Up);
        player1down = Input.IsKeyDown(Key::Down);
        player2up = Input.IsKeyDown(Key::W);
        player2down = Input.IsKeyDown(Key::S);

        ///Ball///
        ////Collision with Walls
        if(Ball.GetPosition().x < 415 && Ball.GetPosition().x > -415 && Ball.GetPosition().y > 291)
        {
        blop.Play();
        movebally = -speed;
        }

        if(Ball.GetPosition().x < 415 && Ball.GetPosition().x > -415 && Ball.GetPosition().y < -291)
        {
        blop.Play();
        movebally = speed;
        }

        if(Ball.GetPosition().x > 600)
        {
            yeah.Play();
            score1++;
            score.str("");
            score << score1;
            player1score.SetText(score.str());
            isballonscreen = false;
        }

        if(Ball.GetPosition().x < -600)
        {
            yeah.Play();
            score2++;
            score.str("");
            score << score2;
            player2score.SetText(score.str());
            isballonscreen = false;
        }

        ////Collision with Player 1
        if(Ball.GetPosition().x < -349 && Ball.GetPosition().x > -391 && Ball.GetPosition().y > Player1.GetPosition().y-46 && Ball.GetPosition().y < Player1.GetPosition().y-44)
        {
        ping.Play();
        movebally = -speed;
        }

        if(Ball.GetPosition().x < -349 && Ball.GetPosition().x > -391 && Ball.GetPosition().y < Player1.GetPosition().y+46 && Ball.GetPosition().y > Player1.GetPosition().y+44)
        {
        ping.Play();
        movebally = speed;
        }

        if(Ball.GetPosition().x > -351 && Ball.GetPosition().x < -349 && Ball.GetPosition().y < Player1.GetPosition().y+40 && Ball.GetPosition().y > Player1.GetPosition().y-40)
        {
        ping.Play();
        moveballx = speed;
        }

        if(Ball.GetPosition().x < -349 && Ball.GetPosition().x > -351 && Ball.GetPosition().y-400 > Player1.GetPosition().y-446 && Ball.GetPosition().y-400 < Player1.GetPosition().y-444)
        {
        ping.Play();
        movebally = -speed;
        moveballx = speed;
        }

        if(Ball.GetPosition().x < -349 && Ball.GetPosition().x > -351 && Ball.GetPosition().y-400 < Player1.GetPosition().y-400+46 && Ball.GetPosition().y-400 > Player1.GetPosition().y-400+44)
        {
        ping.Play();
        movebally = speed;
        moveballx = speed;
        }

        ////Collision with Player 2
        if(Ball.GetPosition().x > 349 && Ball.GetPosition().x < 391 && Ball.GetPosition().y > Player2.GetPosition().y-46 && Ball.GetPosition().y < Player2.GetPosition().y-44)
        {
        ping.Play();
        movebally = -speed;
        }

        if(Ball.GetPosition().x > 349 && Ball.GetPosition().x < 391 && Ball.GetPosition().y < Player2.GetPosition().y+46 && Ball.GetPosition().y > Player2.GetPosition().y+44)
        {
        ping.Play();
        movebally = speed;
        }

        if(Ball.GetPosition().x < 351 && Ball.GetPosition().x > 349 && Ball.GetPosition().y < Player2.GetPosition().y+40 && Ball.GetPosition().y > Player2.GetPosition().y-40)
        {
        ping.Play();
        moveballx = -speed;
        }

        if(Ball.GetPosition().x > 349 && Ball.GetPosition().x < 351 && Ball.GetPosition().y-400 > Player2.GetPosition().y-446 && Ball.GetPosition().y-400 < Player2.GetPosition().y-444)
        {
        ping.Play();
        movebally = -speed;
        moveballx = -speed;
        }

        if(Ball.GetPosition().x > 349 && Ball.GetPosition().x < 351 && Ball.GetPosition().y-400 < Player2.GetPosition().y-400+46 && Ball.GetPosition().y-400 > Player2.GetPosition().y-400+44)
        {
        ping.Play();
        movebally = speed;
        moveballx = -speed;
        }

        ////Movement
        Ball.Move(moveballx*Janela.GetFrameTime(), movebally*Janela.GetFrameTime());

        ///Players///
        ////Player 1 Movement
        if(player1up && Player1.GetPosition().y > -255)
        Player1.Move(0,-500*Janela.GetFrameTime());

        if(player1down && Player1.GetPosition().y < 255)
        Player1.Move(0,500*Janela.GetFrameTime());

        ////Player 2 Movement
        if(player2up && Player2.GetPosition().y > -255)
        Player2.Move(0,-500*Janela.GetFrameTime());

        if(player2down && Player2.GetPosition().y < 255)
        Player2.Move(0,500*Janela.GetFrameTime());


        ///Rendering///
        Janela.Clear();
        Janela.Draw(player1score);
        Janela.Draw(player2score);
        Janela.Draw(Line);
        Janela.Draw(Ball);
        Janela.Draw(Player1);
        Janela.Draw(Player2);
        Janela.Display();
        cout << framerate << endl;
    }

return EXIT_SUCCESS;
}


First question why it is that the collision system doesn't work? Is it because the ball is not colliding in one frame and in another it's already past collision zone (the ball is inside the paddle)?

Second question is it really worth to cap the frame rate? If you use (for example) sprite.Move(0, 500*App.GetFrameTime()) then there's no point in capping the frame rate (except for animation, but you can use clocks for that). You could cap the frame rate and don't use App.GetFrameTime but then heavier games on slower pcs wouldn't run properly right?

Help please.
Cheers

nandeeta

  • Newbie
  • *
  • Posts: 1
    • View Profile
Pong game doesn't work when i SetFramerateLimit
« Reply #1 on: November 11, 2011, 07:00:54 am »
Does the ping pong ball and radio hallucination experiment work? There are a million websites now that all say the same thing about this taping ping pong balls over your eyes and listening to static radio. Apparently its supposed to make u hallucinate. Has anyone tried it? Has it worked?
_____________________
yahoo keyword tool ~ overture ~ traffic estimator ~ adwords traffic estimator
a barking dog seldom bite

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
Pong game doesn't work when i SetFramerateLimit
« Reply #2 on: November 11, 2011, 09:24:47 am »
Quote from: "Hair_FTW"

I use for movement sprite.Move(x, y*App.GetFrameTime());


You should not do that.

Rather do something like this:

Code: [Select]

const float UPDATE_INTERVAL = 10f; //10ms -> 100 ticks per second

 float restTime = 0f;

 while(app.IsOpened())
 {
     restTime += app.GetFrameTime();
     while(restTime >= UPDATE_INTERVAL)
     {
          restTime -= UPDATE_INTERVAL;
          Update(UPDATE_INTERVAL); //here you move your sprites and so on
     }
     Draw();
 }


it might be that when reducing the frame rate the move steps get too big so that you are missing a collision.
A fixed timestep is almost in any situation better than using the exact elapsed time.

tobybear

  • Newbie
  • *
  • Posts: 27
    • View Profile
Pong game doesn't work when i SetFramerateLimit
« Reply #3 on: January 19, 2012, 09:41:04 pm »
Using the latest SFML2 from git hub, the example code from P@u1 above does not work on my PC as GetFrameTime() returns 0, so Update is never called as restTime is not increased.
Any suggestions/fix for this? Thanks!