diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-31 18:41:30 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-31 18:41:30 +0100 |
commit | 8e3367b186e60eb1e33bf58a066823cb00a7566e (patch) | |
tree | c4038a31993767276efec5fa1b1a37dff3b79465 /src/example | |
parent | b7df77d6cc26cb9ee46891d7108f01734b3104dd (diff) | |
parent | 35ef3ba91ce9e00466508f2388f4c1dd2321b505 (diff) |
Merge branch 'master' into poc/audio-miniaudio
Diffstat (limited to 'src/example')
-rw-r--r-- | src/example/CMakeLists.txt | 26 | ||||
-rw-r--r-- | src/example/asset_manager.cpp | 39 | ||||
-rw-r--r-- | src/example/audio_internal.cpp | 45 | ||||
-rw-r--r-- | src/example/components_internal.cpp | 61 | ||||
-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 | 54 |
9 files changed, 452 insertions, 0 deletions
diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt new file mode 100644 index 0000000..fea6f60 --- /dev/null +++ b/src/example/CMakeLists.txt @@ -0,0 +1,26 @@ +# 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) +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/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..3c68974 --- /dev/null +++ b/src/example/components_internal.cpp @@ -0,0 +1,61 @@ +/** \file + * + * Standalone example for usage of the internal ECS + */ + +#include <cassert> +#include <chrono> + +#include <crepe/Component.h> +#include <crepe/ComponentManager.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); + } + + auto stop_adding = chrono::high_resolution_clock::now(); + + auto sprites = mgr.get_components_by_type<Sprite>(); + for (auto sprite : sprites) { + assert(true); + } + + 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/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 new file mode 100644 index 0000000..cda9591 --- /dev/null +++ b/src/example/script.cpp @@ -0,0 +1,54 @@ +/** \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/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; + +// Unrelated stuff that is not part of this POC +int _ = [] () { + // Show dbg_trace() output + auto & cfg = api::Config::get_instance(); + cfg.log.level = util::LogLevel::TRACE; + + return 0; // satisfy compiler +}(); + + + +// User-defined script: +class MyScript : public Script { + void update() { + // Retrieve component from the same GameObject this script is on + Transform & test = get_component<Transform>(); + dbg_logf("Transform(%.2f, %.2f)", test.position.x, test.position.y); + } +}; + +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); + 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(); + // Update all scripts. This should result in MyScript::update being called + sys.update(); + + return EXIT_SUCCESS; +} + |