Skip to content

Commit

Permalink
Initial setup
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackxSnow committed Mar 28, 2022
0 parents commit d6fb423
Show file tree
Hide file tree
Showing 34 changed files with 799 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Visual Studio files
/.vs
*.sln
*.vcxproj

# Intermediaries
**/obj
**/bin

*.obj
!SpaceSim/Resources/*.obj
*.idb
*.pdb

# Engine binaries
Engine.*.lib

# Static assimp binaries
lib/assimps*.lib
21 changes: 21 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[submodule "lib/src/bgfx"]
path = lib/src/bgfx
url = https://github.com/bkaradzic/bgfx.git
[submodule "lib/src/bimg"]
path = lib/src/bimg
url = [email protected]:bkaradzic/bimg.git
[submodule "lib/src/bx"]
path = lib/src/bx
url = [email protected]:bkaradzic/bx.git
[submodule "lib/src/glfw"]
path = lib/src/glfw
url = [email protected]:glfw/glfw.git
[submodule "lib/src/glm"]
path = lib/src/glm
url = [email protected]:g-truc/glm.git
[submodule "lib/src/assimp"]
path = lib/src/assimp
url = [email protected]:assimp/assimp.git
[submodule "lib/src/flecs"]
path = lib/src/flecs
url = [email protected]:SanderMertens/flecs.git
6 changes: 6 additions & 0 deletions Engine/Components/Camera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

struct Camera
{

};
31 changes: 31 additions & 0 deletions Engine/Data/Mesh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <glm/glm.hpp>
#include <Vector>

/// <summary>
/// Stores data for a mesh vertex.
/// </summary>
struct Vertex
{
glm::dvec3 Position;
glm::vec3 Normal;
glm::vec2 TexCoord;

Vertex(glm::dvec3 pos, glm::dvec3 normal, glm::dvec2 uv)
{
Position = pos;
Normal = (glm::vec3)normal;
TexCoord = (glm::vec2)uv;
}
Vertex()
{

}
};

struct MeshData
{
std::vector<Vertex> Vertices;
std::vector<int> Indices;
};
157 changes: 157 additions & 0 deletions Engine/Engine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#include <bx/bx.h>
#include <bgfx/bgfx.h>
#include <bgfx/platform.h>
#include <GLFW/glfw3.h>

#if BX_PLATFORM_LINUX
#define GLFW_EXPOSE_NATIVE_X11
#elif BX_PLATFORM_WINDOWS
#define GLFW_EXPOSE_NATIVE_WIN32
#elif BX_PLATFORM_OSX
#define GLFW_EXPOSE_NATIVE_COCOA
#endif

#include <GLFW/glfw3native.h>
#include <assimp/scene.h>
#include <assimp/Importer.hpp>
#include <assimp/postprocess.h>
#include <memory>

#include "utility/ConsoleLogging.h"
#include "Data/Mesh.h"
#include "utility/Conversion.h"
#include "Engine.h"

int WindowWidth = 1024;
int WindowHeight = 768;

static void glfw_error(int error, const char* description)
{
LogError(description, false);
}

static void glfw_key(GLFWwindow* window, int key, int scanCode, int action, int mods)
{
//TODO replace this function with an input manager
}

static GLFWwindow* InitialiseGLFW()
{
glfwSetErrorCallback(glfw_error);
if (!glfwInit())
{
LogError("Unable to initialise GLFW", true);
}
GLFWwindow* window = glfwCreateWindow(WindowWidth, WindowHeight, "SpaceSim", nullptr, nullptr);
if (!window)
{
LogError("Unable to create GLFW Window", true);
}
glfwSetKeyCallback(window, glfw_key);
return window;
}

static void InitialiseBGFX(GLFWwindow* window)
{
bgfx::renderFrame();
bgfx::Init init;

#if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
init.platformData.ndt = glfwGetX11Display();
init.platformData.nwh = (void*)(uintptr_t)glfwGetX11Window(window);
#elif BX_PLATFORM_OSX
init.platformData.nwh = glfwGetCocoaWindow(window);
#elif BX_PLATFORM_WINDOWS
init.platformData.nwh = glfwGetWin32Window(window);
#endif

init.resolution.width = (uint32_t)WindowWidth;
init.resolution.height = (uint32_t)WindowHeight;
init.resolution.reset = BGFX_RESET_VSYNC;
if (!bgfx::init(init))
{
LogError("Unable to intiialise BGFX", true);
}
}

static void HandleWindowResize(GLFWwindow* window, const bgfx::ViewId& clearView)
{
int oldWidth = WindowWidth, oldHeight = WindowHeight;
glfwGetWindowSize(window, &WindowWidth, &WindowHeight);

if (oldWidth != WindowWidth || oldHeight != WindowHeight)
{
bgfx::reset((uint32_t)WindowWidth, (uint32_t)WindowHeight, BGFX_RESET_VSYNC);
bgfx::setViewRect(clearView, 0, 0, bgfx::BackbufferRatio::Equal);
}
}

/// <summary>
/// Placeholder. Loads the first mesh in a scene from a given file.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
static std::shared_ptr<MeshData> LoadMesh(const std::string& path)
{
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(path, aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_JoinIdenticalVertices);

if (scene == nullptr)
{
LogError(importer.GetErrorString(), false);
return nullptr;
}

aiMesh* mesh = scene->mMeshes[0];


std::shared_ptr<MeshData> data = std::make_shared<MeshData>();
data->Vertices.resize(mesh->mNumVertices);
for (int i = 0; i < mesh->mNumVertices; i++)
{
//Vertex vert = Vertex(ToGLM(mesh->mVertices[i]),
// ToGLM(mesh->mNormals[i]),
// glm::dvec2(ToGLM(mesh->mTextureCoords[0][i])));

data->Vertices.emplace_back(
ToGLM(mesh->mVertices[i]),
ToGLM(mesh->mNormals[i]),
glm::dvec2(ToGLM(mesh->mTextureCoords[0][i])));
}

data->Indices.resize(mesh->mNumFaces * static_cast<size_t>(3));

for (int i = 0; i < mesh->mNumFaces; i++)
{
aiFace& face = mesh->mFaces[i];
for (int j = 0; j < face.mNumIndices; j++)
{
data->Indices.push_back(face.mIndices[j]);
}
}
return data;
}

int Loop(int argc, char** argv)
{
GLFWwindow* window = InitialiseGLFW();
InitialiseBGFX(window);

const bgfx::ViewId kClearView = 0;
bgfx::setViewClear(kClearView, BGFX_CLEAR_COLOR);
bgfx::setViewRect(kClearView, 0, 0, bgfx::BackbufferRatio::Equal);

// Test
auto cubeMesh = LoadMesh("Resources/Cube.obj");

while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
HandleWindowResize(window, kClearView);

bgfx::touch(kClearView);

bgfx::frame();
}
return 0;
}
3 changes: 3 additions & 0 deletions Engine/Engine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

int Loop(int argc, char** argv);
35 changes: 35 additions & 0 deletions Engine/Engine.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Components">
<UniqueIdentifier>{6B7DD516-5735-1764-C03C-F0BFAC13B254}</UniqueIdentifier>
</Filter>
<Filter Include="Data">
<UniqueIdentifier>{3F05847C-2B3C-850D-D428-6B10C03E010F}</UniqueIdentifier>
</Filter>
<Filter Include="utility">
<UniqueIdentifier>{79DE5C48-E5BD-DBE3-EED5-66BA5A344245}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Components\Camera.h">
<Filter>Components</Filter>
</ClInclude>
<ClInclude Include="Data\Mesh.h">
<Filter>Data</Filter>
</ClInclude>
<ClInclude Include="Engine.h" />
<ClInclude Include="utility\ConsoleLogging.h">
<Filter>utility</Filter>
</ClInclude>
<ClInclude Include="utility\Conversion.h">
<Filter>utility</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Engine.cpp" />
<ClCompile Include="utility\ConsoleLogging.cpp">
<Filter>utility</Filter>
</ClCompile>
</ItemGroup>
</Project>
80 changes: 80 additions & 0 deletions Engine/utility/ConsoleLogging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "ConsoleLogging.h"
#include <iostream>
#include <Windows.h>

#include <ctime>
#include <time.h>

HANDLE console;
bool isInitialised = false;

clog::LogFlags clog::LogSettings = clog::LogFlags::Time | clog::LogFlags::SourceInfo;

void Init()
{
if (!isInitialised)
{
console = GetStdHandle(STD_OUTPUT_HANDLE);
isInitialised = true;
}
}

void LayoutPrefix(int line, const std::string& func, const std::string& file, std::string& output)
{
if ((int)clog::LogSettings != 0)
{
if (clog::LogSettings & clog::LogFlags::Time)
{
std::time_t t = std::time(nullptr);

struct tm timeInfo;
localtime_s(&timeInfo, &t);

std::string buffer;
buffer.resize(20);
int len = strftime(&buffer[0], buffer.size(), "%X", &timeInfo);
while (len == 0) {
buffer.resize(buffer.size() * 2);
len = strftime(&buffer[0], buffer.size(), "%X", &timeInfo);
}

output += "[" + buffer +"]";
}
if (clog::LogSettings & clog::LogFlags::SourceInfo)
{
output += "(Line " + std::to_string(line) + " @ " + func + " @ " + file + ")";
}
output += " ";
}
}

void clog::Error(int line, std::string sourceFunc, std::string sourceFile, std::string message, bool throwException)
{
Init();
SetConsoleTextAttribute(console, FOREGROUND_RED);
std::string output = "[ERROR] ";
LayoutPrefix(line, sourceFunc, sourceFile, output);
std::cout << output << message << "\n";
if (throwException)
{
throw std::exception(message.c_str());
}
}

void clog::Warning(int line, std::string sourceFunc, std::string sourceFile, std::string message)
{
Init();
SetConsoleTextAttribute(console, FOREGROUND_RED | FOREGROUND_GREEN);
std::string output = "[WARN] ";
LayoutPrefix(line, sourceFunc, sourceFile, output);
std::cout << output << message << "\n";
}

void clog::Info(int line, std::string sourceFunc, std::string sourceFile, std::string message)
{
Init();
SetConsoleTextAttribute(console, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
std::string output = "[INFO] ";
LayoutPrefix(line, sourceFunc, sourceFile, output);
std::cout << output << message << "\n";
}
Loading

0 comments on commit d6fb423

Please sign in to comment.