Skip to content

Commit

Permalink
First render on both OpenGL and DX11.
Browse files Browse the repository at this point in the history
ClearBuffer abstraction working for both.

Trying to figure out if there's any memory leak or not, because of some `wil::ResultException`s thrown after i close the app.

After I will be done with the basic triangle, I will implement Vulkan and DX12 aswell.
  • Loading branch information
wowvain-dev committed Mar 12, 2024
1 parent 0c3df73 commit 2a7521a
Show file tree
Hide file tree
Showing 23 changed files with 321 additions and 179 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build/
.vs/
build/
1 change: 1 addition & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions CMakeSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"configurations": [
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": ""
}
],
"environment": {
"CFLAGS": "/fsanitize=address",
"CXXFLAGS": "/fsanitize=address"
}
}
2 changes: 2 additions & 0 deletions Folder.DotSettings.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Albita/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
37 changes: 29 additions & 8 deletions include/types.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
#pragma once

#include <string>

namespace Vane {
enum BACKEND {
AUTO = 0,
DX11 = 1,
DX12 = 2,
VULKAN = 3,
OPENGL = 4
};
namespace Albita {
enum BACKEND {
DX11 = 0,
DX12 = 1,
VULKAN = 2,
OPENGL = 3
};

}
inline std::string STRING_FROM_BACKEND(BACKEND backend) {
std::string res;
switch (backend) {
case DX11:
res.append("DX11");
break;
case DX12:
res.append("DX12");
break;
case VULKAN:
res.append("VULKAN");
break;
case OPENGL:
res.append("OpenGL");
break;
}
return res;
}
}
}
2 changes: 2 additions & 0 deletions include/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#pragma once

43 changes: 39 additions & 4 deletions src/Engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,56 @@
#include "fmt/chrono.h"

namespace Vane {

Engine::Engine() {
initialize();
}

Engine::~Engine() {

}
Engine::~Engine() { shutdown(); }

void Engine::initialize() {
config.initialize();

std::cout << "\nAfter config.initizliaze()\n";

window = std::make_shared<Albita::Window>();

std::cout << "\nAfter making the window shared pointer.()\n";

const auto backend = config.getBackend();


if (window->createWindow("VaneEngine", 1350, 900, backend)) {
renderer = window->renderer;
}
else {
throw std::runtime_error("Failed to create window. Aborting");
}
}

void Engine::mainLoop() const {
bool windowRunning = true;

while (windowRunning) {
window->inputLoop(windowRunning);
renderer->clearBuffer(0.3f, 0.5f, 0.0f);
renderer->render();
}
}

void Engine::run() {
std::cout << "\nVane engine started...\n\tUSING:\t"
<< Albita::STRING_FROM_BACKEND(config.getBackend()) << '\n';
mainLoop();
std::cout << "\nStopped running...\n";
}

void Engine::shutdown() {
std::cout << "\nShutting down engine.";
if (renderer != nullptr) {
renderer.reset();
}
if (window != nullptr) {
window.reset();
}
}
}
77 changes: 51 additions & 26 deletions src/Engine/EngineConfig.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
//
// Created by wowva on 3/11/2024.
//

#include "EngineConfig.h"


namespace Vane {



bool EngineConfig::parseConfigFile() {
auto configPath = EngineConfig::configPath();
const std::string configPath = EngineConfig::configPath();

if (!std::filesystem::exists(configPath)) {
return false;
Expand All @@ -19,26 +12,39 @@ bool EngineConfig::parseConfigFile() {
try {
_table = toml::parse_file(configPath);
// std::cout << tbl << "\n";
} catch (const toml::parse_error& err) {
}
catch (const toml::parse_error& err) {
std::cerr << "Toml parsing error: \n" << err << "\n";
std::throw_with_nested(err);
}

return true;
}

BACKEND EngineConfig::getBackend() const {
Albita::BACKEND EngineConfig::getBackend() {
std::optional<std::string_view> opt_backend =
_table["engine"]["backend"].value<std::string_view>();
_table["engine"]["backend"].value<std::string_view>();

if (!opt_backend.has_value()) {
std::cerr << "Invalid `engine.backend` config value." << std::endl;
std::cerr << "Returning default value..." << std::endl;
std::cerr << "\nInvalid `engine.backend` config value.\n";
std::cerr << "Setting the file back to defaults.\n";

std::ofstream config(configPath().c_str());

return AUTO;
_table = defaultConfiguration();
config << _table;

config.close();

opt_backend = _table["engine"]["backend"].value<std::string_view>();
}

auto backend = opt_backend.value();
/// Even if we use `.value_or("VULKAN")`, it is impossible to get the `_or` value
/// since in the case that no value initially existed, we replaced the config with
/// the defaul valid one.
const std::string_view backend = opt_backend.value_or("VULKAN");

using namespace Albita;

if (backend == "DX11") {
return DX11;
Expand All @@ -53,17 +59,35 @@ BACKEND EngineConfig::getBackend() const {
return DX12;
}

std::cerr << "Invalid `engine.backend` config value." << std::endl;
std::cerr << "Returning default value..." << std::endl;
return AUTO;
std::cerr << "Invalid `engine.backend` config value.\n";
std::cerr << "Returning default value...\n";

std::ofstream config(configPath().c_str());

_table = defaultConfiguration();
config << _table;

config.close();

#ifdef _WIN32
return DX11;
#else
returne VULKAN;
#endif
}


toml::table EngineConfig::defaultConfiguration() {
auto defaults = toml::table {
{"engine", toml::table {
{"backend", "auto"}
}}
auto defaults = toml::table{
{
"engine", toml::table{
#ifdef _WIN32
{"backend", "DX11"}
#else
{"backend", "VULKAN"}
#endif
}
}
};

return defaults;
Expand All @@ -80,7 +104,8 @@ std::string EngineConfig::configPath() {
if (_dupenv_s(&buffer, &sz, "USERPROFILE") == 0 && buffer != nullptr) {
configPath.append(buffer);
free(buffer);
} else {
}
else {
throw std::runtime_error("Could not find %USERPROFILE% environment variable.");
}
#elif unix
Expand Down Expand Up @@ -116,12 +141,12 @@ void EngineConfig::initialize() {
}

void EngineConfig::saveChanges() const {
auto path = configPath();
const auto configPathStr = configPath();
const auto path = configPathStr.c_str();
std::ofstream config(path);

config << _table;

config.close();
}

} // Vane
} // Vane
14 changes: 11 additions & 3 deletions src/Engine/include/Engine.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
#pragma once
#include "EngineConfig.h"

namespace Vane {
#include <Renderer.h>
#include <Window.h>

namespace Vane {
class Engine {
public:
EngineConfig config;

private:
std::shared_ptr<Albita::Renderer> renderer;
std::shared_ptr<Albita::Window> window;

public:
Engine();
~Engine();

void initialize();
void run();
void shutdown();

private:
void mainLoop() const;
void initialize();
};

}
6 changes: 2 additions & 4 deletions src/Engine/include/EngineConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@
#endif

namespace Vane {

class EngineConfig {
toml::table _table;

public:
void initialize();
bool parseConfigFile();

BACKEND getBackend() const;
Albita::BACKEND getBackend();

private:
void saveChanges() const;
static toml::table defaultConfiguration();
static std::string configPath();
};

} // Vane
} // Vane
2 changes: 1 addition & 1 deletion src/Renderer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ add_library(Renderer STATIC
include/Renderer.h
include/GLRenderer.h
include/DX11Renderer.h
)
"DX12Renderer.cpp" "include/DX12Renderer.h" "include/VKRenderer.h")

include_directories(${SDL2_INCLUDE_DIRS})
include_directories(${PROJECT_SOURCE_DIR}/include)
Expand Down
Loading

0 comments on commit 2a7521a

Please sign in to comment.