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...
Consider: std::string doesn't implicitly convert to a const char pointer, the client code must explicitly write c_str().