Repository navigation
Examples & Usage
Importing • Main • Application • Layers • Scenes • GameObjects & Components
Here you'll find some examples of setting up and using the TNAH engine!
The TNAH engine defines the main function for you as well as manages the application loop, so you don't need to define a int main() function in your application!
Every application using the TNAH engine, requires its own Application class being derived from the type tnah::Application, this allows you to not only override some application-specific functions you may like to customize but also allows you to keep any custom use-case or additional functions you define external to the engine source code. Here's an example of how to define a TNAH application.
#pragma once
#include <TNAH-App.h>
class Application : public tnah::Application
{
public:
Application();
~Application();
void OnEvent(tnah::Event& e) override;
};
inline tnah::Application* tnah::CreateApplication()
{
return new Application();
}Within the TNAH engine, an application is defined to at least have one user-defined layer attached. The application class already attaches a default ImGui layer to the application which handles setting up and initializing ImGui but an additional user-defined layer is required. Here's an example of a layer class within an application.
#pragma once
#include <TNAH-App.h>
class MainLayer : public tnah::Layer
{
public:
MainLayer();
void OnUpdate(tnah::Timestep deltaTime) override;
void OnFixedUpdate(tnah::Timestep ts,tnah::PhysicsTimestep ps) override;
void OnImGuiRender() override;
void OnEvent(tnah::Event& event) override;
void OnAttach() override;
};
This layer also is required to be defined and created when your application instance is created. Within your application class source file, add:
Application::Application()
:tnah::Application("This is a Example Application Name")
{
PushLayer(new MainLayer());
}Now an empty layer is being created within the application, you can use the provided functions to override and define your own logic for the layer. Here are some examples of different overrides:
MainLayer::MainLayer()
:Layer("This is a example Layer Name")
{
// Dont worry about where m_Camera came from just yet!
auto& ct = m_Camera.Transform();
auto& cc = m_Camera.GetComponent<tnah::CameraComponent>();
cc.Camera.SetViewportSize(1280, 720);
ct.Position = glm::vec3(0, -4, 1);
cc.ClearMode = tnah::CameraClearMode::Skybox;
auto& m_Skybox = m_Camera.AddComponent<tnah::SkyboxComponent>();
}void MainLayer::OnUpdate(tnah::Timestep deltaTime)
{
if(tnah::Input::IsKeyPressed(tnah::Key::W))
{
m_Camera.Transform().Position += m_Camera.Transform().Forward * m_CameraMovementSpeed * deltaTime.GetSeconds();
}
if(tnah::Input::IsKeyPressed(tnah::Key::S))
{
m_Camera.Transform().Position -= m_Camera.Transform().Forward * m_CameraMovementSpeed * deltaTime.GetSeconds();
}
if(tnah::Input::IsKeyPressed(tnah::Key::A))
{
m_Camera.Transform().Position -= m_Camera.Transform().Right * m_CameraMovementSpeed * deltaTime.GetSeconds();
}
if(tnah::Input::IsKeyPressed(tnah::Key::D))
{
m_Camera.Transform().Position += m_Camera.Transform().Right * m_CameraMovementSpeed * deltaTime.GetSeconds();
}
//Camera Mouse rotation controls
auto mousePos = tnah::Input::GetMousePos();
if (m_FirstMouseInput)
{
m_LastMouseXPos = mousePos.first;
m_LastMouseYPos = mousePos.second;
m_FirstMouseInput = false;
}
float offsetX = mousePos.first - m_LastMouseXPos;
float offsetY = m_LastMouseYPos - mousePos.second;
m_LastMouseXPos = mousePos.first;
m_LastMouseYPos = mousePos.second;
offsetX *= m_CameraMouseSensitivity;
offsetY *= m_CameraMouseSensitivity;
m_Camera.Transform().Rotation.x += offsetX;
m_Camera.Transform().Rotation.y += offsetY;
if (m_Camera.Transform().Rotation.y > 89.0f)
{
m_Camera.Transform().Rotation.y = 89.0f;
}
if (m_Camera.Transform().Rotation.y < -89.0f)
{
m_Camera.Transform().Rotation.y = -89.0f;
}
m_ActiveScene->OnUpdate(deltaTime); // Dont worry about this yet!
}ImGui panels can be rendered within the same layer ontop of any other items rendered to the applicaion window, no need to create a new layer just for a panel! Standard ImGui useage applies when creating panels/windows and any UI within said panels. The TNAH engine does provide a simplified UI system that takes away some of the guess work out of using ImGui if your not familuar. You can find out more on the UI Wiki page!
void MainLayer::OnImGuiRender()
{
ImGui::Begin("Application", &m_ApplicationPanel);
ImGui::Text("%.1f FPS | %.0f ms", ImGui::GetIO().Framerate, 1000.0f / ImGui::GetIO().Framerate);
ImGui::Text("Options");
if(ImGui::Checkbox("Debug Mode", &tnah::Application::Get().GetDebugModeStatus()))
{
tnah::Application::Get().SetDebugStatusChange();
}
if(ImGui::CollapsingHeader("Controls & Toggles"))
{
ImGui::Text("Key Controls");
ImGui::BulletText("0 - Debug");
ImGui::BulletText("1 - Cursor");
ImGui::BulletText("2 - Wireframe mode");
ImGui::BulletText("3 - Borderless fullscreen");
ImGui::BulletText("4 - VSync");
ImGui::BulletText("5 - Camera look");
ImGui::BulletText("6 - Camera move");
ImGui::BulletText("L/R Shift - Camera speed override");
ImGui::BulletText("ESC - Quit");
}
...
//More ImGui Headers, text, etc here.
...
ImGui::End();
}Or
void MainLayer::OnImGuiRender()
{
tnah::UI::BeginWindow("Options");
tnah::UI::Text("%.f fps", ImGui::GetIO().Framerate);
...
tnah::UI::EndWindow();
}When you make a layer, you now need something to render and do inside that layer!
You can create a scene object by firstly defining a scene Ref<> object in the layers class definition.
class MainLayer : public tnah::Layer
{
public:
... // Function definitions here!
private:
tnah::Ref<tnah::Scene> m_ActiveScene;
};And to create the scene:
MainLayer::MainLayer()
:Layer("This is an example layer name!")
{
m_ActiveScene = tnah::Scene::CreateEmptyScene();
...
}Scenes hold all the GameObjects as well as other usefull objects like the Entity Component System registry.
The scene also handles any rendering that contained GameObjects require! For this reason, the active scene needs to be
updated every OnUpdate() and OnFixedUpdate() within its containing layer. This is done just by calling:
void MainLayer::OnUpdate(tnah::Timestep deltaTime)
{
...
m_ActiveScene->OnUpdate(deltaTime);
}And
void MainLayer::OnFixedUpdate(tnah::Timestep ts, tnah::PhysicsTimestep ps)
{
m_ActiveScene->OnFixedUpdate(ts, ps);
}GameObjects within a scene are created by using the scenes CreateGameObject() function and have a set components by default.
Default components attached to a given GameObject are a Transform Component and a
Tag Component,
being both the transform of the GameObject and the name/tag of the GameObject. Here's an example of creating a component:
MainLayer::MainLayer()
:Layer("Main Layer")
{
...
auto& gameObject = m_ActiveScene->CreateGameObject("Cube");
...
}The string passed into the CreateGameObject() function is the name/tag your giving this GameObject. The function also passes back a
reference as a GameObject& type so you can save and store a reference to the GameObject if you want to use it later without having to
search the scene for it again.
To access a component on a GameObject, you have a few functions available to you to get a reference to the component attached to it. These functions include:
gameObject.GetComponent<Type>(Peram, ...);
gameObject.HasComponent<Type>(Peram, ...);
gameObject.AddComponent<Type>(Peram, ...);
gameObject.RemoveComponent<Type>(Peram, ...);Each function is a template and requires a component type to be defined between the <> brackets. The AddComponent<>()
function also will accept perameters for the components constructor.
MainLayer::MainLayer()
:Layer("Main Layer")
{
...
auto& gameObject = m_ActiveScene->CreateGameObject("Cube");
auto& terrain = gameObject.AddComponent<TerrainComponent>("assets/heightmaps/map.tga");
...
}In this example, we're creating a Terrain Component on the before created gameObject
and providing it with a heightmap to generate a terrain from. Like most other create or add functions, the AddComponent<>() function
also returns a reference object back to the user so you can use it later without getting it from the GameObject again using the GetComponent<>()
Note The GetComponent<>() function does some basic checks for on the GameObject but is up to you to ensure the GameObject
is holding the component you want to get with the HasComponent<>() function.
A GameObject also has a short form function to access the Transform Component on itself using:
auto& transform = gameObject.Transform();This gives you quick access to the component to allow you to manipulate positions, rotations and scales saving the use of the GetComponent<>() function.