1
General / Jerky gameplay
« on: November 19, 2014, 03:23:47 pm »
Hi guys, made a small sample program. That will eventually become my second game. But I got this weird jerky game-play when moving around etc... also notice some screen-tearing and I dont get what causes this weird phenomenon.
Executable: http://puu.sh/cX70G/9dc21552d5.zip
Project Files: http://puu.sh/cX7j3/fd2d7c3238.zip
Anyone got any clue's? Is it also jerky on your computer?
The camera class and player class are very simple so I didn't include them. Could the problem be in this code? I also have an old Asus k52n laptop.
Executable: http://puu.sh/cX70G/9dc21552d5.zip
Project Files: http://puu.sh/cX7j3/fd2d7c3238.zip
Anyone got any clue's? Is it also jerky on your computer?
const sf::Time Game::kTimePerFrame = sf::seconds(1.f / 60.f);
Game::Game()
: window_(sf::VideoMode(720, 540), "Prototype"),
player_("assets/textures/player.png") {
window_.setVerticalSyncEnabled(true); // Without this there is less stuttering but also takes like 45% cpu
camera_.AdaptSize(window_); // Simply gets window size, and set view to same size.
camera_.Attach(player_); // Set view/camera center to player
if (!background_texture.loadFromFile("assets/textures/background.png"))
std::cout << "Failed to load background.png" << std::endl;
background_.setTexture(background_texture);
}
// Logic loop is fixed at 60, render loop unlimited but limited with v-sync.
void Game::Run() {
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (window_.isOpen()) {
timeSinceLastUpdate += clock.restart();
while (timeSinceLastUpdate > kTimePerFrame) {
timeSinceLastUpdate -= kTimePerFrame;
ProcessEvents();
Update(kTimePerFrame);
}
Render();
}
}
void Game::ProcessEvents() {
sf::Event event;
while (window_.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window_.close();
player_.ProcessEvents(event); // Player events like walking...
}
}
void Game::Update(const sf::Time& dt) {
player_.Update(dt);
camera_.Update(dt);
}
void Game::Render() {
window_.setView(camera_.GetView()); // Get sf::view from camera class.
window_.clear(sf::Color(15, 15, 15, 255));
window_.draw(background_);
window_.draw(player_);
window_.display();
}
Game::Game()
: window_(sf::VideoMode(720, 540), "Prototype"),
player_("assets/textures/player.png") {
window_.setVerticalSyncEnabled(true); // Without this there is less stuttering but also takes like 45% cpu
camera_.AdaptSize(window_); // Simply gets window size, and set view to same size.
camera_.Attach(player_); // Set view/camera center to player
if (!background_texture.loadFromFile("assets/textures/background.png"))
std::cout << "Failed to load background.png" << std::endl;
background_.setTexture(background_texture);
}
// Logic loop is fixed at 60, render loop unlimited but limited with v-sync.
void Game::Run() {
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (window_.isOpen()) {
timeSinceLastUpdate += clock.restart();
while (timeSinceLastUpdate > kTimePerFrame) {
timeSinceLastUpdate -= kTimePerFrame;
ProcessEvents();
Update(kTimePerFrame);
}
Render();
}
}
void Game::ProcessEvents() {
sf::Event event;
while (window_.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window_.close();
player_.ProcessEvents(event); // Player events like walking...
}
}
void Game::Update(const sf::Time& dt) {
player_.Update(dt);
camera_.Update(dt);
}
void Game::Render() {
window_.setView(camera_.GetView()); // Get sf::view from camera class.
window_.clear(sf::Color(15, 15, 15, 255));
window_.draw(background_);
window_.draw(player_);
window_.display();
}
The camera class and player class are very simple so I didn't include them. Could the problem be in this code? I also have an old Asus k52n laptop.