diff --git a/Font.h b/Font.h index 094915c..3442966 100644 --- a/Font.h +++ b/Font.h @@ -54,7 +54,7 @@ namespace gj { for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { - *dst = *src*1./UINT8_MAX; + *dst = static_cast(*src*1./UINT8_MAX); ++dst, ++src; } } diff --git a/Game.h b/Game.h index 37fd894..c6ebe8b 100644 --- a/Game.h +++ b/Game.h @@ -1,10 +1,12 @@ #pragma once +#include #include #include "iDrawable.h" #include "iWritable.h" #include "Logger.h" +#include "Texture.h" namespace gj { @@ -19,25 +21,45 @@ class Game : public iDrawable, public iWritable { Game& operator=(Game&&) = delete; Game& operator=(const Game&) = delete; - Game(uint32_t w, uint32_t h) : w_(w), h_(h), logger_(h) { - logger_.Print(L"こんにちは"); + Game(iAllocator* alloc, uint32_t w, uint32_t h) : + alloc_(alloc), 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; + test_ = Texture(std::move(temp)); } void Update() { static int i = 0; ++i; - logger_.Print(L"すべての人類は死滅する: "+std::to_wstring(i)); + if (i%100 == 0) logger_.Print(L"すべての人類は死滅する: "+std::to_wstring(i)); + + double t = i%200/200.; + t = 1 - t; + + mat3 m = mat3{ {.2, 0, 0},{0, .2, 0},{0, 0, 1} }; + + const double s = sin(3.14*2*t*t*t), c = cos(3.14*2*t*t*t); + m = ::linalg::mul(mat3{{c, -s, 0,}, {s, c, 0}, {0, 0, 1}}, m); + m = ::linalg::mul(mat3{{ 1, 0, 0,}, {0, 1, 0}, {(1 - t * t * t * 2) * .6, 0, 1}}, m); + m = ::linalg::mul(mat3{ {1, 0, 0},{0, 16/9., 0},{0, 0, 1} }, m); + test_.SetMatrix(m); } void Draw(Colorbuffer& fb) const override { + test_.Draw(fb); } void Write(Textbuffer& fb) const override { logger_.Write(fb); } private: + iAllocator* alloc_; + uint32_t w_, h_; Logger logger_; + + Texture test_; }; diff --git a/GlyphsJuke.vcxproj b/GlyphsJuke.vcxproj index 3a8456b..627fec8 100644 --- a/GlyphsJuke.vcxproj +++ b/GlyphsJuke.vcxproj @@ -158,6 +158,7 @@ + diff --git a/GlyphsJuke.vcxproj.filters b/GlyphsJuke.vcxproj.filters index 0a4d344..f74e228 100644 --- a/GlyphsJuke.vcxproj.filters +++ b/GlyphsJuke.vcxproj.filters @@ -77,5 +77,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/Logger.h b/Logger.h index fd904a6..ef6fcd1 100644 --- a/Logger.h +++ b/Logger.h @@ -34,7 +34,7 @@ class Logger : public iWritable, public iLogger { lines_.erase(lines_.begin()); --n; for (size_t i = 0; i < n; ++i) { - lines_[i].SetPosition(0, i); + lines_[i].SetPosition(0, static_cast(i)); } } diff --git a/Texture.h b/Texture.h new file mode 100644 index 0000000..0174332 --- /dev/null +++ b/Texture.h @@ -0,0 +1,117 @@ +#pragma once + +#include + +#include "thirdparty/linalg.h" + +#include "iDrawable.h" +#include "Rasterbuffer.h" + + +namespace gj { + + +class Texture : public DrawableBase { + public: + Texture() = delete; + Texture(Texture&&) = default; + Texture(const Texture&) = default; + + Texture& operator=(Texture&&) = default; + Texture& operator=(const Texture&) = default; + + Texture(Colorbuffer&& src) : src_(std::move(src)) { + } + + void Draw(Colorbuffer& fb) const override { + const int32_t w = static_cast(fb.width()); + const int32_t h = static_cast(fb.height()); + + const int32_t srcw = static_cast(src_.width()); + const int32_t srch = static_cast(src_.height()); + + vec3 p[4] = { + { -1, 1, 1 }, + { -1, -1, 1 }, + { 1, -1, 1 }, + { 1, 1, 1 }, + }; + for (size_t i = 0; i < 4; ++i) { + p[i] = ::linalg::mul(mat_, p[i]); + } + const double pl = std::min({p[0].x, p[1].x, p[2].x, p[3].x})-.1; + const double pr = std::max({p[0].x, p[1].x, p[2].x, p[3].x})+.1; + const double pu = std::max({p[0].y, p[1].y, p[2].y, p[3].y})+.1; + const double pb = std::min({p[0].y, p[1].y, p[2].y, p[3].y})-.1; + const double pw = pr - pl; + const double ph = pu - pb; + + const int32_t pli = static_cast((pl + 1) / 2 * w); + const int32_t pri = static_cast((pr + 1) / 2 * w); + const int32_t pui = static_cast((pu + 1) / 2 * h); + const int32_t pbi = static_cast((pb + 1) / 2 * h); + const int32_t pwi = pri - pli; + const int32_t phi = pui - pbi; + + vec3 q[4] = { + { pl, pu, 1 }, + { pl, pb, 1 }, + { pr, pb, 1 }, + { pr, pu, 1 }, + }; + for (size_t i = 0; i < 4; ++i) { + q[i] = ::linalg::mul(invmat_, q[i]); + } + const double ql = std::min({q[0].x, q[1].x, q[2].x, q[3].x}); + const double qr = std::max({q[0].x, q[1].x, q[2].x, q[3].x}); + const double qu = std::max({q[0].y, q[1].y, q[2].y, q[3].y}); + const double qb = std::min({q[0].y, q[1].y, q[2].y, q[3].y}); + + const double qldx = q[0].x - q[1].x; + const double qrdx = q[3].x - q[2].x; + const double qldy = q[0].y - q[1].y; + const double qrdy = q[3].y - q[2].y; + + const float* src = src_.ptr(); + float* dst = fb.ptr(); + for (int32_t y = 0; y <= phi; ++y) { + const double yfr = y*1. / phi; + + const double lxf = qldx * yfr + q[1].x; + const double rxf = qrdx * yfr + q[2].x; + const double ax = (rxf - lxf) / pwi; + + const double lyf = qldy * yfr + q[1].y; + const double ryf = qrdy * yfr + q[2].y; + const double ay = (ryf - lyf) / pwi; + + for (int32_t x = 0; x <= pwi; ++x) { + const double xf = lxf + ax*x; + const double yf = lyf + ay*x; + if (std::abs(xf) > 1 || std::abs(yf) > 1) continue; + + int32_t srcx = static_cast((xf+1)/2 * srcw); + int32_t srcy = srch - 1 - static_cast((yf+1)/2 * srch); + if (srcx >= srcw) srcx = srcw - 1; + if (srcy >= srch) srcy = srch - 1; + + const int32_t dstx = pli + x; + const int32_t dsty = (h-pui) + y; + if (dstx < 0 || w <= dstx) continue; + if (dsty < 0 || h <= dsty) continue; + + dst[dstx + w*dsty] = src[srcx + srcw*srcy]; + } + } + } + + void SetSource(Colorbuffer&& src) { + src_ = std::move(src); + } + + private: + Colorbuffer src_; +}; + + +} \ No newline at end of file diff --git a/Win32Console.h b/Win32Console.h index 41cfcb7..704aa9f 100644 --- a/Win32Console.h +++ b/Win32Console.h @@ -53,7 +53,7 @@ class Win32Console : public iConsole { SetConsoleScreenBufferInfoEx(screen_, &size); ShowWindow(win_, FALSE); - SetWindowLong(win_, GWL_STYLE, GetWindowLong(win_, GWL_STYLE) & ~WS_SIZEBOX); + SetWindowLong(win_, GWL_STYLE, GetWindowLong(win_, GWL_STYLE) & ~(WS_SIZEBOX | WS_MAXIMIZEBOX)); } ~Win32Console() { alive_.store(false); diff --git a/iAllocator.h b/iAllocator.h index 6413b8d..494a230 100644 --- a/iAllocator.h +++ b/iAllocator.h @@ -34,11 +34,11 @@ public: template using UniquePtr = std::unique_ptr>; - iAllocator(iAllocator&&) = delete; - iAllocator(const iAllocator&) = delete; + iAllocator(iAllocator&&) = default; + iAllocator(const iAllocator&) = default; - iAllocator& operator=(iAllocator&&) = delete; - iAllocator& operator=(const iAllocator&) = delete; + iAllocator& operator=(iAllocator&&) = default; + iAllocator& operator=(const iAllocator&) = default; iAllocator() = default; diff --git a/iDrawable.h b/iDrawable.h index fc81d52..ab15fed 100644 --- a/iDrawable.h +++ b/iDrawable.h @@ -8,8 +8,8 @@ namespace gj { -using mat4 = ::linalg::mat; -using vec4 = ::linalg::vec; +using mat3 = ::linalg::mat; +using vec3 = ::linalg::vec; class iDrawable { public: @@ -36,15 +36,17 @@ class DrawableBase : public iDrawable { DrawableBase& operator=(DrawableBase&&) = default; DrawableBase& operator=(const DrawableBase&) = default; - void setMatrix(const mat4& m) { - setMatrix(mat4(m)); + void SetMatrix(const mat3& m) { + SetMatrix(mat3(m)); } - void setMatrix(mat4&& m) { - mat_ = std::move(m); + void SetMatrix(mat3&& m) { + mat_ = std::move(m); + invmat_ = ::linalg::inverse(mat_); } protected: - mat4 mat_ = ::linalg::identity; + mat3 mat_ = ::linalg::identity; + mat3 invmat_ = ::linalg::identity; }; diff --git a/main.cc b/main.cc index 3b59ed4..8f2e391 100644 --- a/main.cc +++ b/main.cc @@ -24,8 +24,7 @@ int main() { gj::Win32Console console(&alloc, kWidth, kHeight); console.Show(); - gj::Game game(kWidth, kHeight); - + gj::Game game(&alloc, kWidth, kHeight); while (true) { game.Update(); { @@ -40,6 +39,6 @@ int main() { game.Write(fb); console.SwapTextbuffer(); } - Sleep(100); + Sleep(10); } } \ No newline at end of file