CartonRippleView.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:9k
源码类别:

游戏

开发平台:

Visual C++

  1. // CartonRippleView.cpp : implementation of the CCartonRippleView class
  2. //
  3. #include "stdafx.h"
  4. #include "CartonRipple.h"
  5. #include "math.h"
  6. #include "CartonRippleDoc.h"
  7. #include "CartonRippleView.h"
  8. #include "SampleDlg.h"
  9. #include "MainFrm.h"
  10. #include <time.h>
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CCartonRippleView
  18. IMPLEMENT_DYNCREATE(CCartonRippleView, CView)
  19. BEGIN_MESSAGE_MAP(CCartonRippleView, CView)
  20. //{{AFX_MSG_MAP(CCartonRippleView)
  21. ON_WM_TIMER()
  22. ON_WM_LBUTTONDOWN()
  23. ON_WM_MOUSEMOVE()
  24. ON_COMMAND(ID_LOAD_FILE, OnLoadFile)
  25. ON_COMMAND(ID_RAIN_EFFECT, OnRainEffect)
  26. ON_COMMAND(ID_STONE_EFFECT, OnStoneEffect)
  27. ON_COMMAND(ID_SETTING_PARAMETER, OnSettingParameter)
  28. //}}AFX_MSG_MAP
  29. // Standard printing commands
  30. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  31. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  32. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  33. END_MESSAGE_MAP()
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CCartonRippleView construction/destruction
  36. CCartonRippleView::CCartonRippleView()
  37. {
  38. // TODO: add construction code here
  39. m_iMouseClickedRadius=50;
  40. m_iMouseClickedHeight=500;
  41. m_iMouseMoveRadius=5;
  42. m_iMouseMoveHeight=50;
  43. m_iRainFrequency=60;
  44. m_pParameterDlg=NULL;
  45. m_iRefreshFrequency=30;
  46. }
  47. CCartonRippleView::~CCartonRippleView()
  48. {
  49. if(m_pParameterDlg!=NULL)
  50. {
  51. delete m_pParameterDlg;
  52. }
  53. }
  54. BOOL CCartonRippleView::PreCreateWindow(CREATESTRUCT& cs)
  55. {
  56. // TODO: Modify the Window class or styles here by modifying
  57. //  the CREATESTRUCT cs
  58. return CView::PreCreateWindow(cs);
  59. }
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CCartonRippleView drawing
  62. void CCartonRippleView::OnDraw(CDC* pDC)
  63. {
  64. CCartonRippleDoc* pDoc = GetDocument();
  65. ASSERT_VALID(pDoc);
  66. // TODO: add draw code for native data here
  67. }
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CCartonRippleView printing
  70. BOOL CCartonRippleView::OnPreparePrinting(CPrintInfo* pInfo)
  71. {
  72. // default preparation
  73. return DoPreparePrinting(pInfo);
  74. }
  75. void CCartonRippleView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  76. {
  77. // TODO: add extra initialization before printing
  78. }
  79. void CCartonRippleView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  80. {
  81. // TODO: add cleanup after printing
  82. }
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CCartonRippleView diagnostics
  85. #ifdef _DEBUG
  86. void CCartonRippleView::AssertValid() const
  87. {
  88. CView::AssertValid();
  89. }
  90. void CCartonRippleView::Dump(CDumpContext& dc) const
  91. {
  92. CView::Dump(dc);
  93. }
  94. CCartonRippleDoc* CCartonRippleView::GetDocument() // non-debug version is inline
  95. {
  96. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCartonRippleDoc)));
  97. return (CCartonRippleDoc*)m_pDocument;
  98. }
  99. #endif //_DEBUG
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CCartonRippleView message handlers
  102. void CCartonRippleView::OnTimer(UINT nIDEvent) 
  103. {
  104. // TODO: Add your message handler code here and/or call default
  105. if(nIDEvent==1)
  106. {
  107. m_myWater.Render((DWORD*)m_bmpRenderSource.GetDIBits(),(DWORD*)m_bmpRenderTarget.GetDIBits());
  108. CClientDC dc(this);
  109. CPoint ptOrigin(0,0);
  110. m_bmpRenderTarget.Draw(&dc,ptOrigin);
  111. }
  112. if(nIDEvent==2)//rain
  113. {
  114. CPoint point;
  115. int count=rand()%8+2;//同时激活的雨滴数目
  116. while(count>0)
  117. {
  118. point.x = rand()%BMP_WIDTH;
  119. point.y = rand()%BMP_HEIGHT;
  120. //=5;
  121. //=50;
  122. //m_myWater.HeightBlob(point.x,point.y,10+rand()%20,60-rand()%20,m_myWater.m_iHpage);
  123. m_myWater.HeightBlob(point.x,point.y,m_iMouseMoveRadius+rand()%10,m_iMouseMoveHeight+rand()%10,m_myWater.m_iHpage);
  124. count--;
  125. }
  126. m_myWater.Render((DWORD*)m_bmpRenderSource.GetDIBits(),(DWORD*)m_bmpRenderTarget.GetDIBits());
  127. CClientDC dc(this);
  128. CPoint ptOrigin(0,0);
  129. m_bmpRenderTarget.Draw(&dc,ptOrigin);
  130. }
  131. CView::OnTimer(nIDEvent);
  132. }
  133. void CCartonRippleView::OnLButtonDown(UINT nFlags, CPoint point) 
  134. {
  135. // TODO: Add your message handler code here and/or call default
  136. point.y -= 20;
  137. point.y = BMP_HEIGHT-point.y;
  138. m_myWater.HeightBlob(point.x-15,point.y,m_iMouseClickedRadius,m_iMouseClickedHeight,
  139. m_myWater.m_iHpage);
  140. CView::OnLButtonDown(nFlags, point);
  141. }
  142. void CCartonRippleView::OnMouseMove(UINT nFlags, CPoint point) 
  143. {
  144. // TODO: Add your message handler code here and/or call default
  145. point.y -= 20;
  146. point.y = BMP_HEIGHT-point.y;
  147. m_myWater.HeightBlob(point.x -15,point.y,m_iMouseMoveRadius,m_iMouseMoveHeight,m_myWater.m_iHpage);
  148. m_myWater.Render((DWORD*)m_bmpRenderSource.GetDIBits(),(DWORD*)m_bmpRenderTarget.GetDIBits());
  149. CClientDC dc(this);
  150. CPoint ptOrigin(0,0);
  151. m_bmpRenderTarget.Draw(&dc,ptOrigin);
  152. CView::OnMouseMove(nFlags, point);
  153. }
  154. BOOL CCartonRippleView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
  155. {
  156. // TODO: Add your specialized code here and/or call the base class
  157. return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
  158. }
  159. void CCartonRippleView::OnLoadFile() 
  160. {
  161. // TODO: Add your command handler code here
  162. static bool first=true;
  163. if(!first)
  164. {
  165. KillTimer(1);
  166. KillTimer(2);
  167. first=true;
  168. m_bmpRenderSource.DeleteObject();
  169. m_bmpRenderTarget.DeleteObject();
  170. }
  171. CSampleDlg dlg;
  172. if(dlg.DoModal()==IDOK)
  173. {
  174. CPictureHolder myTmpPicture;
  175. switch(dlg.m_iSelected)
  176. {
  177. case 0:
  178. myTmpPicture.CreateFromBitmap(IDB_BITMAP1);
  179. break;
  180. case 1:
  181. myTmpPicture.CreateFromBitmap(IDB_BITMAP4);
  182. break;
  183. case 2:
  184. myTmpPicture.CreateFromBitmap(IDB_BITMAP3);
  185. break;
  186. case 3:
  187. myTmpPicture.CreateFromBitmap(IDB_BITMAP2);
  188. break;
  189. }
  190. m_bmpRenderSource.Create32BitFromPicture(&myTmpPicture,BMP_WIDTH,BMP_HEIGHT);
  191. m_bmpRenderTarget.Create32BitFromPicture(&myTmpPicture,BMP_WIDTH,BMP_HEIGHT);
  192. //创建水纹对象
  193. m_myWater.Create(BMP_WIDTH,BMP_HEIGHT);
  194. }
  195. if(first)
  196. {
  197. SetTimer(1,m_iRefreshFrequency,NULL);
  198. first=false;
  199. }
  200. CMenu *menu=((CMainFrame*)(AfxGetMainWnd()))->GetMenu();
  201. CMenu * sub =menu->GetSubMenu(3);
  202. sub->EnableMenuItem(ID_RAIN_EFFECT, MF_ENABLED);
  203. // sub->EnableMenuItem(ID_STONE_EFFECT,MF_ENABLED);
  204. }
  205. void CCartonRippleView::ResetRefreshFrequency() 
  206. {
  207. KillTimer(1);
  208. SetTimer(1,m_iRefreshFrequency,NULL);
  209. }
  210. void CCartonRippleView::OnRainEffect() 
  211. {
  212. // TODO: Add your command handler code here
  213. static bool first=true;
  214. if(!first)
  215. {
  216. KillTimer(2);
  217. CMenu *menu=((CMainFrame*)(AfxGetMainWnd()))->GetMenu();
  218. CMenu * sub =menu->GetSubMenu(3);
  219. sub->ModifyMenu(2,MF_STRING,0,"ssss");
  220. sub->EnableMenuItem(ID_RAIN_EFFECT, MF_ENABLED);
  221. sub->EnableMenuItem(ID_STONE_EFFECT,MF_ENABLED);
  222. first=true;
  223. }
  224. else
  225. {
  226. first=false;
  227. srand((unsigned int)time(NULL));
  228. SetTimer(2,m_iRainFrequency,NULL);
  229. }
  230. }
  231. //interface for class CParameterSettingDlg
  232. void CCartonRippleView::ResetRainEffect() 
  233. {
  234. // TODO: Add your command handler code here
  235. KillTimer(2);
  236. srand((unsigned int)time(NULL));
  237. SetTimer(2,m_iRainFrequency,NULL);
  238. }
  239. //这个函数现在负责填充障碍物
  240. void CCartonRippleView::OnStoneEffect() 
  241. {
  242. // TODO: Add your command handler code here
  243.     STARTUPINFO si;
  244.     PROCESS_INFORMATION pi;
  245.     ZeroMemory( &si, sizeof(si) );
  246.     si.cb = sizeof(si);
  247.     ZeroMemory( &pi, sizeof(pi) );
  248.     // Start the child process. 
  249.     if( !CreateProcess( NULL, // No module name (use command line). 
  250.         "ScanConverting.exe", // Command line. 
  251.         NULL,             // Process handle not inheritable. 
  252.         NULL,             // Thread handle not inheritable. 
  253.         FALSE,            // Set handle inheritance to FALSE. 
  254.         0,                // No creation flags. 
  255.         NULL,             // Use parent's environment block. 
  256.         NULL,             // Use parent's starting directory. 
  257.         &si,              // Pointer to STARTUPINFO structure.
  258.         &pi )             // Pointer to PROCESS_INFORMATION structure.
  259.     )
  260. {
  261. AfxMessageBox("创建子进程失败!");
  262. }
  263.     // Wait until child process exits.
  264.     WaitForSingleObject( pi.hProcess, INFINITE );
  265.     // Close process and thread handles. 
  266.     CloseHandle( pi.hProcess );
  267.     CloseHandle( pi.hThread );
  268. m_myWater.LoadBarrierData();
  269. }
  270. void CCartonRippleView::OnSettingParameter() 
  271. {
  272. // TODO: Add your command handler code here
  273. static bool first=true;
  274. if(first)
  275. {
  276. m_pParameterDlg= new CParameterSettingDlg;
  277. m_pParameterDlg->Init(this);
  278. if(!m_pParameterDlg->Create(IDD_DIALOG2,AfxGetMainWnd()))
  279. {
  280. AfxMessageBox("Error creating Dialog");
  281. }
  282. first=false;
  283. }
  284. m_pParameterDlg->ShowWindow(SW_SHOW);
  285. }