🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

World::View

Published November 25, 2008
Advertisement
Today (and partially last night) I wrote up a View class to wrap the clipped view of the World returned by World::DrawView(). It's constructible only by World (excepting the copy-ctor, which is public), taking width and height parameters to create a dynamically-allocated CHAR_INFO buffer. It overloads the [] operator and a conversion operator (because it was easy and it makes sense), and handles the buffer's memory itself.

class View{    private:        CHAR_INFO* view;        int width, height;        View(int width, int height);        friend const View World::DrawView(int, int, int, int) const;    public:        View(const View& v);        ~View();        View& operator=(const View& v);        CHAR_INFO* const operator[] (int idx);        operator const CHAR_INFO* const() const;        int Width() const;        int Height() const;};


This is all I need to do to draw my world view to the screen:
void Game::Draw(){    World::View worldview = world.DrawView(0, 0, world.Width(), world.Height());    SMALL_RECT drawRect = {0, 3, worldview.Width()-1, worldview.Height()+2};    COORD origin = {0, 0};    COORD worldsize = {worldview.Width(), worldview.Height()};    WriteConsoleOutput(hOut, worldview, worldsize, origin, &drawRect);}

Notice that I can pass worldview to WriteConsoleOutput? That's the conversion operator (converts to const CHAR_INFO* const). I think it looks really clean. DrawView() takes the xy coordinates of the upper-left and lower-right rectangle to view. Technically, passing the world's Width and Height puts the rect one over the edge on the right and bottom sides, but DrawView clips it down, so it's a small shortcut rather than subtracting one from each. Then again, I have to do it in the drawRect anyways...
0 likes 3 comments

Comments

rip-off
I would argue against implicit conversion operators. They can cause quite subtle problems.

Consider: std::string doesn't implicitly convert to a const char pointer, the client code must explicitly write c_str().
November 25, 2008 03:01 PM
Twisol
I would argue that std::string has obvious clarity and semantical problems, whereas my World::View has a very limited application, and since CHAR_INFO isn't a built-in data type like char, you'd really only use World::View where it logically makes sense anyways.

In other words, I don't think it causes problems here specifically. Although I do know what you mean. I considered it before I added it for that very reason.

EDIT: Just now on a hunch, I commented out operator[]. It does look like it implicitly converts to a const CHAR_INFO* const when I use it anyways (which means the code that uses [] to assign won't work due to const-ness). I can see where some confusion might spring from that. However, overloading operator[] like I did prevents it from converting in the first place. If you could point out an example here where the conversion operator would cause problems it would be much appreciated.

EDIT 2: At the urging of MaulingMonkey at #gamedev, I've changed it to a getter function. :|
November 25, 2008 06:07 PM
matt_j
:O Text mode!

I will definitely be watching this journal. :D
November 26, 2008 05:55 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement