Asset Index loader implemented (can not test yet)

Beginnings of asset pipeline designed
Gui_Panel_Refactor
Joeyrp 4 years ago
parent 5bd27e5f15
commit 03fb715622

@ -52,6 +52,8 @@ set(LUNARIUM_SRC
"src/scripting/scriptManager.cpp"
"src/scripting/coreAPI.cpp"
"src/assets/types/image.cpp"
"src/assets/types/asset.cpp"
"src/assets/loaders/binary/assetIndex.cpp"
)
# add the executable

@ -78,12 +78,12 @@ Game:
Editor:
✔ Come up with project directory structure @done (9/17/2021, 6:46:44 PM)
☐ Implement Run Mode interface class
✔ Implement Run Mode interface class @done (10/25/2021, 6:54:36 PM)
☐ Reference raw asset files in a "content" folder
☐ Platform independant file browsing
☐ Scan script files to make sure they don't overwrite globals
Raw Asset Loaders:
Raw Asset Importers:
- Need classes to load raw resource files for the editor
☐ Raw Resource loader interface class
☐ Raw Image loader class
@ -131,7 +131,7 @@ Assets:
Tester:
- A special class that is used to unit-test features of the engine
☐ Implement Run Mode interface class
✔ Implement Run Mode interface class @done (10/25/2021, 7:37:00 PM)
☐ Needs a timer to keep track of how long a test has run
☐ Main Tick method should use the timer to determine when to switch to the next test
☐ Add function for testing render to Texture

@ -0,0 +1,38 @@
<game name>.bin
This is the first file that will be loaded. <game name> will be set by with the
editor and the engine_state.xml file will direct the engine to this file.
FORMAT:
Engine ID, 8 bytes (should say "lunarium")
Engine version, 3 bytes (major, minor, patch)
index.dat
This is an index of all of the assets in the game project. Each asset will be
listed by name (and/or ID) followed by the file name and offset position within
the file where the asset can be found (ex. the asset with ID 6 might be found in file assets0.dat at byte offset 93846).
FORMAT:
Number of assets, 4 bytes
[REPEAT]
Asset ID, 4 bytes
Name Size, 2 bytes
Asset Name, <Name Size> bytes (Must be null-terminated)
Asset Type, 2 bytes
File Name Size, 2 bytes
Asset File, <File Name Size> bytes (Must be null-terminated)
Asset Offset, 4 bytes
[REPEAT - for all assets]
asset0.dat
This is an asset pack file. There could be many of these files and when there are
more than one the 0 in the file name will increase on each file (assets0.dat, assets1.dat, assets2.dat etc..)
FORMAT:
Asset ID, 4 bytes
Asset Data Size, 4 bytes
Asset Data, <Data Size> bytes
[REPEAT FOR EACH ASSET]

@ -16,6 +16,7 @@ Project directory structure (this should be auto-generated by the editor when cr
└─ release/
├── (This directory is generated by the editor when building the project)
├── game_name.exe - (The non-editor version of the engine renamed to the game name)
├── engine_state.xml - (configured to load the game from the data/ directory)
└── data/ - (The data folder generated by the asset compiler, contains all the assets for the game in binary formats)

@ -10,10 +10,10 @@ IF not exist build/ (
IF "%~1" == "r" (
cmake --build build/ --target ALL_BUILD --config Release
xcopy /y test_data\lunarium_state.xml build\Release\
xcopy /y test_data\engine_state.xml build\Release\
) ELSE (
cmake --build build/ --target ALL_BUILD --config Debug
xcopy /y test_data\lunarium_state.xml build\Debug\
xcopy /y test_data\engine_state.xml build\Debug\
)
:END

@ -3,4 +3,4 @@
# ex. scripts/build.sh
make -C build/
cp test_data/lunarium_state.xml build/lunarium_state.xml
cp test_data/engine_state.xml build/engine_state.xml

@ -0,0 +1,55 @@
/******************************************************************************
* File - assetManager.h
* Author - Joey Pollack
* Date - 2021/10/25 (y/m/d)
* Mod Date - 2021/10/25 (y/m/d)
* Description - Manages loading/unloading of all data/assets
******************************************************************************/
#ifndef ASSET_MANAGER_H_
#define ASSET_MANAGER_H_
#include <vector>
#include <map>
#include <string>
namespace lunarium
{
class Asset;
class AssetManager
{
public:
static AssetManager& GetInstance();
OpRes Initialize();
void Shutdown();
// Used for preloading a list of assets
OpRes LoadAssets(std::vector<std::string> names);
Asset* GetAsset(std::string name);
void ReturnAsset(Asset* pAsset)
void UnloadAsset(std::string name);
private:
static AssetManager* mpInstance;
struct AssetBox
{
Asset* asset;
int references;
};
// Assets stored by name
std::map<std::string, AssetBox> mAssets;
private:
AssetManager();
AssetManager(const AssetManager&) = delete;
AssetManager& operator=(const AssetManager&) = delete;
};
}
#endif // ASSET_MANAGER_H_

@ -0,0 +1,106 @@
/******************************************************************************
* File - assetIndex.h
* Author - Joey Pollack
* Date - 2021/10/25 (y/m/d)
* Mod Date - 2021/10/25 (y/m/d)
* Description - Loads and stores the index.dat file. Allows querying
* the asset index by asset ID or name.
******************************************************************************/
#include "assetIndex.h"
#include <utils/binaryFileBuffer.h>
namespace lunarium
{
AssetIndex* AssetIndex::mpInstance = nullptr;
AssetIndex::AssetIndex()
{
}
AssetIndex& AssetIndex::GetInstance()
{
if (!mpInstance)
{
mpInstance = new AssetIndex;
}
return *mpInstance;
}
void AssetIndex::FreeInstance()
{
for (auto iter = mpInstance->mAssets.begin(); iter != mpInstance->mAssets.end(); iter++)
{
delete iter->second;
}
mpInstance->mAssets.clear();
mpInstance->mAssetsByName.clear();
delete mpInstance;
mpInstance = nullptr;
}
OpRes AssetIndex::LoadIndex(const char* filename)
{
BinaryFileBuffer buffer;
if (!buffer.LoadFile(filename))
{
return OpRes::Fail("AssetIndex::LoadIndex - unable to load file: %s", filename);
}
int32_t num_assets = 0;
buffer.Read((char*) &num_assets, 4);
char str_buf[512];
for (int i = 0; i < num_assets; i++)
{
AssetInfo* pAI = new AssetInfo;
buffer.Read((char*) &pAI->ID, 4);
int16_t name_size = 0;
buffer.Read((char*) &name_size, 2);
buffer.Read(str_buf, name_size);
pAI->Name = str_buf;
buffer.Read((char*) &pAI->Type, 2);
buffer.Read((char*) &name_size, 2);
buffer.Read(str_buf, name_size);
pAI->File = str_buf;
buffer.Read((char*) &pAI->Offset, 4);
mAssets[pAI->ID] = pAI;
mAssetsByName[pAI->Name] = pAI;
}
buffer.Unload();
return OpRes::OK();
}
const AssetIndex::AssetInfo* AssetIndex::GetAssetInfo(int id) const
{
auto res = mAssets.find(id);
if (res != mAssets.end())
{
return res->second;
}
return nullptr;
}
const AssetIndex::AssetInfo* AssetIndex::GetAssetInfo(const char* name) const
{
auto res = mAssetsByName.find(name);
if (res != mAssetsByName.end())
{
return res->second;
}
return nullptr;
}
}

@ -0,0 +1,54 @@
/******************************************************************************
* File - assetIndex.h
* Author - Joey Pollack
* Date - 2021/10/25 (y/m/d)
* Mod Date - 2021/10/25 (y/m/d)
* Description - Loads and stores the index.dat file. Allows querying
* the asset index by asset ID or name.
******************************************************************************/
#ifndef ASSET_INDEX_H_
#define ASSET_INDEX_H_
#include <string>
#include <map>
#include <assets/types/asset.h>
#include <utils/opRes.h>
#include <utils/types.h>
namespace lunarium
{
class AssetIndex
{
public:
struct AssetInfo
{
int32_t ID;
std::string Name;
AssetType Type;
std::string File;
int32_t Offset;
};
public:
static AssetIndex& GetInstance();
static void FreeInstance();
OpRes LoadIndex(const char* filename);
const AssetInfo* GetAssetInfo(int id) const;
const AssetInfo* GetAssetInfo(const char* name) const;
private:
static AssetIndex* mpInstance;
std::map<int, AssetInfo*> mAssets;
std::map<std::string, AssetInfo*> mAssetsByName;
AssetIndex();
AssetIndex(const AssetIndex&) = delete;
AssetIndex& operator=(const AssetIndex&) = delete;
};
}
#endif // ASSET_INDEX_H_

@ -0,0 +1,29 @@
/******************************************************************************
* File - asset.cpp
* Author - Joey Pollack
* Date - 2021/10/25 (y/m/d)
* Mod Date - 2021/10/25 (y/m/d)
* Description - The base class for all asset type files
******************************************************************************/
#include "asset.h"
namespace lunarium
{
Asset::Asset(AssetType type)
: mType(type)
{
}
Asset::~Asset()
{
}
AssetType Asset::GetType() const
{
return mType;
}
}

@ -0,0 +1,34 @@
/******************************************************************************
* File - asset.h
* Author - Joey Pollack
* Date - 2021/10/25 (y/m/d)
* Mod Date - 2021/10/25 (y/m/d)
* Description - The base class for all asset type files
******************************************************************************/
#ifndef ASSET_H_
#define ASSET_H_
namespace lunarium
{
enum AssetType
{
ASSET_TYPE_IMAGE,
ASSET_TYPE_SCRIPT,
ASSET_TYPE_AUDIO,
ASSET_TYPE_UNKNOWN,
};
class Asset
{
public:
Asset(AssetType type = AssetType::ASSET_TYPE_UNKNOWN);
virtual ~Asset() = 0;
AssetType GetType() const;
private:
AssetType mType;
};
}
#endif // ASSET_H_

@ -14,7 +14,7 @@ namespace lunarium
{
Image::Image()
: mRawData(nullptr), mRawDataSize(0), mWidth(0), mHeight(0)
: Asset(AssetType::ASSET_TYPE_IMAGE), mRawData(nullptr), mRawDataSize(0), mWidth(0), mHeight(0)
{
}

@ -11,11 +11,12 @@
#define IMAGE_H_
#include <graphics/definitions.h>
#include "asset.h"
namespace lunarium
{
class OglGraphics;
class Image
class Image : public Asset
{
friend OglGraphics;

@ -128,14 +128,14 @@ namespace lunarium
Logger::Log(LogCategory::CORE, LogLevel::INFO, "Running Lunarium version %s", Version::GetVersion().ToString().c_str());
// Attempt to load the engine state file. This file should be placed in the same directory as the lunarium program.
if (Failed(State::CreateFromFile("lunarium_state.xml", mState)))
if (Failed(State::CreateFromFile("engine_state.xml", mState)))
{
Logger::Log(LogCategory::CORE, LogLevel::WARNING, "Unable to load state file: lunarium_state.xml. Loading default state.");
Logger::Log(LogCategory::CORE, LogLevel::WARNING, "Unable to load state file: engine_state.xml. Loading default state.");
mState = State::CreateDefault();
}
else
{
Logger::Log(LogCategory::CORE, LogLevel::INFO, "Loaded state file: lunarium_state.xml");
Logger::Log(LogCategory::CORE, LogLevel::INFO, "Loaded state file: engine_state.xml");
}
// RUN MODE

@ -9,8 +9,12 @@
#include "opRes.h"
#include <stdarg.h>
namespace lunarium
{
char OpRes::Buffer[OpRes::BUFFER_SIZE];
bool OpRes::operator==(const OpRes& rhs)
{
return this->Type == rhs.Type;
@ -26,9 +30,14 @@ namespace lunarium
return { ResultType::OK, "The operation succeeded" };
}
OpRes OpRes::Fail(const char* why)
OpRes OpRes::Fail(const char* why, ...)
{
return { ResultType::FAIL, why };
va_list args;
va_start(args, why);
vsprintf(OpRes::Buffer, why, args);
va_end(args);
return { ResultType::FAIL, OpRes::Buffer };
}
bool IsOK(OpRes&& res)

@ -32,7 +32,11 @@ namespace lunarium
static OpRes OK();
static OpRes Fail(const char* why);
static OpRes Fail(const char* why, ...);
private:
static const int BUFFER_SIZE = 512;
static char Buffer[OpRes::BUFFER_SIZE];
};
bool IsOK(OpRes&& res);

Loading…
Cancel
Save