Skip to content

Commit

Permalink
Merge branch 'dev' into ui-redesign
Browse files Browse the repository at this point in the history
  • Loading branch information
brenocq committed Dec 30, 2023
2 parents 88b077a + 124cf6b commit 35dd8e9
Show file tree
Hide file tree
Showing 267 changed files with 10,533 additions and 4,033 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/web.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
}

steps:
- uses: mymindstorm/setup-emsdk@v9
- uses: mymindstorm/setup-emsdk@v13

- name: Checkout atta
uses: actions/checkout@v2
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.14)

project(atta VERSION 0.4.0 LANGUAGES CXX C)
project(atta VERSION 0.3.1 LANGUAGES CXX C)

OPTION(ATTA_BUILD_TESTS
"Set to ON to build also the test executables"
Expand Down Expand Up @@ -82,7 +82,7 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL Emscripten)#----- Web build
set(ATTA_BUILD_TESTS OFF CACHE INTERNAL "" FORCE)

# Configure emscripten link flags
set(EMSCRIPTEN_LINK_PROPERTIES "-s USE_WEBGL2=1 -s USE_GLFW=3")
set(EMSCRIPTEN_LINK_PROPERTIES "-s USE_WEBGL2=1 -s USE_GLFW=3 -s GL_ENABLE_GET_PROC_ADDRESS")
set(EMSCRIPTEN_LINK_PROPERTIES "${EMSCRIPTEN_LINK_PROPERTIES} -s ALLOW_MEMORY_GROWTH=1 -s FORCE_FILESYSTEM=1")
set(EMSCRIPTEN_LINK_PROPERTIES "${EMSCRIPTEN_LINK_PROPERTIES} -s NO_DISABLE_EXCEPTION_CATCHING -s EXIT_RUNTIME=1")
set(EMSCRIPTEN_LINK_PROPERTIES "${EMSCRIPTEN_LINK_PROPERTIES} --preload-file ${CMAKE_CURRENT_SOURCE_DIR}/resources@/resources/")
Expand Down
1 change: 1 addition & 0 deletions src/atta/atta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ void Atta::loop() {

file::update();
graphics::update();
resource::update();
}

void Atta::step() {
Expand Down
2 changes: 2 additions & 0 deletions src/atta/cmakeConfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

#ifdef ATTA_OS_WEB
#define ATTA_DIR "/"
#define ATTA_BUILD_DIR "/"
#else
#define ATTA_DIR "@CMAKE_SOURCE_DIR@"
#define ATTA_BUILD_DIR "@CMAKE_BINARY_DIR@"
#endif

#define ATTA_VERSION "@CMAKE_PROJECT_VERSION@"
Expand Down
1 change: 1 addition & 0 deletions src/atta/component/components/cameraSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const uint8_t* CameraSensor::getImage() {
if (cameraInfo.component == this)
return cameraInfo.data.data();
ASSERT(false, "(component::CameraSensor) Could not get camera frame from sensor::Manager.");
return nullptr;
}

} // namespace atta::component
57 changes: 42 additions & 15 deletions src/atta/component/components/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

namespace atta::component {

void renderComboImage(std::string attribute, StringId& image) {
bool isImage = (image != resource::Material::emptyImage);
bool renderComboImage(std::string attribute, StringId& image) {
bool isImage = (image != StringId());
bool updated = false;
std::string imguiId = attribute;
if (isImage) {
std::string selectedName = image.getString();
Expand All @@ -24,17 +25,22 @@ void renderComboImage(std::string attribute, StringId& image) {
if (imageStr == "")
imageStr = "##";
const bool selected = (rImage == image);
if (ImGui::Selectable(imageStr.c_str(), selected))
if (ImGui::Selectable(imageStr.c_str(), selected)) {
image = rImage;
updated = true;
}
if (selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}
}

if (ImGui::Checkbox(("Is image##IsImage" + imguiId).c_str(), &isImage))
image = isImage ? "textures/white.jpg"_sid : resource::Material::emptyImage;
if (ImGui::Checkbox(("Is image##IsImage" + imguiId).c_str(), &isImage)) {
image = isImage ? "textures/white.png"_sid : StringId();
updated = true;
}
return updated;
}

void materialRenderImGui(void* data, std::string imguiId) {
Expand Down Expand Up @@ -132,29 +138,50 @@ void materialRenderImGui(void* data, std::string imguiId) {
//---------- Edit ----------//
ImGui::Text("Color");
if (!m->colorIsImage()) {
ImGui::SliderFloat("R##ColorR", &m->color.x, 0.0f, 1.0f);
ImGui::SliderFloat("G##ColorG", &m->color.y, 0.0f, 1.0f);
ImGui::SliderFloat("B##ColorB", &m->color.z, 0.0f, 1.0f);
vec3 color = m->getColor();
bool updated = false;
if (ImGui::SliderFloat("R##ColorR", &color.x, 0.0f, 1.0f))
updated = true;
if (ImGui::SliderFloat("G##ColorG", &color.y, 0.0f, 1.0f))
updated = true;
if (ImGui::SliderFloat("B##ColorB", &color.z, 0.0f, 1.0f))
updated = true;
if (updated)
m->setColor(color);
}
renderComboImage("Color", m->colorImage);
StringId colorImage = m->getColorImage();
if (renderComboImage("Color", colorImage))
m->setColorImage(colorImage);

ImGui::Text("Roughness");
if (!m->roughnessIsImage()) {
ImGui::SliderFloat("##Roughness", &m->roughness, 0.0f, 1.0f);
float roughness = m->getRoughness();
if (ImGui::SliderFloat("##Roughness", &roughness, 0.0f, 1.0f))
m->setRoughness(roughness);
}
renderComboImage("Roughness", m->roughnessImage);
StringId roughnessImage = m->getRoughnessImage();
if (renderComboImage("Roughness", roughnessImage))
m->setRoughnessImage(roughnessImage);

ImGui::Text("Metallic");
if (!m->metallicIsImage()) {
ImGui::SliderFloat("##Metallic", &m->metallic, 0.0f, 1.0f);
float metallic = m->getMetallic();
if (ImGui::SliderFloat("##Metallic", &metallic, 0.0f, 1.0f))
m->setMetallic(metallic);
}
renderComboImage("Metallic", m->metallicImage);
StringId metallicImage = m->getMetallicImage();
if (renderComboImage("Metallic", metallicImage))
m->setMetallicImage(metallicImage);

ImGui::Text("AO");
if (!m->aoIsImage()) {
ImGui::SliderFloat("##AO", &m->ao, 0.0f, 1.0f);
float ao = m->getAo();
if (ImGui::SliderFloat("##AO", &ao, 0.0f, 1.0f))
m->setAo(ao);
}
renderComboImage("AO", m->aoImage);
StringId aoImage = m->getAoImage();
if (renderComboImage("AO", aoImage))
m->setAoImage(aoImage);
}

template <>
Expand Down
5 changes: 3 additions & 2 deletions src/atta/component/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ void Manager::createDefaultImpl() {
cube.add<Name>()->set("Cube");
cube.add<Mesh>()->set("meshes/cube.obj");

resource::Material* rmt = resource::create<resource::Material>("Material", resource::Material::CreateInfo{});
rmt->color = {0.5, 0.5, 0.5};
resource::Material::CreateInfo mInfo{};
mInfo.color = {0.5, 0.5, 0.5};
resource::Material* rmt = resource::create<resource::Material>("Material", mInfo);
cube.add<Material>()->set(rmt);

// Light entity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
//--------------------------------------------------
// Atta Event Module
// materialLoad.h
// materialCreate.h
// Date: 2022-08-13
// By Breno Cunha Queiroz
//--------------------------------------------------
#ifndef ATTA_EVENT_EVENTS_MATERIAL_LOAD_H
#define ATTA_EVENT_EVENTS_MATERIAL_LOAD_H
#ifndef ATTA_EVENT_EVENTS_MATERIAL_CREATE_H
#define ATTA_EVENT_EVENTS_MATERIAL_CREATE_H
#include <atta/event/event.h>

namespace atta::event {

class MaterialLoad : public EventTyped<SID("MaterialLoad")> {
class MaterialCreate : public EventTyped<SID("MaterialCreate")> {
public:
MaterialLoad(StringId sid_) : sid(sid_) {}
MaterialCreate(StringId sid_) : sid(sid_) {}

const StringId sid;
};

} // namespace atta::event

#endif // ATTA_EVENT_EVENTS_MATERIAL_LOAD_H
#endif // ATTA_EVENT_EVENTS_MATERIAL_CREATE_H
22 changes: 22 additions & 0 deletions src/atta/event/events/materialDestroy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//--------------------------------------------------
// Atta Event Module
// materialDestroy.h
// Date: 2022-08-13
// By Breno Cunha Queiroz
//--------------------------------------------------
#ifndef ATTA_EVENT_EVENTS_MATERIAL_DESTROY_H
#define ATTA_EVENT_EVENTS_MATERIAL_DESTROY_H
#include <atta/event/event.h>

namespace atta::event {

class MaterialDestroy : public EventTyped<SID("MaterialDestroy")> {
public:
MaterialDestroy(StringId sid_) : sid(sid_) {}

const StringId sid;
};

} // namespace atta::event

#endif // ATTA_EVENT_EVENTS_MATERIAL_DESTROY_H
22 changes: 22 additions & 0 deletions src/atta/event/events/materialUpdate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//--------------------------------------------------
// Atta Event Module
// materialUpdate.h
// Date: 2022-08-13
// By Breno Cunha Queiroz
//--------------------------------------------------
#ifndef ATTA_EVENT_EVENTS_MATERIAL_UPDATE_H
#define ATTA_EVENT_EVENTS_MATERIAL_UPDATE_H
#include <atta/event/event.h>

namespace atta::event {

class MaterialUpdate : public EventTyped<SID("MaterialUpdate")> {
public:
MaterialUpdate(StringId sid_) : sid(sid_) {}

const StringId sid;
};

} // namespace atta::event

#endif // ATTA_EVENT_EVENTS_MATERIAL_UPDATE_H
22 changes: 22 additions & 0 deletions src/atta/event/events/meshUpdate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//--------------------------------------------------
// Atta Event Module
// meshUpdate.h
// Date: 2023-12-30
// By Breno Cunha Queiroz
//--------------------------------------------------
#ifndef ATTA_EVENT_EVENTS_MESH_UPDATE_H
#define ATTA_EVENT_EVENTS_MESH_UPDATE_H
#include <atta/event/event.h>

namespace atta::event {

class MeshUpdate : public EventTyped<SID("MeshUpdate")> {
public:
MeshUpdate(StringId sid_) : sid(sid_) {}

const StringId sid;
};

} // namespace atta::event

#endif // ATTA_EVENT_EVENTS_MESH_UPDATE_H
17 changes: 11 additions & 6 deletions src/atta/event/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
namespace atta::event {

using Callback = std::function<void(Event&)>;
#define BIND_EVENT_FUNC(x) std::bind(&x, this, std::placeholders::_1)
#define BIND_EVENT_FUNC(x) (void*)this, std::bind(&x, this, std::placeholders::_1)

// static void subscribe(Event::Type type, Callback&& callback) { getInstance().subscribeImpl(type, std::move(callback)); }
template <typename E>
void subscribe(Callback&& callback);
// static void subscribe(Event::Type type, Callback&& callback) { getInstance()._observers[type].push_back(callback); }
void subscribe(void* source, Callback&& callback);
template <typename E>
void unsubscribe(void* source, Callback&& callback);
void publish(Event& event);
void clear();

Expand All @@ -28,8 +28,13 @@ void clear();
namespace atta::event {

template <typename E>
void subscribe(Callback&& callback) {
Manager::getInstance().subscribeImpl(E::type, std::move(callback));
void subscribe(void* source, Callback&& callback) {
Manager::getInstance().subscribeImpl(E::type, source, std::move(callback));
}

template <typename E>
void unsubscribe(void* source, Callback&& callback) {
Manager::getInstance().unsubscribeImpl(E::type, source, std::move(callback));
}

} // namespace atta::event
Expand Down
28 changes: 26 additions & 2 deletions src/atta/event/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,31 @@ Manager& Manager::getInstance() {
return instance;
}

void Manager::subscribeImpl(Event::Type type, Callback&& callback) { _observers[type].push_back(callback); }
void Manager::subscribeImpl(Event::Type type, void* source, Callback&& callback) {
// Make sure source has only one observer for this type
for (size_t i = 0; i < _observers[type].size(); i++) {
if (_observers[type][i].source == source) {
_observers[type].erase(_observers[type].begin() + i);
LOG_WARN("evt::Manager", "An object must not subscribe to the same event more than once");
break;
}
}

_observers[type].push_back({source, callback});
}

void Manager::unsubscribeImpl(Event::Type type, void* source, Callback&& callback) {
// If there are no observers for this type
if (_observers.find(type) == _observers.end())
return;

for (size_t i = 0; i < _observers[type].size(); i++) {
if (_observers[type][i].source == source) {
_observers[type].erase(_observers[type].begin() + i);
break;
}
}
}

void Manager::publishImpl(Event& event) const {
Event::Type type = event.getType();
Expand All @@ -25,7 +49,7 @@ void Manager::publishImpl(Event& event) const {

// Loop over observers until event is handled
for (auto& observer : _observers.at(type)) {
observer(event);
observer.callback(event);
if (event.handled)
return;
}
Expand Down
13 changes: 10 additions & 3 deletions src/atta/event/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,24 @@ class Manager final {
static Manager& getInstance();

template <typename E>
friend void subscribe(Callback&& callback);
friend void subscribe(void* source, Callback&& callback);
template <typename E>
friend void unsubscribe(void* source, Callback&& callback);
friend void publish(Event& event);
friend void clear();

private:
void subscribeImpl(Event::Type type, Callback&& callback);
void subscribeImpl(Event::Type type, void* source, Callback&& callback);
void unsubscribeImpl(Event::Type type, void* source, Callback&& callback);

void publishImpl(Event& event) const;
void clearImpl();

std::unordered_map<Event::Type, std::vector<Callback>> _observers;
struct Observer {
void* source;
Callback callback;
};
std::unordered_map<Event::Type, std::vector<Observer>> _observers;
};

} // namespace atta::event
Expand Down
8 changes: 4 additions & 4 deletions src/atta/event/tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ TEST(Event, Subscribe) {

TestObserver observer;

event::subscribe<TestEvent>(std::bind(&TestObserver::handle, &observer, _1));
event::subscribe<TestEvent>(&observer, std::bind(&TestObserver::handle, &observer, _1));

EXPECT_EQ(observer.getSum(), 0);
}
Expand All @@ -79,11 +79,11 @@ TEST(Event, MultipleEventsObservers) {
event::publish(e0);

// The observer0 should not receive testEvents, so its sum stays in 0
event::subscribe<event::WindowMouseMove>(std::bind(&TestObserver::handle, &observer0, _1));
event::subscribe<event::WindowMouseMove>(&observer0, std::bind(&TestObserver::handle, &observer0, _1));
// The observer1 receives all testEvents after subscription
event::subscribe<TestEvent>(std::bind(&TestObserver::handle, &observer1, _1));
event::subscribe<TestEvent>(&observer1, std::bind(&TestObserver::handle, &observer1, _1));
// Because observer1 will consume the events, observer2 will not receive any event
event::subscribe<TestEvent>(std::bind(&TestObserver::handle, &observer2, _1));
event::subscribe<TestEvent>(&observer2, std::bind(&TestObserver::handle, &observer2, _1));

// Publish more two events, 2+4=6
event::publish(e1);
Expand Down
Loading

0 comments on commit 35dd8e9

Please sign in to comment.