diff options
Diffstat (limited to 'src/example')
-rw-r--r-- | src/example/AITest.cpp | 50 | ||||
-rw-r--r-- | src/example/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/example/FontExample.cpp | 55 | ||||
-rw-r--r-- | src/example/button.cpp | 16 | ||||
-rw-r--r-- | src/example/game.cpp | 292 | ||||
-rw-r--r-- | src/example/loadfont.cpp | 12 | ||||
-rw-r--r-- | src/example/rendering_particle.cpp | 71 | ||||
-rw-r--r-- | src/example/replay.cpp | 89 |
8 files changed, 174 insertions, 414 deletions
diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp index 93ba500..4c4e25e 100644 --- a/src/example/AITest.cpp +++ b/src/example/AITest.cpp @@ -30,10 +30,12 @@ class Script1 : public Script { } void init() { - subscribe<ShutDownEvent>( - [this](const ShutDownEvent & ev) -> bool { return this->shutdown(ev); }); - subscribe<MouseMoveEvent>( - [this](const MouseMoveEvent & ev) -> bool { return this->mousemove(ev); }); + subscribe<ShutDownEvent>([this](const ShutDownEvent & ev) -> bool { + return this->shutdown(ev); + }); + subscribe<MouseMoveEvent>([this](const MouseMoveEvent & ev) -> bool { + return this->mousemove(ev); + }); } }; @@ -43,21 +45,23 @@ public: 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); + 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"}; + 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}, - }); + 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(); @@ -65,17 +69,19 @@ public: 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{ + 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, - }); + 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"; } diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index f62414e..afe6cb7 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -17,8 +17,7 @@ function(add_example target_name) endfunction() add_example(rendering_particle) -add_example(game) add_example(button) +add_example(replay) add_example(loadfont) -add_example(FontExample) add_example(AITest) diff --git a/src/example/FontExample.cpp b/src/example/FontExample.cpp deleted file mode 100644 index 6a334b1..0000000 --- a/src/example/FontExample.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#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/button.cpp b/src/example/button.cpp index 4220588..ea7f528 100644 --- a/src/example/button.cpp +++ b/src/example/button.cpp @@ -21,14 +21,16 @@ 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); + 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}); + 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(); diff --git a/src/example/game.cpp b/src/example/game.cpp deleted file mode 100644 index 3975650..0000000 --- a/src/example/game.cpp +++ /dev/null @@ -1,292 +0,0 @@ -#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(duration_t) { - 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 index e459332..dd7caff 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -15,19 +15,21 @@ int main() { // SDLFontContext font_facade; Mediator mediator; - FontFacade font_facade{}; - SDLContext sdl_context{mediator}; + FontFacade font_facade {}; + SDLContext sdl_context {mediator}; // ComponentManager component_manager{mediator}; - ResourceManager resource_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"); + 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{}); + 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; diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index add43f4..7942fce 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -1,4 +1,7 @@ + + #include "api/Asset.h" +#include "api/Text.h" #include <crepe/Component.h> #include <crepe/api/Animator.h> #include <crepe/api/Button.h> @@ -13,7 +16,6 @@ #include <crepe/manager/ComponentManager.h> #include <crepe/manager/Mediator.h> #include <crepe/types.h> -#include <iostream> using namespace crepe; using namespace std; @@ -21,45 +23,52 @@ 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); + GameObject game_object = new_object("", "", vec2 {0, 0}, 0, 1); Color color(255, 255, 255, 255); - Asset img{"asset/spritesheet/spritesheet_test.png"}; + Asset img {"asset/texture/square.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}, - }); + img, + Sprite::Data { + .color = color, + .flip = Sprite::FlipSettings {false, false}, + .sorting_in_layer = 2, + .order_in_layer = 2, + .size = {1, 1}, + .angle_offset = 0, + .position_offset = {0, 1}, + .world_space = false, + } + ); + //auto & emitter = game_object.add_component<ParticleEmitter>(test_sprite, ParticleEmitter::Data{}); - //auto & anim = game_object.add_component<Animator>(test_sprite,ivec2{32, 64}, uvec2{4,1}, Animator::Data{}); - //anim.set_anim(0); + Sprite & test_sprite1 = game_object.add_component<Sprite>( + img, + Sprite::Data { + .color = color, + .size = {1, 1}, + .position_offset = {0, -1}, + .world_space = false, + } + ); - auto & cam = game_object.add_component<Camera>(ivec2{720, 1280}, vec2{400, 400}, - Camera::Data{ - .bg_color = Color::WHITE, - }); + auto & cam = game_object.add_component<Camera>( + ivec2 {1280, 720}, vec2 {5, 5}, + Camera::Data { + .bg_color = Color::WHITE, + .postion_offset = {1000, 1000}, + } + ); - 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; }; + /* + game_object.add_component<Text>(vec2{1, 1}, vec2{0, -0.5}, "ComicSansMS", + Text::Data{.text_color = Color::RED}, "test TEST"); - 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; + game_object.add_component<Text>(vec2{1, 1}, vec2{0, 0.5}, "ComicSansMS", + Text::Data{.text_color = Color::BLACK}, "TEST test"); + */ } string get_name() const { return "TestScene"; }; diff --git a/src/example/replay.cpp b/src/example/replay.cpp new file mode 100644 index 0000000..00a6502 --- /dev/null +++ b/src/example/replay.cpp @@ -0,0 +1,89 @@ +#include <crepe/api/Config.h> +#include <crepe/api/Engine.h> +#include <crepe/api/Script.h> + +using namespace crepe; +using namespace std; + +class AnimationScript : public Script { + Transform * transform; + float t = 0; + + void init() { transform = &get_component<Transform>(); } + + void update() { + t += 0.05; + transform->position = {sin(t), cos(t)}; + } +}; + +class Timeline : public Script { + unsigned i = 0; + recording_t recording; + + void update() { + switch (i++) { + default: + break; + case 10: + logf("record start"); + replay.record_start(); + break; + case 60: + logf("record end, playing recording"); + this->recording = replay.record_end(); + replay.play(this->recording); + break; + case 61: + logf("done, releasing recording"); + replay.release(this->recording); + break; + case 72: + logf("exit"); + queue_event<ShutDownEvent>(); + break; + }; + } +}; + +class TestScene : public Scene { +public: + using Scene::Scene; + + void load_scene() { + Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; + + GameObject cam = mgr.new_object("cam"); + cam.add_component<Camera>( + ivec2 {640, 480}, vec2 {3, 3}, + Camera::Data { + .bg_color = Color::WHITE, + } + ); + + GameObject square = mgr.new_object("square"); + square.add_component<Sprite>( + Asset {"asset/texture/square.png"}, + Sprite::Data { + .size = {0.5, 0.5}, + } + ); + square.add_component<BehaviorScript>().set_script<AnimationScript>(); + + GameObject scapegoat = mgr.new_object(""); + scapegoat.add_component<BehaviorScript>().set_script<Timeline>(); + } + + string get_name() const { return "scene1"; } +}; + +int main(int argc, char * argv[]) { + Config & cfg = Config::get_instance(); + cfg.log.level = Log::Level::DEBUG; + + Engine engine; + + engine.add_scene<TestScene>(); + return engine.main(); +} |