view dwt/opengl/GLCanvas.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 649b8e223d5a
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2000, 2007 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
 *******************************************************************************/
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;

/**
 * GLCanvas is a widget capable of displaying OpenGL content.
 * 
 * @since 3.2
 */

public class GLCanvas extends Canvas {
    NSOpenGLView glView;
    NSOpenGLPixelFormat pixelFormat;
    static final 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 GLCanvas (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 this is not working
//  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 = (NSOpenGLPixelFormat)new NSOpenGLPixelFormat().alloc();
    if (pixelFormat is null) {      
        dispose ();
        DWT.error (DWT.ERROR_UNSUPPORTED_DEPTH);
    }
    pixelFormat.initWithAttributes(attrib);
    
    glView = (NSOpenGLView)new NSOpenGLView().alloc();
    if (glView is null) {       
        dispose ();
        DWT.error (DWT.ERROR_UNSUPPORTED_DEPTH);
    }
    glView.initWithFrame(parent.view.frame(), pixelFormat);
    glView.setAutoresizingMask(OS.NSViewWidthSizable | OS.NSViewHeightSizable);

    Listener listener = new Listener () {
        public void handleEvent (Event event) {
            switch (event.type) {
            case DWT.Dispose:
                if (glView !is null) glView.release();
                glView = null;
                if (pixelFormat !is null) pixelFormat.release();
                pixelFormat = null;
                break;
            }
        }
    };
    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];
//  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();
}
}