PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Chap14/Bounce/Bounce.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 199 lines | 153 code | 42 blank | 4 comment | 10 complexity | 826e55b4b4c5fcfbdf4d3e68b10cf9f8 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Bounce;
  6. import core.memory;
  7. import core.runtime;
  8. import core.thread;
  9. import std.algorithm : max, min;
  10. import std.conv;
  11. import std.math;
  12. import std.range;
  13. import std.string;
  14. import std.utf;
  15. auto toUTF16z(S)(S s)
  16. {
  17. return toUTFz!(const(wchar)*)(s);
  18. }
  19. pragma(lib, "gdi32.lib");
  20. import core.sys.windows.windef;
  21. import core.sys.windows.winuser;
  22. import core.sys.windows.wingdi;
  23. import core.sys.windows.winbase;
  24. enum ID_TIMER = 1;
  25. string appName = "Bounce";
  26. string description = "Bouncing Ball";
  27. HINSTANCE hinst;
  28. extern (Windows)
  29. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  30. {
  31. int result;
  32. try
  33. {
  34. Runtime.initialize();
  35. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  36. Runtime.terminate();
  37. }
  38. catch (Throwable o)
  39. {
  40. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  41. result = 0;
  42. }
  43. return result;
  44. }
  45. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  46. {
  47. hinst = hInstance;
  48. HACCEL hAccel;
  49. HWND hwnd;
  50. MSG msg;
  51. WNDCLASS wndclass;
  52. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  53. wndclass.lpfnWndProc = &WndProc;
  54. wndclass.cbClsExtra = 0;
  55. wndclass.cbWndExtra = 0;
  56. wndclass.hInstance = hInstance;
  57. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  58. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  59. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  60. wndclass.lpszMenuName = appName.toUTF16z;
  61. wndclass.lpszClassName = appName.toUTF16z;
  62. if (!RegisterClass(&wndclass))
  63. {
  64. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  65. return 0;
  66. }
  67. hwnd = CreateWindow(appName.toUTF16z, // window class name
  68. description.toUTF16z, // window caption
  69. WS_OVERLAPPEDWINDOW, // window style
  70. CW_USEDEFAULT, // initial x position
  71. CW_USEDEFAULT, // initial y position
  72. CW_USEDEFAULT, // initial x size
  73. CW_USEDEFAULT, // initial y size
  74. NULL, // parent window handle
  75. NULL, // window menu handle
  76. hInstance, // program instance handle
  77. NULL); // creation parameters
  78. ShowWindow(hwnd, iCmdShow);
  79. UpdateWindow(hwnd);
  80. while (GetMessage(&msg, NULL, 0, 0))
  81. {
  82. TranslateMessage(&msg);
  83. DispatchMessage(&msg);
  84. }
  85. return msg.wParam;
  86. }
  87. extern (Windows)
  88. LRESULT WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  89. {
  90. static HBITMAP hBitmap;
  91. static int cxClient, cyClient, xCenter, yCenter, cxTotal, cyTotal,
  92. cxRadius, cyRadius, cxMove, cyMove, xPixel, yPixel;
  93. HBRUSH hBrush;
  94. HDC hdc, hdcMem;
  95. int iScale;
  96. switch (iMsg)
  97. {
  98. case WM_CREATE:
  99. hdc = GetDC(hwnd);
  100. xPixel = GetDeviceCaps(hdc, ASPECTX);
  101. yPixel = GetDeviceCaps(hdc, ASPECTY);
  102. ReleaseDC(hwnd, hdc);
  103. SetTimer(hwnd, ID_TIMER, 50, NULL);
  104. return 0;
  105. case WM_SIZE:
  106. xCenter = (cxClient = LOWORD(lParam)) / 2;
  107. yCenter = (cyClient = HIWORD(lParam)) / 2;
  108. iScale = min(cxClient * xPixel, cyClient * yPixel) / 16;
  109. cxRadius = iScale / xPixel;
  110. cyRadius = iScale / yPixel;
  111. cxMove = max(1, cxRadius / 2);
  112. cyMove = max(1, cyRadius / 2);
  113. cxTotal = 2 * (cxRadius + cxMove);
  114. cyTotal = 2 * (cyRadius + cyMove);
  115. if (hBitmap)
  116. DeleteObject(hBitmap);
  117. hdc = GetDC(hwnd);
  118. hdcMem = CreateCompatibleDC(hdc);
  119. hBitmap = CreateCompatibleBitmap(hdc, cxTotal, cyTotal);
  120. ReleaseDC(hwnd, hdc);
  121. SelectObject(hdcMem, hBitmap);
  122. Rectangle(hdcMem, -1, -1, cxTotal + 1, cyTotal + 1);
  123. hBrush = CreateHatchBrush(HS_DIAGCROSS, 0L);
  124. SelectObject(hdcMem, hBrush);
  125. SetBkColor(hdcMem, RGB(255, 0, 255));
  126. Ellipse(hdcMem, cxMove, cyMove, cxTotal - cxMove, cyTotal - cyMove);
  127. DeleteDC(hdcMem);
  128. DeleteObject(hBrush);
  129. return 0;
  130. case WM_TIMER:
  131. if (!hBitmap)
  132. break;
  133. hdc = GetDC(hwnd);
  134. hdcMem = CreateCompatibleDC(hdc);
  135. SelectObject(hdcMem, hBitmap);
  136. BitBlt(hdc, xCenter - cxTotal / 2,
  137. yCenter - cyTotal / 2, cxTotal, cyTotal,
  138. hdcMem, 0, 0, SRCCOPY);
  139. ReleaseDC(hwnd, hdc);
  140. DeleteDC(hdcMem);
  141. xCenter += cxMove;
  142. yCenter += cyMove;
  143. if ((xCenter + cxRadius >= cxClient) || (xCenter - cxRadius <= 0))
  144. cxMove = -cxMove;
  145. if ((yCenter + cyRadius >= cyClient) || (yCenter - cyRadius <= 0))
  146. cyMove = -cyMove;
  147. return 0;
  148. case WM_DESTROY:
  149. if (hBitmap)
  150. DeleteObject(hBitmap);
  151. KillTimer(hwnd, ID_TIMER);
  152. PostQuitMessage(0);
  153. return 0;
  154. default:
  155. }
  156. return DefWindowProc(hwnd, iMsg, wParam, lParam);
  157. }