Moving to Sandbox

引言

上次我们创建了一个正交相机类,今天我们需要将大量的代码移动到实际的沙盒程序中。

重构沙盒与应用

由于演示需要应用类已经变得非常臃肿了,让我们把冗余代码放到沙盒类中。由于修改较多,请参考 Moved sandbox code to Sandbox 修改。

相机移动

Sandbox.cpp 中的 OnUpdate() 函数定义相机移动方法:

void OnUpdate() override
{
// IFN_INFO("ExampleLayer::Update");
if (Infinite::Input::IsKeyPressed(IFN_KEY_LEFT))
m_CameraPosition.x -= m_CameraMoveSpeed;
else if (Infinite::Input::IsKeyPressed(IFN_KEY_RIGHT))
m_CameraPosition.x += m_CameraMoveSpeed;

if (Infinite::Input::IsKeyPressed(IFN_KEY_DOWN))
m_CameraPosition.y -= m_CameraMoveSpeed;
else if (Infinite::Input::IsKeyPressed(IFN_KEY_UP))
m_CameraPosition.y += m_CameraMoveSpeed;

if (Infinite::Input::IsKeyPressed(IFN_KEY_A))
m_CameraRotation += m_CameraRotationSpeed;
else if (Infinite::Input::IsKeyPressed(IFN_KEY_D))
m_CameraRotation -= m_CameraRotationSpeed;


Infinite::RenderCommand::SetClearColor({ 0.1f, 0.1f, 0.1f, 1 });
Infinite::RenderCommand::Clear();

m_Camera.SetPosition(m_CameraPosition);
m_Camera.SetRotation(m_CameraRotation);

Infinite::Renderer::BeginScene(m_Camera);

Infinite::Renderer::Submit(m_BlueShader, m_SquareVertexArray);
Infinite::Renderer::Submit(m_Shader, m_VertexArray);

Infinite::Renderer::EndScene();

if (Infinite::Input::IsKeyPressed(IFN_KEY_TAB))
IFN_INFO("Tab key is pressed (poll)!");
}

在私有变量这里定义相机的位置、角度以及两者的移动速度:

private:

std::shared_ptr<Infinite::Shader> m_Shader;
std::shared_ptr<Infinite::VertexArray> m_VertexArray;

std::shared_ptr<Infinite::Shader> m_BlueShader;
std::shared_ptr<Infinite::VertexArray> m_SquareVertexArray;

Infinite::OrthographicCamera m_Camera;
glm::vec3 m_CameraPosition;
float m_CameraRotation = 0.0f;
float m_CameraMoveSpeed = 0.1f;
float m_CameraRotationSpeed = 0.1f;
};

调试