#include <windows.h>
#include <ddraw.h>
#include "main.h"
#include "video.h"
#include "ddutil.h"


objEntryClass ::objEntryClass()
{
	objRect.top = 0;
	objRect.bottom = 0;
	objRect.left = 0;
	objRect.right = 0;
}

objEntryClass ::~objEntryClass()
{
}

void objEntryClass ::setRect(RECT * passRect)
{
	objRect.top = passRect->top;
	objRect.bottom = passRect ->bottom;
	objRect.left = passRect ->left;
	objRect.right = passRect ->right;
}

RECT * objEntryClass ::getRect()
{
	return (&objRect);
}

int objEntryClass ::getWidth()
{
	return objRect.right - objRect.left;
}

int objEntryClass ::getHeight()
{
	return objRect.bottom - objRect.top;
}


videoEngineClass :: videoEngineClass(int passWidth, int passHeight, int passDepth)
{
	hWnd = NULL;
	g_pDD = NULL;
	gammaControl = NULL;
	frontSurface = NULL;
	backSurface = NULL;
	tileSurface = NULL;
	cursorSurface[0] = NULL;
	cursorSurface[1] = NULL;

	palette = NULL;
	timerID = 1;
	timerRate = 20;
	
	vidWidth = passWidth;
	vidHeight = passHeight;
	vidDepth = passDepth;
}

videoEngineClass :: ~videoEngineClass()
{
	//Free all the surfaces

	if (frontSurface != NULL)
	{
		frontSurface ->Release();
		frontSurface = NULL;
	}

	if (backSurface != NULL)
	{
		backSurface ->Release();
		backSurface = NULL;
	}

	if (tileSurface != NULL)
	{
		tileSurface ->Release();
		tileSurface = NULL;
	}

	if (cursorSurface[0] != NULL)
	{
		cursorSurface[0] ->Release();
		cursorSurface[0] = NULL;
	}

	if (cursorSurface[1] != NULL)
	{
		cursorSurface[1] ->Release();
		cursorSurface[1] = NULL;
	}


	if (palette != NULL)
	{
		palette ->Release();
		palette = NULL;
	}

	if (gammaControl != NULL)
	{
		gammaControl ->Release();
		gammaControl = NULL;
	}

	if (g_pDD != NULL)
	{
		g_pDD ->Release();
		g_pDD = NULL;
	}
}


void videoEngineClass ::blankBack()
{
	// First clear background so we dont get weird flashing
	HDC tempHDC;
	HBRUSH brush;

	backSurface ->GetDC(&tempHDC);

	brush = CreateSolidBrush(RGB(0,0,0));
	SelectObject(tempHDC, brush);
	Rectangle(tempHDC, 0, 0, vidWidth, vidHeight);

	backSurface ->ReleaseDC(tempHDC);

}

int videoEngineClass ::initVideo(HINSTANCE passInstance, int passCmd)
{
   
	HRESULT hReturn;
	DDSURFACEDESC2 ddsd;
	DDSCAPS2 ddscaps;
	WNDCLASS wc;


	 // Set up and register window class
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = passInstance;
    wc.hIcon = LoadIcon(passInstance, MAKEINTRESOURCE(IDI_MAIN_ICON));
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH )GetStockObject(BLACK_BRUSH);
    wc.lpszMenuName = "A Tree House Adventure";
    wc.lpszClassName = "A Tree House Adventure";
    RegisterClass(&wc);

    // Create a window
    hWnd = CreateWindowEx(WS_EX_TOPMOST,
                          "A Tree House Adventure",
                          "A Tree House Adventure",
                          WS_POPUP,
                          0,
                          0,
                          GetSystemMetrics(SM_CXSCREEN),
                          GetSystemMetrics(SM_CYSCREEN),
                          NULL,
                          NULL,
                          passInstance,
                          NULL);

	if (!hWnd)
	{
		MessageBox(NULL, "Error: Could not create a window.", "Error!", MB_OK);
		return 1;
	}

	ShowWindow(hWnd, passCmd);
	UpdateWindow(hWnd);
	SetFocus(hWnd);

	hReturn = DirectDrawCreateEx(NULL, (void **)&g_pDD, IID_IDirectDraw7, NULL);
	if (hReturn != DD_OK)
	{
		MessageBox(hWnd, "Error: Could not initialize Direct Draw", "Error!", MB_OK);
		return 1;
	}

	
	// We'll try to get exclusive mode, but it doesnt matter either way.
	hReturn = g_pDD ->SetCooperativeLevel(hWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
	
	hReturn = g_pDD ->SetDisplayMode(vidWidth, vidHeight, vidDepth, 0, 0);
	if (hReturn != DD_OK)
	{
		MessageBox(hWnd, "Error: Could not set screen mode", "Error!", MB_OK);
		return 1;
	}

	ZeroMemory(&ddsd, sizeof(ddsd));
	ddsd.dwSize = sizeof(ddsd);
	ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP |
                          DDSCAPS_COMPLEX;
    ddsd.dwBackBufferCount = 1;

	hReturn = g_pDD ->CreateSurface(&ddsd, &frontSurface, NULL);
	if (hReturn != DD_OK)
	{
		MessageBox(hWnd, "Error: Could not create front surface", "Error!", MB_OK);
		return 1;
	}

    ZeroMemory(&ddscaps, sizeof(ddscaps));
    ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
    hReturn = frontSurface->GetAttachedSurface(&ddscaps, &backSurface);
    if (hReturn != DD_OK)
    {
		MessageBox(hWnd, "Error: Could not attach back surface", "Error!", MB_OK);
		return 1;
	}


	// Create and set the palette
    palette = DDLoadPalette(g_pDD, GRAPHICS1_DATA);
    if (palette)
        frontSurface->SetPalette(palette);
	else
	{
		MessageBox(hWnd, "Error: Could not set palette", "Error!", MB_OK);
		return 1;
	}


	//Lets load our tile surface
	tileSurface = DDLoadBitmap(g_pDD, "tiles.bmp", 0, 0, hWnd);
	if (tileSurface == NULL)
	{
		MessageBox(hWnd, "Error: Could not load tile data", "Error!", MB_OK);
		return 1;
	}

	//Now our object surface
	objectSurface = DDLoadBitmap(g_pDD, "objects.bmp", 0, 0, hWnd);
	if (objectSurface == NULL)
	{
		MessageBox(hWnd, "Error: Could not load object data", "Error!", MB_OK);
		return 1;
	}


	//Now our cursor surface
	cursorSurface[0] = DDLoadBitmap(g_pDD, "cursors.bmp", 0, 0, hWnd);
	if (cursorSurface == NULL)
	{
		MessageBox(hWnd, "Error: Could not cursor data", "Error!", MB_OK);
		return 1;
	}
	cursorSurface[1] = DDLoadBitmap(g_pDD, "cursors.bmp", 0, 0, hWnd);
	if (cursorSurface == NULL)
	{
		MessageBox(hWnd, "Error: Could not cursor data", "Error!", MB_OK);
		return 1;
	}



	//Set Color Keys
	DDSetColorKey(tileSurface, RGB(255,255,255), hWnd);
	DDSetColorKey(objectSurface, RGB(255,255,255), hWnd);
	DDSetColorKey(cursorSurface[0], RGB(255,255,255), hWnd);

	//Now the gamma controller for fading
	frontSurface ->QueryInterface(IID_IDirectDrawGammaControl, (void **)& gammaControl);

	//Now set timer
	SetTimer(hWnd, timerID, 1000 / timerRate, NULL);

	return 0;
}

void videoEngineClass ::update()
{
	HRESULT hResult;

/*	crap++;

	if ((crap >= 0) && (crap < 2))
		drawObject(150, 280, 2, 1);
	else if ((crap >= 2) && (crap < 4))
		drawObject(150,280, 2, 0);
	else if ((crap >= 4) && (crap < 6))
		drawObject(150, 280, 2, 2);
	else 
		drawObject(150, 280, 2, 0);

	if (crap > 8)
		crap = 0;
*/
	while (1)
	{
		hResult = frontSurface->Flip(NULL, 0);
		if (hResult == DD_OK)
			break;

		if (hResult == DDERR_SURFACELOST)
		{
			hResult = frontSurface ->Restore();
			if (hResult != DD_OK)
				break;
		}

		if (hResult != DDERR_WASSTILLDRAWING)
			break;
	}

	drawCursor(0);

}

void videoEngineClass ::loadCursorset()
{
	RECT tempRect;

	tempRect.top = 0;
	tempRect.left = 0;
	tempRect.right = 16;
	tempRect.bottom = 15;

	cursorArray[0].setRect(&tempRect);
}

void videoEngineClass ::loadTileset()
{
	RECT tempRect;

	tempRect.top = 1;
	tempRect.left = 1;
	tempRect.right = 25;
	tempRect.bottom = 20;

	tileArray[0].setRect(&tempRect);

	tempRect.top = 1;
	tempRect.left = 26;
	tempRect.right = 50;
	tempRect.bottom = 20;

	tileArray[1].setRect(&tempRect);

	tempRect.top = 1;
	tempRect.left = 76;
	tempRect.right = 100;
	tempRect.bottom = 20;

	tileArray[3].setRect(&tempRect);

	tempRect.top = 1;
	tempRect.left = 101;
	tempRect.right = 125;
	tempRect.bottom = 20;

	tileArray[4].setRect(&tempRect);

	tempRect.top = 21;
	tempRect.left = 1;
	tempRect.right = 25;
	tempRect.bottom = 41;

	tileArray[6].setRect(&tempRect);

	tempRect.top = 21;
	tempRect.left = 26;
	tempRect.right = 50;
	tempRect.bottom = 41;

	tileArray[7].setRect(&tempRect);

	tempRect.top = 21;
	tempRect.left = 76;
	tempRect.right = 100;
	tempRect.bottom = 41;

	tileArray[9].setRect(&tempRect);

	tempRect.top = 21;
	tempRect.left = 101;
	tempRect.right = 125;
	tempRect.bottom = 41;

	tileArray[10].setRect(&tempRect);

	tempRect.top = 42;
	tempRect.left = 1;
	tempRect.right = 25;
	tempRect.bottom = 62;

	tileArray[12].setRect(&tempRect);

	tempRect.top = 42;
	tempRect.left = 26;
	tempRect.right = 50;
	tempRect.bottom = 62;

	tileArray[13].setRect(&tempRect);

	tempRect.top = 42;
	tempRect.left = 76;
	tempRect.right = 100;
	tempRect.bottom = 62;

	tileArray[15].setRect(&tempRect);

	tempRect.top = 42;
	tempRect.left = 101;
	tempRect.right = 125;
	tempRect.bottom = 62;

	tileArray[16].setRect(&tempRect);

	tempRect.top = 63;
	tempRect.left = 1;
	tempRect.right = 25;
	tempRect.bottom = 83;

	tileArray[18].setRect(&tempRect);

	tempRect.top = 63;
	tempRect.left = 26;
	tempRect.right = 50;
	tempRect.bottom = 83;

	tileArray[19].setRect(&tempRect);

	tempRect.top = 63;
	tempRect.left = 76;
	tempRect.right = 100;
	tempRect.bottom = 83;

	tileArray[21].setRect(&tempRect);

	tempRect.top = 63;
	tempRect.left = 101;
	tempRect.right = 125;
	tempRect.bottom = 83;

	tileArray[22].setRect(&tempRect);

	tempRect.top = 84;
	tempRect.left = 1;
	tempRect.right = 25;
	tempRect.bottom = 104;

	tileArray[24].setRect(&tempRect);

	tempRect.top = 84;
	tempRect.left = 26;
	tempRect.right = 50;
	tempRect.bottom = 104;

	tileArray[25].setRect(&tempRect);

	tempRect.top = 84;
	tempRect.left = 76;
	tempRect.right = 100;
	tempRect.bottom = 104;

	tileArray[27].setRect(&tempRect);

	tempRect.top = 84;
	tempRect.left = 101;
	tempRect.right = 125;
	tempRect.bottom = 104;

	tileArray[28].setRect(&tempRect);

	tempRect.top = 105;
	tempRect.left = 1;
	tempRect.right = 25;
	tempRect.bottom = 125;

	tileArray[30].setRect(&tempRect);

	tempRect.top = 105;
	tempRect.left = 26;
	tempRect.right = 50;
	tempRect.bottom = 125;

	tileArray[31].setRect(&tempRect);

	tempRect.top = 105;
	tempRect.left = 76;
	tempRect.right = 100;
	tempRect.bottom = 125;

	tileArray[33].setRect(&tempRect);

	tempRect.top = 105;
	tempRect.left = 101;
	tempRect.right = 125;
	tempRect.bottom = 125;

	tileArray[34].setRect(&tempRect);

	tempRect.top = 126;
	tempRect.left = 1;
	tempRect.right = 25;
	tempRect.bottom = 146;

	tileArray[36].setRect(&tempRect);

	tempRect.top = 126;
	tempRect.left = 26;
	tempRect.right = 50;
	tempRect.bottom = 146;

	tileArray[37].setRect(&tempRect);

	tempRect.top = 126;
	tempRect.left = 76;
	tempRect.right = 100;
	tempRect.bottom = 146;

	tileArray[39].setRect(&tempRect);

	tempRect.top = 126;
	tempRect.left = 101;
	tempRect.right = 125;
	tempRect.bottom = 146;

	tileArray[40].setRect(&tempRect);

	tempRect.top = 147;
	tempRect.left = 1;
	tempRect.right = 25;
	tempRect.bottom = 167;

	tileArray[42].setRect(&tempRect);

	tempRect.top = 147;
	tempRect.left = 26;
	tempRect.right = 50;
	tempRect.bottom = 167;

	tileArray[43].setRect(&tempRect);

	tempRect.top = 147;
	tempRect.left = 76;
	tempRect.right = 100;
	tempRect.bottom = 167;

	tileArray[45].setRect(&tempRect);

	tempRect.top = 147;
	tempRect.left = 101;
	tempRect.right = 125;
	tempRect.bottom = 167;

	tileArray[46].setRect(&tempRect);

	tempRect.top = 168;
	tempRect.left = 1;
	tempRect.right = 25;
	tempRect.bottom = 188;

	tileArray[48].setRect(&tempRect);

	tempRect.top = 168;
	tempRect.left = 26;
	tempRect.right = 50;
	tempRect.bottom = 188;

	tileArray[49].setRect(&tempRect);

	tempRect.top = 168;
	tempRect.left = 76;
	tempRect.right = 100;
	tempRect.bottom = 188;

	tileArray[51].setRect(&tempRect);

	tempRect.top = 168;
	tempRect.left = 101;
	tempRect.right = 125;
	tempRect.bottom = 188;

	tileArray[52].setRect(&tempRect);


	tempRect.top = 189;
	tempRect.left = 1;
	tempRect.right = 25;
	tempRect.bottom = 209;

	tileArray[54].setRect(&tempRect);

	tempRect.top = 189;
	tempRect.left = 26;
	tempRect.right = 50;
	tempRect.bottom = 209;

	tileArray[55].setRect(&tempRect);

	tempRect.top = 189;
	tempRect.left = 76;
	tempRect.right = 100;
	tempRect.bottom = 209;

	tileArray[57].setRect(&tempRect);

	tempRect.top = 189;
	tempRect.left = 101;
	tempRect.right = 125;
	tempRect.bottom = 209;

	tileArray[58].setRect(&tempRect);

	tempRect.top = 210;
	tempRect.left = 1;
	tempRect.right = 25;
	tempRect.bottom = 230;

	tileArray[60].setRect(&tempRect);

	tempRect.top = 210;
	tempRect.left = 26;
	tempRect.right = 50;
	tempRect.bottom = 230;

	tileArray[61].setRect(&tempRect);

	tempRect.top = 210;
	tempRect.left = 76;
	tempRect.right = 100;
	tempRect.bottom = 230;

	tileArray[63].setRect(&tempRect);

	tempRect.top = 210;
	tempRect.left = 101;
	tempRect.right = 125;
	tempRect.bottom = 230;

	tileArray[64].setRect(&tempRect);

	tempRect.top = 231;
	tempRect.left = 1;
	tempRect.right = 25;
	tempRect.bottom = 251;

	tileArray[66].setRect(&tempRect);

	tempRect.top = 231;
	tempRect.left = 26;
	tempRect.right = 50;
	tempRect.bottom = 251;

	tileArray[67].setRect(&tempRect);

	tempRect.top = 231;
	tempRect.left = 76;
	tempRect.right = 100;
	tempRect.bottom = 251;

	tileArray[69].setRect(&tempRect);

	tempRect.top = 231;
	tempRect.left = 101;
	tempRect.right = 125;
	tempRect.bottom = 251;

	tileArray[70].setRect(&tempRect);

	tempRect.top = 252;
	tempRect.left = 1;
	tempRect.right = 25;
	tempRect.bottom = 272;

	tileArray[72].setRect(&tempRect);

	tempRect.top = 252;
	tempRect.left = 26;
	tempRect.right = 50;
	tempRect.bottom = 272;

	tileArray[73].setRect(&tempRect);

	tempRect.top = 252;
	tempRect.left = 76;
	tempRect.right = 100;
	tempRect.bottom = 272;

	tileArray[75].setRect(&tempRect);

	tempRect.top = 252;
	tempRect.left = 101;
	tempRect.right = 125;
	tempRect.bottom = 272;

	tileArray[76].setRect(&tempRect);

	tempRect.top = 273;
	tempRect.left = 1;
	tempRect.right = 25;
	tempRect.bottom = 293;

	tileArray[78].setRect(&tempRect);

	tempRect.top = 273;
	tempRect.left = 26;
	tempRect.right = 50;
	tempRect.bottom = 293;

	tileArray[79].setRect(&tempRect);

	tempRect.top = 273;
	tempRect.left = 76;
	tempRect.right = 100;
	tempRect.bottom = 293;

	tileArray[81].setRect(&tempRect);

	tempRect.top = 273;
	tempRect.left = 101;
	tempRect.right = 125;
	tempRect.bottom = 293;

	tileArray[82].setRect(&tempRect);

	tempRect.top = 294;
	tempRect.left = 1;
	tempRect.right = 25;
	tempRect.bottom = 314;

	tileArray[84].setRect(&tempRect);

	tempRect.top = 294;
	tempRect.left = 26;
	tempRect.right = 50;
	tempRect.bottom = 314;

	tileArray[85].setRect(&tempRect);

	tempRect.top = 294;
	tempRect.left = 76;
	tempRect.right = 100;
	tempRect.bottom = 314;

	tileArray[87].setRect(&tempRect);

	tempRect.top = 294;
	tempRect.left = 101;
	tempRect.right = 125;
	tempRect.bottom = 314;

	tileArray[88].setRect(&tempRect);


	tempRect.top = 315;
	tempRect.left = 1;
	tempRect.right = 25;
	tempRect.bottom = 335;

	tileArray[90].setRect(&tempRect);

	tempRect.top = 315;
	tempRect.left = 26;
	tempRect.right = 50;
	tempRect.bottom = 335;

	tileArray[91].setRect(&tempRect);

	tempRect.top = 315;
	tempRect.left = 76;
	tempRect.right = 100;
	tempRect.bottom = 335;

	tileArray[93].setRect(&tempRect);

	tempRect.top = 315;
	tempRect.left = 101;
	tempRect.right = 125;
	tempRect.bottom = 335;

	tileArray[94].setRect(&tempRect);

	tempRect.top = 336;
	tempRect.left = 1;
	tempRect.right = 25;
	tempRect.bottom = 356;

	tileArray[96].setRect(&tempRect);

	tempRect.top = 336;
	tempRect.left = 26;
	tempRect.right = 50;
	tempRect.bottom = 356;

	tileArray[97].setRect(&tempRect);

	tempRect.top = 336;
	tempRect.left = 76;
	tempRect.right = 100;
	tempRect.bottom = 356;

	tileArray[99].setRect(&tempRect);

	tempRect.top = 336;
	tempRect.left = 101;
	tempRect.right = 125;
	tempRect.bottom = 356;

	tileArray[100].setRect(&tempRect);

}

void videoEngineClass ::loadObjectset()
{
	RECT tempRect;

	tempRect.top = 1;
	tempRect.left = 1;
	tempRect.right = 21;
	tempRect.bottom = 50;

	objectArray[0].setRect(&tempRect);

	tempRect.top = 1;
	tempRect.left = 22;
	tempRect.right = 43;
	tempRect.bottom = 50;

	objectArray[1].setRect(&tempRect);

	tempRect.top = 1;
	tempRect.left = 44;
	tempRect.right = 68;
	tempRect.bottom = 50;

	objectArray[2].setRect(&tempRect);

	tempRect.top = 51;
	tempRect.left = 1;
	tempRect.right = 37;
	tempRect.bottom = 263;

	objectArray[3].setRect(&tempRect);


}

void videoEngineClass ::drawObject(int passX, int passY, int passTable, int passIndex)
{
	RECT *objRect;
	RECT tempRect;
	int objWidth, objHeight;

	switch (passTable)
	{
		case 1:
			objWidth = (tileArray[passIndex]).getWidth();
			objHeight = (tileArray[passIndex]).getHeight();
			objRect = (tileArray[passIndex]).getRect();

			if (passX + objWidth < camX || passX > camX + vidWidth)
				return;

			if (passY + objHeight < camY || passY > camY + vidWidth)
				return;

			tempRect.top = passY < camY ? objRect->top + (camY - passY) : objRect ->top;
			tempRect.left = passX < camX ? objRect->left + (camX - passX) : objRect ->left;
			tempRect.right = passX + objWidth > camX + vidWidth ? objRect->right - ((passX + objWidth) - (camX + vidWidth)) : objRect ->right;
			tempRect.bottom = passY + objHeight > camY + vidHeight ? objRect->bottom - ((passY + objHeight) - (camY + vidHeight)) : objRect ->bottom;
			
			backSurface ->BltFast(passX > camX ? passX - camX : 0, passY > camY ? passY - camY : 0, tileSurface, &tempRect, DDBLTFAST_SRCCOLORKEY);

			break;

		case 2:
			objWidth = (objectArray[passIndex]).getWidth();
			objHeight = (objectArray[passIndex]).getHeight();
			objRect = (objectArray[passIndex]).getRect();

			if (passX + objWidth < camX || passX > camX + vidWidth)
				return;

			if (passY + objHeight < camY || passY > camY + vidWidth)
				return;

			tempRect.top = passY < camY ? objRect->top + (camY - passY) : objRect ->top;
			tempRect.left = passX < camX ? objRect->left + (camX - passX) : objRect ->left;
			tempRect.right = passX + objWidth > camX + vidWidth ? objRect->right - ((passX + objWidth) - (camX + vidWidth)) : objRect ->right;
			tempRect.bottom = passY + objHeight > camY + vidHeight ? objRect->bottom - ((passY + objHeight) - (camY + vidHeight)) : objRect ->bottom;
			
			backSurface ->BltFast(passX > camX ? passX - camX : 0, passY > camY ? passY - camY : 0, objectSurface, &tempRect, DDBLTFAST_SRCCOLORKEY);

			break;		
	}
}

void videoEngineClass ::setCursor(int passCursor)
{
	cursor = passCursor;
}

void videoEngineClass ::drawCursor(int passUseBuffer)
{
	RECT *objRect;
	RECT restoreRect;
	int savedRight, savedBottom; //Needed for restoring after clipping

	restoreRect.top = *cursorY;
	restoreRect.left = *cursorX;
	restoreRect.bottom = *cursorY + 30;
	restoreRect.right = *cursorX + 30;


	objRect = cursorArray[cursor].getRect();
	savedRight = objRect ->right;
	savedBottom = objRect ->bottom;

	// Do a check to see if any part of the save buffer / cursor goes offscreen and clip
	if (restoreRect.right > getCameraWidth())
		restoreRect.right = getCameraWidth();
	if (restoreRect.bottom > getCameraHeight())
		restoreRect.bottom = getCameraHeight();

	if (*cursorX + objRect ->right > getCameraWidth())
		objRect ->right = getCameraWidth() - *cursorX;

	if (*cursorY + objRect ->bottom > getCameraHeight())
		objRect ->bottom = getCameraHeight() - *cursorY;

	if (passUseBuffer)
		frontSurface ->BltFast(cursorXOld, cursorYOld, cursorSurface[1], objRect, DDBLTFAST_NOCOLORKEY);
	cursorSurface[1] ->BltFast(0, 0, frontSurface, &restoreRect, DDBLTFAST_NOCOLORKEY);
	frontSurface ->BltFast(*cursorX, *cursorY, cursorSurface[0], objRect, DDBLTFAST_SRCCOLORKEY);
	cursorXOld = *cursorX;
	cursorYOld = *cursorY;

	objRect ->right = savedRight;
	objRect ->bottom = savedBottom;

}

void videoEngineClass ::setCursorPtrs(int * passXPtr, int * passYPtr)
{
	cursorX = passXPtr;
	cursorY = passYPtr;

	cursorXOld = *cursorX;
	cursorYOld = *cursorY;
}

void videoEngineClass ::setViewBorder(RECT passViewBorder)
{
	viewBorder.top = passViewBorder.top;
	viewBorder.bottom = passViewBorder.bottom;
	viewBorder.left = passViewBorder.left;
	viewBorder.right = passViewBorder.right;
}

void videoEngineClass ::setCamera(int passX, int passY)
{
	if (passX < viewBorder.left)
		passX = viewBorder.left;
	if (passY < viewBorder.top)
		passY = viewBorder.top;

	if (passX + vidWidth > viewBorder.right)
		passX = viewBorder.right - (vidWidth + 1);
	if (passY + vidHeight > viewBorder.bottom)
		passY = viewBorder.bottom - (vidHeight + 1);

	camX = passX;
	camY = passY;
}

int videoEngineClass ::getCameraX()
{
	return camX;
}

int videoEngineClass ::getCameraY()
{
	return camY;
}

int videoEngineClass ::getCameraHeight()
{
	return vidHeight;
}

int videoEngineClass ::getCameraWidth()
{
	return vidWidth;
}

void videoEngineClass ::getObjectCenter(int passIndex, int * retX, int * retY)
{
	*retX = 0 - ((int)objectArray[passIndex].getWidth() / 2);
	*retY = 0 - objectArray[passIndex].getHeight();
}

void videoEngineClass ::fade(int passMethod) 
{
	DDGAMMARAMP gammaRamp;
	DDGAMMARAMP oldGammaRamp;
	int done = false;
	int rate = 30;

	gammaControl ->GetGammaRamp(0, &oldGammaRamp);
	gammaControl ->GetGammaRamp(0, &gammaRamp);


	switch (passMethod)
	{
		case 0:
			while (!done)
			{
				done = true;
				for(int lcv = 0;lcv < 256 ;lcv++)
				{
					if (gammaRamp.red[lcv] > rate)
					{
						gammaRamp.red[lcv] = gammaRamp.red[lcv] - rate;
						done = false;
					}
					
					if (gammaRamp.green[lcv] > rate)
					{
						gammaRamp.green[lcv] = gammaRamp.green[lcv] - rate;
						done = false;
					}

					if (gammaRamp.blue[lcv] > rate)
					{
						gammaRamp.blue[lcv] = gammaRamp.blue[lcv] - rate;
						done = false;
					}

				}
					gammaControl ->SetGammaRamp(0, &gammaRamp);
				
			}
			break;
			
		case 1:

			while (!done)
			{
				done = true;
				for(int lcv = 0;lcv < 256 ;lcv++)
				{
					if (gammaRamp.red[lcv] < 65535 - rate)
					{
						gammaRamp.red[lcv] = gammaRamp.red[lcv] + rate;
						done = false;
					}
					
					if (gammaRamp.green[lcv] < 65535 - rate)
					{
						gammaRamp.green[lcv] = gammaRamp.green[lcv] + rate;
						done = false;
					}

					if (gammaRamp.blue[lcv] < 65535 - rate)
					{
						gammaRamp.blue[lcv] = gammaRamp.blue[lcv] + rate;
						done = false;
					}

				}
					gammaControl ->SetGammaRamp(0, &gammaRamp);
				
			}
			break;

		case 2:

			for (int lcv = 0 ; lcv < 256 ; lcv++)
			{
				gammaRamp.red[lcv] = 0;
				gammaRamp.green[lcv] = 0;
				gammaRamp.blue[lcv] = 0;
			}
			gammaControl ->SetGammaRamp(0, &gammaRamp);
			Sleep(500);
			while (!done)
			{
				done = true;
				for(int lcv = 0;lcv < 256 ;lcv++)
				{
					if (gammaRamp.red[lcv] < oldGammaRamp.red[lcv] - rate)
					{
						gammaRamp.red[lcv] = gammaRamp.red[lcv] + (oldGammaRamp.red[lcv]/2000)+1;
						done = false;
					}
					
					if (gammaRamp.green[lcv] < oldGammaRamp.green[lcv] - rate)
					{
						gammaRamp.green[lcv] = gammaRamp.green[lcv] + (oldGammaRamp.green[lcv]/2000)+1;
						done = false;
					}

					if (gammaRamp.blue[lcv] < oldGammaRamp.blue[lcv] - rate)
					{
						gammaRamp.blue[lcv] = gammaRamp.blue[lcv] + (oldGammaRamp.blue[lcv]/2000)+1;
						done = false;
					}

				}
					gammaControl ->SetGammaRamp(0, &gammaRamp);
					//Sleep(1);
				
			}
			break;

	}

}



void videoEngineClass ::test()
{



/*	char * videoMemory;
	DWORD Offset, Pixel;

	DDSURFACEDESC2 ddsd;
	ZeroMemory(&ddsd, sizeof(ddsd));
	ddsd.dwSize = sizeof(ddsd);
	//ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
	backSurface->Lock(NULL, &ddsd, DDLOCK_NOSYSLOCK, NULL);
	//videoMemory = (char *)ddsd.lpSurface;
	//pitch = ddsd.lPitch;

	for (int y = 150 ; y < 250 ; y++)
		for (int x = 150 ; x < 450 ; x++)
		{
			Offset = y * ddsd.lPitch + x * (ddsd.ddpfPixelFormat.dwRGBBitCount >> 3);
			Pixel = *((LPDWORD)((DWORD)ddsd.lpSurface+Offset));
			
			//halfPixel = 0x0000;
			//Pixel = (Pixel & 0xFFFF0000) ^ halfPixel;
			//Pixel = Pixel + 0x2108;
			unsigned char redBits, blueBits, greenBits;
			blueBits = 0 | (0x001F & Pixel);
			greenBits = 0 | ((0x03E0 & Pixel) >> 5);
			redBits = 0 | ((0x7C00 & Pixel) >> 10);

			blueBits += 0;
			if (blueBits > 31)
				blueBits = 31;

			greenBits += 0;
			if (greenBits > 31)
				greenBits = 31;

			redBits += 30;
			if (redBits > 31)
				redBits = 31;

			Pixel = Pixel | (0x00007C00 & (redBits << 10));
			Pixel = Pixel | (0x000003E0 & (greenBits << 5));
			Pixel = Pixel | (0x0000001F & (blueBits));

			*((LPDWORD)((DWORD)ddsd.lpSurface+Offset)) = Pixel;
		}

	backSurface ->Unlock(0);


*/		
/*	tempRect.top = 600;
	tempRect.left = 0;
	tempRect.bottom = 1200;
	tempRect.right = 800;

	tileArray[0].setRect(&tempRect);

	
	tempRect.top = 1200;
	tempRect.left = 0;
	tempRect.bottom = 1800;
	tempRect.right = 800;

	tileArray[1].setRect(&tempRect);


    tempRect.top = 0;
	tempRect.left = 0;
	tempRect.bottom = 38;
	tempRect.right = 64;

	tileArray[2].setRect(&tempRect);

	tempRect.top = 0;
	tempRect.left = 88;
	tempRect.right = 336;
	tempRect.bottom = 205;

	tileArray[3].setRect(&tempRect);

	tempRect.top = 0;
	tempRect.left = 361;
	tempRect.right = 388;
	tempRect.bottom = 24;

	tileArray[4].setRect(&tempRect);


    int testerX, testerY;
	int objX, objY;

//	for (testerY = 5 ; testerY < 25 ; testerY++)
//	{
//		for ( testerX = 100+1 ; testerX < 100+600 ; testerX += 27)
//			setBack(testerX, testerY * 24, 4);
//
//		for (testerX = 100+14 ; testerX < 100+650 ; testerX += 27)
//			setBack(testerX, testerY * 24 + 6, 4);
//	}
	
testerX = 1;
testerY = 2;

objX = 1;
objY = 300;

	while (1)
    {
		if (testerX)
			testerY++;
		else
			testerY--;

		if (testerY < 1)
			break;
		if (testerY > 600)
			testerX = 0;

		if (objX)
			objY -= 3;
		else
			objY += 3;

		if (objY < 200)
			objX = 0;
		if (objY > 300)
			objX = 1;

		setCamera(testerY,testerY);
		drawObject(200, objY, 1, 3);
		update();
	}

	for (int x = 0 ; x < 32000 ; x++)
		for (int y = 0 ; y < 10000 ; y++);
	
*/


}