Skip to content

Commit

Permalink
Fix: ProjectScript::onLoad after components are loaded #29
Browse files Browse the repository at this point in the history
  • Loading branch information
brenocq committed Aug 14, 2022
1 parent 0e7e4aa commit b95b217
Show file tree
Hide file tree
Showing 22 changed files with 87 additions and 62 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ if(NOT (ATTA_SYSTEM_NAME MATCHES "Web") AND NOT ATTA_STATIC_PROJECT_FILE)
FILES_MATCHING REGEX ".*\.(h|inl)$")

# Install extern includes
install(DIRECTORY build/_deps/imgui-src/ DESTINATION include/${ATTA_VERSION_SAFE}/extern/imgui
install(DIRECTORY ${CMAKE_BINARY_DIR}/_deps/imgui-src/ DESTINATION include/${ATTA_VERSION_SAFE}/extern/imgui
FILES_MATCHING REGEX ".*\.(h|inl)$")
install(DIRECTORY build/_deps/implot-src/ DESTINATION include/${ATTA_VERSION_SAFE}/extern/implot
install(DIRECTORY ${CMAKE_BINARY_DIR}/_deps/implot-src/ DESTINATION include/${ATTA_VERSION_SAFE}/extern/implot
FILES_MATCHING REGEX ".*\.(h|inl)$")
install(DIRECTORY src/extern/glad/ DESTINATION include/${ATTA_VERSION_SAFE}/extern/glad
FILES_MATCHING REGEX ".*\.(h|inl)$")
Expand Down
8 changes: 7 additions & 1 deletion scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
SOURCE_PATH="$SCRIPT_PATH/.."
BUILD_PATH="$SOURCE_PATH/build"
CMAKE_BUILD_TYPE="-DCMAKE_BUILD_TYPE=Release"
BUILD_PATH_TYPE=""
BUILD_PATH_SUFIX="release"
CMAKE_ATTA_STATIC=""
BUILD_TYPE="default"
RUN_AFTER="false"
Expand Down Expand Up @@ -137,6 +139,7 @@ while [[ $# -gt 0 ]]; do
;;
-d|--debug)
CMAKE_BUILD_TYPE="-DCMAKE_BUILD_TYPE=Debug"
BUILD_PATH_SUFIX="debug"
shift # past argument
;;
-g|--gdb)
Expand All @@ -159,6 +162,9 @@ while [[ $# -gt 0 ]]; do
;;
-t|--type)
BUILD_TYPE="$2"
if [[ "$BUILD_TYPE" != "default" ]]; then
BUILD_PATH_TYPE="$BUILD_TYPE-"
fi
shift # past argument
shift # past value
;;
Expand All @@ -176,7 +182,7 @@ done


# Change to build directory
BUILD_PATH="$BUILD_PATH/$BUILD_TYPE"
BUILD_PATH="$BUILD_PATH/$BUILD_PATH_TYPE$BUILD_PATH_SUFIX"
mkdir -p $BUILD_PATH && cd $BUILD_PATH

# Build
Expand Down
12 changes: 7 additions & 5 deletions src/atta/component/components/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ void renderComboImage(std::string attribute, StringId& image) {
if (ImGui::BeginCombo(("##ComboImage" + imguiId).c_str(), selectedName.c_str())) {
std::vector<StringId> rImages = resource::getResources<resource::Image>();
for (StringId rImage : rImages) {
std::string imageStr = rImage.getString();
if(imageStr == "") imageStr = "##";
const bool selected = (rImage == image);
if (ImGui::Selectable(rImage.getString().c_str(), selected))
if (ImGui::Selectable(imageStr.c_str(), selected))
image = rImage;
if (selected)
ImGui::SetItemDefaultFocus();
Expand All @@ -46,8 +48,10 @@ void renderImGui(void* data, std::string imguiId) {
if (ImGui::BeginCombo(("##Combo" + imguiId).c_str(), selectedName.c_str())) {
std::vector<StringId> rMaterials = resource::getResources<resource::Material>();
for (StringId rMaterial : rMaterials) {
std::string materialStr = rMaterial.getString();
if(materialStr == "") materialStr = "##";
const bool selected = (rMaterial == material->sid);
if (ImGui::Selectable(rMaterial.getString().c_str(), selected))
if (ImGui::Selectable(materialStr.c_str(), selected))
material->sid = rMaterial;
if (selected)
ImGui::SetItemDefaultFocus();
Expand Down Expand Up @@ -165,9 +169,7 @@ ComponentDescription& TypedComponentRegistry<Material>::getDescription() {

resource::Material* Material::getResource() const { return resource::get<resource::Material>(sid.getString()); }

void Material::set(StringId material) {
sid = material;
}
void Material::set(StringId material) { sid = material; }

void Material::set(const resource::Material* material) {
if (material == nullptr) {
Expand Down
2 changes: 2 additions & 0 deletions src/atta/component/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
// By Breno Cunha Queiroz
//--------------------------------------------------
#include <atta/component/interface.h>

#include <atta/component/entity.h>

namespace atta::component {

Entity::Entity() : Entity(-1, -1) {}
Entity::Entity(EntityId id) : Entity(id, 0) {}
Entity::Entity(EntityId id, int cloneId) : _id(id), _cloneId(cloneId) {}

Expand Down
5 changes: 3 additions & 2 deletions src/atta/component/entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ namespace atta::component {

class Entity {
public:
Entity();
Entity(EntityId id);
Entity(EntityId id, int cloneId);
operator EntityId() const { return _id; }

template <typename T>
T* add() {
T* add() const {
return component::addComponent<T>(_id);
}

template <typename T>
T* get() {
T* get() const {
return component::getComponent<T>(_id);
}

Expand Down
10 changes: 10 additions & 0 deletions src/atta/component/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <atta/component/components/relationship.h>
#include <atta/component/components/script.h>
#include <atta/component/factory.h>
#include <atta/component/interface.h>
#include <atta/script/interface.h>

namespace atta::component {
Expand Down Expand Up @@ -121,6 +122,15 @@ std::vector<uint8_t*> Factory::getMemories() const {
return memories;
}

std::vector<Entity> Factory::getClones() const {
std::vector<Entity> clones;
clones.resize(_maxClones);
int i = 0;
for (EntityId entity = _firstCloneEid; entity < EntityId(_firstCloneEid + _maxClones); entity++, i++)
clones[i] = Entity(entity, i);
return clones;
}

std::vector<EntityId> Factory::getCloneIds() const {
std::vector<EntityId> clones;
clones.resize(_maxClones);
Expand Down
2 changes: 2 additions & 0 deletions src/atta/component/factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <atta/component/base.h>

namespace atta::component {
class Entity;
class ComponentRegistry;
class Factory {
public:
Expand All @@ -24,6 +25,7 @@ class Factory {

EntityId getPrototypeId() const { return _prototypeId; }
EntityId getFirstCloneId() const { return _firstCloneEid; }
std::vector<Entity> getClones() const;
std::vector<EntityId> getCloneIds() const;
uint64_t getMaxClones() const { return _maxClones; }
uint64_t getNumClones() const { return _maxClones; }
Expand Down
2 changes: 1 addition & 1 deletion src/atta/component/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void removeComponentById(ComponentId id, Entity entity) { Manager::getInstance()
// Getters
std::vector<ComponentRegistry*> getComponentRegistries() { return Manager::getInstance().getComponentRegistriesImpl(); }
std::vector<Factory>& getFactories() { return Manager::getInstance().getFactoriesImpl(); }
Factory* getPrototypeFactory(Entity prototype) { return Manager::getInstance().getPrototypeFactoryImpl(prototype); }
Factory* getFactory(Entity prototype) { return Manager::getInstance().getFactoryImpl(prototype); }

// Views
std::vector<EntityId> getEntitiesView() { return Manager::getInstance().getEntitiesViewImpl(); }
Expand Down
4 changes: 2 additions & 2 deletions src/atta/component/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

#include <atta/component/base.h>
#include <atta/component/components/component.h>
#include <atta/component/factory.h>
#include <atta/component/typedComponentRegistry.h>

namespace atta::component {

class Factory;
class Entity;

void startUp();
Expand Down Expand Up @@ -43,7 +43,7 @@ void removeComponentById(ComponentId id, Entity entity);
// Getters
std::vector<ComponentRegistry*> getComponentRegistries();
std::vector<Factory>& getFactories();
Factory* getPrototypeFactory(Entity prototype);
Factory* getFactory(Entity prototype);

// Views
std::vector<EntityId> getEntitiesView();
Expand Down
21 changes: 1 addition & 20 deletions src/atta/component/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ Entity Manager::createEntityImpl(EntityId entity, size_t quantity) {
event::CreateEntity event;
event.entityId = eid;
event::publish(event);
LOG_DEBUG("component::Manager", "Entity $0 created", eid);

return Entity(eid);
}
Expand Down Expand Up @@ -581,7 +580,7 @@ void Manager::destroyFactories() {
_factories.clear();
}

Factory* Manager::getPrototypeFactoryImpl(Entity prototype) {
Factory* Manager::getFactoryImpl(Entity prototype) {
for (Factory& factory : _factories)
if (factory.getPrototypeId() == prototype)
return &factory;
Expand Down Expand Up @@ -628,24 +627,6 @@ void Manager::onImageEvent(event::Event& event) {
case event::ImageLoad::type: {
event::ImageLoad& e = reinterpret_cast<event::ImageLoad&>(event);

// Update material options
{
bool found = false;
for (auto op : TypedComponentRegistry<Material>::description->attributeDescriptions[4].options)
if (std::any_cast<StringId>(op) == e.sid) {
found = true;
break;
}

if (!found) {
TypedComponentRegistry<Material>::description->attributeDescriptions[4].options.push_back(std::any(e.sid));
TypedComponentRegistry<Material>::description->attributeDescriptions[5].options.push_back(std::any(e.sid));
TypedComponentRegistry<Material>::description->attributeDescriptions[6].options.push_back(std::any(e.sid));
TypedComponentRegistry<Material>::description->attributeDescriptions[8].options.push_back(std::any(e.sid));
TypedComponentRegistry<Material>::description->attributeDescriptions[7].options.push_back(std::any(e.sid));
}
}

// Update environment light options
{
bool found = false;
Expand Down
5 changes: 3 additions & 2 deletions src/atta/component/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <atta/memory/allocators/poolAllocatorT.h>
#include <atta/memory/allocators/stackAllocator.h>
#include <atta/memory/interface.h>
#include <atta/component/factory.h>

namespace atta::component {

Expand Down Expand Up @@ -43,7 +44,7 @@ class Manager final {
friend void removeComponentById(ComponentId id, Entity entity);
friend std::vector<ComponentRegistry*> getComponentRegistries();
friend std::vector<Factory>& getFactories();
friend Factory* getPrototypeFactory(Entity prototype);
friend Factory* getFactory(Entity prototype);
friend std::vector<EntityId> getEntitiesView();
friend std::vector<EntityId> getNoPrototypeView();
friend std::vector<EntityId> getCloneView();
Expand Down Expand Up @@ -136,7 +137,7 @@ class Manager final {
void destroyFactories();
EntityId createClonesImpl(size_t quantity);
std::vector<Factory>& getFactoriesImpl() { return _factories; }
Factory* getPrototypeFactoryImpl(Entity prototype);
Factory* getFactoryImpl(Entity prototype);

std::vector<Factory> _factories;
friend Factory;
Expand Down
1 change: 0 additions & 1 deletion src/atta/resource/resources/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ void Material::serialize(std::ostream& os) {
file::write(os, roughnessImage);
file::write(os, aoImage);
file::write(os, normalImage);
LOG_DEBUG("res::Material", "Values $0 $1 $2 $3 $4", colorImage, metallicImage, roughnessImage, aoImage, normalImage);
}

void Material::deserialize(std::istream& is) {
Expand Down
2 changes: 1 addition & 1 deletion src/atta/script/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ProjectScript* registerProjectScript(std::string name, ProjectScript* projectScr
return projectScript;
}

Script* Manager::registerScript(std::string name, Script* script) {
Script* registerScript(std::string name, Script* script) {
LOG_VERBOSE("script::Manager", "Registering script [w]$0[]", name);
Manager::getInstance()._scripts[StringId(name)] = script;

Expand Down
2 changes: 2 additions & 0 deletions src/atta/script/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Manager final {
#ifndef ATTA_STATIC_PROJECT
// Handle events
void onFileChange(event::Event& event);
void onProjectBeforeDeserialize(event::Event& event);
void onProjectOpen(event::Event& event);
void onProjectClose(event::Event& event);

Expand All @@ -53,6 +54,7 @@ class Manager final {
void linkTarget(StringId target);
void releaseTarget(StringId target);

bool _projectDeserialized;
std::shared_ptr<Compiler> _compiler;
std::shared_ptr<Linker> _linker;
std::map<StringId, StringId> _targetToScript; // Convert target name to script name
Expand Down
20 changes: 16 additions & 4 deletions src/atta/script/managerDynamic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ void Manager::startUpImpl() {
#endif

event::subscribe<event::FileWatch>(BIND_EVENT_FUNC(Manager::onFileChange));
event::subscribe<event::ProjectBeforeDeserialize>(BIND_EVENT_FUNC(Manager::onProjectOpen));
event::subscribe<event::ProjectBeforeDeserialize>(BIND_EVENT_FUNC(Manager::onProjectBeforeDeserialize));
event::subscribe<event::ProjectOpen>(BIND_EVENT_FUNC(Manager::onProjectOpen));
event::subscribe<event::ProjectClose>(BIND_EVENT_FUNC(Manager::onProjectClose));

_projectDeserialized = false;
_projectScript = std::make_pair(StringId(), nullptr);
}

Expand All @@ -54,7 +55,10 @@ void Manager::onFileChange(event::Event& event) {
event::publish(evt);
}

void Manager::onProjectOpen(event::Event& event) {
void Manager::onProjectBeforeDeserialize(event::Event& event) {
// Load scripts and components. It is necessary to do it before deserializing
// the project to be able to deserialize custom components
_projectDeserialized = false;
updateAllTargets();

// Publish event
Expand All @@ -63,6 +67,14 @@ void Manager::onProjectOpen(event::Event& event) {
event::publish(evt);
}

void Manager::onProjectOpen(event::Event& event) {
_projectDeserialized = true;

// Make sure to call onLoad only after the project is deserialized
if (_projectScript.second)
_projectScript.second->onLoad();
}

void Manager::onProjectClose(event::Event& event) {
// Release all targets
for (auto target : _compiler->getTargets())
Expand Down Expand Up @@ -116,8 +128,8 @@ void Manager::linkTarget(StringId target) {

_projectScript.first = StringId(name);
_projectScript.second = projectScript;

_projectScript.second->onLoad();
if (_projectDeserialized)
_projectScript.second->onLoad();
}

if (script || projectScript)
Expand Down
4 changes: 2 additions & 2 deletions src/atta/script/managerStatic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace atta::script {
void Manager::startUpImpl() {
// Publish registered scripts. Need to do here because scripts are
// registered before ComponentSystem::startUp())
ScriptTargetEvent evt;
event::ScriptTarget evt;
evt.scriptSids = getScriptSids();
EventManager::publish(evt);
event::publish(evt);
}

void Manager::shutDownImpl() {
Expand Down
10 changes: 7 additions & 3 deletions src/atta/script/projectScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,23 @@ class ProjectScript {
//---------- While ----------//
virtual void onAttaLoop(){};
};
} // namespace atta::script

#ifdef ATTA_STATIC_PROJECT
#include <atta/script/interface.h>
namespace atta::script {
template <typename T>
class ProjectScriptRegistration {
static ProjectScript* reg;
};
} // namespace atta::script
#define ATTA_REGISTER_PROJECT_SCRIPT(TYPE) \
template <> \
inline ::atta::script::ProjectScript* ::atta::script::ProjectScriptRegistration<TYPE>::reg = \
::atta::script::Manager::registerProjectScript(#TYPE, new TYPE());
::atta::script::registerProjectScript(#TYPE, new TYPE());

#else

#define ATTA_REGISTER_PROJECT_SCRIPT(TYPE) \
extern "C" { \
std::pair<const char*, atta::script::ProjectScript*> createProjectScript() { \
Expand All @@ -53,6 +59,4 @@ class ProjectScriptRegistration {
}
#endif

} // namespace atta::script

#endif // ATTA_SCRIPT_PROJECT_SCRIPT_H
4 changes: 2 additions & 2 deletions src/atta/script/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class ScriptRegistration {
};
#define ATTA_REGISTER_SCRIPT(TYPE) \
template <> \
inline ::atta::Script* ::atta::ScriptRegistration<TYPE>::reg = ::atta::script::Manager::registerScript(#TYPE, new TYPE());
inline ::atta::script::Script* ::atta::script::ScriptRegistration<TYPE>::reg = ::atta::script::registerScript(#TYPE, new TYPE());
#else
#define ATTA_REGISTER_SCRIPT(TYPE) \
extern "C" { \
std::pair<const char*, atta::Script*> createScript() { return {#TYPE, static_cast<atta::Script*>(new TYPE())}; } \
std::pair<const char*, atta::script::Script*> createScript() { return {#TYPE, static_cast<atta::script::Script*>(new TYPE())}; } \
}
#endif

Expand Down
Loading

0 comments on commit b95b217

Please sign in to comment.