diff --git a/assets/games/helloworld/CMakeLists.txt b/assets/games/helloworld/CMakeLists.txt new file mode 100644 index 00000000..164a2df4 --- /dev/null +++ b/assets/games/helloworld/CMakeLists.txt @@ -0,0 +1,6 @@ +# Copyright (c) 2023 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +include("${CMAKE_CURRENT_LIST_DIR}/textures/CMakeLists.txt") \ No newline at end of file diff --git a/assets/games/helloworld/textures/CMakeLists.txt b/assets/games/helloworld/textures/CMakeLists.txt new file mode 100644 index 00000000..4c0ef183 --- /dev/null +++ b/assets/games/helloworld/textures/CMakeLists.txt @@ -0,0 +1,8 @@ +# Copyright (c) 2023 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +tool_texture(texture_border + FILE=${CMAKE_CURRENT_LIST_DIR}/texture_test.png +) \ No newline at end of file diff --git a/assets/games/helloworld/textures/texture_test.png b/assets/games/helloworld/textures/texture_test.png new file mode 100644 index 00000000..e6400c51 Binary files /dev/null and b/assets/games/helloworld/textures/texture_test.png differ diff --git a/src/dawn/CMakeLists.txt b/src/dawn/CMakeLists.txt index cd0cd9b6..ef24eb1d 100644 --- a/src/dawn/CMakeLists.txt +++ b/src/dawn/CMakeLists.txt @@ -8,6 +8,7 @@ target_link_libraries(${DAWN_TARGET_NAME} PUBLIC glm freetype + archive_static ) # Includes @@ -18,7 +19,7 @@ target_include_directories(${DAWN_TARGET_NAME} # Subdirs add_subdirectory(assert) -# add_subdirectory(asset) +add_subdirectory(asset) # add_subdirectory(audio) add_subdirectory(component) add_subdirectory(display) diff --git a/src/dawn/asset/AssetDataLoader.cpp b/src/dawn/asset/AssetDataLoader.cpp new file mode 100644 index 00000000..ba959cd6 --- /dev/null +++ b/src/dawn/asset/AssetDataLoader.cpp @@ -0,0 +1,200 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "assert/assert.hpp" +#include "AssetDataLoader.hpp" +#include "util/Math.hpp" + +using namespace Dawn; + +ssize_t assetDataLoaderArchiveRead( + struct archive *archive, + void *d, + const void **buffer +) { + assertNotNull(archive, "Archive is NULL!"); + assertNotNull(d, "Data is NULL!"); + assertNotNull(buffer, "Buffer is NULL!"); + AssetDataLoader *loader = (AssetDataLoader*)d; + + *buffer = loader->buffer; + size_t read = fread( + loader->buffer, 1, ASSET_LOADER_BUFFER_SIZE, loader->assetArchiveFile + ); + if(ferror(loader->assetArchiveFile)) return ARCHIVE_FATAL; + return read; +} + +int64_t assetDataLoaderArchiveSeek( + struct archive *archive, + void *d, + int64_t offset, + int32_t whence +) { + assertNotNull(archive, "Archive is NULL!"); + assertNotNull(d, "Data is NULL!"); + assertTrue(offset > 0, "Offset must be greater than 0!"); + AssetDataLoader *loader = (AssetDataLoader*)d; + int32_t ret = fseek(loader->assetArchiveFile, offset, whence); + assertTrue(ret == 0, "Failed to seek!"); + return ftell(loader->assetArchiveFile); +} + +int32_t assetDataLoaderArchiveOpen(struct archive *a, void *d) { + assertNotNull(a, "Archive is NULL!"); + assertNotNull(d, "Data is NULL!"); + AssetDataLoader *loader = (AssetDataLoader*)d; + + int32_t ret = fseek(loader->assetArchiveFile, 0, SEEK_SET); + assertTrue(ret == 0, "Failed to seek to start of file!"); + return ARCHIVE_OK; +} + +int32_t assetDataLoaderArchiveClose(struct archive *a, void *d) { + assertNotNull(a, "Archive is NULL!"); + assertNotNull(d, "Data is NULL!"); + return assetDataLoaderArchiveOpen(a, d); +} + +// // // // // // // // // // // // // // // // // // // // // // // // // // // + +AssetDataLoader::AssetDataLoader(std::string fileName) : fileName(fileName) { + assertTrue( + fileName.size() > 0, + "IAssetDataLoader::IAssetDataLoader: fileName must be greater than 0" + ); +} + +void AssetDataLoader::open() { + assertNull(this->assetArchiveFile, "AssetDataLoader::open: File is already open"); + assertNull(this->assetArchive, "AssetDataLoader::open: Archive is already open"); + assertNull(this->assetArchiveEntry, "AssetDataLoader::open: Entry is already open"); + + this->assetArchiveFile = this->openAssetArchiveFile(); + assertNotNull(this->assetArchiveFile, "AssetDataLoader::open: Failed to open archive file!"); + + // Open archive reader + assetArchive = archive_read_new(); + assertNotNull(assetArchive, "AssetDataLoader::open: Failed to create archive reader"); + + // Set up the reader + archive_read_support_format_tar(assetArchive); + + // Open reader + archive_read_set_open_callback(assetArchive, &assetDataLoaderArchiveOpen); + archive_read_set_read_callback(assetArchive, &assetDataLoaderArchiveRead); + archive_read_set_seek_callback(assetArchive, &assetDataLoaderArchiveSeek); + archive_read_set_close_callback(assetArchive, &assetDataLoaderArchiveClose); + archive_read_set_callback_data(assetArchive, this); + + int32_t ret = archive_read_open1(assetArchive); + assertTrue(ret == ARCHIVE_OK, "AssetDataLoader::open: Failed to open archive!"); + position = 0; + + // Iterate over each file to find the one for this asset loader. + while(archive_read_next_header(assetArchive, &assetArchiveEntry) == ARCHIVE_OK) { + const char_t *headerFile = (char_t*)archive_entry_pathname(assetArchiveEntry); + if(std::string(headerFile) == this->fileName) return; + int32_t ret = archive_read_data_skip(assetArchive); + assertTrue(ret == ARCHIVE_OK, "AssetDataLoader::open: Failed to skip data!"); + } + + assertUnreachable("AssetDataLoader::open: Failed to find file!"); +} + +int32_t AssetDataLoader::close() { + assertNotNull(this->assetArchiveFile, "AssetDataLoader::close: File is NULL"); + assertNotNull(this->assetArchive, "AssetDataLoader::close: Archive is NULL!"); + assertNotNull(this->assetArchiveEntry, "AssetDataLoader::close: Entry is NULL!"); + + // Close the archive + int32_t ret = archive_read_free(this->assetArchive); + assertTrue(ret == ARCHIVE_OK, "AssetDataLoader::close: Failed to close archive!"); + + this->assetArchive = nullptr; + this->assetArchiveEntry = nullptr; + + // Close the file + int32_t res = fclose(this->assetArchiveFile); + this->assetArchiveFile = nullptr; + return res; +} + +size_t AssetDataLoader::read(uint8_t *buffer, size_t size) { + assertNotNull(buffer, "Buffer is NULL!"); + assertTrue(size > 0, "Size must be greater than 0!"); + assertNotNull(this->assetArchive, "assetRead: Archive is NULL!"); + assertNotNull(this->assetArchiveEntry, "assetRead: Entry is NULL!"); + + ssize_t read = archive_read_data(this->assetArchive, buffer, size); + this->position += read; + + if(read == ARCHIVE_FATAL) { + assertUnreachable(archive_error_string(this->assetArchive)); + } + + assertTrue(read != ARCHIVE_RETRY, "assetRead: Failed to read data (RETRY)!"); + assertTrue(read != ARCHIVE_WARN, "assetRead: Failed to read data (WARN)!"); + + return read; +} + +size_t AssetDataLoader::readUntil( + uint8_t *buffer, + const size_t maxSize, + const char_t delimiter +) { + size_t totalRead = this->read(buffer, maxSize); + size_t i = 0; + while(i < totalRead) { + if(buffer[i] == delimiter) break; + i++; + } + buffer[i] = '\0'; + return i; +} + +size_t AssetDataLoader::getSize() { + assertTrue(this->assetArchiveEntry != nullptr, "AssetDataLoader::getSize: Entry is NULL!"); + assertTrue(archive_entry_size_is_set(assetArchiveEntry), "assetGetSize: Entry size is not set!"); + return archive_entry_size(assetArchiveEntry); +} + +size_t AssetDataLoader::skip(size_t n) { + assertTrue(n >= 0, "AssetDataLoader::skip: Byte count must be greater than 0."); + + uint8_t dumpBuffer[ASSET_LOADER_BUFFER_SIZE]; + size_t skipped = 0; + size_t n2, n3; + while(n != 0) { + n2 = Math::min(n, ASSET_LOADER_BUFFER_SIZE); + n3 = this->read(dumpBuffer, n2); + assertTrue(n3 == n2, "AssetDataLoader::skip: Failed to skip bytes!"); + n -= n3; + } + + return skipped; +} + +size_t AssetDataLoader::setPosition(const size_t position) { + assertTrue(position >= 0, "Position must be greater than or equal to 0"); + this->rewind(); + return this->skip(position); +} + +void AssetDataLoader::rewind() { + // TODO: See if I can optimize this + this->close(); + this->open(); +} + +size_t AssetDataLoader::getPosition() { + assertNotNull(this->assetArchiveFile, "AssetDataLoader::getPosition: File is not open!"); + return this->position; +} + +AssetDataLoader::~AssetDataLoader() { + if(this->assetArchiveFile != nullptr) this->close(); +} \ No newline at end of file diff --git a/src/dawn/asset/AssetDataLoader.hpp b/src/dawn/asset/AssetDataLoader.hpp new file mode 100644 index 00000000..61b7941d --- /dev/null +++ b/src/dawn/asset/AssetDataLoader.hpp @@ -0,0 +1,166 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "dawnlibs.hpp" + +extern "C" { + #include + #include +} + +#define ASSET_LOADER_BUFFER_SIZE 32768 + +/** + * Method invoked by the libarchive internals to read bytes from the archive + * file pointer. + * + * @param archive Archive requesting the read. + * @param data Data pointer passed to the archive. + * @param buffer Pointer to where the buffer pointer should be stored. + * @return Count of bytes read. + */ +ssize_t assetDataLoaderArchiveRead( + struct archive *archive, + void *data, + const void **buffer +); + +/** + * Method invoked by the libarchive internals to seek the archive file pointer. + * + * @param archive Archive requesting the seek. + * @param data Data pointer passed to the archive. + * @param offset Offset to seek to. + * @param whence Whence to seek from. + * @return The new offset. + */ +int64_t assetDataLoaderArchiveSeek( + struct archive *archive, + void *data, + int64_t offset, + int32_t whence +); + +/** + * Method invoked by the libarchive internals to open the archive file pointer. + * + * @param archive Archive requesting the open. + * @param data Data pointer passed to the archive. + * @return 0 if success, otherwise for failure. + */ +int32_t assetDataLoaderArchiveOpen(struct archive *a, void *data); + +/** + * Method invoked by the libarchive internals to close the archive file pointer. + * + * @param archive Archive requesting the close. + * @param data Data pointer passed to the archive. + * @return 0 if success, otherwise for failure. + */ +int32_t assetDataLoaderArchiveClose(struct archive *a, void *data); + +namespace Dawn { + class AssetDataLoader { + protected: + struct archive *assetArchive = nullptr; + struct archive_entry *assetArchiveEntry = nullptr; + size_t position; + std::string fileName; + + public: + uint8_t buffer[ASSET_LOADER_BUFFER_SIZE]; + FILE *assetArchiveFile = nullptr; + + /** + * Unimplemented method intended to be implemented by the platform that + * will be used to request a File pointer to the asset. + * + * @return Pointer to the opened asset archive. + */ + FILE * openAssetArchiveFile(); + + /** + * Create a new asset loader. Asset Loaders can be used to load data from + * a file in a myriad of ways. + * + * @param fileName File name of the asset that is to be loaded. + */ + AssetDataLoader(std::string filename); + + /** + * Platform-centric method to open a file buffer to an asset. + */ + void open(); + + /** + * Closes the previously ppened asset. + * @return 0 if successful, otherwise false. + */ + int32_t close(); + + /** + * Read bytes from buffer. + * @param buffer Pointer to a ubyte array to buffer data into. + * @param size Length of the data buffer (How many bytes to read). + * @return The count of bytes read. + */ + size_t read(uint8_t *buffer, size_t size); + + /** + * Reads bytes from the buffer until a given delimiter is found. Returned + * position will be the index of the delimiter within the buffer. + * + * @param buffer Buffer to read into. + * @param maxSize Maximum size of the buffer. + * @param delimiter Delimiter to read until. + * @return The count of bytes read. + */ + size_t readUntil( + uint8_t *buffer, + const size_t maxSize, + const char_t delimiter + ); + + /** + * Get the size of the asset. + * @return The size of the asset in bytes. + */ + size_t getSize(); + + /** + * Skips the read head forward to a given position. + * + * @param n Count of bytes to progress the read head by. + * @return Count of bytes progressed. + */ + size_t skip(size_t n); + + /** + * Rewind the read head to the beginning of the file. + */ + void rewind(); + + /** + * Sets the absolute position of the read head within the buffer of the + * file. + * + * @param absolutePosition Absolute position to set the read head to. + */ + size_t setPosition(const size_t absolutePosition); + + /** + * Returns the current position of the read head. + * + * @return The current read head position. + */ + size_t getPosition(); + + /** + * Cleanup the asset loader. + */ + virtual ~AssetDataLoader(); + }; +} \ No newline at end of file diff --git a/src/dawn/asset/AssetLoader.cpp b/src/dawn/asset/AssetLoader.cpp new file mode 100644 index 00000000..e2e3a3a6 --- /dev/null +++ b/src/dawn/asset/AssetLoader.cpp @@ -0,0 +1,17 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "AssetLoader.hpp" +#include "assert/assert.hpp" + +using namespace Dawn; + +AssetLoader::AssetLoader(const std::string name) : name(name) { + assertTrue(name.size() > 0, "Asset::Asset: Name cannot be empty"); +} + +AssetLoader::~AssetLoader() { + this->loaded = false; +} \ No newline at end of file diff --git a/src/dawn/asset/AssetLoader.hpp b/src/dawn/asset/AssetLoader.hpp new file mode 100644 index 00000000..563904fa --- /dev/null +++ b/src/dawn/asset/AssetLoader.hpp @@ -0,0 +1,41 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "dawnlibs.hpp" + +namespace Dawn { + class AssetLoader { + public: + const std::string name; + bool_t loaded = false; + + /** + * Create an abstract Asset object. + * + * @param name Name of the asset. + */ + AssetLoader(const std::string name); + + /** + * Virtual function that will be called by the asset manager on a + * synchronous basis. This will only trigger if the blocks are false and + * the loaded is also false. + */ + virtual void updateSync() = 0; + + /** + * Virtual function called by the asset manager asynchronously every tick. + * This will only trigger if blocks are false and the loaded state is also + * false. + */ + virtual void updateAsync() = 0; + + /** + * Dispose the asset item. + */ + virtual ~AssetLoader(); + }; +} \ No newline at end of file diff --git a/src/dawn/asset/AssetManager.cpp b/src/dawn/asset/AssetManager.cpp new file mode 100644 index 00000000..97d07c9b --- /dev/null +++ b/src/dawn/asset/AssetManager.cpp @@ -0,0 +1,46 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "AssetManager.hpp" +#include "loaders/TextureLoader.hpp" + +using namespace Dawn; + +void AssetManager::init() { + +} + +void AssetManager::update() { + auto itPending = pendingAssetLoaders.begin(); + while(itPending != pendingAssetLoaders.end()) { + auto loader = *itPending; + loader->updateSync(); + loader->updateAsync(); + if(loader->loaded) { + finishedAssetLoaders.push_back(loader); + itPending = pendingAssetLoaders.erase(itPending); + } else { + itPending++; + } + } +} + +template<> +std::shared_ptr AssetManager::get( + const std::string filename +) { + // TODO: Check for existing loader + + // Create new loader + std::shared_ptr loader = std::make_shared( + filename + ); + pendingAssetLoaders.push_back(std::static_pointer_cast(loader)); + return loader->sharedTexture; +} + +AssetManager::~AssetManager() { + +} \ No newline at end of file diff --git a/src/dawn/asset/AssetManager.hpp b/src/dawn/asset/AssetManager.hpp new file mode 100644 index 00000000..3a878b1b --- /dev/null +++ b/src/dawn/asset/AssetManager.hpp @@ -0,0 +1,35 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "dawnlibs.hpp" +#include "asset/AssetLoader.hpp" + +namespace Dawn { + class AssetManager final { + private: + std::vector> pendingAssetLoaders; + std::vector> finishedAssetLoaders; + + public: + /** + * Initializes this asset manager so it can begin accepting assets. + */ + void init(); + + /** + * Updates the asset manager. + */ + void update(); + + template + std::shared_ptr get(const std::string filename); + + /** + * Dispose the asset manager, and all attached assets. + */ + ~AssetManager(); + }; +} \ No newline at end of file diff --git a/src/dawn/asset/CMakeLists.txt b/src/dawn/asset/CMakeLists.txt new file mode 100644 index 00000000..bc04aeec --- /dev/null +++ b/src/dawn/asset/CMakeLists.txt @@ -0,0 +1,15 @@ +# Copyright (c) 2022 Dominic Msters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Sources +target_sources(${DAWN_TARGET_NAME} + PRIVATE + AssetLoader.cpp + AssetDataLoader.cpp + AssetManager.cpp +) + +# Subdirs +add_subdirectory(loaders) \ No newline at end of file diff --git a/src/dawn/asset/loaders/CMakeLists.txt b/src/dawn/asset/loaders/CMakeLists.txt new file mode 100644 index 00000000..b4f5f2b4 --- /dev/null +++ b/src/dawn/asset/loaders/CMakeLists.txt @@ -0,0 +1,10 @@ +# Copyright (c) 2022 Dominic Msters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Sources +target_sources(${DAWN_TARGET_NAME} + PRIVATE + TextureLoader.cpp +) \ No newline at end of file diff --git a/src/dawn/asset/loaders/TextureLoader.cpp b/src/dawn/asset/loaders/TextureLoader.cpp new file mode 100644 index 00000000..c48dc3c0 --- /dev/null +++ b/src/dawn/asset/loaders/TextureLoader.cpp @@ -0,0 +1,45 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "TextureLoader.hpp" +#include "assert/assert.hpp" + +using namespace Dawn; + +TextureLoader::TextureLoader(const std::string name) : + AssetLoader(name), + loader(name + ".texture"), + state(TextureLoaderLoadState::INITIAL) +{ + sharedTexture = std::make_shared(); +} + +void TextureLoader::updateAsync() { + if(this->state != TextureLoaderLoadState::INITIAL) return; + this->state = TextureLoaderLoadState::ASYNC_LOADING; + this->loader.open(); + + // Read in the header. + uint8_t buffer[TEXTURE_LOADER_HEADER_SIZE]; + this->loader.read(buffer, TEXTURE_LOADER_HEADER_SIZE); + size_t pos = 0; + + // Read Version + pos += this->loader.readUntil(buffer, TEXTURE_LOADER_HEADER_SIZE, '|'); + std::string version = std::string((char*)buffer, pos); + assertTrue(version == "DT_2.00", "Invalid Texture Version!"); + + // Read Texture Width + this->loader.setPosition(pos); + pos += this->loader.readUntil(buffer, TEXTURE_LOADER_HEADER_SIZE, '|'); +} + +void TextureLoader::updateSync() { + +} + +TextureLoader::~TextureLoader() { + this->sharedTexture = nullptr; +} \ No newline at end of file diff --git a/src/dawn/asset/loaders/TextureLoader.hpp b/src/dawn/asset/loaders/TextureLoader.hpp new file mode 100644 index 00000000..83e78bf4 --- /dev/null +++ b/src/dawn/asset/loaders/TextureLoader.hpp @@ -0,0 +1,64 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "asset/AssetLoader.hpp" +#include "asset/AssetDataLoader.hpp" +#include "display/Texture.hpp" + +#define TEXTURE_LOADER_HEADER_SIZE 256 + +namespace Dawn { + enum class TextureLoaderHeaderParseState { + VERSION, + WIDTH, + HEIGHT, + FORMAT, + WRAP_MODE_X, + WRAP_MODE_Y, + FILTER_MODE_MIN, + FILTER_MODE_MAG, + DONE + }; + + enum class TextureLoaderLoadState { + INITIAL, + ASYNC_LOADING, + }; + + class TextureLoader : public AssetLoader { + protected: + AssetDataLoader loader; + // int32_t width = -1, height = -1; + // uint8_t *colors; + enum TextureLoaderLoadState state; + // enum TextureFormat format; + // enum TextureWrapMode wrapModeX; + // enum TextureWrapMode wrapModeY; + // enum TextureFilterMode filterModeMin; + // enum TextureFilterMode filterModeMag; + + public: + std::shared_ptr sharedTexture; + std::weak_ptr weakTexture; + + /** + * Constructs a texture asset loader. You should instead use the parent + * asset managers' abstracted load method + * + * @param name File name asset to load, omitting the extension. + */ + TextureLoader(const std::string name); + + void updateSync() override; + void updateAsync() override; + + /** + * Dispose / Cleanup the texture asset. Will also dispose the underlying + * texture itself. + */ + ~TextureLoader(); + }; +} diff --git a/src/dawn/component/display/IRenderableComponent.hpp b/src/dawn/component/display/IRenderableComponent.hpp index 655dce5d..3ed34ff7 100644 --- a/src/dawn/component/display/IRenderableComponent.hpp +++ b/src/dawn/component/display/IRenderableComponent.hpp @@ -108,6 +108,7 @@ namespace Dawn { void upload() override { for(auto &pair : textures) { + if(!pair.second->isReady()) continue; pair.second->bind(pair.first); } shader->upload(); diff --git a/src/dawn/display/ITexture.hpp b/src/dawn/display/ITexture.hpp index 36330fa9..506aed57 100644 --- a/src/dawn/display/ITexture.hpp +++ b/src/dawn/display/ITexture.hpp @@ -75,7 +75,7 @@ namespace Dawn { * * @return True if ready, otherwise false. */ - // virtual bool_t isReady() = 0; + virtual bool_t isReady() = 0; /** * Buffer pixel data onto the GPU. Pixel buffering is rather costly so diff --git a/src/dawn/display/shader/IShaderStage.hpp b/src/dawn/display/shader/IShaderStage.hpp index b76cfa75..fae14b94 100644 --- a/src/dawn/display/shader/IShaderStage.hpp +++ b/src/dawn/display/shader/IShaderStage.hpp @@ -7,7 +7,7 @@ #include "dawnlibs.hpp" namespace Dawn { - enum ShaderStageType { + enum class ShaderStageType { VERTEX, FRAGMENT, // COMPUTE diff --git a/src/dawn/environment/Environment.hpp b/src/dawn/environment/Environment.hpp new file mode 100644 index 00000000..39c62e61 --- /dev/null +++ b/src/dawn/environment/Environment.hpp @@ -0,0 +1,15 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "dawnlibs.hpp" + +namespace Dawn { + class Environment { + public: + + }; + extern Dawn::Environment environment; +} diff --git a/src/dawn/game/Game.cpp b/src/dawn/game/Game.cpp index a45401e7..cee08d64 100644 --- a/src/dawn/game/Game.cpp +++ b/src/dawn/game/Game.cpp @@ -22,6 +22,8 @@ void Game::init() { } void Game::update() { + this->assetManager.update(); + if(nextFrameScene) { nextFrameScene->stage(); currentScene = nextFrameScene; diff --git a/src/dawn/game/Game.hpp b/src/dawn/game/Game.hpp index e796e995..5730d5d4 100644 --- a/src/dawn/game/Game.hpp +++ b/src/dawn/game/Game.hpp @@ -8,6 +8,7 @@ #include "display/RenderHost.hpp" #include "input/InputManager.hpp" #include "time/TimeManager.hpp" +#include "asset/AssetManager.hpp" namespace Dawn { class Scene; @@ -21,6 +22,7 @@ namespace Dawn { RenderHost renderHost; InputManager inputManager; TimeManager timeManager; + AssetManager assetManager; /** * Constructs the game instance, does not initialize anything. diff --git a/src/dawn/util/Math.hpp b/src/dawn/util/Math.hpp index 2221d11d..0888bf9d 100644 --- a/src/dawn/util/Math.hpp +++ b/src/dawn/util/Math.hpp @@ -12,110 +12,111 @@ namespace Dawn { class Math final { - /** - * Returns the largest of the two provided int32 numbers. - * - * @param left Left number to get the largest of. - * @param right Right number to get the largest of. - * @return The larger of the two numbers - */ - template - static T max(T left, T right) { - return left < right ? right : left; - } + public: + /** + * Returns the largest of the two provided int32 numbers. + * + * @param left Left number to get the largest of. + * @param right Right number to get the largest of. + * @return The larger of the two numbers + */ + template + static T max(T left, T right) { + return left < right ? right : left; + } - /** - * Returns the smallest of two provided int32 numbers. - * - * @param left Left number to get the smallest of. - * @param right Right number to get the smallest of. - * @return Smaller of the two numbers. - */ - template - static T min(T left, T right) { - return left < right ? left : right; - } + /** + * Returns the smallest of two provided int32 numbers. + * + * @param left Left number to get the smallest of. + * @param right Right number to get the smallest of. + * @return Smaller of the two numbers. + */ + template + static T min(T left, T right) { + return left < right ? left : right; + } - /** - * Returns the input value, constrained between the min and max values, so that - * the value cannot underceed the min, and cannot exceed the max. - * - * @param val Value to get the clamp for. - * @param min Minimum clamping value. - * @param max Maximum clamping value. - * @return The value, or the closest clamped value. - */ - template - static T clamp(T val, T min, T max) { - return mathMin(mathMax(val, min), max); - } + /** + * Returns the input value, constrained between the min and max values, so that + * the value cannot underceed the min, and cannot exceed the max. + * + * @param val Value to get the clamp for. + * @param min Minimum clamping value. + * @param max Maximum clamping value. + * @return The value, or the closest clamped value. + */ + template + static T clamp(T val, T min, T max) { + return mathMin(mathMax(val, min), max); + } - /** - * Returns the absolute value (the non-negative representation of) for the given - * int32 number.Abs values will be -value if value < 0. - * - * @param value Value to get the absolute value for. - * @return The absolute value (-value if value < 0) - */ - template - static T abs(T value) { - return value < 0 ? -value : value; - } + /** + * Returns the absolute value (the non-negative representation of) for the given + * int32 number.Abs values will be -value if value < 0. + * + * @param value Value to get the absolute value for. + * @return The absolute value (-value if value < 0) + */ + template + static T abs(T value) { + return value < 0 ? -value : value; + } - /** - * Returns the modulous a result for b. Works for floating point numbers. - * - * @param a Number to modulo against. (a % b) - * @param b Number to modulo with. (a % b) - * @returns The modulo result. - */ - template - static inline T mod(T value, T modulo) { - return ((value % modulo) + modulo) % modulo; - } + /** + * Returns the modulous a result for b. Works for floating point numbers. + * + * @param a Number to modulo against. (a % b) + * @param b Number to modulo with. (a % b) + * @returns The modulo result. + */ + template + static inline T mod(T value, T modulo) { + return ((value % modulo) + modulo) % modulo; + } - static inline float_t fmod(float_t value, float_t modulo) { - float_t n = fmod(value, modulo); - return n; - } + static inline float_t fmod(float_t value, float_t modulo) { + float_t n = fmod(value, modulo); + return n; + } - /** - * Convert degrees to radians. - * - * @param n Degrees to convert. - * @returns The number in radians. - */ - static float_t deg2rad(float_t degrees) { - return degrees * (MATH_PI / 180.0f); - } + /** + * Convert degrees to radians. + * + * @param n Degrees to convert. + * @returns The number in radians. + */ + static float_t deg2rad(float_t degrees) { + return degrees * (MATH_PI / 180.0f); + } - /** - * Convert radians to degrees. - * @param n Radians to convert. - * @returns The number in degrees. - */ - static float_t rad2deg(float_t n) { - return (n * 180.0f) / MATH_PI; - } + /** + * Convert radians to degrees. + * @param n Radians to convert. + * @returns The number in degrees. + */ + static float_t rad2deg(float_t n) { + return (n * 180.0f) / MATH_PI; + } - /** - * Round a number to the nearest whole number. - * @param n Number to round. - * @return Rounded number. - */ - template - static T round(float_t n) { - return (T)roundf(n); - } + /** + * Round a number to the nearest whole number. + * @param n Number to round. + * @return Rounded number. + */ + template + static T round(float_t n) { + return (T)roundf(n); + } - /** - * Rounds the number down to the nearest whole number. - * @param n Number to round down. - * @return Rounded number. - */ - template - static T floor(float_t n) { - return (T)floorf(n); - } + /** + * Rounds the number down to the nearest whole number. + * @param n Number to round down. + * @return Rounded number. + */ + template + static T floor(float_t n) { + return (T)floorf(n); + } }; } \ No newline at end of file diff --git a/src/dawnhelloworld/CMakeLists.txt b/src/dawnhelloworld/CMakeLists.txt index 5b3bee10..841ba55b 100644 --- a/src/dawnhelloworld/CMakeLists.txt +++ b/src/dawnhelloworld/CMakeLists.txt @@ -11,4 +11,7 @@ target_include_directories(${DAWN_TARGET_NAME} # Subdirs add_subdirectory(game) -add_subdirectory(scene) \ No newline at end of file +add_subdirectory(scene) + +# Assets +include("${DAWN_ASSETS_SOURCE_DIR}/games/helloworld/CMakeLists.txt") \ No newline at end of file diff --git a/src/dawnhelloworld/scene/HelloWorldScene.cpp b/src/dawnhelloworld/scene/HelloWorldScene.cpp index c43bd5a3..0acadcc4 100644 --- a/src/dawnhelloworld/scene/HelloWorldScene.cpp +++ b/src/dawnhelloworld/scene/HelloWorldScene.cpp @@ -23,19 +23,7 @@ void Dawn::helloWorldScene(Scene &s) { cubeMesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT); CubeMesh::buffer(cubeMesh, glm::vec3(-1, -1, -1), glm::vec3(1, 1, 1), 0, 0); - auto texture = std::make_shared(); - texture->setSize( - 3, - 3, - TextureFormat::RGBA, - TextureDataFormat::FLOAT - ); - struct Color colors[9] = { - COLOR_RED, COLOR_BLUE, COLOR_GREEN, - COLOR_MAGENTA, COLOR_WHITE, COLOR_BLACK, - COLOR_CORNFLOWER_BLUE, COLOR_YELLOW, COLOR_CYAN - }; - texture->buffer(colors); + auto texture = s.getGame()->assetManager.get("test_texture"); auto cubeItem = s.createSceneItem(); auto cubeMeshRenderer = cubeItem->addComponent(); @@ -47,7 +35,6 @@ void Dawn::helloWorldScene(Scene &s) { events.push_back(cmp.getScene()->onUnpausedUpdate.listen([&](float_t delta) { auto item = cmp.getItem(); item->setLocalRotation(item->getLocalRotation() * glm::quat(glm::vec3(1, 1, 0) * delta)); - // item->setLocalScale(item->getLocalScale() + glm::vec3(1, 1, 1) * delta); })); }); } \ No newline at end of file diff --git a/src/dawnlinux/main.cpp b/src/dawnlinux/main.cpp index 2cff1977..0a95ccbc 100644 --- a/src/dawnlinux/main.cpp +++ b/src/dawnlinux/main.cpp @@ -6,10 +6,21 @@ #include "main.hpp" #include "display/RenderHost.hpp" #include "game/Game.hpp" +#include "asset/AssetDataLoader.hpp" using namespace Dawn; +std::string executablePath; -int32_t main() { +FILE * AssetDataLoader::openAssetArchiveFile() { + std::string path = + return fopen((path + "../../assets.tar").c_str(), "rb"); +} + +int32_t main(int32_t argc, const char **argv) { + //Set the path + path = argv[0]; + + //Create the game auto game = std::make_shared(); game->init(); diff --git a/src/dawnlinux/main.hpp b/src/dawnlinux/main.hpp index 46f20bcd..242c021e 100644 --- a/src/dawnlinux/main.hpp +++ b/src/dawnlinux/main.hpp @@ -9,6 +9,8 @@ /** * The main entry point for the program. * + * @param argc The count of arguments passed to the program. + * @param argv The arguments passed to the program. * @return 0 for success, anything else for failure. */ -int32_t main(); \ No newline at end of file +int32_t main(int32_t argc, const char **argv); \ No newline at end of file diff --git a/src/dawnopengl/display/Texture.cpp b/src/dawnopengl/display/Texture.cpp index 868f6e6a..90c5cf39 100644 --- a/src/dawnopengl/display/Texture.cpp +++ b/src/dawnopengl/display/Texture.cpp @@ -26,6 +26,10 @@ int32_t Texture::getHeight() { return this->height; } +bool_t Texture::isReady() { + return this->id != -1; +} + void Texture::setSize( const int32_t width, const int32_t height, diff --git a/src/dawnopengl/display/Texture.hpp b/src/dawnopengl/display/Texture.hpp index 83b41352..15677102 100644 --- a/src/dawnopengl/display/Texture.hpp +++ b/src/dawnopengl/display/Texture.hpp @@ -32,7 +32,7 @@ namespace Dawn { const enum TextureFormat format, const enum TextureDataFormat dataForat ) override; - // bool_t isReady() override; + bool_t isReady() override; void buffer(const struct ColorU8 pixels[]) override; void buffer(const struct Color pixels[]); void buffer(const uint8_t pixels[]) override; diff --git a/src/dawnopengl/display/shader/Shader.hpp b/src/dawnopengl/display/shader/Shader.hpp index 73234b0d..a3a0e58e 100644 --- a/src/dawnopengl/display/shader/Shader.hpp +++ b/src/dawnopengl/display/shader/Shader.hpp @@ -14,7 +14,7 @@ namespace Dawn { typedef GLuint shadertexturebinding_t; - enum ShaderOpenGLVariant { + enum class ShaderOpenGLVariant { GLSL_330_CORE }; @@ -60,7 +60,7 @@ namespace Dawn { */ void init() override { // Determine which kind of OpenGL shader to use. - variant = GLSL_330_CORE; + variant = ShaderOpenGLVariant::GLSL_330_CORE; // Now get the stages T dummy;