Skip to content

Commit b2893b8

Browse files
authored
Merge pull request #2 from cpp-gamedev/reset-clock
Add tick state machine and clock reset animation
2 parents cdfa4d2 + 8dcc116 commit b2893b8

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

src/main.cpp

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
#include <cassert>
22
#include <iostream>
3-
#include <context.hpp>
4-
#include <scene.hpp>
5-
63
#include <set>
4+
#include <context.hpp>
75
#include <delta_time.hpp>
86
#include <world_clock/gui.hpp>
97
#include <world_clock/io.hpp>
108

11-
/*
12-
- poll
13-
- update / tick (based on dt and new events)
14-
- render / draw
15-
*/
16-
179
using namespace misc;
1810

1911
namespace {
@@ -44,13 +36,50 @@ class input_t {
4436
std::set<key_t> m_held;
4537
};
4638

47-
void tick(world_clock_t& out_clock, input_t const& input, sec_t dt) noexcept {
48-
static constexpr f32 scale = 10.0f;
49-
f32 delta{};
50-
if (input.any_held(key_t::Down, key_t::Left, key_t::S, key_t::A)) { delta += -1.0f; }
51-
if (input.any_held(key_t::Up, key_t::Right, key_t::W, key_t::D)) { delta += 1.0f; }
52-
out_clock += scale * delta * dt.count();
53-
}
39+
class clock_ticker_t {
40+
public:
41+
clock_ticker_t(input_t const* input, world_clock_t* clock) noexcept : m_input(input), m_clock(clock) {}
42+
43+
void operator()(sec_t dt) noexcept {
44+
if (m_input->any_held(key_t::Enter, key_t::R)) { update(flag::zeroing); }
45+
static constexpr f32 translation_scale = 10.0f;
46+
if (test(flag::zeroing)) {
47+
static constexpr f32 zeroing_scale = 5.0f;
48+
hour_t const delta = zeroing_scale * translation_scale * dt.count();
49+
if (m_offset.hour() > delta) {
50+
add(-delta);
51+
} else {
52+
add(-m_offset.hour());
53+
update(flag::none, flag::zeroing);
54+
}
55+
} else {
56+
f32 delta{};
57+
if (m_input->any_held(key_t::Down, key_t::Left, key_t::S, key_t::A)) { delta += -1.0f; }
58+
if (m_input->any_held(key_t::Up, key_t::Right, key_t::W, key_t::D)) { delta += 1.0f; }
59+
add(translation_scale * delta * dt.count());
60+
}
61+
}
62+
63+
private:
64+
using flags_t = u8;
65+
enum flag : flags_t { none = 0, zeroing = 1 << 0 };
66+
67+
input_t const* m_input;
68+
world_clock_t* m_clock;
69+
world_hour_t m_offset;
70+
flags_t flags = flag::none;
71+
72+
bool test(flags_t f) const noexcept { return (flags & f) == f; }
73+
void update(flags_t set, flags_t unset = 0) noexcept {
74+
flags |= set;
75+
flags &= ~unset;
76+
}
77+
78+
void add(hour_t offset) noexcept {
79+
(*m_clock) += offset;
80+
m_offset += offset;
81+
}
82+
};
5483
} // namespace
5584

5685
int main() {
@@ -63,9 +92,10 @@ int main() {
6392
std::cout << clock << '\n';
6493
input_t input;
6594
delta_time_t dt;
95+
clock_ticker_t tick{&input, &clock};
6696
while (ctx.running()) {
6797
input.update(ctx.poll());
68-
tick(clock, input, ++dt);
98+
tick(++dt);
6999
if (auto drawer = ctx.drawer()) { world_clock_drawer_t{}(drawer, clock, {}); }
70100
}
71101
std::cout << "\nCompleted successfully\n";

src/types.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace misc {
55
using s32 = std::uint32_t;
6+
using u8 = std::uint8_t;
67
using u32 = std::uint32_t;
78
using f32 = float;
89
} // namespace misc

0 commit comments

Comments
 (0)