PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Samples/Chap15/Apollo11/Apollo11.d

http://github.com/AndrejMitrovic/DWinProgramming
D | 231 lines | 178 code | 42 blank | 11 comment | 9 complexity | e2977b4f32104d07bd56c279a3da6382 MD5 | raw file
  1. /+
  2. + Copyright (c) Charles Petzold, 1998.
  3. + Ported to the D Programming Language by Andrej Mitrovic, 2011.
  4. +/
  5. module Apollo11;
  6. import core.memory;
  7. import core.runtime;
  8. import core.thread;
  9. import std.conv;
  10. import std.math;
  11. import std.range;
  12. import std.string;
  13. import std.utf;
  14. auto toUTF16z(S)(S s)
  15. {
  16. return toUTFz!(const(wchar)*)(s);
  17. }
  18. pragma(lib, "gdi32.lib");
  19. import core.sys.windows.windef;
  20. import core.sys.windows.winuser;
  21. import core.sys.windows.wingdi;
  22. import core.sys.windows.winbase;
  23. import DibFile;
  24. string appName = "Apollo11";
  25. string description = "Apollo 11";
  26. HINSTANCE hinst;
  27. extern (Windows)
  28. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  29. {
  30. int result;
  31. try
  32. {
  33. Runtime.initialize();
  34. result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
  35. Runtime.terminate();
  36. }
  37. catch (Throwable o)
  38. {
  39. MessageBox(null, o.toString().toUTF16z, "Error", MB_OK | MB_ICONEXCLAMATION);
  40. result = 0;
  41. }
  42. return result;
  43. }
  44. int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
  45. {
  46. hinst = hInstance;
  47. HACCEL hAccel;
  48. HWND hwnd;
  49. MSG msg;
  50. WNDCLASS wndclass;
  51. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  52. wndclass.lpfnWndProc = &WndProc;
  53. wndclass.cbClsExtra = 0;
  54. wndclass.cbWndExtra = 0;
  55. wndclass.hInstance = hInstance;
  56. wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  57. wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  58. wndclass.hbrBackground = cast(HBRUSH) GetStockObject(WHITE_BRUSH);
  59. wndclass.lpszMenuName = appName.toUTF16z;
  60. wndclass.lpszClassName = appName.toUTF16z;
  61. if (!RegisterClass(&wndclass))
  62. {
  63. MessageBox(NULL, "This program requires Windows NT!", appName.toUTF16z, MB_ICONERROR);
  64. return 0;
  65. }
  66. hwnd = CreateWindow(appName.toUTF16z, // window class name
  67. description.toUTF16z, // window caption
  68. WS_OVERLAPPEDWINDOW, // window style
  69. CW_USEDEFAULT, // initial x position
  70. CW_USEDEFAULT, // initial y position
  71. CW_USEDEFAULT, // initial x size
  72. CW_USEDEFAULT, // initial y size
  73. NULL, // parent window handle
  74. NULL, // window menu handle
  75. hInstance, // program instance handle
  76. NULL); // creation parameters
  77. ShowWindow(hwnd, iCmdShow);
  78. UpdateWindow(hwnd);
  79. while (GetMessage(&msg, NULL, 0, 0))
  80. {
  81. TranslateMessage(&msg);
  82. DispatchMessage(&msg);
  83. }
  84. return msg.wParam;
  85. }
  86. extern (Windows)
  87. LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
  88. {
  89. scope (failure) assert(0);
  90. static BITMAPFILEHEADER *[2] pbmfh;
  91. static BITMAPINFO *[2] pbmi;
  92. static BYTE *[2] pBits;
  93. static int cxClient, cyClient;
  94. static int[2] cxDib, cyDib;
  95. HDC hdc;
  96. PAINTSTRUCT ps;
  97. switch (message)
  98. {
  99. case WM_CREATE:
  100. pbmfh[0] = DibLoadImage("Apollo11.bmp");
  101. pbmfh[1] = DibLoadImage("ApolloTD.bmp");
  102. if (pbmfh[0] == NULL || pbmfh[1] == NULL)
  103. {
  104. MessageBox(hwnd, "Cannot load DIB file", appName.toUTF16z, 0);
  105. return 0;
  106. }
  107. // Get pointers to the info structure && the bits
  108. pbmi [0] = cast(BITMAPINFO *)(pbmfh[0] + 1);
  109. pbmi [1] = cast(BITMAPINFO *)(pbmfh[1] + 1);
  110. pBits [0] = cast(BYTE *)pbmfh[0] + pbmfh[0].bfOffBits;
  111. pBits [1] = cast(BYTE *)pbmfh[1] + pbmfh[1].bfOffBits;
  112. // Get the DIB width and height (assume BITMAPINFOHEADER)
  113. // Note that cyDib is the absolute value of the header value!!!
  114. cxDib [0] = pbmi[0].bmiHeader.biWidth;
  115. cxDib [1] = pbmi[1].bmiHeader.biWidth;
  116. cyDib [0] = abs(pbmi[0].bmiHeader.biHeight);
  117. cyDib [1] = abs(pbmi[1].bmiHeader.biHeight);
  118. return 0;
  119. case WM_SIZE:
  120. cxClient = LOWORD(lParam);
  121. cyClient = HIWORD(lParam);
  122. return 0;
  123. case WM_PAINT:
  124. hdc = BeginPaint(hwnd, &ps);
  125. // Bottom-up DIB full size
  126. SetDIBitsToDevice(hdc,
  127. 0, // xDst
  128. cyClient / 4, // yDst
  129. cxDib[0], // cxSrc
  130. cyDib[0], // cySrc
  131. 0, // xSrc
  132. 0, // ySrc
  133. 0, // first scan line
  134. cyDib[0], // number of scan lines
  135. pBits[0],
  136. pbmi[0],
  137. DIB_RGB_COLORS);
  138. // Bottom-up DIB partial
  139. SetDIBitsToDevice(hdc,
  140. 240, // xDst
  141. cyClient / 4, // yDst
  142. 80, // cxSrc
  143. 166, // cySrc
  144. 80, // xSrc
  145. 60, // ySrc
  146. 0, // first scan line
  147. cyDib[0], // number of scan lines
  148. pBits[0],
  149. pbmi[0],
  150. DIB_RGB_COLORS);
  151. // Top-down DIB full size
  152. SetDIBitsToDevice(hdc,
  153. 340, // xDst
  154. cyClient / 4, // yDst
  155. cxDib[0], // cxSrc
  156. cyDib[0], // cySrc
  157. 0, // xSrc
  158. 0, // ySrc
  159. 0, // first scan line
  160. cyDib[0], // number of scan lines
  161. pBits[0],
  162. pbmi[0],
  163. DIB_RGB_COLORS);
  164. // Top-down DIB partial
  165. SetDIBitsToDevice(hdc,
  166. 580, // xDst
  167. cyClient / 4, // yDst
  168. 80, // cxSrc
  169. 166, // cySrc
  170. 80, // xSrc
  171. 60, // ySrc
  172. 0, // first scan line
  173. cyDib[1], // number of scan lines
  174. pBits[1],
  175. pbmi[1],
  176. DIB_RGB_COLORS);
  177. EndPaint(hwnd, &ps);
  178. return 0;
  179. case WM_DESTROY:
  180. if (pbmfh[0])
  181. GC.free(pbmfh[0]);
  182. if (pbmfh[1])
  183. GC.free(pbmfh[1]);
  184. PostQuitMessage(0);
  185. return 0;
  186. default:
  187. }
  188. return DefWindowProc(hwnd, message, wParam, lParam);
  189. }