Directx hướng dẫn lập trình c

  1. #pragma once

  2. #include

  3. #include

  4. class MGraphic

  5. {

  6. private :

  7. //doi tuong direct x

  8. LPDIRECT3D9 d3d;

  9. //thiet bi ve

  10. LPDIRECT3DDEVICE9 d3ddev;

  11. //Ve texture

  12. LPD3DXSPRITE d3dxSprite;   

  13. //hwnd cua cua so ve

  14. HWND hWnd;

  15. //Kich thuoc cua bộ đệm

  16. int Width;

  17. int Height;

  18. public:

  19. //Cài đặt thiết bị vẽ

  20. bool InitD3D();

  21. //Constructor

  22. MGraphic(HWND,int,int);

  23. //Destructor

  24. ~MGraphic(void);

  25. //Bắt đầu vẽ

  26. void Begin();

  27. //Kết thúc vẽ

  28. void End();

  29. //Vẽ các hình cơ bản

  30. void DrawTexture(LPDIRECT3DTEXTURE9 texture,D3DXVECTOR2 postion,D3DCOLOR color);

  31. void DrawTexture(LPDIRECT3DTEXTURE9 texture,RECT source,D3DXVECTOR2 postion,D3DCOLOR color);

  32. void DrawTexture(LPDIRECT3DTEXTURE9 texture,D3DXVECTOR2 postion,D3DCOLOR color,float angle);

  33. //Vẽ các sufface

  34. void DrawSurface();

  35. //Lấy thiết bị vẽ

  36. LPDIRECT3DDEVICE9 GetDevice(); 

  37. };

  38. MGraphic::MGraphic(HWND hwnd,int W,int H)

  39. {

  40. this->hWnd = hwnd;

  41. this->Width = W;

  42. this->Height = H;

  43. this->d3d = NULL;

  44. this->d3ddev = NULL;

  45. this->d3dxSprite = NULL;   

  46. }

  47. bool MGraphic::InitD3D()

  48. {

  49. //tao mot doi tuong Direct3D

  50. this->d3d = Direct3DCreate9(D3D_SDK_VERSION);

  51. if(!this->d3d)

  52. return false;

  53. //tao thiet bi ve

  54. D3DPRESENT_PARAMETERS d3dpp;

  55. ZeroMemory(&d3dpp,sizeof(d3dpp));

  56. d3dpp.BackBufferCount = 1;// Có một back buffer

  57. d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;//Tự tạo format phù hợp

  58. d3dpp.BackBufferWidth = this->Width;//Chiều rộng back buffer

  59. d3dpp.BackBufferHeight = this->Height;//Chiều cao back buffer

  60. d3dpp.hDeviceWindow = this->hWnd;// Handle của cửa sổ mình sẽ vẽ lên

  61. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;// Một tham số hay dùng

  62. d3dpp.Windowed = true;// Chế độ có cửa sổ

  63. HRESULT hr = this->d3d->CreateDevice(D3DADAPTER_DEFAULT,//chon thiet bi ve mac dinh

  64. D3DDEVTYPE_HAL,//su dung ho tro cua phan cung. Có rất nhiều sự lựa chọn. Tham khảo MSDN để biết thêm

  65. this->hWnd,// Cửa sổ vẽ

  66. D3DCREATE_SOFTWARE_VERTEXPROCESSING,//MSDN không nói rõ. :D

  67. &d3dpp,//Các thông số cho back buffer

  68. &this->d3ddev//Con trỏ nhận dữ liẹu sau khi tạo devicce

  69. );

  70. // Ở đây vì ngắn gọn nên mình chỉ tạo device cho thiết bị có hỗ trợ phần cứng.

  71. if(FAILED(hr))

  72. {      

  73. return false;

  74. }

  75. Tạo bút vẽ các texture

  76. hr = D3DXCreateSprite(this->d3ddev,&this->d3dxSprite);

  77. if(FAILED(hr))

  78. return false;  

  79. return true;

  80. }

  81. MGraphic::~MGraphic(void)

  82. {

  83. }

  84. void MGraphic::Begin()

  85. {

  86. // xóa cửa sổ vẽ bằng màu trắng D3DCOLOR_XRGB(255,255,255)

  87. this->d3ddev->Clear(1,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(255,255,255),1,NULL);

  88. // Bắt đầu vẽ

  89. this->d3ddev->BeginScene();

  90. this->d3dxSprite->Begin(D3DXSPRITE_ALPHABLEND);

  91. }

  92. void MGraphic::End()

  93. {

  94. // Kết thúc vẽ

  95. this->d3dxSprite->End();

  96. this->d3ddev->EndScene();

  97. // Thể hiện các đối tượng từ back buffer ra ngoài màn hình

  98. this->d3ddev->Present(NULL,NULL,NULL,NULL);

  99. }

  100. void MGraphic::DrawTexture(LPDIRECT3DTEXTURE9 texture,D3DXVECTOR2 postion,D3DCOLOR color)

  101. {

  102. this->d3dxSprite->Draw(

  103. texture,//hình cần vẽ

  104. NULL,//khung cắt từ ảnh. Xem thêm các bài viết trên cviet về sprite. Chọn NULL là lấy hết cả texture

  105. NULL,//Tâm của texture. Chọn NULL là tọa đọ 0,0

  106. &D3DXVECTOR3(postion.x,postion.y,0),//Tọa đọ của texture trên màn hình đồ họa

  107. color//màu của texture

  108. );

  109. }

  110. void MGraphic::DrawTexture(LPDIRECT3DTEXTURE9 texture,RECT source,D3DXVECTOR2 postion,D3DCOLOR color)

  111. {

  112. // Tương tự

  113. this->d3dxSprite->Draw(texture,&source,NULL,&D3DXVECTOR3(postion.x,postion.y,0),color);

  114. }

  115. void MGraphic::DrawSurface()

  116. {

  117. }

  118. LPDIRECT3DDEVICE9 MGraphic::GetDevice()

  119. {

  120. return this->d3ddev;

  121. }

  122. void MGraphic::DrawTexture(LPDIRECT3DTEXTURE9 texture,D3DXVECTOR2 postion,D3DCOLOR color,float angle)

  123. {

  124. // Vừa vẽ vừa xoay, Xem thêm trên msdn. Mình chưa fix bug đoạn code này :D

  125. D3DXMATRIX m1,m2,m3;

  126. this->d3dxSprite->GetTransform(&m1);

  127. D3DXMatrixTransformation2D(&m2,NULL,NULL,NULL,NULL,1,&postion);

  128. m3 = m1 * m2;

  129. this->d3dxSprite->SetTransform(&m3);

  130. this->d3dxSprite->Draw(texture,NULL,NULL,&D3DXVECTOR3(postion.x,postion.y,0),D3DCOLOR_XRGB(255,255,255));

  131. this->d3dxSprite->SetTransform(&m1);

  132. }