文档视图结构
文档/视图体系结构是一个神奇的架构,通过这个架构,我们可以分离应用程序的数据和显示。文档包含了全部的数据,视图从文档获取数据并且显示给用户。针对这个游戏例子,我们得数据是游戏板和有颜色的砖块以及消耗的时间等信息。视图显示游戏板和有颜色的砖块,并且允许用户点击他们。视图同时与用户交互,并且修改文档中的游戏数据,相应地视图被更新以反应数据的变化,如此循环。
文档: 保有数据
终于来到编码时间. 在显示任何数据到屏幕之前,我们要设计好数据的保存方式,所以我们将先从文档部分开始,然后再考虑如何实现将这些数据显示出来。
首先,我们创建一个游戏板类型,让我们叫它CSameGameBoard。通过在解决方案浏览界面(Solution Explorer)单击鼠标右键,然后选择"Add -> Class..." or "Add Class..." 。
在弹出的Generic C++ Class Wizard界面中,我们填上类名CSameGameBoard。
下面是CSameGameBoard类的头文件:
#pragma once class CSameGameBoard { public: /* Default Constructor */ CSameGameBoard(void); /* Destructor */ ~CSameGameBoard(void); /* Function to randomly setup the board */ void SetupBoard(void); /* Get the color at a particular location */ COLORREF GetBoardSpace(int row, int col); /* Accessor functions to get board size information */ int GetWidth(void) const { return m_nWidth; } int GetHeight(void) const { return m_nHeight; } int GetColumns(void) const { return m_nColumns; } int GetRows(void) const { return m_nRows; } /* Function to delete the board and free memory */ void DeleteBoard(void); private: /* Function to create the board and allocate memory */ void CreateBoard(void); /* 2D array pointer */ int** m_arrBoard; /* List of colors, 0 is background and 1-3 are piece colors */ COLORREF m_arrColors[4]; /* Board size information */ int m_nColumns; int m_nRows; int m_nHeight; int m_nWidth; };
这个类是十分简单的,它包含了一个指针,叫做m_arrBoard,它是一个二维数组,数组的每一个元素的取值为0或三个颜色之一(1-3),这个变量代表了我们看到的整个彩色砖块的全部。我们也添加了跟踪砖块行数 (m_nRows)和列数(m_nColumns)的变量,砖块宽度 (m_nHeight)和砖块高度 (m_nHeight)的变量,以及管理游戏板的一些函数。
create函数需要给二维数组m_arrBoard分配空间来存储游戏板数据,并且初始化所有的砖块为空。setup函数将reset游戏板,并游戏板上的每一个砖块随机地选择一种颜色。最后delete函数重新分配游戏板内存并释放掉之前的内存以防止内存泄漏。
在游戏板类中,有一个COLORREF类型的数组,COLORREF 存储了一个32位的颜色值。这个数组包含了四种颜色:0代表背景色,以及1-3,代表砖块可能的三种颜色(红黄蓝)。二维数组m_arrBoard的每一个元素中存储着这里的四种颜色索引之一。
以下是CSameGameBoard类的实现,在SameGameBoard.cpp文件中。
#include "StdAfx.h" #include "SameGameBoard.h" CSameGameBoard::CSameGameBoard(void) : m_arrBoard(NULL), m_nColumns(15), m_nRows(15), m_nHeight(35), m_nWidth(35) { m_arrColors[0] = RGB( 0, 0, 0); m_arrColors[1] = RGB(255, 0, 0); m_arrColors[2] = RGB(255,255, 64); m_arrColors[3] = RGB( 0, 0,255); } CSameGameBoard::~CSameGameBoard(void) { // Simply delete the board DeleteBoard(); } void CSameGameBoard::SetupBoard(void) { // Create the board if needed if(m_arrBoard == NULL) CreateBoard(); // Randomly set each square to a color for(int row = 0; row < m_nRows; row++) for(int col = 0; col < m_nColumns; col++) m_arrBoard[row][col] = (rand() % 3) + 1; } COLORREF CSameGameBoard::GetBoardSpace(int row, int col) { // Check the bounds of the array if(row < 0 || row >= m_nRows || col < 0 || col >= m_nColumns) return m_arrColors[0]; return m_arrColors[m_arrBoard[row][col]]; } void CSameGameBoard::DeleteBoard(void) { // Don‘t delete a NULL board if(m_arrBoard != NULL) { for(int row = 0; row < m_nRows; row++) { if(m_arrBoard[row] != NULL) { // Delete each row first delete [] m_arrBoard[row]; m_arrBoard[row] = NULL; } } // Finally delete the array of rows delete [] m_arrBoard; m_arrBoard = NULL; } } void CSameGameBoard::CreateBoard(void) { // If there is already a board, delete it if(m_arrBoard != NULL) DeleteBoard(); // Create the array of rows m_arrBoard = new int*[m_nRows]; // Create each row for(int row = 0; row < m_nRows; row++) { m_arrBoard[row] = new int[m_nColumns]; // Set each square to be empty for(int col = 0; col < m_nColumns; col++) m_arrBoard[row][col] = 0; } }
现在我们已经将游戏板封装成了一个类型,接下来,我们可以在文档类中,创建一个这个类型的实例。需要牢记的是,文档类拥有我们应用的全部数据,它和显示数据的视图类保持分离。下面是文档类的头文件 SameGameDoc.h:
#pragma once #include "SameGameBoard.h" class CSameGameDoc : public CDocument { protected: // create from serialization only CSameGameDoc(); virtual ~CSameGameDoc(); DECLARE_DYNCREATE(CSameGameDoc) // Attributes public: // Operations public: /* Functions for accessing the game board */ COLORREF GetBoardSpace(int row, int col) { return m_board.GetBoardSpace(row, col); } void SetupBoard(void) { m_board.SetupBoard(); } int GetWidth(void) { return m_board.GetWidth(); } int GetHeight(void) { return m_board.GetHeight(); } int GetColumns(void) { return m_board.GetColumns(); } int GetRows(void) { return m_board.GetRows(); } void DeleteBoard(void) { m_board.DeleteBoard(); } // Overrides public: virtual BOOL OnNewDocument(); protected: /* Instance of the game board */ CSameGameBoard m_board; // Generated message map functions protected: DECLARE_MESSAGE_MAP() };
文档类实际上是一个游戏板类的简单的封装,在后面的文章中我们将增加更多的功能到这里。但是目前它是保持非常简单的,我们增加了一个游戏板类的实例和7个函数,这将允许视图类接入我们的游戏板类的信息经由文档类。文档类所有功能的实现是非常简单的,它们都是间接的调用了游戏板类。
#include "stdafx.h" #include "SameGame.h" #include "SameGameDoc.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // CSameGameDoc IMPLEMENT_DYNCREATE(CSameGameDoc, CDocument) BEGIN_MESSAGE_MAP(CSameGameDoc, CDocument) END_MESSAGE_MAP() // CSameGameDoc construction/destruction CSameGameDoc::CSameGameDoc() { } CSameGameDoc::~CSameGameDoc() { } BOOL CSameGameDoc::OnNewDocument() { if (!CDocument::OnNewDocument()) return FALSE; // Set (or reset) the game board m_board.SetupBoard(); return TRUE; }
Really all we added was the call to the SetupBoard function in the OnNewDocument handler in the document. All this does is allows the user to start a new game with the built-in accelerator Ctrl+N or from the menu File->New.
As we continue through the series of articles we‘ll be adding new functions to both the game board and the document to implement different features for the game but for now we are done with the document and are ready to display this information in the view.