Space Fighter
A "shmup" game for Computer Programming C++
Loading...
Searching...
No Matches
Game.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{
19 int Game::s_screenWidth = 800;
20 int Game::s_screenHeight = 600;
21
22 std::string Game::s_contentDirectory = "..\\Content\\";
23 std::string Game::s_windowTitle = "Game";
24
25
27 {
28 m_isInitialized = al_init();
29
30 al_install_audio();
31 al_init_acodec_addon();
32
33 srand(time(nullptr));
34
35 m_requireOpenGL = false;
36
37 m_pInput = nullptr;
38 m_pFrameCounterFont = nullptr;
39
40 m_currentTime = al_get_time();
41 m_previousTime = al_get_time();
42 m_frameCounter = 0;
43 m_actualFramesPerSec = 60;
45
46 s_windowTitle = "";
47
48 m_pResourceManager = new ResourceManager();
49 m_pScreenManager = nullptr;
50
51 m_pSpriteBatch = new SpriteBatch();
52
54
55 srand(time(nullptr));
56 }
57
58
60 {
61 m_pResourceManager->UnloadAllResources();
62
63 delete m_pResourceManager;
64 delete m_pSpriteBatch;
65 delete m_pInput;
66 delete m_pGameTime;
67 }
68
69
70 void Game::Update(const GameTime& gameTime)
71 {
72 ScreenManager *pScreenManager = GetScreenManager();
73 if (!pScreenManager) return;
74 pScreenManager->HandleInput(*m_pInput);
75 pScreenManager->Update(*m_pGameTime);
76 }
77
78 void Game::Draw(SpriteBatch& spriteBatch)
79 {
80 ScreenManager* pScreenManager = GetScreenManager();
81 if (!pScreenManager) return;
82 pScreenManager->Draw(spriteBatch);
83 }
84
85
87 {
88 m_isRunning = true;
89
90 if (!m_isInitialized)
91 {
92 const char message[] = "Failed to initialize the game.";
93 al_show_native_message_box(nullptr, nullptr, nullptr, message, nullptr, 0);
94 return -1;
95 }
96
97 al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_REQUIRE);
98 if (m_isFullScreen) al_set_new_display_flags(ALLEGRO_FULLSCREEN);
99 if (m_requireOpenGL) al_set_new_display_flags(ALLEGRO_OPENGL);
100
101 ALLEGRO_DISPLAY *pDisplay = al_create_display(GetScreenWidth(), GetScreenHeight());
102 if (!pDisplay)
103 {
104 const char message[] = "Failed to initialize display.";
105 al_show_native_message_box(nullptr, nullptr, nullptr, message, nullptr, 0);
106 return -1;
107 }
108
109
110 if (s_windowTitle.length() == 0) s_windowTitle = GetName();
111 al_set_window_title(pDisplay, s_windowTitle.c_str());
112 al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
113 al_inhibit_screensaver(true);
114
115 RenderTarget::SetDisplay(pDisplay);
116 bool redraw = true;
117
118
119 // Create and register events
120 ALLEGRO_EVENT_QUEUE *pEventQueue = al_create_event_queue();
121 ALLEGRO_TIMER *pTimer = al_create_timer(m_inverseTargetFrames);
122
123 al_register_event_source(pEventQueue, al_get_timer_event_source(pTimer));
124 al_register_event_source(pEventQueue, al_get_display_event_source(pDisplay));
125
126 m_pInput = new InputState();
127
128 ALLEGRO_EVENT_SOURCE *joystickEventSource = al_get_joystick_event_source();
129 if (joystickEventSource)
130 {
131 al_register_event_source(pEventQueue, joystickEventSource);
132 }
133
135
136 LoadContent(*m_pResourceManager);
137
138 al_start_timer(pTimer);
139 ALLEGRO_EVENT alEvent;
140
141 while (IsRunning())
142 {
143 al_wait_for_event(pEventQueue, &alEvent);
144
145 switch (alEvent.type)
146 {
147 case ALLEGRO_EVENT_DISPLAY_CLOSE:
148 al_stop_timer(pTimer);
149 Quit();
150 break;
151
152 case ALLEGRO_EVENT_JOYSTICK_CONFIGURATION:
153 m_pInput->UpdateConfigurationEvent();
154 break;
155
156 case ALLEGRO_EVENT_JOYSTICK_AXIS:
157 m_pInput->UpdateAxisEvent(alEvent);
158 break;
159 case ALLEGRO_EVENT_JOYSTICK_BUTTON_UP:
160 m_pInput->UpdateButtonEvent(alEvent, ButtonState::RELEASED);
161 break;
162 case ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN:
163 m_pInput->UpdateButtonEvent(alEvent, ButtonState::PRESSED);
164 break;
165
166 case ALLEGRO_EVENT_TIMER:
167 m_pGameTime->Update();
168 Update(*m_pGameTime);
169 m_pInput->Update();
170 redraw = true;
171 break;
172 }
173
174 if (redraw && al_event_queue_is_empty(pEventQueue))
175 {
176 redraw = false;
177
178 al_clear_to_color(al_map_rgb(0, 0, 0));
179
180 Draw(*m_pSpriteBatch);
181
182 m_frameCounter++;
183
184 al_flip_display();
185 }
186 }
187
189
190 al_destroy_display(pDisplay);
191
192 return 0;
193 }
194
196 {
197 m_currentTime = al_get_time();
198
199 if (m_currentTime - m_previousTime >= 1.0)
200 {
201 m_actualFramesPerSec = m_frameCounter / (m_currentTime - m_previousTime);
202 m_frameCounter = 0;
203 m_previousTime = m_currentTime;
204 }
205
206 if (m_pFrameCounterFont)
207 {
208 Color color(0, 1, 0);
209
210 float percent = (float)(m_actualFramesPerSec * m_inverseTargetFrames);
211
212 if (percent >= 0.98f)
213 {
214 color = Color(0, 0.5, 0);
215 }
216 if (percent < 0.98f && percent >= 0.8f)
217 {
218 color = Color(1, 1, 0);
219 }
220 else if (percent < 0.8f && percent >= 0.6f)
221 {
222 color = Color(1, 0.5, 0);
223 }
224 else if (percent < 0.6f)
225 {
226 color = Color(1, 0, 0);
227 }
228
229 char buffer[16];
230 sprintf_s(buffer, "FPS: %.2f", (float)m_actualFramesPerSec);
231 std::string text = buffer;
232
233 m_pSpriteBatch->Begin();
234 m_pSpriteBatch->DrawString(m_pFrameCounterFont, &text, Vector2(10, 10), color);
235 m_pSpriteBatch->End();
236 }
237 else
238 {
239 std::cout << "FPS: " << m_actualFramesPerSec << std::endl;
240 }
241 }
242
243 void Game::SetTargetFramesPerSecond(int const frames)
244 {
245 m_targetFramesPerSecond = frames;
246 m_inverseTargetFrames = 1.0 / frames;
247 }
248}
static void ReserveSamples(int count)
Definition AudioSample.h:28
Represents a four-component color using red, green, blue, and alpha data.
Definition Color.h:21
virtual std::string GetName() const =0
Gets the name of the game.
virtual int Run()
Runs the game instance.
Definition Game.cpp:86
virtual void ResetGameTime()
Resets the game's timing values.
Definition Game.h:120
virtual void DisplayFrameRate()
Displays the game's current frame rate.
Definition Game.cpp:195
static int GetScreenHeight()
Gets the screen width in pixels.
Definition Game.h:40
static int GetScreenWidth()
Gets the screen width in pixels.
Definition Game.h:36
virtual void SetTargetFramesPerSecond(int const frames)
Sets the target frame rate for the game.
Definition Game.cpp:243
virtual ScreenManager * GetScreenManager() const
Gets a pointer to the ScreenManager, for managing game screens.
Definition Game.h:56
virtual void UnloadContent()
Called when resources need to be unloaded. Override this method to unload any game-specific resources...
Definition Game.h:69
virtual void Quit()
Quits the game.
Definition Game.h:80
virtual ~Game()
Definition Game.cpp:59
virtual void Update(const GameTime &gameTime)
Called when the game has determined that game logic needs to be processed.
Definition Game.cpp:70
virtual void LoadContent(ResourceManager &resourceManager)
Called when resources need to be loaded.
Definition Game.h:65
virtual void Draw(SpriteBatch &spriteBatch)
Called when the game determines it is time to draw a frame.
Definition Game.cpp:78
Contains timing values for game updates and rendering.
Definition GameTime.h:21
Handles the state of multiple player input devices.
Definition InputState.h:22
static void SetDisplay(ALLEGRO_DISPLAY *pDisplay)
Set the display of the target.
Loads and manages the lifespan of objects from external files.
void UnloadAllResources()
Unloads all game resources.
Updates, renders, and manages transitions between instances of the Screen class.
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.
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 DrawString(const Font *pFont, std::string *text, const Vector2 position, const Color color=Color::White, const TextAlign alignment=TextAlign::Left, const float drawDepth=0)
Adds a string to a batch of sprites to be rendered.
Defines a vector with 2 components (x and y).
Definition Vector2.h:21
Katana Engine is a library of classes, interfaces, and value types that provides a foundation for dev...
Definition Animation.cpp:18