view dwt/opengl/GLCanvas.d @ 116:580596d83ac4

Ported dwt.opengl.GLCanvas
author Jacob Carlborg <doob@me.com>
date Wed, 31 Dec 2008 15:46:19 +0100
parents d8635bb48c7c
children 04928add86e6
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     
 * Port to the D programming language:
 *     Jacob Carlborg <doob@me.com>
 *******************************************************************************/
module dwt.opengl.GLCanvas;

import dwt.dwthelper.utils;

import dwt.DWT;
import dwt.DWTException;
import dwt.internal.cocoa.NSOpenGLContext;
import dwt.internal.cocoa.NSOpenGLPixelFormat;
import dwt.internal.cocoa.NSOpenGLView;
import dwt.internal.cocoa.OS;
import dwt.widgets.Canvas;
import dwt.widgets.Composite;
import dwt.widgets.Event;
import dwt.widgets.Listener;

import dwt.internal.objc.cocoa.Cocoa;
import dwt.opengl.GLData;

/**
 * GLCanvas is a widget capable of displaying OpenGL content.
 * 
 * @see GLData
 * @see <a href="http://www.eclipse.org/swt/snippets/#opengl">OpenGL snippets</a>
 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
 *
 * @since 3.2
 */

public class GLCanvas : Canvas {
    NSOpenGLView glView;
    NSOpenGLPixelFormat pixelFormat;
    static const int MAX_ATTRIBUTES = 32;

/**
 * Create a GLCanvas widget using the attributes described in the GLData
 * object provided.
 *
 * @param parent a composite widget
 * @param style the bitwise OR'ing of widget styles
 * @param data the requested attributes of the GLCanvas
 *
 * @exception IllegalArgumentException
 * <ul><li>ERROR_NULL_ARGUMENT when the data is null
 *     <li>ERROR_UNSUPPORTED_DEPTH when the requested attributes cannot be provided</ul> 
 * </ul>
 */
public this (Composite parent, int style, GLData data) {
    super (parent, style);
    if (data is null) DWT.error (DWT.ERROR_NULL_ARGUMENT);
    int attrib [] = new int [MAX_ATTRIBUTES];
    int pos = 0;
    //TODO use color options
//  attrib [pos++] = OS.AGL_RGBA;
    if (data.doubleBuffer) attrib [pos++] = OS.NSOpenGLPFADoubleBuffer;
    if (data.stereo) attrib [pos++] = OS.NSOpenGLPFAStereo;
//  if (data.redSize > 0) {
//      attrib [pos++] = OS.AGL_RED_SIZE;
//      attrib [pos++] = data.redSize;
//  }
//  if (data.greenSize > 0) {
//      attrib [pos++] = OS.AGL_GREEN_SIZE;
//      attrib [pos++] = data.greenSize;
//  }
//  if (data.blueSize > 0) {
//      attrib [pos++] = OS.AGL_BLUE_SIZE;
//      attrib [pos++] = data.blueSize;
//  }
    if (data.alphaSize > 0) {
        attrib [pos++] = OS.NSOpenGLPFAAlphaSize;
        attrib [pos++] = data.alphaSize;
    }
    if (data.depthSize > 0) {
        attrib [pos++] = OS.NSOpenGLPFADepthSize;
        attrib [pos++] = data.depthSize;
    }
    if (data.stencilSize > 0) {
        attrib [pos++] = OS.NSOpenGLPFAStencilSize;
        attrib [pos++] = data.stencilSize;
    }
//  if (data.accumRedSize > 0) {
//      attrib [pos++] = OS.AGL_ACCUM_RED_SIZE;
//      attrib [pos++] = data.accumRedSize;
//  }
//  if (data.accumGreenSize > 0) {
//      attrib [pos++] = OS.AGL_ACCUM_GREEN_SIZE;
//      attrib [pos++] = data.accumGreenSize;
//  }
//  if (data.accumBlueSize > 0) {
//      attrib [pos++] = OS.AGL_ACCUM_BLUE_SIZE;
//      attrib [pos++] = data.accumBlueSize;
//  }
//  if (data.accumAlphaSize > 0) {
//      attrib [pos++] = OS.AGL_ACCUM_ALPHA_SIZE;
//      attrib [pos++] = data.accumAlphaSize;
//  }
    if (data.sampleBuffers > 0) {
        attrib [pos++] = OS.NSOpenGLPFASampleBuffers;
        attrib [pos++] = data.sampleBuffers;
    }
    if (data.samples > 0) {
        attrib [pos++] = OS.NSOpenGLPFASamples;
        attrib [pos++] = data.samples;
    }
    attrib [pos++] = 0;
    pixelFormat = cast(NSOpenGLPixelFormat)(new NSOpenGLPixelFormat()).alloc();
    if (pixelFormat is null) {      
        dispose ();
        DWT.error (DWT.ERROR_UNSUPPORTED_DEPTH);
    }
    pixelFormat.initWithAttributes((cast(NSOpenGLPixelFormatAttribute[])attrib).ptr);
    
    glView = cast(NSOpenGLView)(new NSOpenGLView()).alloc();
    if (glView is null) {       
        dispose ();
        DWT.error (DWT.ERROR_UNSUPPORTED_DEPTH);
    }
    glView.initWithFrame(parent.view.bounds(), pixelFormat);
    glView.setAutoresizingMask(OS.NSViewWidthSizable | OS.NSViewHeightSizable);
    parent.view.addSubview(glView);

    Listener listener = new class () Listener {
        public void handleEvent (Event event) {
            switch (event.type) {
                case DWT.Dispose:
                    if (glView !is null) {
                        glView.clearGLContext();
                        glView.release();
                    }
                    glView = null;
                    if (pixelFormat !is null) pixelFormat.release();
                    pixelFormat = null;
                    break;
                default:
            }
        }
    };
    addListener (DWT.Dispose, listener);
}

/**
 * Returns a GLData object describing the created context.
 *  
 * @return GLData description of the OpenGL context attributes
 * @exception DWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 */
public GLData getGLData () {
    checkWidget ();
    GLData data = new GLData ();
    int [] value = new int [1];
    //TODO implement getGLData()
//  AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_DOUBLEBUFFER, value);
//  data.doubleBuffer = value [0] !is 0;
//  AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_STEREO, value);
//  data.stereo = value [0] !is 0;
//  AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_RED_SIZE, value);
//  data.redSize = value [0];
//  AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_GREEN_SIZE, value);
//  data.greenSize = value [0];
//  AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_BLUE_SIZE, value);
//  data.blueSize = value [0];
//  AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_ALPHA_SIZE, value);
//  data.alphaSize = value [0];
//  AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_DEPTH_SIZE, value);
//  data.depthSize = value [0];
//  AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_STENCIL_SIZE, value);
//  data.stencilSize = value [0];
//  AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_ACCUM_RED_SIZE, value);
//  data.accumRedSize = value [0];
//  AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_ACCUM_GREEN_SIZE, value);
//  data.accumGreenSize = value [0];
//  AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_ACCUM_BLUE_SIZE, value);
//  data.accumBlueSize = value [0];
//  AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_ACCUM_ALPHA_SIZE, value);
//  data.accumAlphaSize = value [0];
//  AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_SAMPLE_BUFFERS_ARB, value);
//  data.sampleBuffers = value [0];
//  AGL.aglDescribePixelFormat (pixelFormat, AGL.AGL_SAMPLES_ARB, value);
    data.samples = value [0];
    return data;
}

/**
 * Returns a bool indicating whether the receiver's OpenGL context
 * is the current context.
 *  
 * @return true if the receiver holds the current OpenGL context,
 * false otherwise
 * @exception DWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 */
public bool isCurrent () {
    checkWidget ();
    return NSOpenGLContext.currentContext().id is glView.openGLContext().id;
}

/**
 * Sets the OpenGL context associated with this GLCanvas to be the
 * current GL context.
 * 
 * @exception DWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 */
public void setCurrent () {
    checkWidget ();
    glView.openGLContext().makeCurrentContext();
}

/**
 * Swaps the front and back color buffers.
 * 
 * @exception DWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 */
public void swapBuffers () {
    checkWidget ();
    glView.openGLContext().flushBuffer();
}
}