Inner classes and map editing

Published June 30, 2008
Advertisement
I've decided I want to work on my map system first, heh. I decided to try something new to prepare for it, and I created my first inner class in my GameView. The definition of the 'mapaccessor' is private, but I declare a public instance called 'map'. Its only role in life is to provide a clear method of accessing the GameView's map buffer. For example:

// gameview with a map of size (5, 5) and a window of the same sizeGameView gv(5, 5, 5, 5);// read and print the character at (0, 0)cout << gv.map[0].Char.AsciiChar << endl;// change the color at (0, 0) to green// the CC_* enum is defined in another header I made, "conutil.h"gv.map[0].Attributes = CC_GREEN;


The mapaccessor inner class is defined as:

class mapaccessor{	private:		GameView& rGv;	public:		mapaccessor(GameView* gv)			: rGv(*gv)		{}		CHAR_INFO& operator[] (int idx)		{			if (idx > rGv.realsize.X * rGv.realsize.Y)				throw std::out_of_range("Attempt to index out of array bounds.");			else				return rGv.playbuf[idx];		}};


I'll be adding an operator= as well, which takes a CHAR_INFO*, mainly for loading an entire map buffer.

Do you see how my mapaccessor takes a GameView pointer? The GameView which instantiates a mapaccessor passes 'this' to the mapaccessor constructor, which stores the address in a GameView reference. Now, in general it's not a good idea to pass 'this' to another ctor, especially in the initializer list. If the code you called happens to access a member of *this that hasn't been initialized yet, bad things happen. The reason I can get away with this is because I only ever store the value of 'this' itself, namely the address of the new object.

I could certainly have just overloaded operator[] on the GameView, true. However, it's not quite as clear what you're accessing just from glancing at the code. Moreover, if I were to implement an operator= on the GameView, in the manner I explained two paragraphs ago, that would be even stranger if you don't know what's going on to begin with. Using the mapaccessor gives code the syntax and clarity of accessing a public array member, but the safety (array bounds checking) and ease of use (such as operator=) of a private member with get/set functions. Handy, no?

Once I have operator= implemented, I think I'll try a simple map editor. It's going to use the GameView, just like the game itself - in fact, what you see in the map editor will be exactly what you see and how you see it in the game - but with a "tablet" of all of the ASCII characters that you can click to select. Yes, you'll be able to use the mouse in the editor. That should make things a bit simpler!

Oh, and a big thanks to ibutsu at #gamedev for the C++ FAQ Lite link on using 'this' in a constructor.


~Jonathan
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement