Skip to content

Commit

Permalink
更新plui
Browse files Browse the repository at this point in the history
  • Loading branch information
copi143 committed Jan 31, 2025
1 parent 4513632 commit 5d7d2c1
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 71 deletions.
4 changes: 4 additions & 0 deletions include/define/type/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ typedef __UINT8_TYPE__ byte;
typedef int errno_t;

typedef __INT64_TYPE__ time_t;

#ifdef __cplusplus
# define lit constexpr
#endif
44 changes: 22 additions & 22 deletions include/pl2d/fb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,27 @@ constexpr PixFmt texture_pixfmt = PixFmt::RGBA;
#endif

struct FrameBuffer {
union { //
void *pix[4] = {}; //
u8 *pix8[4]; //
u16 *pix16[4]; //
u32 *pix32[4]; //
struct { //
byte *plane1; //
byte *plane2; //
byte *plane3; //
byte *plane4; //
}; //
}; // 缓冲区
union { //
u32 plane_size[4] = {}; //
struct { //
u32 plane1_size; //
u32 plane2_size; //
u32 plane3_size; //
u32 plane4_size; //
}; //
}; // 缓冲区大小
union { // 缓冲区
void *pix[4] = {}; //
u8 *pix8[4]; //
u16 *pix16[4]; //
u32 *pix32[4]; //
struct { //
byte *plane1; //
byte *plane2; //
byte *plane3; //
byte *plane4; //
};
};
union { // 缓冲区大小
u32 plane_size[4] = {}; //
struct { //
u32 plane1_size; //
u32 plane2_size; //
u32 plane3_size; //
u32 plane4_size; //
};
};
u32 *pal = null; // 调色板
u32 width = 0; // 宽度(可自动计算) width = pitch / size_of_pixel
u32 height = 0; // 高度
Expand All @@ -114,7 +114,7 @@ struct FrameBuffer {
FrameBuffer(const FrameBuffer &) = delete;
FrameBuffer(FrameBuffer &&) noexcept = default;

auto operator=(const FrameBuffer &) -> FrameBuffer & = delete;
auto operator=(const FrameBuffer &) -> FrameBuffer & = delete;
auto operator=(FrameBuffer &&) noexcept -> FrameBuffer & = default;

void reset() {
Expand Down
15 changes: 6 additions & 9 deletions include/pl2d/pixel/pixel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ struct BasePixel {
}

// 计算颜色的差值
CONST auto diff(const BasePixel &p) -> T;
CONST auto diff(const BasePixel &p) -> T {
T dr = cpp::diff(r, p.r);
T dg = cpp::diff(g, p.g);
T db = cpp::diff(b, p.b);
return cpp::max(dr, dg, db);
}

// 按比例进行颜色混合函数
HOT void mix_ratio(const BasePixel &s, FT k);
Expand Down Expand Up @@ -223,12 +228,4 @@ struct BasePixel {
}
};

template <BasePixelTemplate>
CONST auto BasePixelT::diff(const BasePixel &p) -> T {
T dr = cpp::diff(r, p.r);
T dg = cpp::diff(g, p.g);
T db = cpp::diff(b, p.b);
return cpp::max(dr, dg, db);
}

} // namespace pl2d
2 changes: 2 additions & 0 deletions include/pl2d/texture/texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ struct BaseTexture {
u32 size = 0; // 储存 height * pitch 而不是占用的字节数
u32 alloced_size = 0; // 储存实际分配的大小 像素数而不是字节数

free_t free_pixels = null; // 释放用的函数

BaseTexture() = default;
BaseTexture(u32 width, u32 height); // 创建一个空的纹理
BaseTexture(u32 width, u32 height, u32 pitch); // 创建一个空的纹理
Expand Down
12 changes: 3 additions & 9 deletions include/plui/element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace plui {

struct Style {

u32 padding;

u32 border_width; // 边框宽度
pl2d::Pixel border_color; // 边框颜色

Expand All @@ -31,15 +33,7 @@ struct Style {
using ChildList = cpp::List<Element *>;

struct Element : Style {
union {
struct {
i32 x; // 元素左上角横坐标
i32 y; // 元素左上角纵坐标
i32 width; // 元素宽度
i32 height; // 元素高度
};
Position pos;
};
Position pos; // 位置
pl2d::Rect internal; // 元素内部的区域
Element *parent; // 父元素,表示当前元素的上一级元素
bool needdraw; // 是否需要刷新,表示是否需要重新绘制元素
Expand Down
22 changes: 13 additions & 9 deletions include/plui/pos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,44 @@ struct Position {
Position(const Position &) = default;
Position(Position &&) noexcept = default;

auto operator=(const Position &) -> Position & = default;
auto operator=(const Position &) -> Position & = default;
auto operator=(Position &&) noexcept -> Position & = default;

auto move(i32 dx, i32 dy) noexcept -> Position & {
INLINE_CONST auto move(i32 dx, i32 dy) noexcept -> Position & {
x += dx, y += dy;
return *this;
}

auto moveto(i32 x, i32 y) noexcept -> Position & {
INLINE_CONST auto moveto(i32 x, i32 y) noexcept -> Position & {
this->x = x, this->y = y;
return *this;
}

[[nodiscard]] auto topleft() const noexcept -> pl2d::Point2I {
INLINE_CONST auto topleft() const noexcept -> pl2d::Point2I {
return {x, y};
}
[[nodiscard]] auto topright() const noexcept -> pl2d::Point2I {
INLINE_CONST auto topright() const noexcept -> pl2d::Point2I {
return {x + w - 1, y};
}
[[nodiscard]] auto bottomleft() const noexcept -> pl2d::Point2I {
INLINE_CONST auto bottomleft() const noexcept -> pl2d::Point2I {
return {x, y + h - 1};
}
[[nodiscard]] auto bottomright() const noexcept -> pl2d::Point2I {
INLINE_CONST auto bottomright() const noexcept -> pl2d::Point2I {
return {x + w - 1, y + h - 1};
}

[[nodiscard]] auto contains(i32 _x, i32 _y) const noexcept -> bool {
INLINE_CONST auto contains(i32 _x, i32 _y) const noexcept -> bool {
if (_x < x || _x >= x + w) return false;
if (_y < y || _y >= y + h) return false;
return true;
}
[[nodiscard]] auto contains(const pl2d::Point2I &p) const noexcept -> bool {
INLINE_CONST auto contains(const pl2d::Point2I &p) const noexcept -> bool {
return contains(p.x, p.y);
}

INLINE_CONST auto rect() const -> pl2d::Rect {
return {x, y, x + w, y + h};
}
};

}; // namespace plui
4 changes: 2 additions & 2 deletions src/ld-plos/ld-plos.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ static int load_elf64(Elf *elf, const Elf64Header *header) {
static usize elf_file_map_addr = 0xb0000000;

static i32 load_elf(cstr path, bool run) {
if (st_has(libs, path)) return 0;
// if (st_has(libs, path)) return 0;

const isize size = syscall(SYSCALL_FILE_SIZE, path);
if (size < 0) return LDE_FILE_NOT_FOUND;
Expand All @@ -128,7 +128,7 @@ static i32 load_elf(cstr path, bool run) {

if (syscall(SYSCALL_LOAD_FILE, path, file->data, size) < 0) return LDE_FILE_UNREADABLE;

if (st_insert(libs, path, file) < 0) return LDE_OUT_OF_MEMORY;
// if (st_insert(libs, path, file) < 0) return LDE_OUT_OF_MEMORY;

val ident = file->ident;
if (ident->magic != ELF_MAGIC) return LDE_FILE_UNPARSABLE;
Expand Down
41 changes: 24 additions & 17 deletions src/pl2d/tex/draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,32 @@ auto BaseTexture<T>::trangle(const Point2I &p1, const Point2I &p2, const Point2I
}

template <typename T>
auto BaseTexture<T>::fill(const T &color) -> BaseTexture & {
for (val[x, y] : size_rect()) {
at(x, y) = color;
auto vectorize BaseTexture<T>::fill(const T &color) -> BaseTexture & {
for (usize y = 0; y < height; y++) {
for (usize x = 0; x < width; x++) {
pixels[y * pitch + x] = color;
}
}
return *this;
}

template <typename T>
auto BaseTexture<T>::fill(RectI rect, const T &color) -> BaseTexture & {
for (val[x, y] : rect) {
at(x, y) = color;
auto vectorize BaseTexture<T>::fill(RectI rect, const T &color) -> BaseTexture & {
rect.trunc(size_rect());
for (usize y = rect.y1; y <= rect.y2; y++) {
for (usize x = rect.x1; x <= rect.x2; x++) {
pixels[y * pitch + x] = color;
}
}
return *this;
}

template <typename T>
auto BaseTexture<T>::fill_mix(RectI rect, const T &color) -> BaseTexture & {
for (val[x, y] : rect) {
at(x, y).mix(color);
auto vectorize BaseTexture<T>::fill_mix(RectI rect, const T &color) -> BaseTexture & {
for (usize y = 0; y < height; y++) {
for (usize x = 0; x < width; x++) {
pixels[y * pitch + x].mix(color);
}
}
return *this;
}
Expand All @@ -82,19 +89,19 @@ auto BaseTexture<T>::fill(const T &(*cb)(i32 x, i32 y)) -> BaseTexture & {
template <typename T>
auto BaseTexture<T>::fill_trangle(const Point2I &p1, const Point2I &p2, const Point2I &p3,
const T &color) -> BaseTexture & {
val edge_function = [](const Point2I &a, const Point2I &b, const Point2I &c) {
static lit val edge_function = [](const Point2I &a, const Point2I &b, const Point2I &c) -> i32 {
return (c.x - a.x) * (b.y - a.y) - (c.y - a.y) * (b.x - a.x);
};

Point2I min_point = {cpp::min(p1.x, p2.x, p3.x), cpp::min(p1.y, p2.y, p3.y)};
Point2I max_point = {cpp::max(p1.x, p2.x, p3.x), cpp::max(p1.y, p2.y, p3.y)};
const Point2I min_point = {cpp::min(p1.x, p2.x, p3.x), cpp::min(p1.y, p2.y, p3.y)};
const Point2I max_point = {cpp::max(p1.x, p2.x, p3.x), cpp::max(p1.y, p2.y, p3.y)};

for (i32 y = min_point.y; y <= max_point.y; y++) {
for (i32 x = min_point.x; x <= max_point.x; x++) {
Point2I p = {x, y};
i32 w0 = edge_function(p2, p3, p);
i32 w1 = edge_function(p3, p1, p);
i32 w2 = edge_function(p1, p2, p);
vectorize for (i32 x = min_point.x; x <= max_point.x; x++) {
val p = Point2I(x, y);
val w0 = edge_function(p2, p3, p);
val w1 = edge_function(p3, p1, p);
val w2 = edge_function(p1, p2, p);
if ((w0 >= 0 && w1 >= 0 && w2 >= 0) || (w0 <= 0 && w1 <= 0 && w2 <= 0)) { //
at(x, y) = color;
}
Expand Down
2 changes: 2 additions & 0 deletions src/pl2d/tex/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ BaseTexture<T>::BaseTexture(u32 width, u32 height)
own_pixels = true;
alloced_size = size;
pixels = (T *)malloc(size * sizeof(T));
free_pixels = free;
assert(pixels != null);
}

Expand All @@ -18,6 +19,7 @@ BaseTexture<T>::BaseTexture(u32 width, u32 height, u32 pitch)
own_pixels = true;
alloced_size = size;
pixels = (T *)malloc(size * sizeof(T));
free_pixels = free;
assert(pixels != null);
}

Expand Down
6 changes: 3 additions & 3 deletions test/plui/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ enum class Event : u32 {

namespace plds {

// plui::Element root;
plui::Element root;

pl2d::FrameBuffer screen_fb;
pl2d::TextureB screen_tex;
Expand Down Expand Up @@ -84,13 +84,13 @@ void flush() {
float i = (f32)nframe * .01f;
tex.fill(pl2d::PixelF::lab(.8f, cpp::cos(i) * .1f, cpp::sin(i) * .1f));
tex.transform([](auto &pix, i32 x, i32 y) {
val k = cpp::sin((x - y + nframe * 4) / 25.f) / 5.f + .8f + .5f;
val k = cpp::sin((x - y + nframe * 4) / 25.f) / 5.f + .8f;
if ((x + y) / 25 % 2 == 0) pix.mix_ratio(pl2d::PixelF{k, k, k}, 0.25);
});
frame_tex[nframe / 60 % 19].paste_to_mix(tex, 20, 20);
image_tex.paste_to_mix(tex, 900, 0);
tex.fill_trangle({100, 100}, {200, 200}, {100, 200},
pl2d::PixelF{.8, cpp::cos(i + 1.f) * .1f, cpp::sin(i + 1.f) * .1f, 1}.LAB2RGB());
pl2d::PixelF::lab(.8, cpp::cos(i + 1.f) * .1f, cpp::sin(i + 1.f) * .1f));
fb.flush(tex);
screen_flush();
}
Expand Down

0 comments on commit 5d7d2c1

Please sign in to comment.