summaryrefslogtreecommitdiff
path: root/app/mac/impl/display-spritekit.mm
blob: c61f1b1eed2419ed68b5ba46955bc55115b918d0 (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
#import "impl/display-spritekit.hh"
#import "nes/common/utils.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;
	}
} // namespace nes::app::mac