From 1c638bdcb908ef79a221b9dd98bde0df84edfb1f Mon Sep 17 00:00:00 2001 From: "Grayson Riffe (Laptop)" Date: Wed, 25 Aug 2021 02:52:45 -0500 Subject: [PATCH] Added two render layers and the basic rendering pipeline; Fixed fullscreen graphics; No shader re-binding --- Game/src/MainState.cpp | 3 +- NothinFancy/NatvisFile.natvis | 2 +- NothinFancy/src/Application.cpp | 42 +++++++++++++++------------ NothinFancy/src/Gamestate.cpp | 1 - NothinFancy/src/Renderer/Drawable.cpp | 22 +++++++++++++- NothinFancy/src/Renderer/Renderer.cpp | 12 ++++++-- NothinFancy/src/Renderer/Shader.cpp | 7 ++++- NothinFancy/src/include/Application.h | 2 +- NothinFancy/src/include/Drawable.h | 15 ++++++---- NothinFancy/src/include/Gamestate.h | 1 - NothinFancy/src/include/Renderer.h | 2 +- NothinFancy/src/include/Shader.h | 2 ++ 12 files changed, 76 insertions(+), 35 deletions(-) diff --git a/Game/src/MainState.cpp b/Game/src/MainState.cpp index 4bb402d..061c9fe 100644 --- a/Game/src/MainState.cpp +++ b/Game/src/MainState.cpp @@ -10,11 +10,10 @@ void MainState::onEnter() { } void MainState::update(double deltaTime) { - } void MainState::render() { - + static Renderer& renderer = *m_app->getRenderer(); } void MainState::onExit() { diff --git a/NothinFancy/NatvisFile.natvis b/NothinFancy/NatvisFile.natvis index 746df7f..0f00592 100644 --- a/NothinFancy/NatvisFile.natvis +++ b/NothinFancy/NatvisFile.natvis @@ -10,6 +10,6 @@ - Width = {width}, Height = {height}, Fullscreen = {fullscreen} + Width = {width}, Height = {height}, Fullscreen = {fullscreen}, Title = {title,sb} \ No newline at end of file diff --git a/NothinFancy/src/Application.cpp b/NothinFancy/src/Application.cpp index 165d7b9..9cf8c92 100644 --- a/NothinFancy/src/Application.cpp +++ b/NothinFancy/src/Application.cpp @@ -11,8 +11,9 @@ namespace nf { Application::Application(Config& config) : m_currentConfig(config), - m_wndPlacement{ sizeof(m_wndPlacement) }, m_running(false), + m_altWidth(1280), + m_altHeight(720), m_defaultStateAdded(false), m_stateChange(false) { @@ -44,7 +45,7 @@ namespace nf { return m_renderer; } - void Application::addState(Gamestate* state,const std::string& stateName) { + void Application::addState(Gamestate* state, const std::string& stateName) { if (m_states.find(stateName) == m_states.end()) { m_states[stateName] = state; } @@ -101,19 +102,19 @@ namespace nf { const HWND& Application::getWindow() { return m_window; } - + //TODO: Throughly test this void Application::changeConfig(const Config& in) { SetWindowText(m_window, toWide(in.title)); - if (in.fullscreen != m_currentConfig.fullscreen) { - m_currentConfig = in; + bool temp = m_currentConfig.fullscreen; + m_currentConfig = in; + if (in.fullscreen != temp) toggleFullscreen(); - } if (m_currentConfig.fullscreen) return; - m_currentConfig = in; int x = 0, y = 0; calculateNewWindowPos(x, y); - SetWindowPos(m_window, HWND_TOP, x, y, in.width, in.height, SWP_NOOWNERZORDER | SWP_FRAMECHANGED); + RECT size = getWindowRect(); + SetWindowPos(m_window, HWND_TOP, x, y, size.right, size.bottom, SWP_NOOWNERZORDER | SWP_FRAMECHANGED); } const Config& Application::getConfig() const { @@ -169,20 +170,20 @@ namespace nf { x = monX - (m_currentConfig.width / 2); y = monY - (m_currentConfig.height / 2); } - //TODO: Test fullscreen graphcis + //TODO: Test fullscreen graphics void Application::toggleFullscreen() { DWORD wndStyle = GetWindowLong(m_window, GWL_STYLE); if (wndStyle & WS_OVERLAPPEDWINDOW) { - GetWindowPlacement(m_window, &m_wndPlacement); MONITORINFO mi = { sizeof(mi) }; GetMonitorInfo(MonitorFromWindow(m_window, MONITOR_DEFAULTTOPRIMARY), &mi); SetWindowLong(m_window, GWL_STYLE, wndStyle & ~WS_OVERLAPPEDWINDOW); - SetWindowPos(m_window, HWND_TOP, mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right - mi.rcMonitor.left, mi.rcMonitor.bottom - mi.rcMonitor.top, SWP_NOOWNERZORDER | SWP_FRAMECHANGED); + m_currentConfig.width = mi.rcMonitor.right - mi.rcMonitor.left; + m_currentConfig.height = mi.rcMonitor.bottom - mi.rcMonitor.top; + SetWindowPos(m_window, HWND_TOP, mi.rcMonitor.left, mi.rcMonitor.top, m_currentConfig.width, m_currentConfig.height, SWP_NOOWNERZORDER | SWP_FRAMECHANGED); } else { - SetWindowLong(m_window, GWL_STYLE, m_defaultWindowStyle); - SetWindowPlacement(m_window, &m_wndPlacement); - SetWindowPos(m_window, NULL, 0, 0, m_currentConfig.width, m_currentConfig.height, SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED); + SetWindowLong(m_window, GWL_STYLE, wndStyle | WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX); + SetWindowPos(m_window, HWND_TOP, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE); } } @@ -230,7 +231,7 @@ namespace nf { m_currentState = m_states[m_nextState]; m_currentState->onEnter(); } - + //TODO: mouse position input LRESULT CALLBACK Application::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { Application* app = (Application*)GetProp(hWnd, L"App"); switch (uMsg) { @@ -240,8 +241,14 @@ namespace nf { case WM_SYSKEYDOWN: { if (GetKeyState(VK_RETURN) & 0x8000) { if (!(lParam & (1 << 30))) { - app->m_currentConfig.fullscreen = !app->m_currentConfig.fullscreen; - app->toggleFullscreen(); + if (!app->m_currentConfig.fullscreen) { + app->m_altWidth = app->m_currentConfig.width; + app->m_altHeight = app->m_currentConfig.height; + } + app->changeConfig({ app->m_currentConfig.width, app->m_currentConfig.height, !app->m_currentConfig.fullscreen, app->m_currentConfig.title }); + if (!app->m_currentConfig.fullscreen) { + app->changeConfig({ app->m_altWidth, app->m_altHeight, app->m_currentConfig.fullscreen, app->m_currentConfig.title }); + } } return 0; } @@ -250,7 +257,6 @@ namespace nf { case WM_MENUCHAR: { return MNC_CLOSE << 16; } - //TODO: mouse position input case WM_LBUTTONDOWN: { app->m_input[1] = true; return 0; diff --git a/NothinFancy/src/Gamestate.cpp b/NothinFancy/src/Gamestate.cpp index 6a4e2ea..e54b8f6 100644 --- a/NothinFancy/src/Gamestate.cpp +++ b/NothinFancy/src/Gamestate.cpp @@ -6,7 +6,6 @@ namespace nf { Gamestate::Gamestate(Application* app) { m_app = app; - m_renderer = m_app->getRenderer(); } void Gamestate::onEnter() { diff --git a/NothinFancy/src/Renderer/Drawable.cpp b/NothinFancy/src/Renderer/Drawable.cpp index 5ee25a7..d2f8260 100644 --- a/NothinFancy/src/Renderer/Drawable.cpp +++ b/NothinFancy/src/Renderer/Drawable.cpp @@ -1,8 +1,28 @@ #include "Drawable.h" namespace nf { - Drawable::Drawable() { + Drawable::Drawable(const char* vertexShader, const char* fragmentShader, const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount) : + m_shader(vertexShader, fragmentShader), + m_vao(), + m_ib(indexBufferData, indexBufferCount) + { + m_vao.addBuffer(vertexBufferData, vertexBufferSize); + m_vao.push(2); + m_vao.finishBufferLayout(); + } + unsigned int Drawable::getIndexCount() { + return m_ib.getCount(); + } + + void Drawable::bind() { + m_shader.bind(); + m_vao.bind(); + m_ib.bind(); + } + + Drawable::DrawableType Drawable::identity() { + return DrawableType::NF_NONE; } Drawable::~Drawable() { diff --git a/NothinFancy/src/Renderer/Renderer.cpp b/NothinFancy/src/Renderer/Renderer.cpp index 5ea2988..b05b3f9 100644 --- a/NothinFancy/src/Renderer/Renderer.cpp +++ b/NothinFancy/src/Renderer/Renderer.cpp @@ -53,8 +53,13 @@ namespace nf { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); } - void Renderer::render(const Drawable& in) { - //TODO: Check identity + void Renderer::render(Drawable* in) { + if (in == nullptr) + Error("Drawable object tried to render before being constructed!"); + if (in->identity() == Drawable::DrawableType::NF_UI) + m_lUI.push_back(in); + else + m_lGame.push_back(in); } void Renderer::doFrame() { @@ -63,7 +68,8 @@ namespace nf { for (Drawable* draw : m_lGame) { Drawable& curr = *draw; - + curr.bind(); + glDrawElements(GL_TRIANGLES, curr.getIndexCount(), GL_UNSIGNED_INT, nullptr); } for (Drawable* draw : m_lUI) { diff --git a/NothinFancy/src/Renderer/Shader.cpp b/NothinFancy/src/Renderer/Shader.cpp index ada799e..beb8c4b 100644 --- a/NothinFancy/src/Renderer/Shader.cpp +++ b/NothinFancy/src/Renderer/Shader.cpp @@ -44,7 +44,10 @@ namespace nf { } void Shader::bind() { - glUseProgram(m_id); + if (m_id != Shader::current) { + glUseProgram(m_id); + Shader::current = m_id; + } } //TODO: Create overloaded setUniform function void Shader::getUniformLocation(const char* uniformName) { @@ -57,4 +60,6 @@ namespace nf { Shader::~Shader() { glDeleteProgram(m_id); } + + unsigned int Shader::current; } \ No newline at end of file diff --git a/NothinFancy/src/include/Application.h b/NothinFancy/src/include/Application.h index 43f5aa6..6465717 100644 --- a/NothinFancy/src/include/Application.h +++ b/NothinFancy/src/include/Application.h @@ -47,7 +47,7 @@ namespace nf { LPCWSTR m_wclassName; HWND m_window; LONG m_defaultWindowStyle; - WINDOWPLACEMENT m_wndPlacement; + int m_altWidth, m_altHeight; std::chrono::duration m_fpsDuration; double m_deltaTime; diff --git a/NothinFancy/src/include/Drawable.h b/NothinFancy/src/include/Drawable.h index 5385cb8..7ca8e05 100644 --- a/NothinFancy/src/include/Drawable.h +++ b/NothinFancy/src/include/Drawable.h @@ -1,20 +1,25 @@ #pragma once -#include "VertexArray.h" #include "Shader.h" +#include "VertexArray.h" #include "IndexBuffer.h" namespace nf { class Drawable { - enum class DrawableType { - NF_GAME, NF_UI - }; public: - Drawable(); + enum class DrawableType { + NF_NONE, NF_GAME, NF_UI + }; + Drawable(const char* vertexShader, const char* fragmentShader, const void* vertexBuffer, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount); + unsigned int getIndexCount(); + void bind(); virtual DrawableType identity(); ~Drawable(); protected: //TODO: Add VAO, Shader, index buffer, etc. + Shader m_shader; + VertexArray m_vao; + IndexBuffer m_ib; }; } \ No newline at end of file diff --git a/NothinFancy/src/include/Gamestate.h b/NothinFancy/src/include/Gamestate.h index 8b52d82..81467f5 100644 --- a/NothinFancy/src/include/Gamestate.h +++ b/NothinFancy/src/include/Gamestate.h @@ -18,7 +18,6 @@ namespace nf { virtual void onExit(); protected: Application* m_app; - Renderer* m_renderer; //Resource identifier? }; } \ No newline at end of file diff --git a/NothinFancy/src/include/Renderer.h b/NothinFancy/src/include/Renderer.h index 5d96715..3fc2412 100644 --- a/NothinFancy/src/include/Renderer.h +++ b/NothinFancy/src/include/Renderer.h @@ -11,7 +11,7 @@ namespace nf { public: Renderer(Application* app); - void render(const Drawable& in); + void render(Drawable* in); void doFrame(); diff --git a/NothinFancy/src/include/Shader.h b/NothinFancy/src/include/Shader.h index e436c0b..f10e0c3 100644 --- a/NothinFancy/src/include/Shader.h +++ b/NothinFancy/src/include/Shader.h @@ -9,6 +9,8 @@ namespace nf { void bind(); void getUniformLocation(const char* uniformName); + static unsigned int current; + ~Shader(); private: unsigned int m_id;