/*
    Stanley Designer
    Copyright (C) 2012  Ivan Pelipenko peri4ko@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <QApplication>
#include <QCleanlooksStyle>
#include <QDebug>
#include <QDir>
#include "qglview_window.h"
//#include <GL/glew.h>
//#include <GL/freeglut.h>
//#include "unistd.h"

/*void display ()
{
	qDebug() << (const char*)glGetString(GLUT_VERSION);
	qDebug() << (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);
	usleep(200000);
}*/
template <typename T, int N>
struct garray {
	garray(T * _i) {for (int i = 0; i < N; ++i) _c[i] = &(_i[i]);}
	T * _c[N];
	garray<T, N> & operator =(const garray<T, N> & o) {for (int i = 0; i < N; ++i) *_c[i] = *(o._c[i]); return *this;};
	template <int ON>
	garray<T, N> & operator =(const garray<T, ON> & o) {for (int i = 0; i < qMin(N, ON); ++i) *_c[i] = *(o._c[i]); return *this;};
};

template <typename T>
class gvec3 {
public:
	gvec3(T def = T()) {x = y = z = def;}
	union {
		T xyz[3];
		struct {T x, y, z;};
	};
private:
	T _c[3];
	
};

template <typename T>
QDebug operator<<(QDebug d, const gvec3<T> & c) {
	d.nospace() << "(" << c.x << ", " << c.y << ", " << c.z << ")";
	return d.space();
}

typedef gvec3<float> vec3;
typedef gvec3<int> ivec3;

int main(int argc, char ** argv) {
	/*float f[3] = {0., 1., 2.};
	float f2[3] = {-1., -1., -1.};
	garray<float, 3> gar(f2);
	qDebug() << f2[0] << f2[1] << f2[2];
	gar = garray<float, 3>((float[3]){f[2], f[1], f[0]});
	qDebug() << f2[0] << f2[1] << f2[2];*/
	/*vec3 v0, v1(20);
	v0.y = 10;
	qDebug() << v0 << v1;*/
	
	/*glutInit            ( &argc, argv );
	glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
	glutInitWindowSize  ( 500, 500 );

								// prepare context for 3.3
	glutInitContextVersion ( 3, 3 );
	glutInitContextFlags   ( GLUT_FORWARD_COMPATIBLE | GLUT_DEBUG );
	glutInitContextProfile ( GLUT_CORE_PROFILE );
	glutCreateWindow ( "Geometry shader example - particles" );
	glutDisplayFunc  ( display );
	glewInit ();
	if ( !GLEW_VERSION_3_3 )
	{
		printf ( "OpenGL 3.3 not supported.\n" );
		
		return 1;
	}
	
	glutMainLoop();
	*/
	//return 0;
	QApplication a(argc, argv);
	/*QGLFormat f;
	f.setVersion(3, 3);
	f.setSampleBuffers(true);
	f.setProfile(QGLFormat::CoreProfile);
	QGLFormat::setDefaultFormat(f);*/
	//QApplication::setStyle(new QCleanlooksStyle());
	//QDir::setCurrent(a.applicationDirPath());
	//a.setWindowIcon(QIcon(":/icons/peri4_paint.png"));
	QGLViewWindow w;
	w.show();
	QStringList al(a.arguments());
	al.pop_front();
	foreach (QString s, al)
		w.loadFile(s);
	return a.exec();
}
