home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / windows / c / sprmgr20 / sprite / demo32 / spritvw.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-08  |  6.1 KB  |  305 lines

  1. // spritvw.cpp : implementation of the CSpritesView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "sprites.h"
  6.  
  7. #include "sprite.h"
  8. #include "sprmgr.h"
  9.  
  10. #include "owndrw.h"
  11.  
  12. #include "spritdoc.h"
  13. #include "spritvw.h"
  14.  
  15. #ifdef _DEBUG
  16. #undef THIS_FILE
  17. static char BASED_CODE THIS_FILE[] = __FILE__;
  18. #endif
  19.  
  20. #define LOOPSPERIMAGE    20
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CSpritesView
  24.  
  25. IMPLEMENT_DYNCREATE(CSpritesView, CView)
  26.  
  27. BEGIN_MESSAGE_MAP(CSpritesView, CView)
  28.     //{{AFX_MSG_MAP(CSpritesView)
  29.     ON_WM_LBUTTONDOWN()
  30.     ON_WM_MOUSEMOVE()
  31.     ON_WM_LBUTTONUP()
  32.     ON_WM_RBUTTONDOWN()
  33.     ON_WM_TIMER()
  34.     //}}AFX_MSG_MAP
  35. END_MESSAGE_MAP()
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CSpritesView construction/destruction
  39.  
  40. CSpritesView::CSpritesView()
  41. {
  42.     // TODO: add construction code here    
  43. }
  44.  
  45. CSpritesView::~CSpritesView()
  46. {
  47.     KillTimer( (UINT)101 );
  48. }
  49.  
  50. void CSpritesView::OnInitialUpdate()
  51. {
  52.     CClientDC dc(this);
  53.  
  54.     for(int id = 1; id < 6; id ++)
  55.     {
  56.         CSprite* pSprite;
  57.         if (id < 5)
  58.             pSprite = new CSprite(id, TRUE, FALSE );
  59.         else
  60.         {
  61.             pSprite = new COwnerDraw(id);
  62.             pSprite->InitSpriteInfo(&dc, CRect(400,200,450,250));
  63.             m_SpriteMgr.AddSprite(pSprite);
  64.             continue;
  65.         }
  66.  
  67.         CPoint pos;
  68.  
  69.         switch(id)
  70.         {
  71.             case 5:
  72.                 pos = CPoint(400,200);
  73.                 break;
  74.             case 1:
  75.                 pos = CPoint(0,0);
  76.                 break;
  77.             case 2:
  78.                 pos = CPoint(100,0);
  79.                 break;
  80.             case 3:
  81.                 pos = CPoint(50,0);
  82.                 break;
  83.             case 4:
  84.                 pos = CPoint(0,50);
  85.                 break;
  86.         }
  87.  
  88.         CBitmap bitmap;
  89.         bitmap.LoadBitmap(IDB_BITMAP1);
  90.  
  91.         pSprite->InitSpriteInfo(&dc, bitmap, LOOPSPERIMAGE, TRUE, RGB(0, 0, 255), pos, TRUE );
  92.  
  93.         bitmap.DeleteObject();
  94.         bitmap.LoadBitmap(IDB_BITMAP2);
  95.  
  96.         pSprite->AddSpriteImage(&dc, bitmap, LOOPSPERIMAGE);
  97.  
  98.         bitmap.DeleteObject();
  99.         bitmap.LoadBitmap(IDB_BITMAP3);
  100.  
  101.         pSprite->AddSpriteImage(&dc, bitmap, LOOPSPERIMAGE);
  102.  
  103.         bitmap.DeleteObject();
  104.         bitmap.LoadBitmap(IDB_BITMAP4);
  105.  
  106.         pSprite->AddSpriteImage(&dc, bitmap, LOOPSPERIMAGE);
  107.  
  108.         bitmap.DeleteObject();
  109.         bitmap.LoadBitmap(IDB_BITMAP5);
  110.  
  111.         pSprite->AddSpriteImage(&dc, bitmap, LOOPSPERIMAGE);
  112.     
  113.         bitmap.DeleteObject();
  114.         bitmap.LoadBitmap(IDB_BITMAP6);
  115.  
  116.         pSprite->AddSpriteImage(&dc, bitmap, LOOPSPERIMAGE);
  117.  
  118.         bitmap.DeleteObject();
  119.         bitmap.LoadBitmap(IDB_BITMAP7);
  120.  
  121.         pSprite->AddSpriteImage(&dc, bitmap, LOOPSPERIMAGE);
  122.     
  123.         bitmap.DeleteObject();
  124.         bitmap.LoadBitmap(IDB_BITMAP8);
  125.  
  126.         pSprite->AddSpriteImage(&dc, bitmap, LOOPSPERIMAGE);
  127.  
  128.         bitmap.DeleteObject();
  129.  
  130.         m_SpriteMgr.AddSprite(pSprite);
  131.     }
  132. }
  133.  
  134.  
  135. /////////////////////////////////////////////////////////////////////////////
  136. // CSpritesView drawing
  137.  
  138. void CSpritesView::OnDraw(CDC* pDC)
  139. {
  140.     CSpritesDoc* pDoc = GetDocument();
  141.  
  142.     // TODO: add draw code here
  143.  
  144.     CRect clipRect;
  145.     pDC->GetClipBox(clipRect);
  146.     
  147.     if(clipRect.IsRectEmpty())
  148.         GetClientRect(clipRect);
  149.                       
  150.     for( int i = 0; i < 100; i++)
  151.     {
  152.         pDC->MoveTo(i*10,clipRect.top);
  153.         pDC->LineTo(i*10,clipRect.bottom);
  154.     }
  155.     for(i = 0; i < 100; i++)
  156.     {
  157.         pDC->MoveTo(clipRect.left, i*10);
  158.         pDC->LineTo(clipRect.right, i*10);
  159.     }    
  160.  
  161.     m_SpriteMgr.OnPaint(pDC);
  162. }
  163.  
  164.  
  165.  
  166. /////////////////////////////////////////////////////////////////////////////
  167. // CSpritesView diagnostics
  168.  
  169. #ifdef _DEBUG
  170. void CSpritesView::AssertValid() const
  171. {
  172.     CView::AssertValid();
  173. }
  174.  
  175. void CSpritesView::Dump(CDumpContext& dc) const
  176. {
  177.     CView::Dump(dc);
  178. }
  179.  
  180. CSpritesDoc* CSpritesView::GetDocument() // non-debug version is inline
  181. {
  182.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSpritesDoc)));
  183.     return (CSpritesDoc*) m_pDocument;
  184. }
  185.  
  186. #endif //_DEBUG
  187.  
  188. /////////////////////////////////////////////////////////////////////////////
  189. // CSpritesView message handlers
  190.  
  191. void CSpritesView::OnLButtonDown(UINT nFlags, CPoint point)
  192. {
  193.     // TODO: Add your message handler code here and/or call default
  194.  
  195.     CClientDC dc(this);
  196.     m_SpriteMgr.StartDrag(this, point, TRUE, &dc);
  197.     
  198.     CView::OnLButtonDown(nFlags, point);
  199. }
  200.  
  201. void CSpritesView::OnMouseMove(UINT nFlags, CPoint point)
  202. {
  203.     // TODO: Add your message handler code here and/or call default
  204.  
  205.     if(m_SpriteMgr.IsDragging())
  206.     {
  207.         CClientDC dc(this);
  208.         m_SpriteMgr.OnDrag(&dc, point);
  209.     }
  210.  
  211.     CView::OnMouseMove(nFlags, point);
  212. }
  213.  
  214. void CSpritesView::OnLButtonUp(UINT nFlags, CPoint point)
  215. {
  216.     // TODO: Add your message handler code here and/or call default
  217.  
  218.     if(m_SpriteMgr.IsDragging())
  219.     {
  220.         CClientDC dc(this);
  221.         m_SpriteMgr.EndDrag(&dc, point);
  222.     }
  223.  
  224.     CView::OnLButtonUp(nFlags, point);
  225. }
  226.  
  227. void CSpritesView::OnRButtonDown(UINT nFlags, CPoint point)
  228. {
  229.     // TODO: Add your message handler code here and/or call default
  230.         
  231.     SetTimer( (UINT)101, (UINT)100, NULL );
  232.  
  233.     CView::OnRButtonDown(nFlags, point);
  234. }
  235.  
  236. void CSpritesView::OnTimer(UINT nIDEvent)
  237. {
  238.     // TODO: Add your message handler code here and/or call default
  239.  
  240.     if(nIDEvent != 101)
  241.         return;
  242.  
  243.     CClientDC dc(this);
  244.  
  245.     CRect spritePos;
  246.     CPoint newPos;
  247.  
  248.     m_SpriteMgr[1]->GetSpritePos(spritePos);
  249.     
  250.     newPos = spritePos.TopLeft() + CSize(7, 7);
  251.  
  252.     if(newPos.x >= 100)
  253.         newPos = CPoint(0, 0);
  254.  
  255. #ifdef _DEFER
  256.     m_SpriteMgr.BeginDeferSpritePos();
  257.     m_SpriteMgr.DeferSpritePos(1, newPos);
  258. #else
  259.     m_SpriteMgr.MoveSprite(1, &dc, newPos);
  260. #endif
  261.  
  262.  
  263.     m_SpriteMgr[2]->GetSpritePos(spritePos);
  264.     
  265.     newPos = spritePos.TopLeft() + CSize(-9, 7);
  266.     
  267.     if(newPos.x <= 0)
  268.         newPos = CPoint(100, 0);
  269.  
  270. #ifdef _DEFER
  271.     m_SpriteMgr.DeferSpritePos(2, newPos);
  272. #else    
  273.     m_SpriteMgr.MoveSprite(2, &dc, newPos);    
  274. #endif
  275.  
  276.     m_SpriteMgr[3]->GetSpritePos(spritePos);
  277.     
  278.     newPos = spritePos.TopLeft() + CSize(0, 7);
  279.  
  280.     if(newPos.y >= 100)
  281.         newPos = CPoint(50, 0);
  282.  
  283. #ifdef _DEFER
  284.     m_SpriteMgr.DeferSpritePos(3, newPos);
  285. #else
  286.     m_SpriteMgr.MoveSprite(3, &dc, newPos);
  287. #endif
  288.  
  289.     m_SpriteMgr[4]->GetSpritePos(spritePos);
  290.     
  291.     newPos = spritePos.TopLeft() + CSize(9, 0);
  292.     
  293.     if(newPos.x >= 100)
  294.         newPos = CPoint(0, 50);
  295.  
  296. #ifdef _DEFER
  297.     m_SpriteMgr.DeferSpritePos(4, newPos);
  298.     m_SpriteMgr.EndDeferSpritePos(&dc);
  299. #else
  300.     m_SpriteMgr.MoveSprite(4, &dc, newPos);    
  301. #endif
  302.  
  303.     CView::OnTimer(nIDEvent);
  304. }
  305.