BIG REFACTOR Part 2

This commit is contained in:
2022-02-16 18:34:49 +01:00
parent bdebabb79e
commit 97a33e5517
11 changed files with 98 additions and 106 deletions

View File

@@ -17,8 +17,9 @@
namespace GL { namespace GL {
struct VertexAttribPointer { struct VertexAttribPointer {
unsigned int m_Index, m_Size; unsigned int m_Index;
int m_Offset; unsigned int m_Size;
unsigned int m_Offset;
}; };
class VertexBuffer { class VertexBuffer {

View File

@@ -9,7 +9,9 @@
#define RENDER_LOADER_TEXTURELOADER_H_ #define RENDER_LOADER_TEXTURELOADER_H_
namespace TextureLoader { namespace TextureLoader {
const unsigned int loadGLTexture(const char* fileName);
unsigned int loadGLTexture(const char* fileName);
} }

View File

@@ -24,21 +24,23 @@ public:
protected: protected:
virtual void getAllUniformLocation() = 0; virtual void getAllUniformLocation() = 0;
int getUniformLocation(const std::string& uniformName) const; int getUniformLocation(const std::string& uniformName) const;
void loadFloat(const int location, const float value)const;
void loadInt(const int& location, const int& value)const; void loadFloat(unsigned int location, float value) const;
void loadVector(const int& location, const glm::vec2& vector)const; void loadInt(unsigned int location, int value) const;
void loadVector(const int& location, const glm::vec3& vector)const; void loadVector(unsigned int location, const glm::vec2& vector) const;
void loadVector(const int& location, const glm::vec4& vector)const; void loadVector(unsigned int location, const glm::vec3& vector) const;
void loadBoolean(const int& location, const bool& value)const; void loadVector(unsigned int location, const glm::vec4& vector) const;
void loadMatrix(const int& location, const glm::mat4& matrix); void loadBoolean(unsigned int location, bool value) const;
void loadMatrix(unsigned int location, const glm::mat4& matrix) const;
void cleanUp() const; void cleanUp() const;
private: private:
unsigned int programID; unsigned int m_ProgramID;
unsigned int vertexShaderID; unsigned int m_VertexShaderID;
unsigned int fragmentShaderID; unsigned int m_FragmentShaderID;
int loadShaderFromFile(const std::string& file, GLenum type);
int loadShader(const std::string& source, GLenum type); unsigned int loadShaderFromFile(const std::string& file, GLenum type);
unsigned int loadShader(const std::string& source, GLenum type);
}; };
#endif /* RENDER_SHADERS_SHADERPROGRAM_H_ */ #endif /* RENDER_SHADERS_SHADERPROGRAM_H_ */

View File

@@ -46,7 +46,7 @@ void Timer::reset() {
bool CooldownTimer::update(std::uint64_t delta) { bool CooldownTimer::update(std::uint64_t delta) {
if (m_Cooldown > 0) { if (m_Cooldown > 0) {
m_Cooldown = std::max(static_cast<std::int64_t>(0), static_cast<std::int64_t>(m_Cooldown - delta)); m_Cooldown = static_cast<std::uint64_t>(std::max(static_cast<std::int64_t>(0), static_cast<std::int64_t>(m_Cooldown - delta)));
} }
return m_Cooldown == 0; return m_Cooldown == 0;
} }
@@ -60,7 +60,7 @@ void CooldownTimer::applyCooldown() {
} }
std::uint64_t getTime() { std::uint64_t getTime() {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock().now().time_since_epoch()).count(); return static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock().now().time_since_epoch()).count());
} }
} // namespace utils } // namespace utils

View File

@@ -36,7 +36,7 @@ IPAddress::IPAddress(const std::string& ip)
std::uint8_t octet3 = std::stoul(std::string(match[3])); std::uint8_t octet3 = std::stoul(std::string(match[3]));
std::uint8_t octet4 = std::stoul(std::string(match[4])); std::uint8_t octet4 = std::stoul(std::string(match[4]));
m_Address = (octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4; m_Address = static_cast<std::uint32_t>((octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4);
m_Valid = true; m_Valid = true;
} }
@@ -55,14 +55,14 @@ IPAddress::IPAddress(const std::wstring& ip)
std::uint8_t octet3 = std::stoul(match[3]); std::uint8_t octet3 = std::stoul(match[3]);
std::uint8_t octet4 = std::stoul(match[4]); std::uint8_t octet4 = std::stoul(match[4]);
m_Address = (octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4; m_Address = static_cast<std::uint32_t>((octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4);
m_Valid = true; m_Valid = true;
} }
/* Initialize by octets */ /* Initialize by octets */
IPAddress::IPAddress(uint8_t octet1, uint8_t octet2, uint8_t octet3, uint8_t octet4) noexcept IPAddress::IPAddress(uint8_t octet1, uint8_t octet2, uint8_t octet3, uint8_t octet4) noexcept
: m_Valid(true) { : m_Valid(true) {
m_Address = (octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4; m_Address = static_cast<std::uint32_t>((octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4);
} }
/* Get the specific octet */ /* Get the specific octet */
@@ -82,7 +82,7 @@ void IPAddress::SetOctet(uint8_t num, uint8_t value) {
octets[num - 1] = value; octets[num - 1] = value;
m_Address = (octets[0] << 24) | (octets[1] << 16) | (octets[2] << 8) | octets[3]; m_Address = static_cast<std::uint32_t>((octets[0] << 24) | (octets[1] << 16) | (octets[2] << 8) | octets[3]);
} }
IPAddress IPAddress::LocalAddress() { IPAddress IPAddress::LocalAddress() {

View File

@@ -56,7 +56,7 @@ bool Renderer::init() {
void Renderer::renderVAO(const GL::VertexArray& vao) { void Renderer::renderVAO(const GL::VertexArray& vao) {
m_WorldShader->start(); m_WorldShader->start();
vao.bind(); vao.bind();
glDrawArrays(GL_TRIANGLES, 0, vao.getVertexCount()); glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(vao.getVertexCount()));
vao.unbind(); vao.unbind();
} }
@@ -64,14 +64,14 @@ void Renderer::renderModel(const Model& model) {
m_EntityShader->start(); m_EntityShader->start();
m_EntityShader->setModelPos(model.positon); m_EntityShader->setModelPos(model.positon);
model.vao->bind(); model.vao->bind();
glDrawArrays(GL_TRIANGLES, 0, model.vao->getVertexCount()); glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(model.vao->getVertexCount()));
model.vao->unbind(); model.vao->unbind();
} }
void Renderer::updateIsometricFade() { void Renderer::updateIsometricFade() {
static std::uint64_t lastTime = utils::getTime(); static std::uint64_t lastTime = utils::getTime();
if (m_IsometricShade != (float)m_IsometricView) { if (m_IsometricShade != static_cast<float>(m_IsometricView)) {
float step = (float)(utils::getTime() - lastTime) / 1000.0f * m_AnimationSpeed; float step = static_cast<float>(utils::getTime() - lastTime) / 1000.0f * m_AnimationSpeed;
if (m_IsometricShade < m_IsometricView) { if (m_IsometricShade < m_IsometricView) {
m_IsometricShade += step; m_IsometricShade += step;
} else { } else {
@@ -92,9 +92,9 @@ void Renderer::prepare() {
void Renderer::resize(int width, int height) { void Renderer::resize(int width, int height) {
m_WorldShader->start(); m_WorldShader->start();
m_WorldShader->setAspectRatio((float)width / height); m_WorldShader->setAspectRatio(static_cast<float>(width) / height);
m_EntityShader->start(); m_EntityShader->start();
m_EntityShader->setAspectRatio((float)width / height); m_EntityShader->setAspectRatio(static_cast<float>(width) / height);
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
} }

View File

@@ -42,8 +42,8 @@ VertexBuffer::~VertexBuffer() {
VertexBuffer::VertexBuffer(const std::vector<float>& data, unsigned int stride) : m_DataStride(stride) { VertexBuffer::VertexBuffer(const std::vector<float>& data, unsigned int stride) : m_DataStride(stride) {
glGenBuffers(1, &m_ID); glGenBuffers(1, &m_ID);
bind(); bind();
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), nullptr, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(data.size() * sizeof(float)), nullptr, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, data.size() * sizeof(float), data.data()); glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast<GLsizeiptr>(data.size() * sizeof(float)), data.data());
unbind(); unbind();
} }
@@ -66,7 +66,7 @@ void VertexBuffer::addVertexAttribPointer(unsigned int index, unsigned int coord
void VertexBuffer::bindVertexAttribs() const { void VertexBuffer::bindVertexAttribs() const {
for (const VertexAttribPointer& pointer : m_VertexAttribs) { for (const VertexAttribPointer& pointer : m_VertexAttribs) {
glEnableVertexAttribArray(pointer.m_Index); glEnableVertexAttribArray(pointer.m_Index);
glVertexAttribPointer(pointer.m_Index, pointer.m_Size, GL_FLOAT, false, m_DataStride * sizeof(float), (void*)(intptr_t)pointer.m_Offset); glVertexAttribPointer(pointer.m_Index, static_cast<GLint>(pointer.m_Size), GL_FLOAT, false, m_DataStride * sizeof(float), reinterpret_cast<void*>(pointer.m_Offset));
} }
} }
} }

View File

@@ -12,7 +12,8 @@
#include "render/GL.h" #include "render/GL.h"
namespace TextureLoader { namespace TextureLoader {
const unsigned int loadGLTexture(const char* fileName) {
unsigned int loadGLTexture(const char* fileName) {
int width, height, comp; int width, height, comp;
@@ -41,4 +42,5 @@ const unsigned int loadGLTexture(const char* fileName) {
stbi_image_free((void*)image); stbi_image_free((void*)image);
return textureID; return textureID;
} }
} }

View File

@@ -97,11 +97,11 @@ void EntityShader::loadShader() {
} }
void EntityShader::getAllUniformLocation() { void EntityShader::getAllUniformLocation() {
location_aspect_ratio = getUniformLocation("aspectRatio"); location_aspect_ratio = static_cast<unsigned int>(getUniformLocation("aspectRatio"));
location_zoom = getUniformLocation("zoom"); location_zoom = static_cast<unsigned int>(getUniformLocation("zoom"));
location_cam = getUniformLocation("camPos"); location_cam = static_cast<unsigned int>(getUniformLocation("camPos"));
location_translation = getUniformLocation("translation"); location_translation = static_cast<unsigned int>(getUniformLocation("translation"));
location_viewtype = getUniformLocation("isometricView"); location_viewtype = static_cast<unsigned int>(getUniformLocation("isometricView"));
} }
void EntityShader::setCamPos(const glm::vec2& camPos) { void EntityShader::setCamPos(const glm::vec2& camPos) {

View File

@@ -9,6 +9,7 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <sstream>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
@@ -17,7 +18,7 @@
#endif #endif
ShaderProgram::ShaderProgram() : ShaderProgram::ShaderProgram() :
programID(0), vertexShaderID(0), fragmentShaderID(0) { m_ProgramID(0), m_VertexShaderID(0), m_FragmentShaderID(0) {
} }
ShaderProgram::~ShaderProgram() { ShaderProgram::~ShaderProgram() {
@@ -25,7 +26,7 @@ ShaderProgram::~ShaderProgram() {
} }
void ShaderProgram::start() const { void ShaderProgram::start() const {
glUseProgram(programID); glUseProgram(m_ProgramID);
} }
void ShaderProgram::stop() const { void ShaderProgram::stop() const {
@@ -33,74 +34,78 @@ void ShaderProgram::stop() const {
} }
int ShaderProgram::getUniformLocation(const std::string& uniformName) const { int ShaderProgram::getUniformLocation(const std::string& uniformName) const {
const int location = glGetUniformLocation(programID, uniformName.c_str()); const int location = glGetUniformLocation(m_ProgramID, uniformName.c_str());
if (location == -1) { if (location == -1) {
std::cout << "Warning ! Uniform variable " << uniformName << " not found !\n"; std::cout << "Warning ! Uniform variable " << uniformName << " not found !\n";
} }
return location; return location;
} }
void ShaderProgram::loadFloat(const int location, const float value) const { void ShaderProgram::loadFloat(unsigned int location, float value) const {
glUniform1f(location, value); glUniform1f(static_cast<GLint>(location), value);
} }
void ShaderProgram::loadInt(const int& location, const int& value) const { void ShaderProgram::loadInt(unsigned int location, int value) const {
glUniform1i(location, value); glUniform1i(static_cast<GLint>(location), value);
} }
void ShaderProgram::loadVector(const int& location, void ShaderProgram::loadVector(unsigned int location,
const glm::vec2& vector) const { const glm::vec2& vector) const {
glUniform2f(location, vector.x, vector.y); glUniform2f(static_cast<GLint>(location), vector.x, vector.y);
} }
void ShaderProgram::loadVector(const int& location, void ShaderProgram::loadVector(unsigned int location,
const glm::vec3& vector) const { const glm::vec3& vector) const {
glUniform3f(location, vector.x, vector.y, vector.z); glUniform3f(static_cast<GLint>(location), vector.x, vector.y, vector.z);
} }
void ShaderProgram::loadVector(const int& location, void ShaderProgram::loadVector(unsigned int location,
const glm::vec4& vector) const { const glm::vec4& vector) const {
glUniform4f(location, vector.x, vector.y, vector.z, vector.w); glUniform4f(static_cast<GLint>(location), vector.x, vector.y, vector.z, vector.w);
} }
void ShaderProgram::loadBoolean(const int& location, const bool& value) const { void ShaderProgram::loadBoolean(unsigned int location, bool value) const {
glUniform1i(location, value); glUniform1i(static_cast<GLint>(location), value);
}
void ShaderProgram::loadMatrix(unsigned int location, const glm::mat4& matrix) const {
glUniformMatrix4fv(static_cast<GLint>(location), 1, false, glm::value_ptr(matrix));
} }
void ShaderProgram::cleanUp() const { void ShaderProgram::cleanUp() const {
stop(); stop();
glDetachShader(programID, vertexShaderID); glDetachShader(m_ProgramID, m_VertexShaderID);
glDetachShader(programID, fragmentShaderID); glDetachShader(m_ProgramID, m_FragmentShaderID);
glDeleteShader(vertexShaderID); glDeleteShader(m_VertexShaderID);
glDeleteShader(fragmentShaderID); glDeleteShader(m_FragmentShaderID);
glDeleteProgram(programID); glDeleteProgram(m_ProgramID);
} }
void ShaderProgram::loadProgramFile(const std::string& vertexFile, void ShaderProgram::loadProgramFile(const std::string& vertexFile,
const std::string& fragmentFile) { const std::string& fragmentFile) {
vertexShaderID = loadShaderFromFile(vertexFile, GL_VERTEX_SHADER); m_VertexShaderID = static_cast<unsigned int>(loadShaderFromFile(vertexFile, GL_VERTEX_SHADER));
fragmentShaderID = loadShaderFromFile(fragmentFile, GL_FRAGMENT_SHADER); m_FragmentShaderID = static_cast<unsigned int>(loadShaderFromFile(fragmentFile, GL_FRAGMENT_SHADER));
programID = glCreateProgram(); m_ProgramID = glCreateProgram();
glAttachShader(programID, vertexShaderID); glAttachShader(m_ProgramID, m_VertexShaderID);
glAttachShader(programID, fragmentShaderID); glAttachShader(m_ProgramID, m_FragmentShaderID);
glLinkProgram(programID); glLinkProgram(m_ProgramID);
glValidateProgram(programID); glValidateProgram(m_ProgramID);
getAllUniformLocation(); getAllUniformLocation();
} }
void ShaderProgram::loadProgram(const std::string& vertexSource, void ShaderProgram::loadProgram(const std::string& vertexSource,
const std::string& fragmentSource) { const std::string& fragmentSource) {
vertexShaderID = loadShader(vertexSource, GL_VERTEX_SHADER); m_VertexShaderID = static_cast<unsigned int>(loadShader(vertexSource, GL_VERTEX_SHADER));
fragmentShaderID = loadShader(fragmentSource, GL_FRAGMENT_SHADER); m_FragmentShaderID = static_cast<unsigned int>(loadShader(fragmentSource, GL_FRAGMENT_SHADER));
programID = glCreateProgram(); m_ProgramID = glCreateProgram();
glAttachShader(programID, vertexShaderID); glAttachShader(m_ProgramID, m_VertexShaderID);
glAttachShader(programID, fragmentShaderID); glAttachShader(m_ProgramID, m_FragmentShaderID);
glLinkProgram(programID); glLinkProgram(m_ProgramID);
glValidateProgram(programID); glValidateProgram(m_ProgramID);
getAllUniformLocation(); getAllUniformLocation();
} }
int ShaderProgram::loadShader(const std::string& source, GLenum type) { unsigned int ShaderProgram::loadShader(const std::string& source, GLenum type) {
unsigned int shaderID = glCreateShader(type); unsigned int shaderID = glCreateShader(type);
const char* c_str = source.c_str(); const char* c_str = source.c_str();
@@ -113,9 +118,9 @@ int ShaderProgram::loadShader(const std::string& source, GLenum type) {
std::cout << "Could not compile shader !\n"; std::cout << "Could not compile shader !\n";
GLsizei size; GLsizei size;
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &size); glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &size);
char error[size]; std::vector<char> shaderError(static_cast<std::size_t>(size));
glGetShaderInfoLog(shaderID, size, &size, error); glGetShaderInfoLog(shaderID, size, &size, shaderError.data());
std::cout << error << std::endl; std::cout << shaderError.data() << std::endl;
#ifdef __ANDROID__ #ifdef __ANDROID__
__android_log_print(ANDROID_LOG_ERROR, "TRACKERS", "Could not compile shader !\n %s", error); __android_log_print(ANDROID_LOG_ERROR, "TRACKERS", "Could not compile shader !\n %s", error);
#endif #endif
@@ -123,35 +128,15 @@ int ShaderProgram::loadShader(const std::string& source, GLenum type) {
return shaderID; return shaderID;
} }
int ShaderProgram::loadShaderFromFile(const std::string& file, GLenum type) { unsigned int ShaderProgram::loadShaderFromFile(const std::string& file, GLenum type) {
std::string shaderSource = ""; std::stringstream stream;
std::ifstream fileStream(file); std::ifstream fileStream(file);
if (fileStream.is_open()) {
std::string line;
while (getline(fileStream, line)) {
shaderSource += line + "\n";
}
fileStream.close();
}
unsigned int shaderID = glCreateShader(type);
const char* c_str = shaderSource.c_str(); if(fileStream) {
int* null = 0; stream << fileStream.rdbuf();
glShaderSource(shaderID, 1, &c_str, null); // @suppress("Function cannot be resolved") } else {
glCompileShader(shaderID); return 0;
GLint compilesuccessful;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compilesuccessful);
if (compilesuccessful == false) {
std::cout << "Could not compile shader !\n";
GLsizei size;
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &size);
char error[size];
glGetShaderInfoLog(shaderID, size, &size, error);
std::cout << error << std::endl;
}
return shaderID;
} }
void ShaderProgram::loadMatrix(const int& location, const glm::mat4& matrix) { return loadShader(stream.str(), type);
glUniformMatrix4fv(location, 1, false, glm::value_ptr(matrix));
} }

View File

@@ -98,10 +98,10 @@ void WorldShader::loadShader() {
} }
void WorldShader::getAllUniformLocation() { void WorldShader::getAllUniformLocation() {
location_aspect_ratio = getUniformLocation("aspectRatio"); location_aspect_ratio = static_cast<unsigned int>(getUniformLocation("aspectRatio"));
location_zoom = getUniformLocation("zoom"); location_zoom = static_cast<unsigned int>(getUniformLocation("zoom"));
location_cam = getUniformLocation("camPos"); location_cam = static_cast<unsigned int>(getUniformLocation("camPos"));
location_viewtype = getUniformLocation("isometricView"); location_viewtype = static_cast<unsigned int>(getUniformLocation("isometricView"));
} }
void WorldShader::setCamPos(const glm::vec2& camPos) { void WorldShader::setCamPos(const glm::vec2& camPos) {