aboutsummaryrefslogtreecommitdiff
path: root/src/example
diff options
context:
space:
mode:
Diffstat (limited to 'src/example')
-rw-r--r--src/example/CMakeLists.txt22
-rw-r--r--src/example/audio_internal.cpp45
-rw-r--r--src/example/components_internal.cpp63
-rw-r--r--src/example/script.cpp32
4 files changed, 162 insertions, 0 deletions
diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt
new file mode 100644
index 0000000..53f584f
--- /dev/null
+++ b/src/example/CMakeLists.txt
@@ -0,0 +1,22 @@
+# all examples
+add_custom_target(examples)
+
+# add_example(target_name [SOURCES...])
+function(add_example target_name)
+ # if SOURCES is not specified
+ if(NOT ARGV1)
+ # A .cpp file with target_name exists, and should be used
+ set(sources ${target_name}.cpp)
+ else()
+ set(sources ${ARGV})
+ endif()
+
+ add_executable(${target_name} EXCLUDE_FROM_ALL ${sources})
+ target_link_libraries(${target_name} PUBLIC crepe)
+ add_dependencies(examples ${target_name})
+endfunction()
+
+add_example(audio_internal)
+add_example(components_internal)
+add_example(script)
+
diff --git a/src/example/audio_internal.cpp b/src/example/audio_internal.cpp
new file mode 100644
index 0000000..1199e2d
--- /dev/null
+++ b/src/example/audio_internal.cpp
@@ -0,0 +1,45 @@
+/** \file
+ *
+ * Standalone example for usage of the internal \c Sound class.
+ */
+
+#include <crepe/Sound.h>
+#include <crepe/util/log.h>
+
+#include <chrono>
+#include <thread>
+
+using namespace crepe;
+using namespace std;
+using namespace std::chrono_literals;
+using std::make_unique;
+
+int main() {
+ dbg_trace();
+
+ auto bgm = Sound("../mwe/audio/bgm.ogg");
+ auto sfx1 = Sound("../mwe/audio/sfx1.wav");
+ auto sfx2 = Sound("../mwe/audio/sfx2.wav");
+ auto sfx3 = Sound("../mwe/audio/sfx3.wav");
+
+ bgm.play();
+
+ // play each sample sequentially
+ this_thread::sleep_for(500ms);
+ sfx1.play();
+ this_thread::sleep_for(500ms);
+ sfx2.play();
+ bgm.pause();
+ this_thread::sleep_for(500ms);
+ sfx3.play();
+ bgm.play();
+ this_thread::sleep_for(500ms);
+
+ // play all samples simultaniously
+ sfx1.play();
+ sfx2.play();
+ sfx3.play();
+ this_thread::sleep_for(1000ms);
+
+ return 0;
+}
diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp
new file mode 100644
index 0000000..e2169e1
--- /dev/null
+++ b/src/example/components_internal.cpp
@@ -0,0 +1,63 @@
+/** \file
+ *
+ * Standalone example for usage of the internal ECS
+ */
+
+#include <cassert>
+#include <chrono>
+
+#include <crepe/Component.h>
+#include <crepe/ComponentManager.h>
+
+#include <crepe/api/Collider.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/api/Rigidbody.h>
+#include <crepe/api/Sprite.h>
+
+#include <crepe/util/log.h>
+
+using namespace crepe::api;
+using namespace crepe;
+using namespace std;
+
+#define OBJ_COUNT 100000
+
+int main() {
+ dbg_trace();
+
+ auto & mgr = ComponentManager::get_instance();
+
+ auto start_adding = chrono::high_resolution_clock::now();
+
+ GameObject * game_object[OBJ_COUNT];
+
+ for (int i = 0; i < OBJ_COUNT; ++i) {
+ game_object[i] = new GameObject(i, "Name", "Tag", 0);
+
+ game_object[i]->add_component<Sprite>("test");
+ game_object[i]->add_component<Rigidbody>(0, 0, i);
+ game_object[i]->add_component<Collider>(i);
+ }
+
+ auto stop_adding = chrono::high_resolution_clock::now();
+
+ auto sprites = mgr.get_components_by_type<Sprite>();
+ for (auto sprite : sprites) {
+ assert(sprite.get().path == "test");
+ }
+
+ auto stop_looping = chrono::high_resolution_clock::now();
+
+ for (int i = 0; i < OBJ_COUNT; ++i) {
+ delete game_object[i];
+ }
+
+ auto add_time = chrono::duration_cast<chrono::microseconds>(stop_adding
+ - start_adding);
+ auto loop_time = chrono::duration_cast<chrono::microseconds>(stop_looping
+ - stop_adding);
+ printf("add time: %ldus\n", add_time.count());
+ printf("loop time: %ldus\n", loop_time.count());
+
+ return 0;
+}
diff --git a/src/example/script.cpp b/src/example/script.cpp
new file mode 100644
index 0000000..6e5563c
--- /dev/null
+++ b/src/example/script.cpp
@@ -0,0 +1,32 @@
+/** \file
+ *
+ * Standalone example for usage of the script component and system
+ */
+
+#include <crepe/ComponentManager.h>
+#include <crepe/ScriptSystem.h>
+#include <crepe/util/log.h>
+
+#include <crepe/api/BehaviorScript.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/api/Script.h>
+
+using namespace crepe;
+using namespace crepe::api;
+using namespace std;
+
+class MyScript : public Script {
+ void update() { dbg_log("MY SCRIPT UPDATE"); }
+};
+
+int main() {
+ dbg_trace();
+
+ auto obj = GameObject(0, "name", "tag", 0);
+ obj.add_component<BehaviorScript>().set_script<MyScript>();
+
+ auto & sys = ScriptSystem::get_instance();
+ sys.update(); // -> MyScript::update
+
+ return 0;
+}