aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/CMakeLists.txt1
-rw-r--r--src/crepe/ComponentManager.h2
-rw-r--r--src/crepe/ComponentManager.hpp4
-rw-r--r--src/crepe/GameObject.h2
-rw-r--r--src/crepe/GameObject.hpp4
-rw-r--r--src/crepe/Script.cpp7
-rw-r--r--src/crepe/ScriptSystem.cpp30
-rw-r--r--src/crepe/ScriptSystem.h12
-rw-r--r--src/crepe/api/BehaviorScript.cpp1
-rw-r--r--src/crepe/api/BehaviorScript.h25
-rw-r--r--src/crepe/api/BehaviorScript.hpp19
-rw-r--r--src/crepe/api/CMakeLists.txt2
-rw-r--r--src/crepe/api/Script.cpp5
-rw-r--r--src/crepe/api/Script.h (renamed from src/crepe/Script.h)9
-rw-r--r--src/crepe/util/CMakeLists.txt2
-rw-r--r--src/crepe/util/fmt.cpp34
-rw-r--r--src/crepe/util/fmt.h10
-rw-r--r--src/crepe/util/log.cpp27
-rw-r--r--src/crepe/util/log.h2
19 files changed, 157 insertions, 41 deletions
diff --git a/src/crepe/CMakeLists.txt b/src/crepe/CMakeLists.txt
index d85aef0..8323490 100644
--- a/src/crepe/CMakeLists.txt
+++ b/src/crepe/CMakeLists.txt
@@ -9,7 +9,6 @@ target_sources(crepe PUBLIC
Rigidbody.cpp
Sprite.cpp
ScriptSystem.cpp
- Script.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
diff --git a/src/crepe/ComponentManager.h b/src/crepe/ComponentManager.h
index eab9b45..38f32e4 100644
--- a/src/crepe/ComponentManager.h
+++ b/src/crepe/ComponentManager.h
@@ -23,7 +23,7 @@ public:
public:
//! Add a component of a specific type
template <typename T, typename... Args>
- void add_component(uint32_t id, Args &&... args);
+ T & add_component(uint32_t id, Args &&... args);
//! Deletes all components of a specific type and id
template <typename T>
void delete_components_by_id(uint32_t id);
diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp
index 8fc1cba..e0242a2 100644
--- a/src/crepe/ComponentManager.hpp
+++ b/src/crepe/ComponentManager.hpp
@@ -7,7 +7,7 @@
namespace crepe {
template <class T, typename... Args>
-void ComponentManager::add_component(uint32_t id, Args &&... args) {
+T & ComponentManager::add_component(uint32_t id, Args &&... args) {
using namespace std;
static_assert(is_base_of<Component, T>::value, "add_component must recieve a derivative class of Component");
@@ -32,6 +32,8 @@ void ComponentManager::add_component(uint32_t id, Args &&... args) {
T * instance = new T(forward<Args>(args)...);
// store its unique_ptr in the vector<>
components[type][id].push_back(unique_ptr<T>(instance));
+
+ return *instance;
}
template <typename T>
diff --git a/src/crepe/GameObject.h b/src/crepe/GameObject.h
index 3588d9a..b5d6399 100644
--- a/src/crepe/GameObject.h
+++ b/src/crepe/GameObject.h
@@ -10,7 +10,7 @@ public:
GameObject(uint32_t id, std::string name, std::string tag, int layer);
template <typename T, typename... Args>
- void add_component(Args &&... args);
+ T & add_component(Args &&... args);
uint32_t id;
std::string name;
diff --git a/src/crepe/GameObject.hpp b/src/crepe/GameObject.hpp
index 5966fbf..8cd1abe 100644
--- a/src/crepe/GameObject.hpp
+++ b/src/crepe/GameObject.hpp
@@ -7,9 +7,9 @@
namespace crepe {
template <typename T, typename... Args>
-void GameObject::add_component(Args &&... args) {
+T & GameObject::add_component(Args &&... args) {
auto & mgr = ComponentManager::get_instance();
- mgr.add_component<T>(id, std::forward<Args>(args)...);
+ return mgr.add_component<T>(id, std::forward<Args>(args)...);
}
} // namespace crepe
diff --git a/src/crepe/Script.cpp b/src/crepe/Script.cpp
deleted file mode 100644
index 42e3666..0000000
--- a/src/crepe/Script.cpp
+++ /dev/null
@@ -1,7 +0,0 @@
-#include "Script.h"
-
-using namespace crepe;
-
-void Script::init() { }
-void Script::update() { }
-
diff --git a/src/crepe/ScriptSystem.cpp b/src/crepe/ScriptSystem.cpp
index e301c71..1a7bff4 100644
--- a/src/crepe/ScriptSystem.cpp
+++ b/src/crepe/ScriptSystem.cpp
@@ -1,8 +1,16 @@
-#include "util/log.h"
+#include <forward_list>
+#include <functional>
+#include <vector>
#include "ScriptSystem.h"
+#include "ComponentManager.h"
+#include "api/BehaviorScript.h"
+#include "api/Script.h"
+#include "util/log.h"
+using namespace std;
using namespace crepe;
+using namespace crepe::api;
ScriptSystem::ScriptSystem() {
dbg_trace();
@@ -17,6 +25,26 @@ ScriptSystem & ScriptSystem::get_instance() {
}
void ScriptSystem::update() {
+ using namespace std;
dbg_trace();
+
+ forward_list<Script *> scripts = this->get_scripts();
+ for (Script * script : scripts)
+ script->update();
+}
+
+forward_list<Script *> ScriptSystem::get_scripts() {
+ forward_list<Script *> scripts = {};
+ ComponentManager & mgr = ComponentManager::get_instance();
+ vector<reference_wrapper<BehaviorScript>> behavior_scripts = mgr.get_components_by_type<BehaviorScript>();
+
+ for (auto behavior_script_ref : behavior_scripts) {
+ BehaviorScript & behavior_script = behavior_script_ref.get();
+ Script * script = behavior_script.script.get();
+ if (script == nullptr) continue;
+ scripts.push_front(script);
+ }
+
+ return scripts;
}
diff --git a/src/crepe/ScriptSystem.h b/src/crepe/ScriptSystem.h
index e1ed290..e0b2a65 100644
--- a/src/crepe/ScriptSystem.h
+++ b/src/crepe/ScriptSystem.h
@@ -1,17 +1,27 @@
#pragma once
+#include <forward_list>
+
#include "System.h"
+namespace crepe::api {
+class Script;
+}
+
namespace crepe {
class ScriptSystem : public System {
public:
static ScriptSystem & get_instance();
- virtual void update();
+ void update();
private:
ScriptSystem();
~ScriptSystem();
+
+private:
+ std::forward_list<api::Script *> get_scripts();
+
};
}
diff --git a/src/crepe/api/BehaviorScript.cpp b/src/crepe/api/BehaviorScript.cpp
index 2dd933e..7a93f10 100644
--- a/src/crepe/api/BehaviorScript.cpp
+++ b/src/crepe/api/BehaviorScript.cpp
@@ -1,5 +1,6 @@
#include "../util/log.h"
+#include "Script.h"
#include "BehaviorScript.h"
using namespace crepe::api;
diff --git a/src/crepe/api/BehaviorScript.h b/src/crepe/api/BehaviorScript.h
index ba60a8c..756c2ca 100644
--- a/src/crepe/api/BehaviorScript.h
+++ b/src/crepe/api/BehaviorScript.h
@@ -1,20 +1,35 @@
#pragma once
-#include "../Script.h"
+#include <memory>
+
#include "../Component.h"
+namespace crepe {
+class ScriptSystem;
+class ComponentManager;
+}
+
namespace crepe::api {
-class BehaviorScript : public Script, public Component {
+class Script;
+
+class BehaviorScript : public Component {
protected:
- // only allow ComponentManager to instantiate scripts
- friend class ComponentManager;
+ friend class crepe::ComponentManager;
BehaviorScript();
public:
- // but allow uniqe_ptr to call the destructor (THIS IS VERY IMPORTANT)
virtual ~BehaviorScript() = default;
+public:
+ template<class T>
+ BehaviorScript & set_script();
+
+protected:
+ friend class crepe::ScriptSystem;
+ std::unique_ptr<Script> script = nullptr;
};
}
+#include "BehaviorScript.hpp"
+
diff --git a/src/crepe/api/BehaviorScript.hpp b/src/crepe/api/BehaviorScript.hpp
new file mode 100644
index 0000000..9a1f427
--- /dev/null
+++ b/src/crepe/api/BehaviorScript.hpp
@@ -0,0 +1,19 @@
+#pragma once
+
+#include <type_traits>
+
+#include "../util/log.h"
+#include "BehaviorScript.h"
+
+namespace crepe::api {
+
+template<class T>
+BehaviorScript & BehaviorScript::set_script() {
+ static_assert(std::is_base_of<Script, T>::value);
+ dbg_trace();
+ Script * s = new T();
+ this->script = std::unique_ptr<Script>(s);
+ return *this;
+}
+
+}
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index 86623de..6b337be 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -1,10 +1,12 @@
target_sources(crepe PUBLIC
# AudioSource.cpp
BehaviorScript.cpp
+ Script.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
# AudioSource.h
BehaviorScript.h
+ Script.h
)
diff --git a/src/crepe/api/Script.cpp b/src/crepe/api/Script.cpp
new file mode 100644
index 0000000..6259574
--- /dev/null
+++ b/src/crepe/api/Script.cpp
@@ -0,0 +1,5 @@
+#include "Script.h"
+
+using namespace crepe::api;
+
+
diff --git a/src/crepe/Script.h b/src/crepe/api/Script.h
index ba4073a..b1e6a95 100644
--- a/src/crepe/Script.h
+++ b/src/crepe/api/Script.h
@@ -1,11 +1,16 @@
#pragma once
namespace crepe {
+class ScriptSystem;
+}
+
+namespace crepe::api {
class Script {
+ friend class crepe::ScriptSystem;
protected:
- virtual void init();
- virtual void update();
+ virtual void init() {}
+ virtual void update() {}
// NOTE: additional *events* (like unity's OnDisable and OnEnable) should be
// implemented as member methods in derivative user script classes and
// registered in init(), otherwise this class will balloon in size with each
diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt
index 100f028..e2cffaf 100644
--- a/src/crepe/util/CMakeLists.txt
+++ b/src/crepe/util/CMakeLists.txt
@@ -1,9 +1,11 @@
target_sources(crepe PUBLIC
log.cpp
+ fmt.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
color.h
log.h
+ fmt.h
)
diff --git a/src/crepe/util/fmt.cpp b/src/crepe/util/fmt.cpp
new file mode 100644
index 0000000..3a43a6f
--- /dev/null
+++ b/src/crepe/util/fmt.cpp
@@ -0,0 +1,34 @@
+#include <string>
+#include <cstdio>
+#include <cstdarg>
+
+#include "fmt.h"
+
+using namespace std;
+
+string crepe::util::va_stringf(va_list args, const char * fmt) {
+ va_list args_copy;
+ va_copy(args_copy, args);
+
+ size_t sz = vsnprintf(NULL, 0, fmt, args_copy) + 1;
+ char * msg = (char *) malloc(sz);
+ va_end(args_copy);
+
+ vsnprintf(msg, sz, fmt, args);
+
+ string out = msg;
+ free(msg);
+
+ va_end(args);
+
+ return out;
+}
+
+string crepe::util::stringf(const char * fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ string out = va_stringf(args, fmt);
+ va_end(args);
+ return out;
+}
+
diff --git a/src/crepe/util/fmt.h b/src/crepe/util/fmt.h
new file mode 100644
index 0000000..5c45703
--- /dev/null
+++ b/src/crepe/util/fmt.h
@@ -0,0 +1,10 @@
+#pragma once
+
+#include <string>
+
+namespace crepe::util {
+
+std::string va_stringf(va_list args, const char * fmt);
+std::string stringf(const char * fmt, ...);
+
+}
diff --git a/src/crepe/util/log.cpp b/src/crepe/util/log.cpp
index f91d52c..b46620d 100644
--- a/src/crepe/util/log.cpp
+++ b/src/crepe/util/log.cpp
@@ -4,6 +4,7 @@
#include <string>
#include "log.h"
+#include "fmt.h"
using namespace crepe::util;
@@ -14,37 +15,27 @@ static const char * const LOG_PREFIX[] = {
[log_level::ERROR] = "[ERR] ",
};
-static void va_logf(enum log_level level, va_list args, const std::string fmt) {
- va_list args_copy;
- va_copy(args_copy, args);
-
- // prepend log level and ensure newline
- std::string format_fixed = LOG_PREFIX[level] + fmt;
- if (!format_fixed.ends_with("\n")) format_fixed += "\n";
-
- size_t sz = vsnprintf(NULL, 0, format_fixed.c_str(), args_copy) + 1;
- char * msg = (char *) malloc(sz);
- va_end(args_copy);
-
- vsnprintf(msg, sz, format_fixed.c_str(), args);
+static void log(enum log_level level, const std::string msg) {
+ using namespace std;
+ string final = string(LOG_PREFIX[level]) + msg;
+ if (!final.ends_with("\n")) final += "\n";
// TODO: also log to file or smth
- printf("%s", msg);
+ printf("%s", final.c_str());
fflush(stdout);
-
- free(msg);
}
void crepe::util::logf(const char * fmt, ...) {
va_list args;
va_start(args, fmt);
- va_logf(crepe::util::log_level::DEBUG, args, fmt);
+ log(log_level::DEBUG, va_stringf(args, fmt));
va_end(args);
}
void crepe::util::logf(log_level level, const char * fmt, ...) {
va_list args;
va_start(args, fmt);
- va_logf(level, args, fmt);
+ log(level, va_stringf(args, fmt));
va_end(args);
}
+
diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h
index fa5f633..3e36f0a 100644
--- a/src/crepe/util/log.h
+++ b/src/crepe/util/log.h
@@ -7,7 +7,7 @@
// utility macros
#define _crepe_logf_here(fmt, ...) \
- crepe::util::logf(util::log_level::DEBUG, "%s%s (%s:%d)" fmt "\n", \
+ crepe::util::logf(util::log_level::DEBUG, "%s%s (%s:%d)%s" fmt "\n", \
crepe::util::color::FG_WHITE, __PRETTY_FUNCTION__, \
__FILE_NAME__, __LINE__, crepe::util::color::RESET, \
__VA_ARGS__)