diff options
Diffstat (limited to 'src/example')
-rw-r--r-- | src/example/AITest.cpp | 96 | ||||
-rw-r--r-- | src/example/CMakeLists.txt | 13 | ||||
-rw-r--r-- | src/example/audio_internal.cpp | 45 | ||||
-rw-r--r-- | src/example/button.cpp | 43 | ||||
-rw-r--r-- | src/example/components_internal.cpp | 60 | ||||
-rw-r--r-- | src/example/loadfont.cpp | 51 | ||||
-rw-r--r-- | src/example/rendering_particle.cpp | 74 | ||||
-rw-r--r-- | src/example/replay.cpp | 89 | ||||
-rw-r--r-- | src/example/script.cpp | 33 |
9 files changed, 362 insertions, 142 deletions
diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp new file mode 100644 index 0000000..4c4e25e --- /dev/null +++ b/src/example/AITest.cpp @@ -0,0 +1,96 @@ +#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 6df4ce7..afe6cb7 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -1,3 +1,6 @@ +# all examples +add_custom_target(examples) + # add_example(target_name [SOURCES...]) function(add_example target_name) # if SOURCES is not specified @@ -10,9 +13,11 @@ function(add_example target_name) 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(rendering_particle) +add_example(button) +add_example(replay) +add_example(loadfont) +add_example(AITest) diff --git a/src/example/audio_internal.cpp b/src/example/audio_internal.cpp deleted file mode 100644 index 1199e2d..0000000 --- a/src/example/audio_internal.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/** \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/button.cpp b/src/example/button.cpp new file mode 100644 index 0000000..ea7f528 --- /dev/null +++ b/src/example/button.cpp @@ -0,0 +1,43 @@ +#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 54ce295..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/Collider.h> -#include <crepe/Component.h> -#include <crepe/ComponentManager.h> -#include <crepe/GameObject.h> -#include <crepe/Rigidbody.h> -#include <crepe/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); - 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"); - } - - 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/loadfont.cpp b/src/example/loadfont.cpp new file mode 100644 index 0000000..dd7caff --- /dev/null +++ b/src/example/loadfont.cpp @@ -0,0 +1,51 @@ +#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/rendering_particle.cpp b/src/example/rendering_particle.cpp new file mode 100644 index 0000000..e6b31a7 --- /dev/null +++ b/src/example/rendering_particle.cpp @@ -0,0 +1,74 @@ + + +#include "api/Asset.h" +#include "api/Text.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/Engine.h> +#include <crepe/api/GameObject.h> +#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> + +using namespace crepe; +using namespace std; + +class TestScene : public Scene { +public: + void load_scene() { + GameObject game_object = new_object("", "", vec2 {0, 0}, 0, 1); + + Color color(255, 255, 255, 255); + + Asset img {"asset/spritesheet/pokemon_spritesheet.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 = {1, 0}, + .angle_offset = 0, + .position_offset = {0, 1}, + .world_space = false, + } + ); + + auto & anim = game_object.add_component<Animator>( + test_sprite, ivec2 {56, 56}, uvec2 {4, 4}, + Animator::Data { + .looping = 0, + } + ); + + anim.set_anim(1); + anim.pause(); + anim.next_anim(); + + auto & cam = game_object.add_component<Camera>( + ivec2 {1280, 720}, vec2 {5, 5}, + Camera::Data { + .bg_color = Color::WHITE, + .postion_offset = {1000, 1000}, + } + ); + } + + string get_name() const { return "TestScene"; }; +}; + +int main(int argc, char * argv[]) { + Engine engine; + engine.add_scene<TestScene>(); + engine.main(); + return 0; +} 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(); +} diff --git a/src/example/script.cpp b/src/example/script.cpp deleted file mode 100644 index 28605c7..0000000 --- a/src/example/script.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/** \file - * - * Standalone example for usage of the script component and system - */ - -#include <crepe/util/log.h> -#include <crepe/ScriptSystem.h> -#include <crepe/ComponentManager.h> -#include <crepe/GameObject.h> - -#include <crepe/api/BehaviorScript.h> - -using namespace crepe; -using namespace std; - -class MyScript : public api::BehaviorScript { - void update() { - dbg_trace(); - } -}; - -int main() { - dbg_trace(); - - auto obj = GameObject(0, "name", "tag", 0); - obj.add_component<MyScript>(); - - auto & sys = ScriptSystem::get_instance(); - sys.update(); // -> MyScript::update - - return 0; -} - |