diff --git a/raytracing/CMakeLists.txt b/raytracing/CMakeLists.txt index 171e19e..704a4a0 100644 --- a/raytracing/CMakeLists.txt +++ b/raytracing/CMakeLists.txt @@ -10,6 +10,7 @@ add_library(raytracing src/common.cpp src/tuple.cpp src/color.cpp + src/canvas.cpp ) target_include_directories(${PROJECT_NAME} diff --git a/raytracing/src/canvas.cpp b/raytracing/src/canvas.cpp new file mode 100644 index 0000000..e87955b --- /dev/null +++ b/raytracing/src/canvas.cpp @@ -0,0 +1,80 @@ +/*! + * canvas.cpp + * + * Copyright (c) 2024, NADAL Jean-Baptiste. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + * + * @Author: NADAL Jean-Baptiste + * @Date: 31/01/2024 + * + */ + +// This is an independent project of an individual developer. Dear PVS-Studio, please check it. +// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com + +/* ------------------------------------------------------------------------- */ + +#include "common.h" +#include "canvas.h" + +using namespace Raytracer; + +/* ------------------------------------------------------------------------- */ + +Canvas::Canvas(void) : m_width(0), m_height(0) +{ +} + +/* ------------------------------------------------------------------------- */ + +Canvas::Canvas(uint16_t a_width, uint16_t a_height) : m_width(a_width), m_height(a_height) +{ + m_pixels = std::vector>(m_width, std::vector(m_height)); + + for (int i = 0; i < m_width; ++i) + for (int j = 0; j < m_height; ++j) + m_pixels[i][j] == Color(0, 0, 0); +} + +/* ------------------------------------------------------------------------- */ + +uint16_t Canvas::width(void) const +{ + return m_width; +} + +/* ------------------------------------------------------------------------- */ + +uint16_t Canvas::height(void) const +{ + return m_height; +} + +/* ------------------------------------------------------------------------- */ + +const Color &Canvas::pixel_at(uint16_t a_pos_x, uint16_t a_pos_y) const +{ + return m_pixels[a_pos_x][a_pos_y]; +} + +/* ------------------------------------------------------------------------- */ + +bool Canvas::write_pixel(uint16_t a_pos_x, uint16_t a_pos_y, const Color &a_color) +{ + m_pixels[a_pos_x][a_pos_y] = a_color; + return true; +} diff --git a/raytracing/src/canvas.h b/raytracing/src/canvas.h new file mode 100644 index 0000000..62a0448 --- /dev/null +++ b/raytracing/src/canvas.h @@ -0,0 +1,59 @@ +/*! + * canvas.h + * + * Copyright (c) 2024, NADAL Jean-Baptiste. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + * + * @Author: NADAL Jean-Baptiste + * @Date: 31/01/2024 + * + */ + +#ifndef _RAYTRACER_CANVAS_H +#define _RAYTRACER_CANVAS_H + +/* ------------------------------------------------------------------------- */ + +#include +#include + +#include "color.h" + +/* ------------------------------------------------------------------------- */ + +namespace Raytracer +{ + class Canvas + { + public: + Canvas(void); + Canvas(uint16_t a_width, uint16_t a_height); + + uint16_t width(void) const; + uint16_t height(void) const; + + const Color &pixel_at(uint16_t a_pos_x, uint16_t a_pos_y) const; + bool write_pixel(uint16_t a_pos_x, uint16_t a_pos_y, const Color &a_color); + + private: + uint16_t m_width; + uint16_t m_height; + std::vector> m_pixels; + }; +}; + +#endif // _RAYTRACER_CANVAS_H diff --git a/raytracing/src/color.h b/raytracing/src/color.h index 6fdd40d..0ecaff2 100644 --- a/raytracing/src/color.h +++ b/raytracing/src/color.h @@ -26,6 +26,8 @@ #ifndef _RAYTRACER_COLOR_H #define _RAYTRACER_COLOR_H +/* ------------------------------------------------------------------------- */ + namespace Raytracer { class Color @@ -60,4 +62,4 @@ namespace Raytracer }; }; -#endif +#endif // _RAYTRACER_COLOR_H diff --git a/tests/03_canvas.cpp b/tests/03_canvas.cpp new file mode 100644 index 0000000..f1c4ae0 --- /dev/null +++ b/tests/03_canvas.cpp @@ -0,0 +1,58 @@ +/*! + * 03_canvas.cpp + * + * Copyright (c) 2015-2024, NADAL Jean-Baptiste. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + * + * @Author: NADAL Jean-Baptiste + * @Date: 31/01/2024 + * + */ + +/*---------------------------------------------------------------------------*/ + +#include + +#include "canvas.h" + +using namespace Raytracer; + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[Canvas] Creating a canvas", "[Canvas]") +{ + Canvas c(10, 20); + + REQUIRE(c.width() == 10); + REQUIRE(c.height() == 20); + + for (int i = 0; i < 10; ++i) + for (int j = 0; j < 20; ++j) + REQUIRE(c.pixel_at(2, 3) == Color(0, 0, 0)); +} + +/* ------------------------------------------------------------------------- */ + +TEST_CASE("[Canvas] Writing pixels to a canvas", "[Canvas]") +{ + Canvas c(10, 20); + Color red(1, 0, 0); + + c.write_pixel(2, 3, red); + + REQUIRE(c.pixel_at(2, 3) == red); +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2fce1ec..16c3feb 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -12,6 +12,7 @@ add_executable(main_test 01_tuples.cpp 02_colors.cpp + 03_canvas.cpp ) include_directories("${CMAKE_SOURCE_DIR}/tests")