So basically, I have a Game class.
This Game class has const properties for the Screen Width/Height;
One way of doing this is as follows:
1 2 3 4 5 6 7 8 9
|
class Game {
Game();
//...
private:
static const int ScreenWidth;
static const int ScreenHeight;
Window mWindow;
std::vector<Room> mRooms;
};
|
The issue is... the Room class (and Window) needs to also know the screen dimensions in order to know how big to make, for example, its walls.
Using this method, I would have do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
//game.cpp
const int Game::ScreenWidth = 600;
const int Game::ScreenHeight = 400;
Game::Game()
: mWindow(ScreenWidth, ScreenHeight)
, mRooms(ScreenWidth, ScreenHeight)
{}
//rooms.cpp
Room::Room(const int ScreenWidth, const int ScreenHeight)
: roomWidth(ScreenWidth)
, roomHeight(ScreenHeight)
{
createWalls();
setupOtherStuff();
}
|
Is this a good way of doing this? It seems like I'm repeating a lot of stuff that I shouldn't need to.
Would it be preferable to separate some constants if they can apply to more than one class? This wouldn't really be OOP, though, right? Also, they would be global variables (but const to avoid problems associated with this)
Something like this?
1 2 3 4 5
|
//GameConstants.h
*include guards*
const int ScreenWidth = 400;
const int ScreenHeight = 300;
|
and then in my Room and Game classes, they would be able to directly access these constants, so I'd have something like this
1 2 3 4 5 6 7 8 9 10
|
//room.cpp
#include "GameConstants.h"
#include "Room.h"
void Room::createWalls()
{
Rectangle wall1(0, 0, ScreenWidth, ScreenHeight); //just an example, doesn't really make sense
Rectangle wall2(42, 84, ScreenWidth/2, 100);
//add rectangles to wall list...
}
|
That seems like it would save a lot of typing, is it a good practice to put in an OOP design?