From 02f963a4bab95b3a328b6a792c9a7e027ade0e26 Mon Sep 17 00:00:00 2001 From: Hannes Schulze Date: Mon, 13 Jan 2025 22:03:10 +0100 Subject: implement display --- src/nes/CMakeLists.txt | 9 +++++++++ src/nes/display.cc | 16 ++++++++++++++++ src/nes/display.hh | 33 +++++++++++++++++++++++++++++++++ src/nes/nes.cc | 13 +++++++++++++ src/nes/nes.hh | 19 +++++++++++++++++++ src/nes/util/CMakeLists.txt | 4 ++++ src/nes/util/rgb.hh | 33 +++++++++++++++++++++++++++++++++ 7 files changed, 127 insertions(+) create mode 100644 src/nes/CMakeLists.txt create mode 100644 src/nes/display.cc create mode 100644 src/nes/display.hh create mode 100644 src/nes/nes.cc create mode 100644 src/nes/nes.hh create mode 100644 src/nes/util/CMakeLists.txt create mode 100644 src/nes/util/rgb.hh (limited to 'src/nes') diff --git a/src/nes/CMakeLists.txt b/src/nes/CMakeLists.txt new file mode 100644 index 0000000..560dcdd --- /dev/null +++ b/src/nes/CMakeLists.txt @@ -0,0 +1,9 @@ +add_subdirectory(util) + +target_sources( + nes + PRIVATE + nes.hh + nes.cc + display.hh + display.cc) \ No newline at end of file diff --git a/src/nes/display.cc b/src/nes/display.cc new file mode 100644 index 0000000..4ff1d5f --- /dev/null +++ b/src/nes/display.cc @@ -0,0 +1,16 @@ +#include "nes/display.hh" +#include "nes/util/rgb.hh" + +namespace nes +{ + auto display::clear(rgb const color) -> void + { + for (auto y = unsigned{ 0 }; y < height; ++y) + { + for (auto x = unsigned{ 0 }; x < width; ++x) + { + set(x, y, color); + } + } + } +} // namespace nes \ No newline at end of file diff --git a/src/nes/display.hh b/src/nes/display.hh new file mode 100644 index 0000000..14ccff1 --- /dev/null +++ b/src/nes/display.hh @@ -0,0 +1,33 @@ +#pragma once + +namespace nes +{ + class rgb; + + /// Abstraction of the display logic. + class display + { + public: + static constexpr auto width = unsigned{ 256 }; + static constexpr auto height = unsigned{ 240 }; + + virtual ~display() = default; + + display(display const&) = delete; + display(display&&) = delete; + auto operator=(display const&) -> display& = delete; + auto operator=(display&&) -> display& = delete; + + /// Switch between front and back buffers. + virtual auto switch_buffers() -> void = 0; + /// Update the pixel at the given position in the back buffer. + virtual auto set(unsigned x, unsigned y, rgb value) -> void = 0; + /// Load the pixel value at the given position in the back buffer. + virtual auto get(unsigned x, unsigned y) const -> rgb = 0; + /// Clear the entire back buffer with a color. + virtual auto clear(rgb color) -> void; + + protected: + explicit display() = default; + }; +} // namespace nes \ No newline at end of file diff --git a/src/nes/nes.cc b/src/nes/nes.cc new file mode 100644 index 0000000..c503d65 --- /dev/null +++ b/src/nes/nes.cc @@ -0,0 +1,13 @@ +#include "nes/nes.hh" +#include "nes/display.hh" +#include "nes/util/rgb.hh" + +namespace nes +{ + nes::nes(display& display) + : display_{ display } + { + display.clear(rgb::from_hex(0xFF0000)); + display.switch_buffers(); + } +} // namespace nes \ No newline at end of file diff --git a/src/nes/nes.hh b/src/nes/nes.hh new file mode 100644 index 0000000..ab975f0 --- /dev/null +++ b/src/nes/nes.hh @@ -0,0 +1,19 @@ +#pragma once + +namespace nes +{ + class display; + + class nes + { + display& display_; + + public: + explicit nes(display&); + + nes(nes const&) = delete; + nes(nes&&) = delete; + auto operator=(nes const&) -> nes& = delete; + auto operator=(nes&&) -> nes& = delete; + }; +} // namespace nes \ No newline at end of file diff --git a/src/nes/util/CMakeLists.txt b/src/nes/util/CMakeLists.txt new file mode 100644 index 0000000..c2255dd --- /dev/null +++ b/src/nes/util/CMakeLists.txt @@ -0,0 +1,4 @@ +target_sources( + nes + PRIVATE + rgb.hh) \ No newline at end of file diff --git a/src/nes/util/rgb.hh b/src/nes/util/rgb.hh new file mode 100644 index 0000000..d62c70c --- /dev/null +++ b/src/nes/util/rgb.hh @@ -0,0 +1,33 @@ +#pragma once + +#include + +namespace nes +{ + /// An RGB value. + class rgb + { + public: + explicit rgb() = default; + + explicit rgb(std::uint8_t const r, std::uint8_t const g, std::uint8_t const b) + : r{ r } + , g{ g } + , b{ b } + { + } + + static auto from_hex(std::uint32_t const hex) -> rgb + { + auto res = rgb{}; + res.r = (hex & 0x00FF0000) >> 16; + res.g = (hex & 0x0000FF00) >> 8; + res.b = (hex & 0x000000FF) >> 0; + return res; + } + + std::uint8_t r{ 0 }; + std::uint8_t g{ 0 }; + std::uint8_t b{ 0 }; + }; +} // namespace nes \ No newline at end of file -- cgit v1.3.1