2 * Copyright (c) 2009-2009 Apple Inc. All rights reserved.
4 * @APPLE_DTS_LICENSE_HEADER_START@
6 * IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
7 * ("Apple") in consideration of your agreement to the following terms, and your
8 * use, installation, modification or redistribution of this Apple software
9 * constitutes acceptance of these terms. If you do not agree with these terms,
10 * please do not use, install, modify or redistribute this Apple software.
12 * In consideration of your agreement to abide by the following terms, and
13 * subject to these terms, Apple grants you a personal, non-exclusive license,
14 * under Apple's copyrights in this original Apple software (the "Apple Software"),
15 * to use, reproduce, modify and redistribute the Apple Software, with or without
16 * modifications, in source and/or binary forms; provided that if you redistribute
17 * the Apple Software in its entirety and without modifications, you must retain
18 * this notice and the following text and disclaimers in all such redistributions
19 * of the Apple Software. Neither the name, trademarks, service marks or logos of
20 * Apple Computer, Inc. may be used to endorse or promote products derived from
21 * the Apple Software without specific prior written permission from Apple. Except
22 * as expressly stated in this notice, no other rights or licenses, express or
23 * implied, are granted by Apple herein, including but not limited to any patent
24 * rights that may be infringed by your derivative works or by other works in
25 * which the Apple Software may be incorporated.
27 * The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
28 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
29 * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
31 * COMBINATION WITH YOUR PRODUCTS.
33 * IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
35 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
37 * DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
38 * CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
39 * APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * @APPLE_DTS_LICENSE_HEADER_END@
44 #import "DispatchLifeGLView.h"
46 #import <OpenGL/OpenGL.h>
48 #include <OpenGL/gl.h>
49 #include <OpenGL/glext.h>
50 #include <OpenGL/glu.h>
52 #include <OpenGL/CGLCurrent.h>
53 #include <OpenGL/CGLContext.h>
55 extern size_t grid_x_size;
56 extern size_t grid_y_size;
58 @implementation DispatchLifeGLView
63 - (void)goFullScreen:(NSOpenGLView*)view {
64 NSOpenGLPixelFormatAttribute attrs[] =
66 NSOpenGLPFAFullScreen,
68 NSOpenGLPFAScreenMask, CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay),
70 NSOpenGLPFAAccelerated,
71 NSOpenGLPFANoRecovery,
72 NSOpenGLPFADoubleBuffer,
75 NSOpenGLPixelFormat* pixFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
77 NSOpenGLContext* screen = [[NSOpenGLContext alloc] initWithFormat:pixFmt shareContext:[view openGLContext]];
79 CGDisplayErr err = CGCaptureAllDisplays();
80 if (err != CGDisplayNoErr) {
85 [screen setFullScreen];
86 [screen makeCurrentContext];
88 glClearColor(0.0, 0.0, 0.0, 0.0);
89 glClear(GL_COLOR_BUFFER_BIT);
91 glClear(GL_COLOR_BUFFER_BIT);
96 - (id)initWithFrame:(NSRect)frame {
97 NSOpenGLPixelFormatAttribute attrs[] =
99 NSOpenGLPFAAccelerated,
100 NSOpenGLPFANoRecovery,
101 NSOpenGLPFADoubleBuffer,
104 NSOpenGLPixelFormat* pixFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
106 self = [super initWithFrame:frame pixelFormat:pixFmt];
109 [[self openGLContext] makeCurrentContext];
110 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
111 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
112 glClearColor(1.0, 1.0, 1.0, 1.0);
113 glColor4f(1.0, 1.0, 1.0, 1.0);
114 glEnable(GL_RASTER_POSITION_UNCLIPPED_IBM);
115 glDisable(GL_DITHER);
120 self->grid = init_grid(grid_x_size, grid_y_size);
121 size_t image_size = grid_x_size * grid_y_size * sizeof(uint32_t);
122 self->image = malloc(image_size);
123 memset(self->image, 0xFF, image_size);
125 [self adjustGLViewBounds];
127 [[NSTimer scheduledTimerWithTimeInterval: (1.0f / 15.0) target: self selector:@selector(drawRect:) userInfo:self repeats:true] retain];
133 - (void)drawRect:(NSRect)rect {
134 [[self openGLContext] makeCurrentContext];
136 glClear(GL_COLOR_BUFFER_BIT);
138 NSRect bounds = [self bounds];
139 glRasterPos2f(-bounds.size.width/2, -bounds.size.height/2);
140 glPixelZoom(bounds.size.width/grid_x_size, bounds.size.height/grid_y_size);
142 const int width = grid_x_size;
143 const int height = grid_y_size;
146 for (y = 0; y < height; ++y) {
147 for (x = 0; x < width; ++x) {
148 int i = y * width + x;
149 switch (get_grid_display_char(grid, x, y)) {
151 image[i] = 0xCCCCCCFF;
154 image[i] = 0x000000FF;
157 image[i] = 0xFFFFFFFF;
160 image[i] = 0x0000FFFF;
166 glDrawPixels(width, height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8, image);
170 [[self openGLContext] flushBuffer];
174 - (void)adjustGLViewBounds
176 [[self openGLContext] makeCurrentContext];
177 [[self openGLContext] update];
179 NSRect rect = [self bounds];
181 glViewport(0, 0, (GLint) rect.size.width, (GLint) rect.size.height);
182 glMatrixMode(GL_PROJECTION);
184 gluOrtho2D(-(rect.size.width/2), rect.size.width/2, -(rect.size.height/2), rect.size.height/2);
185 glMatrixMode(GL_MODELVIEW);
188 [self setNeedsDisplay:true];
191 - (void)update // moved or resized
194 [self adjustGLViewBounds];
197 - (void)reshape // scrolled, moved or resized
200 [self adjustGLViewBounds];