diff options
Diffstat (limited to 'src/engine.cpp')
| -rw-r--r-- | src/engine.cpp | 64 |
1 files changed, 50 insertions, 14 deletions
diff --git a/src/engine.cpp b/src/engine.cpp index 91f7447..a9991c4 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -21,13 +21,16 @@ #include <fstream> #include <map> #include <chrono> -#include <print> #include <cstdio> +#include <filesystem> #define GLFW_INCLUDE_VULKAN #include <GLFW/glfw3.h> #include <vulkan/vk_enum_string_helper.h> +#ifdef _WIN32 +# define STBI_WINDOWS_UTF8 +#endif #include <stb_image.h> #ifdef HAVE_NCURSES @@ -54,6 +57,7 @@ #include "renderer.hpp" #include "obj_parser.hpp" #include "vulkan_utils.hpp" +#include "path_utils.hpp" using engine::Renderer, @@ -190,7 +194,7 @@ static void render_software(Renderer<FrameBuffer, Shaders>& renderer, const Matr #ifdef HAVE_NCURSES #define MKEY_ESC 27 -static int main_term(Scene& scene) { +static int main_term(const std::filesystem::path& files_root, Scene& scene) { // init std::setlocale(LC_ALL, ""); initscr(); @@ -202,10 +206,22 @@ static int main_term(Scene& scene) { curs_set(0); int texture_w, texture_h, texture_channels; - stbi_uc* texture_pixels = stbi_load(DATADIR "/assets/textures/viking_room.png", &texture_w, &texture_h, &texture_channels, STBI_rgb_alpha); + const auto texture_path = std::filesystem::path(files_root).append(ASSETS_TEXTURES_DIR "/viking_room.png"); +#ifdef _WIN32 + const auto texture_path_wcstr = texture_path.c_str(); + const auto texture_path_cstr_size = stbi_convert_wchar_to_utf8(nullptr, 0, texture_path_wcstr); + const auto texture_path_cstr = new char[texture_path_cstr_size]; + stbi_convert_wchar_to_utf8(texture_path_cstr, texture_path_cstr_size, texture_path_wcstr); +#else + const auto texture_path_cstr = texture_path.c_str(); +#endif + stbi_uc* texture_pixels = stbi_load(texture_path_cstr, &texture_w, &texture_h, &texture_channels, STBI_rgb_alpha); +#ifdef _WIN32 + delete[] texture_path_cstr; +#endif if (!texture_pixels) { - std::println(stderr, "failed to load texture image, reason: {}", stbi_failure_reason()); + std::cerr << "failed to load texture image, reason: " << stbi_failure_reason() << std::endl; exit(EXIT_FAILURE); } @@ -755,7 +771,7 @@ static void generate_mipmaps(VkCommandBuffer cmd_buf, VkPhysicalDevice physical_ if ((format_props.formatProperties.optimalTilingFeatures & (VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT | VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT)) != (VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT | VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT)) { - std::println(stderr, "texture format {} doesn't support vkCmdBlitImage2", string_VkFormat(fmt)); + std::cerr << "texture format " << string_VkFormat(fmt) << " doesn't support vkCmdBlitImage2" << std::endl; exit(EXIT_FAILURE); } @@ -891,7 +907,7 @@ static void generate_mipmaps(VkCommandBuffer cmd_buf, VkPhysicalDevice physical_ } } -static int main_graphical(Scene& scene) { +static int main_graphical(const std::filesystem::path& files_root, Scene& scene) { // init window glfwInit(); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); @@ -1565,10 +1581,11 @@ static int main_graphical(Scene& scene) { // reading shader file auto shader_module = [&] { auto shader_code = [&] { - const char* shader_file_name = SHADERSDIR "/shader.spv"; + const auto shader_path = std::filesystem::path(files_root).append(SHADERS_DIR "/shader.spv"); + const auto shader_file_name = shader_path.c_str(); std::ifstream shader_file(shader_file_name, std::ios::ate | std::ios::binary); if (!shader_file.is_open()) { - std::cerr << "file `" << shader_file_name << "' not found" << std::endl; // TODO: improve + std::cerr << "file `" << shader_path << "' not found" << std::endl; // TODO: improve exit(EXIT_SUCCESS); } // shader code has to be 32-bits aligned, which is the case with the default allocator @@ -1815,7 +1832,19 @@ static int main_graphical(Scene& scene) { // create texture image auto [texture_img, texture_mip_lvls, texture_img_device_mem, texture_w, texture_h, texture_pixels] = [&] { int w, h, channels; - stbi_uc* pixels = stbi_load(DATADIR "/assets/textures/viking_room.png", &w, &h, &channels, STBI_rgb_alpha); + const auto texture_path = std::filesystem::path(files_root).append(ASSETS_TEXTURES_DIR "/viking_room.png"); +#ifdef _WIN32 + const auto texture_path_wcstr = texture_path.c_str(); + const auto texture_path_cstr_size = stbi_convert_wchar_to_utf8(nullptr, 0, texture_path_wcstr); + const auto texture_path_cstr = new char[texture_path_cstr_size]; + stbi_convert_wchar_to_utf8(texture_path_cstr, texture_path_cstr_size, texture_path_wcstr); +#else + const auto texture_path_cstr = texture_path.c_str(); +#endif + stbi_uc* pixels = stbi_load(texture_path_cstr, &w, &h, &channels, STBI_rgb_alpha); +#ifdef _WIN32 + delete[] texture_path_cstr; +#endif uint32_t texture_mip_lvls = engine::math::utils::log2_floored(static_cast<uint32_t>(std::max(w, h))) + 1; @@ -2863,6 +2892,13 @@ static void parse_args(const std::vector<std::string_view>& args, Mode& mode) { } int main(int argc, char *argv[]) { + const auto exe_path_opt = engine::path_utils::exe_path(); + if (!exe_path_opt) { + std::cerr << "cannot find path of executable" << std::endl; + exit(1); + } + const auto files_root = (*exe_path_opt).parent_path(); + Mode mode = Mode::graphical; parse_args(convert_args(argc, argv), mode); @@ -2884,7 +2920,7 @@ int main(int argc, char *argv[]) { case GameType::suzanne: return { { - engine::parse_object(DATADIR "/assets/suzanne.obj"), + engine::parse_object(std::filesystem::path(files_root).append(ASSETS_OBJS_DIR "/suzanne.obj")), { Vector3(0.f, 0.f, 0.f), Quaternion::one(), @@ -2903,7 +2939,7 @@ int main(int argc, char *argv[]) { } }, { - engine::parse_object(DATADIR "/assets/suzanne.obj"), + engine::parse_object(std::filesystem::path(files_root).append(ASSETS_OBJS_DIR "/suzanne.obj")), { Vector3(0.f, 1.f, 0.f), Quaternion::one(), @@ -2914,7 +2950,7 @@ int main(int argc, char *argv[]) { case GameType::test: return { { - engine::parse_object(DATADIR "/assets/viking_room.obj"), + engine::parse_object(std::filesystem::path(files_root).append(ASSETS_OBJS_DIR "/viking_room.obj")), { Vector3(0.f, .5f, 0.f), Quaternion::look_towards({ -1.f, 0.f, 0.f }, { 0.f, 0.f, 1.f }).conjugate(), @@ -2933,13 +2969,13 @@ int main(int argc, char *argv[]) { return EXIT_SUCCESS; case Mode::term: #ifdef HAVE_NCURSES - return main_term(scene); + return main_term(files_root, scene); #else std::cerr << "Error: ncurses was not enabled during compilation." << std::endl; return EXIT_FAILURE; #endif case Mode::graphical: - return main_graphical(scene); + return main_graphical(files_root, scene); default: std::unreachable(); } |
