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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - JordyD

Pages: [1]
1
Window / sf::Input and keypress repeat problem
« on: August 10, 2009, 01:49:21 am »
I'm using SFML 1.4 on Ubuntu 9.04.

It seems when I use this code:
Code: [Select]
if (Input.IsKeyDown(sf::Key::A))
        OffsetDirection(1);
if (Input.IsKeyDown(sf::Key::D))
        OffsetDirection(-1);

and press and hold the 'D' or 'A' key, it calls OffsetDirection once, pauses, then calls it continuously as if I was holding the key down (which I am). But I don't want it to pause. Is this normal behavior for sf::Input? If it is, is there something I can use as an alternative that doesn't pause?

By the way, if I configure my computer to have no delay between key press repeating through System>Preferences>Keyboard, then this problem goes away. But I don't want to have to have anybody that wants to use my program to have to toggle their system preferences.

I've tried doing this as an alternative:

Code: [Select]

// outside the main loop
struct KeysPressed {
        bool A;
        bool B;
} KeysPressed;
...
// inside the main loop
if (Event.Type == sf::Event::KeyPressed)
{
        switch (Event.Key.Code)
        {
        case sf::Key::A:
                KeysPressed.A = true;
                break;
        case sf::Key::D:
                KeysPressed.D = true;
                break;
        default:
                break;
        }
}
else if (Event.Type == sf::Event::KeyReleased)
{
        switch (Event.Key.Code)
        {
        case sf::Key::A:
                KeysPressed.A = false;
                break;
        case sf::Key::D:
                KeysPressed.D = false;
                break;
        default:
                break;
        }
}

if (KeysPressed.A == true)
        OffsetDirection(1);
if (KeysPressed.D == true)
        OffsetDirection(-1);


But the problem persists.

Any help would be appreciated.

Thanks,
Jordy

EDIT: I deleted this from System and reposted here, because I realized I posted in the wrong section. Sorry.

2
Graphics / When an sf::Shape::SetPosition is called, what is moved?
« on: August 04, 2009, 04:46:00 am »
Center? Upper-left corner? I was wondering because I'm trying to get sf::Rect and sf::Shape to play nice, meaning they would both move at the same time. So I've implemented a class like this:
Shape.h
Code: [Select]
/*
 * Shape.h
 *
 *  Created on: Aug 3, 2009
 *      Author: Jordy
 */

#ifndef SHAPE_H_
#define SHAPE_H_

#include <algorithm>
#include <SFML/Graphics/Shape.hpp>
#include <SFML/Graphics/Rect.hpp>
#include <SFML/System/Vector2.hpp>

namespace Solids
{

class Shape : public sf::Shape, public sf::Rect<float>
{
public:
Shape();
virtual ~Shape();

virtual void Move(float OffsetX, float OffsetY);
virtual void Move(const sf::Vector2f &Offset);
virtual void SetPosition(float OffsetX, float OffsetY);
virtual void SetPosition(const sf::Vector2f &Offset);

void SetSpeed(float NewSpeedX, float NewSpeedY);
void SetSpeedX(float NewSpeedX);
void SetSpeedY(float NewSpeedY);

void Update(float FrameTime=1);

static Shape Rectangle(float X, float Y, float Width, float Height);
static Shape Triangle(float P1X, float P1Y, float P2X, float P2Y, float P3X, float P3Y);

private:
float SpeedX, SpeedY;
};

}

#endif /* SHAPE_H_ */

Shape.cpp
Code: [Select]
/*
 * Shape.cpp
 *
 *  Created on: Aug 3, 2009
 *      Author: Jordy
 */

#include "Shape.h"

namespace Solids
{

Shape::Shape()
{
}

Shape::~Shape()
{
}

void Shape::Move(float OffsetX, float OffsetY)
{
sf::Shape::Move(OffsetX, OffsetY);
Offset(OffsetX, OffsetY);
}

void Shape::Move(const sf::Vector2f &Offset)
{
Move(Offset.x, Offset.y);
}


void Shape::SetSpeed(float NewSpeedX, float NewSpeedY)
{
SpeedX = NewSpeedX;
SpeedY = NewSpeedY;
}

void Shape::SetSpeedX(float NewSpeedX)
{
SpeedX = NewSpeedX;
}

void Shape::SetSpeedY(float NewSpeedY)
{
SpeedY = NewSpeedY;
}

void Shape::Update(float FrameTime)
{
Move(SpeedX * FrameTime, SpeedY * FrameTime);
}

Shape Shape::Rectangle(float X, float Y, float Width, float Height)
{
Shape Rect;

Rect.Left   = X;
Rect.Top    = Y;
Rect.Right  = Rect.Left + Width;
Rect.Bottom = Rect.Top + Height;

Rect.AddPoint(Rect.Left,  Rect.Top);
Rect.AddPoint(Rect.Right, Rect.Top);
Rect.AddPoint(Rect.Right, Rect.Bottom);
Rect.AddPoint(Rect.Left,  Rect.Bottom);

return Rect;
}

Shape Shape::Triangle(float P1X, float P1Y, float P2X, float P2Y, float P3X, float P3Y)
{
Shape Tri;

// Find boundaries of enclosing sf::Rect
Tri.Left   = std::min(std::min(P1X, P2X), P3X);
Tri.Right  = std::max(std::max(P1X, P2X), P3X);
Tri.Top    = std::min(std::min(P1Y, P2Y), P3Y);
Tri.Bottom = std::max(std::max(P1Y, P2Y), P3Y);

Tri.AddPoint(P1X, P1Y);
Tri.AddPoint(P2X, P2Y);
Tri.AddPoint(P3X, P3Y);

return Tri;
}

}


As you can see, I've implemented a Shape::Move method, but not the Shape::SetPosition method. This is because I'm not sure whether I should be doing this:
Code: [Select]
void Shape::SetPosition(float PosX, float PosY)
{
sf::Shape::SetPosition(PosX, PosY);

float Width  = GetWidth();
float Height = GetHeight();

Left   = PosX;
Top    = PosY;
Right  = Left + Width;
Bottom = Top + Height;
}

or this:
Code: [Select]
void Shape::SetPosition(float PosX, float PosY)
{
sf::Shape::SetPosition(PosX, PosY);

float Width  = GetWidth();
float Height = GetHeight();

Left   = PosX - Width / 2;
Top    = PosY - Height / 2;
Right  = Left + Width;
Bottom = Top  + Height;
}


Note that there may be other errors in my code. I haven't tested it thoroughly yet.

Thanks,
Jordy

3
Feature requests / pkg-config support
« on: August 03, 2009, 05:05:44 am »
It would make it much easier to use SFML. It would mean I don't have to manually edit configure.ac, too.

What do you say?

4
Graphics / error: cannot convert 'const float' to 'sf::Rect<float>
« on: July 24, 2009, 05:10:06 pm »
I'm getting two errors when trying to compile this function:

Code: [Select]
sf::Rect<float>* DeriveRect(sf::Shape* shape)
{
    sf::Rect<float>* rect(shape->GetPointPosition(0).x,
                          shape->GetPointPosition(0).y,
                          shape->GetPointPosition(3).x,
                          shape->GetPointPosition(3).y); /* The errors bring me to this line */
    return rect;
}


The errors:
Code: [Select]
error: initializer expression list treated as compound expression
error: cannot convert 'const float' to 'sf::Rect<float>*' in initialization


I get the feeling that these are simple errors, but I could be wrong.

More information:
OS: Mac OS X 10.5
Compiler: g++
IDE: XCode 3
I have not called this function anywhere in the other parts of the program yet.
This is my first time with SFML.

Could anybody help me out with this?

Thanks,
Jordy

Pages: [1]