Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views


Sign in to follow this  


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
CHAR_INFO* view;
int width, height;

View(int width, int height);
friend const View World::DrawView(int, int, int, int) const;
View(const View& v);

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...
Sign in to follow this  


Recommended Comments

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().

Share this comment

Link to comment
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. :|

Share this comment

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!