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:
// 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:
// 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:
// 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.
// 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!