Skip to content

Commit

Permalink
Adds AssetStore, TextureAssetStore (#188)
Browse files Browse the repository at this point in the history
* Adds AssetStore, TextureAssetStore
  Adds ctest verbose output
  • Loading branch information
Jerboa-app authored Jul 29, 2024
1 parent 3b11009 commit 41c2a3b
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 4 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ jobs:
working-directory: ${{github.workspace}}/build
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest
run: |
export DISPLAY=:99
sudo Xvfb :99 -screen 0 800x600x24 &
sleep 5
MESA_GL_VERSION_OVERRIDE=3.3 ctest --output-on-failure --verbose
- name: buildArtifact
uses: actions/upload-artifact@v4
Expand Down Expand Up @@ -154,7 +158,12 @@ jobs:
working-directory: ${{github.workspace}}/build
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: MESA_GL_VERSION_OVERRIDE=3.3 ctest --exclude-regex Vulkan
run: |
cp *.dll TestAssetStore/
export DISPLAY=:99
sudo Xvfb :99 -screen 0 800x600x24 &
sleep 5
MESA_GL_VERSION_OVERRIDE=3.3 ctest --output-on-failure --verbose --exclude-regex Vulkan
- name: buildArtifact
uses: actions/upload-artifact@v4
Expand Down Expand Up @@ -204,7 +213,7 @@ jobs:
working-directory: ${{github.workspace}}/build
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: MESA_GL_VERSION_OVERRIDE=3.3 ctest --exclude-regex Vulkan
run: MESA_GL_VERSION_OVERRIDE=3.3 ctest --output-on-failure --verbose --exclude-regex Vulkan --exclude-regex assetStore

- name: buildArtifact
uses: actions/upload-artifact@v4
Expand Down
88 changes: 88 additions & 0 deletions include/Util/assetStore.h
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 */
3 changes: 2 additions & 1 deletion tests/regression/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
add_subdirectory(scriptPack)
add_subdirectory(scriptPack)
add_subdirectory(assets)
33 changes: 33 additions & 0 deletions tests/regression/assets/CMakeLists.txt
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()
22 changes: 22 additions & 0 deletions tests/regression/assets/main.cpp
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;
}
10 changes: 10 additions & 0 deletions tests/regression/assets/main.h
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.
Binary file added tests/regression/assets/resource/texture/Pi.png
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.

0 comments on commit 41c2a3b

Please sign in to comment.