Clean up of sandbox
* Added a statistics panel * Added comments to `Sandbox.cpp` * Moved utility functions and GUI code out of sandbox.cpp
This commit is contained in:
parent
8cc6ed1f15
commit
02727c74bb
@ -1,5 +1,7 @@
|
||||
#include "BarinkEngine.h"
|
||||
#include <imgui.h>
|
||||
#include <chrono>
|
||||
|
||||
|
||||
extern void Start();
|
||||
extern void Update();
|
||||
@ -9,7 +11,20 @@ extern void Stop();
|
||||
using namespace BarinkEngine;
|
||||
|
||||
|
||||
struct EngineStatistics{
|
||||
unsigned int lastSampleTime;
|
||||
unsigned int frames;
|
||||
float frameTime;
|
||||
unsigned int FPS;
|
||||
};
|
||||
|
||||
EngineStatistics* ES;
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
ES = new EngineStatistics();
|
||||
ES->frames = 0;
|
||||
ES->lastSampleTime = 0;
|
||||
|
||||
// Startup services
|
||||
BarinkWindow MainWindow = BarinkWindow(800, 600);
|
||||
@ -24,8 +39,27 @@ int main(int argc, char* argv[]) {
|
||||
// First call to setup game
|
||||
Start();
|
||||
|
||||
|
||||
ES->lastSampleTime = std::chrono::duration_cast<std::chrono::milliseconds> (std::chrono::system_clock::now().time_since_epoch()).count();
|
||||
|
||||
// Runtime loop
|
||||
while (!MainWindow.WindowShouldClose()) {
|
||||
|
||||
ES->frames++;
|
||||
|
||||
unsigned int now = std::chrono::duration_cast<std::chrono::milliseconds> (std::chrono::system_clock::now().time_since_epoch()).count();
|
||||
unsigned int MilliSecondsPast = ES->lastSampleTime - now;
|
||||
|
||||
if ( MilliSecondsPast >= 1000) {
|
||||
ES->FPS = 1000 / ES->frames ;
|
||||
ES->frameTime = ES->frames / 1000;
|
||||
ES->frames = 0;
|
||||
ES->lastSampleTime = now;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
InputSystem.PollEvents();
|
||||
|
||||
Update();
|
||||
@ -36,6 +70,8 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
GUISystem.Render();
|
||||
|
||||
|
||||
|
||||
MainWindow.SwapBuffers();
|
||||
}
|
||||
|
||||
@ -45,9 +81,18 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
|
||||
// Shutdown Services
|
||||
|
||||
delete ES;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void ShowStats() {
|
||||
ImGui::Begin("Statistics", false, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove );
|
||||
|
||||
ImGui::Text("Frames: %i", ES->frames);
|
||||
ImGui::Text("FPS: %i", ES->FPS);
|
||||
ImGui::Text("Frame Time: %f", ES->frameTime);
|
||||
ImGui::End();
|
||||
|
||||
}
|
@ -16,6 +16,6 @@
|
||||
#include "Graphics/GUI/GUIManager.h"
|
||||
#include "Scene.h"
|
||||
|
||||
void WARN(std::string message);
|
||||
|
||||
|
||||
void ShowStats();
|
@ -2,6 +2,8 @@
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform vec3 MatColour;
|
||||
|
||||
void main(){
|
||||
FragColor = vec4(0.5f, 0.5f, 0.0f , 1.0f);
|
||||
FragColor = vec4(MatColour, 1.0f);
|
||||
}
|
31
SandboxApplication/GUI.cpp
Normal file
31
SandboxApplication/GUI.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
#include "GUI.h"
|
||||
|
||||
void CameraTool(Camera* cam) {
|
||||
|
||||
ImGui::Begin("Camera");
|
||||
|
||||
ImGui::SliderFloat("Zoom:", &cam->Zoom, 10, 190);
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
void ScriptingTool(char* code) {
|
||||
ImGui::Begin("Scripting");
|
||||
|
||||
ImGui::InputTextMultiline("Lua Script", code, 255);
|
||||
bool runCode = ImGui::Button("Run");
|
||||
|
||||
|
||||
ImGui::End();
|
||||
|
||||
}
|
||||
|
||||
|
||||
void transformWindow(Transform& transform, std::string PanelName) {
|
||||
ImGui::Begin(PanelName.c_str());
|
||||
ImGui::InputFloat3("Position:", (float*)&transform.Position[0]);
|
||||
ImGui::InputFloat3("Rotation:", (float*)&transform.Rotation[0]);
|
||||
ImGui::InputFloat3("Scale:", (float*)&transform.Scale[0]);
|
||||
ImGui::End();
|
||||
|
||||
}
|
7
SandboxApplication/GUI.h
Normal file
7
SandboxApplication/GUI.h
Normal file
@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
#include "imgui.h"
|
||||
#include <BarinkEngine.h>
|
||||
|
||||
void CameraTool(Camera* camera);
|
||||
void ScriptingTool(char* code);
|
||||
void transformWindow(Transform& transform, std::string PanelName);
|
@ -1,32 +1,31 @@
|
||||
#include "BarinkEngine.h"
|
||||
#include "imgui.h"
|
||||
using namespace BarinkEngine;
|
||||
#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";
|
||||
|
||||
void PrintSceneTree(SceneNode& node, int depth ){
|
||||
// Indent name based on depth
|
||||
std::cout << " ";
|
||||
for (int i = 0; i < depth; i++) {
|
||||
std::cout << "-";
|
||||
}
|
||||
|
||||
std::cout << " " << node.name << std::endl;
|
||||
|
||||
depth++;
|
||||
for (auto child : node.children) {
|
||||
PrintSceneTree(*child, depth);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 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";
|
||||
|
||||
@ -48,6 +47,13 @@ void Start() {
|
||||
// Walk scene graph
|
||||
PrintSceneTree(scene.GetRoot(),0);
|
||||
|
||||
|
||||
shader = new Shader(vertexShaderSource, fragmentShaderSource);
|
||||
|
||||
|
||||
/*
|
||||
* load meshes
|
||||
*/
|
||||
Cube = Renderable::Load();
|
||||
Cube2 = Renderable::Load();
|
||||
Cube->addChild(*Cube2);
|
||||
@ -56,96 +62,72 @@ void Start() {
|
||||
|
||||
memset(code, '\0', 254);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void transformWindow(Transform& transform, std::string PanelName) {
|
||||
ImGui::Begin(PanelName.c_str());
|
||||
ImGui::InputFloat3("Position:", (float*)&transform.Position[0]);
|
||||
ImGui::InputFloat3("Rotation:", (float*)&transform.Rotation[0]);
|
||||
ImGui::InputFloat3("Scale:", (float*)&transform.Scale[0]);
|
||||
ImGui::End();
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* 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();
|
||||
|
||||
ImGui::Begin("Camera");
|
||||
|
||||
ImGui::SliderFloat("Zoom:", &cam->Zoom, 10, 190);
|
||||
// Show internal BarinkEngine stats
|
||||
ShowStats();
|
||||
|
||||
ImGui::End();
|
||||
|
||||
// Show different tooling for this specific sandbox
|
||||
CameraTool(cam);
|
||||
ScriptingTool(code);
|
||||
|
||||
transformWindow(Cube->transform, "Transform (Cube)");
|
||||
|
||||
transformWindow(Cube2->transform, "Transform (Cube2)");
|
||||
|
||||
|
||||
ImGui::Begin("Scripting!!");
|
||||
|
||||
ImGui::InputTextMultiline("Lua Script", code, 255);
|
||||
bool runCode = ImGui::Button("Run");
|
||||
|
||||
|
||||
ImGui::End();
|
||||
|
||||
|
||||
ImGui::ShowDemoWindow();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
glm::mat4 CalculateModelMat(Transform& transform) {
|
||||
|
||||
glm::mat4 tran = glm::translate(glm::mat4(), transform.Position);
|
||||
glm::mat4 scale = glm::scale(glm::mat4(), transform.Scale);
|
||||
glm::mat4 rot =
|
||||
glm::rotate(glm::mat4(), glm::radians(transform.Rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)) *
|
||||
glm::rotate(glm::mat4(), glm::radians(transform.Rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)) *
|
||||
glm::rotate(glm::mat4(), glm::radians(transform.Rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
|
||||
|
||||
|
||||
return tran * rot * scale;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Runs every frame
|
||||
* - Meant for game logic ( non-physics related)
|
||||
*/
|
||||
void Update()
|
||||
{
|
||||
|
||||
std::string vertexShaderSource = "build/SandboxApplication/Debug/test.vs";
|
||||
std::string fragmentShaderSource = "build/SandboxApplication/Debug/test.fs";
|
||||
Shader shader = Shader(vertexShaderSource, fragmentShaderSource);
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* 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));
|
||||
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));
|
||||
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();
|
||||
@ -156,4 +138,6 @@ void Stop() {
|
||||
|
||||
delete Cube2;
|
||||
delete Cube;
|
||||
|
||||
delete shader;
|
||||
}
|
32
SandboxApplication/Util.cpp
Normal file
32
SandboxApplication/Util.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
#include "Util.h"
|
||||
|
||||
void PrintSceneTree(SceneNode& node, int depth) {
|
||||
// Indent name based on depth
|
||||
std::cout << " ";
|
||||
for (int i = 0; i < depth; i++) {
|
||||
std::cout << "-";
|
||||
}
|
||||
|
||||
std::cout << " " << node.name << std::endl;
|
||||
|
||||
depth++;
|
||||
for (auto child : node.children) {
|
||||
PrintSceneTree(*child, depth);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
glm::mat4 CalculateModelMat(Transform& transform) {
|
||||
|
||||
glm::mat4 tran = glm::translate(glm::mat4(), transform.Position);
|
||||
glm::mat4 scale = glm::scale(glm::mat4(), transform.Scale);
|
||||
glm::mat4 rot =
|
||||
glm::rotate(glm::mat4(), glm::radians(transform.Rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)) *
|
||||
glm::rotate(glm::mat4(), glm::radians(transform.Rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)) *
|
||||
glm::rotate(glm::mat4(), glm::radians(transform.Rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
|
||||
|
||||
|
||||
return tran * rot * scale;
|
||||
}
|
||||
|
6
SandboxApplication/Util.h
Normal file
6
SandboxApplication/Util.h
Normal file
@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
#include "BarinkEngine.h"
|
||||
|
||||
void PrintSceneTree(SceneNode& node, int depth);
|
||||
|
||||
glm::mat4 CalculateModelMat(Transform& transform);
|
Loading…
Reference in New Issue
Block a user