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

Author Topic: Ubuntu 14.04, window grayed out, but it is still responding.  (Read 2282 times)

0 Members and 1 Guest are viewing this topic.

huuuynh

  • Newbie
  • *
  • Posts: 3
    • View Profile
Hi guys!

I have tried to program some small things but when I run my application, the window grays out after around 5 seconds, but the application is still running and responding ... does anybody know how to get that fixed?


Thank you a lot guys!


Code:

#include <SFML/Graphics.hpp>
#include "ball.h"
#include <iostream>
#include "paddle.h"


int main()
{
    sf::RenderWindow window(sf::VideoMode(1000, 700), "lets olearn");
    window.setFramerateLimit(60);
    ball ball;
    paddle paddle;
    while (window.isOpen())
    {


        window.clear();
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
           break;
        ball.update();
        paddle.update();
        window.draw(ball.shape);
        window.draw(paddle.shape);
        window.display();
    }

    return 0;
}

/*
 * ball.hpp
 *
 *  Created on: May 30, 2015
 *      Author: huynh
 */

#ifndef BALL_HPP_
#define BALL_HPP_

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

/*--------------------------------------- Define class -------------------------------------------------*/
class ball
{

public:
   sf::CircleShape shape;
   sf::Vector2f velocity;
   float x,y;
   int d;

   void update();
   float left();
   float right();
   float top();
   float bot();
   ball();
   ~ball();


};

/*--------------------------------------- Define methods -------------------------------------------------*/
float ball::left()
{
   sf::Vector2f cords = shape.getPosition();
   cords.x -= shape.getRadius();
   return cords.x;
}

float ball::right()
{
   sf::Vector2f cords = shape.getPosition();
   cords.x += shape.getRadius();
   return cords.x;
}

float ball::top()
{
   sf::Vector2f cords = shape.getPosition();
   cords.y -= shape.getRadius();
   return cords.y;
}

float ball::bot()
{
   sf::Vector2f cords = shape.getPosition();
   cords.y += shape.getRadius();
   return cords.y;
}


ball::ball()
{
    x= 500;
    y= 600;
    d= 10;
    shape.setFillColor(sf::Color::Blue);
    shape.setPosition(x,y);
    shape.setRadius(10);
    velocity.x = 5;
    velocity.y = 5;

}

void ball::update()
{


   if( bot() > 700)
      velocity.y = -velocity.y;
   if( top() < 0)
      velocity.y = -velocity.y;
   if( right() > 1000)
      velocity.x = -velocity.x;
   if( left() < 0)
      velocity.x = -velocity.x;


   shape.move(velocity);

/*   if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
       {
        shape.setPosition(x-d, y);
        x-=d;
       }
   if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
       {
        shape.setPosition(x+d, y);
        x+=d;
       }
   if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
       {
        shape.setPosition(x, y+d);
        y+=d;
       }
   if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
       {
        shape.setPosition(x, y-d);
        y-=5;
       }*/
}

ball::~ball()
{

}


#endif /* BALL_HPP_ */

/*
 * paddle.h
 *
 *  Created on: May 31, 2015
 *      Author: huynh
 */

#ifndef PADDLE_H_
#define PADDLE_H_


//--------------------------------------class definition -------------------------
class paddle
{
public:
   sf::RectangleShape shape;
   sf::Vector2f velocity;
   sf::Vector2f position;

   void update();
   paddle();
   ~paddle();

};

//----------------------------------------methods definition--------------------------

paddle::paddle()
{
   sf::Vector2f size(25,100);
   position.x = 950;
   position.y = 350;
   shape.setPosition(position);
   shape.setFillColor(sf::Color::Red);
   shape.setSize(size);
}

void paddle::update()
{

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
       position.y -= 10;
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
          position.y += 10;
    shape.setPosition(position);

}

paddle::~paddle(){};

#endif /* PADDLE_H_ */




shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Ubuntu 14.04, window grayed out, but it is still responding.
« Reply #1 on: June 01, 2015, 08:16:07 pm »
Does this happen with a simple program such as the tutorial display a circle program from the setting up sfml tutorials on this website? Also, please use code tags.

huuuynh

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Ubuntu 14.04, window grayed out, but it is still responding.
« Reply #2 on: June 01, 2015, 08:34:15 pm »
sorry, I am new to this page ...

The tutorial program doens't gray out.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Ubuntu 14.04, window grayed out, but it is still responding.
« Reply #3 on: June 01, 2015, 08:43:55 pm »
You need to handle events as stated in the tutorials.

A mistake that people often make is forget the event loop, simply because they don't yet care about handling events (they use real-time inputs instead). Without an event loop, the window will become unresponsive. It is important to note that the event loop has two roles: in addition to providing events to the user, it gives the window a chance to process its internal events too, which is required so that it can react to move or resize user actions.

Yes it is in red....
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

huuuynh

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Ubuntu 14.04, window grayed out, but it is still responding.
« Reply #4 on: June 01, 2015, 09:02:28 pm »
wow, I am so dumb .. thanks a lot!