Make scrolling speed depend on frame duration.
This commit is contained in:
parent
7e8657b9d8
commit
33c18680d6
7 changed files with 37 additions and 18 deletions
|
@ -41,6 +41,7 @@ namespace curry {
|
|||
} //unnamed namespace
|
||||
|
||||
GameSceneBase::GameSceneBase (cloonel::SDLMain* parSdlMain) :
|
||||
m_time0(std::chrono::steady_clock::now()),
|
||||
m_input(std::make_unique<cloonel::InputBag>()),
|
||||
m_sdlmain(parSdlMain),
|
||||
m_wants_to_quit(false)
|
||||
|
@ -53,6 +54,8 @@ namespace curry {
|
|||
void GameSceneBase::prepare() {
|
||||
m_wants_to_quit = false;
|
||||
this->on_prepare();
|
||||
|
||||
m_time0 = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
void GameSceneBase::destroy() noexcept {
|
||||
|
@ -69,12 +72,16 @@ namespace curry {
|
|||
}
|
||||
|
||||
void GameSceneBase::exec() {
|
||||
const auto time1 = std::chrono::steady_clock::now();
|
||||
|
||||
const bool quit = DoEvents(*m_input, m_sdlmain);
|
||||
m_input->KeyStateUpdateFinished();
|
||||
if (quit)
|
||||
set_wants_to_quit();
|
||||
|
||||
this->on_update();
|
||||
const float delta = std::chrono::duration<float>(time1 - m_time0).count();
|
||||
m_time0 = time1;
|
||||
this->on_update(delta);
|
||||
}
|
||||
|
||||
cloonel::SDLMain* GameSceneBase::sdl_main() {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <chrono>
|
||||
|
||||
namespace cloonel {
|
||||
class InputBag;
|
||||
|
@ -24,11 +25,12 @@ namespace curry {
|
|||
protected:
|
||||
virtual void on_prepare() = 0;
|
||||
virtual void on_destroy() noexcept = 0;
|
||||
virtual void on_update() = 0;
|
||||
virtual void on_update (float parDeltaT) = 0;
|
||||
cloonel::SDLMain* sdl_main();
|
||||
cloonel::InputBag& input_bag();
|
||||
|
||||
private:
|
||||
std::chrono::time_point<std::chrono::steady_clock> m_time0;
|
||||
std::unique_ptr<cloonel::InputBag> m_input;
|
||||
cloonel::SDLMain* m_sdlmain;
|
||||
bool m_wants_to_quit;
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include "key.hpp"
|
||||
#include "inputbag.hpp"
|
||||
#include <SDL2/SDL.h>
|
||||
#include <algorithm>
|
||||
|
||||
namespace curry {
|
||||
namespace {
|
||||
|
@ -47,15 +48,21 @@ namespace curry {
|
|||
m_worldtiles.load(RESOURCES_PATH "textures.png", *this->sdl_main());
|
||||
}
|
||||
|
||||
void IngameScene::on_update() {
|
||||
void IngameScene::on_update (float parDeltaT) {
|
||||
const float speed_per_sec =
|
||||
static_cast<float>(
|
||||
std::max(m_world.tile_size().x(), m_world.tile_size().y())
|
||||
) * 1.5f;
|
||||
|
||||
const float speed = parDeltaT * speed_per_sec;
|
||||
if (cloonel::IsPressed(input_bag(), ActionRight))
|
||||
m_viewport.set_position(m_viewport.position() + vec2i(1, 0));
|
||||
m_viewport.set_position(m_viewport.position() + vec2f(speed, 0));
|
||||
if (cloonel::IsPressed(input_bag(), ActionLeft))
|
||||
m_viewport.set_position(m_viewport.position() - vec2i(1, 0));
|
||||
m_viewport.set_position(m_viewport.position() - vec2f(speed, 0));
|
||||
if (cloonel::IsPressed(input_bag(), ActionUp))
|
||||
m_viewport.set_position(m_viewport.position() - vec2i(0, 1));
|
||||
m_viewport.set_position(m_viewport.position() - vec2f(0, speed));
|
||||
if (cloonel::IsPressed(input_bag(), ActionDown))
|
||||
m_viewport.set_position(m_viewport.position() + vec2i(0, 1));
|
||||
m_viewport.set_position(m_viewport.position() + vec2f(0, speed));
|
||||
|
||||
const auto tilesize(m_world.tile_size());
|
||||
for (auto tile : m_viewport) {
|
||||
|
|
|
@ -18,7 +18,7 @@ namespace curry {
|
|||
protected:
|
||||
virtual void on_prepare() override;
|
||||
virtual void on_destroy() noexcept override;
|
||||
virtual void on_update() override;
|
||||
virtual void on_update (float parDeltaT) override;
|
||||
|
||||
private:
|
||||
WorldGrid m_world;
|
||||
|
|
|
@ -25,7 +25,10 @@ namespace curry {
|
|||
m_index(parEnd ? -1 : 0)
|
||||
{
|
||||
assert(m_viewport);
|
||||
m_pixel_pos = first_col_starting_pixel(m_viewport->position(), m_viewport->world()->tile_size());
|
||||
m_pixel_pos = first_col_starting_pixel(
|
||||
static_cast<vec2i>(m_viewport->position()),
|
||||
m_viewport->world()->tile_size()
|
||||
);
|
||||
}
|
||||
|
||||
void TileIterator::increment() {
|
||||
|
@ -34,7 +37,7 @@ namespace curry {
|
|||
const WorldGrid& world = *m_viewport->world();
|
||||
vec2i tile_size(world.tile_size());
|
||||
auto first_tile_pos = first_col_starting_pixel(
|
||||
m_viewport->position(),
|
||||
static_cast<vec2i>(m_viewport->position()),
|
||||
m_viewport->world()->tile_size()
|
||||
);
|
||||
vec2i pos(tile_size * m_index + first_tile_pos);
|
||||
|
|
|
@ -15,11 +15,11 @@ namespace curry {
|
|||
return m_world;
|
||||
}
|
||||
|
||||
const vec2i& WorldViewport::position() const {
|
||||
const vec2f& WorldViewport::position() const {
|
||||
return m_position;
|
||||
}
|
||||
|
||||
const vec2i& WorldViewport::size() const {
|
||||
const vec2f& WorldViewport::size() const {
|
||||
return m_size;
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,7 @@ namespace curry {
|
|||
return iterator(this, true);
|
||||
}
|
||||
|
||||
void WorldViewport::set_position (const vec2i& parPos) {
|
||||
void WorldViewport::set_position (const vec2f& parPos) {
|
||||
m_position = parPos; //TODO: assert validity
|
||||
}
|
||||
} //namespace curry
|
||||
|
|
|
@ -12,17 +12,17 @@ namespace curry {
|
|||
|
||||
WorldViewport (WorldGrid* parWorld, vec2i parSize);
|
||||
const WorldGrid* world() const;
|
||||
const vec2i& position() const;
|
||||
const vec2i& size() const;
|
||||
const vec2f& position() const;
|
||||
const vec2f& size() const;
|
||||
|
||||
void set_position (const vec2i& parPos);
|
||||
void set_position (const vec2f& parPos);
|
||||
|
||||
iterator begin();
|
||||
iterator end();
|
||||
|
||||
private:
|
||||
vec2i m_position;
|
||||
vec2i m_size;
|
||||
vec2f m_position;
|
||||
vec2f m_size;
|
||||
WorldGrid* m_world;
|
||||
};
|
||||
} //namespace curry
|
||||
|
|
Loading…
Add table
Reference in a new issue