Space Fighter
A "shmup" game for Computer Programming C++
Loading...
Searching...
No Matches
MainMenuScreen.cpp
Go to the documentation of this file.
1
2#include <string>
3#include "MainMenuScreen.h"
4#include "GameplayScreen.h"
5
6// Callback Functions
7
11{
12 pScreen->Exit();
13}
14
18{
19 MainMenuScreen *pMainMenuScreen = (MainMenuScreen *)pScreen;
20 pMainMenuScreen->SetQuittingGame();
21 pScreen->Exit();
22}
23
27{
28 MainMenuScreen *pMainMenuScreen = (MainMenuScreen *)pScreen;
29
30 if (pMainMenuScreen->IsQuittingGame()) {
31 pScreen->GetGame()->Quit();
32 return;
33 }
34
35 pScreen->GetScreenManager()->AddScreen(new GameplayScreen());
36}
37
38
48
50{
51 // Logo
52 m_pTexture = resourceManager.Load<Texture>("Textures\\Logo.png");
53 m_texturePosition = Game::GetScreenCenter() - Vector2::UNIT_Y * 150;
54
55 // Create the menu items
56 const int COUNT = 2;
57 MenuItem *pItem;
58 Font::SetLoadSize(20, true);
59 Font *pFont = resourceManager.Load<Font>("Fonts\\arial.ttf");
60
61 SetDisplayCount(COUNT);
62
63 enum Items { START_GAME, QUIT };
64 std::string text[COUNT] = { "Start Game", "Quit" };
65
66 for (int i = 0; i < COUNT; i++)
67 {
68 pItem = new MenuItem(text[i]);
69 pItem->SetPosition(Vector2(100, 100 + 50 * i));
70 pItem->SetFont(pFont);
71 pItem->SetColor(Color::Blue);
72 pItem->SetSelected(i == 0);
73 AddMenuItem(pItem);
74 }
75
78}
79
80void MainMenuScreen::Update(const GameTime& gameTime)
81{
82 MenuItem *pItem;
83
84 // Set the menu item colors
85 for (int i = 0; i < GetDisplayCount(); i++)
86 {
87 pItem = GetMenuItem(i);
88 pItem->SetAlpha(GetAlpha());
89 pItem->SetColor(pItem->IsSelected() ? Color::White : Color::Blue);
90 }
91
92 MenuScreen::Update(gameTime);
93}
94
96{
97 spriteBatch.Begin();
98 spriteBatch.Draw(m_pTexture, m_texturePosition, Color::White * GetAlpha(), m_pTexture->GetCenter());
99 spriteBatch.End();
100
101 MenuScreen::Draw(spriteBatch);
102}
void OnMainMenuScreenRemoved(Screen *pScreen)
Callback function for when the main menu screen is removed from the screen manager.
void OnStartGameSelect(MenuScreen *pScreen)
Callback function for when the Start Game menu item is selected.
void OnQuitSelect(MenuScreen *pScreen)
Callback function for when the Quit menu item is selected.
The gameplay screen for the Space Fighter Game. It is responsible for creating and loading levels.
static const Color Blue
Blue.
Definition Color.h:69
static const Color White
White.
Definition Color.h:201
Represents a font or true-type font.
Definition Font.h:21
static void SetLoadSize(const int size, const bool restore=false)
Specifies the size of the font when loaded.
Definition Font.cpp:26
virtual void Quit()
Quits the game.
Definition Game.h:80
static Vector2 GetScreenCenter()
Gets the screen size in pixels.
Definition Game.h:44
Contains timing values for game updates and rendering.
Definition GameTime.h:21
Class for menu items contained in a MenuScreen.
Definition MenuItem.h:28
virtual void SetAlpha(const float alpha)
Sets the alpha value (opacity) of the menu item.
Definition MenuItem.h:97
virtual bool IsSelected() const
Determines whether or not this menu item is the one that is currently selected.
Definition MenuItem.h:68
virtual void SetFont(Font *pFont)
Sets the font of the menu item.
Definition MenuItem.h:85
virtual void SetColor(const Color color)
Sets the color of the menu item.
Definition MenuItem.h:93
virtual void SetPosition(const Vector2 position)
Sets the screen position of the menu item.
Definition MenuItem.h:89
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 SetSelectCallback(OnSelect callback)
Sets the callback function for when the menu item is selected.
Definition MenuItem.h:112
Base class for all game menu screens.
Definition MenuScreen.h:23
virtual int GetDisplayCount() const
Get the number of menu items that are set to display.
Definition MenuScreen.h:72
virtual void Draw(SpriteBatch &spriteBatch)
Called when the game determines it is time to draw a frame.
virtual void SetDisplayCount(const unsigned int count)
Set how many menu items to display.
Definition MenuScreen.h:68
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.
virtual MenuItem * GetMenuItem(const int itemIndex) const
Get a menu item by providing it's index.
Definition MenuScreen.h:60
Loads and manages the lifespan of objects from external files.
T * Load(const std::string &path, const bool cache=true, const bool appendContentPath=true)
Load and manage a resource.
Base class for all game screens and menus.
Definition Screen.h:40
virtual ScreenManager * GetScreenManager()
Gets a pointer to the ScreenManager, for managing game screens.
Definition Screen.h:83
virtual Game * GetGame() const
Gets a pointer to the Game.
Definition Screen.cpp:122
float GetAlpha() const
Gets the overall screen transition alpha value (or opacity). This is handy for fading screens in and ...
Definition Screen.h:79
virtual void SetTransitionInTime(double seconds)
Set the time in seconds that the screen should transition in.
Definition Screen.h:145
virtual void Show()
Tells the screen to transition in.
Definition Screen.cpp:90
virtual void Exit()
Tells the screen to transition out. When the screen has completed its transition, UnloadContent() wil...
Definition Screen.cpp:95
virtual void SetTransitionOutTime(double seconds)
Set the time in seconds that the screen should transition out.
Definition Screen.h:149
virtual void SetRemoveCallback(OnRemove callback)
Sets the callback function for when the screen is about to be removed by the screen manager.
Definition Screen.h:110
virtual void AddScreen(Screen *pScreen)
Add a screen to be managed.
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.
void Draw(const Texture *pTexture, const Vector2 position, const Region region, const Color color=Color::White, const Vector2 origin=Vector2::ZERO, const Vector2 scale=Vector2::ONE, const float rotation=0, const float drawDepth=0)
Adds a sprite to a batch of sprites to be rendered.
Represents a 2D grid of texels.
Definition Texture.h:21
Vector2 GetCenter() const
Gets the center position of the texture.
Definition Texture.h:48
Defines a vector with 2 components (x and y).
Definition Vector2.h:21
static const Vector2 UNIT_Y
A unit vector on the y-axis.
Definition Vector2.h:34
The main menu screen for the game.
MainMenuScreen()
Instantiate a main menu screen object.
virtual void Update(const GameTime &gameTime)
Unload the content for the screen.
virtual void Draw(SpriteBatch &spriteBatch)
Render the screen.
virtual void SetQuittingGame()
Set the flag to quit the game, so the game will exit when screen fades out.
virtual bool IsQuittingGame()
Check if the game is quitting.
virtual void LoadContent(ResourceManager &resourceManager)
Load the content for the screen.