Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C++

A Fast Version of Conway's Game of Life with Thread and DirectX Draw

Rate me:
Please Sign up or sign in to vote.
4.42/5 (14 votes)
14 Apr 2009CPOL4 min read 57.1K   904   30  
A fast version of Conway's Game of Life with thread and DirectX draw
// SortAndLifeDoc.cpp : implementation of the CSortAndLifeDoc class
//

#include "stdafx.h"
#include "SortAndLife.h"

#include "SortAndLifeDoc.h"
#include "MainFrm.h"

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

int CSortAndLifeDoc::m_bDocNumber = 0;
/////////////////////////////////////////////////////////////////////////////
// CSortAndLifeDoc

IMPLEMENT_DYNCREATE(CSortAndLifeDoc, CDocument)

BEGIN_MESSAGE_MAP(CSortAndLifeDoc, CDocument)
	//{{AFX_MSG_MAP(CSortAndLifeDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSortAndLifeDoc construction/destruction

CSortAndLifeDoc::CSortAndLifeDoc()
{
	// TODO: add one-time construction code here
	m_barColor = RGB(255,10,80);
	m_cellColor = RGB(10,255,80);
	m_nSleep = 20;
	m_bGrid = FALSE;
	InitializeCriticalSection(&m_cr);

	GetRandomNumberArray(&m_nNumberArray,CONST_INT_BARNUMBER);
	initializeCellLife();
	if(m_bDocNumber==0)
	{
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_SPEEDUP,FALSE);
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_SLOWDOWN,FALSE);
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_COLOR,FALSE);
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_START,FALSE);
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_SEED,FALSE);
	
	}
	m_bDocNumber++;
}

CSortAndLifeDoc::~CSortAndLifeDoc()
{
	m_bDocNumber--;
	if(m_bDocNumber <= 0)
	{
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_START,TRUE);
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_SEED,TRUE);
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_SPEEDUP,TRUE);
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_SLOWDOWN,TRUE);
		((CMainFrame *)AfxGetApp()->GetMainWnd())->m_wndToolBar.GetToolBarCtrl().HideButton(ID_BUTTON_COLOR,TRUE);
	
	}
	DeleteCriticalSection(&m_cr);
}

BOOL CSortAndLifeDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CSortAndLifeDoc serialization

void CSortAndLifeDoc::Serialize(CArchive& ar)
{
	int i,j;

	if (ar.IsStoring())
	{
		ar <<  m_nSleep;
		ar <<  m_cellColor;
		ar <<  m_barColor;
		for( i=0; i<CONST_INT_BARNUMBER; i++)
			ar << m_nNumberArray[i];
		for(i=0; i<CONST_INT_GRIDNUMBER; i++)
			for( j=0; j<CONST_INT_GRIDNUMBER; j++)
			ar << m_cCellArray[i][j];
		ar <<  m_bGrid;
		// TODO: add storing code here
	}
	else
	{
		ar >>  m_nSleep;
		ar >>  m_cellColor;
		ar >>  m_barColor;
		for(i=0; i<CONST_INT_BARNUMBER; i++)
			ar >> m_nNumberArray[i];
		for( i=0; i<CONST_INT_GRIDNUMBER; i++)
			for(j=0; j<CONST_INT_GRIDNUMBER; j++)
			ar >> m_cCellArray[i][j];
		ar >>  m_bGrid;
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CSortAndLifeDoc diagnostics

#ifdef _DEBUG
void CSortAndLifeDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CSortAndLifeDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSortAndLifeDoc commands

void CSortAndLifeDoc::SetNewRandom()
{
	GetRandomNumberArray(&m_nNumberArray,CONST_INT_BARNUMBER);
	initializeCellLife();
}

void CSortAndLifeDoc::GetRandomNumberArray(void *m_nNumberArray,int nSize)
{
    int *pArray = (int *)m_nNumberArray;
	for(int i = 0; i < nSize;i++)
	{
		pArray[i] = rand()%CONST_INT_BARNUMBER;
		if(pArray[i] <= 0)
			pArray[i] = 1;
	}
}
void CSortAndLifeDoc::Clear()
{
	LockData();
	memset(m_cCellArray,0,sizeof(m_cCellArray));
	UnLockData();
}

void CSortAndLifeDoc::CreateCell(int x, int y)
{
	if((x >= CONST_INT_GRIDNUMBER) || (y >= CONST_INT_GRIDNUMBER))
		return;
	LockData();
	m_cCellArray[x][y] = !m_cCellArray[x][y];
	UnLockData();
}

void CSortAndLifeDoc::initializeCellLife()
{
	int z;

	LockData();
	for(int y=0; y<CONST_INT_GRIDNUMBER;y++)
	{
		for(int x=0;x<CONST_INT_GRIDNUMBER;x++)
		{
			z = (int) (10.0*rand()/RAND_MAX+1.0);
			if(z < 10)
					m_cCellArray[y][x] = 0;
			else
					m_cCellArray[y][x] = 1;
		}
	}
	UnLockData();
}

void CSortAndLifeDoc::CopyArray(char *doubleArray)
{
	memcpy(&m_cCellArray[0][0],doubleArray,CONST_INT_GRIDNUMBER*CONST_INT_GRIDNUMBER);
}

int CSortAndLifeDoc::Neighbors(int y,int x)
{
	
	int sum=0,i;
	if((x-1) >= 0)
	{
		for(i=y-1;i<=y+1;i++)
		{
			if((i >= 0) && (i < CONST_INT_GRIDNUMBER))
			{
				if(m_cCellArray[i][x-1])
					sum++;
			}
		}
	}

	if((x+1)<(CONST_INT_GRIDNUMBER))
	{
		for(i=y-1;i <=y+1;i++)
		{
			if((i>=0) && (i < CONST_INT_GRIDNUMBER))
			{
				if(m_cCellArray[i][x+1])
					sum++;
			}
		}
	}

	if((y-1) >= 0)
	{
		if(m_cCellArray[y-1][x])
			sum++;
	}

	if((y+1) < CONST_INT_GRIDNUMBER)
	{
		if(m_cCellArray[y+1][x])
			sum++;
	}
	
	return sum;
}

void CSortAndLifeDoc::StepGeneration()
{
	int		i, j, x;

	LockData();
	for(i=0;i<CONST_INT_GRIDNUMBER;i++)
	{
		for(j=0;j<CONST_INT_GRIDNUMBER;j++)
		{
			x = Neighbors(i,j);
			if(m_cCellArray[i][j])
				m_cShadowCellArray[i][j] = ((x == 2) || (x == 3)) ? 1 :0;
			else
				m_cShadowCellArray[i][j] = (x == 3) ? 1:0;
	    }
	}
	CopyArray(&m_cShadowCellArray[0][0]);	
	UnLockData();
}

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
Founder SmartTick Software Inc.
Canada Canada
Jerry Jiang(BOLIANG JIANG)

A passionate software developer since 1992

Education:Master of Computer Science.

jerry@smarttick.com

Comments and Discussions