⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aseview.cpp

📁 A*算法的演示程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* Copyright (C) William van der Sterren, 2002. 
 * All rights reserved worldwide.
 *
 * This software is provided "as is" without express or implied
 * warranties. You may freely copy and compile this source into
 * applications you distribute provided that the copyright text
 * below is included in the resulting source code, for example:
 * "Portions Copyright (C) William van der Sterren, 2002"
 */

/* Copyright (C) James Matthews, 2001. 
 * All rights reserved worldwide.
 *
 * This software is provided "as is" without express or implied
 * warranties. You may freely copy and compile this source into
 * applications you distribute provided that the copyright text
 * below is included in the resulting source code, for example:
 * "Portions Copyright (C) James Matthews, 2001"
 */

// aseView.cpp : implementation of the CAseView class
//

#include "stdafx.h"
#include "ase.h"

#include "aseDoc.h"
#include "aseView.h"

#define C_ASSERT(e) typedef char __C_ASSERT__[(e)?1:-1]

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define ci(x,y) x*ASE_BOARDY+y

/////////////////////////////////////////////////////////////////////////////
// CAseView

IMPLEMENT_DYNCREATE(CAseView, CView)

BEGIN_MESSAGE_MAP(CAseView, CView)
	//{{AFX_MSG_MAP(CAseView)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_ERASEBKGND()
	ON_WM_RBUTTONDBLCLK()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAseView construction/destruction

//! \todo remove first 4 entries
static COLORREF crBrushes [] = {
	RGB(255,000,000), // start
	RGB(000,255,000)  // end
};



CAseView::CAseView()
{
	m_bDragging = false;
	m_uBrushType = 0;
}

CAseView::~CAseView()
{
}

BOOL CAseView::PreCreateWindow(CREATESTRUCT& cs)
{
	return CView::PreCreateWindow(cs);
}


void CAseView::DisplayWaitCursor()
  {
    BeginWaitCursor();
  }


void CAseView::DisplayDefaultCursor()
  {
    EndWaitCursor();
  }

/////////////////////////////////////////////////////////////////////////////
// CAseView drawing

void CAseView::OnDraw(CDC* pDC)
{
	CAseDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	CRect rect;
	GetClientRect(rect);

	CDC memdc;
	CBitmap bmp, *oldbmp;
	CPen *oldpen, pen(PS_SOLID, 1, RGB(255,0,255));

	memdc.CreateCompatibleDC(GetDC());
	bmp.CreateDiscardableBitmap(pDC, rect.Width(), rect.Height());
	oldbmp = memdc.SelectObject(&bmp);
	oldpen = memdc.SelectObject(&pen);
	
	memdc.FillSolidRect(rect, RGB(255,255,255));

	int  i;
	int  dx = rect.Width();
	int  dy = rect.Height();
	bool bSearch    = pDoc->DoesDisplaySearchSpace();
  bool bThreat    = pDoc->DoesDisplayThreatPositions();
  bool bLOF       = pDoc->DoesDisplayThreatLinesOfFire();
  bool bApproxLOF = pDoc->DoesDisplayApproximatedLinesOfFire();

	// Cycle through the board and draw any squares
  const ASE_TerrainBoard*          terrain;
  const ASE_ThreatBoard*           threats;
  const ASE_SearchBoard*           searchspace;
  const ASE_LOSApproximationBoard* losapprox;
  terrain     = pDoc->GetTerrainBoard();
  threats     = pDoc->GetThreatBoard();
  searchspace = pDoc->GetSearchSpaceBoard();
  losapprox   = pDoc->GetLOSApproximationBoard();

  // first draw non-boxed contents (lof, search space)
  unsigned int row;
  unsigned int col;
  for ( row = 0; row < terrain->GetNumberOfRows(); ++row )
    {
      for ( col = 0; col < terrain->GetNumberOfColumns(); ++col )
        {
          unsigned int idx;
          idx = terrain->GetIndexForRowColumn(row, col);

          // ignore boxed terrain here 
          if ( !terrain->IsEmptyCell(idx) )
            continue;

          ASE_Board::CellValue threattype;
          // clip to threat lof
          threattype = min(1, threats->GetCellValue(idx));

          if (   ( bSearch ) 
              && ( !searchspace->IsEmptyCell(idx) )
             )
            {
              // empty terrain, so solely look at lof
              if ( bApproxLOF )
                {
                  threattype  = losapprox->GetCellValue(idx);
                  threattype  = min(2, threattype);
                }
              else
              if ( !bLOF )
                {
                  threattype = 0;
                }
              DrawRectangleNB(row, col, &memdc, 
                              searchspace->GetCellColorForOffset(idx, threattype) 
                             );
            }
          else 
          if (   ( bApproxLOF ) 
              && ( !losapprox->IsEmptyCell(idx) )
             )
            {
              // empty terrain, only display approx lof here (even if it might be a threat position)
              DrawRectangleNB(row, col, &memdc, losapprox->GetCellColorForOffset(idx, 0));
            }
          else
          if (   ( bLOF ) 
              && ( !threats->IsEmptyCell(idx) )
             )
            {
              // empty terrain, only display lof here (even if it might be a threat position)
              DrawRectangleNB(row, col, &memdc, threats->GetCellColorForOffset(idx, 0));
            }
        }
    }

  for ( row = 0; row < terrain->GetNumberOfRows(); ++row )
    {
      for ( col = 0; col < terrain->GetNumberOfColumns(); ++col )
        {
          unsigned int idx;
          idx = terrain->GetIndexForRowColumn(row, col);

          // ignore non-boxed terrain here 
          if (   ( terrain->IsEmptyCell(idx) )
              && ( !threats->IsThreatPosition(idx) )
             )
            continue;

          if (   ( bSearch ) 
              && ( !searchspace->IsEmptyCell(idx) )
             )
            {
              ASE_Board::CellValue terraintype;
              terraintype = terrain->GetCellValue(idx);

              ASE_Board::CellValue threattype;
              threattype = threats->GetCellValue(idx);
              if (   ( threattype != 0 )
                  && (   (   ( !bLOF )
                          && ( !threats->IsThreatPosition(idx) )
                         )
                      || (   ( !bThreat )
                          && ( threats->IsThreatPosition(idx) )
                         )
                     )
                 )
                {
                  threattype = 0;
                }
              else
                {
                  threattype = 1;
                }
              
              ASE_Board::CellValue offset;
              offset = max(terraintype, threattype);

              DrawRectangleNB(row, col, &memdc, 
                              searchspace->GetCellColorForOffset(idx, offset) 
                             );
            }
          else
          if (   ( !threats->IsEmptyCell(idx) )
              && (   (   ( bThreat ) 
                      && ( threats->IsThreatPosition(idx) )
                     )
                  || (   ( bLOF )
                      && ( !terrain->IsEmptyCell(idx) )
                     )  
                 )
             )
            {
              ASE_Board::CellValue terraintype;
              terraintype = terrain->GetCellValue(idx);

              DrawRectangle(row, col, &memdc, 
                            threats->GetCellColorForOffset(idx, terraintype) 
                           );
            }
          else
          if ( !terrain->IsEmptyCell(idx) )
            {
				      DrawRectangle(row, col, &memdc, terrain->GetCellColor(idx));
            }
        }
    }

	// Draw start, end and debug points
	if (m_cStart.x != -1) DrawRectangle(m_cStart.x, m_cStart.y, &memdc, crBrushes[0]);
	if (m_cEnd.x != -1)   DrawRectangle(m_cEnd.x, m_cEnd.y, &memdc, crBrushes[1]);

  // set pen for grid
	CPen  gridpen(PS_SOLID, 0, RGB(192, 192, 192));
  CPen* oldgridpen;

	oldgridpen   = memdc.SelectObject(&gridpen);


	// Draw the grid...
	for (i=0; i<dx; i+=8) 
    {
		  for (int j=0;j<dy;j+=8) 
        {
			    memdc.SetPixel(i, j, RGB(192, 192, 192));

          if (   ( 0 == (i % 64) )
              && ( 0 == (j % 64) )
             )
            {
			        memdc.MoveTo(i - 2, j);
			        memdc.LineTo(i + 3, j);
			        memdc.MoveTo(i, j - 2);
			        memdc.LineTo(i, j + 3);
            }
		    }
	  }
	memdc.SelectObject(oldgridpen);

	// Draw route if necessary
	if ( pDoc->DoesDisplayRoute() ) 
    DrawRoute(&memdc);

	if (m_cHilight.x != -1) {
		DrawRectangle(m_cHilight.x, m_cHilight.y, &memdc, RGB(0,0,255));
		
		CPoint centre;
		centre.x = m_cHilight.x*ASE_GRIDSIZE+(ASE_GRIDSIZE/2);
		centre.y = m_cHilight.y*ASE_GRIDSIZE+(ASE_GRIDSIZE/2);

		memdc.MoveTo(centre);

		unsigned int numchildren = m_cHilightNode->GetNumberOfChildren();
		_asNode *temp;
	
		for ( unsigned int i=0; i<numchildren; i++ ) 
      {
			  temp = m_cHilightNode->GetChild(i);
			  memdc.LineTo(temp->GetX() * ASE_GRIDSIZE + (ASE_GRIDSIZE/2),
				             temp->GetY() * ASE_GRIDSIZE + (ASE_GRIDSIZE/2));
			  memdc.MoveTo(centre);
		  }
	}

	CPoint bp = pDoc->GetBreakpoint();
	if (bp.x != -1) DrawCircle(bp.x, bp.y, &memdc, RGB(128,0,0));

	pDC->BitBlt(0,0,rect.Width(), rect.Height(), &memdc, 0, 0, SRCCOPY);

	memdc.SelectObject(oldpen);
	memdc.SelectObject(oldbmp);
}

void CAseView::DrawRectangle(int x, int y, CDC *pDC, COLORREF cr)
{
	CBrush br(RGB(0,0,0));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -