aboutsummaryrefslogtreecommitdiff
path: root/src/example
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-07 20:08:09 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-07 20:08:09 +0100
commit1c4156ee127b14760ed3b1a0cd16ad12180c7ac6 (patch)
tree211505d5328f24c9f9beabf8f874d9e13ef92130 /src/example
parent9df087ede0b539ecbd2778236c7d1143362b384d (diff)
parent3d2428af8e8d9d49b4ade52d4806a7dae4cf1ab8 (diff)
merge `master` into `loek/savemgr`
Diffstat (limited to 'src/example')
-rw-r--r--src/example/CMakeLists.txt3
-rw-r--r--src/example/asset_manager.cpp2
-rw-r--r--src/example/audio_internal.cpp2
-rw-r--r--src/example/ecs.cpp55
-rw-r--r--src/example/particle.cpp102
-rw-r--r--src/example/physics.cpp28
-rw-r--r--src/example/rendering.cpp24
-rw-r--r--src/example/scene_manager.cpp75
-rw-r--r--src/example/script.cpp10
9 files changed, 151 insertions, 150 deletions
diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt
index 9698c41..36f9d4d 100644
--- a/src/example/CMakeLists.txt
+++ b/src/example/CMakeLists.txt
@@ -22,9 +22,10 @@ add_example(script)
add_example(log)
add_example(rendering)
add_example(asset_manager)
-add_example(particle)
add_example(physics)
add_example(savemgr)
add_example(proxy)
add_example(db)
+add_example(ecs)
+add_example(scene_manager)
diff --git a/src/example/asset_manager.cpp b/src/example/asset_manager.cpp
index ba18b62..cf64f89 100644
--- a/src/example/asset_manager.cpp
+++ b/src/example/asset_manager.cpp
@@ -1,6 +1,6 @@
-#include <crepe/facade/Sound.h>
#include <crepe/api/AssetManager.h>
#include <crepe/api/Texture.h>
+#include <crepe/facade/Sound.h>
using namespace crepe;
diff --git a/src/example/audio_internal.cpp b/src/example/audio_internal.cpp
index 0b36daa..1ea839d 100644
--- a/src/example/audio_internal.cpp
+++ b/src/example/audio_internal.cpp
@@ -3,8 +3,8 @@
* Standalone example for usage of the internal \c Sound class.
*/
-#include <crepe/facade/Sound.h>
#include <crepe/api/Config.h>
+#include <crepe/facade/Sound.h>
#include <crepe/util/log.h>
#include <thread>
diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp
new file mode 100644
index 0000000..e61c398
--- /dev/null
+++ b/src/example/ecs.cpp
@@ -0,0 +1,55 @@
+#include <iostream>
+
+#include <crepe/ComponentManager.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/api/Metadata.h>
+#include <crepe/api/Transform.h>
+
+using namespace crepe;
+using namespace std;
+
+int main() {
+ // Create a few GameObjects
+ try {
+ GameObject body(0, "body", "person", Vector2{0, 0}, 0, 1);
+ GameObject right_leg(1, "rightLeg", "person", Vector2{1, 1}, 0, 1);
+ GameObject left_leg(2, "leftLeg", "person", Vector2{1, 1}, 0, 1);
+ GameObject right_foot(3, "rightFoot", "person", Vector2{2, 2}, 0, 1);
+ GameObject left_foot(4, "leftFoot", "person", Vector2{2, 2}, 0, 1);
+
+ // Set the parent of each GameObject
+ right_foot.set_parent(right_leg);
+ left_foot.set_parent(left_leg);
+ right_leg.set_parent(body);
+ left_leg.set_parent(body);
+
+ // Adding a second Transform component is not allowed and will invoke an exception
+ body.add_component<Transform>(Vector2{10, 10}, 0, 1);
+ } catch (const exception & e) {
+ cerr << e.what() << endl;
+ }
+
+ // Get the Metadata and Transform components of each GameObject
+ ComponentManager & mgr = ComponentManager::get_instance();
+ vector<reference_wrapper<Metadata>> metadata
+ = mgr.get_components_by_type<Metadata>();
+ vector<reference_wrapper<Transform>> transform
+ = mgr.get_components_by_type<Transform>();
+
+ // Print the Metadata and Transform components
+ for (auto & m : metadata) {
+ cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name
+ << " Tag: " << m.get().tag << " Parent: " << m.get().parent
+ << " Children: ";
+ for (auto & c : m.get().children) {
+ cout << c << " ";
+ }
+ cout << endl;
+ }
+ for (auto & t : transform) {
+ cout << "Id: " << t.get().game_object_id << " Position: ["
+ << t.get().position.x << ", " << t.get().position.y << "]" << endl;
+ }
+
+ return 0;
+}
diff --git a/src/example/particle.cpp b/src/example/particle.cpp
deleted file mode 100644
index 83b1e8a..0000000
--- a/src/example/particle.cpp
+++ /dev/null
@@ -1,102 +0,0 @@
-#include <chrono>
-#include <iostream>
-#include <thread>
-
-#include <crepe/Component.h>
-#include <crepe/ComponentManager.h>
-#include <crepe/Particle.h>
-#include <crepe/facade/SDLApp.h>
-#include <crepe/api/GameObject.h>
-#include <crepe/api/ParticleEmitter.h>
-#include <crepe/system/ParticleSystem.h>
-
-using namespace crepe;
-using namespace std;
-
-const int WINDOW_WIDTH = 800;
-const int WINDOW_HEIGHT = 600;
-
-int main(int argc, char * argv[]) {
- SDLApp app(WINDOW_WIDTH, WINDOW_HEIGHT);
-
- if (!app.initialize()) {
- cerr << "Failed to initialize SDLApp." << endl;
- return 1;
- }
-
- GameObject * game_object[1];
- game_object[0] = new GameObject(0, "Name", "Tag", 0);
-
- // FIXME: all systems are singletons, so this shouldn't even compile.
- ParticleSystem particle_system;
-
- unsigned int max_particles = 100; // maximum number of particles
- unsigned int emission_rate = 10; // particles created per second
- unsigned int speed = 50; // base speed of particles
- unsigned int speed_offset = 10; // random offset for particle speed
- unsigned int angle = 270; // base angle of particle emission
- unsigned int angle_offset = 30; // random offset for particle angle
- float begin_lifespan = 0.0f; // beginning lifespan of particles
- float end_lifespan = 6.0f; // ending lifespan of particles
-
- // Vector to hold all the emitters
- // vector<ParticleEmitter> emitters;
- game_object[0]->add_component<ParticleEmitter>(
- max_particles, emission_rate, speed, speed_offset, angle, angle_offset,
- begin_lifespan, end_lifespan);
-
- // Loop to create 1000 emitters
- // for (unsigned int i = 0; i < 1000; ++i) {
- // ParticleEmitter emitter(maxParticles, emissionRate, speed, speedOffset, angle, angleOffset, beginLifespan, endLifespan);
-
- // // Set a position for each emitter, modifying the position for demonstration
- // emitter.m_position = {static_cast<float>(200 + (i % 100)), static_cast<float>(200 + (i / 100) * 10)}; // Adjust position for each emitter
-
- // emitters.push_back(emitter); // Add the emitter to the vector
- // }
- float delta_time = 0.1f;
- bool running = true;
- cout << "start loop " << endl;
- while (running) {
- app.handle_events(running);
-
- // Start timing
- auto start = chrono::high_resolution_clock::now();
-
- // POC CODE
- particle_system.update();
- // POC CODE
-
- // End timing
- auto end = chrono::high_resolution_clock::now();
- chrono::duration<float, milli> duration
- = end - start; // get duration in milliseconds
-
- cout << "Update took " << duration.count() << " ms" << endl;
- app.clear_screen();
-
- start = chrono::high_resolution_clock::now();
- // render particles using the draw_square method from SDLApp
- ComponentManager & mgr = ComponentManager::get_instance();
- std::vector<std::reference_wrapper<ParticleEmitter>> emitters
- = mgr.get_components_by_type<ParticleEmitter>();
- for (const ParticleEmitter & emitter : emitters) {
- for (const Particle & particle : emitter.particles) {
- if (particle.active)
- app.draw_square(particle.position.x, particle.position.y,
- 5); // draw each particle
- }
- }
-
- app.present_screen();
- end = chrono::high_resolution_clock::now();
- duration = end - start; // get duration in milliseconds
-
- cout << "screen took " << duration.count() << " ms" << endl;
-
- this_thread::sleep_for(chrono::milliseconds(20)); // simulate ~50 FPS
- }
-
- app.clean_up();
- return 0;
-}
diff --git a/src/example/physics.cpp b/src/example/physics.cpp
index 2dbddc2..848f857 100644
--- a/src/example/physics.cpp
+++ b/src/example/physics.cpp
@@ -1,10 +1,5 @@
-#include <chrono>
-#include <iostream>
-#include <thread>
-
#include <crepe/Component.h>
#include <crepe/ComponentManager.h>
-#include <crepe/api/Force.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Transform.h>
@@ -14,17 +9,16 @@ using namespace crepe;
using namespace std;
int main(int argc, char * argv[]) {
- PhysicsSystem physics_system;
- GameObject * game_object[2];
- game_object[1] = new GameObject(2, "Name", "Tag", 0); // not found not used
- game_object[0] = new GameObject(5, "Name", "Tag", 0);
- Point point = {
- .x = 0,
- .y = 0,
- };
- game_object[0]->add_component<Transform>(point, 0, 0);
- game_object[0]->add_component<Rigidbody>(1, 1, BodyType::DYNAMIC);
- game_object[0]->add_component<Force>(1, 0);
- physics_system.update();
+ GameObject * game_object;
+ game_object = new GameObject(0, "Name", "Tag", Vector2{0, 0}, 0, 0);
+ game_object->add_component<Rigidbody>(Rigidbody::Data{
+ .mass = 1,
+ .gravity_scale = 1,
+ .body_type = Rigidbody::BodyType::DYNAMIC,
+ .constraints = {0, 0, 0},
+ .use_gravity = true,
+ .bounce = false,
+ });
+ delete game_object;
return 0;
}
diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp
index b0ab60a..d554a8a 100644
--- a/src/example/rendering.cpp
+++ b/src/example/rendering.cpp
@@ -5,10 +5,10 @@
#include <crepe/api/AssetManager.h>
#include <crepe/api/Color.h>
-#include <crepe/api/Point.h>
#include <crepe/api/Sprite.h>
#include <crepe/api/Texture.h>
#include <crepe/api/Transform.h>
+#include <crepe/api/Vector2.h>
#include <chrono>
#include <memory>
@@ -17,22 +17,16 @@ using namespace std;
using namespace crepe;
int main() {
-
dbg_trace();
- auto obj = GameObject(0, "name", "tag", 0);
- auto obj1 = GameObject(1, "name", "tag", 0);
- auto obj2 = GameObject(2, "name", "tag", 0);
+ auto obj = GameObject(0, "name", "tag", Vector2{0, 0}, 1, 1);
+ auto obj1 = GameObject(1, "name", "tag", Vector2{500, 0}, 1, 0.1);
+ auto obj2 = GameObject(2, "name", "tag", Vector2{800, 0}, 1, 0.1);
auto & mgr = AssetManager::get_instance();
// Normal adding components
{
Color color(0, 0, 0, 0);
- Point point = {
- .x = 0,
- .y = 0,
- };
- obj.add_component<Transform>(point, 1, 1);
obj.add_component<Sprite>(
make_shared<Texture>("../asset/texture/img.png"), color,
FlipSettings{true, true});
@@ -40,22 +34,12 @@ int main() {
{
Color color(0, 0, 0, 0);
- Point point = {
- .x = 500,
- .y = 0,
- };
- obj1.add_component<Transform>(point, 0, 0.1);
auto img = mgr.cache<Texture>("../asset/texture/second.png");
obj1.add_component<Sprite>(img, color, FlipSettings{true, true});
}
{
Color color(0, 0, 0, 0);
- Point point = {
- .x = 800,
- .y = 0,
- };
- //obj.add_component<Transform>(point, 0, 0.1);
auto img = mgr.cache<Texture>("../asset/texture/second.png");
obj2.add_component<Sprite>(img, color, FlipSettings{true, true});
}
diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp
new file mode 100644
index 0000000..f46dc36
--- /dev/null
+++ b/src/example/scene_manager.cpp
@@ -0,0 +1,75 @@
+#include <iostream>
+
+#include <crepe/ComponentManager.h>
+#include <crepe/api/GameObject.h>
+#include <crepe/api/Metadata.h>
+#include <crepe/api/Scene.h>
+#include <crepe/api/SceneManager.h>
+#include <crepe/api/Vector2.h>
+
+using namespace crepe;
+using namespace std;
+
+class ConcreteScene1 : public Scene {
+public:
+ ConcreteScene1(string name) : Scene(name) {}
+
+ void load_scene() {
+ GameObject object1(0, "scene_1", "tag_scene_1", Vector2{0, 0}, 0, 1);
+ GameObject object2(1, "scene_1", "tag_scene_1", Vector2{1, 0}, 0, 1);
+ GameObject object3(2, "scene_1", "tag_scene_1", Vector2{2, 0}, 0, 1);
+ }
+};
+
+class ConcreteScene2 : public Scene {
+public:
+ ConcreteScene2(string name) : Scene(name) {}
+
+ void load_scene() {
+ GameObject object1(0, "scene_2", "tag_scene_2", Vector2{0, 0}, 0, 1);
+ GameObject object2(1, "scene_2", "tag_scene_2", Vector2{0, 1}, 0, 1);
+ GameObject object3(2, "scene_2", "tag_scene_2", Vector2{0, 2}, 0, 1);
+ GameObject object4(3, "scene_2", "tag_scene_2", Vector2{0, 3}, 0, 1);
+ }
+};
+
+int main() {
+ SceneManager & scene_mgr = SceneManager::get_instance();
+
+ // Add the scenes to the scene manager
+ scene_mgr.add_scene<ConcreteScene1>("scene1");
+ scene_mgr.add_scene<ConcreteScene2>("scene2");
+
+ // There is no need to call set_next_scene() at the beginnen, because the first scene will be automatically set as the next scene
+ // Load scene1 (the first scene added)
+ scene_mgr.load_next_scene();
+
+ // Get the Metadata components of each GameObject of Scene1
+ ComponentManager & component_mgr = ComponentManager::get_instance();
+ vector<reference_wrapper<Metadata>> metadata
+ = component_mgr.get_components_by_type<Metadata>();
+
+ cout << "Metadata components of Scene1:" << endl;
+ // Print the Metadata
+ for (auto & m : metadata) {
+ cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name
+ << " Tag: " << m.get().tag << endl;
+ }
+
+ // Set scene2 as the next scene
+ scene_mgr.set_next_scene("scene2");
+ // Load scene2
+ scene_mgr.load_next_scene();
+
+ // Get the Metadata components of each GameObject of Scene2
+ metadata = component_mgr.get_components_by_type<Metadata>();
+
+ cout << "Metadata components of Scene2:" << endl;
+ // Print the Metadata
+ for (auto & m : metadata) {
+ cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name
+ << " Tag: " << m.get().tag << endl;
+ }
+
+ return 0;
+}
diff --git a/src/example/script.cpp b/src/example/script.cpp
index dac7af3..9e8b147 100644
--- a/src/example/script.cpp
+++ b/src/example/script.cpp
@@ -36,18 +36,12 @@ class MyScript : public Script {
int main() {
// Create game object with Transform and BehaviorScript components
- auto obj = GameObject(0, "name", "tag", 0);
- obj.add_component<Transform>(
- Point{
- .x = 1.2,
- .y = 3.4,
- },
- 0, 0);
+ auto obj = GameObject(0, "name", "tag", Vector2{1.2, 3.4}, 0, 1);
obj.add_component<BehaviorScript>().set_script<MyScript>();
// Get ScriptSystem singleton instance (this would normally be done from the
// game loop)
- auto & sys = ScriptSystem::get_instance();
+ ScriptSystem sys;
// Update all scripts. This should result in MyScript::update being called
sys.update();