PageRenderTime 19ms CodeModel.GetById 12ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/OJGL/GLView.j

http://github.com/tartiflop/ojgl
Unknown | 72 lines | 53 code | 19 blank | 0 comment | 0 complexity | 23c9ba14667dd6bed7cfb9a0eb3648a7 MD5 | raw file
 1
 2@import <AppKit/CPView.j>
 3@import "GLContext.j"
 4
 5@implementation GLView : CPView {
 6
 7	DOMElement _DOMCanvasElement;
 8	GLContext _gl;
 9}
10
11- (id)initWithFrame:(CGRect)aFrame {
12	self = [super initWithFrame:aFrame];
13	
14	if (self) {
15		_DOMCanvasElement = document.createElement("canvas");
16		_DOMCanvasElement.width = CGRectGetWidth([self bounds]);
17		_DOMCanvasElement.height = CGRectGetHeight([self bounds]);
18		_DOMCanvasElement.style.top = "0px";
19		_DOMCanvasElement.style.left = "0px";
20		
21		_DOMElement.appendChild(_DOMCanvasElement);
22
23		_gl = [[GLContext alloc] initWithGL:[self _context]];
24		[self prepareOpenGL];
25	}
26	
27	return self;
28}
29
30- (void)setNeedsDisplay:(BOOL)aFlag
31{
32  [self drawRect:nil]; // Because the right way to do it is too slow.
33}
34
35- (void)prepareOpenGL {
36}
37
38- (GLContext)glContext {
39	return _gl;
40}
41
42- (int)width {
43	return _DOMCanvasElement.width;
44}
45
46- (int)height {
47	return _DOMCanvasElement.height;
48}
49
50- (DOMElement)_context {
51	
52	var gl;
53	try { 
54		gl = _DOMCanvasElement.getContext("webkit-3d");
55		return gl;
56	} catch(e) {
57	}
58
59	try { 
60		gl = _DOMCanvasElement.getContext("moz-webgl"); 
61	} catch(e) {
62	}
63
64	if (!gl) {
65		CPLog.error(@"Could not create context")
66		return nil;
67	}
68	
69	return gl;
70}
71
72@end