diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-10-25 20:59:34 +0200 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-10-25 20:59:34 +0200 |
commit | 15a4c4a1cf67e13be53ef423c51c56488f332ea9 (patch) | |
tree | 661084fc8171089ce32bf779f2b3a85d634b38e4 /src/example | |
parent | f472e0ba3bf54fc5055cf9f08925bed3f98a1dbc (diff) | |
parent | b3b762a34e7ccb4a0dcd041a693ac7180af16002 (diff) |
merge conficts fixed
Diffstat (limited to 'src/example')
-rw-r--r-- | src/example/CMakeLists.txt | 8 | ||||
-rw-r--r-- | src/example/asset_manager.cpp | 39 | ||||
-rw-r--r-- | src/example/components_internal.cpp | 4 | ||||
-rw-r--r-- | src/example/log.cpp | 24 | ||||
-rw-r--r-- | src/example/particle.cpp | 102 | ||||
-rw-r--r-- | src/example/physics.cpp | 30 | ||||
-rw-r--r-- | src/example/rendering.cpp | 71 | ||||
-rw-r--r-- | src/example/script.cpp | 15 |
8 files changed, 286 insertions, 7 deletions
diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 53f584f..fea6f60 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -17,6 +17,10 @@ function(add_example target_name) endfunction() add_example(audio_internal) -add_example(components_internal) +# add_example(components_internal) add_example(script) - +add_example(log) +add_example(rendering) +add_example(asset_manager) +add_example(particle) +add_example(physics) diff --git a/src/example/asset_manager.cpp b/src/example/asset_manager.cpp new file mode 100644 index 0000000..7e15d1f --- /dev/null +++ b/src/example/asset_manager.cpp @@ -0,0 +1,39 @@ + + +#include <crepe/Sound.h> +#include <crepe/api/AssetManager.h> +#include <crepe/api/Texture.h> + +using namespace crepe; +using namespace crepe::api; +int main() { + + // this needs to be called before the asset manager otherwise the destructor + // of sdl is not in the right order + { Texture test("../asset/texture/img.png"); } + // FIXME: make it so the issue described by the above comment is not possible + // (i.e. the order in which internal classes are instantiated should not + // impact the way the engine works). + + auto & mgr = AssetManager::get_instance(); + + { + // TODO: [design] the Sound class can't be directly included by the user as + // it includes SoLoud headers. + auto bgm = mgr.cache<Sound>("../mwe/audio/bgm.ogg"); + auto sfx1 = mgr.cache<Sound>("../mwe/audio/sfx1.wav"); + auto sfx2 = mgr.cache<Sound>("../mwe/audio/sfx2.wav"); + + auto img = mgr.cache<Texture>("../asset/texture/img.png"); + auto img1 = mgr.cache<Texture>("../asset/texture/second.png"); + } + + { + auto bgm = mgr.cache<Sound>("../mwe/audio/bgm.ogg"); + auto sfx1 = mgr.cache<Sound>("../mwe/audio/sfx1.wav"); + auto sfx2 = mgr.cache<Sound>("../mwe/audio/sfx2.wav"); + + auto img = mgr.cache<Texture>("../asset/texture/img.png"); + auto img1 = mgr.cache<Texture>("../asset/texture/second.png"); + } +} diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp index e2169e1..3c68974 100644 --- a/src/example/components_internal.cpp +++ b/src/example/components_internal.cpp @@ -9,7 +9,6 @@ #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> @@ -36,14 +35,13 @@ int main() { 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"); + assert(true); } auto stop_looping = chrono::high_resolution_clock::now(); diff --git a/src/example/log.cpp b/src/example/log.cpp new file mode 100644 index 0000000..3bc6d80 --- /dev/null +++ b/src/example/log.cpp @@ -0,0 +1,24 @@ +/** \file + * + * Standalone example for usage of the logging functions + */ + +#include <crepe/api/Config.h> +#include <crepe/util/log.h> + +using namespace crepe; +using namespace crepe::util; + +int main() { + auto & cfg = api::Config::get_instance(); + // make sure all log messages get printed + cfg.log.level = util::LogLevel::TRACE; + + dbg_trace(); + dbg_logf("cfg.log.color is equal to %d", cfg.log.color); + logf(LogLevel::INFO, "info message!"); + logf(LogLevel::WARNING, "very scary warning"); + logf(LogLevel::ERROR, "fatal error!!!"); + + return 0; +} diff --git a/src/example/particle.cpp b/src/example/particle.cpp new file mode 100644 index 0000000..53fa213 --- /dev/null +++ b/src/example/particle.cpp @@ -0,0 +1,102 @@ +#include "Particle.h" +#include "ParticleSystem.h" +#include "SDLApp.h" +#include "api/ParticleEmitter.h" +#include <chrono> +#include <crepe/Component.h> +#include <crepe/ComponentManager.h> +#include <crepe/api/GameObject.h> +#include <iostream> +#include <thread> + +using namespace crepe::api; +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 new file mode 100644 index 0000000..db69b9b --- /dev/null +++ b/src/example/physics.cpp @@ -0,0 +1,30 @@ +#include "PhysicsSystem.h" +#include <chrono> +#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> +#include <iostream> +#include <thread> + +using namespace crepe::api; +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(); + return 0; +} diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp new file mode 100644 index 0000000..1bf448c --- /dev/null +++ b/src/example/rendering.cpp @@ -0,0 +1,71 @@ + + +#include <crepe/ComponentManager.h> +#include <crepe/RenderSystem.h> +#include <crepe/api/GameObject.h> +#include <crepe/util/log.h> + +#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 <chrono> +#include <memory> + +using namespace std; +using namespace crepe; +using namespace crepe::api; + +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 & 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}); + } + + { + 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}); + } + + auto & sys = crepe::RenderSystem::get_instance(); + auto start = std::chrono::steady_clock::now(); + while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { + sys.update(); + } +} diff --git a/src/example/script.cpp b/src/example/script.cpp index 6e5563c..43f1c22 100644 --- a/src/example/script.cpp +++ b/src/example/script.cpp @@ -8,21 +8,32 @@ #include <crepe/util/log.h> #include <crepe/api/BehaviorScript.h> +#include <crepe/api/Config.h> #include <crepe/api/GameObject.h> #include <crepe/api/Script.h> +#include <crepe/api/Transform.h> using namespace crepe; using namespace crepe::api; using namespace std; class MyScript : public Script { - void update() { dbg_log("MY SCRIPT UPDATE"); } + void update() { + Transform & test = get_component<Transform>(); + dbg_logf("Transform(%.2f, %.2f)", test.position.x, test.position.y); + } }; int main() { - dbg_trace(); + auto & cfg = api::Config::get_instance(); + cfg.log.level = util::LogLevel::TRACE; auto obj = GameObject(0, "name", "tag", 0); + Point point = { + .x = 1.2, + .y = 3.4, + }; + obj.add_component<Transform>(point, 0, 0); obj.add_component<BehaviorScript>().set_script<MyScript>(); auto & sys = ScriptSystem::get_instance(); |