Space Fighter
A "shmup" game for Computer Programming C++
Loading...
Searching...
No Matches
ScreenManager.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_pGame = pGame;
22 }
23
28
29 void ScreenManager::HandleInput(const InputState& input)
30 {
31 if (m_screens.size() > 0)
32 {
33 bool update = true;
34
35 for (m_rit = m_screens.rbegin(); m_rit != m_screens.rend(); ++m_rit)
36 {
37 Screen *pScreen = *m_rit;
38
39 if (update)
40 {
41 if (pScreen->NeedsToBeRemoved()) continue;
42 pScreen->HandleInput(input);
43 update = pScreen->ShouldHandleInputBelow();
44 }
45 else break;
46 }
47 }
48 }
49
50 void ScreenManager::Update(const GameTime& gameTime)
51 {
52 Screen *pScreen;
53
54 if (m_screensToAdd.size() > 0)
55 {
56 m_screens.insert(m_screens.end(), m_screensToAdd.begin(), m_screensToAdd.end());
57 m_screensToAdd.clear();
58 }
59
60 if (m_screens.size() > 0)
61 {
62 bool update = true;
63
64 for (m_rit = m_screens.rbegin(); m_rit != m_screens.rend(); ++m_rit)
65 {
66 pScreen = *m_rit;
67
68 pScreen->UpdateTransition(gameTime);
69
70 if (pScreen->NeedsToBeRemoved())
71 {
72 m_screensToRemove.push_back(pScreen);
73 }
74 else if (update)
75 {
76 pScreen->Update(gameTime);
77 update = pScreen->ShouldUpdateBelow();
78 }
79 }
80 }
81
82 if (m_screensToRemove.size() > 0)
83 {
84 for (m_it = m_screensToRemove.begin(); m_it != m_screensToRemove.end(); ++m_it)
85 {
86 pScreen = *m_it;
87
88 if (pScreen->m_onRemove) ((OnRemove)pScreen->m_onRemove)(pScreen);
89
90 pScreen->UnloadContent();
91
92 m_screens.erase(std::remove(m_screens.begin(), m_screens.end(), pScreen), m_screens.end());
93
94 delete pScreen;
95 }
96
97 m_screensToRemove.clear();
98 }
99 }
100
102 {
103 Screen *pScreen;
104 if (m_screens.size() > 0)
105 {
106 for (m_rit = m_screens.rbegin(); m_rit != m_screens.rend(); ++m_rit)
107 {
108 pScreen = *m_rit;
109 if (pScreen->NeedsToBeRemoved()) continue;
110 m_screensToDraw.push_back(pScreen);
111 if (!pScreen->ShouldDrawBelow()) break;
112 }
113 }
114
115 if (m_screensToDraw.size() > 0)
116 {
117 for (m_rit = m_screensToDraw.rbegin(); m_rit != m_screensToDraw.rend(); ++m_rit)
118 {
119 Screen *pScreen = *m_rit;
120 RenderTarget *pRenderTarget = pScreen->GetRenderTarget();
121
122 if (pRenderTarget) RenderTarget::Set(pRenderTarget);
123
124 (*m_rit)->Draw(spriteBatch);
125
126 if (pRenderTarget)
127 {
128 RenderTarget::Set(nullptr);
129 spriteBatch.Begin();
130 spriteBatch.Draw(pRenderTarget, Vector2::ZERO, pScreen->GetRenderTargetColor());
131 spriteBatch.End();
132 }
133 }
134
135 m_screensToDraw.clear();
136 }
137 }
138
140 {
141 pScreen->SetScreenManager(*this);
142 pScreen->LoadContent(*GetResourceManager());
143
144 m_screensToAdd.push_back(pScreen);
145 }
146}
Base class for all games. Provides graphics initialization, game loop, and rendering code....
Definition Game.h:22
virtual ResourceManager * GetResourceManager() const
Gets a pointer to the ResourceManager, for loading and managing resources.
Definition Game.h:52
Contains timing values for game updates and rendering.
Definition GameTime.h:21
Handles the state of multiple player input devices.
Definition InputState.h:22
Contains a 2D texture that can be used as a render target.
static void Set(RenderTarget *pTarget)
Set the desired target to render to when drawing.
Loads and manages the lifespan of objects from external files.
Base class for all game screens and menus.
Definition Screen.h:40
virtual bool ShouldUpdateBelow()
Checks if the screen manager should call Update on the screen below this one.
Definition Screen.h:136
virtual bool ShouldDrawBelow()
Checks if the screen manager should call Draw on the screen below this one.
Definition Screen.h:141
virtual void LoadContent(ResourceManager &resourceManager)
Called when resources need to be loaded.
Definition Screen.h:52
virtual void Update(const GameTime &gameTime)=0
Called when the game has determined that screen logic needs to be processed.
virtual bool NeedsToBeRemoved() const
Determines if the screen has completely faded out and needs to be removed by the ScreenManager.
Definition Screen.h:167
virtual void UnloadContent()
Called when resources need to be unloaded. Override this method to unload any screen-specific resourc...
Definition Screen.h:56
virtual Color GetRenderTargetColor() const
Get the color that will be used to tint the render target.
Definition Screen.h:179
virtual RenderTarget * GetRenderTarget() const
Get screen's render target.
Definition Screen.h:175
virtual void HandleInput(const InputState &input)
Called when the game has determined that player input needs to be processed.
Definition Screen.h:60
virtual void SetScreenManager(ScreenManager &screenManager)
Gets a pointer to the ScreenManager, for managing game screens.
Definition Screen.cpp:85
virtual bool ShouldHandleInputBelow()
Checks if the screen manager should call HandleInput on the screen below this one.
Definition Screen.h:130
Game * GetGame() const
Gets a pointer to the Game.
ResourceManager * GetResourceManager() const
Gets a pointer to the ResourceManager, for loading and managing resources.
virtual void AddScreen(Screen *pScreen)
Add a screen to be managed.
virtual void Update(const GameTime &gameTime)
Called when the game has determined that screen logic needs to be processed.
virtual void Draw(SpriteBatch &spriteBatch)
Called when the game determines it is time to draw a frame.
ScreenManager(Game *pGame)
Instantiate a screen manager object.
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.
static const Vector2 ZERO
A vector with both of its components set to zero.
Definition Vector2.h:31
Katana Engine is a library of classes, interfaces, and value types that provides a foundation for dev...
Definition Animation.cpp:18
void(* OnRemove)(Screen *pScreen)
Callback function for when a screen has completely exited and is about to be removed by the screen ma...
Definition Screen.h:36