2013-04-09 13:31:46 +00:00
|
|
|
#include "opengl/opengl.hpp"
|
2013-03-19 08:48:50 +00:00
|
|
|
|
2013-03-21 12:59:01 +00:00
|
|
|
namespace ruby {
|
|
|
|
class pVideoCGL;
|
|
|
|
}
|
|
|
|
|
|
|
|
@interface RubyVideoCGL : NSOpenGLView {
|
|
|
|
@public
|
2013-05-02 11:25:45 +00:00
|
|
|
ruby::pVideoCGL* video;
|
2013-03-21 12:59:01 +00:00
|
|
|
}
|
|
|
|
-(id) initWith:(ruby::pVideoCGL*)video pixelFormat:(NSOpenGLPixelFormat*)pixelFormat;
|
|
|
|
-(void) reshape;
|
|
|
|
@end
|
|
|
|
|
2013-03-19 08:48:50 +00:00
|
|
|
namespace ruby {
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
struct pVideoCGL : OpenGL {
|
2014-01-28 10:04:58 +00:00
|
|
|
RubyVideoCGL* view = nullptr;
|
2013-03-19 08:48:50 +00:00
|
|
|
|
|
|
|
struct {
|
2014-01-28 10:04:58 +00:00
|
|
|
NSView* handle = nullptr;
|
|
|
|
bool synchronize = false;
|
|
|
|
unsigned filter = 0;
|
2013-03-19 08:48:50 +00:00
|
|
|
string shader;
|
|
|
|
} settings;
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
bool cap(const string& name) {
|
2013-03-19 08:48:50 +00:00
|
|
|
if(name == Video::Handle) return true;
|
|
|
|
if(name == Video::Synchronize) return true;
|
|
|
|
if(name == Video::Filter) return true;
|
|
|
|
if(name == Video::Shader) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
any get(const string& name) {
|
2013-03-19 08:48:50 +00:00
|
|
|
if(name == Video::Handle) return (uintptr_t)settings.handle;
|
|
|
|
if(name == Video::Synchronize) return settings.synchronize;
|
|
|
|
if(name == Video::Filter) return settings.filter;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
bool set(const string& name, const any& value) {
|
2013-03-19 08:48:50 +00:00
|
|
|
if(name == Video::Handle) {
|
|
|
|
settings.handle = (NSView*)any_cast<uintptr_t>(value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(name == Video::Synchronize) {
|
|
|
|
if(settings.synchronize != any_cast<bool>(value)) {
|
|
|
|
settings.synchronize = any_cast<bool>(value);
|
|
|
|
|
|
|
|
if(view) {
|
|
|
|
@autoreleasepool {
|
|
|
|
[[view openGLContext] makeCurrentContext];
|
2013-04-14 08:52:47 +00:00
|
|
|
int synchronize = settings.synchronize;
|
|
|
|
[[view openGLContext] setValues:&synchronize forParameter:NSOpenGLCPSwapInterval];
|
2013-03-19 08:48:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(name == Video::Filter) {
|
|
|
|
settings.filter = any_cast<unsigned>(value);
|
2013-04-09 13:31:46 +00:00
|
|
|
if(settings.shader.empty()) OpenGL::filter = settings.filter ? GL_LINEAR : GL_NEAREST;
|
2013-03-19 08:48:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(name == Video::Shader) {
|
|
|
|
settings.shader = any_cast<const char*>(value);
|
|
|
|
@autoreleasepool {
|
|
|
|
[[view openGLContext] makeCurrentContext];
|
|
|
|
}
|
2013-04-09 13:31:46 +00:00
|
|
|
OpenGL::shader(settings.shader);
|
|
|
|
if(settings.shader.empty()) OpenGL::filter = settings.filter ? GL_LINEAR : GL_NEAREST;
|
2013-03-19 08:48:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
bool lock(uint32_t*& data, unsigned& pitch, unsigned width, unsigned height) {
|
2013-04-09 13:31:46 +00:00
|
|
|
OpenGL::size(width, height);
|
2013-03-19 08:48:50 +00:00
|
|
|
return OpenGL::lock(data, pitch);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
@autoreleasepool {
|
|
|
|
[view lockFocus];
|
|
|
|
OpenGL::clear();
|
|
|
|
[[view openGLContext] flushBuffer];
|
|
|
|
[view unlockFocus];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void refresh() {
|
|
|
|
@autoreleasepool {
|
|
|
|
if([view lockFocusIfCanDraw]) {
|
|
|
|
auto area = [view frame];
|
2013-04-09 13:31:46 +00:00
|
|
|
outputWidth = area.size.width, outputHeight = area.size.height;
|
|
|
|
OpenGL::refresh();
|
2013-03-19 08:48:50 +00:00
|
|
|
[[view openGLContext] flushBuffer];
|
|
|
|
[view unlockFocus];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool init() {
|
|
|
|
term();
|
|
|
|
|
|
|
|
@autoreleasepool {
|
|
|
|
NSOpenGLPixelFormatAttribute attributes[] = {
|
2013-04-14 08:52:47 +00:00
|
|
|
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
|
2013-03-19 08:48:50 +00:00
|
|
|
NSOpenGLPFAColorSize, 24,
|
2013-04-14 08:52:47 +00:00
|
|
|
NSOpenGLPFAAlphaSize, 8,
|
2013-03-19 08:48:50 +00:00
|
|
|
NSOpenGLPFADoubleBuffer,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
auto size = [settings.handle frame].size;
|
|
|
|
auto format = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attributes] autorelease];
|
2013-04-14 08:52:47 +00:00
|
|
|
auto context = [[[NSOpenGLContext alloc] initWithFormat:format shareContext:nil] autorelease];
|
2013-03-19 08:48:50 +00:00
|
|
|
|
2013-03-21 12:59:01 +00:00
|
|
|
view = [[RubyVideoCGL alloc] initWith:this pixelFormat:format];
|
2013-04-14 08:52:47 +00:00
|
|
|
[view setOpenGLContext:context];
|
2013-03-19 08:48:50 +00:00
|
|
|
[view setFrame:NSMakeRect(0, 0, size.width, size.height)];
|
|
|
|
[view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
|
|
|
|
[settings.handle addSubview:view];
|
2013-04-14 08:52:47 +00:00
|
|
|
[context setView:view];
|
2013-03-19 08:48:50 +00:00
|
|
|
|
|
|
|
[view lockFocus];
|
|
|
|
|
|
|
|
OpenGL::init();
|
2013-04-14 08:52:47 +00:00
|
|
|
//print((const char*)glGetString(GL_VERSION), "\n");
|
2013-03-19 08:48:50 +00:00
|
|
|
|
|
|
|
int synchronize = settings.synchronize;
|
2013-04-14 08:52:47 +00:00
|
|
|
[[view openGLContext] setValues:&synchronize forParameter:NSOpenGLCPSwapInterval];
|
2013-03-19 08:48:50 +00:00
|
|
|
|
|
|
|
[view unlockFocus];
|
|
|
|
}
|
|
|
|
|
2013-03-21 12:59:01 +00:00
|
|
|
clear();
|
2013-03-19 08:48:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void term() {
|
|
|
|
OpenGL::term();
|
|
|
|
|
|
|
|
@autoreleasepool {
|
|
|
|
[view removeFromSuperview];
|
|
|
|
[view release];
|
|
|
|
view = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~pVideoCGL() {
|
|
|
|
term();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DeclareVideo(CGL)
|
|
|
|
|
|
|
|
}
|
2013-03-21 12:59:01 +00:00
|
|
|
|
|
|
|
@implementation RubyVideoCGL : NSOpenGLView
|
|
|
|
|
|
|
|
-(id) initWith:(ruby::pVideoCGL*)videoPointer pixelFormat:(NSOpenGLPixelFormat*)pixelFormat {
|
|
|
|
if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0) pixelFormat:pixelFormat]) {
|
|
|
|
video = videoPointer;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) reshape {
|
|
|
|
video->refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|