cleaned things up a bit, looks good to start implementing the rpg mechs

This commit is contained in:
2024-11-25 21:37:28 -06:00
parent de55029356
commit 4914ec6168
21 changed files with 121 additions and 57 deletions

BIN
assets/rosa.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -8,8 +8,8 @@
using namespace Dawn; using namespace Dawn;
AssetLoader::AssetLoader(const std::string &name) : name(name) { AssetLoader::AssetLoader(const std::string name) : name(name) {
assertTrue(name.size() > 0, "Asset::Asset: Name cannot be empty"); assertTrue(name.size() > 0, "Name cannot be empty");
} }
AssetLoader::~AssetLoader() { AssetLoader::~AssetLoader() {

View File

@ -17,7 +17,7 @@ namespace Dawn {
* *
* @param name Name of the asset. * @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 * Virtual function that will be called by the asset manager on a

View File

@ -17,6 +17,7 @@ void AssetManager::update() {
auto loader = *itPending; auto loader = *itPending;
loader->updateSync(); loader->updateSync();
loader->updateAsync(); loader->updateAsync();
loader->updateSync();
if(loader->loaded) { if(loader->loaded) {
finishedAssetLoaders.push_back(loader); finishedAssetLoaders.push_back(loader);
itPending = pendingAssetLoaders.erase(itPending); itPending = pendingAssetLoaders.erase(itPending);

View File

@ -22,13 +22,14 @@ namespace Dawn {
template<class T> template<class T>
std::shared_ptr<T> getExisting(const std::string &filename) { std::shared_ptr<T> getExisting(const std::string &filename) {
auto existing = std::find_if( auto existing = std::find_if(
pendingAssetLoaders.begin(), pendingAssetLoaders.end(), pendingAssetLoaders.begin(),
pendingAssetLoaders.end(),
[&](auto &loader) { [&](auto &loader) {
return loader->name == filename; return loader->name == filename;
} }
); );
if(existing == finishedAssetLoaders.end()) { if(existing == pendingAssetLoaders.end()) {
existing = std::find_if( existing = std::find_if(
finishedAssetLoaders.begin(), finishedAssetLoaders.end(), finishedAssetLoaders.begin(), finishedAssetLoaders.end(),
[&](auto &loader) { [&](auto &loader) {

View File

@ -7,7 +7,7 @@
using namespace Dawn; using namespace Dawn;
JSONLoader::JSONLoader(const std::string &name) : JSONLoader::JSONLoader(const std::string name) :
AssetLoader(name), AssetLoader(name),
loader(name), loader(name),
state(JSONLoaderState::INITIAL) state(JSONLoaderState::INITIAL)

View File

@ -25,7 +25,7 @@ namespace Dawn {
public: public:
json data; json data;
JSONLoader(const std::string &name); JSONLoader(const std::string name);
void updateSync() override; void updateSync() override;
void updateAsync() override; void updateAsync() override;
~JSONLoader(); ~JSONLoader();

View File

@ -8,13 +8,12 @@
using namespace Dawn; using namespace Dawn;
TextureLoader::TextureLoader(const std::string &name) : TextureLoader::TextureLoader(const std::string name) :
AssetLoader(name), AssetLoader(name),
loader(name + ".texture"), loader(name),
state(TextureLoaderLoadState::INITIAL) state(TextureLoaderLoadState::INITIAL)
{ {
sharedTexture = std::make_shared<Texture>(); texture = std::make_shared<Texture>();
weakTexture = sharedTexture;
} }
void TextureLoader::updateAsync() { void TextureLoader::updateAsync() {
@ -114,34 +113,35 @@ void TextureLoader::updateSync() {
if(this->state != TextureLoaderLoadState::ASYNC_DONE) return; if(this->state != TextureLoaderLoadState::ASYNC_DONE) return;
this->state = TextureLoaderLoadState::SYNC_LOADING; this->state = TextureLoaderLoadState::SYNC_LOADING;
assertNotNull(this->sharedTexture, "Texture is null!"); assertNotNull(this->texture, "Texture is null!");
assertNotNull(this->data, "Texture data is null!"); assertNotNull(this->data, "Texture data is null!");
// Setup Texture // Setup Texture
this->sharedTexture->setSize( this->texture->setSize(
this->width, this->width,
this->height, this->height,
this->format, this->format,
TextureDataFormat::UNSIGNED_BYTE TextureDataFormat::UNSIGNED_BYTE
); );
this->sharedTexture->buffer(this->data); this->texture->buffer(this->data);
// Free data buffer // Free data buffer
delete[] this->data; delete[] this->data;
this->data = nullptr; this->data = nullptr;
// Leat go of the held pointer
this->sharedTexture = nullptr;
// Hand off and call done // Hand off and call done
this->state = TextureLoaderLoadState::SYNC_DONE; this->state = TextureLoaderLoadState::SYNC_DONE;
this->loaded = true; this->loaded = true;
} }
std::shared_ptr<Texture> TextureLoader::getTexture() {
return this->texture;
}
TextureLoader::~TextureLoader() { TextureLoader::~TextureLoader() {
if(this->data != nullptr) { if(this->data != nullptr) {
delete[] this->data; delete[] this->data;
this->data = nullptr; this->data = nullptr;
} }
this->sharedTexture = nullptr; this->texture = nullptr;
} }

View File

@ -30,22 +30,27 @@ namespace Dawn {
enum TextureWrapMode wrapY; enum TextureWrapMode wrapY;
enum TextureFilterMode filterMin; enum TextureFilterMode filterMin;
enum TextureFilterMode filterMag; enum TextureFilterMode filterMag;
std::shared_ptr<Texture> texture;
public: public:
std::shared_ptr<Texture> sharedTexture;
std::weak_ptr<Texture> weakTexture;
/** /**
* Constructs a texture asset loader. You should instead use the parent * Constructs a texture asset loader. You should instead use the parent
* asset managers' abstracted load method * asset managers' abstracted load method
* *
* @param name File name asset to load, omitting the extension. * @param name File name asset to load, omitting the extension.
*/ */
TextureLoader(const std::string &name); TextureLoader(const std::string name);
void updateSync() override; void updateSync() override;
void updateAsync() override; void updateAsync() override;
/**
* Get the texture asset.
*
* @return Texture asset.
*/
std::shared_ptr<Texture> getTexture();
/** /**
* Dispose / Cleanup the texture asset. Will also dispose the underlying * Dispose / Cleanup the texture asset. Will also dispose the underlying
* texture itself. * texture itself.

View File

@ -8,9 +8,9 @@
using namespace Dawn; using namespace Dawn;
TrueTypeLoader::TrueTypeLoader(const std::string &name) : TrueTypeLoader::TrueTypeLoader(const std::string name) :
AssetLoader(name), AssetLoader(name),
loader(name + ".ttf") loader(name)
{ {
// Init the font. // Init the font.
auto ret = FT_Init_FreeType(&fontLibrary); auto ret = FT_Init_FreeType(&fontLibrary);

View File

@ -33,7 +33,7 @@ namespace Dawn {
* *
* @param name File name asset to load, omitting the extension. * @param name File name asset to load, omitting the extension.
*/ */
TrueTypeLoader(const std::string &name); TrueTypeLoader(const std::string name);
void updateSync() override; void updateSync() override;
void updateAsync() override; void updateAsync() override;

View File

@ -49,6 +49,13 @@ void SceneComponent::dispose() {
this->item.reset(); this->item.reset();
} }
bool_t SceneComponent::isInitialized() {
return Flag::isOn<uint_fast8_t>(
sceneComponentState,
SCENE_COMPONENT_STATE_INIT
);
}
std::shared_ptr<SceneItem> SceneComponent::getItem() { std::shared_ptr<SceneItem> SceneComponent::getItem() {
return this->item.lock(); return this->item.lock();
} }

View File

@ -47,6 +47,13 @@ namespace Dawn {
*/ */
void dispose(); 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. * Returns the scene item that this scene component belongs to.
* *

View File

@ -25,8 +25,28 @@ std::shared_ptr<Scene> SceneItem::getScene() {
void SceneItem::init() { void SceneItem::init() {
auto sharedThis = shared_from_this(); 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;
} }
} }

View File

@ -45,7 +45,7 @@ void SimpleTexturedShader::getStages(
"uniform sampler2D u_Texture;\n" "uniform sampler2D u_Texture;\n"
"void main() {\n" "void main() {\n"
"if(u_HasTexture) {\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" "} else {\n"
"o_Color = u_Color;" "o_Color = u_Color;"
"}\n" "}\n"

View File

@ -15,4 +15,4 @@ add_subdirectory(game)
add_subdirectory(scene) add_subdirectory(scene)
# Assets # Assets
# include("${DAWN_ASSETS_SOURCE_DIR}/games/helloworld/CMakeLists.txt") tool_texture(rosa FILE=rosa.png)

View File

@ -5,11 +5,35 @@
#include "RPGEntity.hpp" #include "RPGEntity.hpp"
#include "scene/Scene.hpp" #include "scene/Scene.hpp"
#include "display/mesh/QuadMesh.hpp"
#include "asset/loader/TextureLoader.hpp"
using namespace Dawn; using namespace Dawn;
void RPGEntity::onInit() { void RPGEntity::onInit() {
const glm::vec2 size = { RPG_ENTITY_SIZE, RPG_ENTITY_SIZE };
const glm::vec2 position = -size;
const auto sprites = getGame()->assetManager->get<TextureLoader>("rosa.texture");
mesh = std::make_shared<Mesh>();
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>();
meshRenderer->mesh = mesh;
material = getItem()->addComponent<SimpleTexturedMaterial>();
material->setColor(COLOR_WHITE);
material->setTexture(sprites->getTexture());
} }
void RPGEntity::onDispose() { void RPGEntity::onDispose() {
meshRenderer = nullptr;
mesh = nullptr;
material = nullptr;
} }

View File

@ -4,15 +4,21 @@
// https://opensource.org/licenses/MIT // https://opensource.org/licenses/MIT
#pragma once #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 { namespace Dawn {
class RPGEntity final : public SceneComponent { class RPGEntity final : public SceneComponent {
private: private:
protected:
std::shared_ptr<MeshRenderer> meshRenderer;
std::shared_ptr<Mesh> mesh;
std::shared_ptr<SimpleTexturedMaterial> material;
public: public:
void onInit() override; void onInit() override;
void onDispose() override; void onDispose() override;
}; };

View File

@ -33,9 +33,9 @@ void RPGPlayer::onInit() {
if(this->camera != nullptr) { if(this->camera != nullptr) {
this->camera->lookAtPixelPerfect( this->camera->lookAtPixelPerfect(
getItem()->getLocalPosition() + glm::vec3(0, -32.0f, 0), getItem()->getLocalPosition() + glm::vec3(0, -(RPG_ENTITY_SIZE*2), 0),
getItem()->getLocalPosition(), getItem()->getLocalPosition(),
2.0f 1.0f
); );
} }
})); }));

View File

@ -6,7 +6,7 @@
#pragma once #pragma once
#include "component/display/Camera.hpp" #include "component/display/Camera.hpp"
#define PLAYER_SPEED 5.0f #define PLAYER_SPEED 128.0f
namespace Dawn { namespace Dawn {
class RPGPlayer final : public SceneComponent { class RPGPlayer final : public SceneComponent {

View File

@ -8,8 +8,8 @@
#include "component/display/Camera.hpp" #include "component/display/Camera.hpp"
#include "prefab/SimpleSpinningCube.hpp" #include "prefab/SimpleSpinningCube.hpp"
#include "component/display/material/SimpleTexturedMaterial.hpp" #include "component/display/material/SimpleTexturedMaterial.hpp"
#include "display/mesh/QuadMesh.hpp"
#include "component/ui/UICanvas.hpp" #include "component/ui/UICanvas.hpp"
#include "asset/loader/TextureLoader.hpp"
#include "component/RPGEntity.hpp" #include "component/RPGEntity.hpp"
#include "component/RPGPlayer.hpp" #include "component/RPGPlayer.hpp"
@ -17,6 +17,8 @@
using namespace Dawn; using namespace Dawn;
void Dawn::helloWorldScene(Scene &s) { void Dawn::helloWorldScene(Scene &s) {
auto textureRosa = s.getGame()->assetManager->get<TextureLoader>("rosa.texture");
while(!s.getGame()->assetManager->isEverythingLoaded()) { while(!s.getGame()->assetManager->isEverythingLoaded()) {
s.getGame()->assetManager->update(); s.getGame()->assetManager->update();
} }
@ -25,29 +27,20 @@ void Dawn::helloWorldScene(Scene &s) {
auto camera = cameraItem->addComponent<Camera>(); auto camera = cameraItem->addComponent<Camera>();
camera->clipFar = 99999.99f; camera->clipFar = 99999.99f;
auto player = s.createSceneItem(); // Player:
{
auto ent = s.createSceneItem();
auto eEnt = ent->addComponent<RPGEntity>();
auto ePlyr = ent->addComponent<RPGPlayer>();
ePlyr->camera = camera;
}
glm::vec2 size = { RPG_ENTITY_SIZE, RPG_ENTITY_SIZE }; // Test Entity
glm::vec2 position = -size; {
auto quadMesh = std::make_shared<Mesh>(); auto ent = s.createSceneItem();
quadMesh->createBuffers(QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT); ent->setLocalPosition(glm::vec3(-128, -32, 0));
QuadMesh::buffer( auto eEnt = ent->addComponent<RPGEntity>();
quadMesh, }
glm::vec4(position.x, position.y, size.x, size.y),
glm::vec4(0, 0, 1, 1),
0, 0, 0
);
auto quadRenderer = player->addComponent<MeshRenderer>();
quadRenderer->mesh = quadMesh;
auto quadMaterial = player->addComponent<SimpleTexturedMaterial>();
quadMaterial->setColor(COLOR_WHITE);
auto ent = player->addComponent<RPGEntity>();
auto ePlyr = player->addComponent<RPGPlayer>();
ePlyr->camera = camera;
auto uiCanvasItem = s.createSceneItem(); auto uiCanvasItem = s.createSceneItem();
auto uiCanvas = uiCanvasItem->addComponent<UICanvas>(); auto uiCanvas = uiCanvasItem->addComponent<UICanvas>();