summaryrefslogtreecommitdiff
path: root/app/mac/impl/display-spritekit.mm
blob: b949f85bbd677f40c43812da7f96b4921f99512b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#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]) }
		, bytes_front_{ static_cast<u8*>([buffer_front_ mutableBytes]) }
	{
	}

	auto display_spritekit::switch_buffers() -> void
	{
		swap(buffer_back_, buffer_front_);
		swap(bytes_back_, bytes_front_);

		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_front_[offset + 0];
		res.g = bytes_front_[offset + 1];
		res.b = bytes_front_[offset + 2];
		return res;
	}
} // namespace nes::app::mac