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 d8f76a2..9da3212 100644 --- a/P0267_RefImpl/Samples/draw_cpp/main.cpp +++ b/P0267_RefImpl/Samples/draw_cpp/main.cpp @@ -1,16 +1,28 @@ #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; -void DrawCPP() +fs::path input_data_dir; // Where to load input files/data from; set on app launch + +template +void DrawCPP(Surface & img) { - 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 +53,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*/) { - DrawCPP(); +int main(int argc, const char** argv) { + if (argc >= 1) { + input_data_dir = fs::path(argv[0]).remove_filename(); + } + fs::path output_file; + if (argc >= 2) { + if (argv[1][0] == '-') { + std::cerr + << "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" + << "\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.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) { + surface.dimensions(surface.display_dimensions()); + }); + img.draw_callback([&](auto &surface) { + DrawCPP(surface); + }); + img.begin_show(); + } + return 0; } 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 bde05eb..1fbc080 100644 --- a/P0267_RefImpl/Samples/sprites/main.cpp +++ b/P0267_RefImpl/Samples/sprites/main.cpp @@ -2,6 +2,16 @@ #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; using namespace std::experimental::io2d; @@ -150,18 +160,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. + fs::current_path(fs::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};