I have a bit of a problem, stemming from this code:
while (App->IsOpened())
{
sf::Sleep(0.001);
// Process all "application" events
sf::Event Event;
while (App->GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App->Close();
// Escape key : exit
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App->Close();
// Resize event : adjust viewport
if (Event.Type == sf::Event::Resized)
glViewport(0, 0, Event.Size.Width, Event.Size.Height);
/*
if (Event.Key.Code == sf::Key::F1)
{
sf::Image Screen = App->Capture();
Screen.SaveToFile("screenshot.jpg");
}
*/
}
cout << "" << 1/App->GetFrameTime() << endl;
moving = false;
// adjust x- y- and z- step amounts based on current direction/view rotation
xStep = playerSpeed * sin((PI * zRotation) / 180);
yStep = playerSpeed * cos((PI * zRotation) / 180);
zStep = -playerSpeed * sin((PI * rotation) / 180);
//cout << "zRot: " << zRotation << " rot: " << rotation << endl;
// process real-time input
if (Input.IsKeyDown(sf::Key::W)) // W = forwards
{
moving = true;
playerX -= (xStep * cos((PI * rotation) / 180));
playerY -= (yStep * cos((PI * rotation) / 180));
playerZ -= zStep;
}
if (Input.IsKeyDown(sf::Key::S)) // S = backwards
{
moving = true;
playerX += (xStep * cos((PI * rotation) / 180));
playerY += (yStep * cos((PI * rotation) / 180));
playerZ += zStep;
}
if (Input.IsKeyDown(sf::Key::A)) //A = strafe left
{
if ((moving = true))
{
xStep *= 0.707106;
yStep *= 0.707106;
}
playerX += yStep;
playerY -= xStep;
}
if (Input.IsKeyDown(sf::Key::D)) //D = strafe right
{
if ((moving = true))
{
xStep *= 0.707106;
yStep *= 0.707106;
}
playerX -= yStep;
playerY += xStep;
}
// Rotate view based on mouse movement
mouseDeltaX = Input.GetMouseX() - centreX;
mouseDeltaY = Input.GetMouseY() - centreY;
zRotation += (mouseDeltaX / 10);
rotation += (mouseDeltaY / 10);
//cout << "DeltaX: " << mouseDeltaX << " DeltaY: " << mouseDeltaY << endl;
// Z rotation normalisation - between 0 and 360
if (zRotation >= 360)
{
zRotation -= 360;
}
if (zRotation < 0)
{
zRotation += 360;
}
// X/Y rotation limits
if (rotation < -90)
{
rotation = -90;
}
if (rotation >= 90)
{
rotation = 90;
}
// Set the active window before using OpenGL commands
// It's useless here because active window is always the same,
// but don't forget it if you use multiple windows or controls
App->SetActive();
// color and depth buffer
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Rotate the view first
glRotatef(-90 + rotation, 1.f, 0.f, 0.f);
glRotatef(zRotation, 0.f, 0.f, 1.f);
// Then translate it
glTranslatef(playerX, playerY, playerZ);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glCallList(ALL_CUBES);
glFlush();
App->Display();
App->SetCursorPosition(centreX, centreY);
}
return EXIT_SUCCESS;
}
What is happening is when I have the sf::Sleep(0.001) in there, the framerate drops to ~60FPS. When I comment it out, it shoots up to ~300-400FPS. Even if it's drawing only one block, the FPS is ~60FPS with sf::Sleep(0.001). This is weird.
Additionally, I still have the same problem listed in
this thread, with the mouse input.
So I have a few questions:
1) What is happening with the framerate varying so much?
2) What is the difference between Input.GetMouseX() and Event.MouseMoved.X?