Space Fighter
A "shmup" game for Computer Programming C++
Loading...
Searching...
No Matches
MenuScreen.cpp
Go to the documentation of this file.
1
2/*
3 ██╗ ██╗ █████╗ ████████╗ █████╗ ███╗ ██╗ █████╗
4 ██║ ██╔╝ ██╔══██╗ ╚══██╔══╝ ██╔══██╗ ████╗ ██║ ██╔══██╗
5 █████╔╝ ███████║ ██║ ███████║ ██╔██╗ ██║ ███████║
6 ██╔═██╗ ██╔══██║ ██║ ██╔══██║ ██║╚██╗██║ ██╔══██║
7 ██║ ██╗ ██║ ██║ ██║ ██║ ██║ ██║ ╚████║ ██║ ██║
8 ╚═╝ ╚═╝ ╚═╝ ╚═╝/\ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝
9 /vvvvvvvvvvvvvvvvvvv \=========================================,
10 `^^^^^^^^^^^^^^^^^^^ /---------------------------------------"
11 Katana Engine \/ © 2012 - Shuriken Studios LLC
12 http://www.shurikenstudios.com
13*/
14
15#include "KatanaEngine.h"
16
17namespace KatanaEngine
18{
20 {
21 m_selectedItemIndex = 0;
22 m_displayStartIndex = 0;
23 m_displayCount = 0;
24 m_itemListWraps = true;
25 }
26
27
29 {
30 std::vector<MenuItem *>::iterator it;
31 for (it = m_menuItems.begin(); it != m_menuItems.end(); ++it)
32 {
33 delete *it;
34 }
35 }
36
37
39 {
40 if (m_displayCount > 0) m_itemListWraps = false;
41
42 if (m_menuItems.size() > 0)
43 {
44 int8_t playerIndexOut;
45
46 const int FIRST_INDEX = 0;
47 const int LAST_INDEX = (int)(m_menuItems.size() - 1);
48
49 if (input.IsNewKeyPress(Key::ENTER) ||
50 input.IsNewButtonPress(Button::A, playerIndexOut))
51 {
52 m_menuItems[m_selectedItemIndex]->Select(this);
53 }
54
55 if (input.IsNewKeyPress(Key::UP) ||
56 input.IsNewButtonPress(Button::DPAD_UP, playerIndexOut)) // TODO?: Make "IsMenuUp" in InputState ?
57 {
58 m_selectedItemIndex--;
59
60 if (m_selectedItemIndex >= m_menuItems.size())
61 {
62 m_selectedItemIndex = (m_itemListWraps) ? LAST_INDEX : FIRST_INDEX;
63 }
64 }
65 else if (input.IsNewKeyPress(Key::DOWN) ||
66 input.IsNewButtonPress(Button::DPAD_DOWN, playerIndexOut)) // TODO?: Make "IsMenuDown" in InputState ?
67 {
68 m_selectedItemIndex++;
69
70 if (m_selectedItemIndex >= m_menuItems.size())
71 {
72 m_selectedItemIndex = (m_itemListWraps) ? FIRST_INDEX : LAST_INDEX;
73 }
74 }
75
76 if (m_displayCount > 0)
77 {
78 if (m_selectedItemIndex < m_displayStartIndex)
79 {
80 m_displayStartIndex--;
81 }
82 else if (m_selectedItemIndex >= m_displayStartIndex + m_displayCount)
83 {
84 m_displayStartIndex++;
85 }
86 }
87 }
88 }
89
90 void MenuScreen::Update(const GameTime& gameTime)
91 {
92 unsigned int index = 0;
93
94 std::vector<MenuItem *>::iterator it;
95 for (it = m_menuItems.begin(); it != m_menuItems.end(); ++it)
96 {
97 MenuItem *pMenuItem = *it;
98
99 bool displayed = (m_displayCount == 0
100 || (index >= m_displayStartIndex
101 && index < m_displayStartIndex + m_displayCount));
102
103 pMenuItem->SetDisplayed(displayed);
104 pMenuItem->SetSelected(index == m_selectedItemIndex);
105 pMenuItem->Update(gameTime);
106
107 index++;
108 }
109 }
110
111 void MenuScreen::Draw(SpriteBatch& spriteBatch)
112 {
113 spriteBatch.Begin();
114
115 std::vector<MenuItem *>::iterator it;
116 for (it = m_menuItems.begin(); it != m_menuItems.end(); ++it)
117 {
118 MenuItem *pMenuItem = *it;
119
120 if (pMenuItem->IsDisplayed()) pMenuItem->Draw(spriteBatch);
121 }
122
123 spriteBatch.End();
124 }
125
126
128 {
129 pItem->SetIndex((int)m_menuItems.size());
130 pItem->SetMenuScreen(this);
131 m_menuItems.push_back(pItem);
132 }
133}
Contains timing values for game updates and rendering.
Definition GameTime.h:21
Handles the state of multiple player input devices.
Definition InputState.h:22
bool IsNewButtonPress(Button button, int8_t &indexOut, int8_t controllingIndex=-1) const
Determines if a the button on an Xbox controller was just pressed this frame.
bool IsNewKeyPress(Key key) const
Determines if a keyboard key was just pressed this frame.
Class for menu items contained in a MenuScreen.
Definition MenuItem.h:28
virtual void SetDisplayed(const bool isDisplayed)
Sets whether or not this menu item is currently displayed.
Definition MenuItem.h:81
virtual bool IsDisplayed() const
Determines whether or not this menu item is currently displayed.
Definition MenuItem.h:77
virtual void SetIndex(const int index)
Sets the index of the menu item.
Definition MenuItem.h:123
virtual void SetMenuScreen(MenuScreen *pMenuScreen)
Sets containing menu screen.
Definition MenuItem.h:108
virtual void Draw(SpriteBatch &spriteBatch)
Called when the game determines it is time to draw a frame.
Definition MenuItem.cpp:38
virtual void Update(const GameTime &gameTime)
Called when the game has determined that screen logic needs to be processed.
Definition MenuItem.h:47
virtual void SetSelected(const bool isSelected)
Sets whether or not this menu item is the one that is currently selected.
Definition MenuItem.h:73
virtual void Draw(SpriteBatch &spriteBatch)
Called when the game determines it is time to draw a frame.
virtual void HandleInput(const InputState &input)
Called when the game has determined that player input needs to be processed.
virtual void Update(const GameTime &gameTime)
Called when the game has determined that screen logic needs to be processed.
virtual void AddMenuItem(MenuItem *pItem)
Adds a menu item to the screen.
Enables a group of sprites to be drawn using the same settings.
Definition SpriteBatch.h:48
void Begin(const SpriteSortMode sortMode=SpriteSortMode::Deferred, const BlendState blendState=BlendState::Alpha, ALLEGRO_TRANSFORM *pTransformation=NULL)
Begins a sprite batch operation.
void End()
Flushes the sprite batch and restores the device state to how it was before Begin was called.
Katana Engine is a library of classes, interfaces, and value types that provides a foundation for dev...
Definition Animation.cpp:18