⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gapiapp.cpp

📁 Windows mobile程序开发的GAPI示例程序,可用于游戏编程的参考
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
// GXSample.cpp : Pocket PC Game API Starfield sample.
//
// Mostly C in a C++ file.
// See the documentation for more information.


//
// Includes
//

#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include <wingdi.h>
#include <gx.h>
#include <winuserm.h>
#include <aygshell.h>
#include "bitmaps.h"

// Defines
//
// Some handy globals that are fine for a sample app.
//

GXDisplayProperties g_gxdp;		// GX struct
GXKeyList g_gxkl;				// GX struct
BOOL g_fPause = FALSE;			// paused or not
BOOL g_bFullScreen = TRUE;
BYTE *g_pMainBuffer = NULL;
HINSTANCE g_hInstance = 0;
HDC hdc;
HWND hWnd, hWndFullScreenButton, hWndDescriptionText;

DWORD g_dwLastMouseClick = 0;

const int gcBitsPerByte = 8;

#define		COLOR_WHITE 0xFFFF

#define		COLOR_RED_2BPP   0x3
#define		COLOR_GREEN_2BPP 0x2
#define		COLOR_BLUE_2BPP  0x1

#define		COLOR_RED_4BPP   0xF
#define		COLOR_GREEN_4BPP 0xA
#define		COLOR_BLUE_4BPP  0x5

#define		COLOR_RED_PALETTE   249
#define		COLOR_GREEN_PALETTE 250
#define		COLOR_BLUE_PALETTE  252

#define		COLOR_RED_444   0x0F00
#define		COLOR_GREEN_444 0x00F0
#define		COLOR_BLUE_444  0x000F

#define		COLOR_RED_555	0x7C00
#define		COLOR_GREEN_555 0x03E0
#define		COLOR_BLUE_555  0x001F

#define		COLOR_RED_565	0xF800
#define		COLOR_GREEN_565 0x07E0
#define		COLOR_BLUE_565  0x001F

#define		COLOR_RED_888	0x00FF0000
#define		COLOR_GREEN_888 0x0000FF00
#define		COLOR_BLUE_888  0x000000FF

#define		COLOR_SQUARES_TOP		80
#define		COLOR_SQUARES_BOTTOM	240
#define		COLOR_SQUARES_LEFT		40
#define		COLOR_SQUARES_RIGHT		200

#define		EXIT_TOP				0
#define		EXIT_BOTTOM				60
#define		EXIT_LEFT				0
#define		EXIT_RIGHT				60

#define		SWITCH_TASK_TOP			0
#define		SWITCH_TASK_BOTTOM		60
#define		SWITCH_TASK_LEFT		180
#define		SWITCH_TASK_RIGHT		240

#define		REBOUND_DELAY			500


BOOL g_bOrientation = PORTRAIT;


long	g_wKeyLoc[MAX_BUTTONS][2][MAX_ORIENTATION];		// ex : [UP][X][PORTRAIT]
BOOL	g_bKeyInverted[MAX_BUTTONS];

//
// Forward declarations.
//

ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass);
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
BOOL ClearScreen();
void DrawRect();
void FillLocations(BOOL);
void DrawButtons(BOOL);
void DrawBitmap(DWORD *, WORD, WORD, long, long, BOOL);
void ChangeDisplayMode(BOOL);

//
// Code Body
//

int WINAPI WinMain(
	HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPTSTR lpCmdLine,
	int nCmdShow
	)
{
	MSG msg;

	// Perform application initialization:

	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	// Main message loop.

	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}


	return msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//

ATOM MyRegisterClass(
	HINSTANCE hInstance, 
	LPTSTR szWindowClass
	)
{
	WNDCLASS	wc;

    wc.style			= CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc		= (WNDPROC) WndProc;
    wc.cbClsExtra		= 0;
    wc.cbWndExtra		= 0;
    wc.hInstance		= hInstance;
    wc.hIcon			= NULL;
    wc.hCursor			= 0;
    wc.hbrBackground	= (HBRUSH) GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName		= 0;
    wc.lpszClassName	= szWindowClass;

	return RegisterClass(&wc);
}

BOOL InitInstance(
	HINSTANCE hInstance, 
	int nCmdShow
	)
{
	TCHAR	szTitle[] = L"GXSample";			// The title bar text
	TCHAR	szWindowClass[] = L"GXSample";	// The window class name
	BOOL    bDRAMBuffer;
    INT iScreenWidth = GetSystemMetrics(SM_CXSCREEN);
	INT iScreenHeight = GetSystemMetrics(SM_CYSCREEN);

	// IMPORTANT:  see if this app is already running.  If so just bring
	// it to the front and quit.  All CE apps need to do this.

	g_hInstance = hInstance;

	hWnd = FindWindow(szWindowClass, szTitle);	
	if (hWnd)
	{
		SetForegroundWindow ((HWND) (((DWORD)hWnd) | 0x01));    
		return 0;
	} 

	MyRegisterClass(hInstance, szWindowClass);

	// In order to create a full screen app CreateWindow() needs to be
	// called with absolute coordinates that cover the entire display.
	// Using CW_USEDEFAULT will not work correctly.

	hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
		0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), 
		NULL, NULL, hInstance, NULL);

	if (!hWnd) 
	{	
		return FALSE;
	}


	if (GXOpenDisplay(hWnd, GX_FULLSCREEN) == 0) {
		return FALSE;				// we won't be able to draw.
	}

	bDRAMBuffer = GXIsDisplayDRAMBuffer();
	if (TRUE == bDRAMBuffer)
	{
		g_bFullScreen = FALSE;

        // on non-standard devices, this is a clipper
	    GXSetViewport(iScreenHeight/2-55, 50, 0, 0);

		hWndFullScreenButton = CreateWindow( _T("BUTTON"), _T("Full screen"),
			BS_CENTER | BS_PUSHBUTTON | WS_VISIBLE, iScreenWidth/2-50,
			iScreenHeight-40, 100, 30, hWnd, NULL, hInstance, NULL);


		hWndDescriptionText =  CreateWindow( _T("STATIC"), 
			_T("DRAM drawing device"), SS_LEFT | WS_VISIBLE, iScreenWidth/2-65,
			iScreenHeight-70, 200, 30, hWnd, NULL, hInstance, NULL);

	}

	GXOpenInput();

	g_gxdp = GXGetDisplayProperties();

	g_gxkl = GXGetDefaultKeys(GX_NORMALKEYS);
	FillLocations(PORTRAIT);

	g_gxkl = GXGetDefaultKeys(GX_LANDSCAPEKEYS);
	FillLocations(LANDSCAPE);

	g_gxkl = GXGetDefaultKeys(GX_NORMALKEYS);


	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//

LRESULT CALLBACK WndProc(
	HWND hWnd, 
	UINT message, 
	WPARAM wParam, 
	LPARAM lParam
	)
{
	PAINTSTRUCT ps;
	short vkKey;

	switch (message) {
		case WM_COMMAND:
        {
			if ((HWND)lParam == hWndFullScreenButton)
			{
				g_bFullScreen = TRUE;
				GXSetViewport(0, GetSystemMetrics(SM_CYSCREEN), 0, 0);
				ShowWindow(hWndFullScreenButton, SW_HIDE);
				ShowWindow(hWndDescriptionText, SW_HIDE);
				PostMessage(hWnd, WM_PAINT, 0, 0);
			}
		}
		break;

		case WM_CREATE:
			break;


		case WM_ACTIVATE:
		{
			//BOOL bActivating = LOWORD(wParam) != WA_INACTIVE;

			if (WA_INACTIVE == LOWORD(wParam))
			{
				// Always, always call GXSuspend() here.
				GXSuspend();
			}
			else
			{
				// And always call GXResume() here.
				GXResume();
				PostMessage(hWnd, WM_PAINT, 0, 0);
			}

			break;
		}

		case WM_KILLFOCUS:

			break;


		case WM_SETFOCUS:

			break;

		// If your app can only draw directly to the screen then you need to make
		// sure it is frontmost before drawing.

		case WM_PAINT:

			if (GetForegroundWindow() == hWnd) {
				hdc = BeginPaint(hWnd, &ps);
                g_pMainBuffer = (BYTE*)GXBeginDraw();
				if(NULL != g_pMainBuffer) 
				{
					ClearScreen();
					DrawRect();
					DrawButtons(g_bOrientation);
	                GXEndDraw();
				}
				EndPaint(hWnd, &ps);
			}
			break;

		case WM_KEYDOWN:
			vkKey = (short)wParam;

			if (vkKey == g_gxkl.vkUp) 
			{
				if(TRUE != g_bKeyInverted[UP])
				{
					g_bKeyInverted[UP] = TRUE;
					PostMessage(hWnd, WM_PAINT, 0, 0);
				}
				break;
			}
			else if (vkKey == g_gxkl.vkDown) 
			{
				if(TRUE != g_bKeyInverted[DOWN])
				{
				g_bKeyInverted[DOWN] = TRUE;
				PostMessage(hWnd, WM_PAINT, 0, 0);
				}
				break;
			}
			
			if (vkKey == g_gxkl.vkLeft) {
				if(TRUE != g_bKeyInverted[LEFT])
				{
				g_bKeyInverted[LEFT] = TRUE;
				PostMessage(hWnd, WM_PAINT, 0, 0);
				}
				break;
			}
			
			if (vkKey == g_gxkl.vkRight) {
				if(TRUE != g_bKeyInverted[RIGHT])
				{
				g_bKeyInverted[RIGHT] = TRUE;
				PostMessage(hWnd, WM_PAINT, 0, 0);
				}
				break;
			}
			
			if (vkKey == g_gxkl.vkA) {
				if(TRUE != g_bKeyInverted[A])
				{
				g_bKeyInverted[A] = TRUE;
				PostMessage(hWnd, WM_PAINT, 0, 0);
				}
				break;
			}
			
			if (vkKey == g_gxkl.vkB) {
				if(TRUE != g_bKeyInverted[B])
				{
				g_bKeyInverted[B] = TRUE;
				PostMessage(hWnd, WM_PAINT, 0, 0);
				}
				break;
			}
			
			if (vkKey == g_gxkl.vkC) {
				if(TRUE != g_bKeyInverted[C])
				{
				g_bKeyInverted[C] = TRUE;
				PostMessage(hWnd, WM_PAINT, 0, 0);
				}
				break;
			}

			if (vkKey == g_gxkl.vkStart) {
				if(TRUE != g_bKeyInverted[START])
				{
				g_bKeyInverted[START] = TRUE;
				PostMessage(hWnd, WM_PAINT, 0, 0);
				}
				break;
			}

#ifdef WIN32_PLATFORM_WFSP //Smartphone platform define
			if (vkKey == VK_TTALK)
			{
				ChangeDisplayMode(!g_bOrientation);
	            PostMessage(hWnd, WM_PAINT, 0, 0);
				break;
			}
			
			if (vkKey == VK_TEND)
			{
	            PostMessage(hWnd, WM_CLOSE, 0,0);
	            break;
			}
			if (VK_TBACK == vkKey)
			{
				SHNavigateBack();
				break;
			}

			if (VK_THOME == vkKey)
			{
				// Put home in foreground
				HWND hwnd = FindWindow(TEXT("DesktopExplorerWindow"), NULL);
				if((NULL != hwnd) && (TRUE == IsWindow(hwnd)))
				{
					ShowWindow(hwnd, SW_SHOWNA);
					SetForegroundWindow((HWND)(((DWORD)((HWND)hwnd)) | 0x01));					
				}
				break;
			}
#endif //WIN32_PLATFORM_WFSP

			break;

		case WM_KEYUP:
			vkKey = (short)wParam;
			if (vkKey == g_gxkl.vkUp) {
				if(FALSE != g_bKeyInverted[UP])
				{
					g_bKeyInverted[UP] = FALSE;
					PostMessage(hWnd, WM_PAINT, 0, 0);
				}
				break;
			}

			if (vkKey == g_gxkl.vkDown) {
				if(FALSE != g_bKeyInverted[DOWN])
				{
				g_bKeyInverted[DOWN] = FALSE;
				PostMessage(hWnd, WM_PAINT, 0, 0);
				}
				break;
			}

			if (vkKey == g_gxkl.vkLeft) {
				if(FALSE != g_bKeyInverted[LEFT])
				{

				g_bKeyInverted[LEFT] = FALSE;
				PostMessage(hWnd, WM_PAINT, 0, 0);
				}
				break;
			}

			if (vkKey == g_gxkl.vkRight) {
				if(FALSE != g_bKeyInverted[RIGHT])
				{
				g_bKeyInverted[RIGHT] = FALSE;
				PostMessage(hWnd, WM_PAINT, 0, 0);
				}
				break;
			}

			if (vkKey == g_gxkl.vkA) {
				if(FALSE != g_bKeyInverted[A])
				{
				g_bKeyInverted[A] = FALSE;
				PostMessage(hWnd, WM_PAINT, 0, 0);
				}
				break;
			}

			if (vkKey == g_gxkl.vkB) {
				if(FALSE != g_bKeyInverted[B])
				{
				g_bKeyInverted[B] = FALSE;
				PostMessage(hWnd, WM_PAINT, 0, 0);
				}
				break;
			}

			if (vkKey == g_gxkl.vkC) {
				if(FALSE != g_bKeyInverted[C])
				{
				g_bKeyInverted[C] = FALSE;
				PostMessage(hWnd, WM_PAINT, 0, 0);
				}
				break;
			}

			if (vkKey == g_gxkl.vkStart) {
				if(FALSE != g_bKeyInverted[START])
				{
				g_bKeyInverted[START] = FALSE;
				PostMessage(hWnd, WM_PAINT, 0, 0);
				}
				break;
			}
			break;

		case WM_LBUTTONDOWN:

			break;

		case WM_LBUTTONUP:

			// Turn the timer back on.

			break;

		case WM_MOUSEMOVE:
			if (wParam & MK_LBUTTON)
			{
				WORD xPos = LOWORD(lParam); 
				WORD yPos = HIWORD(lParam);

				if ((xPos >= COLOR_SQUARES_LEFT) && (xPos <= COLOR_SQUARES_RIGHT) &&
					(yPos >= COLOR_SQUARES_TOP)  && (yPos <= COLOR_SQUARES_BOTTOM))
				{
					DWORD l_dwTemp = GetTickCount();
					//Only allow sytlus to rotate every REBOUND_DELAY ms
					if (l_dwTemp - g_dwLastMouseClick > REBOUND_DELAY)
					{
						ChangeDisplayMode(!g_bOrientation);
						PostMessage(hWnd, WM_PAINT, 0, 0);
						g_dwLastMouseClick = l_dwTemp;
					}
				}
				else if ((xPos >= EXIT_LEFT) && (xPos <= EXIT_RIGHT) &&
					(yPos >= EXIT_TOP)  && (yPos <= EXIT_BOTTOM))
				{
					PostMessage(hWnd, WM_CLOSE, 0,0);
				}
				else if ((xPos >= SWITCH_TASK_LEFT) && (xPos <= SWITCH_TASK_RIGHT) &&
					(yPos >= SWITCH_TASK_TOP)  && (yPos <= SWITCH_TASK_BOTTOM))
				{
					MessageBox(NULL, _T("GX OEM Test 1.0"),_T("About"), MB_OK);
				}				
			}
			break;

		case WM_DESTROY:
			GXCloseInput();			
			GXCloseDisplay();
			PostQuitMessage(0);
			break;

		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
	}

	return 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -