]> git.saurik.com Git - apple/libdispatch.git/blob - examples/DispatchLife/DispatchLifeGLView.m
libdispatch-84.5.tar.gz
[apple/libdispatch.git] / examples / DispatchLife / DispatchLifeGLView.m
1 /*
2 * Copyright (c) 2009-2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_DTS_LICENSE_HEADER_START@
5 *
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.
11 *
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.
26 *
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.
32 *
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.
40 *
41 * @APPLE_DTS_LICENSE_HEADER_END@
42 */
43
44 #import "DispatchLifeGLView.h"
45
46 #import <OpenGL/OpenGL.h>
47
48 #include <OpenGL/gl.h>
49 #include <OpenGL/glext.h>
50 #include <OpenGL/glu.h>
51
52 #include <OpenGL/CGLCurrent.h>
53 #include <OpenGL/CGLContext.h>
54
55 extern size_t grid_x_size;
56 extern size_t grid_y_size;
57
58 @implementation DispatchLifeGLView
59
60 #define CELL_WIDTH 8
61 #define CELL_HEIGHT 8
62
63 - (void)goFullScreen:(NSOpenGLView*)view {
64 NSOpenGLPixelFormatAttribute attrs[] =
65 {
66 NSOpenGLPFAFullScreen,
67
68 NSOpenGLPFAScreenMask, CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay),
69
70 NSOpenGLPFAAccelerated,
71 NSOpenGLPFANoRecovery,
72 NSOpenGLPFADoubleBuffer,
73 0
74 };
75 NSOpenGLPixelFormat* pixFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
76
77 NSOpenGLContext* screen = [[NSOpenGLContext alloc] initWithFormat:pixFmt shareContext:[view openGLContext]];
78
79 CGDisplayErr err = CGCaptureAllDisplays();
80 if (err != CGDisplayNoErr) {
81 [screen release];
82 return;
83 }
84
85 [screen setFullScreen];
86 [screen makeCurrentContext];
87
88 glClearColor(0.0, 0.0, 0.0, 0.0);
89 glClear(GL_COLOR_BUFFER_BIT);
90 [screen flushBuffer];
91 glClear(GL_COLOR_BUFFER_BIT);
92 [screen flushBuffer];
93 }
94
95
96 - (id)initWithFrame:(NSRect)frame {
97 NSOpenGLPixelFormatAttribute attrs[] =
98 {
99 NSOpenGLPFAAccelerated,
100 NSOpenGLPFANoRecovery,
101 NSOpenGLPFADoubleBuffer,
102 0
103 };
104 NSOpenGLPixelFormat* pixFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
105
106 self = [super initWithFrame:frame pixelFormat:pixFmt];
107 if (self) {
108
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);
116
117 grid_x_size = 128;
118 grid_y_size = 96;
119
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);
124
125 [self adjustGLViewBounds];
126
127 [[NSTimer scheduledTimerWithTimeInterval: (1.0f / 15.0) target: self selector:@selector(drawRect:) userInfo:self repeats:true] retain];
128
129 }
130 return self;
131 }
132
133 - (void)drawRect:(NSRect)rect {
134 [[self openGLContext] makeCurrentContext];
135
136 glClear(GL_COLOR_BUFFER_BIT);
137
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);
141
142 const int width = grid_x_size;
143 const int height = grid_y_size;
144
145 int x, y;
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)) {
150 case '.':
151 image[i] = 0xCCCCCCFF;
152 break;
153 case '#':
154 image[i] = 0x000000FF;
155 break;
156 case ' ':
157 image[i] = 0xFFFFFFFF;
158 break;
159 default:
160 image[i] = 0x0000FFFF;
161 break;
162 }
163 }
164 }
165
166 glDrawPixels(width, height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8, image);
167
168 glFinish();
169
170 [[self openGLContext] flushBuffer];
171
172 }
173
174 - (void)adjustGLViewBounds
175 {
176 [[self openGLContext] makeCurrentContext];
177 [[self openGLContext] update];
178
179 NSRect rect = [self bounds];
180
181 glViewport(0, 0, (GLint) rect.size.width, (GLint) rect.size.height);
182 glMatrixMode(GL_PROJECTION);
183 glLoadIdentity();
184 gluOrtho2D(-(rect.size.width/2), rect.size.width/2, -(rect.size.height/2), rect.size.height/2);
185 glMatrixMode(GL_MODELVIEW);
186 glLoadIdentity();
187
188 [self setNeedsDisplay:true];
189 }
190
191 - (void)update // moved or resized
192 {
193 [super update];
194 [self adjustGLViewBounds];
195 }
196
197 - (void)reshape // scrolled, moved or resized
198 {
199 [super reshape];
200 [self adjustGLViewBounds];
201 }
202
203 @end