SameBoy | Accurate GB/GBC emulator |
| download: https://git.y1.nz/archives/sameboy.tar.gz | |
| README | Files | Log | Refs | LICENSE |
AppleCommon/GBViewBase.m
1 #import "GBViewBase.h"
2
3 @implementation GBViewBase
4 {
5 uint32_t *_imageBuffers[3];
6 unsigned _currentBuffer;
7 GB_frame_blending_mode_t _frameBlendingMode;
8 bool _oddFrame;
9 GBViewBase *_parent;
10 __weak GBViewBase *_child;
11 }
12
13 - (void)screenSizeChanged
14 {
15 if (_parent) return;
16 if (_imageBuffers[0]) free(_imageBuffers[0]);
17 if (_imageBuffers[1]) free(_imageBuffers[1]);
18 if (_imageBuffers[2]) free(_imageBuffers[2]);
19
20 size_t buffer_size = sizeof(_imageBuffers[0][0]) * GB_get_screen_width(_gb) * GB_get_screen_height(_gb);
21
22 _imageBuffers[0] = calloc(1, buffer_size);
23 _imageBuffers[1] = calloc(1, buffer_size);
24 _imageBuffers[2] = calloc(1, buffer_size);
25 }
26
27 - (void)flip
28 {
29 if (_parent) return;
30 _currentBuffer = (_currentBuffer + 1) % self.numberOfBuffers;
31 _oddFrame = GB_is_odd_frame(_gb);
32 [_child flip];
33 }
34
35 - (unsigned) numberOfBuffers
36 {
37 assert(!_parent);
38 return _frameBlendingMode? 3 : 2;
39 }
40
41 - (void) createInternalView
42 {
43 assert(false && "createInternalView must not be inherited");
44 }
45
46 - (uint32_t *)currentBuffer
47 {
48 if (GB_unlikely(_parent)) {
49 return [_parent currentBuffer];
50 }
51 return _imageBuffers[_currentBuffer];
52 }
53
54 - (uint32_t *)previousBuffer
55 {
56 if (GB_unlikely(_parent)) {
57 return [_parent previousBuffer];
58 }
59 return _imageBuffers[(_currentBuffer + 2) % self.numberOfBuffers];
60 }
61
62 - (uint32_t *) pixels
63 {
64 assert(!_parent);
65 return _imageBuffers[(_currentBuffer + 1) % self.numberOfBuffers];
66 }
67
68 - (void) setFrameBlendingMode:(GB_frame_blending_mode_t)frameBlendingMode
69 {
70 _frameBlendingMode = frameBlendingMode;
71 [self setNeedsDisplay];
72 [_child setNeedsDisplay];
73 }
74
75 - (GB_frame_blending_mode_t)frameBlendingMode
76 {
77 if (GB_unlikely(_parent)) {
78 return [_parent frameBlendingMode];
79 }
80 if (_frameBlendingMode == GB_FRAME_BLENDING_MODE_ACCURATE) {
81 if (!_gb || GB_is_sgb(_gb)) {
82 return GB_FRAME_BLENDING_MODE_SIMPLE;
83 }
84 return _oddFrame ? GB_FRAME_BLENDING_MODE_ACCURATE_ODD : GB_FRAME_BLENDING_MODE_ACCURATE_EVEN;
85 }
86 return _frameBlendingMode;
87 }
88
89 - (void)dealloc
90 {
91 if (_parent) return;
92 free(_imageBuffers[0]);
93 free(_imageBuffers[1]);
94 free(_imageBuffers[2]);
95 }
96
97 #if !TARGET_OS_IPHONE
98 - (void)setNeedsDisplay
99 {
100 [self setNeedsDisplay:true];
101 }
102 #endif
103
104 - (void)setGb:(GB_gameboy_t *)gb
105 {
106 assert(!_parent);
107 _gb = gb;
108 if (_child) {
109 _child->_gb = gb;
110 }
111 }
112
113 - (instancetype)mirroredView
114 {
115 if (_child) return _child;
116 GBViewBase *ret = [[self.class alloc] initWithFrame:self.bounds];
117 ret->_parent = self;
118 ret->_gb = _gb;
119 return _child = ret;
120 }
121 @end
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.