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
Render texture

Ảnh ngoài màn hình để vẽ vào, dùng cho post-processing. Tiếp...

Các hàm

render_texture_handle njin::render_texture_load (context &ctx, u32 width, u32 height)
 Tạo một render texture: ảnh ngoài màn hình để vẽ vào, rồi vẽ lại như một texture.
void njin::render_texture_unload (context &ctx, render_texture_handle handle)
 Giải phóng render texture.
vec2 njin::render_texture_size (const context &ctx, render_texture_handle handle)
 Kích thước render texture (pixel).
void njin::render_texture_begin (const context &ctx, render_texture_handle handle)
 Bắt đầu vẽ vào render texture, giữ nguyên nội dung cũ.
void njin::render_texture_begin (const context &ctx, render_texture_handle handle, rgba clear)
 Giống bản trên nhưng xóa render texture bằng màu clear trước.
void njin::render_texture_end (const context &ctx)
 Kết thúc vẽ vào render texture.
bool njin::render_texture_save (context &ctx, render_texture_handle handle, const char *path)
 Lưu nội dung render texture ra file ảnh, ngay lập tức.
void njin::render_texture_draw (const context &ctx, render_texture_handle handle, vec2 pos, rgba tint)
 Vẽ nội dung render texture theo đúng chiều, góc trên trái tại pos.

Mô tả chi tiết

Ảnh ngoài màn hình để vẽ vào, dùng cho post-processing.

Thông tin về hàm

◆ render_texture_load()

render_texture_handle njin::render_texture_load ( context & ctx,
u32 width,
u32 height )

Tạo một render texture: ảnh ngoài màn hình để vẽ vào, rồi vẽ lại như một texture.

Dùng cho post-processing hoặc vẽ trước một cảnh phức tạp.

Các tham số
ctxContext của engine.
widthChiều rộng (pixel).
heightChiều cao (pixel).
Giá trị trả về
Handle, hoặc handle có id 0 nếu kích thước bằng 0 hoặc tạo thất bại.

◆ render_texture_unload()

void njin::render_texture_unload ( context & ctx,
render_texture_handle handle )

Giải phóng render texture.

Handle không hợp lệ bị bỏ qua.

Các tham số
ctxContext của engine.
handleRender texture cần giải phóng.

◆ render_texture_size()

vec2 njin::render_texture_size ( const context & ctx,
render_texture_handle handle )

Kích thước render texture (pixel).

Các tham số
ctxContext của engine.
handleRender texture cần hỏi.
Giá trị trả về
Kích thước, hoặc {0, 0} nếu handle không hợp lệ.

◆ render_texture_begin() [1/2]

void njin::render_texture_begin ( const context & ctx,
render_texture_handle handle )

Bắt đầu vẽ vào render texture, giữ nguyên nội dung cũ.

Mọi lệnh vẽ giữa render_texture_begin() và render_texture_end() đi vào render texture, trong hệ pixel của chính nó (không qua camera).

Lệnh này đặt lại phép biến đổi của camera, nên không dùng trong phase_pre_render hay phase_render. Hãy vẽ vào render texture ở phase_post_update hoặc phase_post_render.

Các tham số
ctxContext của engine.
handleRender texture cần vẽ vào. Handle không hợp lệ bị bỏ qua.

◆ render_texture_begin() [2/2]

void njin::render_texture_begin ( const context & ctx,
render_texture_handle handle,
rgba clear )

Giống bản trên nhưng xóa render texture bằng màu clear trước.

Các tham số
ctxContext của engine.
handleRender texture cần vẽ vào. Handle không hợp lệ bị bỏ qua.
clearMàu dùng để xóa.

◆ render_texture_end()

void njin::render_texture_end ( const context & ctx)

Kết thúc vẽ vào render texture.

Các tham số
ctxContext của engine.

◆ render_texture_save()

bool njin::render_texture_save ( context & ctx,
render_texture_handle handle,
const char * path )

Lưu nội dung render texture ra file ảnh, ngay lập tức.

Ảnh theo đúng chiều đã vẽ (góc trên trái của hình vẽ là góc trên trái của file) và giữ nguyên từng byte, kể cả kênh alpha: không nhân hay chia alpha, nên một render texture dùng để chứa dữ liệu (sprite sheet đã bake, xem draw_instanced()) lưu ra được đúng như nó chứa.

Định dạng theo đuôi file: .png (nên dùng), .bmp, .tga, .qoi. Thư mục cha được tạo nếu chưa có. Không gọi giữa render_texture_begin() và render_texture_end(). Đọc lại từ GPU nên chậm hơn nhiều so với một lệnh vẽ: dùng khi xuất file, không phải mỗi frame.

Các tham số
ctxContext của engine.
handleRender texture cần lưu. Handle không hợp lệ thì trả về false.
pathĐường dẫn file. Muốn lưu vào thư mục lưu game thì dùng save_path().
Giá trị trả về
true nếu đã lưu. Lý do thất bại (đuôi file lạ, không ghi được) ghi vào log.

◆ render_texture_draw()

void njin::render_texture_draw ( const context & ctx,
render_texture_handle handle,
vec2 pos,
rgba tint )

Vẽ nội dung render texture theo đúng chiều, góc trên trái tại pos.

Các tham số
ctxContext của engine.
handleRender texture cần vẽ.
posVị trí góc trên trái.
tintMàu nhân vào ảnh. Màu trắng {1, 1, 1, 1} giữ nguyên ảnh.