Best way to pass consts info to properties in class

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?
Last edited on
I don't think passing constants in to other classes is a good idea, it defeats part of the purpose of declaring such a constant let alone parameters. Instead why not encapsulate your constants inside the game class, but keep it public for others to use though the namespace?

1
2
3
4
5
6
7
8
9
10
11
12
class Game {
  public:
    Game();
    const static int WindowHeight = 800;
    const static int WindowWidth = 600;
    //...
  private:
    static const int ScreenWidth;
    static const int ScreenHeight;
    Window mWindow;
    std::vector<Room> mRooms;
}; 


this way in say, your room function you can use

Rectangle wall1(0, 0, Game::windowHeight);

Last edited on
Some things really are global. So in general I agree.

But screen width and height? Those vary with the user. They can even change during the game if the user decides to change screen resolution. So these should not be const.
@Need4Sleep
Wow, that looks nice. It seems so simple but I never thought of doing it like that, I'll probably try this.

@dhayden
Well, sure in a good program it will vary based on a configuration file or something, for now though I was just keeping them as consts because it's just simpler.

However, that does bring up a good point, if I were to make it so the user could change the resolution during run-time, I couldn't use const variables anymore, so I'd have to just make it be an int variable in the class, and then have methods for scaling the sizes of every drawable thing. I think I'm not going to worry about something dynamic like that just yet ;P

Thanks for the replies!
Topic archived. No new replies allowed.