summaryrefslogtreecommitdiff
path: root/app/mac/impl/display-spritekit.mm
diff options
context:
space:
mode:
Diffstat (limited to 'app/mac/impl/display-spritekit.mm')
-rw-r--r--app/mac/impl/display-spritekit.mm50
1 files changed, 50 insertions, 0 deletions
diff --git a/app/mac/impl/display-spritekit.mm b/app/mac/impl/display-spritekit.mm
new file mode 100644
index 0000000..b6123d6
--- /dev/null
+++ b/app/mac/impl/display-spritekit.mm
@@ -0,0 +1,50 @@
+#import "impl/display-spritekit.hh"
+
+namespace nes::app::mac
+{
+ namespace
+ {
+ auto make_buffer() -> NSMutableData*
+ {
+ auto const length = NSUInteger{ display::width * display::height * 4 };
+ return [NSMutableData dataWithLength:length];
+ }
+ } // namespace
+
+ display_spritekit::display_spritekit(SKSpriteNode* node)
+ : node_{ node }
+ , buffer_front_{ make_buffer() }
+ , buffer_back_{ make_buffer() }
+ , bytes_back_{ static_cast<u8*>([buffer_back_ mutableBytes]) }
+ {
+ }
+
+ auto display_spritekit::switch_buffers() -> void
+ {
+ std::swap(buffer_back_, buffer_front_);
+ bytes_back_ = static_cast<u8*>([buffer_back_ mutableBytes]);
+
+ auto texture = [SKTexture textureWithData:buffer_front_ size:CGSizeMake(width, height) flipped:YES];
+ [texture setFilteringMode:SKTextureFilteringNearest];
+ [node_ setTexture:texture];
+ }
+
+ auto display_spritekit::set(u32 const x, u32 const y, rgb const value) -> void
+ {
+ auto const offset = (y * width + x) * 4;
+ bytes_back_[offset + 0] = value.r;
+ bytes_back_[offset + 1] = value.g;
+ bytes_back_[offset + 2] = value.b;
+ bytes_back_[offset + 3] = 255;
+ }
+
+ auto display_spritekit::get(u32 const x, u32 const y) const -> rgb
+ {
+ auto const offset = (y * width + x) * 4;
+ auto res = rgb{};
+ res.r = bytes_back_[offset + 0];
+ res.g = bytes_back_[offset + 1];
+ res.b = bytes_back_[offset + 2];
+ return res;
+ }
+} // namespace nes::app::mac \ No newline at end of file