-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds AssetStore, TextureAssetStore (#188)
* Adds AssetStore, TextureAssetStore Adds ctest verbose output
- Loading branch information
1 parent
3b11009
commit 41c2a3b
Showing
9 changed files
with
167 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#ifndef ASSETSTORE_H | ||
#define ASSETSTORE_H | ||
|
||
#include <jGL/jGL.h> | ||
#include <jGL/texture.h> | ||
|
||
#include <map> | ||
#include <filesystem> | ||
#include <memory> | ||
#include <iterator> | ||
|
||
namespace Hop::Util::Assets | ||
{ | ||
template <class T> | ||
class AssetStore | ||
{ | ||
public: | ||
|
||
AssetStore(std::filesystem::path root) | ||
: root(root) | ||
{} | ||
|
||
void scan() | ||
{ | ||
if (std::filesystem::is_directory(root)) | ||
{ | ||
scanDirectory(root); | ||
} | ||
}; | ||
|
||
virtual void load(std::filesystem::path assetPath) = 0; | ||
|
||
std::shared_ptr<T> get(std::filesystem::path relative_path) { return assets[relative_path]; } | ||
|
||
typename std::map<std::filesystem::path, std::shared_ptr<T>>::const_iterator begin() { return assets.cbegin(); } | ||
typename std::map<std::filesystem::path, std::shared_ptr<T>>::const_iterator end() { return assets.cend(); } | ||
|
||
protected: | ||
std::filesystem::path root; | ||
std::map<std::filesystem::path, std::shared_ptr<T>> assets; | ||
|
||
virtual bool matchesAssetType(std::filesystem::path file) = 0; | ||
|
||
void scanDirectory(std::filesystem::path dir) | ||
{ | ||
for (auto const & entry : std::filesystem::directory_iterator{dir}) | ||
{ | ||
if (entry.is_regular_file()) | ||
{ | ||
if (matchesAssetType(entry)) { load(entry); } | ||
} | ||
else | ||
{ | ||
scanDirectory(entry); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
class TextureAssetStore : public AssetStore<jGL::Texture> | ||
{ | ||
public: | ||
|
||
TextureAssetStore(std::filesystem::path root, std::unique_ptr<jGL::jGLInstance> & instance) | ||
: AssetStore<jGL::Texture>(root), instance(instance) | ||
{} | ||
|
||
void load(std::filesystem::path assetPath) | ||
{ | ||
std::filesystem::path relative = std::filesystem::relative(assetPath, root); | ||
assets[relative] = instance->createTexture | ||
( | ||
assetPath.generic_string(), | ||
jGL::Texture::Type::RGBA | ||
); | ||
}; | ||
|
||
protected: | ||
std::unique_ptr<jGL::jGLInstance> & instance; | ||
|
||
bool matchesAssetType(std::filesystem::path file) | ||
{ | ||
static std::vector<std::string> extensions = {".png"}; | ||
return std::find(extensions.begin(), extensions.end(), file.extension().generic_string()) != extensions.end(); | ||
} | ||
}; | ||
} | ||
#endif /* ASSETSTORE_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
add_subdirectory(scriptPack) | ||
add_subdirectory(scriptPack) | ||
add_subdirectory(assets) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
set(OUTPUT_NAME TestAssetStore) | ||
|
||
include_directories(.) | ||
|
||
if (WINDOWS) | ||
if (RELEASE) | ||
# launch as windows, not console app - so cmd does not open as well | ||
add_link_options(-mwindows) | ||
endif () | ||
else() | ||
# so nautilus etc recognise target as executable rather than .so | ||
add_link_options(-no-pie) | ||
endif() | ||
|
||
add_executable(${OUTPUT_NAME} "main.cpp") | ||
|
||
target_link_libraries(${OUTPUT_NAME} Hop) | ||
|
||
set_target_properties(${OUTPUT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${OUTPUT_NAME}") | ||
|
||
file(COPY "resource" DESTINATION "${CMAKE_BINARY_DIR}/${OUTPUT_NAME}/") | ||
|
||
if (TEST_SUITE) | ||
if(WINDOWS) | ||
add_test(NAME assetStore COMMAND "${CMAKE_CROSSCOMPILING_EMULATOR}" "${CMAKE_BINARY_DIR}/${OUTPUT_NAME}/${OUTPUT_NAME}.exe") | ||
else() | ||
add_test(NAME assetStore COMMAND "/${CMAKE_BINARY_DIR}/${OUTPUT_NAME}/${OUTPUT_NAME}") | ||
endif() | ||
set_tests_properties(assetStore PROPERTIES | ||
PASS_REGULAR_EXPRESSION "HEART\.png.*Pi\.png.*random\.png" | ||
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/${OUTPUT_NAME}/" | ||
) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include "main.h" | ||
#include <iostream> | ||
|
||
int main(int argc, char ** argv) | ||
{ | ||
|
||
jGL::DesktopDisplay::Config conf; | ||
conf.VULKAN = false; | ||
conf.COCOA_RETINA = false; | ||
|
||
jGL::DesktopDisplay display(glm::ivec2(1,1),"Test TextureAssetStore", conf); | ||
glewInit(); | ||
std::unique_ptr<jGL::jGLInstance> jGLInstance = std::move(std::make_unique<jGL::GL::OpenGLInstance>(display.getRes())); | ||
Hop::Util::Assets::TextureAssetStore textureStore(std::filesystem::path("resource"), jGLInstance); | ||
textureStore.scan(); | ||
for (auto tex : textureStore) | ||
{ | ||
std::cout << tex.first << " "; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#ifndef MAIN_H | ||
#define MAIN_H | ||
|
||
#include <iostream> | ||
#include <Util/assetStore.h> | ||
|
||
#include <jGL/OpenGL/openGLInstance.h> | ||
#include <jGL/jGL.h> | ||
|
||
#endif /* MAIN_H */ |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.