/* 
 * This file is automaticaly generated, Please dont edit this file! 
 */
#include <engine/opengl.h> 



























































event * globalEvent; 

resourceManager	* resources; 



































void opengl_initialize( opengl * this ) {

		printf("initialize opengl.\n");

		resources 	= resourceManager_newPointer();

		opengl_setupWindow( this );

		opengl_setupManagers( this );

		opengl_showVersion( this );

		

		opengl_setupPipeline( this );

		

		opengl_setupTime( this );

		opengl_setupRenderLoop( this );

	}

void opengl_showExtensions( opengl * this ) {

		GLint max_layers;

		glGetIntegerv ( GL_MAX_ARRAY_TEXTURE_LAYERS, &max_layers );

		printf("GL_MAX_ARRAY_TEXTURE_LAYERS:	%i\n", max_layers);


		GLint max_texture_size;

		glGetIntegerv (GL_MAX_TEXTURE_SIZE, &max_texture_size);

		printf("GL_MAX_TEXTURE_SIZE:	%i\n", max_texture_size);


	}

void opengl_showVersion( opengl * this ) {

		printf("opengl version : %s\n\n", glGetString(GL_VERSION) );

	}

void opengl_setupTime( opengl * this ) {

		clock_gettime( CLOCK_REALTIME, &this->startTime );

	}

void opengl_setupManagers( opengl * this ) {

		this->eventManger->mainDisplay = this->mainDisplay;

		this->eventManger->mainWindow = this->mainWindow;

		this->eventManger->RootWindow = this->RootWindow;

	}

void opengl_setupWindow( opengl * this ) {

		windowManager_setupDisplay( this->windowManager );

		windowManager_setupWindow( this->windowManager );


		this->mainDisplay 	= this->windowManager->mainDisplay;

		this->mainWindow 	= this->windowManager->mainWindow;

		this->RootWindow 	= this->windowManager->RootWindow;

	}

void opengl_setupRenderLoop( opengl * this ) {

		int IsProgramRunning 	= 1;

		while( IsProgramRunning ) {

		    while( XPending( this->mainDisplay ) ) {

		        XEvent GeneralEvent = {};

		        XNextEvent( this->mainDisplay, &GeneralEvent );

		        switch( GeneralEvent.type ) {

		            case ClientMessage: 
		            

		                IsProgramRunning = 0;

		            break;
		        }

		    }

			opengl_render( this );

		}

	}

void opengl_setupPipeline( opengl * this ) {

		quads * quadsPass 			= quads_newPointer();

		font * fontPass 			= font_newPointer();

		compute * computePass 		= compute_newPointer();

		compute2 * computePass2 	= compute2_newPointer();

		tesselation * tesselationPass 	= tesselation_newPointer();


		

		pipeline_addRenderPass( this->pipeline, 1, (int[1]){ 44 },  quadsPass );

		

		

		


	}

double opengl_clockToMilliseconds( opengl * this, clock_t ticks ) {

		return ( ticks / ( double ) CLOCKS_PER_SEC );

	}

void opengl_render( opengl * this ) {

		

		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

		opengl_clear( this,  GL_DEPTH_BUFFER_BIT );

		globalEvent = eventManger_fetchEvent( this->eventManger );

		pipeline_render( this->pipeline );

		opengl_swapBuffers( this );

		

	}

void opengl_displayFPS( opengl * this ) {

		struct timespec 	now;

		clock_gettime( CLOCK_REALTIME, &now );

		this->frameCount++;

		int elapsedTime		= now.tv_sec - this->startTime.tv_sec;
    	
		if( elapsedTime != this->lastTime ) {

			printf("%i fps.\n\n",  this->frameCount  );
			
			this->lastTime = elapsedTime;

			this->frameCount = 0;

		}

	}

void opengl_clear( opengl * this, GLbitfield mask ) {

		 glClear( mask );

	}

void opengl_clearColor( opengl * this, float r,  float g,  float b,  float a ) {

		glClearColor( r, g, b, a );

	}

void opengl_swapBuffers( opengl * this ) {

		PFNGLXSWAPINTERVALEXTPROC 		glXSwapIntervalEXT;

		PFNGLXSWAPINTERVALMESAPROC 		glXSwapIntervalMESA;

		PFNGLXSWAPINTERVALSGIPROC 		glXSwapIntervalSGI;


		glXSwapIntervalEXT 			= ( PFNGLXSWAPINTERVALEXTPROC ) glXGetProcAddress( ( const GLubyte * ) "glXSwapIntervalEXT" );

		if ( glXSwapIntervalEXT != NULL ) {

			glXSwapIntervalEXT( this->mainDisplay, this->mainWindow, 0 );

		} else {

			glXSwapIntervalMESA = ( PFNGLXSWAPINTERVALMESAPROC ) glXGetProcAddress( ( const GLubyte * ) "glXSwapIntervalMESA" );

			if ( glXSwapIntervalMESA != NULL ) {

				glXSwapIntervalMESA( 0 );

			} else {

				glXSwapIntervalSGI = ( PFNGLXSWAPINTERVALSGIPROC ) glXGetProcAddress( ( const GLubyte * ) "glXSwapIntervalSGI" );
				
				if ( glXSwapIntervalSGI != NULL ) {

					glXSwapIntervalSGI( 0 );
				
				}
			}
		}

		glXSwapBuffers( this->mainDisplay, this->mainWindow );

	}

opengl opengl_new() {

	opengl instance;

	instance.lastTime =  clock();

	instance.frameCount =  0;

	instance.windowManager = windowManager_newPointer();

	instance.eventManger = eventManger_newPointer();

	instance.pipeline = pipeline_newPointer();

	return instance;

}

opengl * opengl_newPointer() {

	struct opengl * pointer = malloc( sizeof ( struct opengl ) );

	pointer->lastTime =  clock();

	pointer->frameCount =  0;

	pointer->windowManager = windowManager_newPointer();

	pointer->eventManger = eventManger_newPointer();

	pointer->pipeline = pipeline_newPointer();

	return pointer;

}

