1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Implementações em Game

This commit is contained in:
Danilo 2024-08-01 16:33:06 -03:00
parent 34d9533261
commit 2135b5859b
5 changed files with 36 additions and 20 deletions

View File

@ -28,7 +28,7 @@ namespace xna {
int Game::StartGameLoop() { int Game::StartGameLoop() {
MSG msg{}; MSG msg{};
impl->_stepTimer = xna::StepTimer(); impl->_stepTimer = xna::StepTimer();
do { do {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
@ -42,6 +42,7 @@ namespace xna {
} while (msg.message != WM_QUIT); } while (msg.message != WM_QUIT);
EndRun();
return static_cast<int>(msg.wParam); return static_cast<int>(msg.wParam);
} }
@ -58,7 +59,9 @@ namespace xna {
Update(_currentGameTime); Update(_currentGameTime);
}); });
BeginDraw();
Draw(_currentGameTime); Draw(_currentGameTime);
EndDraw();
} }
int Game::Run() { int Game::Run() {
@ -79,6 +82,7 @@ namespace xna {
} }
isRunning = true; isRunning = true;
BeginRun();
return StartGameLoop(); return StartGameLoop();
} }
catch (std::exception& e) { catch (std::exception& e) {

View File

@ -53,19 +53,29 @@ namespace xna {
void ResizeWindow(int width, int heigth); void ResizeWindow(int width, int heigth);
protected: 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); 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(); virtual void Initialize();
//Called when graphics resources need to be loaded.
virtual void LoadContent(){} virtual void LoadContent(){}
virtual void Update(GameTime const& gameTime); //Called when the game has determined that game logic needs to be processed.
int StartGameLoop(); virtual void Update(GameTime const& gameTime);
public:
sptr<GraphicsDevice> graphicsDevice = nullptr;
protected:
sptr<GameServiceContainer> services = nullptr;
private: private:
int StartGameLoop();
private:
friend class GraphicsDeviceManager;
sptr<GameComponentCollection> _gameComponents = nullptr; sptr<GameComponentCollection> _gameComponents = nullptr;
sptr<GameWindow> _gameWindow{ nullptr }; sptr<GameWindow> _gameWindow{ nullptr };
sptr<AudioEngine> _audioEngine = nullptr; sptr<AudioEngine> _audioEngine = nullptr;
@ -77,6 +87,8 @@ namespace xna {
bool isFixedTimeStep{ true }; bool isFixedTimeStep{ true };
TimeSpan targetElapsedTime{ TimeSpan::FromTicks(166667L) }; TimeSpan targetElapsedTime{ TimeSpan::FromTicks(166667L) };
bool isRunning{ false }; bool isRunning{ false };
sptr<GameServiceContainer> services = nullptr;
sptr<GraphicsDevice> graphicsDevice = nullptr;
public: public:
struct PlatformImplementation; struct PlatformImplementation;

View File

@ -20,14 +20,14 @@ namespace xna {
//graphics->Initialize(); //graphics->Initialize();
graphics->ApplyChanges(); graphics->ApplyChanges();
std::any device = graphicsDevice; std::any device = Device();
services->AddService(*typeof<GraphicsDevice>(), device); Services()->AddService(*typeof<GraphicsDevice>(), device);
Game::Initialize(); Game::Initialize();
} }
void LoadContent() override { void LoadContent() override {
spriteBatch = snew<SpriteBatch>(graphicsDevice); spriteBatch = snew<SpriteBatch>(Device());
Game::LoadContent(); Game::LoadContent();
} }
@ -39,7 +39,7 @@ namespace xna {
} }
void Draw(GameTime const& gameTime) override { void Draw(GameTime const& gameTime) override {
graphicsDevice->Clear(Colors::CornflowerBlue); Device()->Clear(Colors::CornflowerBlue);
Game::Draw(gameTime); Game::Draw(gameTime);
} }

View File

@ -24,14 +24,14 @@ namespace PlatformerStarterKit {
graphics = snew<GraphicsDeviceManager>(game->shared_from_this()); graphics = snew<GraphicsDeviceManager>(game->shared_from_this());
graphics->ApplyChanges(); graphics->ApplyChanges();
std::any device = graphicsDevice; std::any device = Device();
services->AddService(*typeof<GraphicsDevice>(), device); Services()->AddService(*typeof<GraphicsDevice>(), device);
Game::Initialize(); Game::Initialize();
} }
void LoadContent() override { void LoadContent() override {
spriteBatch = snew<SpriteBatch>(graphicsDevice); spriteBatch = snew<SpriteBatch>(Device());
// Load fonts // Load fonts
hudFont = Content()->Load<PSpriteFont>("Fonts/Hud"); hudFont = Content()->Load<PSpriteFont>("Fonts/Hud");
@ -55,7 +55,7 @@ namespace PlatformerStarterKit {
} }
void Draw(GameTime const& gameTime) override { void Draw(GameTime const& gameTime) override {
graphicsDevice->Clear(Colors::CornflowerBlue); Device()->Clear(Colors::CornflowerBlue);
spriteBatch->Begin(); spriteBatch->Begin();
@ -111,7 +111,7 @@ namespace PlatformerStarterKit {
levelIndex = -1; levelIndex = -1;
} }
level = snew<Level>(services, levelPath); level = snew<Level>(Services(), levelPath);
level->Initialize(); level->Initialize();
} }
@ -122,7 +122,7 @@ namespace PlatformerStarterKit {
void DrawHud() void DrawHud()
{ {
auto titleSafeArea = graphicsDevice->Viewport().Bounds(); auto titleSafeArea = Device()->Viewport().Bounds();
auto hudLocation = Vector2(titleSafeArea.X, titleSafeArea.Y); auto hudLocation = Vector2(titleSafeArea.X, titleSafeArea.Y);
auto center = Vector2(titleSafeArea.X + titleSafeArea.Width / 2.0f, auto center = Vector2(titleSafeArea.X + titleSafeArea.Width / 2.0f,
titleSafeArea.Y + titleSafeArea.Height / 2.0f); titleSafeArea.Y + titleSafeArea.Height / 2.0f);

View File

@ -4,4 +4,4 @@
# Add source to this project's executable. # Add source to this project's executable.
add_subdirectory ("01_blank") add_subdirectory ("01_blank")
#add_subdirectory ("02_PlatfformerStarterKit") add_subdirectory ("02_PlatfformerStarterKit")