Skip to content

Commit f5f5b58

Browse files
committed
Use std::filesystem in examples
Since we aim for C++17 and up it should be supported.
1 parent 92cb8b6 commit f5f5b58

File tree

10 files changed

+144
-140
lines changed

10 files changed

+144
-140
lines changed

examples/CMakeLists.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
cmake_minimum_required ( VERSION 3.10 )
1+
cmake_minimum_required(VERSION 3.10)
22

3-
file( GLOB_RECURSE EXAMPLES_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" )
3+
file(GLOB_RECURSE EXAMPLES_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
44

5-
foreach(example_file ${EXAMPLES_FILES})
5+
foreach (example_file ${EXAMPLES_FILES})
66
message(STATUS "Adding example ${example_file}")
77
get_filename_component(target_name ${example_file} NAME_WE)
88

99
add_executable(${target_name} ${example_file})
10-
target_link_libraries( ${target_name} bmp::BitmapPlusPlus )
11-
target_compile_definitions ( ${target_name} PRIVATE "ROOT_DIR=\"${PROJECT_SOURCE_DIR}\"" )
12-
target_compile_definitions ( ${target_name} PRIVATE "BIN_DIR=\"${PROJECT_BINARY_DIR}\"" )
10+
target_link_libraries(${target_name} bmp::BitmapPlusPlus)
11+
target_compile_definitions(${target_name} PRIVATE "ROOT_DIR=\"${PROJECT_SOURCE_DIR}\"")
12+
target_compile_definitions(${target_name} PRIVATE "BIN_DIR=\"${PROJECT_BINARY_DIR}\"")
1313

14-
add_test (
15-
NAME ${target_name}
16-
COMMAND ${target_name}
14+
add_test(
15+
NAME ${target_name}
16+
COMMAND ${target_name}
1717
)
18-
endforeach()
18+
endforeach ()

examples/bernoulli_dist.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <iostream>
22
#include "BitmapPlusPlus.hpp"
33
#include <random>
4+
#include <filesystem>
45

56
int main() {
67
try {
@@ -16,7 +17,7 @@ int main() {
1617
pixel = color;
1718
}
1819

19-
image.save(std::string(BIN_DIR) + "/bernoulli.bmp");
20+
image.save(std::filesystem::path(BIN_DIR) / "bernoulli.bmp");
2021

2122
return EXIT_SUCCESS;
2223
}

examples/chess_board.cpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
#include <iostream>
2+
#include <filesystem>
23
#include "BitmapPlusPlus.hpp"
34

45
int main() {
5-
try {
6-
// 8x8 chess board
7-
bmp::Bitmap image(640, 640);
8-
const std::size_t board_dims = 8;
9-
const std::int32_t rect_w = image.width() / board_dims;
10-
const std::int32_t rect_h = image.height() / board_dims;
6+
try {
7+
// 8x8 chess board
8+
bmp::Bitmap image(640, 640);
9+
const std::size_t board_dims = 8;
10+
const std::int32_t rect_w = image.width() / board_dims;
11+
const std::int32_t rect_h = image.height() / board_dims;
1112

12-
// Iterate over rects
13-
bool is_white = true;
14-
for (std::size_t x = 0; x < image.width(); x += rect_w) {
15-
for (std::size_t y = 0; y < image.height(); y += rect_h) {
16-
const bmp::Pixel color = is_white ? bmp::White : bmp::Black;
17-
// Fill rect
18-
image.fill_rect(x, y, rect_w, rect_h, color);
19-
// Next rect in will be the opposite color
20-
is_white = !is_white;
21-
}
22-
is_white = !is_white;
23-
}
13+
// Iterate over rects
14+
bool is_white = true;
15+
for (std::size_t x = 0; x < image.width(); x += rect_w) {
16+
for (std::size_t y = 0; y < image.height(); y += rect_h) {
17+
const bmp::Pixel color = is_white ? bmp::White : bmp::Black;
18+
// Fill rect
19+
image.fill_rect(x, y, rect_w, rect_h, color);
20+
// Next rect in will be the opposite color
21+
is_white = !is_white;
22+
}
23+
is_white = !is_white;
24+
}
2425

25-
// Save bitmap to file
26-
image.save(std::string(BIN_DIR) + "/chess_board.bmp");
26+
// Save bitmap to file
27+
image.save(std::filesystem::path(BIN_DIR) / "chess_board.bmp");
2728

28-
return EXIT_SUCCESS;
29-
}
30-
catch (const bmp::Exception& e) {
31-
std::cerr << "[BMP ERROR]: " << e.what() << '\n';
32-
return EXIT_FAILURE;
33-
}
29+
return EXIT_SUCCESS;
30+
} catch (const bmp::Exception &e) {
31+
std::cerr << "[BMP ERROR]: " << e.what() << '\n';
32+
return EXIT_FAILURE;
33+
}
3434
}

examples/draw_primitives.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <iostream>
1+
#include <filesystem>
22
#include "BitmapPlusPlus.hpp"
33

44
using namespace bmp;
@@ -29,7 +29,7 @@ int main() {
2929
image.fill_circle(420, 170, 50, Lime);
3030

3131
// Save bitmap
32-
image.save(std::string(BIN_DIR) + "/primitives.bmp");
32+
image.save(std::filesystem::path(BIN_DIR) / "primitives.bmp");
3333

3434
return EXIT_SUCCESS;
3535
}

examples/fractals/julia.cpp

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
1+
#include <filesystem>
12
#include "BitmapPlusPlus.hpp"
23
#include "color_maps.inl"
34

4-
int main(void) {
5-
bmp::Bitmap image(1280, 960);
5+
int main() {
6+
bmp::Bitmap image(1280, 960);
67

7-
constexpr const std::uint16_t max_iterations = 300;
8+
constexpr std::uint16_t max_iterations = 300;
89

9-
constexpr const double cr = -0.70000;
10-
constexpr const double ci = 0.27015;
10+
constexpr double cr = -0.70000;
11+
constexpr double ci = 0.27015;
1112

12-
double prevr, previ;
13+
double prevr, previ;
1314

14-
for (std::int32_t y = 0; y < image.height(); ++y) {
15-
for (std::int32_t x = 0; x < image.width(); ++x) {
16-
double nextr = 1.5 * (2.0 * x / image.width() - 1.0);
17-
double nexti = (2.0 * y / image.height() - 1.0);
15+
for (std::int32_t y = 0; y < image.height(); ++y) {
16+
for (std::int32_t x = 0; x < image.width(); ++x) {
17+
double nextr = 1.5 * (2.0 * x / image.width() - 1.0);
18+
double nexti = (2.0 * y / image.height() - 1.0);
1819

19-
for (std::uint16_t i = 0; i < max_iterations; ++i) {
20-
prevr = nextr;
21-
previ = nexti;
20+
for (std::uint16_t i = 0; i < max_iterations; ++i) {
21+
prevr = nextr;
22+
previ = nexti;
2223

23-
nextr = prevr * prevr - previ * previ + cr;
24-
nexti = 2 * prevr * previ + ci;
24+
nextr = prevr * prevr - previ * previ + cr;
25+
nexti = 2 * prevr * previ + ci;
2526

26-
if (((nextr * nextr) + (nexti * nexti)) > 4) {
27-
const bmp::Pixel color = hsv_colormap[static_cast<std::size_t>((1000.0 * i) / max_iterations)];
28-
image.set(x, y, color);
29-
break;
30-
}
31-
}
32-
}
33-
}
27+
if (((nextr * nextr) + (nexti * nexti)) > 4) {
28+
const bmp::Pixel color = hsv_colormap[static_cast<std::size_t>((1000.0 * i) / max_iterations)];
29+
image.set(x, y, color);
30+
break;
31+
}
32+
}
33+
}
34+
}
3435

35-
image.save(std::string(BIN_DIR) + "/julia.bmp");
36+
image.save(std::filesystem::path(BIN_DIR) / "julia.bmp");
3637

37-
return EXIT_SUCCESS;
38+
return EXIT_SUCCESS;
3839
}

examples/fractals/mandelbrot.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
#include "BitmapPlusPlus.hpp"
22
#include "color_maps.inl"
33
#include <cmath>
4+
#include <filesystem>
45

5-
int main(void) {
6-
bmp::Bitmap image(1280, 960);
6+
int main() {
7+
bmp::Bitmap image(1280, 960);
78

8-
double cr, ci;
9-
double nextr, nexti;
10-
double prevr, previ;
11-
constexpr const std::uint16_t max_iterations = 3000;
9+
double cr, ci;
10+
double nextr, nexti;
11+
double prevr, previ;
12+
constexpr std::uint16_t max_iterations = 3000;
1213

13-
for (std::int32_t y = 0; y < image.height(); ++y) {
14-
for (std::int32_t x = 0; x < image.width(); ++x) {
15-
cr = 1.5 * (2.0 * x / image.width() - 1.0) - 0.5;
16-
ci = (2.0 * y / image.height() - 1.0);
14+
for (std::int32_t y = 0; y < image.height(); ++y) {
15+
for (std::int32_t x = 0; x < image.width(); ++x) {
16+
cr = 1.5 * (2.0 * x / image.width() - 1.0) - 0.5;
17+
ci = (2.0 * y / image.height() - 1.0);
1718

18-
nextr = nexti = 0;
19-
prevr = previ = 0;
19+
nextr = nexti = 0;
20+
prevr = previ = 0;
2021

21-
for (std::uint16_t i = 0; i < max_iterations; ++i) {
22-
prevr = nextr;
23-
previ = nexti;
22+
for (std::uint16_t i = 0; i < max_iterations; ++i) {
23+
prevr = nextr;
24+
previ = nexti;
2425

25-
nextr = prevr * prevr - previ * previ + cr;
26-
nexti = 2 * prevr * previ + ci;
26+
nextr = prevr * prevr - previ * previ + cr;
27+
nexti = 2 * prevr * previ + ci;
2728

28-
if (((nextr * nextr) + (nexti * nexti)) > 4) {
29-
const double z = sqrt(nextr * nextr + nexti * nexti);
29+
if (((nextr * nextr) + (nexti * nexti)) > 4) {
30+
const double z = sqrt(nextr * nextr + nexti * nexti);
3031

31-
// https://en.wikipedia.org/wiki/Mandelbrot_set#Continuous_.28smooth.29_coloring
32-
const std::uint32_t index = static_cast<std::uint32_t>(1000.0 * log2(1.75 + i - log2(log2(z))) /
33-
log2(max_iterations));
32+
// https://en.wikipedia.org/wiki/Mandelbrot_set#Continuous_.28smooth.29_coloring
33+
const std::uint32_t index = static_cast<std::uint32_t>(1000.0 * log2(1.75 + i - log2(log2(z))) / log2(max_iterations));
3434

35-
image.set(x, y, jet_colormap[index]);
35+
image.set(x, y, jet_colormap[index]);
3636

37-
break;
38-
}
39-
}
40-
}
41-
}
37+
break;
38+
}
39+
}
40+
}
41+
}
4242

43-
image.save(std::string(BIN_DIR) + "/mandelbrot.bmp");
43+
image.save(std::filesystem::path(BIN_DIR) / "mandelbrot.bmp");
4444

45-
return EXIT_SUCCESS;
46-
}
45+
return EXIT_SUCCESS;
46+
}

examples/polymorphic_shapes.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <iostream>
2+
#include <filesystem>
23
#include "BitmapPlusPlus.hpp"
34

45
struct Shape {
@@ -64,7 +65,7 @@ int main() {
6465
shape->draw(image);
6566
delete shape;
6667
}
67-
image.save(std::string(BIN_DIR) + "/polymorphic_shapes.bmp");
68+
image.save(std::filesystem::path(BIN_DIR) / "polymorphic_shapes.bmp");
6869

6970
return EXIT_SUCCESS;
7071
}

examples/read_bitmap.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
#include "BitmapPlusPlus.hpp"
22
#include <iostream>
3+
#include <filesystem>
34

4-
int main(void) {
5-
try {
6-
bmp::Bitmap image;
5+
int main() {
6+
try {
7+
bmp::Bitmap image;
78

8-
// Load penguin.bmp bitmap
9-
image.load(std::string(ROOT_DIR) + "/images/penguin.bmp");
9+
// Load penguin.bmp bitmap
10+
image.load(std::filesystem::path(ROOT_DIR) / "images" / "penguin.bmp");
1011

11-
// Modify loaded image (makes half of the image black)
12-
for (std::int32_t y = 0; y < image.height(); ++y) {
13-
for (std::int32_t x = 0; x < image.width() / 2; ++x) {
14-
image.set(x, y, bmp::Black);
15-
}
16-
}
12+
// Modify loaded image (makes half of the image black)
13+
for (std::int32_t y = 0; y < image.height(); ++y) {
14+
for (std::int32_t x = 0; x < image.width() / 2; ++x) {
15+
image.set(x, y, bmp::Black);
16+
}
17+
}
1718

18-
// Save
19-
image.save(std::string(BIN_DIR) + "/modified-penguin.bmp");
19+
// Save
20+
image.save(std::filesystem::path(BIN_DIR) / "modified-penguin.bmp");
2021

21-
return EXIT_SUCCESS;
22-
}
23-
catch (const bmp::Exception& e) {
24-
std::cerr << e.what() << std::endl;
25-
return EXIT_FAILURE;
26-
}
22+
return EXIT_SUCCESS;
23+
} catch (const bmp::Exception &e) {
24+
std::cerr << e.what() << std::endl;
25+
return EXIT_FAILURE;
26+
}
2727
}

examples/variant_shapes.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "BitmapPlusPlus.hpp"
2+
#include <filesystem>
23
#include <iostream>
34
#include <variant>
45

@@ -89,7 +90,7 @@ int main() {
8990
for (Shape& shape : shapes) {
9091
std::visit(drawer, shape); // visit will call the appropriate ShapeDrawer::operator() for each shape
9192
}
92-
image.save(std::string(BIN_DIR) + "/variant_shapes.bmp");
93+
image.save(std::filesystem::path(BIN_DIR) / "variant_shapes.bmp");
9394

9495
return EXIT_SUCCESS;
9596
}

0 commit comments

Comments
 (0)