-
Notifications
You must be signed in to change notification settings - Fork 9
/
MainMenuState.cpp
85 lines (73 loc) · 1.93 KB
/
MainMenuState.cpp
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
#include "MainMenuState.h"
#include <iostream>
#include "TextureManager.h"
#include "Game.h"
#include "MenuButton.h"
#include "PlayState.h"
#include "StateParser.h"
const std::string MainMenuState::s_menuID = "MENU";
//function to be called when PLAY button is pressed
void MainMenuState::s_menuToPlay()
{
//change the state to Play State
TheGame::Instance()->getStateMachine()->changeState(new PlayState());
}
//function to be called when EXIT button is pressed
void MainMenuState::s_exitFromMenu()
{
//exit the game
TheGame::Instance()->quit();
}
//update each game object
void MainMenuState::update()
{
for (int i = 0; i < (int)m_gameObjects.size(); ++i)
{
m_gameObjects[i]->update();
}
}
//draw the game objects
void MainMenuState::render()
{
for (int i = 0; i < (int)m_gameObjects.size(); ++i)
{
m_gameObjects[i]->draw();
}
}
bool MainMenuState::onEnter()
{
//parse the state
StateParser stateParser;
stateParser.parseState("test.xml", s_menuID, &m_gameObjects, &m_textureIDList);
m_callbacks.push_back(0); //pushback 0 callbackID start from 1
m_callbacks.push_back(s_menuToPlay);
m_callbacks.push_back(s_exitFromMenu);
//set the callbacks for menu items
setCallbacks(m_callbacks);
std::cout << "entering MenuState\n";
return true;
}
bool MainMenuState::onExit()
{
//clear the texture manager
for (int i = 0; i < (int)m_textureIDList.size(); ++i)
{
TheTextureManager::Instance()->clearFromTextureMap(m_textureIDList[i]);
}
std::cout << "exiting MenuState\n";
return true;
}
void MainMenuState::setCallbacks(const std::vector<Callback> &callbacks)
{
//go through the game objects
for (int i = 0; i < m_gameObjects.size(); ++i)
{
//if they are of type MenuButton then assign a callback
//based on the id passed in from the file
if (dynamic_cast<MenuButton*>(m_gameObjects[i]))
{
MenuButton* pButton = dynamic_cast<MenuButton*>(m_gameObjects[i]);
pButton->setCallback(callbacks[pButton->getCallbackID()]);
}
}
}