From 88482bb52fee2fb1f1ae4fb1ddbc35de362fefaf Mon Sep 17 00:00:00 2001 From: David Ludwig Date: Tue, 18 Sep 2018 14:04:29 -0400 Subject: [PATCH 1/4] bug-fix: 'sprites' sample-app doesn't find input files, if launched via command-line and from a directory other than the one the executable is in --- P0267_RefImpl/Samples/sprites/main.cpp | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/P0267_RefImpl/Samples/sprites/main.cpp b/P0267_RefImpl/Samples/sprites/main.cpp index bde05eb..6033027 100644 --- a/P0267_RefImpl/Samples/sprites/main.cpp +++ b/P0267_RefImpl/Samples/sprites/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include using namespace std; using namespace std::chrono; @@ -150,18 +151,15 @@ Cat SpawnCat( display_point output_size ) return cat; } -int main(int /*argc*/, char *argv[]) { - auto image = [&]{ - try { - return image_surface{"cat.jpg", image_file_format::jpeg, format::argb32}; - } - catch(...) { - // We're on some weird system like iOS. To avoid bringing Cocoa stuff here, lets instead use a small hack: - auto path = string{argv[0]}; - path = path.substr(0, path.length() - "sprites"s.length()) + "cat.jpg"; - return image_surface{path, image_file_format::jpeg, format::argb32}; - } - }(); +int main(int argc, char *argv[]) { + if (argc >= 1) { + // Set the running app's Current Working Directory to that which + // contains the executable. This is rather than, for example, the + // directory from which the app was launched from. + std::filesystem::current_path(std::filesystem::path(argv[0]).remove_filename()); + } + + auto image = image_surface{"cat.jpg", image_file_format::jpeg, format::argb32}; const auto image_size = image.dimensions(); const auto cat_brush = brush{move(image)}; auto display = output_surface{500, 500, format::argb32, scaling::none}; From f856f5ade5355cedf2ee8d8d453b91a8cd267e57 Mon Sep 17 00:00:00 2001 From: David Ludwig Date: Tue, 18 Sep 2018 14:18:00 -0400 Subject: [PATCH 2/4] bug-fix: 'draw_cpp' sample-app wasn't finding source-image, if launched from a directory other than that which contains the sample's executable --- P0267_RefImpl/Samples/draw_cpp/main.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/P0267_RefImpl/Samples/draw_cpp/main.cpp b/P0267_RefImpl/Samples/draw_cpp/main.cpp index d8f76a2..8f4783e 100644 --- a/P0267_RefImpl/Samples/draw_cpp/main.cpp +++ b/P0267_RefImpl/Samples/draw_cpp/main.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -5,12 +6,14 @@ using namespace std; using namespace std::experimental; using namespace std::experimental::io2d; +std::filesystem::path input_data_dir; // Where to load input files/data from; set on app launch + void DrawCPP() { auto img = image_surface{format::argb32, 300, 200}; img.clear(); - auto surface_brush = brush{ image_surface{"texture.jpg", image_file_format::jpeg, format::argb32 } }; + auto surface_brush = brush{ image_surface{(input_data_dir / "texture.jpg").string().c_str(), image_file_format::jpeg, format::argb32 } }; auto shadow_brush = brush{ rgba_color(0.5, 0.5, 0.5, 0.6) }; auto shadow_render = render_props{}; shadow_render.surface_matrix(matrix_2d::create_translate({ -2.0, -2.0f })); @@ -41,11 +44,13 @@ void DrawCPP() pb.insert(pb.begin(), figure_items::rel_matrix(matrix_2d::create_translate({ 80.0f, 0.0f }))); img.fill(shadow_brush, pb); img.fill(surface_brush, pb, nullopt, shadow_render); - img.save("cpp.png", image_file_format::png); } -int main(int /*argc*/, const char** /*argv*/) { - DrawCPP(); +int main(int argc, const char** argv) { + if (argc >= 1) { + input_data_dir = std::filesystem::path(argv[0]).remove_filename(); + } + DrawCPP(); return 0; } From fcb213068cd3a276031c734bf2caa2c2345fd817 Mon Sep 17 00:00:00 2001 From: David Ludwig Date: Wed, 19 Sep 2018 09:53:32 -0400 Subject: [PATCH 3/4] draw_cpp: default to showing output in a window; make file-based output be optional, via cmd-line args --- P0267_RefImpl/Samples/draw_cpp/main.cpp | 36 ++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/P0267_RefImpl/Samples/draw_cpp/main.cpp b/P0267_RefImpl/Samples/draw_cpp/main.cpp index 8f4783e..a0572b6 100644 --- a/P0267_RefImpl/Samples/draw_cpp/main.cpp +++ b/P0267_RefImpl/Samples/draw_cpp/main.cpp @@ -8,9 +8,9 @@ using namespace std::experimental::io2d; std::filesystem::path input_data_dir; // Where to load input files/data from; set on app launch -void DrawCPP() +template +void DrawCPP(Surface & img) { - auto img = image_surface{format::argb32, 300, 200}; img.clear(); auto surface_brush = brush{ image_surface{(input_data_dir / "texture.jpg").string().c_str(), image_file_format::jpeg, format::argb32 } }; @@ -44,13 +44,41 @@ void DrawCPP() pb.insert(pb.begin(), figure_items::rel_matrix(matrix_2d::create_translate({ 80.0f, 0.0f }))); img.fill(shadow_brush, pb); img.fill(surface_brush, pb, nullopt, shadow_render); - img.save("cpp.png", image_file_format::png); } int main(int argc, const char** argv) { if (argc >= 1) { input_data_dir = std::filesystem::path(argv[0]).remove_filename(); } - DrawCPP(); + std::filesystem::path output_file; + if (argc >= 2) { + if (argv[1][0] == '-') { + std::cerr + << "Usage: " << std::filesystem::path(argv[0]).filename().u8string() << " [output png file]\n" + << "\n" + << " If the 'output png file' is left unspecified, then the output will be\n" + << " displayed in a window.\n" + << "\n"; + return 1; + } else { + output_file = argv[1]; + } + } + + if (!output_file.empty()) { + auto img = image_surface{format::argb32, 300, 200}; + DrawCPP(img); + img.save(output_file.u8string(), image_file_format::png); + } else { + auto img = output_surface{300, 200, format::argb32, scaling::none, refresh_style::as_needed, 30.f}; + img.size_change_callback([&](output_surface &surface) { + surface.dimensions(surface.display_dimensions()); + }); + img.draw_callback([&](auto &surface) { + DrawCPP(surface); + }); + img.begin_show(); + } + return 0; } From 1b17705efd4099c3353464e23be216e924689cc3 Mon Sep 17 00:00:00 2001 From: David Ludwig Date: Sat, 29 Sep 2018 16:12:11 -0700 Subject: [PATCH 4/4] samples: use boost::filesystem if std::filesystem is unavailable Apple's latest Xcode (v.10.0) does not include std::filesystem support, despite having language-level support for C++17. This change helps permit MacOS compilation via a stock copy of Xcode. --- P0267_RefImpl/Samples/draw_cpp/CMakeLists.txt | 9 ++++++++ P0267_RefImpl/Samples/draw_cpp/main.cpp | 21 +++++++++++++------ P0267_RefImpl/Samples/sprites/CMakeLists.txt | 9 ++++++++ P0267_RefImpl/Samples/sprites/main.cpp | 13 ++++++++++-- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/P0267_RefImpl/Samples/draw_cpp/CMakeLists.txt b/P0267_RefImpl/Samples/draw_cpp/CMakeLists.txt index 84cbd99..480877b 100644 --- a/P0267_RefImpl/Samples/draw_cpp/CMakeLists.txt +++ b/P0267_RefImpl/Samples/draw_cpp/CMakeLists.txt @@ -10,6 +10,15 @@ add_executable(${PROJECT_ID} main.cpp) target_link_libraries(${PROJECT_ID} io2d) +# If std::filesystem is not available, use boost::filesystem +include(CheckIncludeFileCXX) +CHECK_INCLUDE_FILE_CXX(filesystem STD_FILESYSTEM_INCLUDED) +if (NOT STD_FILESYSTEM_INCLUDED) + find_package(Boost REQUIRED COMPONENTS system filesystem) + target_include_directories(${PROJECT_ID} PRIVATE ${Boost_INCLUDE_DIRS}) + target_link_libraries(${PROJECT_ID} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY}) +endif() + set(RSC_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/texture.jpg) set(RSC_TARGET $/texture.jpg) diff --git a/P0267_RefImpl/Samples/draw_cpp/main.cpp b/P0267_RefImpl/Samples/draw_cpp/main.cpp index a0572b6..9da3212 100644 --- a/P0267_RefImpl/Samples/draw_cpp/main.cpp +++ b/P0267_RefImpl/Samples/draw_cpp/main.cpp @@ -1,12 +1,21 @@ -#include #include #include +#if __has_include() + #include + namespace fs = std::filesystem; +#elif __has_include() + #include + namespace fs = boost::filesystem; +#else + #error "Cannot find either std::filesystem or boost::filesystem" +#endif + using namespace std; using namespace std::experimental; using namespace std::experimental::io2d; -std::filesystem::path input_data_dir; // Where to load input files/data from; set on app launch +fs::path input_data_dir; // Where to load input files/data from; set on app launch template void DrawCPP(Surface & img) @@ -48,13 +57,13 @@ void DrawCPP(Surface & img) int main(int argc, const char** argv) { if (argc >= 1) { - input_data_dir = std::filesystem::path(argv[0]).remove_filename(); + input_data_dir = fs::path(argv[0]).remove_filename(); } - std::filesystem::path output_file; + fs::path output_file; if (argc >= 2) { if (argv[1][0] == '-') { std::cerr - << "Usage: " << std::filesystem::path(argv[0]).filename().u8string() << " [output png file]\n" + << "Usage: " << fs::path(argv[0]).filename().string() << " [output png file]\n" << "\n" << " If the 'output png file' is left unspecified, then the output will be\n" << " displayed in a window.\n" @@ -68,7 +77,7 @@ int main(int argc, const char** argv) { if (!output_file.empty()) { auto img = image_surface{format::argb32, 300, 200}; DrawCPP(img); - img.save(output_file.u8string(), image_file_format::png); + img.save(output_file.string(), image_file_format::png); } else { auto img = output_surface{300, 200, format::argb32, scaling::none, refresh_style::as_needed, 30.f}; img.size_change_callback([&](output_surface &surface) { diff --git a/P0267_RefImpl/Samples/sprites/CMakeLists.txt b/P0267_RefImpl/Samples/sprites/CMakeLists.txt index ea4bffa..6fb798c 100644 --- a/P0267_RefImpl/Samples/sprites/CMakeLists.txt +++ b/P0267_RefImpl/Samples/sprites/CMakeLists.txt @@ -9,6 +9,15 @@ project(${PROJECT_ID}) add_executable(${PROJECT_ID} main.cpp) target_link_libraries(${PROJECT_ID} io2d) +# If std::filesystem is not available, use boost::filesystem +include(CheckIncludeFileCXX) +CHECK_INCLUDE_FILE_CXX(filesystem STD_FILESYSTEM_INCLUDED) +if (NOT STD_FILESYSTEM_INCLUDED) + find_package(Boost REQUIRED COMPONENTS system filesystem) + target_include_directories(${PROJECT_ID} PRIVATE ${Boost_INCLUDE_DIRS}) + target_link_libraries(${PROJECT_ID} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY}) +endif() + add_custom_command( TARGET ${PROJECT_ID} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/cat.jpg" "$/cat.jpg" ) diff --git a/P0267_RefImpl/Samples/sprites/main.cpp b/P0267_RefImpl/Samples/sprites/main.cpp index 6033027..1fbc080 100644 --- a/P0267_RefImpl/Samples/sprites/main.cpp +++ b/P0267_RefImpl/Samples/sprites/main.cpp @@ -1,7 +1,16 @@ #include #include #include -#include + +#if __has_include() + #include + namespace fs = std::filesystem; +#elif __has_include() + #include + namespace fs = boost::filesystem; +#else + #error "Cannot find either std::filesystem or boost::filesystem" +#endif using namespace std; using namespace std::chrono; @@ -156,7 +165,7 @@ int main(int argc, char *argv[]) { // Set the running app's Current Working Directory to that which // contains the executable. This is rather than, for example, the // directory from which the app was launched from. - std::filesystem::current_path(std::filesystem::path(argv[0]).remove_filename()); + fs::current_path(fs::path(argv[0]).remove_filename()); } auto image = image_surface{"cat.jpg", image_file_format::jpeg, format::argb32};