Skip to content

Commit

Permalink
Merge pull request #2 from GBMiro/Development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
GBMiro authored Jan 17, 2021
2 parents d91c8f1 + ba77a2c commit 665734e
Show file tree
Hide file tree
Showing 227 changed files with 23,186 additions and 1,587 deletions.
34 changes: 28 additions & 6 deletions Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "ModuleTextures.h"
#include "ModuleScene.h"
#include "Brofiler/include/Brofiler.h"
#include "ModuleFileSystem.h"
#include "Leaks.h"
#include "Timer.h"

Expand All @@ -24,7 +25,9 @@ Application::Application() {
modules.push_back(input = new ModuleInput());
modules.push_back(editor = new ModuleEditor());
modules.push_back(camera = new ModuleCamera());
modules.push_back(FS = new ModuleFileSystem());
capTimer = new Timer();
isFrameRateCapped = true;
SetFrameCap(60);
}

Expand Down Expand Up @@ -69,17 +72,18 @@ update_status Application::Update() {
for (list<Module*>::iterator it = modules.begin(); it != modules.end() && ret == UPDATE_CONTINUE; ++it)
ret = (*it)->PostUpdate();

if (GetUseFrameCap()) {
if (millisPerFrame > (Uint32)0) {
Uint32 frameMillis = capTimer->Read();

if (millisPerFrame > (Uint32)0) {
Uint32 frameMillis = capTimer->Read();

if (frameMillis < millisPerFrame) {
SDL_Delay(millisPerFrame - frameMillis);
if (frameMillis < millisPerFrame) {
SDL_Delay(millisPerFrame - frameMillis);
}
}
}

lastDeltaTime = capTimer->Read() / 1000;
editor->registerFPS(lastDeltaTime);
editor->RegisterFPS(lastDeltaTime);

return ret;
}
Expand Down Expand Up @@ -108,3 +112,21 @@ bool Application::CleanUp() {

return ret;
}

bool Application::GetUseFrameCap()const {
return isFrameRateCapped;
}

void Application::SetUseFrameCap(bool should) {
isFrameRateCapped = should;
}

bool Application::GetUseVSync()const {
return SDL_GL_GetSwapInterval() == 1;
}

void Application::SetUseVSync(bool should) {
SDL_GL_SetSwapInterval(should ? 1 : 0);
}


7 changes: 7 additions & 0 deletions Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ModuleEditor;
class ModuleDebugDraw;
class ModuleTextures;
class ModuleScene;
class ModuleFileSystem;
class Model;
class Timer;

Expand All @@ -36,13 +37,19 @@ class Application {
ModuleDebugDraw* debugDraw = nullptr;
ModuleTextures* textures = nullptr;
ModuleScene* scene = nullptr;
ModuleFileSystem* FS = nullptr;

float GetDeltaTime();
void SetFrameCap(int frameCap);
const int& GetFrameCap()const;

bool GetUseFrameCap()const;
void SetUseFrameCap(bool should);
bool GetUseVSync()const;
void SetUseVSync(bool should);

private:
bool isFrameRateCapped, useVSync;
unsigned int frameCap, millisPerFrame;
float lastDeltaTime;
std::list<Module*> modules;
Expand Down
24 changes: 22 additions & 2 deletions Component.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "Component.h"

Component::Component(ComponentType type, GameObject* parent) : type(type), owner(parent) {
#include "GameObject.h"
#include "MathGeoLib/Algorithm/Random/LCG.h"
Component::Component(ComponentType type, GameObject* parent) : type(type), owner(parent), enabled(true) {
UUID = LCG().Int();
}

Component::~Component() {
Expand All @@ -9,3 +11,21 @@ Component::~Component() {
ComponentType Component::GetType() const {
return type;
}

void Component::Enable() {
enabled = true;
OnEnable();
}

void Component::Disable() {
enabled = false;
OnDisable();
}


bool Component::Enabled()const {
if (owner->Active()) {
return enabled;
}
return false;
}
20 changes: 17 additions & 3 deletions Component.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#pragma once
#include <stdint.h>
#include "rapidjson/document.h"

enum ComponentType {
CTTransform,
Expand All @@ -19,15 +21,27 @@ class Component {
virtual void Update() = 0;
virtual void Disable() = 0;

virtual void DrawOnEditor() {};
virtual void OnNewParent(GameObject* oldParent, GameObject* newParent) {};
virtual void OnDisable() = 0;
virtual void OnEnable() = 0;


virtual void DrawOnEditor() = 0;
virtual void OnNewParent(const GameObject* oldParent, const GameObject* newParent) {};
virtual void OnTransformChanged() {};
virtual void DrawGizmos() {};
ComponentType GetType() const;

uint32_t GetUUID() const { return UUID; }

virtual void WriteToJSON(rapidjson::Value& component, rapidjson::Document::AllocatorType& alloc) {}
virtual void LoadFromJSON(const rapidjson::Value& component) {}

public:
GameObject* owner;
bool enabled = true;
bool Enabled()const;
private:
ComponentType type;
bool active = false;
uint32_t UUID;
};

114 changes: 114 additions & 0 deletions ComponentCamera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include "ComponentCamera.h"
#include "imgui.h"
#include "Leaks.h"
#include "Application.h"
#include "ModuleDebugDraw.h"
#include "ModuleWindow.h"
#include "GameObject.h"
#include "ComponentTransform.h"
#include "ModuleCamera.h"
#include "ModuleRender.h"

void ComponentCamera::Enable() {

}

void ComponentCamera::Update() {

}

void ComponentCamera::Disable() {

}

void ComponentCamera::OnEnable() {

}
void ComponentCamera::OnDisable() {

}

void ComponentCamera::DrawOnEditor() {
if (ImGui::CollapsingHeader("Camera")) {
bool thisCameraCulls = App->renderer->GetCullingCamera() == this;
bool frustumCulling = App->renderer->GetFrustumCulling() && thisCameraCulls;

if (ImGui::Checkbox("Frustum Culling", &frustumCulling)) {
App->renderer->SetCullingCamera(frustumCulling ? this : nullptr);
}

}
}

void ComponentCamera::OnNewParent(const GameObject* prevParent, const GameObject* newParent) {

}

Frustum& ComponentCamera::GetFrustum() {
return frustum;
}

ComponentCamera::~ComponentCamera() {

if (App->renderer != nullptr) {
if (App->renderer->GetCullingCamera() == this) {
App->renderer->SetCullingCamera(nullptr);
}
}
}
//
void ComponentCamera::SetUpFrustum(float nearDistance, float farDistance) {
ComponentTransform* transform = (ComponentTransform*)owner->GetComponentOfType(ComponentType::CTTransform);
frustum.SetKind(FrustumSpaceGL, FrustumRightHanded);
frustum.SetViewPlaneDistances(nearDistance, farDistance);
frustum.SetHorizontalFovAndAspectRatio(DegToRad(90), 1.77f);
frustum.SetPos(transform->LocalPosition());
transform->SetLocalRotation(Quat::identity);
frustum.SetFront(float3::unitZ);
frustum.SetUp(float3::unitY);
}

void ComponentCamera::WriteToJSON(rapidjson::Value& component, rapidjson::Document::AllocatorType& alloc) {
component.AddMember("Component Type", GetType(), alloc);
component.AddMember("UUID", GetUUID(), alloc);
component.AddMember("ParentUUID", owner->GetUUID(), alloc);
component.AddMember("Enabled", enabled, alloc);
component.AddMember("Horizontal FOV", RadToDeg(frustum.HorizontalFov()), alloc);
component.AddMember("Aspect Ratio", frustum.AspectRatio(), alloc);
component.AddMember("Near Distance", frustum.NearPlaneDistance(), alloc);
component.AddMember("Far Distance", frustum.FarPlaneDistance(), alloc);
}

void ComponentCamera::LoadFromJSON(const rapidjson::Value& component) {
frustum.SetKind(FrustumSpaceGL, FrustumRightHanded);
frustum.SetViewPlaneDistances(component["Near Distance"].GetFloat(), component["Far Distance"].GetFloat());
frustum.SetHorizontalFovAndAspectRatio(DegToRad(component["Horizontal FOV"].GetFloat()), component["Aspect Ratio"].GetFloat());
if (component.HasMember("Enabled")) {
enabled = component["Enabled"].GetBool();
} else {
enabled = true;
}
}


ComponentCamera::ComponentCamera(GameObject* anOwner, float aNearPDistance, float aFarPDistance) : Component(ComponentType::CTCamera, anOwner) {
SetUpFrustum(aNearPDistance, aFarPDistance);
}


void ComponentCamera::DrawGizmos() {
if (App->debugDraw != nullptr) {
App->debugDraw->DrawFrustum(frustum);
}
}

void ComponentCamera::OnTransformChanged() {

ComponentTransform* transform = (ComponentTransform*)owner->GetComponentOfType(ComponentType::CTTransform);

frustum.SetPos(transform->Position());
if (owner->parent != nullptr) {
frustum.SetFront(transform->Forward());
frustum.SetUp(transform->Up());
}
}
32 changes: 32 additions & 0 deletions ComponentCamera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef COMPONENT_CAMERA_H
#define COMPONENT_CAMERA_H
#include "Component.h"
#include "../MathGeoLib/Geometry/Frustum.h"
#include "../MathGeoLib/Math/float3x3.h"

class ComponentCamera :public Component {
private:
Frustum frustum;
public:
ComponentCamera(GameObject* anOwner, float aNearPDistance, float aFarPDistance);
~ComponentCamera();
void Enable() override;
void Update()override;
void Disable()override;
void OnEnable()override;
void OnDisable()override;
void DrawOnEditor() override;
void DrawGizmos() override;
void OnNewParent(const GameObject* prevParent, const GameObject* newParent)override;
void OnTransformChanged()override;
Frustum& GetFrustum();
void ComponentCamera::SetUpFrustum(float nearDistance, float farDistance);

void WriteToJSON(rapidjson::Value& component, rapidjson::Document::AllocatorType& alloc);
void LoadFromJSON(const rapidjson::Value& component);



};

#endif
Loading

0 comments on commit 665734e

Please sign in to comment.