Reworked gamestates to reset NF objects when exiting states; Intro state + fading is now based on delta time
This commit is contained in:
1 parent
6e3a576fef
commit
2d3b3653ff
29 files changed
+260
-108
No files matched your search
@@ -120,6 +120,6 @@ namespace nf {
|
||||
}
|
||||
|
||||
Camera::~Camera() {
|
||||
|
||||
m_app->trackMouse(false);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "GL/glew.h"
|
||||
#include "stb_image.h"
|
||||
|
||||
#include "Application.h"
|
||||
#include "Assets.h"
|
||||
|
||||
namespace nf {
|
||||
@@ -108,6 +109,8 @@ namespace nf {
|
||||
m_vao->addBuffer(vb, sizeof(vb));
|
||||
m_vao->push<float>(3);
|
||||
m_vao->finishBufferLayout();
|
||||
|
||||
Application::getApp()->getCurrentState()->m_nfObjects.push_back(this);
|
||||
}
|
||||
|
||||
bool Cubemap::isConstructed() {
|
||||
@@ -122,8 +125,15 @@ namespace nf {
|
||||
glDepthFunc(GL_LESS);
|
||||
}
|
||||
|
||||
Cubemap::~Cubemap() {
|
||||
if(m_constructed)
|
||||
void Cubemap::destroy() {
|
||||
if (m_constructed)
|
||||
glDeleteTextures(1, &m_id);
|
||||
m_constructed = false;
|
||||
m_id = 0;
|
||||
delete m_vao;
|
||||
}
|
||||
|
||||
Cubemap::~Cubemap() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#include<vector>
|
||||
|
||||
#include "Application.h"
|
||||
#include "Model.h"
|
||||
#include "Shader.h"
|
||||
|
||||
namespace nf {
|
||||
@@ -18,24 +20,28 @@ namespace nf {
|
||||
void Entity::create(Asset* modelAsset, Asset* textureAsset) {
|
||||
m_constructed = true;
|
||||
AModel& model = *(AModel*)modelAsset;
|
||||
//TODO: Change this when doing materials
|
||||
if (model.alreadyLoaded && textureAsset == nullptr) {
|
||||
m_model = model.loadedModel;
|
||||
return;
|
||||
}
|
||||
if (!textureAsset)
|
||||
Error("No texture given to Entity create function on new model load!");
|
||||
ATexture& texture = *(ATexture*)textureAsset;
|
||||
std::string obj = model.data;
|
||||
m_model = new Model;
|
||||
std::vector<float> vb;
|
||||
std::vector<unsigned int> ib;
|
||||
size_t ibCount = 0;
|
||||
std::vector<float> tc;
|
||||
std::vector<float> vn;
|
||||
parseOBJ(obj, vb, ib, ibCount, tc, vn);
|
||||
m_model->create(&vb[0], vb.size() * sizeof(float), &ib[0], ibCount, &vn[0], vn.size() * sizeof(float), &tc[0], tc.size() * sizeof(float), &texture);
|
||||
model.alreadyLoaded = true;
|
||||
model.loadedModel = m_model;
|
||||
else {
|
||||
if (!textureAsset)
|
||||
Error("No texture given to Entity create function on new model load!");
|
||||
ATexture& texture = *(ATexture*)textureAsset;
|
||||
std::string obj = model.data;
|
||||
m_model = new Model;
|
||||
std::vector<float> vb;
|
||||
std::vector<unsigned int> ib;
|
||||
size_t ibCount = 0;
|
||||
std::vector<float> tc;
|
||||
std::vector<float> vn;
|
||||
parseOBJ(obj, vb, ib, ibCount, tc, vn);
|
||||
m_model->create(&vb[0], vb.size() * sizeof(float), &ib[0], ibCount, &vn[0], vn.size() * sizeof(float), &tc[0], tc.size() * sizeof(float), &texture);
|
||||
model.alreadyLoaded = true;
|
||||
model.loadedModel = m_model;
|
||||
}
|
||||
m_model->setBaseAsset(model.isBaseAsset);
|
||||
Application::getApp()->getCurrentState()->m_nfObjects.push_back(this);
|
||||
}
|
||||
|
||||
bool Entity::isConstructed() {
|
||||
@@ -90,6 +96,22 @@ namespace nf {
|
||||
shader->setUniform("model", model);
|
||||
}
|
||||
|
||||
void Entity::destroy() {
|
||||
m_constructed = false;
|
||||
if(m_model && !m_model->isBaseAsset())
|
||||
delete m_model;
|
||||
m_model = nullptr;
|
||||
m_position.x = 0.0;
|
||||
m_position.y = 0.0;
|
||||
m_position.z = 0.0;
|
||||
m_rotation.x = 0.0;
|
||||
m_rotation.y = 0.0;
|
||||
m_rotation.z = 0.0;
|
||||
m_scale.x = 1.0;
|
||||
m_scale.y = 1.0;
|
||||
m_scale.z = 1.0;
|
||||
}
|
||||
|
||||
Entity::~Entity() {
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
#include "Light.h"
|
||||
|
||||
#include "Application.h"
|
||||
#include "Shader.h"
|
||||
|
||||
namespace nf {
|
||||
Light::Light() :
|
||||
m_type(Type::POINT),
|
||||
m_constructed(false),
|
||||
m_type(Type::POINT),
|
||||
m_strength(1.0f)
|
||||
{
|
||||
|
||||
@@ -18,6 +19,7 @@ namespace nf {
|
||||
m_type = type;
|
||||
m_strength = strength;
|
||||
|
||||
Application::getApp()->getCurrentState()->m_nfObjects.push_back(this);
|
||||
}
|
||||
|
||||
bool Light::isConstructed() {
|
||||
@@ -77,6 +79,14 @@ namespace nf {
|
||||
return m_strength;
|
||||
}
|
||||
|
||||
void Light::destroy() {
|
||||
m_constructed = false;
|
||||
m_type = Type::POINT;
|
||||
m_position = Vec3(0.0);
|
||||
m_color = Vec3(0.0);
|
||||
m_strength = 1.0f;
|
||||
}
|
||||
|
||||
Light::~Light() {
|
||||
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
namespace nf {
|
||||
Model::Model() :
|
||||
m_base(false),
|
||||
m_texture(nullptr)
|
||||
{
|
||||
|
||||
@@ -29,7 +30,7 @@ namespace nf {
|
||||
}
|
||||
else {
|
||||
m_texture = new Texture;
|
||||
m_texture->create(texture->data, texture->size);
|
||||
m_texture->create(texture);
|
||||
texture->alreadyLoaded = true;
|
||||
texture->loadedTexture = m_texture;
|
||||
}
|
||||
@@ -45,6 +46,14 @@ namespace nf {
|
||||
m_ib->bind();
|
||||
}
|
||||
|
||||
void Model::setBaseAsset(bool isBase) {
|
||||
m_base = isBase;
|
||||
}
|
||||
|
||||
bool Model::isBaseAsset() {
|
||||
return m_base;
|
||||
}
|
||||
|
||||
Model::~Model() {
|
||||
delete m_texture;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "ft2build.h"
|
||||
#include FT_FREETYPE_H
|
||||
|
||||
#include "Application.h"
|
||||
#include "UIElement.h"
|
||||
#include "Shader.h"
|
||||
|
||||
@@ -53,6 +54,7 @@ namespace nf {
|
||||
FT_Done_FreeType(ft);
|
||||
newFont.alreadyLoaded = true;
|
||||
newFont.loadedFont = m_font;
|
||||
m_font->isBase = newFont.isBaseAsset;
|
||||
}
|
||||
m_vao = new VertexArray;
|
||||
m_vao->addBuffer(nullptr, 0);
|
||||
@@ -61,6 +63,13 @@ namespace nf {
|
||||
m_vao->addBuffer(nullptr, 0);
|
||||
m_vao->push<float>(2);
|
||||
m_vao->finishBufferLayout();
|
||||
|
||||
if (m_string == "NFLoadingText") {
|
||||
m_string = "Loading...";
|
||||
}
|
||||
else {
|
||||
Application::getApp()->getCurrentState()->m_nfObjects.push_back(this);
|
||||
}
|
||||
}
|
||||
|
||||
void Text::setText(const std::string& string) {
|
||||
@@ -135,6 +144,18 @@ namespace nf {
|
||||
}
|
||||
}
|
||||
|
||||
void Text::destroy() {
|
||||
m_constructed = false;
|
||||
m_position = Vec2(0.0);
|
||||
m_centeredX = m_centeredY = false;
|
||||
m_string = std::string();
|
||||
if (!m_font->isBase)
|
||||
delete m_font;
|
||||
m_color = Vec3(0.0);
|
||||
m_scale = 1.0f;
|
||||
m_opacity = 1.0f;
|
||||
}
|
||||
|
||||
Text::~Text() {
|
||||
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace nf {
|
||||
}
|
||||
else {
|
||||
m_texture = new Texture;
|
||||
m_texture->create(tex->data, tex->size);
|
||||
m_texture->create(tex);
|
||||
}
|
||||
|
||||
m_vao = new VertexArray;
|
||||
@@ -84,6 +84,17 @@ namespace nf {
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
}
|
||||
|
||||
void UITexture::destroy() {
|
||||
m_constructed = false;
|
||||
m_position = Vec2(0.0);
|
||||
m_centeredX = m_centeredY = false;
|
||||
if (!m_texture->isBaseAsset())
|
||||
delete m_texture;
|
||||
m_scale = 1.0f;
|
||||
m_opacity = 1.0f;
|
||||
delete m_vao;
|
||||
}
|
||||
|
||||
UITexture::~UITexture() {
|
||||
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "Shader.h"
|
||||
#include "Light.h"
|
||||
#include "Entity.h"
|
||||
#include "Model.h"
|
||||
#include "Cubemap.h"
|
||||
#include "UIElement.h"
|
||||
#include "Camera.h"
|
||||
@@ -107,7 +108,7 @@ namespace nf {
|
||||
m_fadeVAO->push<float>(2);
|
||||
m_fadeVAO->finishBufferLayout();
|
||||
m_fadeIB = new IndexBuffer(fadeIB, 6);
|
||||
m_loadingText.create("Loading...", Vec2(0.025, 0.044), Vec3(0.7, 0.7, 0.7));
|
||||
m_loadingText.create("NFLoadingText", Vec2(0.025, 0.044), Vec3(0.7, 0.7, 0.7));
|
||||
}
|
||||
|
||||
void Renderer::setFade(bool in, bool out, bool noText) {
|
||||
@@ -142,7 +143,7 @@ namespace nf {
|
||||
m_cubemap = ∈
|
||||
}
|
||||
|
||||
void Renderer::doFrame(Camera* camera) {
|
||||
void Renderer::doFrame(Camera* camera, double dT) {
|
||||
//Begin frame
|
||||
glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
@@ -202,7 +203,7 @@ namespace nf {
|
||||
m_loadingText.setOpacity(opacity);
|
||||
m_loadingText.render(m_textShader, m_app->getConfig().width, m_app->getConfig().height);
|
||||
}
|
||||
opacity -= 0.03;
|
||||
opacity -= 0.8 * dT;
|
||||
if (opacity <= 0.0) {
|
||||
m_fadeIn = false;
|
||||
m_fadeOutComplete = false;
|
||||
@@ -220,7 +221,7 @@ namespace nf {
|
||||
m_loadingText.setOpacity(opacity);
|
||||
m_loadingText.render(m_textShader, m_app->getConfig().width, m_app->getConfig().height);
|
||||
}
|
||||
opacity += 0.03;
|
||||
opacity += 1.2 * dT;
|
||||
if (opacity >= 1.0) {
|
||||
m_fadeIn = false;
|
||||
m_fadeOutComplete = true;
|
||||
|
||||
@@ -4,28 +4,33 @@
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
|
||||
#include "Assets.h"
|
||||
#include "Utility.h"
|
||||
|
||||
namespace nf {
|
||||
Texture::Texture() :
|
||||
m_isBase(false),
|
||||
m_id(0),
|
||||
m_x(0),
|
||||
m_y(0)
|
||||
{
|
||||
glGenTextures(1, &m_id);
|
||||
|
||||
}
|
||||
|
||||
void Texture::create(const char* textureData, size_t textureSize) {
|
||||
void Texture::create(ATexture* tex) {
|
||||
int nChannels;
|
||||
stbi_set_flip_vertically_on_load(true);
|
||||
unsigned char* texture = stbi_load_from_memory((unsigned char*)textureData, textureSize, &m_x, &m_y, &nChannels, 0);
|
||||
unsigned char* texture = stbi_load_from_memory((unsigned char*)tex->data, tex->size, &m_x, &m_y, &nChannels, 0);
|
||||
if (!texture)
|
||||
Error("Texture failed to load from memory!");
|
||||
glGenTextures(1, &m_id);
|
||||
glBindTexture(GL_TEXTURE_2D, m_id);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_x, m_y, 0, nChannels == 3 ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, texture);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
stbi_image_free(texture);
|
||||
m_isBase = tex->isBaseAsset;
|
||||
}
|
||||
|
||||
void Texture::bind() {
|
||||
@@ -36,6 +41,10 @@ namespace nf {
|
||||
return { (float)m_x, (float)m_y };
|
||||
}
|
||||
|
||||
bool Texture::isBaseAsset() {
|
||||
return m_isBase;
|
||||
}
|
||||
|
||||
Texture::~Texture() {
|
||||
glDeleteTextures(1, &m_id);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user