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.

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