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

(image error) Size: 1.4 KiB

@ -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() {

@ -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

@ -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);

@ -22,13 +22,14 @@ namespace Dawn {
template<class T>
std::shared_ptr<T> 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) {

@ -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)

@ -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();

@ -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<Texture>();
weakTexture = sharedTexture;
texture = std::make_shared<Texture>();
}
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<Texture> TextureLoader::getTexture() {
return this->texture;
}
TextureLoader::~TextureLoader() {
if(this->data != nullptr) {
delete[] this->data;
this->data = nullptr;
}
this->sharedTexture = nullptr;
this->texture = nullptr;
}

@ -30,22 +30,27 @@ namespace Dawn {
enum TextureWrapMode wrapY;
enum TextureFilterMode filterMin;
enum TextureFilterMode filterMag;
std::shared_ptr<Texture> texture;
public:
std::shared_ptr<Texture> sharedTexture;
std::weak_ptr<Texture> 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<Texture> getTexture();
/**
* Dispose / Cleanup the texture asset. Will also dispose the underlying
* texture itself.

@ -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);

@ -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;

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

@ -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.
*

@ -25,8 +25,28 @@ std::shared_ptr<Scene> 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;
}
}

@ -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"

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

@ -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<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() {
meshRenderer = nullptr;
mesh = nullptr;
material = nullptr;
}

@ -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> meshRenderer;
std::shared_ptr<Mesh> mesh;
std::shared_ptr<SimpleTexturedMaterial> material;
public:
void onInit() override;
void onDispose() override;
};

@ -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
);
}
}));

@ -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 {

@ -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<TextureLoader>("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>();
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 };
glm::vec2 position = -size;
auto quadMesh = std::make_shared<Mesh>();
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<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;
// Test Entity
{
auto ent = s.createSceneItem();
ent->setLocalPosition(glm::vec3(-128, -32, 0));
auto eEnt = ent->addComponent<RPGEntity>();
}
auto uiCanvasItem = s.createSceneItem();
auto uiCanvas = uiCanvasItem->addComponent<UICanvas>();