From 1518e429b4a9325851bc87acbfd41485a737e6c7 Mon Sep 17 00:00:00 2001 From: falsycat Date: Tue, 24 Aug 2021 13:13:04 +0900 Subject: [PATCH] Adds iClock interface and its implementations. --- Game.h | 13 ++++++++----- GlyphsJuke.vcxproj | 3 +++ GlyphsJuke.vcxproj.filters | 9 +++++++++ OffsetClock.h | 38 ++++++++++++++++++++++++++++++++++++++ SystemClock.h | 35 +++++++++++++++++++++++++++++++++++ iClock.h | 25 +++++++++++++++++++++++++ 6 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 OffsetClock.h create mode 100644 SystemClock.h create mode 100644 iClock.h diff --git a/Game.h b/Game.h index c6ebe8b..a44c41b 100644 --- a/Game.h +++ b/Game.h @@ -6,6 +6,8 @@ #include "iDrawable.h" #include "iWritable.h" #include "Logger.h" +#include "OffsetClock.h" +#include "SystemClock.h" #include "Texture.h" @@ -22,7 +24,8 @@ class Game : public iDrawable, public iWritable { Game& operator=(const Game&) = delete; Game(iAllocator* alloc, uint32_t w, uint32_t h) : - alloc_(alloc), w_(w), h_(h), logger_(h), test_(Colorbuffer(alloc, 1, 1)) { + alloc_(alloc), uptime_(&SystemClock::instance()), + w_(w), h_(h), logger_(h), test_(Colorbuffer(alloc, 1, 1)) { Colorbuffer temp(alloc_, 5, 5); float* ptr = temp.ptr(); for (size_t i = 0; i < 25; ++i) ptr[i] = i%2*.3+.7; @@ -30,11 +33,10 @@ class Game : public iDrawable, public iWritable { } void Update() { - static int i = 0; - ++i; - if (i%100 == 0) logger_.Print(L"すべての人類は死滅する: "+std::to_wstring(i)); + const uint64_t i = uptime_.now(); + if (i%1000 == 0) logger_.Print(L"すべての人類は死滅する: "+std::to_wstring(i)); - double t = i%200/200.; + double t = i%2000/2000.; t = 1 - t; mat3 m = mat3{ {.2, 0, 0},{0, .2, 0},{0, 0, 1} }; @@ -55,6 +57,7 @@ class Game : public iDrawable, public iWritable { private: iAllocator* alloc_; + OffsetClock uptime_; uint32_t w_, h_; Logger logger_; diff --git a/GlyphsJuke.vcxproj b/GlyphsJuke.vcxproj index 627fec8..1b66d8c 100644 --- a/GlyphsJuke.vcxproj +++ b/GlyphsJuke.vcxproj @@ -150,13 +150,16 @@ + + + diff --git a/GlyphsJuke.vcxproj.filters b/GlyphsJuke.vcxproj.filters index f74e228..ec2bd0b 100644 --- a/GlyphsJuke.vcxproj.filters +++ b/GlyphsJuke.vcxproj.filters @@ -80,5 +80,14 @@ Header Files + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file diff --git a/OffsetClock.h b/OffsetClock.h new file mode 100644 index 0000000..794870b --- /dev/null +++ b/OffsetClock.h @@ -0,0 +1,38 @@ +#pragma once + +#include + +#define NOMINMAX +#include +#undef NOMINMAX + +#include "iClock.h" + +namespace gj { + + +class OffsetClock : public iClock { + public: + OffsetClock(OffsetClock&&) = default; + OffsetClock(const OffsetClock&) = default; + + OffsetClock& operator=(OffsetClock&&) = default; + OffsetClock& operator=(const OffsetClock&) = default; + + OffsetClock(const iClock* parent, uint64_t offset) : + parent_(parent), offset_(offset) { + } + OffsetClock(const iClock* parent) : OffsetClock(parent, parent->now()) { + } + + uint64_t now() const override { + return parent_->now() - offset_; + } + + private: + const iClock* parent_; + uint64_t offset_; +}; + + +} \ No newline at end of file diff --git a/SystemClock.h b/SystemClock.h new file mode 100644 index 0000000..46a51ff --- /dev/null +++ b/SystemClock.h @@ -0,0 +1,35 @@ +#pragma once + +#include + +#define NOMINMAX +#include +#undef NOMINMAX + +#include "iClock.h" + + +namespace gj { + + +class SystemClock : public iClock { + public: + static const SystemClock& instance() { + static const SystemClock instance_; + return instance_; + } + + SystemClock() = default; + SystemClock(SystemClock&&) = default; + SystemClock(const SystemClock&) = default; + + SystemClock& operator=(SystemClock&&) = default; + SystemClock& operator=(const SystemClock&) = default; + + uint64_t now() const override { + return GetTickCount64(); + } +}; + + +} \ No newline at end of file diff --git a/iClock.h b/iClock.h new file mode 100644 index 0000000..8ee1cc7 --- /dev/null +++ b/iClock.h @@ -0,0 +1,25 @@ +#pragma once + +#include + + +namespace gj { + + +class iClock { + public: + iClock() = default; + iClock(iClock&&) = default; + iClock(const iClock&) = default; + + iClock& operator=(iClock&&) = default; + iClock& operator=(const iClock&) = default; + + virtual ~iClock() = default; + + virtual uint64_t now() const = 0; +}; + + + +} \ No newline at end of file