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

Author Topic: Laggy / Slowness with hotkeys. / Multiple Hotkeys  (Read 1853 times)

0 Members and 1 Guest are viewing this topic.

Nolan

  • Newbie
  • *
  • Posts: 6
    • View Profile
Laggy / Slowness with hotkeys. / Multiple Hotkeys
« on: June 12, 2011, 12:58:22 am »
I have a couple questions / problems, that I'd love if you guys could help.

a) How would I implement multiple hot keys at once? ex. I can be going left and jumping at the same time without lag / freezing.

b) When moving my sprite, I find that there is a lag before it starts to actually move. How can I fix this?

My code is as follows:

Code: [Select]


#include <SFML/Graphics.hpp>
int pX,pY,Speed = 6;
sf::Event Event;
sf::Sprite Box;
HWND hwnd;
int main()
{
hwnd = FindWindow("Test",NULL);
ShowWindow(hwnd,SW_HIDE);
sf::RenderWindow fa(sf::VideoMode(800,600,256),"Test");
sf::Image image;

if(!image.LoadFromFile("res/Box1.png"))
return EXIT_FAILURE;
Box.SetImage(image);
pY = fa.GetHeight() - (image.GetHeight());
pX = fa.GetWidth() / 2;
while(fa.IsOpened())
{
Box.SetPosition(pX,pY);

while(fa.GetEvent(Event))
{
if (fa.GetInput().IsKeyDown(sf::Key::Left))  pX -= Speed;
if (fa.GetInput().IsKeyDown(sf::Key::Right)) pX += Speed;
if(Event.Type == sf::Event::Closed)
fa.Close();
if(pX > fa.GetWidth()) pX = 0;
}
sf::Image bg;
if(!bg.LoadFromFile("res/background.png"))
return EXIT_FAILURE;
sf::Sprite BG;
BG.SetImage(bg);
BG.SetPosition(0,0);
fa.Clear();
fa.Draw(BG);
fa.Draw(Box);
fa.Display();
}

return 0;
}
[/code]

Xander314

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • http://sfmlcoder.wordpress.com/
    • Email
Laggy / Slowness with hotkeys. / Multiple Hotkeys
« Reply #1 on: June 12, 2011, 09:08:54 am »
I don't think your real time input processing (with sf::Window::GetInput) should be inside the event loop.

EDIT: Only put event processing in there and nothing else as there are no benefits and the obvious disadvantage which you are experiencing.

Nolan

  • Newbie
  • *
  • Posts: 6
    • View Profile
Laggy / Slowness with hotkeys. / Multiple Hotkeys
« Reply #2 on: June 12, 2011, 05:24:45 pm »
Yeah I realized that after messing around with the code, thanks! I also limit framerate to 60.