diff options
Diffstat (limited to 'src/example')
-rw-r--r-- | src/example/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/example/button.cpp | 54 | ||||
-rw-r--r-- | src/example/game.cpp | 53 | ||||
-rw-r--r-- | src/example/gameloop.cpp | 7 |
4 files changed, 101 insertions, 16 deletions
diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 3ec5e43..8ef71bb 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -19,6 +19,5 @@ endfunction() add_example(asset_manager) add_example(savemgr) add_example(rendering_particle) -add_example(gameloop) add_example(game) - +add_example(button) diff --git a/src/example/button.cpp b/src/example/button.cpp new file mode 100644 index 0000000..00bdc28 --- /dev/null +++ b/src/example/button.cpp @@ -0,0 +1,54 @@ +#include <SDL2/SDL_timer.h> +#include <chrono> +#include <crepe/Component.h> +#include <crepe/ComponentManager.h> +#include <crepe/api/Animator.h> +#include <crepe/api/Button.h> +#include <crepe/api/Camera.h> +#include <crepe/api/Color.h> +#include <crepe/api/EventManager.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/Sprite.h> +#include <crepe/api/Texture.h> +#include <crepe/api/Transform.h> +#include <crepe/system/AnimatorSystem.h> +#include <crepe/system/InputSystem.h> +#include <crepe/system/RenderSystem.h> +#include <crepe/types.h> +#include <iostream> +using namespace crepe; +using namespace std; + +int main(int argc, char * argv[]) { + ComponentManager mgr; + RenderSystem sys{mgr}; + EventManager & event_mgr = EventManager::get_instance(); + InputSystem input_sys{mgr}; + AnimatorSystem asys{mgr}; + GameObject camera_obj = mgr.new_object("", "", vec2{1000, 1000}, 0, 1); + camera_obj.add_component<Camera>(Color::WHITE, ivec2{1080, 720}, vec2{2000, 2000}, 1.0f); + + GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); + auto s2 = Texture("asset/texture/test_ap43.png"); + bool button_clicked = false; + auto & sprite2 = button_obj.add_component<Sprite>( + s2, Color::GREEN, Sprite::FlipSettings{false, false}, 2, 1, 100); + std::function<void()> on_click = [&]() { std::cout << "button clicked" << std::endl; }; + std::function<void()> on_enter = [&]() { std::cout << "enter" << std::endl; }; + std::function<void()> on_exit = [&]() { std::cout << "exit" << std::endl; }; + auto & button + = button_obj.add_component<Button>(vec2{100, 100}, 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; + auto start = std::chrono::steady_clock::now(); + while (true) { + input_sys.update(); + sys.update(); + asys.update(); + event_mgr.dispatch_events(); + SDL_Delay(30); + } + return 0; +} diff --git a/src/example/game.cpp b/src/example/game.cpp index a8d6d1b..ade90c9 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -1,8 +1,10 @@ +#include "manager/ComponentManager.h" +#include "api/Scene.h" +#include "manager/Mediator.h" #include <crepe/api/BoxCollider.h> #include <crepe/api/Camera.h> #include <crepe/api/Color.h> #include <crepe/api/Event.h> -#include <crepe/api/EventManager.h> #include <crepe/api/GameObject.h> #include <crepe/api/LoopManager.h> #include <crepe/api/Rigidbody.h> @@ -21,21 +23,58 @@ class MyScript : public Script { 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: + { + Transform & tf = this->get_component<Transform>(); + tf.position.x -= 1; + break; + } + case Keycode::W: + { + Transform & tf = this->get_component<Transform>(); + tf.position.y -= 1; + break; + } + case Keycode::S: + { + Transform & tf = this->get_component<Transform>(); + tf.position.y += 1; + break; + } + case Keycode::D: + { + Transform & tf = this->get_component<Transform>(); + tf.position.x += 1; + break; + } + default: + break; + } + return false; + } + void init() { - subscribe<CollisionEvent>( - [this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); }); + 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() { - ComponentManager & mgr = this->component_manager; + + Mediator & m = this->mediator; + ComponentManager & mgr = m.component_manager; Color color(0, 0, 0, 255); float screen_size_width = 640; @@ -68,9 +107,9 @@ public: "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1); game_object1.add_component<Rigidbody>(Rigidbody::Data{ .mass = 1, - .gravity_scale = 0.01, + .gravity_scale = 0, .body_type = Rigidbody::BodyType::DYNAMIC, - .linear_velocity = {1, 1}, + .linear_velocity = {0, 0}, .constraints = {0, 0, 0}, .elastisity_coefficient = 1, .offset = {0, 0}, diff --git a/src/example/gameloop.cpp b/src/example/gameloop.cpp deleted file mode 100644 index a676f20..0000000 --- a/src/example/gameloop.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "crepe/api/LoopManager.h" -using namespace crepe; -int main() { - LoopManager gameloop; - gameloop.start(); - return 1; -} |