Пример #1
0
/********************************************************************
* Function : WinMain()
* Purpose : Mandatory Windows Init function.
********************************************************************/
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
						 LPSTR lpCmdLine, int nCmdShow)
{
	MSG		msg;
	HWND		hwnd;
	WNDCLASS	wc;
	static char ClassName[] = "ChromeTestingFacility";
	DDSURFACEDESC	ddsd;
	DDSCAPS			ddscaps;
	HRESULT			ddreturn;
	int n;

	// Set all key booleans to FALSE, assume no key is pressed.
	bForwardKey = FALSE;
	bBackKey = FALSE;
	bLeftKey = FALSE;
	bRightKey = FALSE;
	nState = 0;
	nGauge = 0;

	lpCmdLine = lpCmdLine;
	hPrevInstance = hPrevInstance;
	RealTime = 0;	/* Start of using spacebar for frameflipping. */

	/* Register and realize our display window */
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WindowProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = NULL;
	wc.lpszMenuName = ClassName;
	wc.lpszClassName = ClassName;
	RegisterClass(&wc);

	/* Initialize our test world. */
	if (!InitWorld(XRES, YRES, Colormap))
	{	return FALSE;
	}

	/* Convert the Chrome colormap to a windows colormap. */
	for (n = 0; n < 256; n++)
	{
		WinColormap[n].peRed = (unsigned char)((Colormap[n] & 0xFF0000) >> 16);
		WinColormap[n].peGreen = (unsigned char)((Colormap[n] & 0xFF00) >> 8);
		WinColormap[n].peBlue = (unsigned char)((Colormap[n] & 0xFF));
		WinColormap[n].peFlags = 0;
	}
	/* Create a full screen window so that GDI won't ever be
	 * called. */
	hwnd = CreateWindowEx(WS_EX_TOPMOST,
								 ClassName,
								 ClassName,
								 WS_POPUP,
								 0,
								 0,
								 GetSystemMetrics(SM_CXSCREEN),
								 GetSystemMetrics(SM_CYSCREEN),
								 NULL,
								 NULL,
								 hInstance,
								 NULL);
	if (hwnd == NULL)
		return FALSE;
	
	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);
	SetFocus(hwnd);
	ShowCursor(FALSE);		/* Remove cursor to prevent GDI from writing. */

	/* Instanciate our DirectDraw object */
	ddreturn = DirectDrawCreate(NULL, &lpDirectDrawObject, NULL);
	if (ddreturn != DD_OK)
	{
		DestroyWindow(hwnd);
		return FALSE;
	}

	ddreturn = lpDirectDrawObject->SetCooperativeLevel(hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN| DDSCL_ALLOWMODEX);
	if (ddreturn != DD_OK)
	{
		DestroyWindow(hwnd);
		return FALSE;
	}

	/* Create a palette for the surfaces. */
	ddreturn = lpDirectDrawObject->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256 | DDPCAPS_INITIALIZE,
																(LPPALETTEENTRY)WinColormap,
																&lpPalette,
																NULL);
	if (ddreturn != DD_OK)
	{	DestroyWindow(hwnd);
		return FALSE;
	}

	/* Set the video mode to XRESxYRESx8. */
	ddreturn = lpDirectDrawObject->SetDisplayMode(XRES, YRES, 8);
	if (ddreturn != DD_OK)
	{	DestroyWindow(hwnd);
		return FALSE;
	}

	/* Create a default font for the application. */
	AppFont = CreateFont(11,
								0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
								ANSI_CHARSET,
								OUT_DEFAULT_PRECIS,
								CLIP_DEFAULT_PRECIS,
								NONANTIALIASED_QUALITY,
								VARIABLE_PITCH,
								"Comic Sans MS");

	/* Create the primary surface and one back buffer surface */
	ddsd.dwSize = sizeof(ddsd);
	ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
	ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
								 DDSCAPS_FLIP |
								 DDSCAPS_COMPLEX;
	ddsd.dwBackBufferCount = 1;
	ddreturn = lpDirectDrawObject->CreateSurface(&ddsd, &lpPrimary, NULL);

	if (ddreturn != DD_OK)
	{	DestroyWindow(hwnd);
		return FALSE;
	}

	ddreturn = lpPrimary->SetPalette(lpPalette);
	if (ddreturn != DD_OK)
	{	DestroyWindow(hwnd);
		return FALSE;
	}

	/* Get a surface pointer to our back buffer. */
	ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
	ddreturn = lpPrimary->GetAttachedSurface(&ddscaps, &lpBackbuffer);

	if (ddreturn != DD_OK)
	{	DestroyWindow(hwnd);
		return FALSE;
	}

/*	ddreturn = lpBackbuffer->SetPalette(lpPalette);
	if (ddreturn != DD_OK)
	{	DestroyWindow(hwnd);
		return FALSE;
	}
*/
	{	/* Clear the background once for both buffers so we don't get anoying flicker effect. */
		DDBLTFX	BltFx;
		BltFx.dwSize = sizeof(BltFx);
		BltFx.dwFillColor = 255;
		ddreturn = lpBackbuffer->Blt(NULL,
											  NULL,
											  NULL,
											  DDBLT_COLORFILL | DDBLT_WAIT,
											  &BltFx);
		BltFx.dwSize = sizeof(BltFx);
		BltFx.dwFillColor = 255;
		ddreturn = lpPrimary->Blt(NULL,
											  NULL,
											  NULL,
											  DDBLT_COLORFILL | DDBLT_WAIT,
											  &BltFx);
	}

	while (1)
	{	if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
		{	if (!GetMessage(&msg, NULL, 0, 0))
				return msg.wParam;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		} else
		{	if (ActiveApp && RealTime)
			{	// Simulation Iteration should go here.
				// Only do this when running Realtime.
				SimLoop();
			} else
			{	WaitMessage();
			}
		}
	}
}