YoggieEngine/SandboxApplication/Sandbox.cpp
nigel 02727c74bb Clean up of sandbox
* Added a statistics panel
* Added comments to `Sandbox.cpp`
* Moved utility functions and GUI code out of sandbox.cpp
2022-05-29 15:23:08 +02:00

143 lines
2.9 KiB
C++

#include "BarinkEngine.h"
#include "imgui.h"
#include "GUI.h"
#include "Util.h"
/*
* Define globals
*/
Camera* cam;
Renderable* Cube;
Renderable* Cube2;
Shader* shader;
char* code = new char[254];
const std::string vertexShaderSource = "build/SandboxApplication/Debug/test.vs";
const std::string fragmentShaderSource = "build/SandboxApplication/Debug/test.fs";
/*
* Runs once at startup
* - USe to initialize the game/sandbox/demo
*/
void Start() {
/*
Building a very basic scene graph
*/
SceneNode MyCube = SceneNode();
MyCube.name = "MyCube";
SceneNode MyBaby = SceneNode();
MyBaby.name = "Baby";
SceneNode MySecondCube = SceneNode();
MySecondCube.name = "MySecondCube";
MyCube.addChild(MyBaby);
Scene scene = Scene("My awesome Game Scene");
scene.GetRoot().addChild(MyCube);
scene.GetRoot().addChild(MySecondCube);
// Walk scene graph
PrintSceneTree(scene.GetRoot(),0);
shader = new Shader(vertexShaderSource, fragmentShaderSource);
/*
* load meshes
*/
Cube = Renderable::Load();
Cube2 = Renderable::Load();
Cube->addChild(*Cube2);
cam = new Camera(glm::vec3(0.0f, 1.5f, -10.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90.0f);
memset(code, '\0', 254);
}
/*
* Runs every frame
* - Use to draw Immediate mode graphics (Not meant for HUD's )
*/
void ImmediateGraphicsDraw() {
ImGui::NewFrame();
// Show ImGui demo such that I can easily look
// at possible GUI elements to use
ImGui::ShowDemoWindow();
// Show internal BarinkEngine stats
ShowStats();
// Show different tooling for this specific sandbox
CameraTool(cam);
ScriptingTool(code);
transformWindow(Cube->transform, "Transform (Cube)");
transformWindow(Cube2->transform, "Transform (Cube2)");
}
/*
* Runs every frame
* - Meant for game logic ( non-physics related)
*/
void Update()
{
/*
* NOTE: this needs to move to the renderer
* Render code should not appear in the sandbox file
*/
glm::mat4 projection = glm::perspective(glm::radians(cam->Zoom), (800.0f / 600.0f), 0.001f, 100.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
shader->Use();
shader->setUniformMat4("P", projection);
shader->setUniformMat4("M", CalculateModelMat(Cube->transform));
shader->setUniformMat4("V", cam->GetViewMatrix());
shader->setUniformVec3("MatColour", glm::vec3(1.0f, 0.0f, 0.0f));
Cube->Draw();
shader->setUniformMat4("M", CalculateModelMat(Cube2->transform));
shader->setUniformVec3("MatColour", glm::vec3(0.0f, 1.0f, 0.0f));
Cube2->Draw();
}
/*
* Runs at the end of the program
* - Meant for cleanup
*/
void Stop() {
// Cleanup
Cube->VAO.Delete();
Cube->elementBuffer.Delete();
Cube2->VAO.Delete();
Cube2->elementBuffer.Delete();
delete Cube2;
delete Cube;
delete shader;
}