dx相关代码
1、空窗口
#include "stdafx.h" #include <stdio.h>//全局變量聲明 HINSTANCE hInst; HBITMAP man[7]; HDC hdc,mdc; HWND hWnd; DWORD tPre,tNow,tCheck; //聲明三個函數(shù)來記錄時間,tPre記錄上一次繪圖的時間,tNow記錄此次準(zhǔn)備繪圖的時間,tCheck記錄每秒開始的時間 int num,frame,fps; //num用來記錄圖號,frame用來累加每次畫面更新的次數(shù),fps(frame per second)用來記錄每秒畫面更新的次數(shù)//全局函數(shù)的聲明 ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void MyPaint(HDC hdc);//***WinMain函數(shù),程序入口點函數(shù)************************************** int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) {MSG msg;MyRegisterClass(hInstance);//運行初始化函數(shù)if (!InitInstance (hInstance, nCmdShow)) {return FALSE;}//游戲循環(huán)GetMessage(&msg,NULL,NULL,NULL);while( msg.message!=WM_QUIT ){if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) ){TranslateMessage( &msg );DispatchMessage( &msg );}else{tNow = GetTickCount();if(tNow-tPre >= 100) //當(dāng)此次循環(huán)運行與上次繪圖時間相差0.1秒時再進(jìn)行重繪操作MyPaint(hdc);}}return msg.wParam; }//****設(shè)計一個窗口類,類似填空題,使用窗口結(jié)構(gòu)體************************* ATOM MyRegisterClass(HINSTANCE hInstance) {WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc = (WNDPROC)WndProc;wcex.cbClsExtra = 0;wcex.cbWndExtra = 0;wcex.hInstance = hInstance;wcex.hIcon = NULL;wcex.hCursor = NULL;wcex.hCursor = LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName = NULL;wcex.lpszClassName = "canvas";wcex.hIconSm = NULL;return RegisterClassEx(&wcex); }//****初始化函數(shù)************************************* // 從文件加載位圖 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {char filename[20] = "";int i;hInst = hInstance;hWnd = CreateWindow("canvas", "動畫演示" , WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);if (!hWnd){return FALSE;}MoveWindow(hWnd,10,10,600,450,true);ShowWindow(hWnd, nCmdShow);UpdateWindow(hWnd);hdc = GetDC(hWnd);mdc = CreateCompatibleDC(hdc);//載入各個人物位圖for(i=0;i<7;i++){sprintf(filename,"man%d.bmp",i);man[i] = (HBITMAP)LoadImage(NULL,filename,IMAGE_BITMAP,640,480,LR_LOADFROMFILE);}num = 0;frame = 0;MyPaint(hdc);return TRUE; }//****自定義繪圖函數(shù)********************************* // 1.計算與顯示每秒畫面更新次數(shù) // 2.按照圖號順序進(jìn)行窗口貼圖 void MyPaint(HDC hdc) {char str[40] = "";if(num == 7)num = 0;frame++; //畫面更新次數(shù)加1if(tNow - tCheck >= 1000) //判斷此次繪圖時間由前一秒算起是否已經(jīng)達(dá)到1秒鐘的時間間隔。若是,則將目前的'frame'值賦給"fps",表示這一秒內(nèi)所更新的畫面次數(shù),然后將“frame”值回0,并重設(shè)下次計算每秒畫面數(shù)的起始時間"iCheck"。{fps = frame;frame = 0;tCheck = tNow;}SelectObject(mdc,man[num]); //選用要更新的圖案到mdc中,再輸出顯示每秒畫面更新次數(shù)的字符串到mdc上,最后將mdc的內(nèi)容貼到窗口中。sprintf(str,"每秒顯示 %d個畫面",fps);TextOut(mdc,0,0,str,strlen(str));BitBlt(hdc,0,0,600,450,mdc,0,0,SRCCOPY);tPre = GetTickCount(); //記錄此次繪圖時間,供下次游戲循環(huán)中判斷是否已經(jīng)達(dá)到畫面更新操作設(shè)定的時間間隔。num++; }//******消息處理函數(shù)********************************* LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {int i;switch (message){case WM_DESTROY: //窗口結(jié)束消息DeleteDC(mdc);for(i=0;i<7;i++)DeleteObject(man[i]);ReleaseDC(hWnd,hdc);PostQuitMessage(0);break;default: //其他消息return DefWindowProc(hWnd, message, wParam, lParam);}return 0; }
2、代碼段 結(jié)構(gòu)體好習(xí)慣
代碼段一 Dx11DemoBases類的頭文件[cpp] view plaincopyprint?
#ifndef _DEMO_BASE_H_
#define _DEMO_BASE_H_
#include<d3d11.h>
#include<d3dx11.h>
#include<DxErr.h>
class Dx11DemoBase
{
public: Dx11DemoBase(); virtual ~Dx11DemoBase(); bool Initialize( HINSTANCE hInstance, HWND hwnd ); void Shutdown( ); virtual bool LoadContent( ); virtual void UnloadContent( ); virtual void Update( float dt ) = 0; virtual void Render( ) = 0; protected: HINSTANCE hInstance_; HWND hwnd_; D3D_DRIVER_TYPE driverType_; D3D_FEATURE_LEVEL featureLevel_; ID3D11Device* d3dDevice_; ID3D11DeviceContext* d3dContext_; IDXGISwapChain* swapChain_; ID3D11RenderTargetView* backBufferTarget_;
};
#endif 上面這段代碼中我們可以看到最精簡的D3D對象,以protected類成員的形式存在于類之中。在類體外初始化變量是比較好的編程習(xí)慣,而且效率比讓先調(diào)用拷貝構(gòu)造函數(shù),再調(diào)用默認(rèn)構(gòu)造函數(shù)要高得多。3、二維的D3D11_TEXTURE2D_DESC聲明形式如下:
二維的D3D11_TEXTURE2D_DESC聲明形式如下:
?
[cpp]?view plaincopyprint?4、 ?Direct3D初始化四部曲
總結(jié)
- 上一篇: Linux 中服务器硬件及RAID配置实
- 下一篇: java 加密 压缩_如何用java 将