Skip to content

Commit 1928886

Browse files
committed
Clean up a bit
1 parent 37ac42b commit 1928886

12 files changed

Lines changed: 85 additions & 45 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ find_package(PkgConfig REQUIRED)
5050
pkg_check_modules(X11_LIBS REQUIRED x11 xext xcursor xrandr xrender xi xfixes)
5151

5252
add_executable(draconic engine/native/main/main.cpp)
53+
enable_modules(draconic)
5354
target_link_libraries(draconic
5455
PRIVATE
55-
core
5656
rhi
57+
core
5758
bgfx
5859
bx
5960
bimg

engine/native/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ include(Modules)
44
add_subdirectory(thirdparty)
55

66
add_modules_library(core SHARED)
7-
target_link_libraries(core PUBLIC definitions math)
7+
target_link_libraries(core PUBLIC definitions math filesystem)
88

99
add_subdirectory(rendering)

engine/native/core/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
add_modules_library(definitions)
22
add_modules_library(math)
3-
target_link_libraries(math PUBLIC definitions)
3+
add_modules_library(filesystem)
4+
target_link_libraries(math PUBLIC definitions)
5+
target_link_libraries(filesystem PUBLIC definitions)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module;
2+
3+
import std;
4+
5+
module core.filesystem;
6+
7+
namespace draco::filesystem
8+
{
9+
std::vector<std::uint8_t> load_binary(const std::string& path)
10+
{
11+
// Open at the end (ate) to get size and in binary mode
12+
std::ifstream file(path, std::ios::binary | std::ios::ate);
13+
14+
if (!file.is_open()) {
15+
std::println("Error: Could not find shader file at: {}", path);
16+
// Return an empty vector
17+
return {};
18+
}
19+
20+
std::streamsize size = file.tellg();
21+
if (size <= 0) {
22+
std::println("Error: Shader file is empty: {}", path);
23+
return {};
24+
}
25+
26+
file.seekg(0, std::ios::beg);
27+
28+
std::vector<std::uint8_t> buffer(static_cast<std::size_t>(size));
29+
if (file.read(reinterpret_cast<char*>(buffer.data()), size)) {
30+
return buffer;
31+
}
32+
33+
return {};
34+
}
35+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export module core.filesystem;
2+
3+
import std;
4+
5+
export namespace draco::filesystem
6+
{
7+
// Returns a buffer of the file data
8+
std::vector<std::uint8_t> load_binary(const std::string& path);
9+
}

engine/native/main/main.cpp

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,10 @@ import std;
44
#include <bgfx/bgfx.h>
55
#include <bx/math.h>
66

7-
import rendering.rhi;
8-
9-
// Vertex struct
10-
// Will later be moved to a seperate file & rewritten
11-
struct Vertex
12-
{
13-
float x, y, z; // Position
14-
uint32_t agbr; // Color, stored as AGBR
15-
};
16-
17-
// Shader binary loadin' logic
18-
// Will later be moved to a seperate file & rewritten
19-
std::vector<uint8_t> loadShaderBinary(const std::string& path);
20-
21-
std::vector<uint8_t> loadShaderBinary(const std::string& path) {
22-
// Open at the end (ate) to get size and in binary mode
23-
std::ifstream file(path, std::ios::binary | std::ios::ate);
24-
25-
if (!file.is_open()) {
26-
std::println("Error: Could not find shader file at: {}", path);
27-
// Return an empty vector
28-
return {};
29-
}
7+
import core.filesystem;
308

31-
std::streamsize size = file.tellg();
32-
if (size <= 0) {
33-
std::println("Error: Shader file is empty: {}", path);
34-
return {};
35-
}
36-
37-
file.seekg(0, std::ios::beg);
38-
39-
std::vector<uint8_t> buffer(static_cast<size_t>(size));
40-
if (file.read(reinterpret_cast<char*>(buffer.data()), size)) {
41-
return buffer;
42-
}
43-
44-
return {};
45-
}
9+
import rendering.rhi;
10+
import rendering.rhi.vertex;
4611

4712
int main(int argc, char* argv[])
4813
{
@@ -95,7 +60,7 @@ int main(int argc, char* argv[])
9560

9661
// Geometry data for a triangle to test rendering
9762
// It includes both positions & colors
98-
Vertex triangle[] = {
63+
draco::rhi::PosColorVertex triangle[] = {
9964
{ 0.0f, 0.5f, 0.0f, 0xff0000ff },
10065
{ 0.5f, -0.5f, 0.0f, 0xff00ff00 },
10166
{ -0.5f, -0.5f, 0.0f, 0xffff0000}
@@ -104,8 +69,8 @@ int main(int argc, char* argv[])
10469
auto vbh = draco::rhi::create_vertex_buffer(triangle, sizeof(triangle));
10570

10671
// Load the vertex & fragment shaders
107-
auto vs_data = loadShaderBinary("vs_triangle.bin");
108-
auto fs_data = loadShaderBinary("fs_triangle.bin");
72+
auto vs_data = draco::filesystem::load_binary("vs_triangle.bin");
73+
auto fs_data = draco::filesystem::load_binary("fs_triangle.bin");
10974

11075
// If the path is empty, return an error
11176
if (vs_data.empty() || fs_data.empty()) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
add_modules_library(rhi SHARED)
2+
target_sources(rhi PRIVATE rhi/rhi_bgfx.cpp rhi/vertex.cppm)
23
target_link_libraries(rhi PUBLIC core bgfx bx)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module;
2+
#include <bgfx/bgfx.h>
3+
4+
export module rendering.rhi.vertex;
5+
6+
import std;
7+
8+
9+
export namespace draco::rhi
10+
{
11+
struct PosColorVertex
12+
{
13+
float x, y, z; // Position
14+
uint32_t agbr; // Color, stored as AGBR
15+
16+
static void init() {
17+
layout
18+
.begin()
19+
.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
20+
.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
21+
.end();
22+
}
23+
24+
static inline bgfx::VertexLayout layout;
25+
};
26+
27+
}
File renamed without changes.

0 commit comments

Comments
 (0)