mirror of
https://codeberg.org/ciciplusplus/bLEH
synced 2026-12-06 10:03:52 -10:00
220 lines
6.6 KiB
Objective-C
220 lines
6.6 KiB
Objective-C
#import <Cocoa/Cocoa.h>
|
|
#import <OpenGL/gl.h>
|
|
#import <math.h>
|
|
|
|
// A view that owns an NSOpenGLContext and renders directly to it. This is the
|
|
// "set up the view and GL context by hand" alternative to NSOpenGLView.
|
|
@interface HelloGLView : NSView {
|
|
NSOpenGLContext *_glContext;
|
|
GLfloat _phase;
|
|
}
|
|
- (id)initWithFrame:(NSRect)frame pixelFormat:(NSOpenGLPixelFormat *)format;
|
|
- (void)render;
|
|
- (void)tick:(NSTimer *)timer;
|
|
@end
|
|
|
|
@implementation HelloGLView
|
|
|
|
- (id)initWithFrame:(NSRect)frame pixelFormat:(NSOpenGLPixelFormat *)format {
|
|
self = [super initWithFrame:frame];
|
|
if (self != nil) {
|
|
_glContext = [[NSOpenGLContext alloc] initWithFormat:format shareContext:nil];
|
|
if (_glContext == nil) {
|
|
NSLog(@"HelloGL: failed to create NSOpenGLContext.");
|
|
}
|
|
_phase = 0.0f;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)dealloc {
|
|
[_glContext release];
|
|
[super dealloc];
|
|
}
|
|
|
|
// The GL content covers the whole view, so let AppKit skip drawing behind it.
|
|
- (BOOL)isOpaque {
|
|
return YES;
|
|
}
|
|
|
|
// A GL context can only be attached to a view once that view belongs to a
|
|
// window, so we do it here rather than in the initializer.
|
|
- (void)viewDidMoveToWindow {
|
|
[super viewDidMoveToWindow];
|
|
if ([self window] != nil && _glContext != nil) {
|
|
[_glContext setView:self];
|
|
[_glContext update];
|
|
|
|
[_glContext makeCurrentContext];
|
|
NSLog(@"OpenGL vendor: %s", (const char *)glGetString(GL_VENDOR));
|
|
NSLog(@"OpenGL renderer: %s", (const char *)glGetString(GL_RENDERER));
|
|
NSLog(@"OpenGL version: %s", (const char *)glGetString(GL_VERSION));
|
|
}
|
|
}
|
|
|
|
// Keep the drawable in sync when the view is resized.
|
|
- (void)setFrameSize:(NSSize)newSize {
|
|
[super setFrameSize:newSize];
|
|
[_glContext update];
|
|
}
|
|
|
|
- (void)drawRect:(NSRect)dirtyRect {
|
|
(void)dirtyRect;
|
|
[self render];
|
|
}
|
|
|
|
// Advance the animation and draw the next frame. Called from the timer.
|
|
- (void)tick:(NSTimer *)timer {
|
|
(void)timer;
|
|
_phase += 0.03f;
|
|
[self render];
|
|
}
|
|
|
|
- (void)render {
|
|
if (_glContext == nil) {
|
|
return;
|
|
}
|
|
|
|
[_glContext makeCurrentContext];
|
|
|
|
NSRect bounds = [self bounds];
|
|
glViewport(0, 0, (GLsizei)bounds.size.width, (GLsizei)bounds.size.height);
|
|
|
|
glClearColor(0.10f, 0.12f, 0.18f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
glLoadIdentity();
|
|
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
|
|
glMatrixMode(GL_MODELVIEW);
|
|
glLoadIdentity();
|
|
|
|
// A triangle whose vertex colours cycle over time (2*pi/3 phase apart).
|
|
GLfloat p = _phase;
|
|
glBegin(GL_TRIANGLES);
|
|
glColor3f(0.5f + 0.5f * sinf(p), 0.2f, 0.3f);
|
|
glVertex2f(0.0f, 0.8f);
|
|
glColor3f(0.2f, 0.5f + 0.5f * sinf(p + 2.0943951f), 0.3f);
|
|
glVertex2f(-0.8f, -0.8f);
|
|
glColor3f(0.2f, 0.3f, 0.5f + 0.5f * sinf(p + 4.1887902f));
|
|
glVertex2f(0.8f, -0.8f);
|
|
glEnd();
|
|
|
|
// We requested a double-buffered pixel format, so present by swapping.
|
|
[_glContext flushBuffer];
|
|
}
|
|
|
|
@end
|
|
|
|
// Choose an NSOpenGLPixelFormat that works across renderers. Crucially it does
|
|
// NOT request NSOpenGLPFAAccelerated: on machines without GPU-accelerated
|
|
// OpenGL (for example OS X 10.5 running under virtualization, where only the
|
|
// Apple software renderer is available) an accelerated request matches nothing
|
|
// and initWithAttributes: returns nil, which cascades into an invalid context
|
|
// and null GL_* strings. We ask for a double-buffered RGBA format with a depth
|
|
// buffer, and fall back to the most permissive request if that is unavailable.
|
|
static NSOpenGLPixelFormat *HelloGLChoosePixelFormat(void) {
|
|
NSOpenGLPixelFormatAttribute preferred[] = {
|
|
NSOpenGLPFADoubleBuffer,
|
|
NSOpenGLPFAColorSize, 24,
|
|
NSOpenGLPFAAlphaSize, 8,
|
|
NSOpenGLPFADepthSize, 16,
|
|
0
|
|
};
|
|
NSOpenGLPixelFormat *format =
|
|
[[NSOpenGLPixelFormat alloc] initWithAttributes:preferred];
|
|
if (format != nil) {
|
|
return [format autorelease];
|
|
}
|
|
|
|
NSLog(@"HelloGL: preferred pixel format unavailable; trying a minimal one.");
|
|
NSOpenGLPixelFormatAttribute minimal[] = {
|
|
NSOpenGLPFADoubleBuffer,
|
|
0
|
|
};
|
|
format = [[NSOpenGLPixelFormat alloc] initWithAttributes:minimal];
|
|
if (format == nil) {
|
|
NSLog(@"HelloGL: no usable NSOpenGLPixelFormat is available.");
|
|
}
|
|
return [format autorelease];
|
|
}
|
|
|
|
// A plain NSObject acting as the application delegate. On OS X 10.5 the
|
|
// NSApplicationDelegate protocol does not exist yet; the delegate methods are an
|
|
// informal protocol, so no protocol conformance is declared.
|
|
@interface HelloGLAppDelegate : NSObject {
|
|
NSWindow *_window;
|
|
HelloGLView *_glView;
|
|
NSTimer *_timer;
|
|
}
|
|
@end
|
|
|
|
@implementation HelloGLAppDelegate
|
|
|
|
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
|
|
(void)notification;
|
|
|
|
NSRect contentRect = NSMakeRect(200.0, 200.0, 480.0, 320.0);
|
|
NSUInteger styleMask = NSTitledWindowMask | NSClosableWindowMask |
|
|
NSMiniaturizableWindowMask | NSResizableWindowMask;
|
|
|
|
_window = [[NSWindow alloc] initWithContentRect:contentRect
|
|
styleMask:styleMask
|
|
backing:NSBackingStoreBuffered
|
|
defer:NO];
|
|
[_window setTitle:@"bleh OpenGL Test"];
|
|
|
|
NSOpenGLPixelFormat *format = HelloGLChoosePixelFormat();
|
|
if (format == nil) {
|
|
NSLog(@"HelloGL: aborting window setup, no OpenGL pixel format.");
|
|
return;
|
|
}
|
|
|
|
_glView = [[HelloGLView alloc] initWithFrame:contentRect pixelFormat:format];
|
|
[_window setContentView:_glView];
|
|
|
|
[_window makeKeyAndOrderFront:nil];
|
|
|
|
// Draw the first frame immediately, then animate at ~60 Hz.
|
|
[_glView render];
|
|
_timer = [[NSTimer scheduledTimerWithTimeInterval:(1.0 / 60.0)
|
|
target:_glView
|
|
selector:@selector(tick:)
|
|
userInfo:nil
|
|
repeats:YES] retain];
|
|
}
|
|
|
|
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)app {
|
|
(void)app;
|
|
return YES;
|
|
}
|
|
|
|
- (void)dealloc {
|
|
[_timer invalidate];
|
|
[_timer release];
|
|
[_glView release];
|
|
[_window release];
|
|
[super dealloc];
|
|
}
|
|
|
|
@end
|
|
|
|
int main(int argc, const char *argv[]) {
|
|
(void)argc;
|
|
(void)argv;
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
[NSApplication sharedApplication];
|
|
|
|
HelloGLAppDelegate *delegate = [[HelloGLAppDelegate alloc] init];
|
|
[NSApp setDelegate:delegate];
|
|
|
|
[NSApp activateIgnoringOtherApps:YES];
|
|
[NSApp run];
|
|
|
|
[delegate release];
|
|
[pool release];
|
|
return 0;
|
|
}
|