/*
    SDL - Simple DirectMedia Layer
    Copyright (C) 1997-2006 Sam Lantinga

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

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Sam Lantinga
    slouken@libsdl.org
*/

#include "SDL_config.h"

#include <proto/ogles2.h>

/*
 * GL support for AmigaOS 4 using GL4ES->OGLES2
 */

#if SDL_VIDEO_OPENGL

#include "SDL_os4video.h"
#include "SDL_os4utils.h"
#include "SDL_os4blit.h"

#include <proto/exec.h>
#include <proto/intuition.h>

#include <GL4ES/gl.h>

//#define DEBUG
#include "../../main/amigaos4/SDL_os4debug.h"

void* aglGetProcAddress(const char *);  // gl4es's aglGetProcAddress
void amiga_pre_swap(); // from gl4es to make working aglSwapBuffers()
void amiga_post_swap(); // from gl4es to make working aglSwapBuffers()


/*
 * initialize OGLES2 context
 */
int os4video_GL_Init(_THIS)
{

	struct SDL_PrivateVideoData *hidden = _this->hidden;
	
	dprintf("Initializing GL4ES->OGLES2..\n");
		
	hidden->IGL=IOGLES2->aglCreateContextTags(0,
				OGLES2_CCT_WINDOW,(ULONG)hidden->win,
				OGLES2_CCT_DEPTH,16,
				OGLES2_CCT_STENCIL,8,
				OGLES2_CCT_VSYNC,0,
				OGLES2_CCT_SINGLE_GET_ERROR_MODE,1,
			TAG_DONE);


	if (hidden->IGL)
	{
		_this->gl_config.driver_loaded = 1;
	
		IOGLES2->aglMakeCurrent(hidden->IGL);		
		
		hidden->OpenGL = TRUE;

		return 0;
	}
	else
	{
		_this->gl_config.driver_loaded = 0;
		SDL_SetError("Failed to create OGLES2 context");
	}

	return -1;

}

void os4video_GL_Term(_THIS)
{

}

int	os4video_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
{
   return 0;
}

int	os4video_GL_MakeCurrent(_THIS)
{
	return 0;
}

void os4video_GL_SwapBuffers(_THIS)
{

    amiga_pre_swap();
    // Swap the Buffers!
    if(IOGLES2) {
        IOGLES2->aglSwapBuffers();
    }
    amiga_post_swap(); 
 	
}


void *os4video_GL_GetProcAddress(_THIS, const char *proc) {
	void *func = NULL;

	if ( !_this->gl_config.driver_loaded )
	{
		if (os4video_GL_Init(_this) != 0)
		{
			return NULL;
		}
	}

	func = (void *)aglGetProcAddress(proc);
	return func;
}

int os4video_GL_LoadLibrary(_THIS, const char *path) {
	/* Library is always open */
	_this->gl_config.driver_loaded = 1;

	return 0;
}
#endif
