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.

166 lines
4.4 KiB

5 years ago
  1. #include <windows.h>
  2. #include <tchar.h>
  3. #include "Raster.h"
  4. #include "CELLTimestamp.hpp"
  5. LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  6. {
  7. switch(msg)
  8. {
  9. case WM_SIZE:
  10. break;
  11. case WM_CLOSE:
  12. case WM_DESTROY:
  13. PostQuitMessage(0);
  14. break;
  15. default:
  16. break;
  17. }
  18. return DefWindowProc( hWnd, msg, wParam, lParam );
  19. }
  20. void getResourcePath(HINSTANCE hInstance,char pPath[1024])
  21. {
  22. char szPathName[1024];
  23. char szDriver[64];
  24. char szPath[1024];
  25. GetModuleFileNameA(hInstance,szPathName,sizeof(szPathName));
  26. _splitpath( szPathName, szDriver, szPath, 0, 0 );
  27. sprintf(pPath,"%s%s",szDriver,szPath);
  28. }
  29. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
  30. {
  31. // 1 ע�ᴰ����
  32. ::WNDCLASSEXA winClass;
  33. winClass.lpszClassName = "Raster";
  34. winClass.cbSize = sizeof(::WNDCLASSEX);
  35. winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
  36. winClass.lpfnWndProc = windowProc;
  37. winClass.hInstance = hInstance;
  38. winClass.hIcon = 0;
  39. winClass.hIconSm = 0;
  40. winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  41. winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
  42. winClass.lpszMenuName = NULL;
  43. winClass.cbClsExtra = 0;
  44. winClass.cbWndExtra = 0;
  45. RegisterClassExA(&winClass);
  46. // 2 ��������
  47. HWND hWnd = CreateWindowExA(
  48. NULL,
  49. "Raster",
  50. "Raster",
  51. WS_OVERLAPPEDWINDOW,
  52. 0,
  53. 0,
  54. 800,
  55. 600,
  56. 0,
  57. 0,
  58. hInstance,
  59. 0
  60. );
  61. UpdateWindow( hWnd );
  62. ShowWindow(hWnd,SW_SHOW);
  63. RECT rt = {0};
  64. GetClientRect(hWnd,&rt);
  65. int width = rt.right - rt.left;
  66. int height = rt.bottom - rt.top;
  67. void* buffer = 0;
  68. HDC hDC = GetDC(hWnd);
  69. HDC hMem = ::CreateCompatibleDC(hDC);
  70. BITMAPINFO bmpInfor;
  71. bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  72. bmpInfor.bmiHeader.biWidth = width;
  73. bmpInfor.bmiHeader.biHeight = -height;
  74. bmpInfor.bmiHeader.biPlanes = 1;
  75. bmpInfor.bmiHeader.biBitCount = 32;
  76. bmpInfor.bmiHeader.biCompression = BI_RGB;
  77. bmpInfor.bmiHeader.biSizeImage = 0;
  78. bmpInfor.bmiHeader.biXPelsPerMeter = 0;
  79. bmpInfor.bmiHeader.biYPelsPerMeter = 0;
  80. bmpInfor.bmiHeader.biClrUsed = 0;
  81. bmpInfor.bmiHeader.biClrImportant = 0;
  82. HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
  83. SelectObject(hMem,hBmp);
  84. char szPath[1024];
  85. getResourcePath(0,szPath);
  86. char szImage[1024];
  87. sprintf(szImage,"%s/image/bg.png",szPath);
  88. CELL::Image* image = CELL::Image::loadFromFile(szImage);
  89. sprintf(szImage,"%s/image/grass.png",szPath);
  90. CELL::Image* colorKey= CELL::Image::loadFromFile(szImage);
  91. CELL::Raster raster(width,height,buffer);
  92. MSG msg = {0};
  93. while(true)
  94. {
  95. if (msg.message == WM_DESTROY
  96. ||msg.message == WM_CLOSE
  97. ||msg.message == WM_QUIT)
  98. {
  99. break;
  100. }
  101. if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  102. {
  103. TranslateMessage( &msg );
  104. DispatchMessage( &msg );
  105. }
  106. raster.clear();
  107. CELL::int2 pt[3] =
  108. {
  109. CELL::int2(100,10),
  110. CELL::int2(10,100),
  111. CELL::int2(200,100),
  112. };
  113. CELL::Rgba color1(255,0,0);
  114. CELL::Rgba color2(0,255,0);
  115. CELL::Rgba color3(0,0,255);
  116. CELL::CELLTimestamp tms;
  117. tms.update();
  118. raster.drawTriangle(pt[0],pt[1],pt[2],color1,color2,color3);
  119. double mis = tms.getElapsedTimeInMicroSec();
  120. char szBuf[128];
  121. sprintf(szBuf,"%f ",mis);
  122. int i = 00;
  123. memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));
  124. raster.drawImage(0,0,image);
  125. raster.drawImageAlphaTest(100,100,colorKey,100);
  126. raster.drawImageAlphaBlend(250,100,colorKey);
  127. TextOut(hMem,10,10,szBuf,strlen(szBuf));
  128. BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
  129. }
  130. delete image;
  131. return 0;
  132. }