Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/yabause/src/cocoa/YabauseGLView.m
2 views
1
/* Copyright 2010 Lawrence Sebald
2
3
This file is part of Yabause.
4
5
Yabause is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 2 of the License, or
8
(at your option) any later version.
9
10
Yabause is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
14
15
You should have received a copy of the GNU General Public License
16
along with Yabause; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
*/
19
20
#include "YabauseGLView.h"
21
22
#include "peripheral.h"
23
#include "vdp1.h"
24
25
@interface YabauseGLView (InternalFunctions)
26
- (NSScreen *)screen;
27
- (CGDirectDisplayID)screenID;
28
29
/* These are nice to have, but not really necessary to things... */
30
- (float)width;
31
- (float)height;
32
@end
33
34
@implementation YabauseGLView
35
36
- (id)initWithFrame:(NSRect)frameRect
37
{
38
NSOpenGLPixelFormatAttribute attrs[] = {
39
NSOpenGLPFAWindow,
40
NSOpenGLPFANoRecovery,
41
NSOpenGLPFAColorSize, 32,
42
NSOpenGLPFADepthSize, 32,
43
NSOpenGLPFADoubleBuffer,
44
0
45
};
46
47
NSOpenGLPixelFormat *fmt;
48
49
fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
50
51
if(fmt == nil) {
52
[fmt release];
53
return nil;
54
}
55
56
if(!(self = [super initWithFrame:frameRect pixelFormat:fmt])) {
57
[fmt release];
58
return nil;
59
}
60
61
_isFullscreen = NO;
62
63
[fmt release];
64
65
return self;
66
}
67
68
- (void)toggleFullscreen
69
{
70
CGError err;
71
CGDisplayFadeReservationToken token;
72
CGDirectDisplayID d = [self screenID];
73
74
err = CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval,
75
&token);
76
77
if(err == kCGErrorSuccess) {
78
CGDisplayFade(token, 0.5, kCGDisplayBlendNormal,
79
kCGDisplayBlendSolidColor, 0, 0, 0, 1);
80
}
81
82
if(!_isFullscreen) {
83
[self enterFullScreenMode:[self screen] withOptions:nil];
84
85
/* Hide the cursor, but store its location so we can restore it later.
86
Also, disassociate the mouse and the cursor position. */
87
CGDisplayHideCursor(d);
88
_mouseLoc = [NSEvent mouseLocation];
89
CGDisplayMoveCursorToPoint(d, CGPointZero);
90
CGAssociateMouseAndMouseCursorPosition(FALSE);
91
}
92
else {
93
CGPoint mousePoint;
94
int height = CGDisplayPixelsHigh(d);
95
96
mousePoint.x = _mouseLoc.x;
97
mousePoint.y = height - _mouseLoc.y;
98
99
/* Show the mouse pointer, and reassociate it with the mouse. */
100
CGAssociateMouseAndMouseCursorPosition(TRUE);
101
CGDisplayMoveCursorToPoint(d, mousePoint);
102
CGDisplayShowCursor(d);
103
104
[self exitFullScreenModeWithOptions:nil];
105
[[self window] makeFirstResponder:self];
106
}
107
108
if(err == kCGErrorSuccess) {
109
CGDisplayFade(token, 0.5, kCGDisplayBlendNormal,
110
kCGDisplayBlendSolidColor, 0, 0, 0, 0);
111
CGReleaseDisplayFadeReservation(token);
112
}
113
114
if(VIDCore)
115
VIDCore->Resize([self width], [self height], 0);
116
117
_isFullscreen = !_isFullscreen;
118
}
119
120
- (BOOL)acceptsFirstResponder
121
{
122
return YES;
123
}
124
125
- (void)keyDown:(NSEvent *)event
126
{
127
if([[event charactersIgnoringModifiers] length] >= 1) {
128
PerKeyDown([[event charactersIgnoringModifiers] characterAtIndex:0]);
129
}
130
}
131
132
- (void)keyUp:(NSEvent *)event
133
{
134
if([[event charactersIgnoringModifiers] length] >= 1) {
135
PerKeyUp([[event charactersIgnoringModifiers] characterAtIndex:0]);
136
}
137
}
138
139
- (void)showWindow
140
{
141
[window makeKeyAndOrderFront:self];
142
}
143
144
- (void)reshape
145
{
146
CGLContextObj cxt = CGLGetCurrentContext();
147
148
/* Make sure that the emulation thread doesn't attempt to do any OpenGL
149
calls during the resize event, otherwise one of the two will crash. */
150
CGLLockContext(cxt);
151
152
if(VIDCore)
153
VIDCore->Resize([self width], [self height], 0);
154
155
CGLUnlockContext(cxt);
156
157
[super reshape];
158
}
159
160
- (void)drawRect:(NSRect)rect
161
{
162
CGLContextObj cxt = CGLGetCurrentContext();
163
164
/* Make sure that the emulation thread doesn't attempt to do any OpenGL
165
calls during the flush to the screen. */
166
CGLLockContext(cxt);
167
[[self openGLContext] flushBuffer];
168
CGLUnlockContext(cxt);
169
}
170
171
@end /* @implementation YabauseGLView */
172
173
@implementation YabauseGLView (InternalFunctions)
174
175
- (NSScreen *)screen
176
{
177
NSArray *screens = [NSScreen screens];
178
NSEnumerator *i = [screens objectEnumerator];
179
NSScreen *obj;
180
NSRect f = [window frame];
181
NSRect sf;
182
183
/* Look for the screen that has the main window on it. */
184
while((obj = (NSScreen *)[i nextObject])) {
185
sf = [obj frame];
186
187
if(f.origin.x >= sf.origin.x && f.origin.y >= sf.origin.y &&
188
f.origin.x <= sf.origin.x + sf.size.width &&
189
f.origin.y <= sf.origin.y + sf.size.height) {
190
return obj;
191
}
192
}
193
194
/* Punt. */
195
return [NSScreen mainScreen];
196
}
197
198
- (CGDirectDisplayID)screenID
199
{
200
NSScreen *s = [self screen];
201
NSDictionary *d = [s deviceDescription];
202
NSNumber *n = (NSNumber *)[d objectForKey:@"NSScreenNumber"];
203
204
return (CGDirectDisplayID)[n unsignedIntValue];
205
}
206
207
- (float)width
208
{
209
return [self bounds].size.width;
210
}
211
212
- (float)height
213
{
214
return [self bounds].size.height;
215
}
216
217
@end /* @implementation YabauseGLView (InternalFunctions) */
218
219