diff --git a/framework/platform-dx/game.cpp b/framework/platform-dx/game.cpp index da44a36..e673ef6 100644 --- a/framework/platform-dx/game.cpp +++ b/framework/platform-dx/game.cpp @@ -28,7 +28,7 @@ namespace xna { int Game::StartGameLoop() { MSG msg{}; - impl->_stepTimer = xna::StepTimer(); + impl->_stepTimer = xna::StepTimer(); do { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) @@ -42,6 +42,7 @@ namespace xna { } while (msg.message != WM_QUIT); + EndRun(); return static_cast(msg.wParam); } @@ -58,7 +59,9 @@ namespace xna { Update(_currentGameTime); }); + BeginDraw(); Draw(_currentGameTime); + EndDraw(); } int Game::Run() { @@ -79,6 +82,7 @@ namespace xna { } isRunning = true; + BeginRun(); return StartGameLoop(); } catch (std::exception& e) { diff --git a/inc/xna/game/game.hpp b/inc/xna/game/game.hpp index 1749ddd..2ac9070 100644 --- a/inc/xna/game/game.hpp +++ b/inc/xna/game/game.hpp @@ -53,19 +53,29 @@ namespace xna { void ResizeWindow(int width, int heigth); protected: + //Starts the drawing of a frame. This method is followed by calls to Draw and EndDraw. + virtual void BeginDraw(){} + //Called after all components are initialized but before the first update in the game loop. + virtual void BeginRun() {}; + //Called when the game determines it is time to draw a frame. virtual void Draw(GameTime const& gameTime); + //Ends the drawing of a frame. This method is preceeded by calls to Draw and BeginDraw. + virtual void EndDraw() {} + //Called after the game loop has stopped running before exiting. + virtual void EndRun() {}; + //Called after the Game and GraphicsDevice are created, but before LoadContent. virtual void Initialize(); + //Called when graphics resources need to be loaded. virtual void LoadContent(){} - virtual void Update(GameTime const& gameTime); - int StartGameLoop(); - - public: - sptr graphicsDevice = nullptr; - - protected: - sptr services = nullptr; + //Called when the game has determined that game logic needs to be processed. + virtual void Update(GameTime const& gameTime); private: + int StartGameLoop(); + + private: + friend class GraphicsDeviceManager; + sptr _gameComponents = nullptr; sptr _gameWindow{ nullptr }; sptr _audioEngine = nullptr; @@ -77,6 +87,8 @@ namespace xna { bool isFixedTimeStep{ true }; TimeSpan targetElapsedTime{ TimeSpan::FromTicks(166667L) }; bool isRunning{ false }; + sptr services = nullptr; + sptr graphicsDevice = nullptr; public: struct PlatformImplementation; diff --git a/samples/01_blank/xna.cpp b/samples/01_blank/xna.cpp index ae50f89..cc68a70 100644 --- a/samples/01_blank/xna.cpp +++ b/samples/01_blank/xna.cpp @@ -20,14 +20,14 @@ namespace xna { //graphics->Initialize(); graphics->ApplyChanges(); - std::any device = graphicsDevice; - services->AddService(*typeof(), device); + std::any device = Device(); + Services()->AddService(*typeof(), device); Game::Initialize(); } void LoadContent() override { - spriteBatch = snew(graphicsDevice); + spriteBatch = snew(Device()); Game::LoadContent(); } @@ -39,7 +39,7 @@ namespace xna { } void Draw(GameTime const& gameTime) override { - graphicsDevice->Clear(Colors::CornflowerBlue); + Device()->Clear(Colors::CornflowerBlue); Game::Draw(gameTime); } diff --git a/samples/02_PlatfformerStarterKit/game.cpp b/samples/02_PlatfformerStarterKit/game.cpp index fc96f09..1d51833 100644 --- a/samples/02_PlatfformerStarterKit/game.cpp +++ b/samples/02_PlatfformerStarterKit/game.cpp @@ -24,14 +24,14 @@ namespace PlatformerStarterKit { graphics = snew(game->shared_from_this()); graphics->ApplyChanges(); - std::any device = graphicsDevice; - services->AddService(*typeof(), device); + std::any device = Device(); + Services()->AddService(*typeof(), device); Game::Initialize(); } void LoadContent() override { - spriteBatch = snew(graphicsDevice); + spriteBatch = snew(Device()); // Load fonts hudFont = Content()->Load("Fonts/Hud"); @@ -55,7 +55,7 @@ namespace PlatformerStarterKit { } void Draw(GameTime const& gameTime) override { - graphicsDevice->Clear(Colors::CornflowerBlue); + Device()->Clear(Colors::CornflowerBlue); spriteBatch->Begin(); @@ -111,7 +111,7 @@ namespace PlatformerStarterKit { levelIndex = -1; } - level = snew(services, levelPath); + level = snew(Services(), levelPath); level->Initialize(); } @@ -122,7 +122,7 @@ namespace PlatformerStarterKit { void DrawHud() { - auto titleSafeArea = graphicsDevice->Viewport().Bounds(); + auto titleSafeArea = Device()->Viewport().Bounds(); auto hudLocation = Vector2(titleSafeArea.X, titleSafeArea.Y); auto center = Vector2(titleSafeArea.X + titleSafeArea.Width / 2.0f, titleSafeArea.Y + titleSafeArea.Height / 2.0f); diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 24ef843..45257e9 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -4,4 +4,4 @@ # Add source to this project's executable. add_subdirectory ("01_blank") -#add_subdirectory ("02_PlatfformerStarterKit") +add_subdirectory ("02_PlatfformerStarterKit")