njin
Engine 2D gọn nhẹ để tự tay làm game theo ý mình. Vibe coding cũng ổn luôn
Loading...
Searching...
No Matches
njin_draw.h
1#pragma once
2#include "_math.h"
3#include <string>
4#include <vector>
5
6namespace njin {
7struct context;
8
11
20void draw_rect(const context &ctx, rect r, rgba color);
21
27void draw_rect_lines(const context &ctx, rect r, f32 thickness, rgba color);
28
35void draw_rect_rotated(const context &ctx, vec2 center, vec2 size,
36 f32 rotation, rgba color);
37
43void draw_circle(const context &ctx, vec2 center, f32 radius, rgba color);
44
51void draw_circle_lines(const context &ctx, vec2 center, f32 radius,
52 f32 thickness, rgba color);
53
60void draw_line(const context &ctx, vec2 a, vec2 b, f32 thickness, rgba color);
61
68void draw_triangle(const context &ctx, vec2 a, vec2 b, vec2 c, rgba color);
69
84void draw_offset_push(const context &ctx, vec2 offset, f32 scale = 1.0f);
85
88void draw_offset_pop(const context &ctx);
90
93
100
112font_handle font_load(context &ctx, const char *path, i32 size = 0,
113 font_style style = font_smooth);
114
128void font_set_style(context &ctx, font_handle font, font_style style);
129
134void font_unload(context &ctx, font_handle font);
135
149void draw_text(const context &ctx, const char *text, vec2 pos, f32 size,
150 rgba color, font_handle font = {});
151
160vec2 text_measure(const context &ctx, const char *text, f32 size,
161 font_handle font = {});
162
174std::vector<std::string> text_wrap(const context &ctx, const char *text, f32 size,
175 f32 max_width, font_handle font = {});
176
188vec2 draw_text_wrapped(const context &ctx, const char *text, vec2 pos, f32 size,
189 f32 max_width, rgba color, font_handle font = {},
190 f32 line_spacing = 1.1f);
192
195
205
210void texture_set_filter(context &ctx, texture_handle handle,
211 texture_filter filter);
212
222 texture_filter filter);
223
230 vec2 scale{1.0f, 1.0f};
234 f32 rotation = 0.0f;
235 bool flip_x = false;
236 bool flip_y = false;
237 rgba tint{1.0f, 1.0f, 1.0f, 1.0f};
238};
239
244void texture_draw_ex(const context &ctx, texture_handle handle,
245 const texture_draw_desc &desc);
246
263void texture_set_shader(context &ctx, texture_handle handle,
264 shader_handle shader);
266
269
276
280void blend_begin(const context &ctx, blend_mode mode);
281
293void draw_set_y_sort(context &ctx, i32 layer, bool on);
294
297void blend_end(const context &ctx);
298
305void clip_begin(const context &ctx, rect area);
306
309void clip_end(const context &ctx);
311} // namespace njin
void blend_end(const context &ctx)
Trở lại cách trộn mặc định.
void draw_rect_lines(const context &ctx, rect r, f32 thickness, rgba color)
Vẽ viền hình chữ nhật.
void draw_triangle(const context &ctx, vec2 a, vec2 b, vec2 c, rgba color)
Vẽ tam giác đặc.
void draw_rect_rotated(const context &ctx, vec2 center, vec2 size, f32 rotation, rgba color)
Vẽ hình chữ nhật đặc xoay quanh tâm của nó.
void blend_begin(const context &ctx, blend_mode mode)
Đổi cách trộn màu cho mọi thứ vẽ sau đó, cho đến blend_end().
void draw_circle(const context &ctx, vec2 center, f32 radius, rgba color)
Vẽ hình tròn đặc.
void draw_rect(const context &ctx, rect r, rgba color)
Vẽ hình chữ nhật đặc.
void draw_line(const context &ctx, vec2 a, vec2 b, f32 thickness, rgba color)
Vẽ đoạn thẳng.
void clip_end(const context &ctx)
Bỏ giới hạn vùng vẽ.
void draw_offset_pop(const context &ctx)
Bỏ độ dời của lần draw_offset_push() gần nhất.
void draw_circle_lines(const context &ctx, vec2 center, f32 radius, f32 thickness, rgba color)
Vẽ viền hình tròn.
void draw_set_y_sort(context &ctx, i32 layer, bool on)
Bật sắp xếp theo Y cho một lớp vẽ: trong lớp đó, sprite và particle có y lớn hơn (thấp hơn trên màn h...
blend_mode
Cách trộn màu của những gì vẽ sau blend_begin().
Definition njin_draw.h:271
void draw_offset_push(const context &ctx, vec2 offset, f32 scale=1.0f)
Phóng mọi hình vẽ sau đó theo scale rồi dời đi offset (đơn vị thế giới) cho tới draw_offset_pop(): đi...
void clip_begin(const context &ctx, rect area)
Chỉ vẽ bên trong một vùng của màn hình, cho đến clip_end().
@ blend_additive
Cộng màu: sáng lên. Dùng cho lửa, ánh sáng, tia lửa.
Definition njin_draw.h:273
@ blend_multiply
Nhân màu: tối đi. Dùng cho bóng đổ, lớp phủ màu.
Definition njin_draw.h:274
@ blend_alpha
Trộn theo độ trong suốt. Mặc định.
Definition njin_draw.h:272
vec2 text_measure(const context &ctx, const char *text, f32 size, font_handle font={})
Kích thước một chuỗi khi vẽ bằng draw_text() với cùng tham số.
font_handle font_load(context &ctx, const char *path, i32 size=0, font_style style=font_smooth)
Nạp một font TrueType/OpenType, kèm sẵn các ký tự tiếng Việt.
void font_unload(context &ctx, font_handle font)
Giải phóng font.
font_style
Kiểu dựng chữ, xem font_set_style().
Definition njin_draw.h:95
vec2 draw_text_wrapped(const context &ctx, const char *text, vec2 pos, f32 size, f32 max_width, rgba color, font_handle font={}, f32 line_spacing=1.1f)
Vẽ một đoạn chữ tự xuống dòng trong bề rộng max_width, góc trên trái tại pos.
void font_set_style(context &ctx, font_handle font, font_style style)
Đổi kiểu dựng chữ của một font, kể cả font mặc định (handle id 0).
void draw_text(const context &ctx, const char *text, vec2 pos, f32 size, rgba color, font_handle font={})
Vẽ chữ với góc trên trái tại pos.
std::vector< std::string > text_wrap(const context &ctx, const char *text, f32 size, f32 max_width, font_handle font={})
Chia một đoạn chữ thành các dòng không rộng quá max_width khi vẽ bằng draw_text() với cùng cỡ và font...
@ font_pixel
Không khử răng cưa, lọc nearest: từng texel của glyph hoặc bật hoặc tắt.
Definition njin_draw.h:97
@ font_smooth
Có khử răng cưa, lọc mượt. Hợp font vector, chữ đọc nhiều.
Definition njin_draw.h:96
void texture_draw_ex(const context &ctx, texture_handle handle, const texture_draw_desc &desc)
Vẽ texture với vùng nguồn, tỉ lệ, điểm neo, góc xoay và lật.
texture_filter
Cách lấy mẫu khi texture bị co giãn.
Definition njin_draw.h:197
void texture_set_filter(context &ctx, texture_handle handle, texture_filter filter)
Đổi cách lấy mẫu của texture.
void texture_set_shader(context &ctx, texture_handle handle, shader_handle shader)
Gắn một shader "vật liệu" cố định vào texture: không cần bọc shader_begin()/shader_end() quanh mỗi lầ...
void render_texture_set_filter(context &ctx, render_texture_handle handle, texture_filter filter)
Đổi cách lấy mẫu của render texture.
@ filter_linear
Làm mượt.
Definition njin_draw.h:199
@ filter_mipmap
Làm mượt ở mọi khoảng cách: tạo mipmap và lọc tam tuyến.
Definition njin_draw.h:203
@ filter_nearest
Giữ nguyên từng pixel, sắc cạnh. Dùng cho pixel art.
Definition njin_draw.h:198
std::uint8_t u8
Số nguyên không dấu 8 bit.
Definition _types.h:20
std::int32_t i32
Số nguyên có dấu 32 bit.
Definition _types.h:14
float f32
Số thực 32 bit.
Definition _types.h:28
Định danh của một font, tạo bởi font_load().
Definition _types.h:131
Hình chữ nhật thẳng trục: pos là góc trên trái, size là rộng và cao.
Definition _math.h:13
Định danh của một render texture, tạo bởi render_texture_load().
Definition _types.h:307
Màu gồm đỏ, xanh lá, xanh dương và alpha.
Definition _types.h:64
Định danh của một shader, tạo bởi shader_load().
Definition _types.h:101
Tham số đầy đủ cho texture_draw_ex().
Definition njin_draw.h:225
vec2 scale
Tỉ lệ theo từng trục.
Definition njin_draw.h:230
vec2 pos
Vị trí của điểm neo origin.
Definition njin_draw.h:226
vec2 origin
Điểm neo, tính theo tỉ lệ kích thước: {0, 0} là góc trên trái, {0.5, 0.5} là tâm.
Definition njin_draw.h:233
f32 rotation
Góc xoay tính bằng độ, theo chiều kim đồng hồ.
Definition njin_draw.h:234
rect source
Vùng trong ảnh, tính bằng pixel.
Definition njin_draw.h:229
rgba tint
Màu nhân vào ảnh.
Definition njin_draw.h:237
bool flip_x
Lật ngang.
Definition njin_draw.h:235
bool flip_y
Lật dọc.
Definition njin_draw.h:236
Định danh của một texture, tạo bởi texture_load().
Definition _types.h:146
Vector 2 chiều. Dùng cho vị trí, kích thước, độ dời.
Definition _types.h:39