Skip to content

Commit 7469b94

Browse files
committed
add result
1 parent f621092 commit 7469b94

File tree

9 files changed

+1814
-13
lines changed

9 files changed

+1814
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
build
2+
GNUmakefile

CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
cmake_minimum_required(VERSION 3.12)
22
project(hellocmake LANGUAGES CXX)
33

4-
add_subdirectory(stbiw)
4+
if (NOT CMAKE_BUILD_TYPE)
5+
set(CMAKE_BUILD_TYPE Release)
6+
endif()
57

6-
add_executable(main main.cpp)
7-
target_link_libraries(main PUBLIC stbiw)
8+
#add_subdirectory(stbiw)
9+
10+
add_executable(main main.cpp rainbow.cpp mandel.cpp)
11+
#target_link_libraries(main PUBLIC stbiw)

README.md

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,43 @@
22

33
第01讲的回家作业
44

5-
# 要求
5+
通过 pull request 提交作业。会批分数,但是:
66

7-
在 main.cpp 中为了导出一个美好的图像,使用了 `stb_image_write.h` 这个头文件。
7+
没有结业证书,回家作业仅仅作为评估学习效果和巩固知识的手段,不必为分数感到紧张 :)
8+
量力而行,只要能在本课中,学到昨天的自己不懂的知识,就是胜利,没必要和别人攀比。
89

9-
在 CMakeLists.txt 中也引用了 stbiw 这个库,然而这个库还没有被定义。
10-
你的任务就是定义 stbiw 这个库,他的内容应该包含 stbi_write_png 的实现。
10+
- 课件:https://github.com/parallel101/course
11+
- 录播:https://space.bilibili.com/263032155
1112

12-
最好以子模块 + 库的形式,实在搞不定的话同一个目录下 include 也可以。但是分数会低
13+
作业提交时间不限 :) 即使完结了还想交的话我也会看的~ 不过最好在下一讲开播前完成
1314

14-
# 参考
15+
## 作业要求
1516

16-
stb_image_write.h 下载地址: https://github.com/nothings/stb/blob/master/stb_image_write.h
17+
在 main.cpp 中为了导出两个"美好的图像",使用了 `stb_image_write.h` 这个头文件。
18+
他在 CMakeLists.txt 中也引用了 stbiw 这个库,然而这个库还没有被定义。
1719

18-
你需要一个 .cpp 文件定义了 STB_IMAGE_WRITE_IMPLEMENTATION 这个宏,才能决定让 stbi 系列函数在这里实现。
20+
你的任务就是 **定义 stbiw 这个库**,他的内容应该包含 `stbi_write_png()` 的实现,
21+
以及允许通过尖括号导入头文件 `<stb_image_write.h>`
22+
23+
运用上课所学知识,尽量不修改 main.cpp 的内容,只修改 stbiw 子目录下的内容,
24+
完成任务。最好以子模块 + 库的形式,实在不行的话直接改 main.cpp 也可以。
25+
26+
运行成功后,应该会在主程序同目录发现两个"美好的图像":mandel.png 和 rainbow.png
27+
28+
## 参考信息
29+
30+
stb_image_write.h 原仓库地址: https://github.com/nothings/stb
31+
32+
你需要在一个且仅一个 .cpp 文件定义了 `STB_IMAGE_WRITE_IMPLEMENTATION` 这个宏,
33+
才能决定让 stbi 系列函数在这里实现。
34+
35+
如果你能不仅完成了作业,解释清楚为什么 stbi 必须要这样设计,可能会给你满分!
36+
37+
## 采分点提示
1938

2039
像这样:
2140
```cmake
2241
target_compile_definitions(stbiw PUBLIC -DSTB_IMAGE_WRITE_IMPLEMENTATION)
2342
```
24-
是不行的,因为如果两个 .cpp 文件都 include 了 stb_image_write.h,那么同一个函数会被定义两遍!
43+
是不行的,因为 mandel.cpp 和 rainbow.cpp 两个文件都 include 了 stb_image_write.h,
44+
这样同一个函数会被定义两遍!

main.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
#include "stb_image_write.h"
1+
#include "rainbow.h"
2+
#include "mandel.h"
3+
4+
int main() {
5+
test_rainbow();
6+
test_mandel();
7+
return 0;
8+
}

mandel.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "mandel.h"
2+
#include <stb_image_write.h>
3+
#include <vector>
4+
#include <complex>
5+
6+
void test_mandel() {
7+
std::vector<char> buf(512 * 512);
8+
for (int j = 0; j < 512; j++) {
9+
for (int i = 0; i < 512; i++) {
10+
float x = i / 512.f * 3.0f - 2.0f;
11+
float y = j / 512.f * 3.0f - 1.5f;
12+
std::complex<float> c(x, y);
13+
std::complex<float> z(0, 0);
14+
for (int steps = 0; steps < 256 / 4; steps++) {
15+
z = z * z + c;
16+
if (std::norm(z) >= 4.f) {
17+
buf[j * 512 + i] = 255 - steps * 4;
18+
break;
19+
}
20+
}
21+
}
22+
}
23+
stbi_write_png("mandel.png", 512, 512, 1, buf.data(), 0);
24+
}

mandel.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
void test_mandel();

rainbow.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "rainbow.h"
2+
#include <stb_image_write.h>
3+
#include <vector>
4+
5+
void test_rainbow() {
6+
std::vector<char> buf(512 * 512 * 3);
7+
for (int j = 0; j < 512; j++) {
8+
for (int i = 0; i < 512; i++) {
9+
buf[(j * 512 + i) * 3 + 0] = i / 2;
10+
buf[(j * 512 + i) * 3 + 1] = j / 2;
11+
buf[(j * 512 + i) * 3 + 2] = 0;
12+
}
13+
}
14+
stbi_write_png("rainbow.png", 512, 512, 3, buf.data(), 0);
15+
}

rainbow.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
void test_rainbow();

0 commit comments

Comments
 (0)