Permalink
Browse files

OpenGL for OSX

  • Loading branch information...
1 parent 0569cba commit 03a36b4620f963d33fa88d2d26c6732d0c31a7fb @digego digego committed Nov 29, 2011
Showing with 160 additions and 6 deletions.
  1. +11 −2 src/Extempore.cpp
  2. +141 −3 src/SchemeFFI.cpp
  3. +8 −1 src/component.make
View
@@ -41,6 +41,10 @@
#include "SchemeREPL.h"
#include "EXTLLVM.h"
#include <string>
+#ifdef TARGET_OS_MAC
+#include <Cocoa/Cocoa.h>
+#include <AppKit/AppKit.h>
+#endif
enum { OPT_RUNTIME, OPT_SAMPLERATE, OPT_FRAMES, OPT_CHANNELS, OPT_INITFILE, OPT_PORT};
CSimpleOptA::SOption g_rgOptions[] = {
@@ -154,14 +158,19 @@ int main(int argc, char** argv)
primary_repl->connectToProcessAtHostname(host,primary_port);
extemp::SchemeREPL* utility_repl = new extemp::SchemeREPL(utility_name);
utility_repl->connectToProcessAtHostname(host,utility_port);
-
- // sleep indefiniately let scheme processes do the work
+
+
+#ifdef TARGET_OS_MAC
+ [[NSApplication sharedApplication] run];
+#else
while(1) {
#ifdef TARGET_OS_WINDOWS
Sleep(5000);
#else
sleep(5000);
#endif
}
+#endif
+
return 0;
}
View
@@ -33,6 +33,7 @@
*
*/
+
#include "SchemeFFI.h"
#include "AudioDevice.h"
#include "UNIV.h"
@@ -109,6 +110,12 @@
#elif TARGET_OS_LINUX
#include <GL/glx.h>
#include <GL/gl.h>
+#elif TARGET_OS_MAC
+#include <Cocoa/Cococa.h>
+#include <CoreFoundation/CoreFoundation.h>
+#include <AppKit/AppKit.h>
+#include <OpenGL/OpenGL.h>
+#include <OpenGL/gl.h>
#endif
#ifdef TARGET_OS_WINDOWS
@@ -276,11 +283,9 @@ namespace extemp {
#if defined (TARGET_OS_LINUX)
{ "glx:get-event", &SchemeFFI::getX11Event },
#endif
-#ifndef TARGET_OS_MAC
{ "gl:make-ctx", &SchemeFFI::makeGLContext },
{ "gl:set-context", &SchemeFFI::glMakeContextCurrent },
{ "gl:swap-buffers", &SchemeFFI::glSwapBuffers },
-#endif
//#ifdef EXT_BOOST
//#else
@@ -316,6 +321,7 @@ namespace extemp {
mk_foreign_func(sc, funcTable[i].func)
);
}
+
}
//////////////////// helper functions ////////////////////////
@@ -1365,7 +1371,6 @@ namespace extemp {
llvm_push_zone_stack((llvm_zone_t*)cptr_value(pair_car(args)));
return _sc->T;
}
-
////////////////////////////////////////////
//
@@ -2797,6 +2802,139 @@ LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
#elif TARGET_OS_MAC
+
+ pointer SchemeFFI::glSwapBuffers(scheme* _sc, pointer args)
+ {
+ //return objc_glSwapBuffers(_sc, args);
+ NSOpenGLView* view = (NSOpenGLView*) cptr_value(pair_car(args));
+ CGLContextObj ctx = (CGLContextObj) [[view openGLContext] CGLContextObj];
+ CGLLockContext(ctx);
+ [[view openGLContext] flushBuffer];
+ CGLUnlockContext(ctx);
+ //CGLContextObj ctx = CGLGetCurrentContext();
+ //CGLFlushDrawable(ctx);
+ return _sc->T;
+ }
+
+ pointer SchemeFFI::glMakeContextCurrent(scheme* _sc, pointer args)
+ {
+ // return objc_glMakeContextCurrent(_sc, args);
+ CGLContextObj ctx = CGLGetCurrentContext();
+ NSOpenGLView* view = (NSOpenGLView*) cptr_value(pair_car(args));
+ ctx = (CGLContextObj) [[view openGLContext] CGLContextObj];
+ //CGLLockContext(ctx);
+ CGLSetCurrentContext(ctx);
+ //CGLUnlockContext(ctx);
+ return _sc->T;
+ }
+
+
+ pointer SchemeFFI::makeGLContext(scheme* _sc, pointer args)
+ {
+ //return objc_makeGLContext(_sc, args);
+
+ bool fullscrn = (pair_cadr(args) == _sc->T) ? 1 : 0;
+ int posx = ivalue(pair_caddr(args));
+ int posy = ivalue(pair_cadddr(args));
+ int width = ivalue(pair_car(pair_cddddr(args)));
+ int height = ivalue(pair_cadr(pair_cddddr(args)));
+
+ NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
+
+ NSRect frameRect = NSMakeRect(posx,posy,width,height);
+
+ // Get the screen rect of our main display
+ NSArray* screens = [NSScreen screens];
+ NSScreen* scrn = [NSScreen mainScreen]; // [screens objectAtIndex:0];
+
+ NSRect screenRect = frameRect;
+ if(fullscrn) screenRect = [scrn frame];
+
+ //NSSize size = screenRect.size;
+ NSPoint position = screenRect.origin;
+ NSSize size = screenRect.size;
+
+ if(fullscrn) {
+ position.x = 0;
+ position.y = 0;
+ screenRect.origin = position;
+ }
+
+ NSOpenGLPixelFormatAttribute array[] =
+ {
+ NSOpenGLPFAWindow,
+ NSOpenGLPFACompliant,
+ NSOpenGLPFANoRecovery,
+
+ // Add these back for multsampling
+ // NSOpenGLPFAMultisample,
+ // NSOpenGLPFASampleBuffers, (NSOpenGLPixelFormatAttribute)1,
+ // NSOpenGLPFASamples, (NSOpenGLPixelFormatAttribute)4,
+
+ NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24,
+ NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)24,
+ NSOpenGLPFAAccumSize, (NSOpenGLPixelFormatAttribute)24,
+ NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)8,
+ NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute)8,
+
+ NSOpenGLPFADoubleBuffer,
+ NSOpenGLPFAAccelerated,
+ (NSOpenGLPixelFormatAttribute)0
+ };
+
+ NSOpenGLPixelFormat* fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes: (NSOpenGLPixelFormatAttribute*) array];
+ //NSOpenGLContext *ctx = _openGLContext = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:nil];
+ NSOpenGLView* view = [[NSOpenGLView alloc] initWithFrame:screenRect pixelFormat:fmt];
+
+ int windowStyleMask = NSTitledWindowMask;
+ if(fullscrn) windowStyleMask = NSBorderlessWindowMask;
+ NSWindow* window = [[NSWindow alloc] initWithContentRect:screenRect
+ styleMask:windowStyleMask
+ backing:NSBackingStoreBuffered
+ defer:YES screen:scrn];
+
+ [window setContentView:view];
+ [window useOptimizedDrawing:YES];
+ [window setOpaque:YES];
+ [window setBackgroundColor:[NSColor colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:1.0]];
+ if(fullscrn) {
+ [window setHasShadow:NO];
+ [window makeKeyAndOrderFront:nil];
+ }else{
+ [window setTitle:@"OpenGL Window"];
+ [window orderFront:nil];
+ }
+
+ [window display];
+
+ GLint swapInt = 1;
+ [[view openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
+
+ CGLContextObj ctx = (CGLContextObj) [[view openGLContext] CGLContextObj];
+
+ CGLSetCurrentContext(ctx);
+ CGLLockContext(ctx);
+
+ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
+ CGLEnable( ctx, kCGLCEMPEngine);
+
+ CGLUnlockContext(ctx);
+
+ // pointer list = _sc->NIL;
+ // _sc->imp_env->insert(list);
+ // pointer tlist = cons(_sc,mk_cptr(_sc,(void*)v),list);
+ // _sc->imp_env->erase(list);
+ // list = tlist;
+ // _sc->imp_env->insert(list);
+ // tlist = cons(_sc,mk_cptr(_sc,(void*)hdc),list);
+ // _sc->imp_env->erase(list);
+ // list = tlist;
+ [pool release];
+
+ return mk_cptr(_sc, view); //list; //_cons(_sc, mk_cptr(_sc, (void*)dpy),mk_cptr(_sc,(void*)glxWin),1);
+ }
+
+
#endif
View
@@ -16,7 +16,14 @@ SRCFILES := \
OBJFILES := $(patsubst src/%.cpp, $(OBJDIR)/%.o, $(SRCFILES))
+ifeq ($(EXTEMPORE_OS),darwin)
$(OBJDIR)/%.o: src/%.cpp
@echo + cxx $<
@mkdir -p $(@D)
- @$(CXX) $(CXXFLAGS) -c -o $@ $<
+ @$(CXX) $(CXXFLAGS) -c -x objective-c++ -o $@ $<
+else
+$(OBJDIR)/%.o: src/%.cpp
+ @echo + cxx $<
+ @mkdir -p $(@D)
+ @$(CXX) $(CXXFLAGS) -c -x c++ -o $@ $<
+endif

0 comments on commit 03a36b4

Please sign in to comment.