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
133 lines (111 loc) · 3.62 KB
/
Main.cpp
File metadata and controls
133 lines (111 loc) · 3.62 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
#include <imgui.h>
#include <Tkge/Engine.hpp>
#include <Tkge/Graphics/Shader.hpp>
#include <klib/assert.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"}; }
static constexpr auto Vertices = std::array{
Tkge::Graphics::Vertex{.position = {-200.0f, -200.0f}},
Tkge::Graphics::Vertex{.position = {200.0f, -200.0f}},
Tkge::Graphics::Vertex{.position = {200.0f, 200.0f}},
Tkge::Graphics::Vertex{.position = {-200.0f, 200.0f}},
};
static constexpr auto Indices = std::array{
0u, 1u, 2u, 2u, 3u, 0u,
};
static constexpr auto Primitive = Tkge::Graphics::Primitive{
.vertices = Vertices,
.indices = Indices,
};
auto instances = std::array<Tkge::Graphics::RenderInstance, 2>{};
instances[0].transform.position.x = -250.0f;
instances[0].tint = kvf::cyan_v;
instances[1].transform.position.x = 250.0f;
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;
instances[0].tint.w = kvf::Color::to_u8((0.5f * std::sin(elapsed.count())) + 0.5f);
instances[1].tint.w = kvf::Color::to_u8((0.5f * std::sin(-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);
renderer.Draw(Primitive, instances);
}
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;
}
}