Skip to content

Commit

Permalink
update 2024年09月15日 19:22:04
Browse files Browse the repository at this point in the history
  • Loading branch information
vanJker committed Sep 15, 2024
1 parent afbe286 commit b85e3e2
Show file tree
Hide file tree
Showing 16 changed files with 966 additions and 204 deletions.
273 changes: 241 additions & 32 deletions content/posts/cpp/modern-cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -2307,6 +2307,10 @@ ss >> a >> ch >> b >> ch >> c; // a = 23, b = 4, c = 56
> The algorithms library defines functions for a variety of purposes (e.g. searching, sorting, counting, manipulating) that operate on ranges of elements. Note that a range is defined as **[`first`, `last`)** where `last` refers to the element past the last element to inspect or modify.
### Nostd
Write our own data structures!
#### Sorting
- cppreference: [std::sort](https://en.cppreference.com/w/cpp/algorithm/sort)
Expand Down Expand Up @@ -2805,43 +2809,43 @@ class String
public:
...
String(String&& other) noexcept
{
std::cout << "Moved!" << std::endl;
m_Size = other.m_Size;
m_Data = other.m_Data;
other.m_Size = 0;
other.m_Data = nullptr;
}
{
std::cout << "Moved!" << std::endl;
m_Size = other.m_Size;
m_Data = other.m_Data;
other.m_Size = 0;
other.m_Data = nullptr;
}
String& operator=(String&& other) noexcept
{
std::cout << "Moved!" << std::endl;
if (this != &other)
{
delete[] m_Data;
m_Size = other.m_Size;
m_Data = other.m_Data;
{
std::cout << "Moved!" << std::endl;
if (this != &other)
{
delete[] m_Data;
m_Size = other.m_Size;
m_Data = other.m_Data;

other.m_Size = 0;
other.m_Data = nullptr;
}
return *this;
}
other.m_Size = 0;
other.m_Data = nullptr;
}
return *this;
}
...
};

int main()
{
{
String s1 = "hello";
String s2 = std::move(s1); // Move Constructor
s2.Print(); // "hello"
}
{
String s1 = "hello";
String s2 = "world";
s1 = std::move(s2); // Move Assignment
s1.Print(); // "world"
}
{
String s1 = "hello";
String s2 = std::move(s1); // Move Constructor
s2.Print(); // "hello"
}
{
String s1 = "hello";
String s2 = "world";
s1 = std::move(s2); // Move Assignment
s1.Print(); // "world"
}
}
```

Expand Down Expand Up @@ -3635,8 +3639,9 @@ GitHub: [Dear ImGui](https://github.com/ocornut/imgui/tree/master)
> Dear ImGui is a **bloat-free graphical user interface library for C++**. It outputs optimized vertex buffers that you can render anytime in your 3D-pipeline-enabled application. It is fast, portable, renderer agnostic, and self-contained (no external dependencies).
bilibili:
- [ImGui 入门到精通](https://space.bilibili.com/443124242/channel/collectiondetail?sid=824431)
- 入门: [ImGui 入门到精通](https://space.bilibili.com/443124242/channel/collectiondetail?sid=824431)
/ [项目源代码](https://www.bilibili.com/read/cv19537138/)
- 进阶: [可能是目前全網最強最好的 ImGui 教學](https://space.bilibili.com/443124242/channel/collectiondetail?sid=2917715) / [项目源代码](https://drive.google.com/file/d/1XRqDM3d_ByF5RafRG0wK1JoI6RSitAsw)

#### 初始设置

Expand All @@ -3649,7 +3654,7 @@ bilibili:
- C/C++ -> Additional Include Directoris
- `$(SolutionDir)\Dependencies\GLFW\include`
- `$(SolutionDir)\Dependencies\GLEW\include`
- `$(ProjectDir)\imgui` 或者新建一个 Project 并将其作为静态库
- `$(ProjectDir)\imgui` (这种处理可以新建筛选器来保持项目组织) 或者新建一个 Project 并将其作为静态库
- Linker -> Additional Library Directories
- `glfw3.lib`
- `glew32s.lib`
Expand All @@ -3667,6 +3672,8 @@ bilibili:

#### 创建窗口

这是基础设施,直接复制一份即可。窗口逻辑在 `ImGui::ShowDemoWindow()` 处:

```c++
GLFWwindow* Windows;

Expand Down Expand Up @@ -3802,6 +3809,208 @@ ImGui::ColorEdit4("Color", (float*)&color, ImGuiColorEditFlags_::ImGuiColorEditF
#### 高级定制
##### 字体设置
```c++
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.Fonts->AddFontFromFileTTF(
"C:\\Windows\\Fonts\\STZHONGS.TTF", // 华文中宋
18, // 字体大小
NULL, // 英文字体
io.Fonts->GetGlyphRangesChineseFull() // 作用范围
);
```

##### 风格设置

在启动 ImGui Demo 后,其窗口的 `tools` 栏位的 `Style Editor` 选项即为风格编辑器。主要改一下圆角之类的设定,进行拖动可以即时看到效果,后续可以根据效果对应的数值在源代码中进行设置:

```c++
ImGuiStyle& style = ImGui::GetStyle();
style.WindowRounding = 6;
style.FrameRounding = 6;
style.GrabRounding = 6;
style.ScrollbarSize = 10;
style.ScrollbarRounding = 5;
```

在风格编辑器理也可切换到颜色 (Colors) 栏目对颜色进行调整并且即时看到效果,以进行相应的设置:

```c++
ImVec4* colors = style.Colors;
colors[ImGuiCol_FrameBg] = ImVec4(0.16f, 0.16f, 0.17f, 1.00f);
colors[ImGuiCol_FrameBgHovered] = ImVec4(0.37f, 0.36f, 0.36f, 102.00f);
colors[ImGuiCol_FrameBgActive] = ImVec4(0.10f, 0.10f, 0.10f, 171.00f);
colors[ImGuiCol_TitleBgActive] = ImVec4(0.20f, 0.20f, 0.20f, 255.00f);
colors[ImGuiCol_CheckMark] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f);
colors[ImGuiCol_SliderGrab] = ImVec4(0.64f, 0.64f, 0.64f, 1.00f);
colors[ImGuiCol_SliderGrabActive] = ImVec4(0.31f, 0.31f, 0.31f, 1.00f);
colors[ImGuiCol_Button] = ImVec4(0.22f, 0.22f, 0.22f, 0.40f);
colors[ImGuiCol_ButtonHovered] = ImVec4(0.29f, 0.29f, 0.29f, 1.00f);
colors[ImGuiCol_ButtonActive] = ImVec4(0.13f, 0.13f, 0.13f, 1.00f);
colors[ImGuiCol_Header] = ImVec4(0.45f, 0.45f, 0.45f, 0.31f);
colors[ImGuiCol_HeaderHovered] = ImVec4(0.55f, 0.55f, 0.55f, 0.80f);
colors[ImGuiCol_HeaderActive] = ImVec4(0.09f, 0.09f, 0.09f, 1.00f);
colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.20f);
colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.46f, 0.46f, 0.46f, 0.67f);
colors[ImGuiCol_ResizeGripActive] = ImVec4(0.17f, 0.17f, 0.17f, 0.95f);
colors[ImGuiCol_SeparatorActive] = ImVec4(0.42f, 0.42f, 0.42f, 1.00f);
colors[ImGuiCol_SeparatorHovered] = ImVec4(0.50f, 0.50f, 0.50f, 0.78f);
colors[ImGuiCol_TabHovered] = ImVec4(0.45f, 0.45f, 0.45f, 0.80f);
colors[ImGuiCol_TabActive] = ImVec4(0.28f, 0.28f, 0.28f, 1.00f);
colors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.19f, 0.19f, 0.19f, 1.00f);
colors[ImGuiCol_DockingPreview] = ImVec4(0.51f, 0.51f, 0.51f, 0.70f);
colors[ImGuiCol_Tab] = ImVec4(0.21f, 0.21f, 0.21f, 0.86f);
colors[ImGuiCol_TabUnfocused] = ImVec4(0.15f, 0.15f, 0.15f, 0.97f);
colors[ImGuiCol_NavHighlight] = ImVec4(1.00f, 0.40f, 0.13f, 1.00f);
colors[ImGuiCol_TextSelectedBg] = ImVec4(0.45f, 1.00f, 0.85f, 0.35f);
```

##### 窗口停靠

设置内部窗口可以拖拽停靠,例如可以拖拽到外部空间和拖拽到其它窗口内部停靠:

```c++
// 允许窗口停靠
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.ConfigFlags |= ImGuiViewportFlags_NoDecoration;
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
io.ConfigFlags |= ImGuiCol_DockingEmptyBg;

while (!glfwWindowShouldClose(Windows))
... // main logic
// 窗口移动出主窗口时创建子窗口
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
GLFWwindow* backup_current_context = glfwGetCurrentContext();
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
glfwMakeContextCurrent(backup_current_context);
}
}
```

允许主窗口也可以作为停靠的主体:

```c++
while (!glfwWindowShouldClose(Windows))
{
ImGui::DockSpaceOverViewport();
... // main logic
}
```

##### 高级控件

绘制队列 (Draw List) 可以绘制高级的自定义控件,常见的有矩形,圆等等:

```c++
ImDrawList* DrawList = ImGui::GetForegroundDrawList(); // 获取绘制队列
DrawList->AddRectFilled( // 绘制填充矩形
ImVec2(0, 0), // 窗口左上角
ImGui::GetMousePos(), // 鼠标位置
ImColor(60, 255, 255) // 颜色 RGB
);
```

##### 事件监听

可以监听键盘按键的动作,同时还可以搭配鼠标位置来限制逻辑的执行范围:

```c++
// 当键盘的按键 Q 被按下,以及鼠标位于窗口的标题处,才在窗口内部生成文本框
if (ImGui::IsKeyDown(static_cast<ImGuiKey>('Q')) && ImGui::IsItemHovered())
{
ImGui::Text(u8"测试 Q");
}
```

##### 控件拖拽

与窗口拖拽类似,窗口内的控件也是可以设置为可拖拽的:

```c++
#include <string>
#include <vector>

std::vector <int> DragList;

void DrawGui()
{
// 拖拽源地址窗口
ImGui::Begin("Drag Source");
ImGui::Text("Drag Source");
for (size_t i = 0; i < 5; i++)
{
ImGui::Button(std::to_string(i).c_str());
if (i + 1 < 5) ImGui::SameLine(); // 位于两个控件之间,用于设置控件横向排列,否则控件默认为纵向排列

if (ImGui::BeginDragDropSource())
{
// 拖拽时的表现内容
ImGui::Text(std::string("Drag : ").append(std::to_string(i)).c_str());
// 设置拖拽控件的负载数据
ImGui::SetDragDropPayload("DragIndexButton", &i, sizeof(int));

ImGui::EndDragDropSource();
}
}
ImGui::End();

// 拖拽目的地窗口
ImGui::Begin("Drag Window");
// 设置下面的文本框为控件拖拽的目的地,即该文本框可以接收被拖拽过来的控件
ImGui::Text("Drag Target");
if (ImGui::BeginDragDropTarget())
{
// 接受拖拽过来的控件,并取出其负载的数据
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("DragIndexButton"))
{
DragList.push_back(*(const int*)payload->Data);
}
ImGui::EndDragDropTarget();
}
for (size_t i = 0; i < DragList.size(); i++)
{
// 被拖拽过来安放好的按钮控件,如果被点击了就会消失
if (ImGui::Button(std::to_string(DragList.at(i)).c_str()))
{
DragList.erase(DragList.begin() + i);
}
if (i + 1 < DragList.size()) ImGui::SameLine();
}
ImGui::End();
}
```

#### 第三方库

imgui 的第三方库可以在其 wiki 的 [Useful Extensions](https://github.com/ocornut/imgui/wiki/Useful-Extensions) 页面中进行查找。只需下载第三方库的源代码,然后仿照对 imgui 库的处理将其源代码进行引入 `$(Project)\<lib>`,或在 Visual Studio 中作为静态库进行链接即可。

下面以第三方库 [ImGuiColorTextEdit](https://github.com/BalazsJako/ImGuiColorTextEdit) 为例:

```c++
#include "TextEditor.h"

int main()
{
...
// define and setup editor
TextEditor* te = new TextEditor();
te->SetLanguageDefinition(TextEditor::LanguageDefinition::C());

while (!glfwWindowShouldClose(Windows))
{
...
// main logic
ImGui::Begin("Text Editor");
te->Render("TextED");
ImGui::End();
...
}
}
```

## References

- The Cherno: [C++](https://www.youtube.com/playlist?list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb) / [中文翻译](https://space.bilibili.com/364152971/channel/collectiondetail?sid=13909): 主要介绍 C++11 及以上版本的语法 (文中未特意标注引用的部分,均出自该处)
Expand Down
1 change: 0 additions & 1 deletion docs/css/0caa95.min.css

This file was deleted.

1 change: 1 addition & 0 deletions docs/css/6dcd57.min.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#id-25{background-color:green}#id-26{background-color:green}
3 changes: 1 addition & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@
<a href="/posts/drop-check/">阅读全文</a><div class="post-tags"><i class="fa-solid fa-tags fa-fw me-1" aria-hidden="true"></i><a href='/tags/rust/' class="post-tag">Rust</a><a href='/tags/drop/' class="post-tag">Drop</a></div></div>
</article>
<article class="single summary" itemscope itemtype="http://schema.org/Article"><h2 class="single-title" itemprop="name headline"><span title="转载" class="icon-repost"><i class="fa-solid fa-share fa-fw" aria-hidden="true"></i></span><a href="/posts/modern-cpp/">Modern C&#43;&#43; (MSVC)</a>
</h2><div class="post-meta"><span class="post-author"><span class="author"><i class="fa-solid fa-user-circle" aria-hidden="true"></i>
</span></span>&nbsp;<span class="post-publish" title='2024-06-30 00:19:25'>发布于 <time datetime="2024-06-30">2024-06-30</time></span><span class="post-included-in">&nbsp;收录于 <a href="/categories/c++/" class="post-category" title="分类 - C++"><i class="fa-regular fa-folder fa-fw" aria-hidden="true"></i> C++</a></span></div><div class="content"><p>&ldquo;Modern&rdquo; <a href="https://en.wikipedia.org/wiki/C%2B%2B"target="_blank" rel="external nofollow noopener noreferrer">C++</a> isn&rsquo;t afraid to use any or all of the following:</p>
</h2><div class="post-meta"><span class="post-author"><a href="https://github.com/vanJker" title="作者"target="_blank" rel="external nofollow noopener noreferrer author" class="author"><img loading="lazy" src="https://avatars.githubusercontent.com/u/88960102?s=96&amp;v=4" alt="vanJker" data-title="vanJker" class="avatar" style="background: url(/images/loading.min.svg) no-repeat center;" onload="this.title=this.dataset.title;for(const i of ['style', 'data-title','onerror','onload']){this.removeAttribute(i);}this.dataset.lazyloaded='';" onerror="this.title=this.dataset.title;for(const i of ['style', 'data-title','onerror','onload']){this.removeAttribute(i);}"/>&nbsp;vanJker</a></span>&nbsp;<span class="post-publish" title='2024-06-30 00:19:25'>发布于 <time datetime="2024-06-30">2024-06-30</time></span><span class="post-included-in">&nbsp;收录于 <a href="/categories/c++/" class="post-category" title="分类 - C++"><i class="fa-regular fa-folder fa-fw" aria-hidden="true"></i> C++</a></span></div><div class="content"><p>&ldquo;Modern&rdquo; <a href="https://en.wikipedia.org/wiki/C%2B%2B"target="_blank" rel="external nofollow noopener noreferrer">C++</a> isn&rsquo;t afraid to use any or all of the following:</p>
<ul>
<li>RAII</li>
<li>standard library containers and algorithms</li>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions docs/page/3/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@
<a href="/posts/c-compiler-optimization/">阅读全文</a><div class="post-tags"><i class="fa-solid fa-tags fa-fw me-1" aria-hidden="true"></i><a href='/tags/sysprog/' class="post-tag">Sysprog</a><a href='/tags/c/' class="post-tag">C</a><a href='/tags/compiler/' class="post-tag">Compiler</a><a href='/tags/optimization/' class="post-tag">Optimization</a></div></div>
</article>
<article class="single summary" itemscope itemtype="http://schema.org/Article"><h2 class="single-title" itemprop="name headline"><span title="转载" class="icon-repost"><i class="fa-solid fa-share fa-fw" aria-hidden="true"></i></span><a href="/posts/c-undefined-behavior/">你所不知道的 C 语言: 未定义/未指定行为篇</a>
</h2><div class="post-meta"><span class="post-author"><span class="author"><i class="fa-solid fa-user-circle" aria-hidden="true"></i>
</span></span>&nbsp;<span class="post-publish" title='2024-04-24 21:09:40'>发布于 <time datetime="2024-04-24">2024-04-24</time></span><span class="post-included-in">&nbsp;收录于 <a href="/categories/c/" class="post-category" title="分类 - C"><i class="fa-regular fa-folder fa-fw" aria-hidden="true"></i> C</a>&ensp;<a href="/categories/c++/" class="post-category" title="分类 - C++"><i class="fa-regular fa-folder fa-fw" aria-hidden="true"></i> C++</a>&ensp;<a href="/categories/linux-kernel-internals/" class="post-category" title="分类 - Linux Kernel Internals"><i class="fa-regular fa-folder fa-fw" aria-hidden="true"></i> Linux Kernel Internals</a></span></div><div class="content"><blockquote>
</h2><div class="post-meta"><span class="post-author"><a href="https://github.com/ccrysisa" title="作者"target="_blank" rel="external nofollow noopener noreferrer author" class="author"><img loading="lazy" src="https://avatars.githubusercontent.com/u/133117003?s=400&amp;v=4" alt="ccrysisa" data-title="ccrysisa" class="avatar" style="background: url(/images/loading.min.svg) no-repeat center;" onload="this.title=this.dataset.title;for(const i of ['style', 'data-title','onerror','onload']){this.removeAttribute(i);}this.dataset.lazyloaded='';" onerror="this.title=this.dataset.title;for(const i of ['style', 'data-title','onerror','onload']){this.removeAttribute(i);}"/>&nbsp;ccrysisa</a></span>&nbsp;<span class="post-publish" title='2024-04-24 21:09:40'>发布于 <time datetime="2024-04-24">2024-04-24</time></span><span class="post-included-in">&nbsp;收录于 <a href="/categories/c/" class="post-category" title="分类 - C"><i class="fa-regular fa-folder fa-fw" aria-hidden="true"></i> C</a>&ensp;<a href="/categories/c++/" class="post-category" title="分类 - C++"><i class="fa-regular fa-folder fa-fw" aria-hidden="true"></i> C++</a>&ensp;<a href="/categories/linux-kernel-internals/" class="post-category" title="分类 - Linux Kernel Internals"><i class="fa-regular fa-folder fa-fw" aria-hidden="true"></i> Linux Kernel Internals</a></span></div><div class="content"><blockquote>
<p>C 語言最初為了開發 UNIX 和系統軟體而生,本質是低階的程式語言,在語言規範層級存在 undefined behavior,可允許編譯器引入更多最佳化</p>
</blockquote></div><div class="post-footer">
<a href="/posts/c-undefined-behavior/">阅读全文</a><div class="post-tags"><i class="fa-solid fa-tags fa-fw me-1" aria-hidden="true"></i><a href='/tags/sysprog/' class="post-tag">Sysprog</a><a href='/tags/c/' class="post-tag">C</a></div></div>
Expand Down
Loading

0 comments on commit b85e3e2

Please sign in to comment.