Remove OpenGL specific code from GBView

This commit is contained in:
Lior Halphon 2018-06-11 20:23:51 +03:00
parent d95ad1ca54
commit 9a3d53ae51
9 changed files with 119 additions and 58 deletions

View File

@ -54,10 +54,10 @@
<rect key="frame" x="0.0" y="0.0" width="160" height="144"/> <rect key="frame" x="0.0" y="0.0" width="160" height="144"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews> <subviews>
<openGLView colorSize="5bit_RGB_8bit_Alpha" useAuxiliaryDepthBufferStencil="NO" allowOffline="YES" wantsBestResolutionOpenGLSurface="YES" id="uqf-pe-VAF" customClass="GBView"> <view id="uqf-pe-VAF" customClass="GBView">
<rect key="frame" x="0.0" y="0.0" width="160" height="144"/> <rect key="frame" x="0.0" y="0.0" width="160" height="144"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
</openGLView> </view>
</subviews> </subviews>
</customView> </customView>
</subviews> </subviews>

View File

@ -1,6 +1,6 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
@interface GBShader : NSObject @interface GBGLShader : NSObject
- (instancetype)initWithName:(NSString *) shaderName; - (instancetype)initWithName:(NSString *) shaderName;
- (void) renderBitmap: (void *)bitmap previous:(void*) previous inSize:(NSSize)size scale: (double) scale; - (void) renderBitmap: (void *)bitmap previous:(void*) previous inSize:(NSSize)size scale: (double) scale;
@end @end

View File

@ -1,4 +1,4 @@
#import "GBShader.h" #import "GBGLShader.h"
#import <OpenGL/gl3.h> #import <OpenGL/gl3.h>
/* /*
@ -16,7 +16,7 @@ void main(void) {\n\
}\n\ }\n\
"; ";
@implementation GBShader @implementation GBGLShader
{ {
GLuint resolution_uniform; GLuint resolution_uniform;
GLuint texture_uniform; GLuint texture_uniform;

6
Cocoa/GBOpenGLView.h Normal file
View File

@ -0,0 +1,6 @@
#import <Cocoa/Cocoa.h>
#import "GBGLShader.h"
@interface GBOpenGLView : NSOpenGLView
@property GBGLShader *shader;
@end

42
Cocoa/GBOpenGLView.m Normal file
View File

@ -0,0 +1,42 @@
#import "GBOpenGLView.h"
#import "GBView.h"
#include <OpenGL/gl.h>
@implementation GBOpenGLView
- (void)drawRect:(NSRect)dirtyRect {
if (!self.shader) {
self.shader = [[GBGLShader alloc] initWithName:[[NSUserDefaults standardUserDefaults] objectForKey:@"GBFilter"]];
}
GBView *gbview = (GBView *)self.superview;
double scale = self.window.backingScaleFactor;
glViewport(0, 0, self.bounds.size.width * scale, self.bounds.size.height * scale);
if (gbview.shouldBlendFrameWithPrevious) {
[self.shader renderBitmap:gbview.currentBuffer
previous:gbview.previousBuffer
inSize:self.bounds.size
scale:scale];
}
else {
[self.shader renderBitmap:gbview.currentBuffer
previous:NULL
inSize:self.bounds.size
scale:scale];
}
glFlush();
}
- (instancetype)initWithFrame:(NSRect)frameRect pixelFormat:(NSOpenGLPixelFormat *)format
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(filterChanged) name:@"GBFilterChanged" object:nil];
return [super initWithFrame:frameRect pixelFormat:format];
}
- (void) filterChanged
{
self.shader = nil;
[self setNeedsDisplay:YES];
}
@end

View File

@ -1,14 +1,16 @@
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#include <Core/gb.h> #include <Core/gb.h>
#import "GBJoystickListener.h" #import "GBJoystickListener.h"
#import "GBShader.h"
@interface GBView<GBJoystickListener> : NSOpenGLView @interface GBView<GBJoystickListener> : NSView
- (void) flip; - (void) flip;
- (uint32_t *) pixels; - (uint32_t *) pixels;
@property GB_gameboy_t *gb; @property GB_gameboy_t *gb;
@property (nonatomic) BOOL shouldBlendFrameWithPrevious; @property (nonatomic) BOOL shouldBlendFrameWithPrevious;
@property GBShader *shader;
@property (getter=isMouseHidingEnabled) BOOL mouseHidingEnabled; @property (getter=isMouseHidingEnabled) BOOL mouseHidingEnabled;
@property bool isRewinding; @property bool isRewinding;
@property NSView *internalView;
- (void) createInternalView;
- (uint32_t *)currentBuffer;
- (uint32_t *)previousBuffer;
@end @end

View File

@ -1,6 +1,6 @@
#import <OpenGL/gl.h>
#import <Carbon/Carbon.h> #import <Carbon/Carbon.h>
#import "GBView.h" #import "GBView.h"
#import "GBViewGL.h"
#import "GBButtons.h" #import "GBButtons.h"
#import "NSString+StringForKey.h" #import "NSString+StringForKey.h"
@ -17,29 +17,26 @@
NSEventModifierFlags previousModifiers; NSEventModifierFlags previousModifiers;
} }
- (void) awakeFromNib + (instancetype)alloc
{ {
NSOpenGLPixelFormatAttribute attrs[] = if (self == [GBView class]) {
{ return [GBViewGL alloc];
NSOpenGLPFAOpenGLProfile, }
NSOpenGLProfileVersion3_2Core, return [super alloc];
0
};
NSOpenGLPixelFormat *pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] ;
if (!pf)
{
NSLog(@"No OpenGL pixel format");
} }
NSOpenGLContext* context = [[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil] ; + (instancetype)allocWithZone:(struct _NSZone *)zone
{
[self setPixelFormat:pf]; if (self == [GBView class]) {
return [GBViewGL allocWithZone: zone];
[self setOpenGLContext:context]; }
return [super allocWithZone:zone];
} }
- (void) createInternalView
{
assert(false && "createInternalView must not be inherited");
}
- (void) _init - (void) _init
{ {
@ -47,7 +44,6 @@
image_buffers[1] = malloc(160 * 144 * 4); image_buffers[1] = malloc(160 * 144 * 4);
image_buffers[2] = malloc(160 * 144 * 4); image_buffers[2] = malloc(160 * 144 * 4);
_shouldBlendFrameWithPrevious = 1; _shouldBlendFrameWithPrevious = 1;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(filterChanged) name:@"GBFilterChanged" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ratioKeepingChanged) name:@"GBAspectChanged" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ratioKeepingChanged) name:@"GBAspectChanged" object:nil];
tracking_area = [ [NSTrackingArea alloc] initWithRect:(NSRect){} tracking_area = [ [NSTrackingArea alloc] initWithRect:(NSRect){}
options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect
@ -55,12 +51,9 @@
userInfo:nil]; userInfo:nil];
[self addTrackingArea:tracking_area]; [self addTrackingArea:tracking_area];
clockMultiplier = 1.0; clockMultiplier = 1.0;
} [self createInternalView];
[self addSubview:self.internalView];
- (void) filterChanged self.internalView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
{
[self setNeedsDisplay:YES];
self.shader = nil;
} }
- (void) ratioKeepingChanged - (void) ratioKeepingChanged
@ -132,29 +125,6 @@
[super setFrame:frame]; [super setFrame:frame];
} }
- (void)drawRect:(NSRect)dirtyRect {
if (!self.shader) {
self.shader = [[GBShader alloc] initWithName:[[NSUserDefaults standardUserDefaults] objectForKey:@"GBFilter"]];
}
double scale = self.window.backingScaleFactor;
glViewport(0, 0, self.bounds.size.width * scale, self.bounds.size.height * scale);
if (_shouldBlendFrameWithPrevious) {
[self.shader renderBitmap:image_buffers[current_buffer]
previous:image_buffers[(current_buffer + 2) % self.numberOfBuffers]
inSize:self.bounds.size
scale:scale];
}
else {
[self.shader renderBitmap:image_buffers[current_buffer]
previous:NULL
inSize:self.bounds.size
scale:scale];
}
glFlush();
}
- (void) flip - (void) flip
{ {
if (underclockKeyDown && clockMultiplier > 0.5) { if (underclockKeyDown && clockMultiplier > 0.5) {
@ -362,4 +332,14 @@
previousModifiers = event.modifierFlags; previousModifiers = event.modifierFlags;
} }
- (uint32_t *)currentBuffer
{
return image_buffers[current_buffer];
}
- (uint32_t *)previousBuffer
{
return image_buffers[(current_buffer + 2) % self.numberOfBuffers];
}
@end @end

5
Cocoa/GBViewGL.h Normal file
View File

@ -0,0 +1,5 @@
#import "GBView.h"
@interface GBViewGL : GBView
@end

26
Cocoa/GBViewGL.m Normal file
View File

@ -0,0 +1,26 @@
#import "GBViewGL.h"
#import "GBOpenGLView.h"
@implementation GBViewGL
- (void)createInternalView
{
NSOpenGLPixelFormatAttribute attrs[] =
{
NSOpenGLPFAOpenGLProfile,
NSOpenGLProfileVersion3_2Core,
0
};
NSOpenGLPixelFormat *pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
assert(pf);
NSOpenGLContext *context = [[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil];
self.internalView = [[GBOpenGLView alloc] initWithFrame:self.frame pixelFormat:pf];
((GBOpenGLView *)self.internalView).wantsBestResolutionOpenGLSurface = YES;
((GBOpenGLView *)self.internalView).openGLContext = context;
}
@end