Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Windmill-City committed Aug 17, 2023
1 parent 7009ed1 commit 9462437
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 138 deletions.
13 changes: 13 additions & 0 deletions include/Main.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "Render.hpp"

struct MainContext
{
static MainContext Main;

ResourceManager R;
Renderer Render;

void setup();
};
8 changes: 4 additions & 4 deletions include/Render.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

#include "Shader.hpp"

struct Render
struct Renderer
{
const Shader shader;
Shader DefaultShader;

Render(Shader shader);
void render();
Renderer();
void newFrame();
};
20 changes: 10 additions & 10 deletions include/Resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ struct ResourceLocation
* @brief Resource n
* eg: default:shader/VertexShader.txt, the domain is 'default'
*/
const std::wstring domain;
const std::wstring Domain;
/**
* @brief Resource path
* eg: default:shader/VertexShader.txt, the path is 'shader/VertexShader.txt'
*/
const std::wstring path;
const std::wstring Path;

/**
* @brief Construct a new Resource Location object
Expand Down Expand Up @@ -94,7 +94,7 @@ struct ResourceManager : public ResourceProvider
* @brief Domain providers
* Key: Domain, Value: Provider
*/
ProviderHolder providers;
ProviderHolder Providers;

ResourceManager();
virtual ResourceStream get(const ResourceLocation& loc) override;
Expand Down Expand Up @@ -122,25 +122,25 @@ struct EmbedResource : public ResourceProvider
* @brief Resource index
* Key: Path, Val:Pair<Offset,Size>
*/
using Index = std::unordered_map<std::wstring, const std::pair<size_t, size_t>>;
using IndexHolder = std::unordered_map<std::wstring, const std::pair<size_t, size_t>>;

/**
* @brief Resource domain
*
*/
const std::wstring domain;
const std::wstring Domain;
/**
* @brief Resource block, contains continuously stored files
*
*/
const uint8_t* block;
const Index index;
const uint8_t* Block;
const IndexHolder Index;

EmbedResource(const Index index, const uint8_t* block);
EmbedResource(const IndexHolder index, const uint8_t* block);
EmbedResource(const uint8_t* index, const uint8_t* block);
virtual ResourceStream get(const ResourceLocation& loc) override;

static Index _make_index(const uint8_t* index);
static IndexHolder _make_index(const uint8_t* index);

template <class T, typename = std::enable_if_t<std::is_trivial_v<T>>>
static T _get(const uint8_t* index, size_t& offset)
Expand All @@ -155,7 +155,7 @@ struct EmbedResource : public ResourceProvider

struct FileResource : public ResourceProvider
{
const std::filesystem::path root;
const std::filesystem::path Root;

FileResource(const std::wstring root);
virtual ResourceStream get(const ResourceLocation& loc) override;
Expand Down
12 changes: 2 additions & 10 deletions include/Shader.hpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
#pragma once

#include <glad/glad.h>

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <stdint.h>

#include "Resource.hpp"

struct Shader
{
protected:
unsigned int ID;
unsigned int ProgramID;

public:
Shader(ResourceStream vShader, ResourceStream fShader);
Shader() = default;
~Shader();

Shader(Shader&& _Right);
Expand Down
40 changes: 2 additions & 38 deletions src/Render.cpp
Original file line number Diff line number Diff line change
@@ -1,45 +1,9 @@
#include "Render.hpp"
#include <format>
#include <fstream>
#include <iostream>
#include <string>

unsigned int VAO;

Render::Render(Shader shader)
: shader(std::move(shader))
Renderer::Renderer()
{
// VAO
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);

// VBO
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);

// EBO
unsigned int EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);

// Vertex Attribute
// 位置属性
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// 颜色属性
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);

// Wireframe
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}

void Render::render()
void Renderer::newFrame()
{
shader.use();

glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
36 changes: 18 additions & 18 deletions src/Resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ ResourceLocation::ResourceLocation(const std::wstring key)
}

ResourceLocation::ResourceLocation(const std::wstring domain, const std::wstring path) noexcept
: domain(domain)
, path(path)
: Domain(domain)
, Path(path)
{
}

bool ResourceLocation::operator==(const ResourceLocation& b) const noexcept
{
return this->domain == b.domain && this->path == b.path;
return this->Domain == b.Domain && this->Path == b.Path;
}

bool ResourceLocation::operator<(const ResourceLocation& b) const noexcept
{
return this->domain < b.domain || (this->domain == b.domain && this->path < b.path);
return this->Domain < b.Domain || (this->Domain == b.Domain && this->Path < b.Path);
}

std::wstring ResourceLocation::getDomain(const std::wstring key)
Expand Down Expand Up @@ -58,16 +58,16 @@ extern "C" const uint8_t _embed_blockAssets[];
ResourceManager::ResourceManager()
{
auto embed_res = std::make_unique<EmbedResource>(_embed_indexAssets, _embed_blockAssets);
providers.emplace(EMBED_DOMAIN, std::move(embed_res));
Providers.emplace(EMBED_DOMAIN, std::move(embed_res));

auto default_res = std::make_unique<FileResource>(DEFAULT_ASSETS_DIR);
providers.emplace(DEFAULT_DOMAIN, std::move(default_res));
Providers.emplace(DEFAULT_DOMAIN, std::move(default_res));
}

ResourceStream ResourceManager::get(const ResourceLocation& loc)
{
auto it = providers.find(loc.domain);
if (it != providers.end())
auto it = Providers.find(loc.Domain);
if (it != Providers.end())
{
return it->second->get(loc);
}
Expand All @@ -76,13 +76,13 @@ ResourceStream ResourceManager::get(const ResourceLocation& loc)
}

FileResource::FileResource(const std::wstring root)
: root(root)
: Root(root)
{
}

ResourceStream FileResource::get(const ResourceLocation& loc)
{
auto stream = std::make_unique<std::ifstream>(root / loc.path, std::ios_base::binary);
auto stream = std::make_unique<std::ifstream>(Root / loc.Path, std::ios_base::binary);
if (stream->fail()) return {};
return stream;
}
Expand Down Expand Up @@ -127,9 +127,9 @@ EmbedResource::imstream::imstream(const uint8_t* base, size_t size)
{
}

EmbedResource::EmbedResource(const Index index, const uint8_t* block)
: index(index)
, block(block)
EmbedResource::EmbedResource(const IndexHolder index, const uint8_t* block)
: Index(index)
, Block(block)
{
}

Expand All @@ -140,21 +140,21 @@ EmbedResource::EmbedResource(const uint8_t* index, const uint8_t* block)

ResourceStream EmbedResource::get(const ResourceLocation& loc)
{
auto it = index.find(loc.path);
if (it != index.end())
auto it = Index.find(loc.Path);
if (it != Index.end())
{
auto offset = it->second.first;
auto size = it->second.second;

auto stream = std::make_unique<imstream>(&block[offset], size);
auto stream = std::make_unique<imstream>(&Block[offset], size);
return stream;
}
return {};
}

EmbedResource::Index EmbedResource::_make_index(const uint8_t* index)
EmbedResource::IndexHolder EmbedResource::_make_index(const uint8_t* index)
{
EmbedResource::Index _index;
EmbedResource::IndexHolder _index;

size_t offset = 0;
auto count = _get<size_t>(index, offset);
Expand Down
51 changes: 8 additions & 43 deletions src/Shader.cpp
Original file line number Diff line number Diff line change
@@ -1,69 +1,34 @@
#include "Shader.hpp"

Shader::Shader(ResourceStream vShader, ResourceStream fShader)
{
// 1. retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::stringstream vShaderStream, fShaderStream;

// read file's buffer contents into streams
vShaderStream << vShader->rdbuf();
fShaderStream << fShader->rdbuf();
// convert stream into string
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();

const char* vShaderCode = vertexCode.c_str();
const char* fShaderCode = fragmentCode.c_str();

// 2. compile shaders
unsigned int vertex, fragment;
// vertex shader
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
// fragment Shader
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
// shader Program
ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
glLinkProgram(ID);
// delete the shaders as they're linked into our program now and no longer necessary
glDeleteShader(vertex);
glDeleteShader(fragment);
}
#include <glad/glad.h>

Shader::~Shader()
{
glDeleteProgram(ID);
glDeleteProgram(ProgramID);
}

Shader::Shader(Shader&& _Right)
: ID(_Right.ID)
: ProgramID(_Right.ProgramID)
{
_Right.ID = 0;
_Right.ProgramID = 0;
}

void Shader::use() const
{
glUseProgram(ID);
glUseProgram(ProgramID);
}

void Shader::setBool(const std::string_view name, bool value) const
{
glUniform1i(glGetUniformLocation(ID, name.data()), (int)value);
glUniform1i(glGetUniformLocation(ProgramID, name.data()), (int)value);
}

void Shader::setInt(const std::string_view name, int value) const
{
glUniform1i(glGetUniformLocation(ID, name.data()), value);
glUniform1i(glGetUniformLocation(ProgramID, name.data()), value);
}

void Shader::setFloat(const std::string_view name, float value) const
{
glUniform1f(glGetUniformLocation(ID, name.data()), value);
glUniform1f(glGetUniformLocation(ProgramID, name.data()), value);
}
24 changes: 9 additions & 15 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
#include <cstdlib>
#include <iostream>

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <Render.hpp>
#include <Resource.hpp>
#include "Main.hpp"

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
Expand Down Expand Up @@ -45,15 +41,7 @@ int main()
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// GLAD Init End

// ResourceManager Start
ResourceManager resMan;
// ResourceManager End

// Render
auto fShader = resMan.get(L"embed:shader/FragmentShader.txt");
auto vShader = resMan.get(L"embed:shader/VertexShader.txt");
Shader shader(std::move(vShader), std::move(fShader));
auto renderer = Render(std::move(shader));
MainContext::Main.setup();

// Event Loop
while (!glfwWindowShouldClose(window))
Expand All @@ -64,7 +52,7 @@ int main()
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

renderer.render();
MainContext::Main.Render.newFrame();

glfwSwapBuffers(window);
glfwPollEvents();
Expand All @@ -82,4 +70,10 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height)
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true);
}

MainContext MainContext::Main = MainContext();

void MainContext::setup()
{
}

0 comments on commit 9462437

Please sign in to comment.