From f6ffb9f87e285d87561a4e4866742bfb61e868fa Mon Sep 17 00:00:00 2001
From: "Grayson Riffe (Laptop)" <graysonriffe@yahoo.com>
Date: Fri, 20 Aug 2021 14:06:03 -0500
Subject: [PATCH] Started work on the Renderer and added directory-creating
 functionality to writeFile

---
 Game/src/include/MainState.h              |  2 +-
 NothinFancy/NothinFancy.vcxproj           |  8 +++
 NothinFancy/NothinFancy.vcxproj.filters   | 24 ++++++++
 NothinFancy/src/Application.cpp           | 65 ++++-----------------
 NothinFancy/src/Renderer/IndexBuffer.cpp  | 22 +++++++
 NothinFancy/src/Renderer/Renderer.cpp     | 71 +++++++++++++++++++++++
 NothinFancy/src/Renderer/VertexArray.cpp  | 12 ++++
 NothinFancy/src/Renderer/VertexBuffer.cpp | 17 ++++++
 NothinFancy/src/Utility.cpp               | 13 +++++
 NothinFancy/src/include/Application.h     | 11 ++--
 NothinFancy/src/include/IndexBuffer.h     | 18 ++++++
 NothinFancy/src/include/Renderer.h        | 22 +++++++
 NothinFancy/src/include/VertexArray.h     | 13 +++++
 NothinFancy/src/include/VertexBuffer.h    | 16 +++++
 14 files changed, 253 insertions(+), 61 deletions(-)
 create mode 100644 NothinFancy/src/Renderer/IndexBuffer.cpp
 create mode 100644 NothinFancy/src/Renderer/Renderer.cpp
 create mode 100644 NothinFancy/src/Renderer/VertexArray.cpp
 create mode 100644 NothinFancy/src/Renderer/VertexBuffer.cpp
 create mode 100644 NothinFancy/src/include/IndexBuffer.h
 create mode 100644 NothinFancy/src/include/Renderer.h
 create mode 100644 NothinFancy/src/include/VertexArray.h
 create mode 100644 NothinFancy/src/include/VertexBuffer.h

diff --git a/Game/src/include/MainState.h b/Game/src/include/MainState.h
index 9257cdb..bc41824 100644
--- a/Game/src/include/MainState.h
+++ b/Game/src/include/MainState.h
@@ -9,5 +9,5 @@ public:
 	void update() override;
 	void render() override;
 private:
-	Application* m_app;
+
 };
\ No newline at end of file
diff --git a/NothinFancy/NothinFancy.vcxproj b/NothinFancy/NothinFancy.vcxproj
index f1c08cb..5b63d20 100644
--- a/NothinFancy/NothinFancy.vcxproj
+++ b/NothinFancy/NothinFancy.vcxproj
@@ -193,16 +193,24 @@
   <ItemGroup>
     <ClCompile Include="src\Application.cpp" />
     <ClCompile Include="src\IntroGamestate.cpp" />
+    <ClCompile Include="src\Renderer\IndexBuffer.cpp" />
+    <ClCompile Include="src\Renderer\Renderer.cpp" />
+    <ClCompile Include="src\Renderer\VertexArray.cpp" />
+    <ClCompile Include="src\Renderer\VertexBuffer.cpp" />
     <ClCompile Include="src\Utility.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="src\include\Application.h" />
     <ClInclude Include="src\include\Config.h" />
     <ClInclude Include="src\include\IGamestate.h" />
+    <ClInclude Include="src\include\IndexBuffer.h" />
     <ClInclude Include="src\include\IntroGamestate.h" />
     <ClInclude Include="src\include\Input.h" />
     <ClInclude Include="src\include\NothinFancy.h" />
+    <ClInclude Include="src\include\Renderer.h" />
     <ClInclude Include="src\include\Utility.h" />
+    <ClInclude Include="src\include\VertexArray.h" />
+    <ClInclude Include="src\include\VertexBuffer.h" />
   </ItemGroup>
   <ItemGroup>
     <Natvis Include="NatvisFile.natvis" />
diff --git a/NothinFancy/NothinFancy.vcxproj.filters b/NothinFancy/NothinFancy.vcxproj.filters
index 908f155..4692b11 100644
--- a/NothinFancy/NothinFancy.vcxproj.filters
+++ b/NothinFancy/NothinFancy.vcxproj.filters
@@ -24,6 +24,18 @@
     <ClCompile Include="src\IntroGamestate.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="src\Renderer\VertexBuffer.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="src\Renderer\Renderer.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="src\Renderer\IndexBuffer.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="src\Renderer\VertexArray.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="src\include\Config.h">
@@ -47,6 +59,18 @@
     <ClInclude Include="src\include\Input.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="src\include\Renderer.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="src\include\VertexBuffer.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="src\include\IndexBuffer.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="src\include\VertexArray.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <Natvis Include="NatvisFile.natvis" />
diff --git a/NothinFancy/src/Application.cpp b/NothinFancy/src/Application.cpp
index 1aef96f..971e7de 100644
--- a/NothinFancy/src/Application.cpp
+++ b/NothinFancy/src/Application.cpp
@@ -61,7 +61,6 @@ namespace nf {
 	}
 
 	void Application::run() {
-		addIntroState();
 		showWindow(true);
 		m_running = true;
 		MSG msg = { };
@@ -111,6 +110,10 @@ namespace nf {
 		SetWindowPos(m_window, HWND_TOP, x, y, in.width, in.height, SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
 	}
 
+	const HWND& Application::getWindow() {
+		return m_window;
+	}
+
 	const Config& Application::getConfig() const {
 		return m_currentConfig;
 	}
@@ -128,20 +131,20 @@ namespace nf {
 		}
 	}
 
-	void Application::addIntroState() {
+	void Application::startIntroState() {
 		m_sIntro = new IntroGamestate;
 		m_sIntro->onEnter(this);
 		m_currentState = m_sIntro;
 	}
 
 	void Application::startMainThread() {
-		createOpenGLContext();
+		m_renderer = new Renderer(this);
+		startIntroState();
 		while (m_running) {
 			m_deltaTime = m_fpsDuration.count();
-			glClear(GL_COLOR_BUFFER_BIT);
 			m_currentState->update();
 			m_currentState->render();
-			SwapBuffers(m_hdc);
+			m_renderer->doFrame();
 			m_frames++;
 			m_fpsClock2 = std::chrono::steady_clock::now();
 			m_fpsDuration = m_fpsClock2 - m_fpsClock1;
@@ -149,7 +152,7 @@ namespace nf {
 				m_fpsClock1 = std::chrono::steady_clock::now();
 				m_FPS = m_frames;
 				m_frames = 0;
-				Log(m_FPS);
+				Log("FPS: " + std::to_string(m_FPS));
 			}
 			m_fpsDuration = std::chrono::steady_clock::now() - m_frameClock;
 			while (m_fpsDuration.count() < m_minFrametime) {
@@ -158,9 +161,7 @@ namespace nf {
 			m_frameClock = std::chrono::steady_clock::now();
 		}
 		m_currentState->onExit();
-		ReleaseDC(m_window, m_hdc);
-		wglMakeCurrent(NULL, NULL);
-		wglDeleteContext(m_hglrc);
+		delete m_renderer;
 	}
 
 	void Application::registerWindowClass() {
@@ -283,52 +284,6 @@ namespace nf {
 		return DefWindowProc(hWnd, uMsg, wParam, lParam);
 	}
 
-	void Application::createOpenGLContext() {
-		m_hdc = GetDC(m_window);
-		PIXELFORMATDESCRIPTOR pfd = {
-			sizeof(PIXELFORMATDESCRIPTOR),
-			1,
-			PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,    //Flags
-			PFD_TYPE_RGBA,        // The kind of framebuffer. RGBA or palette.
-			32,                   // Colordepth of the framebuffer.
-			0, 0, 0, 0, 0, 0,
-			0,
-			0,
-			0,
-			0, 0, 0, 0,
-			24,                   // Number of bits for the depthbuffer
-			8,                    // Number of bits for the stencilbuffer
-			0,                    // Number of Aux buffers in the framebuffer.
-			PFD_MAIN_PLANE,
-			0,
-			0, 0, 0
-		};
-		int pf = ChoosePixelFormat(m_hdc, &pfd);
-		SetPixelFormat(m_hdc, pf, &pfd);
-		m_hglrc = wglCreateContext(m_hdc);
-		wglMakeCurrent(m_hdc, m_hglrc);
-		glewExperimental = GL_TRUE;
-		if (glewInit() != GLEW_OK) {
-			Error("Could not initialize GLEW");
-		}
-		const int attrib[] = {
-			WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
-			WGL_CONTEXT_MINOR_VERSION_ARB, 3,
-			WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
-			0, 0
-		};
-		wglDeleteContext(m_hglrc);
-		m_hglrc = wglCreateContextAttribsARB(m_hdc, NULL, attrib);
-		wglMakeCurrent(m_hdc, m_hglrc);
-		wglSwapIntervalEXT(0);
-		Log("OpenGL version: " + std::string((char*)glGetString(GL_VERSION)));
-		//TODO: Move default vertex array to Renderer
-		GLuint vao;
-		glGenVertexArrays(1, &vao);
-		glBindVertexArray(vao);
-		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
-	}
-
 	Application::~Application() {
 		Log("Exiting NF application");
 
diff --git a/NothinFancy/src/Renderer/IndexBuffer.cpp b/NothinFancy/src/Renderer/IndexBuffer.cpp
new file mode 100644
index 0000000..047992b
--- /dev/null
+++ b/NothinFancy/src/Renderer/IndexBuffer.cpp
@@ -0,0 +1,22 @@
+#include "IndexBuffer.h"
+
+namespace nf {
+	IndexBuffer::IndexBuffer(const void* data, size_t count) {
+		m_count = count;
+		glGenBuffers(1, &m_id);
+		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_id);
+		glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(unsigned int), data, GL_STATIC_DRAW);//TODO: See if I need to change this to dynamic
+	}
+
+	void IndexBuffer::bind() const {
+		glBindBuffer(GL_ARRAY_BUFFER, m_id);
+	}
+
+	unsigned int IndexBuffer::getCount() {
+		return m_count;
+	}
+
+	IndexBuffer::~IndexBuffer() {
+		glDeleteBuffers(1, &m_id);
+	}
+}
\ No newline at end of file
diff --git a/NothinFancy/src/Renderer/Renderer.cpp b/NothinFancy/src/Renderer/Renderer.cpp
new file mode 100644
index 0000000..308c7bb
--- /dev/null
+++ b/NothinFancy/src/Renderer/Renderer.cpp
@@ -0,0 +1,71 @@
+#include "Renderer.h"
+
+#include "Application.h"
+
+namespace nf {
+	Renderer::Renderer(Application* app) {
+		m_app = app;
+		m_hdc = GetDC(m_app->getWindow());
+		PIXELFORMATDESCRIPTOR pfd = {
+			sizeof(PIXELFORMATDESCRIPTOR),
+			1,
+			PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,    //Flags
+			PFD_TYPE_RGBA,        // The kind of framebuffer. RGBA or palette.
+			32,                   // Colordepth of the framebuffer.
+			0, 0, 0, 0, 0, 0,
+			0,
+			0,
+			0,
+			0, 0, 0, 0,
+			24,                   // Number of bits for the depthbuffer
+			8,                    // Number of bits for the stencilbuffer
+			0,                    // Number of Aux buffers in the framebuffer.
+			PFD_MAIN_PLANE,
+			0,
+			0, 0, 0
+		};
+		int pf = ChoosePixelFormat(m_hdc, &pfd);
+		SetPixelFormat(m_hdc, pf, &pfd);
+		m_hglrc = wglCreateContext(m_hdc);
+		wglMakeCurrent(m_hdc, m_hglrc);
+		glewExperimental = GL_TRUE;
+		if (glewInit() != GLEW_OK) {
+			Error("Could not initialize GLEW");
+		}
+		const int attrib[] = {
+			WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
+			WGL_CONTEXT_MINOR_VERSION_ARB, 3,
+			WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
+			0, 0
+		};
+		wglDeleteContext(m_hglrc);
+		m_hglrc = wglCreateContextAttribsARB(m_hdc, NULL, attrib);
+		wglMakeCurrent(m_hdc, m_hglrc);
+		wglSwapIntervalEXT(0);
+		Log("OpenGL version: " + std::string((char*)glGetString(GL_VERSION)));
+		GLuint vao;
+		glGenVertexArrays(1, &vao);
+		glBindVertexArray(vao);
+		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+	}
+
+	void Renderer::doFrame() {
+		glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height);
+		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+		SwapBuffers(m_hdc);
+
+#ifdef _DEBUG
+		GLenum err = glGetError();
+		if (err != GL_NO_ERROR) {
+			Error(("OpenGL error " + std::to_string(err)).c_str());
+		}
+#endif
+	}
+
+	Renderer::~Renderer() {
+		ReleaseDC(m_app->getWindow(), m_hdc);
+		wglMakeCurrent(NULL, NULL);
+		wglDeleteContext(m_hglrc);
+	}
+}
\ No newline at end of file
diff --git a/NothinFancy/src/Renderer/VertexArray.cpp b/NothinFancy/src/Renderer/VertexArray.cpp
new file mode 100644
index 0000000..a30a846
--- /dev/null
+++ b/NothinFancy/src/Renderer/VertexArray.cpp
@@ -0,0 +1,12 @@
+#include "VertexArray.h"
+
+namespace nf{
+	VertexArray::VertexArray(VertexBuffer& buffer) {
+		glGenVertexArrays(1, &m_id);
+		//TODO: Bind buffer and set the layout
+	}
+
+	VertexArray::~VertexArray() {
+		glDeleteVertexArrays(1, &m_id);
+	}
+}
\ No newline at end of file
diff --git a/NothinFancy/src/Renderer/VertexBuffer.cpp b/NothinFancy/src/Renderer/VertexBuffer.cpp
new file mode 100644
index 0000000..f87488c
--- /dev/null
+++ b/NothinFancy/src/Renderer/VertexBuffer.cpp
@@ -0,0 +1,17 @@
+#include "VertexBuffer.h"
+
+namespace nf {
+	VertexBuffer::VertexBuffer(const void* data, size_t size) {
+		glGenBuffers(1, &m_id);
+		glBindBuffer(GL_ARRAY_BUFFER, m_id);
+		glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);//TODO: See if I need to change this to dynamic
+	}
+
+	void VertexBuffer::bind() const {
+		glBindBuffer(GL_ARRAY_BUFFER, m_id);
+	}
+
+	VertexBuffer::~VertexBuffer() {
+		glDeleteBuffers(1, &m_id);
+	}
+}
\ No newline at end of file
diff --git a/NothinFancy/src/Utility.cpp b/NothinFancy/src/Utility.cpp
index 7b0656c..bdb71b2 100644
--- a/NothinFancy/src/Utility.cpp
+++ b/NothinFancy/src/Utility.cpp
@@ -49,6 +49,19 @@ namespace nf {
 	}
 
 	bool writeFile(const char* filename, const std::string& in) {
+		std::string file(filename);
+		if (file.find('/') || file.find('\\')) {
+			int pos = file.find_last_of("/\\");
+			std::string temp = file.substr(0, pos);
+			std::wstring folders(temp.begin(), temp.end());
+			WCHAR exe[MAX_PATH];
+			GetModuleFileName(GetModuleHandle(NULL), exe, MAX_PATH);
+			std::wstring rootDir(exe);
+			pos = rootDir.find_last_of(L"/\\");
+			rootDir = rootDir.substr(0, pos + 1);
+			folders = rootDir + folders;
+			CreateDirectory(folders.c_str(), NULL);
+		}
 		std::ofstream out;
 		out.open(filename);
 		if (!out) {
diff --git a/NothinFancy/src/include/Application.h b/NothinFancy/src/include/Application.h
index dd41135..d1f8920 100644
--- a/NothinFancy/src/include/Application.h
+++ b/NothinFancy/src/include/Application.h
@@ -7,6 +7,7 @@
 #include "Config.h"
 #include "Utility.h"
 #include "IntroGamestate.h"
+#include "Renderer.h"
 
 namespace nf {
 	class Application {
@@ -22,6 +23,7 @@ namespace nf {
 		void run();
 		void showWindow(bool show);
 		void changeState(const char* stateName);
+		const HWND& getWindow();
 		void changeConfig(const Config& in);
 		const Config& getConfig() const;
 		int getFPS() const;
@@ -29,7 +31,7 @@ namespace nf {
 
 		~Application();
 	private:
-		void addIntroState();
+		void startIntroState();
 		void startMainThread();
 		void registerWindowClass();
 		void toggleFullscreen();
@@ -38,8 +40,6 @@ namespace nf {
 
 		static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
 
-		void createOpenGLContext();
-
 		Config m_currentConfig;
 		bool m_running;
 		HINSTANCE m_hInst;
@@ -47,8 +47,6 @@ namespace nf {
 		HWND m_window;
 		LONG m_defaultWindowStyle;
 		WINDOWPLACEMENT m_wndPlacement;
-		HDC m_hdc;
-		HGLRC m_hglrc;
 
 		std::chrono::steady_clock::time_point m_frameClock = std::chrono::steady_clock::now();
 		std::chrono::duration<double> m_fpsDuration;
@@ -70,5 +68,8 @@ namespace nf {
 
 		//Array of booleans that represent keyboard and mouse input minus the scrollwheel
 		bool m_input[164];
+
+		//Renderer object to use OpenGL to render the current state
+		Renderer* m_renderer;
 	};
 }
\ No newline at end of file
diff --git a/NothinFancy/src/include/IndexBuffer.h b/NothinFancy/src/include/IndexBuffer.h
new file mode 100644
index 0000000..ed7b143
--- /dev/null
+++ b/NothinFancy/src/include/IndexBuffer.h
@@ -0,0 +1,18 @@
+#pragma once
+
+#include "Renderer.h"
+
+namespace nf {
+	class IndexBuffer {
+	public:
+		IndexBuffer(const void* data, size_t count);
+
+		void bind() const;
+		unsigned int getCount();
+
+		~IndexBuffer();
+	private:
+		unsigned int m_id;
+		unsigned int m_count;
+	};
+}
\ No newline at end of file
diff --git a/NothinFancy/src/include/Renderer.h b/NothinFancy/src/include/Renderer.h
new file mode 100644
index 0000000..aa4b769
--- /dev/null
+++ b/NothinFancy/src/include/Renderer.h
@@ -0,0 +1,22 @@
+#pragma once
+#ifdef NFENGINE
+#include "GL/glew.h"
+#include "GL\wglew.h"
+#endif
+
+namespace nf {
+	class Application;
+	class Renderer {
+	public:
+		Renderer(Application* app);
+
+		void doFrame();
+
+		~Renderer();
+	private:
+		Application* m_app;
+
+		HDC m_hdc;
+		HGLRC m_hglrc;
+	};
+}
\ No newline at end of file
diff --git a/NothinFancy/src/include/VertexArray.h b/NothinFancy/src/include/VertexArray.h
new file mode 100644
index 0000000..27a9b0a
--- /dev/null
+++ b/NothinFancy/src/include/VertexArray.h
@@ -0,0 +1,13 @@
+#pragma once
+#include "VertexBuffer.h"
+
+namespace nf {
+	class VertexArray {
+	public:
+		VertexArray(VertexBuffer& buffer);
+
+		~VertexArray();
+	private:
+		unsigned int m_id;
+	};
+}
\ No newline at end of file
diff --git a/NothinFancy/src/include/VertexBuffer.h b/NothinFancy/src/include/VertexBuffer.h
new file mode 100644
index 0000000..0ea4512
--- /dev/null
+++ b/NothinFancy/src/include/VertexBuffer.h
@@ -0,0 +1,16 @@
+#pragma once
+
+#include "Renderer.h"
+
+namespace nf {
+	class VertexBuffer {
+	public:
+		VertexBuffer(const void* data, size_t size);
+
+		void bind() const;
+
+		~VertexBuffer();
+	private:
+		unsigned int m_id;
+	};
+}
\ No newline at end of file