Skip to content

Commit

Permalink
Small refactor, added new model
Browse files Browse the repository at this point in the history
  • Loading branch information
alekasm committed Dec 23, 2020
1 parent e62d012 commit 5e4d36c
Show file tree
Hide file tree
Showing 13 changed files with 476 additions and 444 deletions.
95 changes: 95 additions & 0 deletions SC2KRender/AssetLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "AssetLoader.h"
#include <map>
#include <string>
#include <fstream>
#include "Types.h"

std::map<std::wstring, Microsoft::WRL::ComPtr<ID3D11ShaderResourceView>>* AssetLoader::mresources;
std::map<std::wstring, CD3D11_TEXTURE2D_DESC>* AssetLoader::mdescriptions;
std::map<std::wstring, std::shared_ptr<DirectX::Model>>* AssetLoader::mmodels;
std::map<int32_t, std::wstring> AssetLoader::xbld_map;

void AssetLoader::LoadSprites(Microsoft::WRL::ComPtr<ID3D11Device1> device, std::wstring directory)
{
mresources = new std::map<std::wstring, Microsoft::WRL::ComPtr<ID3D11ShaderResourceView>>();
mdescriptions = new std::map<std::wstring, CD3D11_TEXTURE2D_DESC>();
if (!std::filesystem::is_directory(directory))
return;
std::vector<std::wstring> files;
FindFiles(directory, files);

for (const std::wstring& wfilename : files)
{
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> texture;
DirectX::CreateWICTextureFromFile(device.Get(), wfilename.c_str(), nullptr,
texture.ReleaseAndGetAddressOf());

Microsoft::WRL::ComPtr<ID3D11Resource> resource;
DirectX::CreateWICTextureFromFile(device.Get(), wfilename.c_str(),
resource.GetAddressOf(),
texture.ReleaseAndGetAddressOf());

Microsoft::WRL::ComPtr<ID3D11Texture2D> texture2d;
resource.As(&texture2d);
CD3D11_TEXTURE2D_DESC desc;
texture2d->GetDesc(&desc);

std::wstring key(wfilename);
key.erase(0, key.find_last_of('\\') + 1);
key.erase(key.find('.'), key.size() - 1);
mresources->operator[](key) = texture;
mdescriptions->operator[](key) = desc;
}
}

void AssetLoader::FindFiles(std::wstring directory, std::vector<std::wstring>& vector)
{
for (const std::filesystem::directory_entry& entry :
std::filesystem::recursive_directory_iterator(directory))
{
if (entry.is_regular_file())
{
vector.push_back(entry.path().wstring());
}
}
}

void AssetLoader::LoadModels(
Microsoft::WRL::ComPtr<ID3D11Device1> device,
const std::unique_ptr<DirectX::IEffectFactory>& effect,
std::wstring directory)
{
mmodels = new std::map<std::wstring, std::shared_ptr<DirectX::Model>>();
if (!std::filesystem::is_directory(directory))
return;

std::vector<std::wstring> files;
FindFiles(directory, files);

for (const std::wstring& wfilename : files)
{
std::wstring key(wfilename);
key.erase(0, key.find_last_of('\\') + 1);
key.erase(key.find('.'), key.size() - 1);
std::unique_ptr<DirectX::Model> model = DirectX::Model::CreateFromCMO(device.Get(), wfilename.c_str(), *effect);
mmodels->operator[](key) = std::move(model);
}
}

void AssetLoader::LoadCustomAssets(std::wstring file)
{
std::wifstream file_stream(file);
if (!file_stream.good())
return;
std::wstring line;
while (std::getline(file_stream, line))
{
auto it = line.find(',');
if (it == std::string::npos || line.empty())
continue;
std::wstring enum_value = line.substr(0, it);
std::wstring file_value = line.substr(it + 1, line.size());
int32_t xbld_value = _wtoi(enum_value.c_str());
xbld_map[xbld_value] = file_value;
}
}
71 changes: 5 additions & 66 deletions SC2KRender/AssetLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,15 @@
#include <iostream>
struct AssetLoader
{
static void FindFiles(std::wstring directory, std::vector<std::wstring>& vector)
{
for (const std::filesystem::directory_entry& entry :
std::filesystem::recursive_directory_iterator(directory))
{
if (entry.is_regular_file())
{
vector.push_back(entry.path().wstring());
}
}
}

static void FindFiles(std::wstring directory, std::vector<std::wstring>& vector);
static void LoadModels(
Microsoft::WRL::ComPtr<ID3D11Device1> device,
const std::unique_ptr<DirectX::IEffectFactory>& effect,
std::wstring directory)
{
mmodels = new std::map<std::wstring, std::shared_ptr<DirectX::Model>>();
if (!std::filesystem::is_directory(directory))
return;

std::vector<std::wstring> files;
FindFiles(directory, files);

for (const std::wstring& wfilename : files)
{
std::wstring key(wfilename);
key.erase(0, key.find_last_of('\\') + 1);
key.erase(key.find('.'), key.size() - 1);
std::unique_ptr<DirectX::Model> model = DirectX::Model::CreateFromCMO(device.Get(), wfilename.c_str(), *effect);
mmodels->operator[](key) = std::move(model);
}
}

static void LoadSprites(Microsoft::WRL::ComPtr<ID3D11Device1> device, std::wstring directory)
{
mresources = new std::map<std::wstring, Microsoft::WRL::ComPtr<ID3D11ShaderResourceView>>();
mdescriptions = new std::map<std::wstring, CD3D11_TEXTURE2D_DESC>();
if (!std::filesystem::is_directory(directory))
return;
std::vector<std::wstring> files;
FindFiles(directory, files);

for (const std::wstring& wfilename : files)
{
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> texture;
DirectX::CreateWICTextureFromFile(device.Get(), wfilename.c_str(), nullptr,
texture.ReleaseAndGetAddressOf());

Microsoft::WRL::ComPtr<ID3D11Resource> resource;
DirectX::CreateWICTextureFromFile(device.Get(), wfilename.c_str(),
resource.GetAddressOf(),
texture.ReleaseAndGetAddressOf());

Microsoft::WRL::ComPtr<ID3D11Texture2D> texture2d;
resource.As(&texture2d);
CD3D11_TEXTURE2D_DESC desc;
texture2d->GetDesc(&desc);

std::wstring key(wfilename);
key.erase(0, key.find_last_of('\\') + 1);
key.erase(key.find('.'), key.size() - 1);
mresources->operator[](key) = texture;
mdescriptions->operator[](key) = desc;
}
}
std::wstring directory);
static void LoadCustomAssets(std::wstring file);
static void LoadSprites(Microsoft::WRL::ComPtr<ID3D11Device1> device, std::wstring directory);
static std::map<std::wstring, Microsoft::WRL::ComPtr<ID3D11ShaderResourceView>>* mresources;
static std::map<std::wstring, CD3D11_TEXTURE2D_DESC>* mdescriptions;
static std::map<std::wstring, std::shared_ptr<DirectX::Model>>* mmodels;
static std::map<int32_t, std::wstring> xbld_map;
};
std::map<std::wstring, Microsoft::WRL::ComPtr<ID3D11ShaderResourceView>>* AssetLoader::mresources;
std::map<std::wstring, CD3D11_TEXTURE2D_DESC>* AssetLoader::mdescriptions;
std::map<std::wstring, std::shared_ptr<DirectX::Model>>* AssetLoader::mmodels;
29 changes: 0 additions & 29 deletions SC2KRender/AssetMaps.h

This file was deleted.

Loading

0 comments on commit 5e4d36c

Please sign in to comment.