Added two render layers and the basic rendering pipeline; Fixed fullscreen graphics; No shader re-binding

This commit is contained in:
Grayson Riffe (Laptop) 2021-08-25 02:52:45 -05:00
parent e35688373e
commit 1c638bdcb9
12 changed files with 76 additions and 35 deletions

View File

@ -10,11 +10,10 @@ void MainState::onEnter() {
}
void MainState::update(double deltaTime) {
}
void MainState::render() {
static Renderer& renderer = *m_app->getRenderer();
}
void MainState::onExit() {

View File

@ -10,6 +10,6 @@
</Expand>
</Type>
<Type Name="nf::Config">
<DisplayString>Width = {width}, Height = {height}, Fullscreen = {fullscreen}</DisplayString>
<DisplayString>Width = {width}, Height = {height}, Fullscreen = {fullscreen}, Title = {title,sb}</DisplayString>
</Type>
</AutoVisualizer>

View File

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

View File

@ -6,7 +6,6 @@
namespace nf {
Gamestate::Gamestate(Application* app) {
m_app = app;
m_renderer = m_app->getRenderer();
}
void Gamestate::onEnter() {

View File

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

View File

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

View File

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

View File

@ -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<double> m_fpsDuration;
double m_deltaTime;

View File

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

View File

@ -18,7 +18,6 @@ namespace nf {
virtual void onExit();
protected:
Application* m_app;
Renderer* m_renderer;
//Resource identifier?
};
}

View File

@ -11,7 +11,7 @@ namespace nf {
public:
Renderer(Application* app);
void render(const Drawable& in);
void render(Drawable* in);
void doFrame();

View File

@ -9,6 +9,8 @@ namespace nf {
void bind();
void getUniformLocation(const char* uniformName);
static unsigned int current;
~Shader();
private:
unsigned int m_id;