diff --git a/easy2d-imgui/libs/ImGui/imgui_demo.cpp b/easy2d-imgui/libs/ImGui/imgui_demo.cpp index 6fbde69a..839d9947 100644 --- a/easy2d-imgui/libs/ImGui/imgui_demo.cpp +++ b/easy2d-imgui/libs/ImGui/imgui_demo.cpp @@ -1150,7 +1150,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } - if (ImGui::TreeNode("Data Types")) + if (ImGui::TreeNode("DataUtil Types")) { // The DragScalar/InputScalar/SliderScalar functions allow various data types: signed/unsigned int/long long and float/double // To avoid polluting the public API with all possible combinations, we use the ImGuiDataType enum to pass the type, diff --git a/easy2d/2d/Animation.h b/easy2d/2d/Animation.h index 878f4e7d..c19360e0 100644 --- a/easy2d/2d/Animation.h +++ b/easy2d/2d/Animation.h @@ -32,7 +32,7 @@ namespace easy2d Animation( Duration duration, /* 动画时长 */ - FramesPtr const& frames, /* 帧集合 */ + FramesPtr const& frames, /* 序列帧 */ EaseFunc func = nullptr /* 速度变化 */ ); diff --git a/easy2d/2d/Frames.h b/easy2d/2d/Frames.h index 128fb2a8..36ddcf5a 100644 --- a/easy2d/2d/Frames.h +++ b/easy2d/2d/Frames.h @@ -23,7 +23,7 @@ namespace easy2d { - // 帧集合 + // 序列帧 class E2D_API Frames : public virtual Object { @@ -31,7 +31,7 @@ namespace easy2d Frames(); explicit Frames( - Array const& frames /* 关键帧数组 */ + Array const& frames /* 序列帧 */ ); virtual ~Frames(); diff --git a/easy2d/common/String.h b/easy2d/common/String.h index a48ae2f1..862ec966 100644 --- a/easy2d/common/String.h +++ b/easy2d/common/String.h @@ -1203,14 +1203,14 @@ namespace std template<> struct hash<::easy2d::String> { - size_t operator()(const easy2d::String& key) const + inline size_t operator()(const easy2d::String& key) const { return key.hash(); } }; template<> - void swap<::easy2d::String>(::easy2d::String& lhs, ::easy2d::String& rhs) + inline void swap<::easy2d::String>(::easy2d::String& lhs, ::easy2d::String& rhs) { lhs.swap(rhs); } diff --git a/easy2d/easy2d.h b/easy2d/easy2d.h index 9366388b..f1e9ebd2 100644 --- a/easy2d/easy2d.h +++ b/easy2d/easy2d.h @@ -106,7 +106,7 @@ // #include "utils/Path.h" -#include "utils/Data.h" +#include "utils/DataUtil.h" #include "utils/File.h" #include "utils/ResLoader.h" diff --git a/samples/Samples/Demo2.h b/samples/Samples/Demo2.h index 25471345..2ec7efb7 100644 --- a/samples/Samples/Demo2.h +++ b/samples/Samples/Demo2.h @@ -3,20 +3,21 @@ #pragma once #include "common.h" -E2D_DECLARE_SMART_PTR(Man); -class Man +// 怪物 +E2D_DECLARE_SMART_PTR(Monster); +class Monster : public Sprite { public: - Man() + Monster() { // 加载图片 - Load(L"res/man.png"); + Load(L"res/akushu.png"); // 缩小图片 - SetScale(0.5f); + SetScale(0.7f); } - // 每帧更新时 + // 每帧渲染前执行 OnUpdate void OnUpdate(Duration dt) override { // 获取输入设备 @@ -41,7 +42,7 @@ public: this->Move(0, 2); } - // 按下鼠标左键,顺时针旋转小人 + // 按下鼠标左键,顺时针旋转怪物 if (input.IsDown(MouseButton::Left)) { // 获取当前旋转角度 @@ -50,7 +51,7 @@ public: this->SetRotation(rotation + 2); } - // 点击鼠标右键,隐藏或显示小人 + // 点击鼠标右键,隐藏或显示怪物 if (input.WasPressed(MouseButton::Right)) { // 获取当前显示状态 @@ -72,11 +73,11 @@ public: Demo2() { - // 创建人物 - ManPtr man = new Man; + // 创建怪物 + MonsterPtr monster = new Monster; // 在屏幕上居中显示 - man->SetAnchor(0.5f, 0.5f); - man->SetPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2); + monster->SetAnchor(0.5f, 0.5f); + monster->SetPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2); // 创建说明文字 TextPtr text = new Text(L"按上下左右键移动\n按鼠标左键旋转\n点击鼠标右键隐藏"); @@ -88,7 +89,7 @@ public: text->SetAlignment(TextAlign::Center); // 添加到场景 - this->AddChild(man); + this->AddChild(monster); this->AddChild(text); } }; diff --git a/samples/Samples/Demo3.h b/samples/Samples/Demo3.h index 0b798212..bc41ee6f 100644 --- a/samples/Samples/Demo3.h +++ b/samples/Samples/Demo3.h @@ -20,7 +20,7 @@ public: { // 加载音乐 music = new Music; - if (!music->Load(L"res/music.wav")) + if (!music->Load(L"res/splash.mp3")) { music = nullptr; diff --git a/samples/Samples/Demo4.h b/samples/Samples/Demo4.h new file mode 100644 index 00000000..298e8945 --- /dev/null +++ b/samples/Samples/Demo4.h @@ -0,0 +1,192 @@ +// Copyright (C) 2019 Nomango + +#pragma once +#include "common.h" + +// 老虎 +E2D_DECLARE_SMART_PTR(Tiger); +class Tiger + : public Sprite +{ + FramesPtr run_frames; // 跑步序列帧 + FramesPtr stand_frames; // 站立序列帧 + bool facing_left; // 面朝左或面朝右 + bool running; // 是否正在跑步 + Direction running_direction; // 跑步方向 + +public: + Tiger() + { + // 获取图片原始大小 + ImagePtr image = new Image(L"res/tiger.png"); + Size source_size = image->GetSize(); + // 计算每帧图片大小 + Size frame_size = { source_size.x / 5, source_size.y / 3 }; + + // 加载帧动画 + run_frames = new Frames; + for (int i = 0; i < 6; ++i) + { + Point pos = { (i % 5) * frame_size.x, (i / 5) * frame_size.y }; + ImagePtr frame = new Image(L"res/tiger.png", Rect{ pos, frame_size }); + run_frames->Add(frame); + } + + stand_frames = new Frames; + for (int i = 0; i < 6; ++i) + { + Point pos = { ((i + 1) % 5) * frame_size.x, ((i + 1) / 5 + 1) * frame_size.y }; + ImagePtr frame = new Image(L"res/tiger.png", Rect{ pos, frame_size }); + stand_frames->Add(frame); + } + + // 执行动画 + AddAction( + Tween::Animation(stand_frames) + .SetDuration(1000) + .SetLoops(-1) + ); + + // 添加按键监听 + AddListener(Event::KeyDown, Closure(this, &Tiger::OnKeyDown)); + AddListener(Event::KeyUp, Closure(this, &Tiger::OnKeyUp)); + + // 默认方向为 Left + facing_left = true; + running = false; + + // 设置锚点 + SetAnchor(0.5f, 0.5f); + } + + void OnKeyDown(Event const& e) + { + if (e.key.code == KeyCode::Left) + Run(Direction::Left); + else if (e.key.code == KeyCode::Right) + Run(Direction::Right); + else if (e.key.code == KeyCode::Up) + Run(Direction::Up); + else if (e.key.code == KeyCode::Down) + Run(Direction::Down); + } + + void OnKeyUp(Event const& e) + { + switch (e.key.code) + { + case KeyCode::Left: + case KeyCode::Right: + case KeyCode::Up: + case KeyCode::Down: + StopRun(); + break; + } + } + + void Run(Direction d) + { + if (!running) + { + running = true; + StopAllActions(); + + // 执行跑步动画 + AddAction( + Tween::Animation(run_frames) + .SetDuration(500) + .SetLoops(-1) + ); + } + + running_direction = d; + if (running_direction == Direction::Left) + { + facing_left = true; + } + else if (running_direction == Direction::Right) + { + facing_left = false; + } + + // 缩放可以调整图片显示方向 + // 缩放至 -1 图片会反转 + SetScaleX(facing_left ? 1.0f : -1.0f); + } + + void StopRun() + { + if (running) + { + running = false; + StopAllActions(); + + // 执行站立动画 + AddAction( + Tween::Animation(stand_frames) + .SetDuration(1000) + .SetLoops(-1) + ); + } + } + + void OnUpdate(Duration dt) + { + if (running) + { + // 计算移动距离 + // OnUpdate 并不是一个稳定间隔执行的函数, 如果想实现稳定 + // 每秒移动 150 像素, 应根据 dt 参数计算移动距离 + const float moving_per_sec = 150; + const float distance = moving_per_sec * dt.Seconds(); + + switch (running_direction) + { + case Direction::Up: + Move(0, -distance); + break; + case Direction::Down: + Move(0, distance); + break; + case Direction::Left: + Move(-distance, 0); + break; + case Direction::Right: + Move(distance, 0); + break; + } + } + } +}; + +class Demo4 + : public Scene +{ +public: + static ScenePtr Create() + { + return new Demo4; + } + + Demo4() + { + // 创建老虎 + TigerPtr tiger = new Tiger; + // 在屏幕上居中显示 + tiger->SetAnchor(0.5f, 0.5f); + tiger->SetPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2); + + // 创建说明文字 + TextPtr text = new Text(L"按上下左右键移动"); + // 设置节点大小为文字布局大小 + text->SetSize(text->GetLayoutSize()); + // 设置文字位置 + text->SetAnchor(0.5f, 0.5f); + text->SetPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT - 80); + text->SetAlignment(TextAlign::Center); + + // 添加到场景 + this->AddChild(tiger); + this->AddChild(text); + } +}; diff --git a/samples/Samples/Samples.vcxproj b/samples/Samples/Samples.vcxproj index 36448e9f..bf42c497 100644 --- a/samples/Samples/Samples.vcxproj +++ b/samples/Samples/Samples.vcxproj @@ -140,6 +140,7 @@ + diff --git a/samples/Samples/Samples.vcxproj.filters b/samples/Samples/Samples.vcxproj.filters index 4683312d..ca8331ac 100644 --- a/samples/Samples/Samples.vcxproj.filters +++ b/samples/Samples/Samples.vcxproj.filters @@ -9,5 +9,6 @@ + \ No newline at end of file diff --git a/samples/Samples/include-forwards.h b/samples/Samples/include-forwards.h index 9c7bae91..725810f8 100644 --- a/samples/Samples/include-forwards.h +++ b/samples/Samples/include-forwards.h @@ -5,3 +5,4 @@ #include "Demo1.h" #include "Demo2.h" #include "Demo3.h" +#include "Demo4.h" diff --git a/samples/Samples/main.cpp b/samples/Samples/main.cpp index 1181bba6..7dbba836 100644 --- a/samples/Samples/main.cpp +++ b/samples/Samples/main.cpp @@ -14,6 +14,7 @@ namespace { L"动画示例", Demo1::Create }, { L"输入示例", Demo2::Create }, { L"音频播放示例", Demo3::Create }, + { L"帧动画示例", Demo4::Create }, }; int s_DemoIndex = 0; int s_DemoNum = sizeof(s_Demos) / sizeof(Demo); @@ -25,6 +26,7 @@ class DemoApp public: DemoApp() { + ShowConsole(); // 使用 Audio 组件 Use(&Audio::Instance()); @@ -39,7 +41,8 @@ public: void ChangeDemoScene() { - GetWindow()->SetTitle(s_Demos[s_DemoIndex].title); + String title = s_Demos[s_DemoIndex].title; + GetWindow()->SetTitle(title); ScenePtr scene = s_Demos[s_DemoIndex].Create(); EnterScene(scene); @@ -48,8 +51,10 @@ public: scene->AddListener(Event::KeyUp, Closure(this, &DemoApp::KeyPressed)); // 显示提示文字 - TextPtr intro = new Text(L"Key 1~3 to select demo"); + String intro_str = format_wstring(L"按键 1~%d 可切换示例\n", s_DemoNum); + TextPtr intro = new Text(intro_str + title); intro->SetFontSize(16.f); + intro->SetPosition(10, 10); scene->AddChild(intro); } diff --git a/samples/Samples/res/akushu.png b/samples/Samples/res/akushu.png new file mode 100644 index 00000000..916e43b7 Binary files /dev/null and b/samples/Samples/res/akushu.png differ diff --git a/samples/Samples/res/man.png b/samples/Samples/res/man.png deleted file mode 100644 index 4f89e869..00000000 Binary files a/samples/Samples/res/man.png and /dev/null differ diff --git a/samples/Samples/res/music.wav b/samples/Samples/res/music.wav deleted file mode 100644 index b54e0547..00000000 Binary files a/samples/Samples/res/music.wav and /dev/null differ diff --git a/samples/Samples/res/splash.mp3 b/samples/Samples/res/splash.mp3 new file mode 100644 index 00000000..1f084890 Binary files /dev/null and b/samples/Samples/res/splash.mp3 differ diff --git a/samples/Samples/res/tiger.png b/samples/Samples/res/tiger.png new file mode 100644 index 00000000..4ddffcde Binary files /dev/null and b/samples/Samples/res/tiger.png differ