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 - 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 / 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

3
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]
anything