diff --git a/assets/rosa.png b/assets/rosa.png new file mode 100644 index 00000000..07121b9e Binary files /dev/null and b/assets/rosa.png differ diff --git a/src/dawn/asset/AssetLoader.cpp b/src/dawn/asset/AssetLoader.cpp index abdcd030..7d8be236 100644 --- a/src/dawn/asset/AssetLoader.cpp +++ b/src/dawn/asset/AssetLoader.cpp @@ -8,8 +8,8 @@ using namespace Dawn; -AssetLoader::AssetLoader(const std::string &name) : name(name) { - assertTrue(name.size() > 0, "Asset::Asset: Name cannot be empty"); +AssetLoader::AssetLoader(const std::string name) : name(name) { + assertTrue(name.size() > 0, "Name cannot be empty"); } AssetLoader::~AssetLoader() { diff --git a/src/dawn/asset/AssetLoader.hpp b/src/dawn/asset/AssetLoader.hpp index d971c444..a9d78a2e 100644 --- a/src/dawn/asset/AssetLoader.hpp +++ b/src/dawn/asset/AssetLoader.hpp @@ -17,7 +17,7 @@ namespace Dawn { * * @param name Name of the asset. */ - AssetLoader(const std::string &name); + AssetLoader(const std::string name); /** * Virtual function that will be called by the asset manager on a diff --git a/src/dawn/asset/AssetManager.cpp b/src/dawn/asset/AssetManager.cpp index f43c6675..184f9a38 100644 --- a/src/dawn/asset/AssetManager.cpp +++ b/src/dawn/asset/AssetManager.cpp @@ -17,6 +17,7 @@ void AssetManager::update() { auto loader = *itPending; loader->updateSync(); loader->updateAsync(); + loader->updateSync(); if(loader->loaded) { finishedAssetLoaders.push_back(loader); itPending = pendingAssetLoaders.erase(itPending); diff --git a/src/dawn/asset/AssetManager.hpp b/src/dawn/asset/AssetManager.hpp index b57cc91d..757e8852 100644 --- a/src/dawn/asset/AssetManager.hpp +++ b/src/dawn/asset/AssetManager.hpp @@ -22,13 +22,14 @@ namespace Dawn { template std::shared_ptr getExisting(const std::string &filename) { auto existing = std::find_if( - pendingAssetLoaders.begin(), pendingAssetLoaders.end(), + pendingAssetLoaders.begin(), + pendingAssetLoaders.end(), [&](auto &loader) { return loader->name == filename; } ); - if(existing == finishedAssetLoaders.end()) { + if(existing == pendingAssetLoaders.end()) { existing = std::find_if( finishedAssetLoaders.begin(), finishedAssetLoaders.end(), [&](auto &loader) { diff --git a/src/dawn/asset/loader/JSONLoader.cpp b/src/dawn/asset/loader/JSONLoader.cpp index 30efbff9..b713dd2d 100644 --- a/src/dawn/asset/loader/JSONLoader.cpp +++ b/src/dawn/asset/loader/JSONLoader.cpp @@ -7,7 +7,7 @@ using namespace Dawn; -JSONLoader::JSONLoader(const std::string &name) : +JSONLoader::JSONLoader(const std::string name) : AssetLoader(name), loader(name), state(JSONLoaderState::INITIAL) diff --git a/src/dawn/asset/loader/JSONLoader.hpp b/src/dawn/asset/loader/JSONLoader.hpp index 58b3ad02..3ffcff6f 100644 --- a/src/dawn/asset/loader/JSONLoader.hpp +++ b/src/dawn/asset/loader/JSONLoader.hpp @@ -25,7 +25,7 @@ namespace Dawn { public: json data; - JSONLoader(const std::string &name); + JSONLoader(const std::string name); void updateSync() override; void updateAsync() override; ~JSONLoader(); diff --git a/src/dawn/asset/loader/TextureLoader.cpp b/src/dawn/asset/loader/TextureLoader.cpp index a82ec130..09695c65 100644 --- a/src/dawn/asset/loader/TextureLoader.cpp +++ b/src/dawn/asset/loader/TextureLoader.cpp @@ -8,13 +8,12 @@ using namespace Dawn; -TextureLoader::TextureLoader(const std::string &name) : +TextureLoader::TextureLoader(const std::string name) : AssetLoader(name), - loader(name + ".texture"), + loader(name), state(TextureLoaderLoadState::INITIAL) { - sharedTexture = std::make_shared(); - weakTexture = sharedTexture; + texture = std::make_shared(); } void TextureLoader::updateAsync() { @@ -114,34 +113,35 @@ void TextureLoader::updateSync() { if(this->state != TextureLoaderLoadState::ASYNC_DONE) return; this->state = TextureLoaderLoadState::SYNC_LOADING; - assertNotNull(this->sharedTexture, "Texture is null!"); + assertNotNull(this->texture, "Texture is null!"); assertNotNull(this->data, "Texture data is null!"); // Setup Texture - this->sharedTexture->setSize( + this->texture->setSize( this->width, this->height, this->format, TextureDataFormat::UNSIGNED_BYTE ); - this->sharedTexture->buffer(this->data); + this->texture->buffer(this->data); // Free data buffer delete[] this->data; this->data = nullptr; - // Leat go of the held pointer - this->sharedTexture = nullptr; - // Hand off and call done this->state = TextureLoaderLoadState::SYNC_DONE; this->loaded = true; } +std::shared_ptr TextureLoader::getTexture() { + return this->texture; +} + TextureLoader::~TextureLoader() { if(this->data != nullptr) { delete[] this->data; this->data = nullptr; } - this->sharedTexture = nullptr; + this->texture = nullptr; } \ No newline at end of file diff --git a/src/dawn/asset/loader/TextureLoader.hpp b/src/dawn/asset/loader/TextureLoader.hpp index 8e3fbe07..caad5894 100644 --- a/src/dawn/asset/loader/TextureLoader.hpp +++ b/src/dawn/asset/loader/TextureLoader.hpp @@ -30,22 +30,27 @@ namespace Dawn { enum TextureWrapMode wrapY; enum TextureFilterMode filterMin; enum TextureFilterMode filterMag; + std::shared_ptr texture; 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); + TextureLoader(const std::string name); void updateSync() override; void updateAsync() override; + /** + * Get the texture asset. + * + * @return Texture asset. + */ + std::shared_ptr getTexture(); + /** * Dispose / Cleanup the texture asset. Will also dispose the underlying * texture itself. diff --git a/src/dawn/asset/loader/TrueTypeLoader.cpp b/src/dawn/asset/loader/TrueTypeLoader.cpp index a37664c3..c07bfef0 100644 --- a/src/dawn/asset/loader/TrueTypeLoader.cpp +++ b/src/dawn/asset/loader/TrueTypeLoader.cpp @@ -8,9 +8,9 @@ using namespace Dawn; -TrueTypeLoader::TrueTypeLoader(const std::string &name) : +TrueTypeLoader::TrueTypeLoader(const std::string name) : AssetLoader(name), - loader(name + ".ttf") + loader(name) { // Init the font. auto ret = FT_Init_FreeType(&fontLibrary); diff --git a/src/dawn/asset/loader/TrueTypeLoader.hpp b/src/dawn/asset/loader/TrueTypeLoader.hpp index 1b23a0b4..8735dab4 100644 --- a/src/dawn/asset/loader/TrueTypeLoader.hpp +++ b/src/dawn/asset/loader/TrueTypeLoader.hpp @@ -33,7 +33,7 @@ namespace Dawn { * * @param name File name asset to load, omitting the extension. */ - TrueTypeLoader(const std::string &name); + TrueTypeLoader(const std::string name); void updateSync() override; void updateAsync() override; diff --git a/src/dawn/scene/SceneComponent.cpp b/src/dawn/scene/SceneComponent.cpp index e03aba60..cc5a0a9b 100644 --- a/src/dawn/scene/SceneComponent.cpp +++ b/src/dawn/scene/SceneComponent.cpp @@ -49,6 +49,13 @@ void SceneComponent::dispose() { this->item.reset(); } +bool_t SceneComponent::isInitialized() { + return Flag::isOn( + sceneComponentState, + SCENE_COMPONENT_STATE_INIT + ); +} + std::shared_ptr SceneComponent::getItem() { return this->item.lock(); } diff --git a/src/dawn/scene/SceneComponent.hpp b/src/dawn/scene/SceneComponent.hpp index eb150586..c1842ad5 100644 --- a/src/dawn/scene/SceneComponent.hpp +++ b/src/dawn/scene/SceneComponent.hpp @@ -47,6 +47,13 @@ namespace Dawn { */ void dispose(); + /** + * Returns whether this scene component is initialized. + * + * @return Whether this scene component is initialized. + */ + bool_t isInitialized(); + /** * Returns the scene item that this scene component belongs to. * diff --git a/src/dawn/scene/SceneItem.cpp b/src/dawn/scene/SceneItem.cpp index 33ef3669..12e3ec84 100644 --- a/src/dawn/scene/SceneItem.cpp +++ b/src/dawn/scene/SceneItem.cpp @@ -25,8 +25,28 @@ std::shared_ptr SceneItem::getScene() { void SceneItem::init() { auto sharedThis = shared_from_this(); - for(auto &component : components) { - component->init(sharedThis); + + // Loop until all components initialized... + while(true) { + // Create copy of the components, components may chose to add more components + // but those sub components will not be initialized at this time. + auto components = this->components; + for(auto &component : components) { + if(component->isInitialized()) continue; + component->init(sharedThis); + } + + // If they are all initalized we are fine. + components = this->components; + bool_t allInitialized = std::all_of( + components.begin(), + components.end(), + [](auto &component) { + return component->isInitialized(); + } + ); + + if(allInitialized) break; } } diff --git a/src/dawnopengl/display/shader/SimpleTexturedShader.cpp b/src/dawnopengl/display/shader/SimpleTexturedShader.cpp index bca7d20f..a129b9b6 100644 --- a/src/dawnopengl/display/shader/SimpleTexturedShader.cpp +++ b/src/dawnopengl/display/shader/SimpleTexturedShader.cpp @@ -45,7 +45,7 @@ void SimpleTexturedShader::getStages( "uniform sampler2D u_Texture;\n" "void main() {\n" "if(u_HasTexture) {\n" - "o_Color = texture(u_Texture, o_TextCoord) * u_Color;\n" + "o_Color = texture(u_Texture, o_TextCoord);\n" "} else {\n" "o_Color = u_Color;" "}\n" diff --git a/src/dawnrpg/CMakeLists.txt b/src/dawnrpg/CMakeLists.txt index 1cbf5c8e..be5eb845 100644 --- a/src/dawnrpg/CMakeLists.txt +++ b/src/dawnrpg/CMakeLists.txt @@ -15,4 +15,4 @@ add_subdirectory(game) add_subdirectory(scene) # Assets -# include("${DAWN_ASSETS_SOURCE_DIR}/games/helloworld/CMakeLists.txt") \ No newline at end of file +tool_texture(rosa FILE=rosa.png) \ No newline at end of file diff --git a/src/dawnrpg/component/RPGEntity.cpp b/src/dawnrpg/component/RPGEntity.cpp index ea2b58d6..18d55fe8 100644 --- a/src/dawnrpg/component/RPGEntity.cpp +++ b/src/dawnrpg/component/RPGEntity.cpp @@ -5,11 +5,35 @@ #include "RPGEntity.hpp" #include "scene/Scene.hpp" +#include "display/mesh/QuadMesh.hpp" +#include "asset/loader/TextureLoader.hpp" using namespace Dawn; void RPGEntity::onInit() { + const glm::vec2 size = { RPG_ENTITY_SIZE, RPG_ENTITY_SIZE }; + const glm::vec2 position = -size; + const auto sprites = getGame()->assetManager->get("rosa.texture"); + + mesh = std::make_shared(); + mesh->createBuffers(QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT); + QuadMesh::buffer( + mesh, + glm::vec4(position.x, position.y, size.x, size.y), + glm::vec4(0, 1.0f, 1, 0.75f), + 0, 0, 0 + ); + + meshRenderer = getItem()->addComponent(); + meshRenderer->mesh = mesh; + + material = getItem()->addComponent(); + material->setColor(COLOR_WHITE); + material->setTexture(sprites->getTexture()); } void RPGEntity::onDispose() { + meshRenderer = nullptr; + mesh = nullptr; + material = nullptr; } \ No newline at end of file diff --git a/src/dawnrpg/component/RPGEntity.hpp b/src/dawnrpg/component/RPGEntity.hpp index f308d09d..dc0c7b48 100644 --- a/src/dawnrpg/component/RPGEntity.hpp +++ b/src/dawnrpg/component/RPGEntity.hpp @@ -4,15 +4,21 @@ // https://opensource.org/licenses/MIT #pragma once -#include "component/display/Camera.hpp" +#include "component/display/material/SimpleTexturedMaterial.hpp" -#define RPG_ENTITY_SIZE 16.0f +#define RPG_ENTITY_SIZE 32.0f namespace Dawn { class RPGEntity final : public SceneComponent { private: + protected: + std::shared_ptr meshRenderer; + std::shared_ptr mesh; + std::shared_ptr material; + public: + void onInit() override; void onDispose() override; }; diff --git a/src/dawnrpg/component/RPGPlayer.cpp b/src/dawnrpg/component/RPGPlayer.cpp index 12bbf5ac..e073955a 100644 --- a/src/dawnrpg/component/RPGPlayer.cpp +++ b/src/dawnrpg/component/RPGPlayer.cpp @@ -33,9 +33,9 @@ void RPGPlayer::onInit() { if(this->camera != nullptr) { this->camera->lookAtPixelPerfect( - getItem()->getLocalPosition() + glm::vec3(0, -32.0f, 0), + getItem()->getLocalPosition() + glm::vec3(0, -(RPG_ENTITY_SIZE*2), 0), getItem()->getLocalPosition(), - 2.0f + 1.0f ); } })); diff --git a/src/dawnrpg/component/RPGPlayer.hpp b/src/dawnrpg/component/RPGPlayer.hpp index 4be0b59c..93fbf11f 100644 --- a/src/dawnrpg/component/RPGPlayer.hpp +++ b/src/dawnrpg/component/RPGPlayer.hpp @@ -6,7 +6,7 @@ #pragma once #include "component/display/Camera.hpp" -#define PLAYER_SPEED 5.0f +#define PLAYER_SPEED 128.0f namespace Dawn { class RPGPlayer final : public SceneComponent { diff --git a/src/dawnrpg/scene/HelloWorldScene.cpp b/src/dawnrpg/scene/HelloWorldScene.cpp index 78a45949..65395fc1 100644 --- a/src/dawnrpg/scene/HelloWorldScene.cpp +++ b/src/dawnrpg/scene/HelloWorldScene.cpp @@ -8,8 +8,8 @@ #include "component/display/Camera.hpp" #include "prefab/SimpleSpinningCube.hpp" #include "component/display/material/SimpleTexturedMaterial.hpp" -#include "display/mesh/QuadMesh.hpp" #include "component/ui/UICanvas.hpp" +#include "asset/loader/TextureLoader.hpp" #include "component/RPGEntity.hpp" #include "component/RPGPlayer.hpp" @@ -17,6 +17,8 @@ using namespace Dawn; void Dawn::helloWorldScene(Scene &s) { + auto textureRosa = s.getGame()->assetManager->get("rosa.texture"); + while(!s.getGame()->assetManager->isEverythingLoaded()) { s.getGame()->assetManager->update(); } @@ -25,29 +27,20 @@ void Dawn::helloWorldScene(Scene &s) { auto camera = cameraItem->addComponent(); camera->clipFar = 99999.99f; - auto player = s.createSceneItem(); + // Player: + { + auto ent = s.createSceneItem(); + auto eEnt = ent->addComponent(); + auto ePlyr = ent->addComponent(); + ePlyr->camera = camera; + } - glm::vec2 size = { RPG_ENTITY_SIZE, RPG_ENTITY_SIZE }; - glm::vec2 position = -size; - auto quadMesh = std::make_shared(); - quadMesh->createBuffers(QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT); - QuadMesh::buffer( - quadMesh, - glm::vec4(position.x, position.y, size.x, size.y), - glm::vec4(0, 0, 1, 1), - 0, 0, 0 - ); - - auto quadRenderer = player->addComponent(); - quadRenderer->mesh = quadMesh; - - auto quadMaterial = player->addComponent(); - quadMaterial->setColor(COLOR_WHITE); - - auto ent = player->addComponent(); - - auto ePlyr = player->addComponent(); - ePlyr->camera = camera; + // Test Entity + { + auto ent = s.createSceneItem(); + ent->setLocalPosition(glm::vec3(-128, -32, 0)); + auto eEnt = ent->addComponent(); + } auto uiCanvasItem = s.createSceneItem(); auto uiCanvas = uiCanvasItem->addComponent();