#include "stdafx.h"
#include "WGLRenderer.h"
#include "OGLrendrer.h"

WGLRenderer::WGLRenderer(void)
{
}


WGLRenderer::WGLRenderer(HDC dca)
{
	devcon = dca;
  	PIXELFORMATDESCRIPTOR pfd;
	ZeroMemory( &pfd, sizeof( pfd ) );
	pfd.nSize = sizeof( pfd );
	pfd.nVersion = 1;
	pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
	  PFD_SUPPORT_OPENGL | PFD_GENERIC_ACCELERATED | PFD_DOUBLEBUFFER;
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cColorBits = 16;
	pfd.cDepthBits = 32;
	//pfd.cAlphaBits = 8;
	pfd.iLayerType = PFD_MAIN_PLANE;
	int iFormat = ChoosePixelFormat( devcon, &pfd );
	SetPixelFormat( devcon, iFormat, &pfd );

	myglrc = wglCreateContext(devcon);
	wglMakeCurrent(devcon, myglrc);
	initGL();
}

WGLRenderer::~WGLRenderer(void)
{
   	wglDeleteContext(myglrc);
	DeleteDC(devcon);
}

void 
WGLRenderer::endFrame()
{
	glFinish();
	SwapBuffers(devcon);
}

void 
WGLRenderer::loadTexture(char *fileName, void **buffer)
{
	HANDLE myHandle;
	unsigned long flesze;
	myHandle = CreateFileA(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

	LARGE_INTEGER fSize;
	GetFileSizeEx(myHandle, &fSize);

	*buffer = malloc(fSize.QuadPart);
	
	if(ReadFile(myHandle, *buffer, fSize.QuadPart, &flesze ,NULL))
	{
		
	}
	CloseHandle(myHandle);
}

void *
WGLRenderer::getGlProcAddress(char *symbolName)
{
	return (void*)wglGetProcAddress(symbolName);
}
