DirectX9 학습(1)

3962 단어 DirectX 학습
2017.8.22
자신에게 쓰고, DirectX를 접하기 시작하라. 이것은 자신이 배운 백업이다.
WinMain과 WinProc를 접하기 시작했는데 대부분 책에 따라 두드렸고 나중에 편리하게 참고할 수 있도록
첫 번째 코드:
#include
#include"iostream"
#include 
using namespace std;
const string ProgramTitle = "Hello World!";


HWND window;
HDC device;
bool gameOver = false;

//       bmp,      
//"E:\\Win32\\test1.bmp"
void DrawBitmap(char *fileName,int x,int y)
{
	//LOAD BMP
	HBITMAP image = (HBITMAP)LoadImage(0, fileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	
	//read the bitmap's properties
	BITMAP bm;
	GetObject(image, sizeof(BITMAP), &bm);

	//create a device context from the bmp
	HDC hdcImage = CreateCompatibleDC(device);
	SelectObject(hdcImage, image);

	//Draw the bitMap to Window
	BitBlt(device, x, y, bm.bmWidth, bm.bmHeight, hdcImage, 0, 0, SRCCOPY);

	DeleteDC(hdcImage);
	DeleteObject((HBITMAP)image);
}

bool Game_Init()
{
	//       
	srand(time(NULL));
	return true;
}

//UPDATE
void Game_Run()
{
	if (gameOver)
		return;
	RECT rect;
	GetClientRect(window, &rect);
	//draw bitmap at random location
	int x = rand() % (rect.right - rect.left);
	int y = rand() % (rect.bottom - rect.top);
	DrawBitmap("E:\\Win32\\test1.bmp", x, y);

}

void Game_End()
{
	ReleaseDC(window, device);
}


//      ,             
//       
//    GetMessage    
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, 
							WPARAM wParam,LPARAM lParam)
{
	//HWND:    ,                  HDC,                     
	
	RECT drawRect;
	PAINTSTRUCT ps; //            
	HDC hdc;

	switch (message)
	{
	/*
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps); //StartDrawing       
		for (int n = 0; n < 20; n++)
		{
			int x = n * 20;
			int y = n * 20;
			drawRect = { x, y, x + 100, y + 100 };
			//      			
			DrawText(hdc, ProgramTitle.c_str(), ProgramTitle.length(), &drawRect, DT_CENTER);
		}
		EndPaint(hWnd, &ps);
		break;
	*/
	case WM_DESTROY:
		gameOver = true;
		PostQuitMessage(0);
		break;
		
	}
	return DefWindowProc(hWnd, message, wParam, lParam);

}

//       
//Helper Function to set up the window properties
ATOM MyRegisterClass(HINSTANCE hIntance)
{
	//set the new window's properties
	WNDCLASSEX wc;	
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;  //         |         
	wc.lpfnWndProc = (WNDPROC)WinProc;  //             ,        ,        HWND
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hIntance;
	wc.hIcon = NULL;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = ProgramTitle.c_str();
	wc.hIconSm = NULL;
	return RegisterClassEx(&wc);  //            Windows,       InitInstance
			

}

//              ,     
bool InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	//                      
	//Create
	window = CreateWindow(
		ProgramTitle.c_str(),	//    
		ProgramTitle.c_str(),	//  
		WS_OVERLAPPEDWINDOW,	//     style
		CW_USEDEFAULT,CW_USEDEFAULT,//  
		640,480,				//  
		NULL,					//   
		NULL,					//  
		hInstance,				//    
		NULL					//    
		);

	if (window == 0)    //    gg
		return false;
	
	//  
	ShowWindow(window, nCmdShow);
	UpdateWindow(window);
	device = GetDC(window);
	return true;

}

//Entry point for a Windows program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevinstance, LPSTR lpCmdLine, int nCmdShow)
{
	//  :        ,     ( 1  ),               ,    
	//    
	MyRegisterClass(hInstance);
	if (!InitInstance(hInstance, nCmdShow))
	{
		return 0;
	}

	if (!Game_Init())
		return 0;
	//     
	MSG msg;
	
	//        
	//GetMessage  :
	/*  LPMSG :        msg   
		HWND :          ,NULL      
		UINT MIN MAX :  
	*/
	while (!gameOver)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);//             
			DispatchMessage(&msg);//   Windows    
		}
		Game_Run();

	}
	
	Game_End();
	return msg.wParam;
}

좋은 웹페이지 즐겨찾기