/OJGL/GLView.j

http://github.com/tartiflop/ojgl · Unknown · 72 lines · 53 code · 19 blank · 0 comment · 0 complexity · 23c9ba14667dd6bed7cfb9a0eb3648a7 MD5 · raw file

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