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

Author Topic: sf::Joystick::update() destroying my framerate?  (Read 1522 times)

0 Members and 1 Guest are viewing this topic.

Blink

  • Newbie
  • *
  • Posts: 4
    • View Profile
sf::Joystick::update() destroying my framerate?
« on: October 11, 2012, 08:32:56 am »
Hey guys,

First, thanks for having this forum! I am quite appreciative that you are all contributing to the community.

Second: I'm brand new! Never used SFML before, was wary due to setup complications at first, but enjoying it now that it's working. I got it for an OpenGL project that I'm already ten months into, where I needed joystick and audio support. I even have joysticks working now! But... the framerate is absolutely obliterated by making a call to Joystick::update().

If I comment out that one line, everything runs fine again, but my speed gets cut in half if I use it every frame. I can call it once every five frames, and that helps, or once every ten (and the problem is gone, but the controls are terrible - it's a platformer, so it needs to feel tight), but I'd really like to be able to call it at least every other frame and still have it running at the same framerate it was before. My code is as follows, where sendCommands is called once per frame:

Code: [Select]
// Once per loop, send off the commands from these inputs
void sendCommands() {

  // joystick support, ALWAYS update its input
  sf::Joystick::update();
 
  // Update each joystick's info before sending commands
  for (int i=0; i < 4; i++) {
    joystickCommands(i);
    mergeInput(i); // then stick it and key inputs together
  }

  // Then get to those commands being issued!
  if (getGameplayRunning()) {
    ... // the meat of sending off those commands here, no issues previously with this code
  }

Here is where I create the joystick commands from SFML's reports, and temporarily/sloppily reassign joystick positions to key presses:

Code: [Select]
// Figure out all joystick input translations to key presses, for now
void joystickCommands(int i) {
  int joystick = joystickNum(i);
 
  // Accept any not-start-or-select button for jumping
  jumpButton[i] = 0;
  for (int b=0; b<8; b++) {
    if (b != 6 && b != 7) {
      jumpButton[i] = jumpButton[i] || sf::Joystick::isButtonPressed(joystick,b);
    } else if (b == 6) { // Join
      playerJoin(i,sf::Joystick::isButtonPressed(joystick,b));
    } else if (b == 7) { // Pause
      playerPause(i,sf::Joystick::isButtonPressed(joystick,b));
    }
  }

  // Convert (for now) joystick to direction buttons
  upButton[i]   = sf::Joystick::getAxisPosition(joystick,sf::Joystick::Y) <-50;
  downButton[i] = sf::Joystick::getAxisPosition(joystick,sf::Joystick::Y) > 50;
  leftButton[i] = sf::Joystick::getAxisPosition(joystick,sf::Joystick::X) <-50;
  rightButton[i]= sf::Joystick::getAxisPosition(joystick,sf::Joystick::X) > 50;

}

And mergeInput, which just combines the keys and button presses:

Code: [Select]
// Combine buttons and keys under one input system
void mergeInput(int i) {
  // Only covering stuff really used here
  // join and pause handled seperately
  upInput[i] = upButton[i] || upKey[i];
  downInput[i] = downButton[i] || downKey[i];
  leftInput[i] = leftButton[i] || leftKey[i];
  rightInput[i] = rightButton[i] || rightKey[i];
  jumpInput[i] = jumpButton[i] || jumpKey[i];
}

And then there's this extra bit, which shouldn't influence things, but converts the joystick num that the 360 controllers are giving vs. order that they really should be in.

Code: [Select]
// Given a player num, returns joystick num
int joystickNum(int i) {
  // Please note... I don't understand the order either,
  // but until I make player order based on joining order,
  // this works.
  switch(i) {
    case 0: // Player 1
      return 1;
    case 1: // Player 2
      return 2;
    case 2: // Player 3
      return 0;
    case 3: // Player 4
      return 3;
    default:
      return 1;
  }
}

So, does anyone know what could be causing this? Thanks guys!

EDIT: Also, using SFML 2.0 from the download page for Windows, C++ Visual Studio 2010 (32-bit since that's the only one available). Did a Google search with laggy as the keyword now and getting some relevant results, so reading through those now. Hoping I can find another answer and apologize for making the thread!
« Last Edit: October 11, 2012, 08:42:44 am by Blink »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::Joystick::update() destroying my framerate?
« Reply #1 on: October 11, 2012, 10:01:03 am »
You need to get the latest sources, this problem has been fixed since the RC.
Laurent Gomila - SFML developer

Blink

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: sf::Joystick::update() destroying my framerate?
« Reply #2 on: October 11, 2012, 10:11:22 am »
Thank you sir, I am quite indebted to your work on the forum tonight :)

Currently I'm going through the tutorial for compiling 2.0 on my own and I'm guessing that's the path I need to take, using the latest snapshot as the Downloads page calls it. Is this correct, or is there some simpler way than this?

(and again, I shall race to finish building this before you can answer, but it's all new to me right now)

EDIT: I win! Followed the above instructions using CMake and built my own latest libraries and dlls, then copied them over in place of my current ones. No lag now, runs smoothly, and very happy. :) Thanks again!
« Last Edit: October 11, 2012, 10:44:58 am by Blink »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::Joystick::update() destroying my framerate?
« Reply #3 on: October 11, 2012, 10:42:18 am »
Quote
Is this correct
Yes :)
Laurent Gomila - SFML developer

Blink

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: sf::Joystick::update() destroying my framerate?
« Reply #4 on: October 11, 2012, 10:45:38 am »
Oh darn! You beat me to it by two minutes! Apparently my refresh + edit were just slightly too late.

Thanks again though! All good now :)