/****************************/
/*   WINDOWS SPECIFIC.C		*/
/****************************/

/*	PangeaVR - 3D accelerated plug-in for viewing panoramas.

	©2005 Pangea Software, Inc.  All rights reserved.
	Pangea Software, PangeaVR and various logos are trademarks of Pangea Software, Inc.
	
	Written by Brian Greenstone,   brian@pangeasoft.net	
	
	This program and source code 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.  
	
	IMPORTANT:  The purpose in releasing this source code is to encourage others
				To port this program to other platforms, and to make improvments to it.
				However, to prevent things from getting out of control with lots of
				different versions of PangeaVR floating around the internet,
				Pangea Software, Inc. prohibits the public distribution of any versions
				of PangeaVR without prior approval from Pangea Software, Inc.
				
				Should you make any improvements to this source code and wish to
				have those changes rolled into an official release then you'll
				need to contact Pangea Software to have your modifications adapted.				
*/





/*

					WINDOWS PORTING NOTES

	NOTE: I've done what I can do make this easy for someone porting this.  On the Mac we use Gworlds for our pixel buffers,
	but I have no idea what you guys use on Windows, so some of the function shells below may not do what you need.  
	
	You'll want to cross reference the functions below with their Mac counterparts to see exactly how they're supposed to work.


*/



#ifndef TARGET_OS_MAC


/****************************/
/*    EXTERNALS             */
/****************************/

extern	MyInstanceType	gMyInstances[];
extern	int				gVideoSampleSize[];
extern	Ptr				gTempImageBuffer;
extern	Rect			gTempImageRect;


/****************************/
/*    PROTOTYPES            */
/****************************/



/****************************/
/*    CONSTANTS             */
/****************************/



/*********************/
/*    VARIABLES      */
/*********************/



/************ DO PLATFORM SPECIFIC INIT : WINDOWS *****************/

NPError DoPlatformSpecificInit_Windows(void)
{

	--------- PUT WINDOWS CODE HERE ------

	
	return(NPERR_NO_ERROR);
}


/******************** DO PLATFORM SPECIFIC SHUTDOWN : WINDOWS *********************/

void DoPlatformSpecificShutdown_Windows(void)
{

	--------- PUT WINDOWS CODE HERE ------

}

#pragma mark -



/********************** OGL:  CREATE DRAW CONTEXT : WINDOWS ***********************/
//
// This is the Mac-specific code for creating an OpenGL draw context.
//

OSErr	OGL_CreateDrawContext_Windows(int instanceNum)
{
PIXELFORMATDESCRIPTOR	pfd;
int						iFormat

		/* IF THERE'S ALREADY A CONTEXT THEN JUST UPDATE IT */
		
	if (gMyInstances[instanceNum].drawContext)
	{
		--------- PUT WINDOWS CODE HERE ------
//		aglUpdateContext(gMyInstances[instanceNum].drawContext);
		SetPaneClipRect_Windows(instanceNum); 
		OGL_DrawScene(instanceNum);
		return(1);
	}


			/* GET WINDOWS DEVICE CONTEXT */
			
	gMyInstances[instanceNum].hdc = GetDC( hWnd );


			/* SET PIXEL FORMAT */			

	ZeroMemory( &pfd, sizeof( pfd ) );
	pfd.nSize			= sizeof( pfd );
	pfd.nVersion		= 1;
	pfd.dwFlags			= PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
	pfd.iPixelType		= PFD_TYPE_RGBA;
	pfd.cColorBits		= 24;
	pfd.iLayerType		= PFD_MAIN_PLANE;
	iFormat				= ChoosePixelFormat( gMyInstances[instanceNum].hdc, &pfd );
	SetPixelFormat( hDC, iFormat, &pfd );


			/* CREATE DRAW CONTEXT */
			
	gMyInstances[instanceNum].drawContext = wglCreateContext(gMyInstances[instanceNum].hdc);						// create draw context

			

	return(noErr);
}


/***************** OGL:  DISPOSE DRAW CONTEXT : WINDOWS ***********************/
//
// Disposes of all data created by OGL_InitDrawContext for a draw context
//

void OGL_DisposeDrawContext_Windows(int instanceNum)
{
	ReleaseDC( hWnd, hDC );
	wglDeleteContext( gMyInstances[instanceNum].drawContext );
}



#pragma mark -



/********************** SET PANE CLIP RECT : WINDOWS ************************/
//
// Sets a buffer rect to clip just to the actual pane that our plug-in is supposed to draw into
// in this window.
//
// This code was tricky as hell.  Seems to work correctly tho :)
//

void SetPaneClipRect_Windows(int instanceNum)
{
	
	if (gMyInstances[instanceNum].drawContext == nil)
		return;
	
	--------- PUT WINDOWS CODE HERE ------
	
		SEE THE MAC VERSION OF THIS CODE AND THEN MODIFY, BECAUSE THIS WAS TRICKY.

	
}


#pragma mark -


/************** CALC FRAMES PER SECOND : WINDOWS *****************/

void CalcFramesPerSecond_Windows(int instanceNum)
{
	--------- PUT WINDOWS CODE HERE ------
}


#pragma mark -


/************************** OGL_INIT FONT : WINDOWS **************************/

void OGL_InitFont_Windows(int instanceNum)
{
	--------- PUT WINDOWS CODE HERE ------
	
}


/******************* OGL_FREE FONT : WINDOWS ***********************/

void OGL_FreeFont_Windows(int instanceNum)
{
	--------- PUT WINDOWS CODE HERE ------
}


/**************** OGL_DRAW STRING : WINDOWS ********************/
//
// s is a pascal string
//

void OGL_DrawString_Windows(int instanceNum, Str255 s, GLint x, GLint y)
{

	--------- PUT WINDOWS CODE HERE ------

}

#pragma mark -

/**************** READ JPEG AND DRAW : WINDOWS ***********************/
//
// Draws the JPEG pointed to by jpegData, and then converts the pixel buffer
// into an OpenGL texture.
//
// INPUT:	instanceNum = plug-in instance index
//			i			= cube face # 0..5
//			jpegData	= ptr to jpeg data
//

OSErr	JPEGToTexture_Windows(int instanceNum, int	i, Ptr jpegData)
{
Rect	r;
OSErr	iErr;

				/* DECODE IT INTO TEMP BUFFER */
	
	iErr = DrawJPEGFromRAMIntoBuffer_Windows(jpegData, gVideoSampleSize[i], &gTempImageBuffer);
	if (iErr)
	{
		printf("SeekAndExtractImages:  DrawJPEGFromRAMIntoBuffer failed!\n"); 
		return(1);
	}
	
	CalcAllowedTextureSize(instanceNum, &gTempImageRect, &r);


				/* ALLOCATE BUFFER FOR TEXTURE */
				//
				// The image is loaded above, but that's not necessarily the right size
				// so, we'll resize it into this...
				//
				
	gMyInstances[instanceNum].textureBuffers[i] = malloc((int)r.right * (int)r.bottom * 4);		// alloc buffer to hold image with no padding
	if (gMyInstances[instanceNum].textureBuffers[i] == nil)
	{
		printf("SeekAndExtractImages: malloc failed!\n");
		return(1);
	}
	

			/* SCALE THE LOADED TEMP IMAGE DOWN INTO THE TEXTURE BUFFER & CREATE TEXTURE */
				
	gMyInstances[instanceNum].textureNames[i] = ScaleTempImageToTexture_Windows(gMyInstances[instanceNum].textureBuffers[i],
																		&r,
																		gMyInstances[instanceNum].compressTextures); 


	return(noErr);
}



/**************** DRAW JPEG FROM RAM INTO BUFFER ***********************/
//
// Decompresses a JPEG who's compressed data is in RAM.
//
//

OSErr DrawJPEGFromRAMIntoBuffer_Windows(Ptr jpegData, int size, Ptr *buffer)
{
		
	--------- PUT WINDOWS CODE HERE ------
		
	return(noErr);
}



/******************** SCALE TEMP IMAGE TO TEXTURE **************************/
//
// Draws the image in gTempImageBuffer into the input buffer while resizing it.
//
// INPUT:	buffer = buffer to draw into.
//			textureRect = buffer's rect
//			compressTextures = flag to use hardware compression or not.
//

GLint ScaleTempImageToTexture_Windows(Ptr	buffer, Rect *textureRect, Boolean compressTextures)
{
GLint		textureName;

		/*******************************************/
		/* SCALE IMAGE FROM TEMP BUFFER TO TEXTURE */
		/*******************************************/
	
	
	CopyPixelsFromBufferToBuffer_Windows(gTempImageBuffer, buffer, &gTempImageRect, textureRect);

			
			/* FREE THE TEMP BUFFER */
			
	if (gTempImageBuffer)
	{
		free(gTempImageBuffer);
		gTempImageBuffer = nil;
	}
	
	textureName = MakeTextureFromBuffer(gMyInstances[instanceNum].nonPower2, buffer,
									textureRect->right, textureRect->bottom, false, compressTextures);

	return(textureName);
}



/**************** DRAW TIFF INTO GWORLD : WINDOWS ***********************/

OSErr DrawTIFFIntoBuffer_Windows(char *tiffPath, Ptr *buffer)
{
	--------- PUT WINDOWS CODE HERE ------
	
	
	return(noErr);
}


#pragma mark -

/******************** COPY PIXELS FROM BUFFER TO BUFFER ******************/
//
// Copy GWorld with designated Rect
//

void CopyPixelsFromBufferToBuffer_Windows(Ptr fromBuff, Ptr toBuff, const Rect *fromRect, const Rect *toRect)
{
	--------- PUT WINDOWS CODE HERE ------
}



#pragma mark -

/**************** HANDLE BROWSER EVENT - WINDOWS ******************/


void HandleBrowserEvent_Windows(int i, void *event)
{
	--------- PUT WINDOWS CODE HERE ------


}



#endif


