Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ add_library(bald_engine STATIC
${CMAKE_CURRENT_SOURCE_DIR}/src/core/platform/opengl/textures/opengl_texture.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/core/utils/file_manager.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/core/utils/image.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/core/utils/split_string.cpp )
${CMAKE_CURRENT_SOURCE_DIR}/src/core/utils/split_string.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/core/error/expected.h
${CMAKE_CURRENT_SOURCE_DIR}/src/core/error/unexpected.h
)

if (APPLE_BUILD)
target_link_libraries(bald_engine
Expand Down Expand Up @@ -164,6 +167,8 @@ target_link_libraries(runUnitTests bald_engine)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tests/test_file_manager.txt test_file_manager.txt COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/res/shaders/basic.vert res/shaders/basic.vert COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/res/shaders/basic.frag res/shaders/basic.frag COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/res/shaders/default.vert res/shaders/default.vert COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/res/shaders/default.frag res/shaders/default.frag COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/res/textures/fabric.jpg res/textures/fabric.jpg COPYONLY)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/res/textures/lena.jpg res/textures/lena.jpg COPYONLY)

Expand Down
5 changes: 5 additions & 0 deletions engine/res/shaders/default.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#version 330

void main() {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
7 changes: 7 additions & 0 deletions engine/res/shaders/default.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#version 330

layout (location = 0) in vec3 in_Position;

void main() {
gl_Position = vec4(in_Position, 1.0);
}
129 changes: 129 additions & 0 deletions engine/src/core/error/expected.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
//
Comment thread
BIGbadEL marked this conversation as resolved.
// Created by grzegorz on 18.09.19.
//

#pragma once

#include <utility>
#include "unexpected.h"
#include "bald_assert.h"


namespace Bald {
template<typename E, typename U>
class expected {
public:
constexpr expected() { new(&m_Valid) E(); }

constexpr expected(const E& rhs) { new(&m_Valid) E(rhs); }

constexpr expected(const unexpected<U>& rhs) :
_isOk(false) { new(&m_Invalid) U(rhs.value); }

constexpr expected(E&& rhs) { new(&m_Valid) E(std::move(rhs)); }

constexpr expected(unexpected<U>&& rhs) :
_isOk(false) { new(&m_Invalid) U(std::move(rhs.m_Value)); }

constexpr expected(const expected<E, U>& rhs) :
_isOk(rhs._isOk) {
if (_isOk) new(&m_Valid) E(rhs.m_Valid);
else new(&m_Invalid) U(rhs.m_Invalid);
}

constexpr expected(expected<E, U>&& rhs) noexcept :
_isOk(rhs._isOk) {
if (_isOk) new(&m_Valid) E(std::move(rhs.m_Valid));
else new(&m_Invalid) U(std::move(rhs.m_Invalid));
}

constexpr expected<E, U>& operator=(const expected<E, U>& rhs) {
if(this == &rhs) return *this;
dtor();
_isOk = rhs._isOk;
if (_isOk) new(&m_Valid) E(rhs.m_Valid);
else new(&m_Invalid) U(rhs.m_Invalid);
return *this;
}

constexpr expected<E, U>& operator=(expected<E, U>&& rhs) noexcept {
if(this == &rhs) return *this;
dtor();
_isOk = rhs._isOk;
if (_isOk) new(&m_Valid) E(std::move(rhs.m_Valid));
else new(&m_Invalid) U(std::move(rhs.m_Invalid));
return *this;
}

~expected() {
dtor();
}

[[nodiscard]] constexpr bool isValid() const noexcept {
#ifdef DEBUG
m_WasChecked = true;
#endif
return _isOk;
}

constexpr operator bool() const noexcept {
#ifdef DEBUG
m_WasChecked = true;
#endif
return _isOk;
}

[[nodiscard]] constexpr U& error() const& {
BALD_ASSERT(m_WasChecked, "expected", "attempt to access value without calling isValid method", m_WasChecked);
BALD_ASSERT(!_isOk, "expected", "attempt to access invalid value after success", _isOk);
return m_Invalid;
}

[[nodiscard]] U& error()& {
BALD_ASSERT(m_WasChecked, "expected", "attempt to access value without calling isValid method", m_WasChecked);
BALD_ASSERT(!_isOk, "expected", "attempt to access invalid value after success", _isOk);
return m_Invalid;
}

[[nodiscard]] U&& error()&& {
BALD_ASSERT(m_WasChecked, "expected", "attempt to access value without calling isValid method", m_WasChecked);
BALD_ASSERT(!_isOk, "expected", "attempt to access invalid value after success", _isOk);
return std::move(m_Invalid);
}

[[nodiscard]] constexpr E& value() const& {
BALD_ASSERT(m_WasChecked, "expected", "attempt to access value without calling isValid method", m_WasChecked);
BALD_ASSERT(_isOk, "expected", "attempt to access valid value after failure", _isOk);
return m_Valid;
}

[[nodiscard]] E& value()& {
BALD_ASSERT(m_WasChecked, "expected", "attempt to access value without calling isValid method", m_WasChecked);
BALD_ASSERT(_isOk, "expected", "attempt to access valid value after failure", _isOk);
return m_Valid;
}

[[nodiscard]] E&& value()&& {
BALD_ASSERT(m_WasChecked, "expected", "attempt to access value without calling isValid method", m_WasChecked);
BALD_ASSERT(_isOk, "expected", "attempt to access valid value after failure", _isOk);
return std::move(m_Valid);
}

private:
void dtor() const noexcept {
if (_isOk) m_Valid.~E();
else m_Invalid.~U();
}

private:
union {
E m_Valid;
U m_Invalid;
};
#ifdef DEBUG
mutable bool m_WasChecked = false;
#endif
bool _isOk = true;
};
}

17 changes: 17 additions & 0 deletions engine/src/core/error/unexpected.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Created by grzegorz on 18.09.19.
//
#pragma once

namespace Bald {
template <typename T>
struct unexpected {
constexpr explicit unexpected(const T& val): m_Value(val) { }
constexpr operator T() const noexcept { return m_Value; }
T m_Value;
};
}




42 changes: 31 additions & 11 deletions engine/src/core/platform/opengl/shaders/opengl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
namespace Bald::Platform::Graphics {

OpenGLShader::OpenGLShader(const char* vertexPath, const char* fragmentPath)
: m_VertexPath(vertexPath), m_FragmentPath(fragmentPath), m_ShaderID(CreateShader()) {}
:
m_VertexPath(vertexPath), m_FragmentPath(fragmentPath), m_ShaderID(CreateShader()) { }

OpenGLShader::~OpenGLShader() {
glDeleteProgram(m_ShaderID);
Expand Down Expand Up @@ -66,7 +67,7 @@ namespace Bald::Platform::Graphics {
}

void OpenGLShader::SetUniform2i(const char* uniformName, int v0, int v1) const noexcept {
glUniform2i(GetUniformLocation(uniformName), v0,v1);
glUniform2i(GetUniformLocation(uniformName), v0, v1);
}

void OpenGLShader::SetUniform2i(const char* uniformName, const glm::tvec2<int>& vec) const noexcept {
Expand Down Expand Up @@ -95,19 +96,28 @@ namespace Bald::Platform::Graphics {

unsigned OpenGLShader::CreateShader() const noexcept {
int success = 0;

using namespace Utils;
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);

std::string vertexShaderSource = Utils::FileManager::ReadFile(m_VertexPath,
Utils::FileManager::Size::SMALL_FILE);
auto vertex_data = FileManager::ReadFile(m_VertexPath);
if (!vertex_data) {
CORE_LOG_WARN("Could't open shader: " + std::string(m_VertexPath));
CORE_LOG_WARN("Compiling default shader instead");
vertex_data = FileManager::ReadFile("../engine/res/shaders/default.vert");
if(!vertex_data) {
CORE_LOG_ERROR("Could't open default shader");
return 0;
}
}
const std::string& vertexShaderSource = vertex_data.value();
const char* vertexData = vertexShaderSource.c_str();

glShaderSource(vertexShader, 1, &vertexData, nullptr);
glCompileShader(vertexShader);

glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if(success == GL_FALSE) {
if (success == GL_FALSE) {
int length = 0;
glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &length);

Expand All @@ -124,16 +134,26 @@ namespace Bald::Platform::Graphics {

unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
auto fragment_data = FileManager::ReadFile(m_FragmentPath);

if (!fragment_data) {
CORE_LOG_WARN("Could't open shader: " + std::string(m_FragmentPath));
CORE_LOG_WARN("Compiling default shader instead");
fragment_data = FileManager::ReadFile("../engine/res/default.frag");
if(!fragment_data) {
CORE_LOG_ERROR("Could't open default shader");
return 0;
}
}

std::string fragmentShaderSource = Utils::FileManager::ReadFile(m_FragmentPath,
Utils::FileManager::Size::SMALL_FILE);
const std::string& fragmentShaderSource = fragment_data.value();
const char* fragmentData = fragmentShaderSource.c_str();

glShaderSource(fragmentShader, 1, &fragmentData, nullptr);
glCompileShader(fragmentShader);

glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if(success == GL_FALSE) {
if (success == GL_FALSE) {
int length = 0;
glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &length);

Expand All @@ -155,7 +175,7 @@ namespace Bald::Platform::Graphics {
glLinkProgram(id);

glGetProgramiv(id, GL_LINK_STATUS, &success);
if(success == GL_FALSE) {
if (success == GL_FALSE) {
int length = 0;

glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length);
Expand Down Expand Up @@ -184,7 +204,7 @@ namespace Bald::Platform::Graphics {
int OpenGLShader::GetUniformLocation(const char* uniformName) const noexcept {
auto iter = m_UniformLocationCache.find(uniformName);

if(iter != m_UniformLocationCache.end()) {
if (iter != m_UniformLocationCache.end()) {
return iter->second;
}

Expand Down
36 changes: 15 additions & 21 deletions engine/src/core/utils/file_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,56 @@

namespace Bald::Utils {

std::string FileManager::ReadFile(const char* filePath, Size size) {
expected<std::string, FileManager::Error> FileManager::ReadFile(const char* filePath, Size size) {
if (size == Size::SMALL_FILE)
return ReadSmallFile(filePath);
return ReadBigFile(filePath);
}

std::string FileManager::ReadSmallFile(const char* filePath) {
#ifdef WINDOWS
expected<std::string, FileManager::Error> FileManager::ReadSmallFile(const char* filePath) {
#if defined(WINDOWS) || defined(APPLE)
FILE* file;
fopen_s(&file, filePath, "r");
fopen_s(&file, filePath, "rb");
#else
FILE* file = fopen(filePath, "r");
FILE* file = fopen(filePath, "rb");
#endif

if (!file) {
CORE_LOG_WARN("[FileManager] Couldn't open the file at path: " + static_cast<std::string>(filePath));
return "[FileManager] Couldn't open the file at path: " + static_cast<std::string>(filePath);
return unexpected(FileManager::Error::CantOpenFile);
}

fseek(file, 0, SEEK_END);
auto stringSize = static_cast<unsigned long>(ftell(file));
fseek(file, 0, SEEK_SET);

char* buffer;
std::string result;
try {
buffer = new char[stringSize + 1];
result.resize(stringSize);
}
catch (std::bad_alloc& ba) {
CORE_LOG_ERROR("[FileManager] bad_alloc caught: " + static_cast<std::string>(ba.what()));
return std::string("Error!");
return unexpected(FileManager::Error::TooBigFile);
}

if (fread(buffer, sizeof(char), stringSize, file) < stringSize) {
delete[] buffer;
if (fread(result.data(), sizeof(char), stringSize, file) < stringSize) {
fclose(file);
CORE_LOG_ERROR("[FileManager] Could not read whole file: " + static_cast<std::string>(filePath));
return "[FileManager] Could not read whole file: " + static_cast<std::string>(filePath);
return unexpected(FileManager::Error::Fail);
}

buffer[stringSize] = '\0';
std::string result(buffer);
delete[] buffer;
fclose(file);
return result;

}

#ifdef LINUX

std::string FileManager::ReadBigFile(const char* filePath) {
expected<std::string, FileManager::Error> FileManager::ReadBigFile(const char* filePath) {
int fileDescriptor = open(filePath, O_RDONLY, S_IRUSR | S_IWUSR);
struct stat sb{};

if (fstat(fileDescriptor, &sb) == -1) {
CORE_LOG_WARN("[FileManager] Couldn't get size of the file. Check if the file exists at path: " + static_cast<std::string>(filePath));
return "[FileManager] Couldn't get size of the file. Check if the file exists at path: " + static_cast<std::string>(filePath);
return unexpected(FileManager::Error::CantOpenFile);
}

auto* buffer = static_cast<char*>(mmap(nullptr, static_cast<unsigned long>(sb.st_size), PROT_READ, MAP_PRIVATE, fileDescriptor, 0));
Expand All @@ -82,8 +76,8 @@ namespace Bald::Utils {
return result;
}

#else
std::string FileManager::ReadBigFile(const char *filePath) {
#if defined(WINDOWS) || defined(APPLE)
expected<std::string, FileManager::Error> FileManager::ReadBigFile(const char *filePath) {
CORE_LOG_INFO("[FILE_MANAGER] Error: Windows implementation is not done yet! Using slower reading method!");
return ReadSmallFile(filePath);
}
Expand Down
Loading