Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beazley D.M.SWIG users manual.pdf
Скачиваний:
13
Добавлен:
23.08.2013
Размер:
1.53 Mб
Скачать

SWIG Users Guide

SWIG and Tcl

238

 

 

 

various OpenGL functions. This a great way to figure out what various functions do and to try different things without having to recompile and run after each change.

Problems with the OpenGL interface

While the OpenGL interface we have generated is fully usable, it is not without problems.

OpenGL constants are installed as global variables. As a result, it is necessary to use the global keyword when writing Tcl subroutines. For example :

proc clear_screan { } {

global GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT glClear $GL_COLOR_BUFFER_BIT

glClear $GL_DEPTH_BUFFER_BIT

}

Arrays need to be accessed via helper functions such as our newfv4() function. This approach certainly works and its easy enough to implement, but it may be preferable to call certain OpenGL functions with a Tcl list instead. For example :

glMaterialfv $GL_FRONT $GL_SPECULAR {1.0 1.0 1.0 1.0}

While these are only minor annoyances, it turns out that you can address both problems using SWIG typemaps (which are discussed shortly).

Exception handling

The %except directive can be used to create a user-definable exception handler in charge of converting exceptions in your C/C++ program into Tcl exceptions. The chapter on exception handling contains more details, but suppose you extended the array example into a C++ class like the following :

class RangeError {}; // Used for an exception

class DoubleArray { private:

int n; double *ptr;

public:

//Create a new array of fixed size DoubleArray(int size) {

ptr = new double[size]; n = size;

}

//Destroy an array

~DoubleArray() { delete ptr;

}

//Return the length of the array int length() {

return n;

}

//Get an item from the array and perform bounds checking. double getitem(int i) {

Version 1.1, June 24, 1997