You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
7.0 KiB

5 years ago
  1. #include <windows.h>
  2. #include <tchar.h>
  3. #include "Raster.h"
  4. #include "CELLTimestamp.hpp"
  5. #include "CELLCamera.hpp"
  6. CELL::CELLCamera g_camera;
  7. CELL::int2 g_rButtonDown;
  8. bool g_rButtonFlag = false;
  9. LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  10. {
  11. switch(msg)
  12. {
  13. case WM_SIZE:
  14. break;
  15. case WM_LBUTTONDOWN:
  16. {
  17. int x = LOWORD(lParam);
  18. int y = HIWORD(lParam);
  19. }
  20. break;
  21. case WM_LBUTTONUP:
  22. {
  23. int x = LOWORD(lParam);
  24. int y = HIWORD(lParam);
  25. }
  26. break;
  27. case WM_MOUSEMOVE:
  28. {
  29. int x = LOWORD(lParam);
  30. int y = HIWORD(lParam);
  31. if (g_rButtonFlag)
  32. {
  33. int offsetX = x - g_rButtonDown.x;
  34. int offsetY = y - g_rButtonDown.y;
  35. g_camera.rotateViewY(offsetX);
  36. g_camera.rotateViewX(offsetY);
  37. g_rButtonDown.x = x;
  38. g_rButtonDown.y = y;
  39. }
  40. }
  41. break;
  42. case WM_RBUTTONDOWN:
  43. {
  44. g_rButtonDown.x = LOWORD(lParam);
  45. g_rButtonDown.y = HIWORD(lParam);
  46. g_rButtonFlag = true;
  47. }
  48. break;
  49. case WM_RBUTTONUP:
  50. {
  51. g_rButtonFlag = false;
  52. }
  53. break;
  54. case WM_MOUSEWHEEL:
  55. {
  56. short delta = GET_WHEEL_DELTA_WPARAM(wParam);
  57. if (delta > 0)
  58. {
  59. float len = CELL::length(g_camera._eye-g_camera._target);
  60. len *= 1.2f;
  61. g_camera._eye = g_camera._target - g_camera._dir * len;
  62. }
  63. else
  64. {
  65. float len = CELL::length(g_camera._eye-g_camera._target);
  66. len *= 0.9f;
  67. g_camera._eye = g_camera._target - g_camera._dir * len;
  68. }
  69. g_camera.update();
  70. }
  71. break;
  72. case WM_CLOSE:
  73. case WM_DESTROY:
  74. PostQuitMessage(0);
  75. break;
  76. default:
  77. break;
  78. }
  79. return DefWindowProc( hWnd, msg, wParam, lParam );
  80. }
  81. void getResourcePath(HINSTANCE hInstance,char pPath[1024])
  82. {
  83. char szPathName[1024];
  84. char szDriver[64];
  85. char szPath[1024];
  86. GetModuleFileNameA(hInstance,szPathName,sizeof(szPathName));
  87. _splitpath( szPathName, szDriver, szPath, 0, 0 );
  88. sprintf(pPath,"%s%s",szDriver,szPath);
  89. }
  90. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
  91. {
  92. // 1 ע�ᴰ����
  93. ::WNDCLASSEXA winClass;
  94. winClass.lpszClassName = "Raster";
  95. winClass.cbSize = sizeof(::WNDCLASSEX);
  96. winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
  97. winClass.lpfnWndProc = windowProc;
  98. winClass.hInstance = hInstance;
  99. winClass.hIcon = 0;
  100. winClass.hIconSm = 0;
  101. winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  102. winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
  103. winClass.lpszMenuName = NULL;
  104. winClass.cbClsExtra = 0;
  105. winClass.cbWndExtra = 0;
  106. RegisterClassExA(&winClass);
  107. // 2 ��������
  108. HWND hWnd = CreateWindowExA(
  109. NULL,
  110. "Raster",
  111. "Raster",
  112. WS_OVERLAPPEDWINDOW,
  113. 0,
  114. 0,
  115. 800,
  116. 600,
  117. 0,
  118. 0,
  119. hInstance,
  120. 0
  121. );
  122. UpdateWindow( hWnd );
  123. ShowWindow(hWnd,SW_SHOW);
  124. RECT rt = {0};
  125. GetClientRect(hWnd,&rt);
  126. int width = rt.right - rt.left;
  127. int height = rt.bottom - rt.top;
  128. void* buffer = 0;
  129. HDC hDC = GetDC(hWnd);
  130. HDC hMem = ::CreateCompatibleDC(hDC);
  131. BITMAPINFO bmpInfor;
  132. bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  133. bmpInfor.bmiHeader.biWidth = width;
  134. bmpInfor.bmiHeader.biHeight = -height;
  135. bmpInfor.bmiHeader.biPlanes = 1;
  136. bmpInfor.bmiHeader.biBitCount = 32;
  137. bmpInfor.bmiHeader.biCompression = BI_RGB;
  138. bmpInfor.bmiHeader.biSizeImage = 0;
  139. bmpInfor.bmiHeader.biXPelsPerMeter = 0;
  140. bmpInfor.bmiHeader.biYPelsPerMeter = 0;
  141. bmpInfor.bmiHeader.biClrUsed = 0;
  142. bmpInfor.bmiHeader.biClrImportant = 0;
  143. HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
  144. SelectObject(hMem,hBmp);
  145. char szPath[1024];
  146. getResourcePath(0,szPath);
  147. char szImage[1024];
  148. sprintf(szImage,"%s/image/bg.png",szPath);
  149. CELL::Image* image = CELL::Image::loadFromFile(szImage);
  150. sprintf(szImage,"%s/image/1.jpg",szPath);
  151. CELL::Image* image1 = CELL::Image::loadFromFile(szImage);
  152. CELL::Raster raster(width,height,buffer);
  153. g_camera.setViewSize(width,height);
  154. g_camera.perspective(60,(float)(width)/(float)(height),0.1,10000);
  155. g_camera.update();
  156. raster.setViewPort(0,0,width,height);
  157. raster.setPerspective(60,(float)(width)/(float)(height),0.1,10000);
  158. struct Vertex
  159. {
  160. float x,y,z;
  161. float u,v;
  162. CELL::Rgba color;
  163. };
  164. MSG msg = {0};
  165. while(true)
  166. {
  167. if (msg.message == WM_DESTROY
  168. ||msg.message == WM_CLOSE
  169. ||msg.message == WM_QUIT)
  170. {
  171. break;
  172. }
  173. if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  174. {
  175. TranslateMessage( &msg );
  176. DispatchMessage( &msg );
  177. }
  178. raster.clear();
  179. CELL::CELLTimestamp tms;
  180. tms.update();
  181. double mis = tms.getElapsedTimeInMicroSec();
  182. char szBuf[128];
  183. sprintf(szBuf,"%f ",mis);
  184. raster.setView(g_camera.getView());
  185. Vertex vertexs[] =
  186. {
  187. {-1, 0, 1, 0, 0, CELL::Rgba()},
  188. { 1, 0, 1, 1, 0, CELL::Rgba()},
  189. { 1, 0, -1, 1, 1, CELL::Rgba()},
  190. {-1, 0, 1, 0, 0, CELL::Rgba()},
  191. { 1, 0, -1, 1, 1, CELL::Rgba()},
  192. {-1, 0, -1, 0, 1, CELL::Rgba()},
  193. };
  194. for (int i = 0 ;i < 6 ; ++ i)
  195. {
  196. vertexs[i].x *= 100;
  197. vertexs[i].z *= 100;
  198. vertexs[i].u *= 10;
  199. vertexs[i].v *= 10;
  200. }
  201. image1->setWrapType(0);
  202. raster.bindTexture(image1);
  203. raster.vertexPointer(2,CELL::DT_FLOAT, sizeof(Vertex),&vertexs[0].x);
  204. raster.textureCoordPointer(2,CELL::DT_FLOAT,sizeof(Vertex),&vertexs[0].u);
  205. raster.colorPointer(4,CELL::DT_BYTE, sizeof(Vertex),&vertexs[0].color);
  206. raster.drawArrays(CELL::DM_TRIANGES,0,6);
  207. //TextOut(hMem,10,10,szBuf,strlen(szBuf));
  208. BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
  209. }
  210. delete image;
  211. delete image1;
  212. return 0;
  213. }