This repository was archived by the owner on Mar 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
138 lines (116 loc) · 3.96 KB
/
Main.cpp
File metadata and controls
138 lines (116 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <imgui.h>
#include <Tkge/Engine.hpp>
#include <Tkge/Graphics/Drawable.hpp>
#include <Tkge/Graphics/Shader.hpp>
#include <klib/assert.hpp>
#include <kvf/color_bitmap.hpp>
#include <kvf/time.hpp>
#include <cmath>
#include <exception>
#include <print>
// temporary, until we have shader Assets.
#include <filesystem>
#include <fstream>
#include <vector>
namespace
{
namespace fs = std::filesystem;
fs::path Upfind(const fs::path& leafDir, const std::string_view pattern)
{
for (auto path = leafDir; path.has_parent_path() && path.parent_path() != path; path = path.parent_path())
{
auto ret = path / pattern;
if (fs::exists(ret)) { return fs::absolute(ret); }
}
return fs::current_path();
}
std::vector<std::uint32_t> LoadSpirV(const klib::CString path)
{
auto file = std::ifstream{path.c_str(), std::ios::binary | std::ios::ate};
if (!file) { return {}; }
const auto size = file.tellg();
if (std::size_t(size) % sizeof(std::uint32_t) != 0) { return {}; } // invalid SPIR-V.
file.seekg(std::ios::beg, {});
auto ret = std::vector<std::uint32_t>{};
ret.resize(std::size_t(size) / sizeof(std::uint32_t));
file.read(reinterpret_cast<char*>(ret.data()), size); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
return ret;
}
void Run(const fs::path& assets_path)
{
static constexpr Tkge::WindowSurface surface{.size = {1280, 720}};
Tkge::Engine engine{surface};
auto shader = Tkge::Graphics::Shader{};
const auto vertexSpirV = LoadSpirV((assets_path / "Shaders/Default.vert").string().c_str());
const auto fragmentSpirV = LoadSpirV((assets_path / "Shaders/Default.frag").string().c_str());
const auto& renderDevice = engine.RenderDevice();
if (!shader.Load(renderDevice.get_device(), vertexSpirV, fragmentSpirV)) { throw std::runtime_error{"Failed to load shaders"}; }
auto quad = Tkge::Graphics::Quad{};
quad.Create(glm::vec2{400.0f});
quad.transform.position.x = -250.0f;
auto colourBitmap = kvf::ColorBitmap{glm::ivec2{2, 2}};
colourBitmap[0, 0] = kvf::red_v;
colourBitmap[1, 0] = kvf::green_v;
colourBitmap[0, 1] = kvf::blue_v;
colourBitmap[1, 1] = kvf::magenta_v;
auto texture = Tkge::Graphics::Texture{&engine.RenderDevice()};
texture.Create(colourBitmap.bitmap());
texture.sampler.filter = vk::Filter::eNearest;
quad.texture = &texture;
auto instancedQuad = Tkge::Graphics::InstancedQuad{};
instancedQuad.Create(glm::vec2{150.0f});
instancedQuad.instances.resize(2);
instancedQuad.instances[0].transform.position = {250.0f, -100.0f};
instancedQuad.instances[0].tint = kvf::cyan_v;
instancedQuad.instances[1].transform.position = {250.0f, 100.0f};
instancedQuad.instances[1].tint = kvf::yellow_v;
auto wireframe = false;
auto lineWidth = 3.0f;
auto deltaTime = kvf::DeltaTime{};
auto elapsed = kvf::Seconds{};
while (engine.IsRunning())
{
engine.NextFrame();
const auto dt = deltaTime.tick();
elapsed += dt;
instancedQuad.instances[0].tint.w = kvf::Color::to_u8((0.5f * std::sin(elapsed.count())) + 0.5f);
instancedQuad.instances[1].tint.w = kvf::Color::to_u8((0.5f * std::sin(-elapsed.count())) + 0.5f);
quad.tint.w = kvf::Color::to_u8((0.5f * std::cos(elapsed.count())) + 0.5f);
if (ImGui::Begin("Misc"))
{
ImGui::Checkbox("wireframe", &wireframe);
ImGui::DragFloat("line width", &lineWidth, 1.0f, 1.0f, 100.0f);
}
ImGui::End();
if (auto renderer = engine.BeginRender())
{
renderer.BindShader(shader);
renderer.SetLineWidth(lineWidth);
renderer.SetWireframe(wireframe);
instancedQuad.Draw(renderer);
quad.Draw(renderer);
}
engine.Present();
}
renderDevice.get_device().waitIdle();
}
} // namespace
int main([[maybe_unused]] int argc, char** argv)
{
try
{
KLIB_ASSERT(argc > 0);
const auto assets_path = Upfind(*argv, "Assets");
Run(assets_path);
}
catch (const std::exception& e)
{
std::println(stderr, "PANIC: {}", e.what());
return EXIT_FAILURE;
}
catch (...)
{
std::println(stderr, "PANIC!");
return EXIT_FAILURE;
}
}