Hello, I am new to the forums as recently started learning C++, and I am trying to wrap my head around C++ from a C# developer perspective.
I am making a game for the first time. Here is a overall information:
- There is a ScreenManager class which has a list of GameScreen, and calls GameScreen's methods.
- GameScreen is the parent class of SplashScreen and has virtual methods.
- SplashScreen overrides the methods of its parent class.
- In the main, I create a ScreenManager, which should call the methods of a SplashScreen added into the list of GameScreens.
The problem is that the ScreenManager object in the main function only calls 1 method correctly (the first one) but the other two are ignored.
I want to know why it doesn't call the other two methods and if there is a better way to do this.
Thank you in advance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
///////////////////////////
// main.cpp
///////////////////////////
#include <iostream>
#include "ScreenManager.h"
#include "SplashScreen.h"
ScreenManager ScreenManager;
bool init()
{
bool success = true;
SplashScreen splash = SplashScreen();
ScreenManager.AddScreen(&splash);
if(!ScreenManager.LoadMedia())
{
success = false;
}
return success;
}
int main()
{
if(!init())
{
std::cout << "Failed to initialize!\n";
}
else
{
ScreenManager.Update();
ScreenManager.Draw();
}
return 0;
}
///////////////////////////
// ScreenManager.h
///////////////////////////
#include <list>
#include "GameScreen.h"
class ScreenManager
{
public:
ScreenManager();
virtual ~ScreenManager();
void AddScreen(GameScreen* gameScreen);
bool LoadMedia();
void Draw();
void Update();
private:
std::list<GameScreen*> screenList;
};
///////////////////////////
// ScreenManager.cpp
///////////////////////////
#include "ScreenManager.h"
ScreenManager::ScreenManager(){}
ScreenManager::~ScreenManager(){}
void ScreenManager::AddScreen(GameScreen* gameScreen)
{
screenList.push_back(gameScreen);
}
bool ScreenManager::LoadMedia()
{
return screenList.back()->LoadMedia();
}
void ScreenManager::Draw()
{
if (!screenList.empty())
{
screenList.back()->Draw();
}
}
void ScreenManager::Update()
{
if (!screenList.empty())
{
screenList.back()->Update();
}
}
///////////////////////////
// GameScreen.h
///////////////////////////
class GameScreen
{
public:
GameScreen();
virtual ~GameScreen();
virtual bool LoadMedia();
virtual void Draw();
virtual void Update();
};
///////////////////////////
// GameScreen.cpp
///////////////////////////
#include "GameScreen.h"
GameScreen::GameScreen(){}
GameScreen::~GameScreen(){}
bool GameScreen::LoadMedia()
{
return true;
}
void GameScreen::Draw(){}
void GameScreen::Update(){}
///////////////////////////
// SplashScreen.h
///////////////////////////
#include "GameScreen.h"
class SplashScreen : public GameScreen
{
public:
SplashScreen();
virtual ~SplashScreen();
bool LoadMedia() override;
void Draw() override;
void Update() override;
};
///////////////////////////
// SplashScreen.cpp
///////////////////////////
#include "SplashScreen.h"
#include <iostream>
SplashScreen::SplashScreen(){}
SplashScreen::~SplashScreen(){}
bool SplashScreen::LoadMedia()
{
std::cout << "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!\n";
return true;
}
void SplashScreen::Draw()
{
std::cout << "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB!\n";
}
void SplashScreen::Update()
{
std::cout << "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC!\n";
}
|
Output:
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!
...Program finished with exit code 0
|