From 5848ce7da2bb82b717c4a0703e7acbc82d6924b8 Mon Sep 17 00:00:00 2001 From: Alan-CRL <92425617+Alan-CRL@users.noreply.github.com> Date: Wed, 31 Jan 2024 09:55:46 +0800 Subject: [PATCH] Update Balls.cpp --- Samples/Recommend/Balls.cpp | 69 +++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/Samples/Recommend/Balls.cpp b/Samples/Recommend/Balls.cpp index fe52912..8f4c436 100644 --- a/Samples/Recommend/Balls.cpp +++ b/Samples/Recommend/Balls.cpp @@ -1,20 +1,20 @@ /** - * @brief 透明小球边界碰撞示例 - * @note 此示例用于演示 HiCanvas 的透明通道、场景和图层 - * @author huidong - * @version HiEasyX Ver0.3.2 - * @date 2023.01.12 + * @brief 閫忔槑灏忕悆杈圭晫纰版挒绀轰緥 + * @note 姝ょず渚嬬敤浜庢紨绀 HiCanvas 鐨勯忔槑閫氶亾銆佸満鏅拰鍥惧眰 + * @author huidong alan-crl + * @version HiEasyX Ver0.4.1 + * @date 2024.01.31 */ #include "HiEasyX.h" -// 小球数量 +// 灏忕悆鏁伴噺 #define BALL_NUM 14 -// 小球半径 +// 灏忕悆鍗婂緞 #define RADIUS 70 -// 球体 +// 鐞冧綋 struct Ball { hiex::ImageBlock img; @@ -22,39 +22,39 @@ struct Ball float vx, vy; }; -hiex::SysButton btn; // 按钮 -bool show_outline = false; // 是否显示轮廓 +hiex::SysButton btn; // 鎸夐挳 +bool show_outline = false; // 鏄惁鏄剧ず杞粨 int main() { - hiex::Window wnd(640, 480); // 创建窗口 - hiex::Canvas canvas; // 创建画布对象 - wnd.BindCanvas(&canvas); // 将窗口和画布绑定 + hiex::Window wnd(640, 480); // 鍒涘缓绐楀彛 + hiex::Canvas canvas; // 鍒涘缓鐢诲竷瀵硅薄 + wnd.BindCanvas(&canvas); // 灏嗙獥鍙e拰鐢诲竷缁戝畾 - // 手动刷新双缓冲 + // 鎵嬪姩鍒锋柊鍙岀紦鍐 //hiex::EnableAutoFlush(false); - canvas.Clear(true, BLACK); // 设置背景色为黑色,清空画布 + canvas.Clear(true, BLACK); // 璁剧疆鑳屾櫙鑹蹭负榛戣壊锛屾竻绌虹敾甯 - // 创建按钮,用于点击设置是否显示轮廓 + // 鍒涘缓鎸夐挳锛岀敤浜庣偣鍑昏缃槸鍚︽樉绀鸿疆寤 btn.Create(wnd.GetHandle(), 50, 50, 120, 30, L"Hide outline"); btn.RegisterMessage([]() { show_outline = !show_outline; - btn.SetText(show_outline ? L"Show outline" : L"Hide outline"); + btn.SetText(show_outline ? L"Show outline" : L"Hide outline"); }); - // 初始化随机数 + // 鍒濆鍖栭殢鏈烘暟 srand((UINT)time(nullptr)); - hiex::Scene scene; // 场景 - hiex::Layer layer; // 图层 + hiex::Scene scene; // 鍦烘櫙 + hiex::Layer layer; // 鍥惧眰 - Ball balls[BALL_NUM]; // 小球 + Ball balls[BALL_NUM]; // 灏忕悆 - // 初始化小球 + // 鍒濆鍖栧皬鐞 for (auto& i : balls) { - // 位置和速度的初始化 + // 浣嶇疆鍜岄熷害鐨勫垵濮嬪寲 i.x = (float)(rand() % canvas.GetWidth()); i.y = (float)(rand() % canvas.GetHeight()); i.vx = rand() % 5 * (rand() % 2 ? 2.0f : -2.0f); @@ -64,24 +64,24 @@ int main() if (i.vy == 0) i.vy = 3; - // 小球的图像块设置 + // 灏忕悆鐨勫浘鍍忓潡璁剧疆 i.img.CreateCanvas(RADIUS * 2, RADIUS * 2); i.img.GetCanvas()->Clear(); i.img.GetCanvas()->SolidCircle(RADIUS, RADIUS, RADIUS, true, rand() % 0xffffff); ReverseAlpha(i.img.GetCanvas()->GetBuffer(), i.img.GetCanvas()->GetBufferSize()); - // 设置图像块透明度 + // 璁剧疆鍥惧儚鍧楅忔槑搴 i.img.alpha = 200; i.img.bUseSrcAlpha = true; - // 加入图像块到图层 + // 鍔犲叆鍥惧儚鍧楀埌鍥惧眰 layer.push_back(&i.img); } - // 加入图层到场景 + // 鍔犲叆鍥惧眰鍒板満鏅 scene.push_back(&layer); - // 主循环(窗口关闭或按下按键时退出) + // 涓诲惊鐜紙绐楀彛鍏抽棴鎴栨寜涓嬫寜閿椂閫鍑猴級 while (wnd.IsAlive() && !peekmessage(nullptr, EM_CHAR)) { for (auto& i : balls) @@ -89,7 +89,7 @@ int main() i.x += i.vx; i.y += i.vy; - // 碰撞判定 + // 纰版挒鍒ゅ畾 if (i.x - RADIUS < 0) { i.x = RADIUS; @@ -111,24 +111,25 @@ int main() i.vy = -i.vy; } - // 更新位置 + // 鏇存柊浣嶇疆 i.img.SetPos((int)i.x - RADIUS, (int)i.y - RADIUS); } if (wnd.BeginTask()) { - // 渲染场景 + // 娓叉煋鍦烘櫙 scene.Render(canvas.GetImagePointer(), show_outline); - // 手动刷新双缓冲 + // 鎵嬪姩鍒锋柊鍙岀紦鍐 //wnd.FlushDrawing(); wnd.EndTask(); wnd.Redraw(); } - // 平衡帧率 - hiex::DelayFPS(24); + // 骞宠 甯х巼 + static hiex::tDelayFPS recond; + hiex::DelayFPS(recond, 24); } return 0;