diff --git a/NothinFancy/NothinFancy.vcxproj b/NothinFancy/NothinFancy.vcxproj index 895c60e..6a553ae 100644 --- a/NothinFancy/NothinFancy.vcxproj +++ b/NothinFancy/NothinFancy.vcxproj @@ -94,7 +94,7 @@ Level3 true - NFENGINE;GLEW_STATIC;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + GLEW_STATIC;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true $(ProjectDir)src\include\;$(ProjectDir)dep\include\ $(IntDir)obj\ @@ -119,7 +119,7 @@ true true true - NFENGINE;GLEW_STATIC;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + GLEW_STATIC;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true $(ProjectDir)src\include\;$(ProjectDir)dep\include\ $(IntDir)obj\ @@ -144,7 +144,7 @@ Level3 true - NFENGINE;GLEW_STATIC;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + GLEW_STATIC;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true $(ProjectDir)src\include\;$(ProjectDir)dep\include\ $(IntDir)obj\ @@ -169,7 +169,7 @@ true true true - NFENGINE;GLEW_STATIC;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + GLEW_STATIC;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true $(ProjectDir)src\include\;$(ProjectDir)dep\include\ $(IntDir)obj\ diff --git a/NothinFancy/src/Application.cpp b/NothinFancy/src/Application.cpp index 893a715..07a461e 100644 --- a/NothinFancy/src/Application.cpp +++ b/NothinFancy/src/Application.cpp @@ -1,8 +1,10 @@ #include "Application.h" -#ifdef NFENGINE + +#include #include "GL\glew.h" #include "GL\wglew.h" -#endif + +#include "Utility.h" namespace nf { DEBUGINIT; @@ -41,7 +43,7 @@ namespace nf { m_states[stateName] = state; } else { - Error(("State \"" + (std::string)stateName + (std::string)"\" already exists!").c_str()); + Error("State \"" + (std::string)stateName + (std::string)"\" already exists!"); } } @@ -52,7 +54,7 @@ namespace nf { m_defaultStateAdded = true; } else { - Error(("State \"" + (std::string)stateName + (std::string)"\" doesn't exist!").c_str()); + Error("State \"" + (std::string)stateName + (std::string)"\" doesn't exist!"); } } else { @@ -93,7 +95,7 @@ namespace nf { m_currentState->onEnter(this); } else { - Error(("State \"" + (std::string)stateName + (std::string)"\" doesn't exist!").c_str()); + Error("State \"" + (std::string)stateName + (std::string)"\" doesn't exist!"); } } @@ -157,6 +159,7 @@ namespace nf { m_frames = 0; Log("FPS: " + std::to_string(m_FPS)); m_fpsClock1 = std::chrono::steady_clock::now(); + //TODO: Rework calculating FPS } std::this_thread::sleep_until(next_time); m_deltaTime = (double)(std::chrono::steady_clock::now() - start_time).count(); diff --git a/NothinFancy/src/IntroGamestate.cpp b/NothinFancy/src/IntroGamestate.cpp index 048f943..fd30787 100644 --- a/NothinFancy/src/IntroGamestate.cpp +++ b/NothinFancy/src/IntroGamestate.cpp @@ -1,5 +1,7 @@ #include "IntroGamestate.h" + #include "Application.h" +#include "Utility.h" namespace nf { void IntroGamestate::onEnter(Application* app) { diff --git a/NothinFancy/src/Renderer/IndexBuffer.cpp b/NothinFancy/src/Renderer/IndexBuffer.cpp index 6382133..589132e 100644 --- a/NothinFancy/src/Renderer/IndexBuffer.cpp +++ b/NothinFancy/src/Renderer/IndexBuffer.cpp @@ -1,5 +1,7 @@ #include "IndexBuffer.h" +#include "GL/glew.h" + namespace nf { IndexBuffer::IndexBuffer(const void* data, size_t count) { m_count = count; diff --git a/NothinFancy/src/Renderer/Renderer.cpp b/NothinFancy/src/Renderer/Renderer.cpp index db20de2..fa22db1 100644 --- a/NothinFancy/src/Renderer/Renderer.cpp +++ b/NothinFancy/src/Renderer/Renderer.cpp @@ -1,6 +1,10 @@ #include "Renderer.h" +#include "GL/glew.h" +#include "GL\wglew.h" + #include "Application.h" +#include "Utility.h" namespace nf { Renderer::Renderer(Application* app) { @@ -54,7 +58,7 @@ namespace nf { GLenum err = glGetError(); if (err != GL_NO_ERROR) { - Error(("OpenGL error " + std::to_string(err)).c_str()); + Error("OpenGL error " + std::to_string(err)); } } diff --git a/NothinFancy/src/Renderer/Shader.cpp b/NothinFancy/src/Renderer/Shader.cpp index 0f1ec8b..4519e5e 100644 --- a/NothinFancy/src/Renderer/Shader.cpp +++ b/NothinFancy/src/Renderer/Shader.cpp @@ -1,5 +1,9 @@ #include "Shader.h" +#include "GL/glew.h" + +#include "Utility.h" + namespace nf { Shader::Shader(const char* vertexSource, const char* fragmentSource) { m_id = glCreateProgram(); @@ -19,7 +23,7 @@ namespace nf { char* message = new char[length]; glGetShaderInfoLog(curr, length, &length, message); message[length - 2] = 0; - Error(("OpenGL Error: " + (std::string)message).c_str()); + Error("OpenGL Error: " + (std::string)message); } } glAttachShader(m_id, vs); @@ -33,7 +37,7 @@ namespace nf { glGetProgramiv(m_id, GL_INFO_LOG_LENGTH, &length); char* message = new char[length]; glGetProgramInfoLog(m_id, length, &length, message); - Error(("OpenGL Error: " + (std::string)message).c_str()); + Error("OpenGL Error: " + (std::string)message); } glDeleteShader(vs); glDeleteShader(fs); @@ -42,6 +46,14 @@ namespace nf { void Shader::bind() { glUseProgram(m_id); } + //TODO: Create overloaded setUniform function + void Shader::getUniformLocation(const char* uniformName) { + unsigned int loc = glGetUniformLocation(m_id, uniformName); + if (loc == -1) { + Error("Uniform \"" + (std::string)uniformName + "\" does not exist!"); + } + m_uniformLocations[uniformName] = loc; + } Shader::~Shader() { glDeleteProgram(m_id); diff --git a/NothinFancy/src/Renderer/VertexArray.cpp b/NothinFancy/src/Renderer/VertexArray.cpp index 917121a..9ee84e8 100644 --- a/NothinFancy/src/Renderer/VertexArray.cpp +++ b/NothinFancy/src/Renderer/VertexArray.cpp @@ -1,50 +1,63 @@ #include "VertexArray.h" +#include "GL/glew.h" + +#include "Utility.h" + namespace nf { - unsigned int VertexBufferElement::getSizeOfType(unsigned int type) { - switch (type) - { - case GL_FLOAT: - return sizeof(type); - default: - return 0; - } - } - - VertexArray::VertexArray(const void* bufferData, size_t bufferSize) : - m_id(0), - m_vb(bufferData, bufferSize), - m_hasLayout(false), - m_vertexStride(0) - { + VertexArray::VertexArray() { glGenVertexArrays(1, &m_id); + glBindVertexArray(m_id); + m_lastBufferHasLayout = true; + m_attribute = 0; + m_lastStride = 0; } - void VertexArray::bind() { - glBindVertexArray(m_id); - m_vb.bind(); - if (!m_hasLayout && m_vertexStride > 0) { - unsigned int offset = 0; - for (unsigned int i = 0; i < m_layoutElements.size(); i++) { - const VertexBufferElement& element = m_layoutElements[i]; - glEnableVertexAttribArray(i); - glVertexAttribPointer(i, element.count, element.type, element.normalized, m_vertexStride, (const void*)offset); - offset += element.count * VertexBufferElement::getSizeOfType(element.type); - } - m_hasLayout = true; - } - else if(!m_hasLayout) { - Error("No layout specified for vertex buffer!"); + void VertexArray::addBuffer(const void* data, const size_t size) { + if (!m_lastBufferHasLayout) { + Error("Buffer added to vertex array has no layout!"); } + m_buffers.push_back(new VertexBuffer(data, size)); + m_buffers.back()->bind(); + m_lastBufferHasLayout = false; + m_lastStride = 0; } template<> void VertexArray::push(unsigned int count) { - m_layoutElements.push_back({ GL_FLOAT, count, GL_FALSE }); - m_vertexStride += VertexBufferElement::getSizeOfType(GL_FLOAT) * count; + if (m_lastBufferHasLayout) { + Error("Tried to modify a vertex array's buffer after the layout was final!"); + } + m_lastBufferLayout.push_back({ GL_FLOAT, count, GL_FALSE }); + m_lastStride += count * sizeof(GL_FLOAT); + } + + void VertexArray::finishBufferLayout() { + unsigned int offset = 0; + for (; m_attribute < m_lastBufferLayout.size(); m_attribute++) { + const VertexBufferElement& curr = m_lastBufferLayout[m_attribute]; + glEnableVertexAttribArray(m_attribute); + glVertexAttribPointer(m_attribute, curr.count, curr.type, curr.normalized, m_lastStride, (const void*)offset); + offset += sizeof(curr.type) * curr.count; + } + m_lastBufferHasLayout = true; + m_lastStride = 0; + } + + void VertexArray::bind(unsigned int buffer) { + if (m_buffers.empty()) { + Error("No buffers and layouts added to vertex array before being bound!"); + } + if (!m_lastBufferHasLayout) { + Error("Buffer added to vertex array has no layout!"); + } + glBindVertexArray(m_id); } VertexArray::~VertexArray() { + for (VertexBuffer* curr : m_buffers) { + delete curr; + } glDeleteVertexArrays(1, &m_id); } } \ No newline at end of file diff --git a/NothinFancy/src/Renderer/VertexBuffer.cpp b/NothinFancy/src/Renderer/VertexBuffer.cpp index b4ae9ac..e3a8148 100644 --- a/NothinFancy/src/Renderer/VertexBuffer.cpp +++ b/NothinFancy/src/Renderer/VertexBuffer.cpp @@ -1,5 +1,9 @@ #include "VertexBuffer.h" +#include "GL/glew.h" + +#include "Utility.h" + namespace nf { VertexBuffer::VertexBuffer(const void* data, const size_t size) { glGenBuffers(1, &m_id); diff --git a/NothinFancy/src/Utility.cpp b/NothinFancy/src/Utility.cpp index 62ca2b8..2a42ef1 100644 --- a/NothinFancy/src/Utility.cpp +++ b/NothinFancy/src/Utility.cpp @@ -1,6 +1,10 @@ +#include "Utility.h" +#include +#include +#include +#include #include -#include "Utility.h" #include "Config.h" namespace nf { @@ -35,6 +39,16 @@ namespace nf { CloseHandle(cmd); } + void Debug::ErrorImp(const std::string& in, const char* filename, int line) { + std::chrono::duration time = getCurrentTime(); + HANDLE cmd = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleTextAttribute(cmd, FOREGROUND_RED); + std::printf("[%.4f] Error (%s, %i): ", time.count(), filename, line); + std::cout << in << "\n"; + SetConsoleTextAttribute(cmd, 7); + CloseHandle(cmd); + } + std::chrono::duration Debug::getCurrentTime() { std::chrono::steady_clock::time_point now = std::chrono::high_resolution_clock::now(); return now - m_initTime; @@ -47,6 +61,13 @@ namespace nf { MultiByteToWideChar(CP_ACP, NULL, in, -1, out, length); return out; } + const wchar_t* toWide(const std::string& in) { + const char* cstr = in.c_str(); + int length = std::strlen(cstr) + 1; + wchar_t* out = new wchar_t[length]; + MultiByteToWideChar(CP_ACP, NULL, cstr, -1, out, length); + return out; + } //TODO: File encryption bool writeFile(const char* filename, const std::string& in) { std::string file(filename); @@ -65,7 +86,7 @@ namespace nf { std::ofstream out; out.open(filename); if (!out) { - Error(("File \"" + (std::string)filename + (std::string)"\" could not be written!").c_str()); + Error("File \"" + (std::string)filename + (std::string)"\" could not be written!"); return false; } out << in; @@ -77,7 +98,7 @@ namespace nf { std::ifstream in; in.open(filename); if (!in) { - Error(("File \"" + (std::string)filename + (std::string)"\" could not be read!").c_str()); + Error("File \"" + (std::string)filename + (std::string)"\" could not be read!"); return NULL; } std::stringstream ss; diff --git a/NothinFancy/src/include/Application.h b/NothinFancy/src/include/Application.h index 51fa9b4..939a262 100644 --- a/NothinFancy/src/include/Application.h +++ b/NothinFancy/src/include/Application.h @@ -1,15 +1,11 @@ #pragma once -#include #include #include -#include +#include #include "Config.h" -#include "Utility.h" #include "IntroGamestate.h" #include "Renderer.h" -//TODO: Check #include consistency -//TODO: Separate #includes between headers and implementations //TODO: Document ALL frontend functions namespace nf { diff --git a/NothinFancy/src/include/IndexBuffer.h b/NothinFancy/src/include/IndexBuffer.h index 06ebbc2..0d53666 100644 --- a/NothinFancy/src/include/IndexBuffer.h +++ b/NothinFancy/src/include/IndexBuffer.h @@ -1,5 +1,4 @@ #pragma once -#include "GL/glew.h" namespace nf { class IndexBuffer { diff --git a/NothinFancy/src/include/IntroGamestate.h b/NothinFancy/src/include/IntroGamestate.h index 808ff62..1367f9b 100644 --- a/NothinFancy/src/include/IntroGamestate.h +++ b/NothinFancy/src/include/IntroGamestate.h @@ -1,6 +1,5 @@ #pragma once #include "IGamestate.h" -#include "Utility.h" namespace nf { class IntroGamestate : public IGamestate { @@ -12,5 +11,6 @@ namespace nf { void render() override; private: int counter; + //TODO: Flesh out intro gamestate }; } \ No newline at end of file diff --git a/NothinFancy/src/include/NothinFancy.h b/NothinFancy/src/include/NothinFancy.h index 45b68f1..c782751 100644 --- a/NothinFancy/src/include/NothinFancy.h +++ b/NothinFancy/src/include/NothinFancy.h @@ -1,6 +1,7 @@ -//Master engine include (Is this even useful?) +//TODO: Rework this file to only contain functions the frontend will need to access #include "Application.h" #include "Input.h" +#include "Utility.h" using namespace nf; \ No newline at end of file diff --git a/NothinFancy/src/include/Renderer.h b/NothinFancy/src/include/Renderer.h index 65a39aa..b4d25ac 100644 --- a/NothinFancy/src/include/Renderer.h +++ b/NothinFancy/src/include/Renderer.h @@ -1,8 +1,5 @@ #pragma once -#ifdef NFENGINE -#include "GL/glew.h" -#include "GL\wglew.h" -#endif +#include namespace nf { class Application; diff --git a/NothinFancy/src/include/Shader.h b/NothinFancy/src/include/Shader.h index c715962..e436c0b 100644 --- a/NothinFancy/src/include/Shader.h +++ b/NothinFancy/src/include/Shader.h @@ -1,9 +1,5 @@ #pragma once -#ifdef NFENGINE -#include "GL/glew.h" -#endif - -#include "Utility.h" +#include namespace nf { class Shader { @@ -11,10 +7,12 @@ namespace nf { Shader(const char* vertexSource, const char* fragmentSource); void bind(); + void getUniformLocation(const char* uniformName); ~Shader(); private: unsigned int m_id; - //Associated resource? + std::unordered_map m_uniformLocations; + //TODO: Load from resource }; } \ No newline at end of file diff --git a/NothinFancy/src/include/Utility.h b/NothinFancy/src/include/Utility.h index 9140ecc..f60715b 100644 --- a/NothinFancy/src/include/Utility.h +++ b/NothinFancy/src/include/Utility.h @@ -1,11 +1,6 @@ #pragma once #include -#include -#include #include -#include -#include -#include namespace nf { #ifdef _DEBUG @@ -33,6 +28,7 @@ __debugbreak(); static void LogImp(int in); static void LogImp(double in); static void ErrorImp(const char* in, const char* filename, int line); + static void ErrorImp(const std::string& in, const char* filename, int line); }; #else #define DEBUGINIT @@ -42,6 +38,7 @@ std::exit(-1) #endif const wchar_t* toWide(const char* in); + const wchar_t* toWide(const std::string& in); bool writeFile(const char* filename, const std::string& in); std::string readFile(const char* filename); } \ No newline at end of file diff --git a/NothinFancy/src/include/VertexArray.h b/NothinFancy/src/include/VertexArray.h index 66ca245..c4b1f21 100644 --- a/NothinFancy/src/include/VertexArray.h +++ b/NothinFancy/src/include/VertexArray.h @@ -1,35 +1,32 @@ #pragma once -#ifdef NFENGINE -#include "GL/glew.h" -#endif #include #include "VertexBuffer.h" -#include "Utility.h" namespace nf { struct VertexBufferElement { unsigned int type; unsigned int count; unsigned char normalized; - - static unsigned int getSizeOfType(unsigned int type); }; class VertexArray { public: - VertexArray(const void* bufferData, size_t bufferSize); + VertexArray(); - void bind(); + void addBuffer(const void* data, const size_t size); template void push(unsigned int count); + void finishBufferLayout(); + void bind(unsigned int buffer = 1); ~VertexArray(); private: unsigned int m_id; - VertexBuffer m_vb; - bool m_hasLayout; - std::vector m_layoutElements; - unsigned int m_vertexStride; + bool m_lastBufferHasLayout; + std::vector m_buffers; + std::vector m_lastBufferLayout; + unsigned int m_attribute; + unsigned int m_lastStride; }; } \ No newline at end of file diff --git a/NothinFancy/src/include/VertexBuffer.h b/NothinFancy/src/include/VertexBuffer.h index 55801c3..0320116 100644 --- a/NothinFancy/src/include/VertexBuffer.h +++ b/NothinFancy/src/include/VertexBuffer.h @@ -1,7 +1,4 @@ #pragma once -#ifdef NFENGINE -#include "GL/glew.h" -#endif namespace nf { class VertexBuffer {