Started work on the Renderer and added directory-creating functionality to writeFile

This commit is contained in:
Grayson Riffe (Laptop) 2021-08-20 14:06:03 -05:00
parent 11dc2deb2f
commit f6ffb9f87e
14 changed files with 253 additions and 61 deletions

View File

@ -9,5 +9,5 @@ public:
void update() override;
void render() override;
private:
Application* m_app;
};

View File

@ -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" />

View File

@ -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" />

View File

@ -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");

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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) {

View File

@ -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;
};
}

View File

@ -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;
};
}

View File

@ -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;
};
}

View File

@ -0,0 +1,13 @@
#pragma once
#include "VertexBuffer.h"
namespace nf {
class VertexArray {
public:
VertexArray(VertexBuffer& buffer);
~VertexArray();
private:
unsigned int m_id;
};
}

View File

@ -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;
};
}