From 30b4364c772114303d9045677186a53855ea5343 Mon Sep 17 00:00:00 2001 From: Hannes Schulze Date: Thu, 6 Feb 2025 16:28:39 +0100 Subject: implement in-game popups --- lib/nes/app/graphics/renderer.cc | 54 +++++++++++++++++++-- lib/nes/app/graphics/renderer.hh | 4 ++ lib/nes/app/graphics/tiles/CMakeLists.txt | 14 ++++++ lib/nes/app/graphics/tiles/border_bottom.png | Bin 0 -> 139 bytes lib/nes/app/graphics/tiles/border_bottom_left.png | Bin 0 -> 151 bytes lib/nes/app/graphics/tiles/border_bottom_right.png | Bin 0 -> 148 bytes lib/nes/app/graphics/tiles/border_left.png | Bin 0 -> 140 bytes lib/nes/app/graphics/tiles/border_right.png | Bin 0 -> 139 bytes lib/nes/app/graphics/tiles/border_top.png | Bin 0 -> 136 bytes lib/nes/app/graphics/tiles/border_top_left.png | Bin 0 -> 146 bytes lib/nes/app/graphics/tiles/border_top_right.png | Bin 0 -> 146 bytes 11 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 lib/nes/app/graphics/tiles/border_bottom.png create mode 100644 lib/nes/app/graphics/tiles/border_bottom_left.png create mode 100644 lib/nes/app/graphics/tiles/border_bottom_right.png create mode 100644 lib/nes/app/graphics/tiles/border_left.png create mode 100644 lib/nes/app/graphics/tiles/border_right.png create mode 100644 lib/nes/app/graphics/tiles/border_top.png create mode 100644 lib/nes/app/graphics/tiles/border_top_left.png create mode 100644 lib/nes/app/graphics/tiles/border_top_right.png (limited to 'lib/nes/app/graphics') diff --git a/lib/nes/app/graphics/renderer.cc b/lib/nes/app/graphics/renderer.cc index d512e71..564125b 100644 --- a/lib/nes/app/graphics/renderer.cc +++ b/lib/nes/app/graphics/renderer.cc @@ -5,6 +5,7 @@ #include "nes/app/graphics/image-view.hh" #include "nes/app/graphics/text-attributes.hh" #include "nes/app/graphics/tiles/characters.hh" +#include "nes/app/graphics/tiles/borders.hh" #include "nes/common/display.hh" namespace nes::app @@ -125,11 +126,9 @@ namespace nes::app } } - auto renderer::render_rect( - i32 const x, i32 const y, u32 const width, u32 const height, color const c) -> void + auto renderer::render_rect(i32 const x, i32 const y, u32 const width, u32 const height, color const c) -> void { if (c == color::transparent) { return; } - if (x >= static_cast(renderer::width) || y >= static_cast(renderer::height)) { return; } auto const resolved = resolve_color(c); auto const x_stop = std::min(x + static_cast(width), static_cast(renderer::width)); @@ -145,6 +144,55 @@ namespace nes::app } } + auto renderer::render_border(i32 const x, i32 const y, u32 const width, u32 const height, color const c) -> void + { + if (width <= 1 || height <= 1) { return; } + + auto const x_last = std::min(x + static_cast(width), static_cast(renderer::width)) - 1; + auto const y_last = std::min(y + static_cast(height), static_cast(renderer::height)) - 1; + auto const x_start = std::max(x, 0); + auto const y_start = std::max(y, 0); + for (auto border_y = i32{ y_start }; border_y <= y_last; ++border_y) + { + for (auto border_x = i32{ x_start }; border_x <= x_last; ++border_x) + { + if (border_x == x_start && border_y == y_start) + { + render_mask_tile(border_x, border_y, tiles::border_top_left, c); + } + else if (border_x == x_last && border_y == y_start) + { + render_mask_tile(border_x, border_y, tiles::border_top_right, c); + } + else if (border_x == x_last && border_y == y_last) + { + render_mask_tile(border_x, border_y, tiles::border_bottom_right, c); + } + else if (border_x == x_start && border_y == y_last) + { + render_mask_tile(border_x, border_y, tiles::border_bottom_left, c); + } + else if (border_x == x_start) + { + render_mask_tile(border_x, border_y, tiles::border_left, c); + } + else if (border_x == x_last) + { + render_mask_tile(border_x, border_y, tiles::border_right, c); + } + else if (border_y == y_start) + { + render_mask_tile(border_x, border_y, tiles::border_top, c); + } + else if (border_y == y_last) + { + render_mask_tile(border_x, border_y, tiles::border_bottom, c); + } + } + } + } + + auto renderer::render_fill(color const c) -> void { render_rect(0, 0, width, height, c); diff --git a/lib/nes/app/graphics/renderer.hh b/lib/nes/app/graphics/renderer.hh index 9b764a6..83cafb3 100644 --- a/lib/nes/app/graphics/renderer.hh +++ b/lib/nes/app/graphics/renderer.hh @@ -37,6 +37,8 @@ namespace nes::app auto operator=(renderer const&) -> renderer& = delete; auto operator=(renderer&&) -> renderer& = delete; + auto get_display() const -> display& { return display_; } + /// Render a colored tile at the given tile coordinates. auto render_image_tile(i32 x, i32 y, image_tile const&) -> void; /// Render an alpha-only tile at the given tile coordinates with the given color. @@ -66,6 +68,8 @@ namespace nes::app /// Render a rectangle at the given tile coordinates with a given size (measured in tiles). auto render_rect(i32 x, i32 y, u32 width, u32 height, color) -> void; + /// Render a border around a rectangle with the given size (measured in tiles). + auto render_border(i32 x, i32 y, u32 width, u32 height, color) -> void; /// Completely fill the screen. auto render_fill(color) -> void; diff --git a/lib/nes/app/graphics/tiles/CMakeLists.txt b/lib/nes/app/graphics/tiles/CMakeLists.txt index 144b9da..eced5ca 100644 --- a/lib/nes/app/graphics/tiles/CMakeLists.txt +++ b/lib/nes/app/graphics/tiles/CMakeLists.txt @@ -72,6 +72,20 @@ nes_generate_tiles( icon_directory.png icon_cartridge.png) +nes_generate_tiles( + nes + borders + TYPE mask + FILES + border_top.png + border_top_left.png + border_top_right.png + border_left.png + border_right.png + border_bottom.png + border_bottom_left.png + border_bottom_right.png) + nes_generate_tiles( nes banner diff --git a/lib/nes/app/graphics/tiles/border_bottom.png b/lib/nes/app/graphics/tiles/border_bottom.png new file mode 100644 index 0000000..05ac7a7 Binary files /dev/null and b/lib/nes/app/graphics/tiles/border_bottom.png differ diff --git a/lib/nes/app/graphics/tiles/border_bottom_left.png b/lib/nes/app/graphics/tiles/border_bottom_left.png new file mode 100644 index 0000000..974b1b1 Binary files /dev/null and b/lib/nes/app/graphics/tiles/border_bottom_left.png differ diff --git a/lib/nes/app/graphics/tiles/border_bottom_right.png b/lib/nes/app/graphics/tiles/border_bottom_right.png new file mode 100644 index 0000000..2fa6822 Binary files /dev/null and b/lib/nes/app/graphics/tiles/border_bottom_right.png differ diff --git a/lib/nes/app/graphics/tiles/border_left.png b/lib/nes/app/graphics/tiles/border_left.png new file mode 100644 index 0000000..ba5ecb6 Binary files /dev/null and b/lib/nes/app/graphics/tiles/border_left.png differ diff --git a/lib/nes/app/graphics/tiles/border_right.png b/lib/nes/app/graphics/tiles/border_right.png new file mode 100644 index 0000000..801bd7d Binary files /dev/null and b/lib/nes/app/graphics/tiles/border_right.png differ diff --git a/lib/nes/app/graphics/tiles/border_top.png b/lib/nes/app/graphics/tiles/border_top.png new file mode 100644 index 0000000..94d5cf5 Binary files /dev/null and b/lib/nes/app/graphics/tiles/border_top.png differ diff --git a/lib/nes/app/graphics/tiles/border_top_left.png b/lib/nes/app/graphics/tiles/border_top_left.png new file mode 100644 index 0000000..f67b585 Binary files /dev/null and b/lib/nes/app/graphics/tiles/border_top_left.png differ diff --git a/lib/nes/app/graphics/tiles/border_top_right.png b/lib/nes/app/graphics/tiles/border_top_right.png new file mode 100644 index 0000000..0b8613c Binary files /dev/null and b/lib/nes/app/graphics/tiles/border_top_right.png differ -- cgit v1.3.1