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

📄 gbsample.cpp

📁 这是针对GAPI而编写的一个程序
💻 CPP
字号:
// gbsample.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "gbsample.h"
#include <commctrl.h>
#include <aygshell.h>
#include <sipapi.h>


#include "STGapiBuffer.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE			hInst;					// The current instance
HWND				hwndCB;					// The command bar handle

CNativeBitmap* pAsteroidBitmap = NULL;
CNativeBitmap* pAsteroidMask = NULL;

CSTGapiBuffer gapiBufferBackground;
CSTGapiBuffer gapiBufferMemory;
CSTGapiBuffer gapiBufferScreen;

DWORD dwTransparentColor = 0;
DWORD dwDispWidth = 0, dwDispHeight = 0;


static SHACTIVATEINFO s_sai;

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass	(HINSTANCE, LPTSTR);
BOOL				InitInstance	(HINSTANCE, int);
LRESULT CALLBACK	WndProc			(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About			(HWND, UINT, WPARAM, LPARAM);
HWND				CreateRpCommandBar(HWND);


void Initialize(HWND hWnd)
{
	dwDispWidth = gapiBufferScreen.GetDisplaySize().cx;
	dwDispHeight = gapiBufferScreen.GetDisplaySize().cy;

	// go to the full screen mode
	SHFullScreen(hWnd, SHFS_HIDETASKBAR | SHFS_HIDESTARTICON | SHFS_HIDESIPBUTTON);
	ShowWindow(hwndCB, SW_HIDE);
	MoveWindow(hWnd, 0, 0, dwDispWidth, dwDispHeight, FALSE);

	// initialize GAPI
	if (!GXOpenDisplay(hWnd, 1) || !GXOpenInput())
	{
		return;
	}


	// for best perfomance load the background in the separate offscreen buffer

	HBITMAP hBackground = ::LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BACKGROUND));
	CNativeBitmap* pBackgroundBitmap = gapiBufferMemory.CreateNativeBitmap(hBackground);
	::DeleteObject(hBackground);

	gapiBufferBackground.CreateMemoryBuffer();
	gapiBufferBackground.BitBlt(0, 0, dwDispWidth, dwDispHeight, pBackgroundBitmap);
	delete pBackgroundBitmap;
	pBackgroundBitmap = NULL;

	////////////////////////////////////
	// load bitmaps in the native format

	HBITMAP hAsteroid = ::LoadBitmap(hInst, MAKEINTRESOURCE(IDB_ASTEROID));
	pAsteroidBitmap = gapiBufferMemory.CreateNativeBitmap(hAsteroid);
	::DeleteObject(hAsteroid);

	HBITMAP hAsteroidMask = ::LoadBitmap(hInst, MAKEINTRESOURCE(IDB_ASTEROID_MASK));
	pAsteroidMask = gapiBufferMemory.CreateNativeBitmap(hAsteroidMask);
	::DeleteObject(hAsteroidMask);

	/////////////////////////////////////////
	// convert colors to the native format

	dwTransparentColor = gapiBufferMemory.GetNativeColor(RGB(249, 57, 198));

	// create an offscreen buffer
	gapiBufferMemory.CreateMemoryBuffer();
}


void Shutdown()
{
	GXCloseDisplay();
	GXCloseInput();

	delete pAsteroidBitmap;
	delete pAsteroidMask;
}

void FillRect(const RECT* pRc, COLORREF clr)
{
	DWORD dwNativeColor = gapiBufferMemory.GetNativeColor(clr);

	// draw a filled rect using high-perfomance methods
	// (current position conception)
	for (int y = pRc->top; y < pRc->bottom; y++)
	{
		gapiBufferMemory.SetPos(pRc->left, y);
		for (int x = pRc->left; x < pRc->right; x++)
		{
			gapiBufferMemory.SetPixel(dwNativeColor);
			gapiBufferMemory.IncXPos();
		}
	}

}

void DrawFrame(int nOffset)
{

	// draw to the offscreen buffer

	// background
	gapiBufferMemory.BitBlt(&gapiBufferBackground);


	///////////////////////////////////////////////////
	// draw 8 asteroids using 2 different technique

	// sprites with transparent color technique
	gapiBufferMemory.TransparentBlt(0, nOffset%dwDispHeight, 50, 60, pAsteroidBitmap, dwTransparentColor);
	gapiBufferMemory.TransparentBlt(100, (nOffset*2)%dwDispHeight, 50, 60, pAsteroidBitmap, dwTransparentColor);
	gapiBufferMemory.TransparentBlt(nOffset%dwDispWidth, 50, 50, 60, pAsteroidBitmap, dwTransparentColor);
	gapiBufferMemory.TransparentBlt((nOffset*2)%dwDispWidth, 150, 50, 60, pAsteroidBitmap, dwTransparentColor);

	// sprites with masks
	gapiBufferMemory.MaskedBlt(dwDispWidth-(nOffset*2)%dwDispWidth, (nOffset*8)%dwDispHeight, 50, 60, pAsteroidBitmap, pAsteroidMask);
	gapiBufferMemory.MaskedBlt((nOffset%dwDispWidth*5), (nOffset*3)%dwDispHeight, 50, 60, pAsteroidBitmap, pAsteroidMask);
	gapiBufferMemory.MaskedBlt((nOffset*3)%dwDispWidth, (nOffset*4)%dwDispHeight, 50, 60, pAsteroidBitmap, pAsteroidMask);
	gapiBufferMemory.MaskedBlt((nOffset%dwDispWidth*2), dwDispHeight-(nOffset*5)%dwDispHeight, 50, 60, pAsteroidBitmap, pAsteroidMask);

	// draw 3 filled rects using high-perfomance methods
	// (current position conception)
	RECT rc = { 0, 0, dwDispWidth/3, 10 };
	FillRect(&rc, RGB(255, 0, 0));

	rc.left = dwDispWidth/3;
	rc.right = 2*dwDispWidth/3;
	FillRect(&rc, RGB(0, 255, 0));

	rc.left = 2*dwDispWidth/3;
	rc.right = dwDispWidth;
	FillRect(&rc, RGB(0, 0, 255));


	// start actual drawing
	void* pBuffer = GXBeginDraw();

	gapiBufferScreen.SetBuffer(pBuffer);
	gapiBufferScreen.BitBlt(&gapiBufferMemory);

	GXEndDraw();
}


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

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_GBSAMPLE);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	Shutdown();

	return msg.wParam;
}


//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    It is important to call this function so that the application 
//    will get 'well formed' small icons associated with it.
//
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			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GBSAMPLE));
    wc.hCursor			= 0;
    wc.hbrBackground	= (HBRUSH) GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName		= 0;
    wc.lpszClassName	= szWindowClass;

	return RegisterClass(&wc);
}

//
//  FUNCTION: InitInstance(HANDLE, int)
//
//  PURPOSE: Saves instance handle and creates main window
//
//  COMMENTS:
//
//    In this function, we save the instance handle in a global variable and
//    create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND	hWnd = NULL;
	TCHAR	szTitle[MAX_LOADSTRING];			// The title bar text
	TCHAR	szWindowClass[MAX_LOADSTRING];		// The window class name

	hInst = hInstance;		// Store instance handle in our global variable
	// Initialize global strings
	LoadString(hInstance, IDC_GBSAMPLE, szWindowClass, MAX_LOADSTRING);
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);

	//If it is already running, then focus on the window
	hWnd = FindWindow(szWindowClass, szTitle);	
	if (hWnd) 
	{
		SetForegroundWindow ((HWND) (((DWORD)hWnd) | 0x01));    
		return 0;
	} 

	MyRegisterClass(hInstance, szWindowClass);
	
	RECT	rect;
	GetClientRect(hWnd, &rect);
	
	hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
	if (!hWnd)
	{	
		return FALSE;
	}
	//When the main window is created using CW_USEDEFAULT the height of the menubar (if one
	// is created is not taken into account). So we resize the window after creating it
	// if a menubar is present
	{
		RECT rc;
		GetWindowRect(hWnd, &rc);
		rc.bottom -= MENU_HEIGHT;
		if (hwndCB)
			MoveWindow(hWnd, rc.left, rc.top, rc.right, rc.bottom, FALSE);
	}


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

	Initialize(hWnd);

	return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	TCHAR szHello[MAX_LOADSTRING];

	switch (message) 
	{
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{	
				case IDM_HELP_ABOUT:
					DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				    break;
				case IDOK:
					SendMessage(hWnd, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), (LPARAM)hWnd);
					SendMessage (hWnd, WM_CLOSE, 0, 0);
					break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_CREATE:
			hwndCB = CreateRpCommandBar(hWnd);
			SetTimer(hWnd, 100, 40, NULL);
			break;

		case WM_TIMER:
			{
				static int nAsteroidPos = 0;
				DrawFrame(nAsteroidPos);
				nAsteroidPos += 2;
			}
			break;

		case WM_PAINT:
			RECT rt;
			hdc = BeginPaint(hWnd, &ps);
			GetClientRect(hWnd, &rt);
			LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
			DrawText(hdc, szHello, _tcslen(szHello), &rt, 
				DT_SINGLELINE | DT_VCENTER | DT_CENTER);
			EndPaint(hWnd, &ps);
			break; 
		case WM_DESTROY:
			CommandBar_Destroy(hwndCB);
			PostQuitMessage(0);
			break;
		case WM_SETTINGCHANGE:
			SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
     		break;

		case WM_KEYDOWN:
			if (VK_RETURN == wParam)
			{
				PostMessage(hWnd, WM_CLOSE, 0, 0);
			}
			break;

		case WM_ACTIVATE:
			if (WA_INACTIVE == LOWORD(wParam))
			{
				PostMessage(hWnd, WM_CLOSE, 0, 0);
			}
			break;

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

HWND CreateRpCommandBar(HWND hwnd)
{
	SHMENUBARINFO mbi;

	memset(&mbi, 0, sizeof(SHMENUBARINFO));
	mbi.cbSize     = sizeof(SHMENUBARINFO);
	mbi.hwndParent = hwnd;
	mbi.nToolBarId = IDM_MENU;
	mbi.hInstRes   = hInst;
	mbi.nBmpId     = 0;
	mbi.cBmpImages = 0;

	if (!SHCreateMenuBar(&mbi)) 
		return NULL;

	return mbi.hwndMB;
}

// Mesage handler for the About box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	SHINITDLGINFO shidi;

	switch (message)
	{
		case WM_INITDIALOG:
			// Create a Done button and size it.  
			shidi.dwMask = SHIDIM_FLAGS;
			 shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
			shidi.hDlg = hDlg;
			SHInitDialog(&shidi);
			return TRUE; 

		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK) {
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
			break;
	}
    return FALSE;
}

⌨️ 快捷键说明

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