Sanjay Madhav 8 лет назад
Родитель
Сommit
59edad40f3

+ 4 - 4
Chapter08/Actor.cpp

@@ -58,21 +58,21 @@ void Actor::UpdateActor(float deltaTime)
 {
 }
 
-void Actor::ProcessInput(const uint8_t* keyState)
+void Actor::ProcessInput(const struct InputState& state)
 {
 	if (mState == EActive)
 	{
 		// First process input for components
 		for (auto comp : mComponents)
 		{
-			comp->ProcessInput(keyState);
+			comp->ProcessInput(state);
 		}
 
-		ActorInput(keyState);
+		ActorInput(state);
 	}
 }
 
-void Actor::ActorInput(const uint8_t* keyState)
+void Actor::ActorInput(const struct InputState& state)
 {
 }
 

+ 2 - 2
Chapter08/Actor.h

@@ -32,9 +32,9 @@ public:
 	virtual void UpdateActor(float deltaTime);
 
 	// ProcessInput function called from Game (not overridable)
-	void ProcessInput(const uint8_t* keyState);
+	void ProcessInput(const struct InputState& state);
 	// Any actor-specific input code (overridable)
-	virtual void ActorInput(const uint8_t* keyState);
+	virtual void ActorInput(const struct InputState& state);
 
 	// Getters/setters
 	const Vector2& GetPosition() const { return mPosition; }

+ 1 - 1
Chapter08/Component.h

@@ -20,7 +20,7 @@ public:
 	// Update this component by delta time
 	virtual void Update(float deltaTime);
 	// Process input for this component
-	virtual void ProcessInput(const uint8_t* keyState) {}
+	virtual void ProcessInput(const struct InputState& state) {}
 	// Called when world transform changes
 	virtual void OnUpdateWorldTransform() { }
 

+ 21 - 4
Chapter08/Game.cpp

@@ -18,6 +18,7 @@
 #include "Ship.h"
 #include "Asteroid.h"
 #include "Random.h"
+#include "InputSystem.h"
 
 Game::Game()
 :mWindow(nullptr)
@@ -25,7 +26,6 @@ Game::Game()
 ,mIsRunning(true)
 ,mUpdatingActors(false)
 {
-	
 }
 
 bool Game::Initialize()
@@ -59,6 +59,14 @@ bool Game::Initialize()
 		SDL_Log("Failed to create window: %s", SDL_GetError());
 		return false;
 	}
+
+	// Initialize input system
+	mInputSystem = new InputSystem();
+	if (!mInputSystem->Initialize())
+	{
+		SDL_Log("Failed to initialize input system");
+		return false;
+	}
 	
 	// Create an OpenGL context
 	mContext = SDL_GL_CreateContext(mWindow);
@@ -104,6 +112,8 @@ void Game::RunLoop()
 
 void Game::ProcessInput()
 {
+	mInputSystem->PrepareForUpdate();
+
 	SDL_Event event;
 	while (SDL_PollEvent(&event))
 	{
@@ -114,9 +124,12 @@ void Game::ProcessInput()
 				break;
 		}
 	}
+
+	mInputSystem->Update();
+	const InputState& state = mInputSystem->GetState();
 	
-	const Uint8* keyState = SDL_GetKeyboardState(NULL);
-	if (keyState[SDL_SCANCODE_ESCAPE])
+	if (state.Keyboard.GetKeyState(SDL_SCANCODE_ESCAPE)
+		== EReleased)
 	{
 		mIsRunning = false;
 	}
@@ -124,7 +137,7 @@ void Game::ProcessInput()
 	mUpdatingActors = true;
 	for (auto actor : mActors)
 	{
-		actor->ProcessInput(keyState);
+		actor->ProcessInput(state);
 	}
 	mUpdatingActors = false;
 }
@@ -306,6 +319,10 @@ void Game::RemoveAsteroid(Asteroid* ast)
 void Game::Shutdown()
 {
 	UnloadData();
+
+	mInputSystem->Shutdown();
+	delete mInputSystem;
+
 	delete mSpriteVerts;
 	mSpriteShader->Unload();
 	delete mSpriteShader;

+ 3 - 0
Chapter08/Game.h

@@ -47,6 +47,9 @@ private:
 
 	// All the actors in the game
 	std::vector<class Actor*> mActors;
+
+	class InputSystem* mInputSystem;
+
 	// Any pending actors
 	std::vector<class Actor*> mPendingActors;
 

+ 6 - 5
Chapter08/InputComponent.cpp

@@ -8,6 +8,7 @@
 
 #include "InputComponent.h"
 #include "Actor.h"
+#include "InputSystem.h"
 
 InputComponent::InputComponent(class Actor* owner)
 :MoveComponent(owner)
@@ -19,15 +20,15 @@ InputComponent::InputComponent(class Actor* owner)
 	
 }
 
-void InputComponent::ProcessInput(const uint8_t* keyState)
+void InputComponent::ProcessInput(const InputState& state)
 {
 	// Calculate forward speed for MoveComponent
 	float forwardSpeed = 0.0f;
-	if (keyState[mForwardKey])
+	if (state.Keyboard.GetKeyValue(mForwardKey))
 	{
 		forwardSpeed += mMaxForwardSpeed;
 	}
-	if (keyState[mBackKey])
+	if (state.Keyboard.GetKeyValue(mBackKey))
 	{
 		forwardSpeed -= mMaxForwardSpeed;
 	}
@@ -35,11 +36,11 @@ void InputComponent::ProcessInput(const uint8_t* keyState)
 
 	// Calculate angular speed for MoveComponent
 	float angularSpeed = 0.0f;
-	if (keyState[mClockwiseKey])
+	if (state.Keyboard.GetKeyValue(mClockwiseKey))
 	{
 		angularSpeed += mMaxAngularSpeed;
 	}
-	if (keyState[mCounterClockwiseKey])
+	if (state.Keyboard.GetKeyValue(mCounterClockwiseKey))
 	{
 		angularSpeed -= mMaxAngularSpeed;
 	}

+ 1 - 1
Chapter08/InputComponent.h

@@ -16,7 +16,7 @@ public:
 	// Lower update order to update first
 	InputComponent(class Actor* owner);
 
-	void ProcessInput(const uint8_t* keyState) override;
+	void ProcessInput(const struct InputState& state) override;
 	
 	// Getters/setters for private variables
 	float GetMaxForward() const { return mMaxForwardSpeed; }

+ 19 - 10
Chapter08/InputSystem.cpp

@@ -7,17 +7,19 @@
 // ----------------------------------------------------------------
 
 #include "InputSystem.h"
+#include <SDL/SDL.h>
+#include <cstring>
 
-bool KeyboardState::GetKeyValue(SDL_Scancode code) const
+bool KeyboardState::GetKeyValue(int keyCode) const
 {
-	return mCurrState[code] == 1;
+	return mCurrState[keyCode] == 1;
 }
 
-ButtonState KeyboardState::GetKeyState(SDL_Scancode code) const
+ButtonState KeyboardState::GetKeyState(int keyCode) const
 {
-	if (mCurrState[code] == 0)
+	if (mCurrState[keyCode] == 0)
 	{
-		if (mPrevState[code] == 0)
+		if (mPrevState[keyCode] == 0)
 		{
 			return ENone;
 		}
@@ -28,7 +30,7 @@ ButtonState KeyboardState::GetKeyState(SDL_Scancode code) const
 	}
 	else // must be 1
 	{
-		if (mPrevState[code] == 0)
+		if (mPrevState[keyCode] == 0)
 		{
 			return EPressed;
 		}
@@ -39,12 +41,14 @@ ButtonState KeyboardState::GetKeyState(SDL_Scancode code) const
 	}
 }
 
-InputSystem::InputSystem()
-{
-}
-
 bool InputSystem::Initialize()
 {
+	// Assign current state pointer
+	mState.Keyboard.mCurrState = SDL_GetKeyboardState(NULL);
+	// Clear previous state memory
+	memset(mState.Keyboard.mPrevState, 0,
+		SDL_NUM_SCANCODES);
+
 	return true;
 }
 
@@ -54,6 +58,11 @@ void InputSystem::Shutdown()
 
 void InputSystem::PrepareForUpdate()
 {
+	// Copy current state to previous
+	// Keyboard
+	memcpy(mState.Keyboard.mPrevState,
+		mState.Keyboard.mCurrState,
+		SDL_NUM_SCANCODES);
 }
 
 void InputSystem::Update()

+ 6 - 4
Chapter08/InputSystem.h

@@ -22,12 +22,15 @@ enum ButtonState
 class KeyboardState
 {
 public:
+	// Friend so InputSystem can easily update it
 	friend class InputSystem;
-	bool GetKeyValue(SDL_Scancode code) const;
-	ButtonState GetKeyState(SDL_Scancode code) const;
+	// Get just the boolean true/false value of key
+	bool GetKeyValue(int keyCode) const;
+	// Get a state based on current and previous frame
+	ButtonState GetKeyState(int keyCode) const;
 private:
 	const Uint8* mCurrState;
-	const Uint8 mPrevState[SDL_NUM_SCANCODES];
+	Uint8 mPrevState[SDL_NUM_SCANCODES];
 };
 
 // Wrapper that contains current state of input
@@ -39,7 +42,6 @@ struct InputState
 class InputSystem
 {
 public:
-	InputSystem();
 	bool Initialize();
 	void Shutdown();
 

+ 4 - 2
Chapter08/Ship.cpp

@@ -11,6 +11,7 @@
 #include "InputComponent.h"
 #include "Game.h"
 #include "Laser.h"
+#include "InputSystem.h"
 
 Ship::Ship(Game* game)
 	:Actor(game)
@@ -35,9 +36,10 @@ void Ship::UpdateActor(float deltaTime)
 	mLaserCooldown -= deltaTime;
 }
 
-void Ship::ActorInput(const uint8_t* keyState)
+void Ship::ActorInput(const InputState& state)
 {
-	if (keyState[SDL_SCANCODE_SPACE] && mLaserCooldown <= 0.0f)
+	if (state.Keyboard.GetKeyValue(SDL_SCANCODE_SPACE)
+		&& mLaserCooldown <= 0.0f)
 	{
 		// Create a laser and set its position/rotation to mine
 		Laser* laser = new Laser(GetGame());

+ 1 - 1
Chapter08/Ship.h

@@ -14,7 +14,7 @@ public:
 	Ship(class Game* game);
 
 	void UpdateActor(float deltaTime) override;
-	void ActorInput(const uint8_t* keyState) override;
+	void ActorInput(const struct InputState& state) override;
 private:
 	float mLaserCooldown;
 };