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.


Messages - n0body

Pages: [1]
1
Graphics / Newbie question about animating
« on: March 20, 2015, 02:33:05 pm »
Hi, this is probably a very noobish question, but when I make a simple main loop and draw a window with a circle in it, then take keyboard input to move the circle's position up, down, left, and right.

If I don't limit the framerate the circle moves position extremely fast.

So, my question is, how should you control the speed in which it moves?

Should it move based on a system clock or something?

Because obviously the rendering speed is going to be different for all different machines, so trying to control the speed by limiting framerate won't work right everywhere.

2
Window / Re: Need Help Speeding This Up
« on: March 19, 2015, 03:50:56 am »
I basically meant if I use it, will it still compile across all platforms as easily.

3
Window / Re: Need Help Speeding This Up
« on: March 18, 2015, 04:08:58 am »
A little sidenote:

If you are able to use C++11, you can use the function std::to_string to convert numbers to strings.

Is there any good reason why I shouldn't use that if it is available?

4
Window / Re: Need Help Speeding This Up
« on: March 17, 2015, 10:14:47 am »
Wow, thanks.

I also added 20.0f instead of 10.0f to the crosshair position in renderCrosshair() and it works exactly like I wanted it to now.

I feel pretty stupid now, I spent a few hours trying to figure out what was wrong :P

This code works perfect now  :D

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/System/String.hpp>
#include <sstream>

#define SSTR( x ) dynamic_cast< std::ostringstream & >( \
        ( std::ostringstream() << std::dec << x ) ).str()


void renderJoyStickStuff(sf::RenderWindow* wind);
void renderCrosshair(sf::RenderWindow* wind, float Xpos, float Ypos, float deadZone, sf::Color crosshairColor, int index);
sf::Text defTextObj(sf::Font fnt, int charSz, sf::Color clr, sf::Text::Style style, int xPosi, int yPosi, sf::String txt);
int centerWindowX = 0;
int centerWindowY = 0;
sf::RectangleShape xline(sf::Vector2f(20, 1));
sf::RectangleShape yline(sf::Vector2f(20, 1));
sf::Font font;
static const int MaxControllers = 4;
float curCrosshairxPos[MaxControllers - 1];
float curCrosshairyPos[MaxControllers - 1];
sf::Color textColors[3];

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Joystick Input Test");
    centerWindowX = 400;
    centerWindowY = 300;
    for(int i = 0; i < MaxControllers - 1; i++)
    {
        curCrosshairxPos[i] = centerWindowX;
        curCrosshairyPos[i] = centerWindowY;
    }
    yline.setRotation(90.0f);    
    textColors[0] = sf::Color(255, 0, 0);
    textColors[1] = sf::Color(255, 102, 0);
    textColors[2] = sf::Color(255, 255, 0);
    textColors[3] = sf::Color(0, 255, 0);
    if (!font.loadFromFile("UbuntuMono-B.ttf"))
    {
        return 0;
    }
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();            
        renderJoyStickStuff(&window);
        window.display();
    }

    return 0;
}

void renderJoyStickStuff(sf::RenderWindow* wind)
{
    int j1BtnCount = 0, j2BtnCount = 0, textXpos = 2;
    //draw text for each controller
    for(int c = 0; c < MaxControllers; c++)
    {
        int textYpos = 0;
        //controller was detected
        if(sf::Joystick::isConnected(c))
        {
            wind->draw(defTextObj(font, 14, textColors[c], sf::Text::Regular, textXpos, textYpos, "Joy " + SSTR(c) + ": " + sf::Joystick::getIdentification(c).name));
            textYpos += 20;
            //draw text for each button
            for(int i = 0; i < sf::Joystick::getButtonCount(c); i++)
            {
                if(sf::Joystick::isButtonPressed(c, i))
                    wind->draw(defTextObj(font, 14, textColors[c], sf::Text::Regular, textXpos + 8, textYpos, "-Button" + SSTR(i) + ": Pressed"));
                else
                    wind->draw(defTextObj(font, 14, textColors[c], sf::Text::Regular, textXpos + 8, textYpos, "-Button" + SSTR(i) + ": "));
                textYpos += 20;
            }
            //draw text for x axis if any on joystick
            if(sf::Joystick::hasAxis(c, sf::Joystick::X))
            {  
                wind->draw(defTextObj(font, 14, textColors[c], sf::Text::Regular, textXpos + 8, textYpos, "-X Axis:" + SSTR(sf::Joystick::getAxisPosition(c, sf::Joystick::X))));
                textYpos += 20;
            }
            //draw text for y axis if any on joystick
            if(sf::Joystick::hasAxis(c, sf::Joystick::Y))
            {  
                wind->draw(defTextObj(font, 14, textColors[c], sf::Text::Regular, textXpos + 8, textYpos, "-Y Axis:" + SSTR(sf::Joystick::getAxisPosition(c, sf::Joystick::Y))));
                textYpos += 20;
            }
            textXpos += 200;
            //render a crosshair for each detected controller
            renderCrosshair(wind, sf::Joystick::getAxisPosition(c, sf::Joystick::X), sf::Joystick::getAxisPosition(c, sf::Joystick::Y), 25.0f, textColors[c], c);
        }
        //controller not detected :(
        else
        {
            wind->draw(defTextObj(font, 14, textColors[c], sf::Text::Regular, textXpos, textYpos, "Joy " + SSTR(c) + ": Not Detected"));
            textXpos += 200;
        }
    }
}

void renderCrosshair(sf::RenderWindow* wind, float Xpos, float Ypos, float deadZone, sf::Color crosshairColor, int index)
{
    //set the color
    xline.setFillColor(crosshairColor);
    yline.setFillColor(crosshairColor);
    //analog stick is pressed left
    if((curCrosshairxPos[index] > 10.0f) && (Xpos < -deadZone))
    {
        curCrosshairxPos[index] -= 20.0f;
    }
    //analog stick is pressed right
    if((curCrosshairxPos[index] < float(centerWindowX) * 2 - 10.0f) && (Xpos > deadZone))
    {
        curCrosshairxPos[index] += 20.0f;
    }
    //analog stick is moved up
    if((curCrosshairyPos[index] > 10.0f) && (Ypos < -deadZone))
    {
        curCrosshairyPos[index] -= 20.0f;
    }
    //analog stick is moved down
    if((curCrosshairyPos[index] < float(centerWindowY) * 2 - 10.0f) && (Ypos > deadZone))
    {
        curCrosshairyPos[index] += 20.0f;
    }
    //analog stick is released
    if((Xpos < deadZone) && (Xpos > -deadZone) && (Ypos < deadZone) && (Ypos > -deadZone))
    {
        if(curCrosshairxPos[index] > float(centerWindowX))
            curCrosshairxPos[index] -= 20.0f;
        if(curCrosshairxPos[index] < float(centerWindowX))
            curCrosshairxPos[index] += 20.0f;
        if(curCrosshairyPos[index] > float(centerWindowY))
            curCrosshairyPos[index] -= 20.0f;
        if(curCrosshairyPos[index] < float(centerWindowY))
            curCrosshairyPos[index] += 20.0f;
    }
    xline.setPosition(int(curCrosshairxPos[index]) - 10, int(curCrosshairyPos[index]));
    yline.setPosition(int(curCrosshairxPos[index]), int(curCrosshairyPos[index]) - 10);
    wind->draw(xline);
    wind->draw(yline);
    return;
}

sf::Text defTextObj(sf::Font fnt, int charSz, sf::Color clr, sf::Text::Style style, int xPosi, int yPosi, sf::String txt)
{
    sf::Text txtObj(txt, fnt, charSz);
    txtObj.setStyle(style);
    txtObj.setPosition(xPosi, yPosi);
    txtObj.setColor(clr);
    return txtObj;
}

5
Window / Need Help Speeding This Up
« on: March 17, 2015, 09:47:36 am »
Hi, I'm not very good at programming.  ;D

I'm trying to learn more with SFML.

I wrote this joystick input test program to test a maximum of 4 controllers it's got a few problems mostly the fact that it's very sluggish at least for me.

Can anyone tell me why it's so slow? I feel like I'm doing something wrong.

Here's the whole thing

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/System/String.hpp>
#include <sstream>

#define SSTR( x ) dynamic_cast< std::ostringstream & >( \
        ( std::ostringstream() << std::dec << x ) ).str()


void renderJoyStickStuff(sf::RenderWindow* wind);
void renderCrosshair(sf::RenderWindow* wind, float Xpos, float Ypos, float deadZone, sf::Color crosshairColor, int index);
sf::Text defTextObj(sf::Font fnt, int charSz, sf::Color clr, sf::Text::Style style, int xPosi, int yPosi, sf::String txt);
int centerWindowX = 0;
int centerWindowY = 0;
sf::RectangleShape xline(sf::Vector2f(20, 1));
sf::RectangleShape yline(sf::Vector2f(20, 1));
sf::Font font;
static const int MaxControllers = 4;
float curCrosshairxPos[MaxControllers - 1];
float curCrosshairyPos[MaxControllers - 1];
sf::Color textColors[3];

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Joystick Input Test");
    centerWindowX = 400;
    centerWindowY = 300;
    for(int i = 0; i < MaxControllers - 1; i++)
    {
        curCrosshairxPos[i] = centerWindowX;
        curCrosshairyPos[i] = centerWindowY;
    }
    yline.setRotation(90.0f);    
    textColors[0] = sf::Color(255, 0, 0);
    textColors[1] = sf::Color(255, 102, 0);
    textColors[2] = sf::Color(255, 255, 0);
    textColors[3] = sf::Color(0, 255, 0);
    if (!font.loadFromFile("UbuntuMono-B.ttf"))
    {
        return 0;
    }
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
            window.clear();            
            renderJoyStickStuff(&window);
            window.display();
        }
    }

    return 0;
}

void renderJoyStickStuff(sf::RenderWindow* wind)
{
    int j1BtnCount = 0, j2BtnCount = 0, textXpos = 2;
    //draw text for each controller
    for(int c = 0; c < MaxControllers; c++)
    {
        int textYpos = 0;
        //controller was detected
        if(sf::Joystick::isConnected(c))
        {
            wind->draw(defTextObj(font, 14, textColors[c], sf::Text::Regular, textXpos, textYpos, "Joy " + SSTR(c) + ": " + sf::Joystick::getIdentification(c).name));
            textYpos += 20;
            //draw text for each button
            for(int i = 0; i < sf::Joystick::getButtonCount(c); i++)
            {
                if(sf::Joystick::isButtonPressed(c, i))
                    wind->draw(defTextObj(font, 14, textColors[c], sf::Text::Regular, textXpos + 8, textYpos, "-Button" + SSTR(i) + ": Pressed"));
                else
                    wind->draw(defTextObj(font, 14, textColors[c], sf::Text::Regular, textXpos + 8, textYpos, "-Button" + SSTR(i) + ": "));
                textYpos += 20;
            }
            //draw text for x axis if any on joystick
            if(sf::Joystick::hasAxis(c, sf::Joystick::X))
            {  
                wind->draw(defTextObj(font, 14, textColors[c], sf::Text::Regular, textXpos + 8, textYpos, "-X Axis:" + SSTR(sf::Joystick::getAxisPosition(c, sf::Joystick::X))));
                textYpos += 20;
            }
            //draw text for y axis if any on joystick
            if(sf::Joystick::hasAxis(c, sf::Joystick::Y))
            {  
                wind->draw(defTextObj(font, 14, textColors[c], sf::Text::Regular, textXpos + 8, textYpos, "-Y Axis:" + SSTR(sf::Joystick::getAxisPosition(c, sf::Joystick::Y))));
                textYpos += 20;
            }
            textXpos += 200;
            //render a crosshair for each detected controller
            renderCrosshair(wind, sf::Joystick::getAxisPosition(c, sf::Joystick::X), sf::Joystick::getAxisPosition(c, sf::Joystick::Y), 25.0f, textColors[c], c);
        }
        //controller not detected :(
        else
        {
            wind->draw(defTextObj(font, 14, textColors[c], sf::Text::Regular, textXpos, textYpos, "Joy " + SSTR(c) + ": Not Detected"));
            textXpos += 200;
        }
    }
}

void renderCrosshair(sf::RenderWindow* wind, float Xpos, float Ypos, float deadZone, sf::Color crosshairColor, int index)
{
    //set the color
    xline.setFillColor(crosshairColor);
    yline.setFillColor(crosshairColor);
    //analog stick is pressed left
    if((curCrosshairxPos[index] > 10.0f) && (Xpos < -deadZone))
    {
        curCrosshairxPos[index] -= 10.0f;
    }
    //analog stick is pressed right
    if((curCrosshairxPos[index] < float(centerWindowX) * 2 - 10.0f) && (Xpos > deadZone))
    {
        curCrosshairxPos[index] += 10.0f;
    }
    //analog stick is moved up
    if((curCrosshairyPos[index] > 10.0f) && (Ypos < -deadZone))
    {
        curCrosshairyPos[index] -= 10.0f;
    }
    //analog stick is moved down
    if((curCrosshairyPos[index] < float(centerWindowY) * 2 - 10.0f) && (Ypos > deadZone))
    {
        curCrosshairyPos[index] += 10.0f;
    }
    //analog stick is released
    if((Xpos < deadZone) && (Xpos > -deadZone) && (Ypos < deadZone) && (Ypos > -deadZone))
    {
        if(curCrosshairxPos[index] > float(centerWindowX))
            curCrosshairxPos[index] -= 10.0f;
        if(curCrosshairxPos[index] < float(centerWindowX))
            curCrosshairxPos[index] += 10.0f;
        if(curCrosshairyPos[index] > float(centerWindowY))
            curCrosshairyPos[index] -= 10.0f;
        if(curCrosshairyPos[index] < float(centerWindowY))
            curCrosshairyPos[index] += 10.0f;
    }
    xline.setPosition(int(curCrosshairxPos[index]) - 10, int(curCrosshairyPos[index]));
    yline.setPosition(int(curCrosshairxPos[index]), int(curCrosshairyPos[index]) - 10);
    wind->draw(xline);
    wind->draw(yline);
    return;
}

sf::Text defTextObj(sf::Font fnt, int charSz, sf::Color clr, sf::Text::Style style, int xPosi, int yPosi, sf::String txt)
{
    sf::Text txtObj(txt, fnt, charSz);
    txtObj.setStyle(style);
    txtObj.setPosition(xPosi, yPosi);
    txtObj.setColor(clr);
    return txtObj;
}

edit: I didn't write the SSTR() thing, I stole it from the internet because I'm too lazy and stupid to figure out how to cast a number to string on my own :D

6
Window / Re: SFML joystick problem?
« on: March 16, 2015, 08:00:59 am »
Yeah, here's the output

$ find /dev/input/ -iname 'js*' -exec udevadm info -q all '{}' \;
P: /devices/pci0000:00/0000:00:10.0/usb6/6-1/6-1:1.0/input/input121/js1
N: input/js1
S: input/by-id/usb-0810_Twin_USB_Joystick-joystick
S: input/by-path/pci-0000:00:10.0-usb-0:1:1.0-joystick
E: DEVLINKS=/dev/input/by-id/usb-0810_Twin_USB_Joystick-joystick /dev/input/by-path/pci-0000:00:10.0-usb-0:1:1.0-joystick
E: DEVNAME=/dev/input/js1
E: DEVPATH=/devices/pci0000:00/0000:00:10.0/usb6/6-1/6-1:1.0/input/input121/js1
E: ID_BUS=usb
E: ID_FOR_SEAT=input-pci-0000_00_10_0-usb-0_1_1_0
E: ID_INPUT=1
E: ID_INPUT_JOYSTICK=1
E: ID_MODEL=Twin_USB_Joystick
E: ID_MODEL_ENC=Twin\x20USB\x20Joystick
E: ID_MODEL_ID=0001
E: ID_PATH=pci-0000:00:10.0-usb-0:1:1.0
E: ID_PATH_TAG=pci-0000_00_10_0-usb-0_1_1_0
E: ID_REVISION=0106
E: ID_SERIAL=0810_Twin_USB_Joystick
E: ID_TYPE=hid
E: ID_USB_DRIVER=usbhid
E: ID_USB_INTERFACES=:030000:
E: ID_USB_INTERFACE_NUM=00
E: ID_VENDOR=0810
E: ID_VENDOR_ENC=0810
E: ID_VENDOR_ID=0810
E: MAJOR=13
E: MINOR=1
E: SUBSYSTEM=input
E: TAGS=:seat:uaccess:
E: USEC_INITIALIZED=90765466996

P: /devices/pci0000:00/0000:00:10.0/usb6/6-1/6-1:1.0/input/input120/js0
N: input/js0
S: input/by-id/usb-0810_Twin_USB_Joystick-joystick
S: input/by-path/pci-0000:00:10.0-usb-0:1:1.0-joystick
E: DEVLINKS=/dev/input/by-id/usb-0810_Twin_USB_Joystick-joystick /dev/input/by-path/pci-0000:00:10.0-usb-0:1:1.0-joystick
E: DEVNAME=/dev/input/js0
E: DEVPATH=/devices/pci0000:00/0000:00:10.0/usb6/6-1/6-1:1.0/input/input120/js0
E: ID_BUS=usb
E: ID_FOR_SEAT=input-pci-0000_00_10_0-usb-0_1_1_0
E: ID_INPUT=1
E: ID_INPUT_JOYSTICK=1
E: ID_MODEL=Twin_USB_Joystick
E: ID_MODEL_ENC=Twin\x20USB\x20Joystick
E: ID_MODEL_ID=0001
E: ID_PATH=pci-0000:00:10.0-usb-0:1:1.0
E: ID_PATH_TAG=pci-0000_00_10_0-usb-0_1_1_0
E: ID_REVISION=0106
E: ID_SERIAL=0810_Twin_USB_Joystick
E: ID_TYPE=hid
E: ID_USB_DRIVER=usbhid
E: ID_USB_INTERFACES=:030000:
E: ID_USB_INTERFACE_NUM=00
E: ID_VENDOR=0810
E: ID_VENDOR_ENC=0810
E: ID_VENDOR_ID=0810
E: MAJOR=13
E: MINOR=0
E: SUBSYSTEM=input
E: TAGS=:seat:uaccess:
E: USEC_INITIALIZED=1690765466899
 



I've noticed that if you plug the joystick in after you open the program, or if you unplug then replug there's a bunch of latency when calling sf::Joystick::isButtonPressed(), but if I have the joystick already plugged in and then run the program, there isn't any latency when calling sf::Joystick::isButtonPressed.

I slapped this crap together real quick to test the buttons.

Of course, I noticed after writing this that it is actually the wrong way to get joystick input, I should have used joystickbutton events, but it does work and I'm still learning  ;D
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <sstream>


#define SSTR( x ) dynamic_cast< std::ostringstream & >( \
        ( std::ostringstream() << std::dec << x ) ).str()


void renderJoyStickText(sf::RenderWindow* wind);

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Joystick Input Test");
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
            window.clear();            
            renderJoyStickText(&window);
            window.display();
        }
    }

    return 0;
}


void renderJoyStickText(sf::RenderWindow* wind)
{
    int joy2Ypos = 20;
    bool joy1identified = false;
    int j1BtnCount = 0;
   
    sf::Font font;
    sf::Text joy1_text;
    sf::Text joy2_text;
    sf::Text joy1_nameText;
    if (!font.loadFromFile("UbuntuMono-B.ttf"))
    {
        return;
    }
    joy1_text.setFont(font);
    joy1_text.setCharacterSize(14);
    joy1_text.setColor(sf::Color::Yellow);
    joy1_text.setStyle(sf::Text::Regular);
    joy1_text.setPosition(2,0);
    joy2_text.setFont(font);
    joy2_text.setCharacterSize(14);
    joy2_text.setColor(sf::Color::Red);
    joy2_text.setStyle(sf::Text::Regular);
    joy2_text.setPosition(2,joy2Ypos);
    if(sf::Joystick::isConnected(0))
    {
        joy1_text.setString("Joy 1: True");
        joy1_nameText.setFont(font);
        joy1_nameText.setCharacterSize(14);
        joy1_nameText.setColor(sf::Color::Yellow);
        joy1_nameText.setStyle(sf::Text::Regular);
        joy1_nameText.setPosition(5,20);
        joy1_nameText.setString("-name:" + sf::Joystick::getIdentification(0).name);
        joy2Ypos += 20;
        joy1identified = true;
    }
    else
    {
        joy1_text.setString("Joy 1: False");
        if(joy1identified)
        {
            joy1identified = false;
            joy2Ypos = 20;
        }
    }
    if(sf::Joystick::isConnected(1))
    {
        joy2_text.setString("Joy 2: True");
    }
    else
    {
        joy2_text.setString("Joy 2: False");
    }
    wind->draw(joy1_text);
    if(joy1identified)
    {
        wind->draw(joy1_nameText);
        j1BtnCount = sf::Joystick::getButtonCount(0);
        sf::Text j1BtnText[j1BtnCount];
        //draw text for each button
        for(int i = 0; i < j1BtnCount; i++)
        {
            joy2Ypos += 20;
            j1BtnText[i].setFont(font);
            j1BtnText[i].setCharacterSize(14);
            j1BtnText[i].setColor(sf::Color::Yellow);
            j1BtnText[i].setStyle(sf::Text::Regular);
            j1BtnText[i].setPosition(5, joy2Ypos - 20);
            if(sf::Joystick::isButtonPressed(0, i))
                j1BtnText[i].setString("Button " + SSTR(i) + ": Pressed");
            else
                j1BtnText[i].setString("Button " + SSTR(i) + ": ");
            wind->draw(j1BtnText[i]);
        }
    }
    joy2_text.setPosition(2,joy2Ypos);
    wind->draw(joy2_text);
}

I definitely get the same error messages exactly like that issue, so it's likely the same problem.

7
Window / Re: SFML joystick problem?
« on: March 16, 2015, 06:12:39 am »
Ok, I'll try that out. :D

Here's the output, it was too long to paste in here so I put it on pastebin.
http://pastebin.com/YapxdZqX


EDIT:

I recompiled with that block and commented out the other part, and it's still spamming with the error, I recompiled with test at the end to make sure it's actually changed and using the new code:
Failed to flush inotify of all pending joystick events. test

EDIT 2:
Don't know if this helps anything, but result is always -1, I recompiled with this change:
if (result < 0)
            err() << "Failed to flush inotify of all pending joystick events. result:" << result << std::endl;

and it spams output with:
Failed to flush inotify of all pending joystick events. result:-1

EDIT 3:  :P
Recompiled with this change, and it no longer spams the output, everything works as it should when unplugging/plugging the usb joystick:
        if (result < -1)
            err() << "Failed to flush inotify of all pending joystick events. result:" << result << std::endl;
I don't know if this is wrong, but it has silenced the error spam.

8
Window / Re: SFML joystick problem?
« on: March 16, 2015, 03:47:56 am »
Ok, here's what it's spamming now that I rebuilt with that code when I unplug while it's running:

Failed to flush inotify of all pending joystick events.29
Failed to flush inotify of all pending joystick events.29
Failed to flush inotify of all pending joystick events.29
Failed to flush inotify of all pending joystick events.29
Failed to flush inotify of all pending joystick events.29
Failed to flush inotify of all pending joystick events.29
Failed to flush inotify of all pending joystick events.29
Failed to flush inotify of all pending joystick events.29
Failed to flush inotify of all pending joystick events.29
Failed to flush inotify of all pending joystick events.29
Failed to flush inotify of all pending joystick events.29
 

9
Window / Re: SFML joystick problem?
« on: March 16, 2015, 02:44:37 am »
If you built SFML yourself, can you make the necessary changes to this part of SFML/src/SFML/Window/Unix/JoystickImpl.cpp to print out errno as well?

Something like:
if (lseek(notifyFd, 0, SEEK_END) < 0)
    err() << "Failed to flush inotify of all pending joystick events: " << errno << std::endl;

Run the same test and post back here with the printed error number. :)

I actually didn't build it myself, but I'll build it myself and add that then report back with what happens.

10
Window / SFML joystick problem?
« on: March 16, 2015, 02:26:52 am »
Hi, I wrote some shitty code to test out sfml's joystick stuff

Here's the shitty code (I just threw this together in like 5 minutes)
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>


void renderJoyStickText(sf::RenderWindow* wind);


int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Joystick Input Test");
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
            window.clear();            
            renderJoyStickText(&window);
            window.display();
        }
    }

    return 0;
}


void renderJoyStickText(sf::RenderWindow* wind)
{
    int joy2Ypos = 20;
    bool joy1identified = false;
   
    sf::Font font;
    sf::Text joy1_text;
    sf::Text joy2_text;
    sf::Text joy1_nameText;
    if (!font.loadFromFile("UbuntuMono-B.ttf"))
    {
        return;
    }
    joy1_text.setFont(font);
    joy1_text.setCharacterSize(14);
    joy1_text.setColor(sf::Color::Yellow);
    joy1_text.setStyle(sf::Text::Regular);
    joy1_text.setPosition(2,0);
    joy2_text.setFont(font);
    joy2_text.setCharacterSize(14);
    joy2_text.setColor(sf::Color::Red);
    joy2_text.setStyle(sf::Text::Regular);
    joy2_text.setPosition(2,joy2Ypos);
    if(sf::Joystick::isConnected(0))
    {
        joy1_text.setString("Joy 1: True");
        joy1_nameText.setFont(font);
        joy1_nameText.setCharacterSize(14);
        joy1_nameText.setColor(sf::Color::Yellow);
        joy1_nameText.setStyle(sf::Text::Regular);
        joy1_nameText.setPosition(5,20);
        joy1_nameText.setString("-name:" + sf::Joystick::getIdentification(0).name);
        joy2Ypos += 20;
        joy1identified = true;
    }
    else
    {
        joy1_text.setString("Joy 1: False");
        if(joy1identified)
        {
            joy1identified = false;
            joy2Ypos = 20;
        }
    }
    if(sf::Joystick::isConnected(1))
    {
        joy2_text.setString("Joy 2: True");
    }
    else
    {
        joy2_text.setString("Joy 2: False");
    }
    joy2_text.setPosition(2,joy2Ypos);
    wind->draw(joy1_text);
    if(joy1identified)
        wind->draw(joy1_nameText);
    wind->draw(joy2_text);
}


Now, here's what happens when i run this:

First I get this weird output when the program starts:
Unable to get joystick attribute. Could not find USB device for joystick at index 0.
Unable to get joystick attribute. Could not find USB device for joystick at index 0.
Failed to flush inotify of all pending joystick events.
Unable to get joystick attribute. Could not find USB device for joystick at index 1.
Unable to get joystick attribute. Could not find USB device for joystick at index 1.
 


the window lags a little bit, but then seemingly works fine.

Now, if I unplug the joystick while the program is still running it spams the console with this:

Failed to flush inotify of all pending joystick events.
Failed to flush inotify of all pending joystick events.
Failed to flush inotify of all pending joystick events.
Failed to flush inotify of all pending joystick events.
Failed to flush inotify of all pending joystick events.
Failed to flush inotify of all pending joystick events.
 

Here's a shitty video I made to show what happens.
https://www.youtube.com/embed/sK0pgl1vxnA

The joystick I'm using is a crappy $4.00 dual port playstation 2 -> usb converter with only one joystick plugged into slot one. It always registers both joysticks as plugged in when i plug it into a usb port even if I don't have any joysticks plugged into either slot. This is normal, It's just how the device was made I guess.

So, why does this happen?

When I was using older libraries, I didn't get the errors when the program launched, but I couldn't use the sf::Joystick::getIdentification function because it wasn't there yet I guess.

Pages: [1]