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

📄 gbsample.cpp

📁 在pda上使用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 WC_GAPISAMPLE		_T("GapiSample.MainWindow")
#define TITLE_GAPISAMPLE	_T("GapiSample")

// Global Variables:
HINSTANCE hInst;					// The current instance

// bitmaps in native format
CNativeBitmap* pAsteroidBitmap = NULL;
CNativeBitmap* pAsteroidMask = NULL;

// gapi buffers
CSTGapiBuffer gapiBufferBackground;	// background
CSTGapiBuffer gapiBufferMemory;		// memory buffer
CSTGapiBuffer gapiBufferScreen;		// device screen buffer

DWORD dwTransparentColor = 0;				// transparent color
DWORD dwDispWidth = 0, dwDispHeight = 0;	// dispay size

HANDLE hWorkerThread = NULL;		// worker thread handle
BOOL bExitApp = FALSE;				// indicates that a worker should exit

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);
	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()
{
	// safe exit from the worker thread
	bExitApp = TRUE;
	WaitForSingleObject(hWorkerThread, INFINITE);
	CloseHandle(hWorkerThread);

	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.TransparentBltEx(0, nOffset%dwDispHeight, 50, 60, pAsteroidBitmap, dwTransparentColor);
	gapiBufferMemory.TransparentBltEx(100, (nOffset*2)%dwDispHeight, 50, 60, pAsteroidBitmap, dwTransparentColor);
	gapiBufferMemory.TransparentBltEx(nOffset%dwDispWidth, 50, 50, 60, pAsteroidBitmap, dwTransparentColor);
	gapiBufferMemory.TransparentBltEx((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();
}

DWORD WINAPI WorkerThreadProc(LPVOID lpParameter)
{
	int nAsteroidPos = 1;
	while (!bExitApp)
	{
		DrawFrame(nAsteroidPos);
		Sleep(5);
		nAsteroidPos += 1;
	}
	return 0;
}

//
//  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)
{
	switch (message) 
	{
		case WM_DESTROY:
			PostQuitMessage(0);
     		break;

		case WM_KEYDOWN:
		case WM_KILLFOCUS:
			Shutdown();
			PostMessage(hWnd, WM_CLOSE, 0, 0);
			break;

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

//
//  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(hInst, 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;

	hInst = hInstance;		// Store instance handle in our global variable

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

	MyRegisterClass(hInst, WC_GAPISAMPLE);
	
	hWnd = CreateWindow(WC_GAPISAMPLE, TITLE_GAPISAMPLE, WS_CLIPCHILDREN,
		0, 0, 0, 0, NULL, NULL, hInstance, NULL);
	if (!hWnd)
	{	
		return FALSE;
	}

	ShowWindow(hWnd, SW_SHOW);

	Initialize(hWnd);
	hWorkerThread = CreateThread(NULL, 0, WorkerThreadProc, NULL, 0, 0);
	SetThreadPriority(hWorkerThread, THREAD_PRIORITY_TIME_CRITICAL);

	return TRUE;
}

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;
}


⌨️ 快捷键说明

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