Click here to Skip to main content
15,881,898 members
Articles / Multimedia / DirectX

Falling Blocks

Rate me:
Please Sign up or sign in to vote.
4.95/5 (11 votes)
17 Apr 2008CPOL 270.2K   9.2K   72  
A game written using Visual C++ and DirectX.
// FlooredBlocks.cpp: implementation of the CFlooredBlocks class.
//
//////////////////////////////////////////////////////////////////////
#include <windows.h>
#include "BlockList.h"
#include "FlooredBlocks.h"
#include "Resource.h"

extern HINSTANCE g_hInstance;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CFlooredBlocks::CFlooredBlocks(RECT rcBoundary)
{
	m_rcBoundary=rcBoundary;

}

CFlooredBlocks::~CFlooredBlocks()
{

}

bool CFlooredBlocks::IsOccupied(short nX, short nY)
{
	return CBlockList::IsOccupied(nX,nY);
}

void CFlooredBlocks::Display()
{
	CBlockList::Display();
}

bool CFlooredBlocks::Insert(CBlockList &BlockList)
{
	SNODE* pCurr = BlockList.m_pListHead->pNext;
	while (pCurr) {
		if (CBlockList::Insert(pCurr->Block) == false)
			return false;
		pCurr= pCurr->pNext;
	}
	return true;
}

bool CFlooredBlocks::IsGameOver()
{
	for (short nI=0; nI <= m_rcBoundary.right; nI++) {
		if (CBlockList::IsOccupied(nI, 1))
			return true;
	}
	return false;
}

////////////////////////////////////////////////////////
// Returns true if the give location already 
// contains a block
///////////////////////////////////////////////////////
bool CFlooredBlocks::IsOccupied(CBlockList &BlockList, short nX, short nY)
{
	SNODE* pCurr= BlockList.m_pListHead->pNext;
	while (pCurr) {
		if (IsOccupied(pCurr->Block.nX+nX,pCurr->Block.nY+nY)) 
			return true;
		pCurr = pCurr->pNext;
	}
	return false;
}

/////////////////////////////////////////
// Increment all Y < nY
// Used when the floored bricks must fall
/////////////////////////////////////////
void CFlooredBlocks::IncrementYabove(short nY)
{
	SNODE* pCurr = m_pListHead->pNext;
	while (pCurr) {
		if (pCurr->Block.nY < nY)
			pCurr->Block.nY++;
		pCurr= pCurr->pNext;
	}
}
//////////////////////////////////////////////////////////////////
//  Checks and removes any completed rows of blocks
//////////////////////////////////////////////////////////////////
short CFlooredBlocks::CheckAndRemoveContinuousBlocks()
{
	SNODE* pCurr= m_pListHead->pNext;
	SNODE* pPrev= m_pListHead;
	short nRowsDeleted=0;
	short nX1 = (short) m_rcBoundary.left;
	short nX2 = (short) m_rcBoundary.right;

	while (pCurr) {
		while (pCurr && pCurr->Block.nX != nX1) {
			pPrev=pCurr;
			pCurr = pCurr->pNext;
		}
		if (pCurr) {
			SNODE* pBegin= pPrev; // pBegin->Next actually contains the begining.
			while (pCurr && pCurr->pNext && pBegin->pNext->Block.nY == pCurr->Block.nY 
				&& (pCurr->Block.nX+1) == (pCurr->pNext->Block.nX)) {
				pPrev = pCurr;
				pCurr=pCurr->pNext;
			}
			if (pBegin->pNext->Block.nX == nX1 && 
				pBegin->pNext->Block.nY == pPrev->Block.nY &&
				pCurr->Block.nX == nX2)  {       // Found a row to remove
				nRowsDeleted++;
				SNODE* pDelete = pBegin->pNext;
				pCurr=pCurr->pNext;   // Move pointer to next location since the current will be deleted
				IncrementYabove(pBegin->pNext->Block.nY);
				while (pDelete && pDelete != pCurr) {
					SNODE* pDel= pDelete;
					pDelete=pDelete->pNext;
				//	delete pDel;
				}
				pBegin->pNext = pCurr;
	            pCurr= m_pListHead->pNext;
	            pPrev= m_pListHead;
			}
			pPrev = pCurr;
			pCurr=pCurr->pNext;
		}
	}
	if (nRowsDeleted) {
		::PlaySound(MAKEINTRESOURCE( IDR_SOUNDLINECOMPLETE), g_hInstance, SND_RESOURCE | SND_ASYNC);
	}
	return nRowsDeleted;

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions