Guys, I present you my longest and stupidest post ever. :lol:
Question No.1:
I've asked a similar question before, but I guess I didn't understand everything. It's concerned with the storage of all my game resources.
By resources I mean my vertex and fragment shaders and my models. I store all of them in a ResourceManager class (I don't know why I called it that way, it doesn't manage anything, it just stores all the stuff, but nevermind).
And when I need to create a player, I just do this: Player player = Player( myResourceManager.getSomeAnimations(), vec3 position );
And the animations I supplied as an argument go to a pointer in the Player class.
So what I do is just storing pointers to the animations that can be found in the ResourceManager.
But what I don't like is that every time that I need a shader or a model in a function, I have to supply ResourceManager as an argument to the function and it gets irritating, because I add too much arguments to every function.
I know the problem may sound stupid to you, but I still decided to ask here because I'm wondering why everything should be kept in one class, and not separated and is this the right way to do things.
Question No.2
All my classes are united in one class called "App". I'm pretty sure they call this 'composition'. This is my App class:
//Some includes...
class App
{
public:
App();
~App();
InputManager myInputManager;
NetworkManager myNetworkManager;
ResourceManager myResourceManager;
Timer timer;
vector<Player> players;
//And so on.....
};
#endif
But now, for example, if I want to add another function to my NetworkManager class and that function needs a variable that is stored as a private to the class 'App' that owns an object of the type NetworkManager, I need to pass a reference to App as an argument to a NetworkManager function and it looks something like this
myNetworkManager.serverReceivePackets( App& myApp );
So I'm actually passing as an argument an object that contains the object to which the function belongs, and it seems really bad to me,
because I can access the same variables in two ways. For example
NetworkManager::serverReceivePackets( App& myApp )
{
serverIP = packet->getIP(); //1st way
myApp->myNetworkManager->serverIP = packet->getIP(); //2nd way. I haven't actually tried this, it's too ugly, I can't.
}
I don't know why but this looks really ugly to me and it stops me from coding, because I can't watch it. Is there other way of accessing private App vars from here?
If you still haven't died from stupidity, Question No.3:
I'm kind of finished with the basic networking and I want to add some singleplayer and some menus. But I'm trying to figure out the best structure for this.
The best thing I found on the internet is this:
First, I create a GameState class that has a bunch of virtual functions because I need to use polymorphism to switch between singleplayer mode, multiplayer mode, main menu and options menu. The class looks something like this:
#ifndef GAMESTATE_H
#define GAMESTATE_H
//Base class
class GameState
{
public:
virtual void handleEvents() = 0;
virtual void updateLogic() = 0;
virtual void renderScene() = 0;
virtual ~GameState(){};
};
#endif
The idea is to use a pointer to GameState like this: GameState *currentState and this way I can create any state I want.
And in the 'while' loop I just write something like this:
currentState->handleEvents();
currentState->updateLogic();
currentState->renderScene();
Now imagine I'm in the main menu and I want to play singleplayer. and I click on "Story mode", the only code I write here is this:
currentState = new SinglePlayer();
No, my bad. This won't work.
The proper way to do it is this: currentState = new SinglePlayer( myResourceManager.GetSomeUglyModelsAsYouAlwaysNeedToDo() );
The main problem with this method is that I need to initialize some private members of App differently for every new state I create, which causes problems. This App class is ruining my health, why did I make it in the first place....
Is this the way normal people do this stuff, or is there something better?
Guys, sorry for the stupid questions, but they are kind of in my head for a very long time and there's always something I don't like, sometimes I'm too perfectionistic, other times I just skim through the details and then spend hours debugging just because of a single detail I've missed. That's why I wrote that novel. Thanks for reading. I would like to make a TL;DR version, but I can't. If someday I become a game programmer, then every single person in the world can become one.