19 int Game::s_screenWidth = 800;
20 int Game::s_screenHeight = 600;
22 std::string Game::s_contentDirectory =
"..\\Content\\";
23 std::string Game::s_windowTitle =
"Game";
28 m_isInitialized = al_init();
31 al_init_acodec_addon();
35 m_requireOpenGL =
false;
38 m_pFrameCounterFont =
nullptr;
40 m_currentTime = al_get_time();
41 m_previousTime = al_get_time();
43 m_actualFramesPerSec = 60;
49 m_pScreenManager =
nullptr;
63 delete m_pResourceManager;
64 delete m_pSpriteBatch;
73 if (!pScreenManager)
return;
74 pScreenManager->HandleInput(*m_pInput);
75 pScreenManager->
Update(*m_pGameTime);
81 if (!pScreenManager)
return;
82 pScreenManager->
Draw(spriteBatch);
92 const char message[] =
"Failed to initialize the game.";
93 al_show_native_message_box(
nullptr,
nullptr,
nullptr, message,
nullptr, 0);
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);
104 const char message[] =
"Failed to initialize display.";
105 al_show_native_message_box(
nullptr,
nullptr,
nullptr, message,
nullptr, 0);
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);
120 ALLEGRO_EVENT_QUEUE *pEventQueue = al_create_event_queue();
121 ALLEGRO_TIMER *pTimer = al_create_timer(m_inverseTargetFrames);
123 al_register_event_source(pEventQueue, al_get_timer_event_source(pTimer));
124 al_register_event_source(pEventQueue, al_get_display_event_source(pDisplay));
128 ALLEGRO_EVENT_SOURCE *joystickEventSource = al_get_joystick_event_source();
129 if (joystickEventSource)
131 al_register_event_source(pEventQueue, joystickEventSource);
138 al_start_timer(pTimer);
139 ALLEGRO_EVENT alEvent;
143 al_wait_for_event(pEventQueue, &alEvent);
145 switch (alEvent.type)
147 case ALLEGRO_EVENT_DISPLAY_CLOSE:
148 al_stop_timer(pTimer);
152 case ALLEGRO_EVENT_JOYSTICK_CONFIGURATION:
153 m_pInput->UpdateConfigurationEvent();
156 case ALLEGRO_EVENT_JOYSTICK_AXIS:
157 m_pInput->UpdateAxisEvent(alEvent);
159 case ALLEGRO_EVENT_JOYSTICK_BUTTON_UP:
162 case ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN:
166 case ALLEGRO_EVENT_TIMER:
167 m_pGameTime->Update();
174 if (redraw && al_event_queue_is_empty(pEventQueue))
178 al_clear_to_color(al_map_rgb(0, 0, 0));
180 Draw(*m_pSpriteBatch);
190 al_destroy_display(pDisplay);
197 m_currentTime = al_get_time();
199 if (m_currentTime - m_previousTime >= 1.0)
201 m_actualFramesPerSec = m_frameCounter / (m_currentTime - m_previousTime);
203 m_previousTime = m_currentTime;
206 if (m_pFrameCounterFont)
208 Color color(0, 1, 0);
210 float percent = (float)(m_actualFramesPerSec * m_inverseTargetFrames);
212 if (percent >= 0.98f)
214 color =
Color(0, 0.5, 0);
216 if (percent < 0.98f && percent >= 0.8f)
218 color =
Color(1, 1, 0);
220 else if (percent < 0.8f && percent >= 0.6f)
222 color =
Color(1, 0.5, 0);
224 else if (percent < 0.6f)
226 color =
Color(1, 0, 0);
230 sprintf_s(buffer,
"FPS: %.2f", (
float)m_actualFramesPerSec);
231 std::string text = buffer;
233 m_pSpriteBatch->
Begin();
235 m_pSpriteBatch->
End();
239 std::cout <<
"FPS: " << m_actualFramesPerSec << std::endl;
245 m_targetFramesPerSecond = frames;
246 m_inverseTargetFrames = 1.0 / frames;
static void ReserveSamples(int count)
Represents a four-component color using red, green, blue, and alpha data.
virtual std::string GetName() const =0
Gets the name of the game.
virtual int Run()
Runs the game instance.
virtual void ResetGameTime()
Resets the game's timing values.
virtual void DisplayFrameRate()
Displays the game's current frame rate.
static int GetScreenHeight()
Gets the screen width in pixels.
static int GetScreenWidth()
Gets the screen width in pixels.
virtual void SetTargetFramesPerSecond(int const frames)
Sets the target frame rate for the game.
virtual ScreenManager * GetScreenManager() const
Gets a pointer to the ScreenManager, for managing game screens.
virtual void UnloadContent()
Called when resources need to be unloaded. Override this method to unload any game-specific resources...
virtual void Quit()
Quits the game.
virtual void Update(const GameTime &gameTime)
Called when the game has determined that game logic needs to be processed.
virtual void LoadContent(ResourceManager &resourceManager)
Called when resources need to be loaded.
virtual void Draw(SpriteBatch &spriteBatch)
Called when the game determines it is time to draw a frame.
Contains timing values for game updates and rendering.
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.
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).
Katana Engine is a library of classes, interfaces, and value types that provides a foundation for dev...