Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample app fixes #103

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions P0267_RefImpl/Samples/draw_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<TARGET_FILE_DIR:${PROJECT_ID}>/texture.jpg)

Expand Down
56 changes: 49 additions & 7 deletions P0267_RefImpl/Samples/draw_cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
#include <iostream>
#include <io2d.h>

#if __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
#elif __has_include(<boost/filesystem.hpp>)
#include <boost/filesystem.hpp>
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 <typename Surface>
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 }));
Expand Down Expand Up @@ -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;
}
9 changes: 9 additions & 0 deletions P0267_RefImpl/Samples/sprites/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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" "$<TARGET_FILE_DIR:${PROJECT_ID}>/cat.jpg"
)
Expand Down
31 changes: 19 additions & 12 deletions P0267_RefImpl/Samples/sprites/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
#include <random>
#include <iostream>

#if __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
#elif __has_include(<boost/filesystem.hpp>)
#include <boost/filesystem.hpp>
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;
Expand Down Expand Up @@ -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};
Expand Down