diff options
Diffstat (limited to 'src/example')
-rw-r--r-- | src/example/AITest.cpp | 90 | ||||
-rw-r--r-- | src/example/CMakeLists.txt | 14 | ||||
-rw-r--r-- | src/example/FontExample.cpp | 55 | ||||
-rw-r--r-- | src/example/asset_manager.cpp | 37 | ||||
-rw-r--r-- | src/example/audio_internal.cpp | 57 | ||||
-rw-r--r-- | src/example/button.cpp | 41 | ||||
-rw-r--r-- | src/example/components_internal.cpp | 60 | ||||
-rw-r--r-- | src/example/game.cpp | 292 | ||||
-rw-r--r-- | src/example/loadfont.cpp | 49 | ||||
-rw-r--r-- | src/example/log.cpp | 28 | ||||
-rw-r--r-- | src/example/particle.cpp | 102 | ||||
-rw-r--r-- | src/example/physics.cpp | 30 | ||||
-rw-r--r-- | src/example/rendering.cpp | 68 | ||||
-rw-r--r-- | src/example/rendering_particle.cpp | 73 | ||||
-rw-r--r-- | src/example/script.cpp | 55 |
15 files changed, 606 insertions, 445 deletions
diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp new file mode 100644 index 0000000..93ba500 --- /dev/null +++ b/src/example/AITest.cpp @@ -0,0 +1,90 @@ +#include <crepe/api/AI.h> +#include <crepe/api/BehaviorScript.h> +#include <crepe/api/Camera.h> +#include <crepe/api/Color.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/LoopManager.h> +#include <crepe/api/Rigidbody.h> +#include <crepe/api/Scene.h> +#include <crepe/api/Script.h> +#include <crepe/api/Sprite.h> +#include <crepe/manager/Mediator.h> +#include <crepe/types.h> + +using namespace crepe; +using namespace std; + +class Script1 : public Script { + bool shutdown(const ShutDownEvent & event) { + // Very dirty way of shutting down the game + throw "ShutDownEvent"; + return true; + } + + bool mousemove(const MouseMoveEvent & event) { + /*RefVector<AI> aivec = this->get_components<AI>(); + AI & ai = aivec.front().get(); + ai.flee_target + = vec2{static_cast<float>(event.mouse_x), static_cast<float>(event.mouse_y)};*/ + return true; + } + + void init() { + subscribe<ShutDownEvent>( + [this](const ShutDownEvent & ev) -> bool { return this->shutdown(ev); }); + subscribe<MouseMoveEvent>( + [this](const MouseMoveEvent & ev) -> bool { return this->mousemove(ev); }); + } +}; + +class Scene1 : public Scene { +public: + void load_scene() override { + Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; + + GameObject game_object1 = mgr.new_object("", "", vec2{0, 0}, 0, 1); + GameObject game_object2 = mgr.new_object("", "", vec2{0, 0}, 0, 1); + + Asset img{"asset/texture/test_ap43.png"}; + + Sprite & test_sprite = game_object1.add_component<Sprite>( + img, Sprite::Data{ + .color = Color::MAGENTA, + .flip = Sprite::FlipSettings{false, false}, + .sorting_in_layer = 2, + .order_in_layer = 2, + .size = {0, 100}, + .angle_offset = 0, + .position_offset = {0, 0}, + }); + + AI & ai = game_object1.add_component<AI>(3000); + // ai.arrive_on(); + // ai.flee_on(); + ai.path_follow_on(); + ai.make_oval_path(500, 1000, {0, -1000}, 1.5708, true); + ai.make_oval_path(1000, 500, {0, 500}, 4.7124, false); + game_object1.add_component<Rigidbody>(Rigidbody::Data{ + .mass = 0.1f, + .max_linear_velocity = 40, + }); + game_object1.add_component<BehaviorScript>().set_script<Script1>(); + + game_object2.add_component<Camera>(ivec2{1080, 720}, vec2{5000, 5000}, + Camera::Data{ + .bg_color = Color::WHITE, + .zoom = 1, + }); + } + + string get_name() const override { return "Scene1"; } +}; + +int main() { + LoopManager engine; + engine.add_scene<Scene1>(); + engine.start(); + + return 0; +} diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index fea6f60..f62414e 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -16,11 +16,9 @@ function(add_example target_name) 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) +add_example(rendering_particle) +add_example(game) +add_example(button) +add_example(loadfont) +add_example(FontExample) +add_example(AITest) diff --git a/src/example/FontExample.cpp b/src/example/FontExample.cpp new file mode 100644 index 0000000..6a334b1 --- /dev/null +++ b/src/example/FontExample.cpp @@ -0,0 +1,55 @@ +#include <SDL2/SDL_ttf.h> +#include <chrono> +#include <crepe/api/Camera.h> +#include <crepe/api/Config.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/LoopManager.h> +#include <crepe/api/Scene.h> +#include <crepe/api/Script.h> +#include <crepe/api/Text.h> +#include <crepe/facade/Font.h> +#include <crepe/facade/SDLContext.h> +#include <crepe/manager/EventManager.h> +#include <crepe/manager/Mediator.h> +#include <crepe/manager/ResourceManager.h> +#include <exception> +#include <iostream> +#include <memory> +using namespace crepe; +using namespace std; +using namespace std::chrono; +class TestScript : public Script { +public: + steady_clock::time_point start_time; + virtual void init() override { start_time = steady_clock::now(); } + virtual void update() override { + auto now = steady_clock::now(); + auto elapsed = duration_cast<seconds>(now - start_time).count(); + + if (elapsed >= 5) { + Mediator & med = mediator; + EventManager & event_mgr = med.event_manager; + event_mgr.trigger_event<ShutDownEvent>(); + } + } +}; +class TestScene : public Scene { +public: + void load_scene() override { + GameObject text_object = this->new_object("test", "test", vec2{0, 0}, 0, 1); + text_object.add_component<Text>(vec2(100, 100), vec2(0, 0), "OpenSymbol", + Text::Data{}); + text_object.add_component<BehaviorScript>().set_script<TestScript>(); + text_object.add_component<Camera>(ivec2{300, 300}, vec2{100, 100}, Camera::Data{}); + } + std::string get_name() const override { return "hey"; } +}; +int main() { + // Config& config = Config::get_instance(); + // config.log.level = Log::Level::TRACE; + LoopManager engine; + engine.add_scene<TestScene>(); + engine.start(); + + return 0; +} diff --git a/src/example/asset_manager.cpp b/src/example/asset_manager.cpp deleted file mode 100644 index ba18b62..0000000 --- a/src/example/asset_manager.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include <crepe/facade/Sound.h> -#include <crepe/api/AssetManager.h> -#include <crepe/api/Texture.h> - -using namespace crepe; - -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 deleted file mode 100644 index 0b36daa..0000000 --- a/src/example/audio_internal.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/** \file - * - * Standalone example for usage of the internal \c Sound class. - */ - -#include <crepe/facade/Sound.h> -#include <crepe/api/Config.h> -#include <crepe/util/log.h> - -#include <thread> - -using namespace crepe; -using namespace std; -using namespace std::chrono_literals; -using std::make_unique; - -// Unrelated stuff that is not part of this POC -int _ = []() { - // Show dbg_trace() output - auto & cfg = Config::get_instance(); - cfg.log.level = LogLevel::TRACE; - - return 0; // satisfy compiler -}(); - -int main() { - // Load a background track (Ogg Vorbis) - auto bgm = Sound("../mwe/audio/bgm.ogg"); - // Load three short samples (WAV) - auto sfx1 = Sound("../mwe/audio/sfx1.wav"); - auto sfx2 = Sound("../mwe/audio/sfx2.wav"); - auto sfx3 = Sound("../mwe/audio/sfx3.wav"); - - // Start the background track - bgm.play(); - - // Play each sample sequentially while pausing and resuming the background - // track - 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); - - // Stop all audio and exit - return EXIT_SUCCESS; -} diff --git a/src/example/button.cpp b/src/example/button.cpp new file mode 100644 index 0000000..4220588 --- /dev/null +++ b/src/example/button.cpp @@ -0,0 +1,41 @@ +#include <SDL2/SDL_timer.h> +#include <chrono> +#include <crepe/Component.h> +#include <crepe/api/Animator.h> +#include <crepe/api/Button.h> +#include <crepe/api/Camera.h> +#include <crepe/api/Color.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/Sprite.h> +#include <crepe/api/Texture.h> +#include <crepe/api/Transform.h> +#include <crepe/facade/SDLContext.h> +#include <crepe/manager/ComponentManager.h> +#include <crepe/manager/EventManager.h> +#include <crepe/system/AnimatorSystem.h> +#include <crepe/system/InputSystem.h> +#include <crepe/system/RenderSystem.h> +#include <crepe/types.h> +using namespace crepe; +using namespace std; + +int main(int argc, char * argv[]) { + Mediator mediator; + ComponentManager mgr{mediator}; + RenderSystem sys{mediator}; + EventManager event_mgr{mediator}; + InputSystem input_sys{mediator}; + SDLContext sdl_context{mediator}; + GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); + auto & camera = obj.add_component<Camera>( + ivec2{500, 500}, vec2{500, 500}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f}); + auto start = std::chrono::steady_clock::now(); + while (true) { + const keyboard_state_t & keyboard_state = sdl_context.get_keyboard_state(); + input_sys.update(); + sys.update(); + event_mgr.dispatch_events(); + SDL_Delay(30); + } + return 0; +} diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp deleted file mode 100644 index ea1eaad..0000000 --- a/src/example/components_internal.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/** \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; -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/game.cpp b/src/example/game.cpp new file mode 100644 index 0000000..22effd2 --- /dev/null +++ b/src/example/game.cpp @@ -0,0 +1,292 @@ +#include "api/CircleCollider.h" +#include "api/ParticleEmitter.h" +#include "api/Scene.h" +#include "manager/ComponentManager.h" +#include "manager/Mediator.h" +#include "types.h" +#include <crepe/api/BoxCollider.h> +#include <crepe/api/Camera.h> +#include <crepe/api/Color.h> +#include <crepe/api/Event.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/LoopManager.h> +#include <crepe/api/Rigidbody.h> +#include <crepe/api/Script.h> +#include <crepe/api/Sprite.h> +#include <crepe/api/Transform.h> +#include <crepe/api/Vector2.h> + +using namespace crepe; + +using namespace std; + +class MyScript1 : public Script { + bool flip = false; + bool oncollision(const CollisionEvent & test) { + Log::logf("Box {} script on_collision()", test.info.this_collider.game_object_id); + return true; + } + bool keypressed(const KeyPressEvent & test) { + Log::logf("Box script keypressed()"); + switch (test.key) { + case Keycode::A: { + Rigidbody & tf = this->get_component<Rigidbody>(); + tf.data.linear_velocity.x -= 1; + break; + } + case Keycode::W: { + Rigidbody & tf = this->get_component<Rigidbody>(); + // tf.data.linear_velocity.y -= 1; + tf.add_force_linear({0, -1}); + break; + } + case Keycode::S: { + Rigidbody & tf = this->get_component<Rigidbody>(); + tf.data.linear_velocity.y += 1; + break; + } + case Keycode::D: { + Rigidbody & tf = this->get_component<Rigidbody>(); + tf.data.linear_velocity.x += 1; + break; + } + case Keycode::E: { + if (flip) { + flip = false; + this->get_component<BoxCollider>().active = true; + this->get_components<Sprite>()[0].get().active = true; + this->get_component<CircleCollider>().active = false; + this->get_components<Sprite>()[1].get().active = false; + } else { + flip = true; + this->get_component<BoxCollider>().active = false; + this->get_components<Sprite>()[0].get().active = false; + this->get_component<CircleCollider>().active = true; + this->get_components<Sprite>()[1].get().active = true; + } + + //add collider switch + break; + } + case Keycode::Q: { + Rigidbody & rg = this->get_component<Rigidbody>(); + rg.data.angular_velocity = 1; + break; + } + default: + break; + } + return false; + } + + void init() { + Log::logf("init"); + subscribe<CollisionEvent>( + [this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); }); + subscribe<KeyPressEvent>( + [this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); }); + } + void update() { + Rigidbody & tf = this->get_component<Rigidbody>(); + Log::logf("linear_velocity.x {}", tf.data.linear_velocity.x); + Log::logf("linear_velocity.y {}", tf.data.linear_velocity.y); + // tf.data.linear_velocity = {0,0}; + } +}; + +class MyScript2 : public Script { + bool flip = false; + bool oncollision(const CollisionEvent & test) { + Log::logf("Box {} script on_collision()", test.info.this_collider.game_object_id); + return true; + } + bool keypressed(const KeyPressEvent & test) { + Log::logf("Box script keypressed()"); + switch (test.key) { + case Keycode::LEFT: { + Transform & tf = this->get_component<Transform>(); + tf.position.x -= 1; + break; + } + case Keycode::UP: { + Transform & tf = this->get_component<Transform>(); + tf.position.y -= 1; + break; + } + case Keycode::DOWN: { + Transform & tf = this->get_component<Transform>(); + tf.position.y += 1; + break; + } + case Keycode::RIGHT: { + Transform & tf = this->get_component<Transform>(); + tf.position.x += 1; + break; + } + case Keycode::PAUSE: { + if (flip) { + flip = false; + this->get_component<BoxCollider>().active = true; + this->get_components<Sprite>()[0].get().active = true; + this->get_component<CircleCollider>().active = false; + this->get_components<Sprite>()[1].get().active = false; + } else { + flip = true; + this->get_component<BoxCollider>().active = false; + this->get_components<Sprite>()[0].get().active = false; + this->get_component<CircleCollider>().active = true; + this->get_components<Sprite>()[1].get().active = true; + } + + //add collider switch + break; + } + default: + break; + } + return false; + } + + void init() { + Log::logf("init"); + subscribe<CollisionEvent>( + [this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); }); + subscribe<KeyPressEvent>( + [this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); }); + } + void update() { + // Retrieve component from the same GameObject this script is on + } +}; + +class ConcreteScene1 : public Scene { +public: + using Scene::Scene; + + void load_scene() { + + Color color(0, 0, 0, 255); + + float screen_size_width = 320; + float screen_size_height = 240; + float world_collider = 1000; + //define playable world + GameObject world = new_object( + "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1); + world.add_component<Rigidbody>(Rigidbody::Data{ + .mass = 0, + .gravity_scale = 0, + .body_type = Rigidbody::BodyType::STATIC, + .offset = {0, 0}, + }); + world.add_component<BoxCollider>( + vec2{world_collider, world_collider}, + vec2{0, 0 - (screen_size_height / 2 + world_collider / 2)}); // Top + world.add_component<BoxCollider>( + vec2{world_collider, world_collider}, + vec2{0, screen_size_height / 2 + world_collider / 2}); // Bottom + world.add_component<BoxCollider>( + vec2{world_collider, world_collider}, + vec2{0 - (screen_size_width / 2 + world_collider / 2), 0}); // Left + world.add_component<BoxCollider>( + vec2{world_collider, world_collider}, + vec2{screen_size_width / 2 + world_collider / 2, 0}); // right + world.add_component<Camera>( + ivec2{static_cast<int>(screen_size_width), static_cast<int>(screen_size_height)}, + vec2{screen_size_width, screen_size_height}, + Camera::Data{ + .bg_color = Color::WHITE, + .zoom = 1, + }); + + GameObject game_object1 = new_object( + "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1); + game_object1.add_component<Rigidbody>(Rigidbody::Data{ + .mass = 1, + .gravity_scale = 1, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = {0, 1}, + .constraints = {0, 0, 0}, + .elastisity_coefficient = 0, + .offset = {0, 0}, + }); + // add box with boxcollider + game_object1.add_component<BoxCollider>(vec2{20, 20}); + game_object1.add_component<BehaviorScript>().set_script<MyScript1>(); + + Asset img1{"asset/texture/square.png"}; + game_object1.add_component<Sprite>(img1, Sprite::Data{ + .size = {20, 20}, + }); + + //add circle with cirlcecollider deactiveated + game_object1.add_component<CircleCollider>(10).active = false; + Asset img2{"asset/texture/circle.png"}; + game_object1 + .add_component<Sprite>(img2, + Sprite::Data{ + .size = {20, 20}, + }) + .active + = false; + + GameObject game_object2 = new_object( + "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1); + game_object2.add_component<Rigidbody>(Rigidbody::Data{ + .mass = 1, + .gravity_scale = 0, + .body_type = Rigidbody::BodyType::STATIC, + .linear_velocity = {0, 0}, + .constraints = {0, 0, 0}, + .elastisity_coefficient = 1, + .offset = {0, 0}, + }); + // add box with boxcollider + game_object2.add_component<BoxCollider>(vec2{20, 20}); + game_object2.add_component<BehaviorScript>().set_script<MyScript2>(); + + game_object2.add_component<Sprite>(img1, Sprite::Data{ + .size = {20, 20}, + }); + + //add circle with cirlcecollider deactiveated + game_object2.add_component<CircleCollider>(10).active = false; + + game_object2 + .add_component<Sprite>(img2, + Sprite::Data{ + .size = {20, 20}, + }) + .active + = false; + Asset img5{"asset/texture/square.png"}; + + GameObject particle = new_object( + "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1); + auto & particle_image = particle.add_component<Sprite>(img5, Sprite::Data{ + .size = {5, 5}, + }); + auto & test + = particle.add_component<ParticleEmitter>(particle_image, ParticleEmitter::Data{ + .offset = {0, 0}, + .max_particles = 256, + .emission_rate = 1, + .min_speed = 10, + .max_speed = 20, + .min_angle = -20, + .max_angle = 20, + .begin_lifespan = 0, + .end_lifespan = 5, + }); + } + + string get_name() const { return "scene1"; } +}; + +int main(int argc, char * argv[]) { + + LoopManager gameloop; + gameloop.add_scene<ConcreteScene1>(); + gameloop.start(); + return 0; +} diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp new file mode 100644 index 0000000..e459332 --- /dev/null +++ b/src/example/loadfont.cpp @@ -0,0 +1,49 @@ +#include <SDL2/SDL_ttf.h> +#include <crepe/api/Asset.h> +#include <crepe/api/Text.h> +#include <crepe/facade/Font.h> +#include <crepe/facade/FontFacade.h> +#include <crepe/facade/SDLContext.h> +#include <crepe/manager/Mediator.h> +#include <crepe/manager/ResourceManager.h> +#include <exception> +#include <iostream> +#include <memory> +#include <optional> +using namespace crepe; +int main() { + + // SDLFontContext font_facade; + Mediator mediator; + FontFacade font_facade{}; + SDLContext sdl_context{mediator}; + // ComponentManager component_manager{mediator}; + ResourceManager resource_manager{mediator}; + try { + // Correct way to create a unique pointer for Text + std::unique_ptr<Text> label = std::make_unique<Text>( + 1, vec2(100, 100), vec2(0, 0), "OpenSymbol", Text::Data{}, "test text"); + // std::cout << "Path: " << label->font.get_path() << std::endl; + Asset asset1 = font_facade.get_font_asset("OpenSymbol"); + std::cout << asset1.get_path() << std::endl; + std::unique_ptr<Text> label2 = std::make_unique<Text>( + 1, vec2(100, 100), vec2(0, 0), "fsaafdafsdafsdafsdasfdds", Text::Data{}); + Asset asset = Asset("test test"); + label->font.emplace(asset); + std::cout << label->font.value().get_path() << std::endl; + // label2->font = std::make_optional(asset); + // std::cout << "Path: " << label2->font.get_path() << std::endl; + ResourceManager & resource_mgr = mediator.resource_manager; + const Font & res = resource_manager.get<Font>(label->font.value()); + // TTF_Font * test_font = res.get_font(); + // if (test_font == NULL) { + // std::cout << "error with font" << std::endl; + // } else { + // std::cout << "correct font retrieved" << std::endl; + // } + } catch (const std::exception & e) { + std::cout << "Standard exception thrown: " << e.what() << std::endl; + } + + return 0; +} diff --git a/src/example/log.cpp b/src/example/log.cpp deleted file mode 100644 index db8aa48..0000000 --- a/src/example/log.cpp +++ /dev/null @@ -1,28 +0,0 @@ -/** \file - * - * Standalone example for usage of the logging functions - */ - -#include <crepe/api/Config.h> -#include <crepe/util/log.h> - -using namespace crepe; - -// unrelated setup code -int _ = []() { - // make sure all log messages get printed - auto & cfg = Config::get_instance(); - cfg.log.level = LogLevel::TRACE; - - return 0; // satisfy compiler -}(); - -int main() { - dbg_trace(); - dbg_logf("test printf parameters: %d", 3); - logf(LogLevel::INFO, "info message"); - logf(LogLevel::WARNING, "warning"); - logf(LogLevel::ERROR, "error"); - - 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 deleted file mode 100644 index 2dbddc2..0000000 --- a/src/example/physics.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#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> -#include <crepe/system/PhysicsSystem.h> - -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 deleted file mode 100644 index b0ab60a..0000000 --- a/src/example/rendering.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include <crepe/ComponentManager.h> -#include <crepe/api/GameObject.h> -#include <crepe/system/RenderSystem.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; - -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/rendering_particle.cpp b/src/example/rendering_particle.cpp new file mode 100644 index 0000000..add43f4 --- /dev/null +++ b/src/example/rendering_particle.cpp @@ -0,0 +1,73 @@ +#include "api/Asset.h" +#include <crepe/Component.h> +#include <crepe/api/Animator.h> +#include <crepe/api/Button.h> +#include <crepe/api/Camera.h> +#include <crepe/api/Color.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/LoopManager.hpp> +#include <crepe/api/ParticleEmitter.h> +#include <crepe/api/Rigidbody.h> +#include <crepe/api/Sprite.h> +#include <crepe/api/Transform.h> +#include <crepe/manager/ComponentManager.h> +#include <crepe/manager/Mediator.h> +#include <crepe/types.h> +#include <iostream> + +using namespace crepe; +using namespace std; + +class TestScene : public Scene { +public: + void load_scene() { + + cout << "TestScene" << endl; + Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; + GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 1); + + Color color(255, 255, 255, 255); + + Asset img{"asset/spritesheet/spritesheet_test.png"}; + + Sprite & test_sprite = game_object.add_component<Sprite>( + img, Sprite::Data{ + .color = color, + .flip = Sprite::FlipSettings{false, false}, + .sorting_in_layer = 2, + .order_in_layer = 2, + .size = {0, 100}, + .angle_offset = 0, + .position_offset = {0, 0}, + }); + + //auto & anim = game_object.add_component<Animator>(test_sprite,ivec2{32, 64}, uvec2{4,1}, Animator::Data{}); + //anim.set_anim(0); + + auto & cam = game_object.add_component<Camera>(ivec2{720, 1280}, vec2{400, 400}, + Camera::Data{ + .bg_color = Color::WHITE, + }); + + function<void()> on_click = [&]() { cout << "button clicked" << std::endl; }; + function<void()> on_enter = [&]() { cout << "enter" << std::endl; }; + function<void()> on_exit = [&]() { cout << "exit" << std::endl; }; + + auto & button + = game_object.add_component<Button>(vec2{200, 200}, vec2{0, 0}, on_click, false); + button.on_mouse_enter = on_enter; + button.on_mouse_exit = on_exit; + button.is_toggle = true; + button.active = true; + } + + string get_name() const { return "TestScene"; }; +}; + +int main(int argc, char * argv[]) { + LoopManager engine; + engine.add_scene<TestScene>(); + engine.start(); + return 0; +} diff --git a/src/example/script.cpp b/src/example/script.cpp deleted file mode 100644 index dac7af3..0000000 --- a/src/example/script.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/** \file - * - * Standalone example for usage of the script component and system - */ - -#include <crepe/ComponentManager.h> -#include <crepe/system/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 std; - -// Unrelated stuff that is not part of this POC -int _ = []() { - // Show dbg_trace() output - auto & cfg = Config::get_instance(); - cfg.log.level = 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; -} |