Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • gamedev/fggl
  • onuralpsezer/fggl
2 results
Show changes
Showing
with 1271 additions and 207 deletions
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 27/06/22.
//
#ifndef FGGL_ASSETS_MODULE_HPP
#define FGGL_ASSETS_MODULE_HPP
#include "fggl/modules/module.hpp"
#include "fggl/data/module.hpp"
#include "fggl/assets/packed/module.hpp"
#include "fggl/assets/manager.hpp"
#include "fggl/assets/loader.hpp"
namespace fggl::assets {
struct AssetFolders {
constexpr static const char *name = "fggl::assets::Folders";
constexpr static const std::array<modules::ServiceName, 2> provides = {
Loader::service,
AssetManager::service
};
constexpr static const std::array<modules::ServiceName, 2> depends = {
data::Storage::service,
CheckinAdapted::service
};
static bool factory(modules::ServiceName name, modules::Services &serviceManager);
};
} // namespace fggl::assets
#endif //FGGL_ASSETS_MODULE_HPP
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 19/11/22.
//
#ifndef FGGL_ASSETS_PACKED_ADAPTER_HPP
#define FGGL_ASSETS_PACKED_ADAPTER_HPP
#include <stack>
#include <ranges>
#include <algorithm>
#include "fggl/assets/packed/direct.hpp"
#include "fggl/data/storage.hpp"
#include "fggl/ds/graph.hpp"
namespace fggl::assets {
using ResourceType = util::OpaqueName<uint64_t, struct ResourceTypeStruct>;
constexpr ResourceType from_mime(const char* mime) {
return ResourceType::make( util::hash_fnv1a_64(mime) );
};
struct ResourceRecord {
std::filesystem::path m_path;
AssetID assetID;
ResourceType m_fileType;
AssetTypeID m_assetType;
std::string m_pack;
std::vector<AssetID> m_requires;
};
struct ManifestHeader {
uint64_t assetID;
uint64_t fileType;
uint64_t assetType;
uint64_t stringSize;
};
[[maybe_unused]]
inline bool NEEDS_CHECKIN(const std::filesystem::path&, MemoryBlock) {
debug::error("attempted to load asset which does not have a valid checkin yet");
return false;
}
/**
* Adapter for Raw Checkin.
*
* For debugging/development it's a pain to have to pack assets directly. Although its much slower, it can be useful
* to be able to load non-optimised formats at runtime. This adapter allows injecting these non-optimised formats
* into the production checkin system.
*/
class CheckinAdapted {
public:
constexpr const static auto service = modules::make_service("fggl::assets::checkin::debug");
using FilePredicate = std::function<AssetTypeID(const std::filesystem::path&)>;
using FileLoader = std::function<bool(const std::filesystem::path&, MemoryBlock& block)>;
using AssetMetadata = std::function<bool(const std::string& pack, const std::filesystem::path& packRoot, ResourceRecord&)>;
CheckinAdapted(data::Storage* storage, RawCheckin* checkSvc) : m_storage(storage), m_checkin(checkSvc) {};
// asset loading
void load(AssetID asset) {
auto& assetRef = m_files.at(asset);
auto& loader = m_loaders.at(assetRef.m_fileType);
MemoryBlock block;
auto result = loader(assetRef.m_path, block);
if ( !result ) {
return;
}
m_checkin->check(assetRef.assetID, assetRef.m_assetType, block);
}
inline bool exists(AssetID asset) const {
return m_files.find(asset) != m_files.end();
}
inline std::filesystem::path getPath(AssetID asset) const {
const auto& file = m_files.at(asset);
return file.m_path;
}
void loadOrder( AssetID id, std::queue<AssetID>& order) {
m_requires.getOrderPartialRev(id, order);
}
void discover( const char* packName, bool useCache = false, bool updateCache = true) {
if ( m_packs.contains(packName) ) {
return;
}
std::string packRoot = "packs/";
auto packDir = m_storage->resolvePath( data::StorageType::Data, packRoot + packName );
discover(packDir, useCache, updateCache);
}
// asset discovery
void discover( std::filesystem::path& packDir, bool useCache=true, bool updateCache=false ) {
// note we're loading this pack
auto packName = packDir.filename();
m_packs[packName].rootDir = packDir;
if ( useCache && has_manifest(packDir)) {
// check if we've cached the search
load_manifest(packName, packDir);
return;
}
std::vector<AssetID> packFiles;
std::stack< std::filesystem::path > paths;
paths.push(packDir);
while ( !paths.empty() ) {
auto path = paths.top();
paths.pop();
if ( std::filesystem::is_directory(path) ) {
std::ranges::for_each( std::filesystem::directory_iterator{path}, [&paths](auto& path) {
paths.push(path);
});
} else if ( std::filesystem::is_regular_file(path) ) {
process_file( path, packDir, packFiles );
}
}
// update the cache and remember what pack maps to what asset
if ( updateCache ) {
save_manifest(packDir.filename(), packDir, packFiles);
}
m_packs[ packDir.filename() ].assets = packFiles;
}
inline void setLoader(ResourceType type, const CheckinAdapted::FileLoader& loader, CheckinAdapted::FilePredicate predicate = nullptr) {
assert( loader != nullptr );
m_loaders[type] = loader;
if ( predicate != nullptr ) {
m_predicates[type] = predicate;
}
}
inline void setProcessor(ResourceType type, AssetMetadata metaFunc) {
m_metadata[type] = metaFunc;
}
inline const ResourceRecord& find(AssetID asset) const {
return m_files.at(asset);
}
inline std::filesystem::path getPackPath(const std::string& name) const {
auto& info = m_packs.at(name);
return info.rootDir;
}
private:
data::Storage* m_storage;
RawCheckin* m_checkin;
std::map<AssetID, ResourceRecord> m_files;
ds::DirectedGraph<AssetID> m_requires;
struct PackInfo {
std::filesystem::path rootDir;
std::vector<AssetID> assets;
};
std::map<ResourceType, FilePredicate> m_predicates;
std::map<ResourceType, FileLoader> m_loaders;
std::map<ResourceType, AssetMetadata> m_metadata;
std::map<std::string, PackInfo> m_packs;
void process_file(std::filesystem::path path, std::filesystem::path packDir, std::vector<AssetID> packFiles) {
for( auto& [rType, pred] : m_predicates ) {
// check the predicate, is this valid?
auto aType = pred(path);
if ( aType != INVALID_ASSET_TYPE ) {
// it was, so we can finish processing
auto packName = packDir.filename();
auto relPath = std::filesystem::relative(path, packDir);
ResourceRecord rr{
.m_path = path,
.assetID = make_asset_id_rt(packName, relPath.generic_string()),
.m_fileType = rType,
.m_assetType = aType,
.m_pack = packName,
.m_requires = {}
};
// processors (for stuff like dependencies)
auto metaProc = m_metadata.find(rType);
if ( metaProc != m_metadata.end() ) {
metaProc->second( packName, packDir, rr );
}
// store the resulting data
m_files[rr.assetID] = rr;
packFiles.push_back( rr.assetID );
m_requires.addEdges( rr.assetID, rr.m_requires );
debug::trace("discovered {} ({}) from pack '{}'", rr.assetID, relPath.c_str(), packName.c_str() );
break;
}
}
}
inline bool has_manifest(const std::string& packName) {
auto packManifest = m_storage->resolvePath( data::StorageType::Cache, packName + "_manifest.bin" );
return std::filesystem::exists(packManifest);
}
void load_manifest_entry(FILE* file, const std::string& packName, const std::filesystem::path& packRoot) {
// read our entry ( id, ftype, atype, pathLen )
ManifestHeader header{};
std::fread(&header, sizeof(ManifestHeader), 1, file);
// read the relative asset path
char* relPath = new char[header.stringSize + 1];
std::fread( relPath, sizeof(char), header.stringSize, file );
relPath[ header.stringSize + 1 ] = '\0';
// read the dependency list
uint64_t nDeps = 0;
std::fread( &nDeps, sizeof(uint64_t), 1, file);
uint64_t *deps = new uint64_t[nDeps];
std::fread( deps, sizeof(uint64_t), nDeps, file );
std::vector<AssetID> depList;
for ( uint64_t i = 0; i < nDeps; ++i ) {
depList.push_back( AssetID::make(deps[i]) );
}
// calculate and verify path
std::filesystem::path fullPath = packRoot / relPath;
delete[] relPath;
if ( !std::filesystem::exists(fullPath) ) {
debug::warning("pack manifest for {} contained invalid path {}", packName, fullPath.c_str());
return;
}
// entry seems valid, load it
ResourceRecord rr {
.m_path = fullPath,
.assetID = AssetID::make(header.assetID),
.m_fileType = ResourceType::make(header.fileType),
.m_assetType = AssetTypeID::make(header.assetType),
.m_pack = packName,
.m_requires = depList
};
m_files[ rr.assetID ] = rr;
m_packs[ packRoot.filename() ].assets.push_back( rr.assetID );
m_requires.addEdges( rr.assetID, depList );
debug::trace("discovered {} ({}) from pack {}", rr.assetID.get(), fullPath.c_str(), packRoot.filename().c_str() );
}
void load_manifest(const std::string& packName, const std::filesystem::path& packRoot) {
auto packManifest = m_storage->resolvePath( data::StorageType::Cache, packName + "_manifest.bin" );
if ( !std::filesystem::exists(packManifest) ) {
return;
}
// open the manifest file and start extracting entries
FILE* file = std::fopen(packManifest.c_str(), "r");
if ( file == nullptr ) {
debug::warning("error opening manifest: {}", packManifest.c_str());
return;
}
// read the number of entries
uint64_t entries{0};
std::fread(&entries, sizeof(uint64_t), 1, file);
for ( uint64_t i = 0; i < entries; ++i) {
load_manifest_entry(file, packName, packRoot);
}
std::fclose( file );
}
void save_manifest(const std::string& packName, const std::filesystem::path& packRoot, const std::vector<AssetID>& assets) {
auto packManifest = m_storage->resolvePath( data::StorageType::Cache, packName + "_manifest.bin", true);
FILE* file = std::fopen(packManifest.c_str(), "w");
if ( file == nullptr) {
debug::warning("error saving manifest {}, missing dir?", packManifest.c_str());
return;
}
const uint64_t entries{ assets.size() };
std::fwrite( &entries, sizeof(uint64_t), 1, file);
// write the entries
for ( uint64_t i = 0; i < entries; ++i ) {
auto& assetID = assets[i];
auto& assetRecord = m_files.at(assetID);
auto relPath = std::filesystem::relative(assetRecord.m_path, packRoot);
auto relPathStr = relPath.generic_string();
ManifestHeader mh {
.assetID = assetRecord.assetID.get(),
.fileType = assetRecord.m_fileType.get(),
.assetType = assetRecord.m_assetType.get(),
.stringSize = relPathStr.size()
};
std::fwrite( &mh, sizeof(ManifestHeader), 1, file );
std::fwrite( relPathStr.c_str(), sizeof(char), relPathStr.size(), file );
}
std::fclose(file);
}
};
}
#endif //FGGL_ASSETS_PACKED_ADAPTER_HPP
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 19/11/22.
//
#ifndef FGGL_ASSETS_PACKED_DIRECT_HPP
#define FGGL_ASSETS_PACKED_DIRECT_HPP
#include <functional>
#include <map>
#include "fggl/assets/types.hpp"
#include "fggl/util/safety.hpp"
#include "fggl/util/guid.hpp"
#include "fggl/modules/module.hpp"
/**
* Raw Checkin.
*
* This is a version of the checkin system where the check-in functions are shown a raw block of memory and its their
* job to parse and load something meaningful from that.
*/
namespace fggl::assets {
class RawCheckin {
public:
constexpr const static auto service = modules::make_service("fggl::assets::checkin");
using DecodeAndCheckFunc = std::function<void(AssetGUID, MemoryBlock& block)>;
void check(AssetID, AssetTypeID, MemoryBlock& block) const;
inline void check(int64_t id, uint64_t type, MemoryBlock& block ) const {
check( AssetID::make(id), AssetTypeID::make(type), block );
}
inline void setCheckin(AssetTypeID type, DecodeAndCheckFunc func) {
m_check[type] = func;
}
private:
std::map<AssetTypeID, DecodeAndCheckFunc> m_check;
};
}
#endif //FGGL_ASSETS_PACKED_DIRECT_HPP
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 27/06/22.
//
#ifndef FGGL_ASSETS_PACKED_MODULE_HPP
#define FGGL_ASSETS_PACKED_MODULE_HPP
#include "fggl/modules/module.hpp"
#include "fggl/data/module.hpp"
#include "fggl/assets/loader.hpp"
#include "fggl/assets/packed/adapter.hpp"
#include "fggl/assets/packed/direct.hpp"
namespace fggl::assets {
struct PackedAssets {
constexpr static const char *name = "fggl::assets::packed";
constexpr static const std::array<modules::ServiceName, 2> provides = {
RawCheckin::service,
CheckinAdapted::service
};
constexpr static const std::array<modules::ServiceName, 1> depends = {
data::Storage::service
};
static bool factory(modules::ServiceName name, modules::Services &serviceManager);
};
} // namespace fggl::assets
#endif //FGGL_ASSETS_PACKED_MODULE_HPP
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 19/11/22.
//
#ifndef FGGL_ASSETS_PACKED_PACKED_HPP
#define FGGL_ASSETS_PACKED_PACKED_HPP
#include <cstdint>
#include <cstdio>
#include <memory>
#include "fggl/assets/types.hpp"
#include "fggl/assets/packed/direct.hpp"
/**
* Packed file reader.
*
* Read assets stored as sequential [header,data] blocks. This reader does not care about dependencies, it assumes this
* was handled before storage (ie, asset dependencies are assumed to be stored before the asset that relies on them).
* The caller is also responsible for ensuring that assets in other archives are already loaded by the time the system
* assembles the composite assets into something usable.
*
* If either of these constraints are violated, the results are undefined.
*/
namespace fggl::assets {
struct Header {
uint64_t name;
uint64_t type;
std::size_t size;
};
bool read_header(std::FILE* stream, Header* header) {
constexpr auto headerSize = sizeof(Header);
auto readBytes = std::fread(header, headerSize, 1, stream);
return readBytes == headerSize;
}
bool read_data(std::FILE* stream, void* block, std::size_t size) {
auto readBytes = std::fread(block, size, 1, stream);
return readBytes == size;
}
void read_archive(RawCheckin* checkin, std::FILE* stream) {
while ( !std::feof(stream) ) {
Header header;
bool headRead = read_header(stream, &header);
if ( headRead && header.size != 0 ) {
// header has data
void* memBlock = std::malloc( header.size );
bool valid = read_data( stream, memBlock, header.size );
// read the data, check it in
if (valid) {
MemoryBlock block{
.data = (std::byte*)memBlock,
.size = header.size
};
checkin->check(header.name, header.type, block);
}
}
}
}
}
#endif //FGGL_ASSETS_PACKED_PACKED_HPP
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 03/07/22.
//
#ifndef FGGL_ASSETS_TYPES_HPP
#define FGGL_ASSETS_TYPES_HPP
#include <filesystem>
#include <variant>
#include <functional>
#include "fggl/util/safety.hpp"
#include "fggl/util/guid.hpp"
namespace fggl::assets {
using AssetType = util::OpaqueName<std::string_view, struct AssetTag>;
using AssetGUID = std::string;
using AssetPath = std::filesystem::path;
using AssetID = util::OpaqueName<uint64_t, struct AssetIDTag>;
template<unsigned L1, unsigned L2>
constexpr AssetID make_asset_id(const char (&pack)[L1], const char (&path)[L2]) {
auto hash = util::hash_fnv1a_64( util::cat( pack, ":", path ).c );
return AssetID::make( hash );
}
template<unsigned L1, unsigned L2, unsigned L3>
constexpr AssetID make_asset_id(const char (&pack)[L1], const char (&path)[L2], const char (&view)[L3]) {
auto hash = util::hash_fnv1a_64( util::cat( pack, ":", path, "[", view, "]").c );
return AssetID::make( hash );
}
AssetID make_asset_id_rt(const std::string &pack, const std::string &path, const std::string &view = "");
AssetID asset_from_user(const std::string &input, const std::string &pack = "core");
using AssetTypeID = util::OpaqueName<uint64_t, struct AssetTypeTag>;
constexpr auto INVALID_ASSET_TYPE = AssetTypeID::make(0);
constexpr AssetTypeID make_asset_type(const char* type) {
return AssetTypeID::make( util::hash_fnv1a_64(type) );
}
struct MemoryBlock {
std::byte *data;
std::size_t size;
template<typename T>
T* viewAs(std::size_t offset = 0) {
static_assert( std::is_standard_layout<T>::value );
return (T*)( data[offset] );
}
};
using AssetRefRaw = std::shared_ptr<void>;
struct LoaderContext {
std::string pack;
std::filesystem::path packRoot;
std::filesystem::path assetPath;
inline std::filesystem::path relParent() const {
return std::filesystem::relative( assetPath, packRoot ).parent_path();
}
inline assets::AssetID makeRef(const char* name) const {
return assets::make_asset_id_rt(pack, relParent() / name );
}
};
class Loader;
using Checkin = std::function<AssetRefRaw(Loader* loader, const AssetID &, const LoaderContext &, void* userPtr)>;
}
// formatter
template<> struct fmt::formatter<fggl::assets::AssetID> {
constexpr auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
return ctx.begin();
}
template <typename FormatContext>
auto format(const fggl::assets::AssetID & guid, FormatContext& ctx) const -> decltype(ctx.out()) {
#ifndef NDEBUG
return fmt::format_to(ctx.out(), "ASSET[{}]", guid_to_string( fggl::util::GUID::make( guid.get() ) ));
#else
return fmt::format_to(ctx.out(), "ASSET[{}]", guid.get());
#endif
}
};
#endif //FGGL_ASSETS_TYPES_HPP
/*
* ${license.title}
* Copyright (C) 2022 ${license.owner}
* ${license.mailto}
* This file is part of FGGL.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef FGGL_AUDIO_AUDIO_HPP
......@@ -23,6 +17,12 @@
#include <string>
#include "fggl/data/storage.hpp"
#include "fggl/modules/module.hpp"
#include "fggl/assets/module.hpp"
#include "fggl/assets/packed/module.hpp"
//! backend independent audio interface
namespace fggl::audio {
/**
......@@ -30,20 +30,47 @@ namespace fggl::audio {
*
* If the sampleCount is -1, the clip is invalid.
*/
struct AudioClip {
int channels;
int sampleRate;
int sampleCount;
short* data;
};
template<typename T>
struct AudioClip {
int channels = 0;
int sampleRate = 0;
int sampleCount = -1;
T *data = nullptr;
AudioClip() = default;
AudioClip(const AudioClip&) = delete;
inline ~AudioClip() {
std::free(data);
data = nullptr;
}
[[nodiscard]]
inline int size() const {
return sampleCount * sizeof(T);
}
};
using AudioClipShort = AudioClip<short>;
using AudioClipByte = AudioClip<char>;
constexpr auto ASSET_CLIP_SHORT = assets::make_asset_type("Audio:Clip:Short");
constexpr auto ASSET_CLIP_BYTE = assets::make_asset_type("Audio:Clip:Byte");
constexpr auto SERVICE_AUDIO_PLAYBACK = modules::make_service("fggl::audio::AudioService");
/**
*
* \ingroup services
*/
class AudioService {
public:
AudioService() = default;
virtual ~AudioService() = default;
constexpr static const modules::ServiceName service = SERVICE_AUDIO_PLAYBACK;
virtual void play(const assets::AssetGUID &asset, bool looping = false) = 0;
virtual void play(const AudioClipShort &clip, bool looping = false) = 0;
virtual void play(const std::string& filename, bool looping = false) = 0;
virtual void play(AudioClip& clip, bool looping = false) = 0;
virtual ~AudioService() = default;
};
} // namespace fggl::audio
......
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 27/06/22.
//
#ifndef FGGL_AUDIO_NULL_AUDIO_HPP
#define FGGL_AUDIO_NULL_AUDIO_HPP
#include "fggl/audio/audio.hpp"
namespace fggl::audio {
class NullAudioService : public AudioService {
public:
NullAudioService() = default;
~NullAudioService() override = default;
NullAudioService(NullAudioService&) = delete;
NullAudioService(NullAudioService&&) = delete;
NullAudioService& operator=(const NullAudioService&) = delete;
NullAudioService& operator=(NullAudioService&&) = delete;
void play(const std::string & /*filename*/, bool /*looping = false*/) override;
void play(const AudioClipShort & /*clip*/, bool /*looping = false*/) override;
};
//! A dummy audio module that does nothing
struct NullAudio {
constexpr static const char *name = "fggl::audio::NULL";
constexpr static const std::array<modules::ServiceName, 1> provides = {
SERVICE_AUDIO_PLAYBACK
};
constexpr static const std::array<modules::ServiceName, 0> depends = {};
bool factory(modules::ServiceName, modules::Services&);
};
} // namespace fggl::audio
#endif //FGGL_AUDIO_NULL_AUDIO_HPP
/*
* ${license.title}
* Copyright (C) 2022 ${license.owner}
* ${license.mailto}
* This file is part of FGGL.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
......@@ -29,14 +23,25 @@
#include <AL/alc.h>
#include "fggl/audio/audio.hpp"
#include "fggl/assets/manager.hpp"
#include "fggl/data/storage.hpp"
#include "fggl/math/types.hpp"
#include <string>
#include <iostream>
#include <memory>
//! Audio backed by openal-soft
namespace fggl::audio::openal {
constexpr uint32_t NULL_BUFFER_ID = 0;
assets::AssetRefRaw load_vorbis(assets::Loader* loader, const assets::AssetID &, const assets::LoaderContext &, void* userPtr);
bool load_vorbis_short(std::filesystem::path path, assets::MemoryBlock& block);
assets::AssetTypeID check_vorbis( const std::filesystem::path& path );
enum class AudioType {
MONO_8 = AL_FORMAT_MONO8,
MONO_16 = AL_FORMAT_MONO16,
......@@ -44,69 +49,78 @@ namespace fggl::audio::openal {
STEREO_16 = AL_FORMAT_STEREO16
};
static void checkError(std::string context) {
static void check_error(const std::string& context) {
auto code = alGetError();
if ( code == AL_NO_ERROR) {
if (code == AL_NO_ERROR) {
return;
}
// now we check the error message
std::string error = "unknown";
switch ( code ) {
case ALC_INVALID_DEVICE:
error = "Invalid Device";
switch (code) {
case ALC_INVALID_DEVICE: error = "Invalid Device";
break;
case ALC_INVALID_CONTEXT:
error = "Invalid Context";
case ALC_INVALID_CONTEXT: error = "Invalid Context";
break;
case ALC_INVALID_ENUM:
error = "Invalid enum";
case ALC_INVALID_ENUM: error = "Invalid enum";
break;
case ALC_INVALID_VALUE:
error = "Invalid value";
case ALC_INVALID_VALUE: error = "Invalid value";
break;
case ALC_OUT_OF_MEMORY:
error = "Out of memory";
case ALC_OUT_OF_MEMORY: error = "Out of memory";
break;
default:
error = "unknown error";
default: error = "unknown error";
}
std::cerr << "OpenAL error: " << context << ": " << error << std::endl;
debug::error("OpenAL error: context={}, error={}", context, error);
}
class AudioBuffer {
public:
AudioBuffer() : m_buffer(0) {
AudioBuffer() : m_buffer(NULL_BUFFER_ID) {
alGenBuffers(1, &m_buffer);
}
~AudioBuffer() {
alDeleteBuffers(1, &m_buffer);
}
inline void setData(AudioType type, void* data, ALsizei size, ALsizei frequency) {
alBufferData(m_buffer, (ALenum)type, data, size, frequency);
AudioBuffer(const AudioBuffer&) = delete;
AudioBuffer(const AudioBuffer&&) = delete;
AudioBuffer& operator=(const AudioBuffer&) = delete;
AudioBuffer& operator=(const AudioBuffer&&) = delete;
inline void setData(AudioType type, void *data, ALsizei size, ALsizei frequency) {
assert( m_buffer != 0 );
assert( data != nullptr );
alBufferData(m_buffer, (ALenum) type, data, size, frequency);
}
inline ALuint value() {
inline ALuint value() const {
return m_buffer;
}
private:
ALuint m_buffer;
ALuint m_buffer = 0;
};
class AudioSource {
public:
AudioSource() : m_source(0), m_splat() {
alGenSources(1, &m_source);
}
~AudioSource(){
~AudioSource() {
alDeleteSources(1, &m_source);
}
AudioSource(const AudioSource& source) = delete;
AudioSource(const AudioSource&& source) = delete;
AudioSource& operator=(const AudioSource&) = delete;
AudioSource& operator=(AudioSource&&) = delete;
inline void play() {
alSourcePlay( m_source );
alSourcePlay(m_source);
}
inline void stop() {
......@@ -114,40 +128,30 @@ namespace fggl::audio::openal {
}
inline void pause() {
alSourcePause( m_source );
alSourcePause(m_source);
}
inline void rewind() {
alSourceRewind( m_source );
alSourceRewind(m_source);
}
inline void play(AudioBuffer& buffer, bool looping) {
inline void play(AudioBuffer &buffer, bool looping) {
alSourcei(m_source, AL_BUFFER, buffer.value());
alSourcei( m_source, AL_LOOPING, looping ? AL_TRUE : AL_FALSE);
alSourcei(m_source, AL_LOOPING, looping ? AL_TRUE : AL_FALSE);
alSourcePlay(m_source);
}
inline void play(AudioClip& clip, bool looping) {
checkError("pre play");
AudioType format = clip.channels == 1 ? AudioType::MONO_8 : AudioType::STEREO_8;
m_splat.setData(format, clip.data, clip.sampleCount, clip.sampleRate);
checkError("saving to buffer");
alSourcei(m_source, AL_BUFFER, m_splat.value());
alSourcei( m_source, AL_LOOPING, looping ? AL_TRUE : AL_FALSE);
checkError("setting parameters");
play();
}
inline void play(const AudioClipShort &clip, bool looping = false);
inline void velocity(math::vec3& value) {
inline void velocity(math::vec3 &value) {
alSource3f(m_source, AL_VELOCITY, value.x, value.y, value.z);
}
inline void position(math::vec3& value) {
inline void position(math::vec3 &value) {
alSource3f(m_source, AL_POSITION, value.x, value.y, value.z);
}
void direction(math::vec3& value) {
void direction(math::vec3 &value) {
alSource3f(m_source, AL_DIRECTION, value.x, value.y, value.z);
}
......@@ -156,36 +160,36 @@ namespace fggl::audio::openal {
AudioBuffer m_splat;
};
class AudioServiceOAL : public AudioService {
public:
AudioServiceOAL() : m_device(alcOpenDevice(nullptr)) {
if ( m_device != nullptr ) {
explicit AudioServiceOAL(assets::AssetManager *assets) : m_device(alcOpenDevice(nullptr)), m_assets(assets) {
if (m_device != nullptr) {
m_context = alcCreateContext(m_device, nullptr);
alcMakeContextCurrent(m_context);
checkError("context setup");
check_error("context setup");
m_defaultSource = std::make_unique<AudioSource>();
checkError("default source setup");
check_error("default source setup");
}
}
~AudioServiceOAL() override {
if ( m_device != nullptr ) {
alcDestroyContext(m_context);
alcCloseDevice(m_device);
if (m_device != nullptr) {
release();
}
}
void play(const std::string& filename, bool looping = false) override;
void play(AudioClip& clip, bool looping = false) override;
void play(const assets::AssetGUID &filename, bool looping = false) override;
void play(const AudioClipShort &clip, bool looping = false) override;
private:
ALCdevice* m_device;
ALCcontext* m_context{nullptr};
ALCdevice *m_device;
ALCcontext *m_context{nullptr};
std::unique_ptr<AudioSource> m_defaultSource{nullptr};
};
assets::AssetManager* m_assets;
void release();
};
} // namespace fggl::audio::openal
......
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 27/06/22.
//
#ifndef FGGL_AUDIO_OPENAL_MODULE_HPP
#define FGGL_AUDIO_OPENAL_MODULE_HPP
#include <array>
#include <string>
#include "fggl/assets/module.hpp"
#include "fggl/assets/packed/module.hpp"
#include "fggl/audio/audio.hpp"
#include "fggl/audio/openal/audio.hpp"
namespace fggl::audio {
constexpr auto OGG_VORBIS = assets::AssetType::make("audio/vorbis");
constexpr auto RES_OGG_VORBIS = assets::from_mime("audio/vorbis");
//! an audio module which uses openal(-soft) as a backend.
struct OpenAL {
constexpr static const char *name = "fggl::audio::OpenAL";
constexpr static const std::array<modules::ServiceName, 1> provides = {
SERVICE_AUDIO_PLAYBACK
};
constexpr static const std::array<modules::ServiceName, 2> depends = {
assets::AssetManager::service,
assets::CheckinAdapted::service
};
static bool factory(modules::ServiceName name, modules::Services &serviceManager);
};
} // namespace fggl::audio
#endif //FGGL_AUDIO_OPENAL_MODULE_HPP
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 18/10/22.
//
#ifndef FGGL_DATA_ASSIMP_MODULE_HPP
#define FGGL_DATA_ASSIMP_MODULE_HPP
#include "fggl/modules/module.hpp"
#include "fggl/assets/loader.hpp"
#include "fggl/assets/packed/module.hpp"
#include "fggl/data/texture.hpp"
namespace fggl::data::models {
constexpr auto MODEL_PROVIDER = modules::make_service("fggl::data::Model");
constexpr auto ASSIMP_MODEL = assets::AssetType::make("model::assimp");
constexpr auto MIME_JPG = assets::from_mime("image/jpeg");
constexpr auto MIME_PNG = assets::from_mime("image/png");
constexpr auto MIME_OBJ = assets::from_mime("model/obj");
constexpr auto MIME_FBX = assets::from_mime("model/fbx");
constexpr auto MODEL_MULTI3D = assets::make_asset_type("model/multi3D");
constexpr auto TEXTURE_RGBA = assets::make_asset_type("texture/rgba");
// old-style loaders
assets::AssetRefRaw load_assimp_model(assets::Loader* loader, const assets::AssetID& guid, const assets::LoaderContext& data, void* userPtr);
assets::AssetRefRaw load_assimp_texture(assets::Loader* loader, const assets::AssetID& guid, const assets::LoaderContext& data, void* userPtr);
// new style loaders (textures)
bool load_tex_stb(const std::filesystem::path& filePath, assets::MemoryBlock& block);
assets::AssetTypeID is_tex_stb(const std::filesystem::path& filePath);
// new style loaders (models)
assets::AssetTypeID is_model_assimp(const std::filesystem::path& filePath);
bool extract_requirements(const std::string& packName, const std::filesystem::path& packRoot, assets::ResourceRecord& rr);
struct AssimpModule {
constexpr static const char *name = "fggl::data::Assimp";
constexpr static const std::array<modules::ServiceName, 1> provides = {
MODEL_PROVIDER
};
constexpr static const std::array<modules::ServiceName, 2> depends = {
assets::Loader::service,
assets::CheckinAdapted::service
};
static bool factory(modules::ServiceName service, modules::Services &serviceManager);
};
}
namespace fggl::data {
using AssimpLoader = models::AssimpModule;
}
#endif //FGGL_DATA_ASSIMP_MODULE_HPP
/*
* ${license.title}
* Copyright (C) 2022 ${license.owner}
* ${license.mailto}
* This file is part of FGGL.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
......@@ -35,15 +29,69 @@
namespace fggl::data {
static inline math::vec3 aiVec3ToFggl(aiVector3D vec) {
return {vec.x, vec.y, vec.z};
}
static inline math::vec2 aiVec2ToFggl(aiVector3D vec) {
return {vec.x, vec.y};
}
static void process_mesh(aiMesh *mesh, data::Mesh &meshRecord) {
// process vertices
std::vector<data::Mesh::IndexType> indexList;
for (auto idx = 0; idx < mesh->mNumVertices; ++idx) {
auto idxVal = meshRecord.pushVertex({
.posititon = aiVec3ToFggl(mesh->mVertices[idx]),
.normal = aiVec3ToFggl(mesh->mNormals[idx]),
.texPos = aiVec2ToFggl(mesh->mTextureCoords[0][idx])
});
indexList.push_back(idxVal);
}
if (mesh->HasFaces()) {
// if there is face data, that's our index list
for (auto face = 0; face < mesh->mNumFaces; ++face) {
auto &facePtr = mesh->mFaces[face];
meshRecord.pushIndex(indexList[facePtr.mIndices[0]]);
meshRecord.pushIndex(indexList[facePtr.mIndices[1]]);
meshRecord.pushIndex(indexList[facePtr.mIndices[2]]);
}
}
}
static void process_model(aiScene *scene, aiNode *node, data::Mesh &mesh) {
auto ptr = node->mMeshes;
for (auto j = 0; j < node->mNumMeshes; ++j) {
for (int meshCount = 0; node->mNumMeshes; ++meshCount) {
auto aiMeshIdx = node->mMeshes[meshCount];
data::Mesh mesh;
auto *aiMesh = scene->mMeshes[aiMeshIdx];
process_mesh(aiMesh, mesh);
}
}
// process child meshes
for (int i = 0; i < node->mNumChildren; ++i) {
auto *child = node->mChildren[i];
process_model(child);
}
}
template<>
bool fggl_deserialize(std::filesystem::path &data, StaticMesh *out) {
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile( data.c_str(), aiProcess_Triangulate | aiProcess_FlipUVs );
const aiScene *scene = importer.ReadFile(data.c_str(), aiProcess_Triangulate | aiProcess_FlipUVs);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
// check if the import worked
if (scene == nullptr || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
return false;
}
// figure out the meshes
process_model(scene->mRootNode);
//TODO implement the rest of this
return false;
}
......
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 20/11/2021.
//
#ifndef FGGL_HEIGHTMAP_H
#define FGGL_HEIGHTMAP_H
#ifndef FGGL_DATA_HEIGHTMAP_HPP
#define FGGL_DATA_HEIGHTMAP_HPP
#include <cstdint>
#include <array>
......@@ -40,7 +54,7 @@ namespace fggl::data {
return x * zMax + z;
}
void generateHeightMesh(const data::HeightMap *heights, data::Mesh &mesh);
void generateHeightMesh(const data::HeightMap &heights, data::Mesh &mesh);
}
#endif //FGGL_HEIGHTMAP_H
#endif //FGGL_DATA_HEIGHTMAP_HPP
#ifndef FGGL_DATA_MODEL_H
#define FGGL_DATA_MODEL_H
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef FGGL_DATA_MODEL_HPP
#define FGGL_DATA_MODEL_HPP
#include <tuple>
#include <vector>
......@@ -13,20 +27,39 @@ namespace fggl::data {
constexpr math::vec3 ILLEGAL_NORMAL{0.0F, 0.0F, 0.F};
constexpr math::vec3 DEFAULT_COLOUR{1.0F, 1.0F, 1.0F};
struct Vertex {
math::vec3 posititon;
math::vec3 normal;
math::vec3 colour;
math::vec3 normal = ILLEGAL_NORMAL;
math::vec3 colour = DEFAULT_COLOUR;
math::vec2 texPos;
inline static Vertex from_pos(math::vec3 pos) {
return {
pos,
ILLEGAL_NORMAL,
DEFAULT_COLOUR
DEFAULT_COLOUR,
{0.0F, 0.0F}
};
}
};
// comparison operators
inline bool operator<(const Vertex &lhs, const Vertex &rhs) {
return std::tie(lhs.posititon, lhs.normal, lhs.colour)
< std::tie(rhs.posititon, rhs.normal, rhs.colour);
}
inline bool operator==(const Vertex &lhs, const Vertex &rhs) {
return lhs.posititon == rhs.posititon
&& lhs.colour == rhs.colour
&& lhs.normal == rhs.normal;
}
inline bool operator!=(const Vertex &lhs, const Vertex &rhs) {
return !(lhs == rhs);
}
struct Vertex2D {
fggl::math::vec2 position;
fggl::math::vec3 colour;
......@@ -48,23 +81,6 @@ namespace fggl::data {
};
// comparison operators
inline bool operator<(const Vertex &lhs, const Vertex &rhs) {
return std::tie(lhs.posititon, lhs.normal, lhs.colour)
< std::tie(rhs.posititon, rhs.normal, rhs.colour);
}
inline bool operator==(const Vertex &lhs, const Vertex &rhs) {
return lhs.posititon == rhs.posititon
&& lhs.colour == rhs.colour
&& lhs.normal == rhs.normal;
}
inline bool operator!=(const Vertex &lhs, const Vertex &rhs) {
return !(lhs == rhs);
}
class Mesh {
public:
using IndexType = unsigned int;
......@@ -118,19 +134,19 @@ namespace fggl::data {
*/
IndexType indexOf(Vertex vert);
inline std::vector<Vertex> &vertexList() {
inline const std::vector<Vertex> &vertexList() const {
return m_verts;
}
inline std::size_t vertexCount() {
inline std::size_t vertexCount() const {
return m_verts.size();
}
inline std::vector<IndexType> &indexList() {
inline const std::vector<IndexType> &indexList() const {
return m_index;
}
inline std::size_t indexCount() {
inline std::size_t indexCount() const {
return m_index.size();
}
......@@ -146,28 +162,16 @@ namespace fggl::data {
struct StaticMesh {
constexpr static const char name[] = "StaticMesh";
constexpr static const util::GUID guid = util::make_guid("StaticMesh");
data::Mesh mesh;
std::string pipeline;
inline StaticMesh() : mesh(), pipeline() {}
inline StaticMesh() = default;
inline StaticMesh(const data::Mesh &aMesh, std::string aPipeline) :
mesh(aMesh), pipeline(std::move(aPipeline)) {}
};
class Model {
public:
Model() = default;
~Model() = default;
inline void append(const Mesh &mesh) {
m_meshes.push_back(mesh);
}
private:
std::vector<Mesh> m_meshes;
};
}
#endif
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 27/06/22.
//
#ifndef FGGL_DATA_MODULE_HPP
#define FGGL_DATA_MODULE_HPP
#include "fggl/modules/module.hpp"
#include "fggl/data/storage.hpp"
#include "fggl/platform/paths.hpp"
namespace fggl::data {
struct LocalStorage {
constexpr static const char *name = "fggl::data::Storage";
constexpr static const std::array<modules::ServiceName, 1> provides = {
SERVICE_STORAGE
};
constexpr static const std::array<modules::ServiceName, 0> depends = {};
static bool factory(modules::ServiceName service, modules::Services &serviceManager);
};
} // namespace fggl::data
#endif //FGGL_DATA_MODULE_HPP
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef FGGL_DATA_PROCEDURAL_HPP
#define FGGL_DATA_PROCEDURAL_HPP
#include "model.hpp"
#include "fggl/mesh/mesh.hpp"
namespace fggl::data {
......@@ -13,24 +28,24 @@ namespace fggl::data {
Mesh make_quad_xz();
// platonic solids
void make_tetrahedron(Mesh& mesh, const math::mat4& offset = OFFSET_NONE);
Mesh make_cube(Mesh &mesh, const math::mat4 &offset = OFFSET_NONE);
void make_octahedron(Mesh& mesh, const math::mat4& offset = OFFSET_NONE);
void make_icosahedron(Mesh& mesh, const math::mat4& offset = OFFSET_NONE);
void make_dodecahedron(Mesh& mesh, const math::mat4& offset = OFFSET_NONE);
void make_tetrahedron(Mesh &mesh, const math::mat4 &offset = OFFSET_NONE);
mesh::Mesh3D make_cube(mesh::Mesh3D &mesh, const math::mat4 &offset = OFFSET_NONE);
void make_octahedron(Mesh &mesh, const math::mat4 &offset = OFFSET_NONE);
void make_icosahedron(Mesh &mesh, const math::mat4 &offset = OFFSET_NONE);
void make_dodecahedron(Mesh &mesh, const math::mat4 &offset = OFFSET_NONE);
// useful shapes
void make_sphere_uv(Mesh &mesh, const math::mat4& offset = OFFSET_NONE);
void make_sphere_iso(Mesh &mesh, const math::mat4& offset = OFFSET_NONE);
void make_sphere_uv(Mesh &mesh, const math::mat4 &offset = OFFSET_NONE);
void make_sphere_iso(Mesh &mesh, const math::mat4 &offset = OFFSET_NONE);
// level block-out shapes
Mesh make_slope(Mesh &mesh, const math::mat4 &offset = OFFSET_NONE);
Mesh make_ditch(Mesh &mesh, const math::mat4 &offset = OFFSET_NONE);
Mesh make_point(Mesh &mesh, const math::mat4 &offset = OFFSET_NONE);
mesh::Mesh3D make_slope(mesh::Mesh3D &mesh, const math::mat4 &offset = OFFSET_NONE);
mesh::Mesh3D make_ditch(mesh::Mesh3D &mesh, const math::mat4 &offset = OFFSET_NONE);
mesh::Mesh3D make_point(mesh::Mesh3D &mesh, const math::mat4 &offset = OFFSET_NONE);
// other useful types people expect
void make_capsule(Mesh& mesh);
void make_sphere(Mesh &mesh, const math::mat4& offset = OFFSET_NONE, int stacks = 16, int slices = 16);
void make_capsule(Mesh &mesh);
void make_sphere(mesh::Mesh3D &mesh, const math::mat4 &offset = OFFSET_NONE, uint32_t stacks = 16U, uint32_t slices = 16U);
}
#endif
\ No newline at end of file
#ifndef FGGL_DATA_FS_H
#define FGGL_DATA_FS_H
namespace fggl::data {
class DataRegistry {
public:
DataRegistry();
~DataRegistry();
};
}
#endif
#ifndef FGGL_DATA_STORAGE_H
#define FGGL_DATA_STORAGE_H
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef FGGL_DATA_STORAGE_HPP
#define FGGL_DATA_STORAGE_HPP
#include <iostream>
#include <string>
#include <filesystem>
#include <utility>
#include <vector>
#include "fggl/debug/logging.hpp"
#include "fggl/modules/module.hpp"
#include "fggl/platform/paths.hpp"
namespace fggl::data {
template<typename T>
......@@ -14,30 +33,35 @@ namespace fggl::data {
template<typename T>
bool fggl_deserialize(std::filesystem::path &data, T *out);
enum StorageType { Data, User, Cache };
enum StorageType { Data, Config, Cache };
class Storage {
constexpr const auto SERVICE_STORAGE = modules::make_service("fggl::data::Storage");
class Storage {
public:
constexpr static auto service = SERVICE_STORAGE;
Storage(fggl::platform::EnginePaths paths) : m_paths(std::move(paths)) {}
template<typename T>
bool load(StorageType pool, const std::string &name, T *out) {
auto path = resolvePath(pool, name);
if (!std::filesystem::exists(path)) {
std::cerr << "Path " << path << " does not exist!" << std::endl;
debug::log(debug::Level::warning, "Attempted to load '{}', but it did not exist", path.string());
return false;
}
return fggl_deserialize<T>(path, out);
}
std::vector<std::filesystem::path> findResources(std::filesystem::path root, const std::string ext) {
if ( !std::filesystem::exists(root) ) {
if (!std::filesystem::exists(root)) {
return {};
}
std::vector<std::filesystem::path> paths;
for ( const auto& entry : std::filesystem::recursive_directory_iterator(root) ) {
if ( entry.is_regular_file() && entry.path().extension() == ext ) {
paths.push_back( entry );
for (const auto &entry : std::filesystem::recursive_directory_iterator(root)) {
if (entry.is_regular_file() && entry.path().extension() == ext) {
paths.push_back(entry);
}
}
return paths;
......@@ -45,28 +69,34 @@ namespace fggl::data {
template<typename T>
void save(StorageType pool, const std::string &name, const T *out) {
auto path = resolvePath(pool, name);
auto path = resolvePath(pool, name, true);
fggl_serialize<T>(path, out);
}
inline std::filesystem::path resolvePath(StorageType pool, const std::string &name) {
inline std::filesystem::path resolvePath(StorageType pool,
const std::string &name,
bool createParents = false) {
std::filesystem::path path;
switch (pool) {
case Data: path = std::filesystem::current_path() / "data";
case Data: path = fggl::platform::locate_data(m_paths, name);
break;
case User: path = "./user-data/";
case Config: path = fggl::platform::locate_config(m_paths, name);
break;
case Cache: path = "/tmp/fggl/";
case Cache: path = fggl::platform::locate_cache(m_paths, name);
break;
}
if (!std::filesystem::exists(path)) {
std::filesystem::create_directories(path);
if (createParents) {
if (!std::filesystem::exists(path.parent_path())) {
std::filesystem::create_directories(path.parent_path());
}
}
return path / name;
return path;
}
private:
fggl::platform::EnginePaths m_paths;
};
}
......
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
//
// Created by webpigeon on 22/10/22.
//
#ifndef FGGL_DATA_TEXTURE_HPP
#define FGGL_DATA_TEXTURE_HPP
#include "fggl/assets/module.hpp"
#include "fggl/math/types.hpp"
namespace fggl::data {
constexpr auto DATA_TEXTURE2D = assets::AssetType::make("gfx::texture");
struct Texture2D {
math::vec2i size;
int channels;
unsigned char* data;
Texture2D() = default;
Texture2D(const Texture2D& other) = delete;
inline ~Texture2D() {
delete data;
}
};
}
#endif //FGGL_DATA_TEXTURE_HPP
#ifndef FGGL_DEBUG_H
#define FGGL_DEBUG_H
/*
* This file is part of FGGL.
*
* FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with FGGL.
* If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef FGGL_DEBUG_DEBUG_H
#define FGGL_DEBUG_DEBUG_H
#include <string>
#include <memory>
......@@ -7,7 +21,7 @@
#include <functional>
#include <unordered_map>
#include "fggl/gfx/window.hpp"
#include "fggl/display/glfw/window.hpp"
namespace fggl::debug {
......@@ -20,14 +34,14 @@ namespace fggl::debug {
class DebugUI {
public:
explicit DebugUI(std::shared_ptr<gfx::GlfwWindow> &window);
explicit DebugUI(std::shared_ptr<display::glfw::Window> &window);
~DebugUI();
void frameStart();
void draw();
inline void addWindow(const std::string &name, DebugUIDraw window) {
m_windows[name] = DebugWindow{true, std::move(window) };
m_windows[name] = DebugWindow{true, std::move(window)};
}
inline void visible(bool state) {
......