Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "engine/native/thirdparty/bgfx"]
path = engine/native/thirdparty/bgfx
url = https://github.com/bkaradzic/bgfx
[submodule "engine/native/thirdparty/sdl"]
path = engine/native/thirdparty/sdl
url = https://github.com/libsdl-org/SDL
44 changes: 43 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,55 @@ endif()

project(DraconicEngine LANGUAGES C CXX)

# Ensure that everyone is on the same page hardware wise or else we get errors
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_compile_options(-mavx2 -mfma)
endif()
Comment thread
coderabbitai[bot] marked this conversation as resolved.

# Only have safe global rules here
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_MODULE_STD ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON) # bgfx & deps give error if this isn't set, this is a temp workaround
# Force all to use Clang or else a mismatch occurs
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -lc++abi")
Comment thread
AR-DEV-1 marked this conversation as resolved.

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")

include(CTest)

add_subdirectory(engine/native)
add_subdirectory(engine/native)

find_package(PkgConfig REQUIRED)
pkg_check_modules(X11_LIBS REQUIRED IMPORTED_TARGET x11 xext xcursor xrandr xrender xi xfixes)

add_executable(draconic engine/native/main/main.cpp)
enable_modules(draconic)
target_link_libraries(draconic
PRIVATE
rhi
core
io
image_loader
bgfx
bx
bimg
-Wl,--whole-archive SDL3::SDL3-static -Wl,--no-whole-archive
c++
c++abi
PkgConfig::X11_LIBS
dl
pthread
m
)

# Shader copying
set(ENGINE_SHADER_DIR "${CMAKE_SOURCE_DIR}/engine/native/rendering/shaders")
add_custom_command(
TARGET draconic POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${ENGINE_SHADER_DIR}"
"$<TARGET_FILE_DIR:draconic>"
COMMENT "Copying shaders to build directory..."
)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
1 change: 0 additions & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"CMAKE_CXX_STANDARD": "23",
"CMAKE_CXX_STANDARD_REQUIRED": "ON",
"CMAKE_CXX_EXTENSIONS": "OFF",
"CMAKE_CXX_MODULE_STD": "1",
"CMAKE_CXX_FLAGS_INIT": "-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0"
}
},
Expand Down
6 changes: 0 additions & 6 deletions cmake/Compiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ else()
add_compile_definitions(DEBUG)
endif()

# Force Clang to use libc++
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-stdlib=libc++)
add_link_options(-stdlib=libc++)
endif()

if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
if (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64")
# TODO: Make SIMD level configurable or detect at runtime
Expand Down
4 changes: 3 additions & 1 deletion engine/native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ include(Modules)
add_subdirectory(thirdparty)

add_modules_library(core SHARED)
target_link_libraries(core PUBLIC definitions math)
target_link_libraries(core PUBLIC definitions math io)

add_subdirectory(rendering)
23 changes: 22 additions & 1 deletion engine/native/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
add_modules_library(definitions)
add_modules_library(math)
target_link_libraries(math PUBLIC definitions)
add_modules_library(io)

target_link_libraries(math PUBLIC definitions)
target_link_libraries(io PUBLIC definitions stb_image)

add_library(image_loader SHARED)

target_sources(image_loader
PUBLIC
FILE_SET CXX_MODULES FILES
io/image_loader.cppm
)

target_sources(image_loader
PRIVATE
io/image_loader.cpp
)

target_link_libraries(image_loader
PRIVATE
stb_image
)
36 changes: 36 additions & 0 deletions engine/native/core/io/filesystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module;

import std;

module core.io.filesystem;

namespace draco::core::io::filesystem
{
std::vector<std::uint8_t> load_binary(const std::string& path)
{
// Open at the end (ate) to get size and in binary mode
std::ifstream file(path, std::ios::binary | std::ios::ate);

if (!file.is_open()) {
std::println("Error: Could not open file at: {}", path);
// Return an empty vector
return {};
}

std::streamsize size = file.tellg();
if (size <= 0) {
std::println("Error: File is empty or unreadable: {}", path);
return {};
}

file.seekg(0, std::ios::beg);

std::vector<std::uint8_t> buffer(static_cast<std::size_t>(size));
if (file.read(reinterpret_cast<char*>(buffer.data()), size)) {
return buffer;
}

std::println("Error: Failed to read file contents: {}", path);
return {};
Comment thread
AR-DEV-1 marked this conversation as resolved.
}
}
9 changes: 9 additions & 0 deletions engine/native/core/io/filesystem.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export module core.io.filesystem;

import std;

export namespace draco::core::io::filesystem
{
// Returns a buffer of the file data
std::vector<std::uint8_t> load_binary(const std::string& path);
}
46 changes: 46 additions & 0 deletions engine/native/core/io/image_loader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module;

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

#include <filesystem>

module core.io.image_loader;

// TODO: I'm too lazy to write code so we need somethin' better

namespace draco::core::io::image_loader
{
ImageData load_image(const std::filesystem::path& path)
{
ImageData result;

if (!std::filesystem::exists(path)) {
std::println("Error: Image path does not exist: {}", path.string());
return result;
}

int width, height, channels;
// STBI_rgb_alpha forces the output to be 4 bytes per pixel (RGBA)
unsigned char* data = stbi_load(path.string().c_str(), &width, &height, &channels, STBI_rgb_alpha);

if (!data) {
std::println("Error: Failed to decode image: {}", path.string());
return result;
}

// Calculate the total size: width * height * 4 (RGBA)
size_t size = static_cast<size_t>(width) * height * 4;

result.pixels.assign(data, data + size);
result.width = static_cast<uint16_t>(width);
result.height = static_cast<uint16_t>(height);
result.channels = 4;
result.is_valid = true;

// Free the memory allocated by stb
stbi_image_free(data);

return result;
}
}
23 changes: 23 additions & 0 deletions engine/native/core/io/image_loader.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module;

#include <cstdint>
#include <vector>

export module core.io.image_loader;

import std;

export namespace draco::core::io::image_loader
{
struct ImageData
{
std::vector<uint8_t> pixels;
uint16_t width = 0;
uint16_t height = 0;
uint8_t channels = 0;
bool is_valid = false;
};

// Load an image file (PNG, JPG, etc) & decode it to raw RGBA8
ImageData load_image(const std::filesystem::path& path);
}
Loading