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.

144 lines
4.0 KiB

5 years ago
  1. #include <windows.h>
  2. #include <tchar.h>
  3. #include "Raster.h"
  4. LRESULT CALLBACK windowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  5. {
  6. switch(msg)
  7. {
  8. case WM_SIZE:
  9. break;
  10. case WM_CLOSE:
  11. case WM_DESTROY:
  12. PostQuitMessage(0);
  13. break;
  14. default:
  15. break;
  16. }
  17. return DefWindowProc( hWnd, msg, wParam, lParam );
  18. }
  19. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
  20. {
  21. // 1 ע�ᴰ����
  22. ::WNDCLASSEXA winClass;
  23. winClass.lpszClassName = "Raster";
  24. winClass.cbSize = sizeof(::WNDCLASSEX);
  25. winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
  26. winClass.lpfnWndProc = windowProc;
  27. winClass.hInstance = hInstance;
  28. winClass.hIcon = 0;
  29. winClass.hIconSm = 0;
  30. winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  31. winClass.hbrBackground = (HBRUSH)(BLACK_BRUSH);
  32. winClass.lpszMenuName = NULL;
  33. winClass.cbClsExtra = 0;
  34. winClass.cbWndExtra = 0;
  35. RegisterClassExA(&winClass);
  36. // 2 ��������
  37. HWND hWnd = CreateWindowExA(
  38. NULL,
  39. "Raster",
  40. "Raster",
  41. WS_OVERLAPPEDWINDOW,
  42. 0,
  43. 0,
  44. 480,
  45. 320,
  46. 0,
  47. 0,
  48. hInstance,
  49. 0
  50. );
  51. UpdateWindow( hWnd );
  52. ShowWindow(hWnd,SW_SHOW);
  53. RECT rt = {0};
  54. GetClientRect(hWnd,&rt);
  55. int width = rt.right - rt.left;
  56. int height = rt.bottom - rt.top;
  57. void* buffer = 0;
  58. HDC hDC = GetDC(hWnd);
  59. HDC hMem = ::CreateCompatibleDC(hDC);
  60. BITMAPINFO bmpInfor;
  61. bmpInfor.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  62. bmpInfor.bmiHeader.biWidth = width;
  63. bmpInfor.bmiHeader.biHeight = -height;
  64. bmpInfor.bmiHeader.biPlanes = 1;
  65. bmpInfor.bmiHeader.biBitCount = 32;
  66. bmpInfor.bmiHeader.biCompression = BI_RGB;
  67. bmpInfor.bmiHeader.biSizeImage = 0;
  68. bmpInfor.bmiHeader.biXPelsPerMeter = 0;
  69. bmpInfor.bmiHeader.biYPelsPerMeter = 0;
  70. bmpInfor.bmiHeader.biClrUsed = 0;
  71. bmpInfor.bmiHeader.biClrImportant = 0;
  72. HBITMAP hBmp = CreateDIBSection(hDC,&bmpInfor,DIB_RGB_COLORS,(void**)&buffer,0,0);
  73. SelectObject(hMem,hBmp);
  74. CELL::Raster raster(width,height,buffer);
  75. MSG msg = {0};
  76. while(true)
  77. {
  78. if (msg.message == WM_DESTROY
  79. ||msg.message == WM_CLOSE
  80. ||msg.message == WM_QUIT)
  81. {
  82. break;
  83. }
  84. if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  85. {
  86. TranslateMessage( &msg );
  87. DispatchMessage( &msg );
  88. }
  89. raster.clear();
  90. CELL::float2 points[] =
  91. {
  92. CELL::float2(50,50),
  93. CELL::float2(200,50),
  94. CELL::float2(33,88),
  95. CELL::float2(159,100),
  96. };
  97. CELL::float2 prev[2];
  98. for (float t = 0 ;t < 1.0f ; t += 0.01f)
  99. {
  100. float x = points[0].x * pow(1-t,3)
  101. + 3 * points[1].x * t * pow(1-t,2)
  102. + 3 * points[2].x * t*t * (1-t)
  103. + points[3].x * t * t * t;
  104. float y = points[0].y * pow(1-t,3)
  105. + 3 * points[1].y * t * pow(1-t,2)
  106. + 3 * points[2].y * t*t * (1-t)
  107. + points[3].y * t * t * t;
  108. if (t == 0)
  109. {
  110. prev[0] = CELL::float2(x,y);
  111. }
  112. else
  113. {
  114. prev[1] = CELL::float2(x,y);
  115. raster.drawArrays(CELL::DM_LINES,prev,2);
  116. prev[0] = prev[1];
  117. }
  118. }
  119. memcpy(buffer,raster._buffer,raster._width * raster._height * sizeof(CELL::Rgba));
  120. BitBlt(hDC,0,0,width,height,hMem,0,0,SRCCOPY);
  121. }
  122. return 0;
  123. }