Question about organizing class with many methods

closed account (2UD8vCM9)
Okay so i'm working on a small game just for learning purposes.

Since the game is multiplayer, I have many things to manage.

For example, I have a few different enums set up

For the current screen you're looking at (Ex. Into Screen/Login Screen/AboutScreen/etc) I have an enum inside of my game class (Note: This is only part of the code, not trying to overcomplicate things.)

1
2
3
4
5
6
7
8
9
10
11
12
13
class Game
{
public:
	enum Screens {
		GS_MainScreen,
		GS_About,
		GS_Online,
		GS_HowToPlay,
		GS_ServerScreen,
		GS_CreateGame,
		GS_LoadingGame
	};
}


For the thread running on the client side, I also have to have enums to store the indexes for the Current Task that the client thread is performing as well as have an enum to store the Packet Type of the packet being received/sent.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace PacketType
{
	enum {
		CHATMESSAGE,
		DISCONNECTCLIENT
	};
}

namespace Task
{
	enum {
		RECEIVINGPACKETTYPE
	};
}


So for example, if I want to set my Task in my Client Thread to be the "Receiving Packet Type" task, I can do
MyTask = Task::RECEIVINGPACKETTYPE;

And also, if I am checking if a packet's type is a chat message, I could do
1
2
3
4
if (MyPacketType == PacketType::CHATMESSAGE)
{
    //Do Stuff;
}



So here is what i'm getting to.

I realize that I can use namespaces outside of a class to organize certain things such as enums, and inside of a class I can organize enums by the main enum name, but here is where i'm lost.

Let's say I have 30+ methods inside the game class. I know I can not make a namespace inside of the game class, but is there any way I can organize my methods inside of my game class other than creating more classes?

So if I have

1
2
3
4
5
6
7
8
9
10
11
12
class Game
{
public:
	void DrawFunc1();
	void DrawFunc2();
	void DrawFunc3();
	void DrawFunc4();
	void SoundFunc1();
	void SoundFunc2();
	void SoundFunc3();
        //etc...
}


Is there any way I could organize it so that I could have something like in order to call DrawFunc1 I could do

1
2
3
Game MyGame;
MyGame::DrawFunctions::DrawFunc1();
MyGame::SoundFunctions::SoundFunc1();

^ I realize it wouldn't look exactly like this, but i'm hoping you understand what i'm trying to get at. Is there any way I can organize my methods inside of a class without creating classes inside of that class?


Thanks for reading through all of my rambling. Hope this isn't too confusing.
You could create a Draw class within the Game class and make each draw function of method of Draw, but the disadvantage there is that you wouldn't have access to the rest of the Game members without some gymnastics:
1
2
3
4
5
6
7
8
9
10
11
12
class Game {
public:
    class Draw {
    public:
        void func1(), func2(), func3();
    };

    Draw draw;
};

Game myGame;
myGame.draw.func1();


But really you may just be overthinking this. In a large program you may have a complex class with many methods.

closed account (2UD8vCM9)
I see. Well thank you very much for your response. I was afraid this was the case, but I guess i'll just have to stop being lazy and implement in classes inside of my class so that it doesn't get too out of hand in the future.

Have a great day!
First instead of using namespaces on enums, use enum classes, they are exactly the same thing, and you can use them as if they were enums in classes, or as objects if you give the enum a name.

I would use a texture wrapper that has a function to load files, and a function to render the texture.
and you would do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Game {
private: 
  //you should put these into global scope and load on creation, but eh...
  TextureObj background; 
  TextureObj button_sheet;
  TextureObj text;
public:
  Game(){
    background.load_file("graphics/background.png");
    button_sheet.load_file("graphics/button_sheet");
    text.load_ttf("fonts/ariel.ttf");
  }
  void Render(){
    background.render(0,0); //the whole screen, starting from the topleft
    button_sheet.render(100,100,0,0,50,50); //using a sprite sheet(x,y(<-coords on the screen),x,y(<-coords on the sheet),w,h(<-dimensions on sheet))
    text.render(10,20); //around the top of the screen
  }
};


But if you are doing a chat server, you would want a queue for chat history(the elements are structs with a string, & a flag for colors), with a max of like 200, and every element represents a line, so every new line is a new element(you gotta create the system, to allow people to change your ttf file, when you load the ttf, load every letter individually and record the width into a std::map using char's as a token).

And another queue that holds texture objects. It would be like a wheel, lets say you can only see only 10 lines in the box(so on init you insert 10 blank elements), and some texts have different colors. When a new chat comes up you insert it to the beginning, and you erase the end. When you scroll up you erase the beginning and insert a element from your history into the top(place some asserts around to help fix bugs).

And for entities walking around, when you get to that point, you use polymorphism to create entities, you put the abstract class into unique pointer<abstract_entity>, as an element for the vectors. (you make a new entities like: entities.emplace_back(std::unique_ptr<abstract_entity>(new player(x, y)));.

Also remember that tiles and entities should share texture objects.
Topic archived. No new replies allowed.