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
Texture

Nạp và vẽ texture từ file ảnh. Tiếp...

Classes

struct  njin::atlas_desc
 Cách tạo một atlas, xem atlas_create(). Tiếp...
struct  njin::texture_draw_desc
 Tham số đầy đủ cho texture_draw_ex(). Tiếp...

Enumerations

enum  njin::texture_filter { njin::filter_nearest , njin::filter_linear , njin::filter_mipmap }
 Cách lấy mẫu khi texture bị co giãn. Tiếp...

Các hàm

atlas_handle njin::atlas_create (context &ctx, const atlas_desc &desc={})
 Tạo một atlas: nơi ghép nhiều ảnh nhỏ vào một texture lớn.
texture_handle njin::atlas_load (context &ctx, atlas_handle atlas, const char *path)
 Nạp một file ảnh và xếp vào atlas.
void njin::atlas_destroy (context &ctx, atlas_handle atlas)
 Hủy atlas: giải phóng các trang.
void njin::texture_set_filter (context &ctx, texture_handle handle, texture_filter filter)
 Đổi cách lấy mẫu của texture.
void njin::render_texture_set_filter (context &ctx, render_texture_handle handle, texture_filter filter)
 Đổi cách lấy mẫu của render texture.
void njin::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.
void njin::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ần vẽ nữa.
texture_handle njin::texture_load (context &ctx, const char *path)
 Nạp một texture từ file ảnh.
void njin::texture_unload (context &ctx, texture_handle handle)
 Giải phóng texture.
vec2 njin::texture_size (const context &ctx, texture_handle handle)
 Kích thước texture (pixel).
void njin::texture_draw (const context &ctx, texture_handle handle, vec2 pos, rgba tint)
 Vẽ texture với góc trên trái tại pos.

Mô tả chi tiết

Nạp và vẽ texture từ file ảnh.

Thông tin về Enumeration Type

◆ texture_filter

Cách lấy mẫu khi texture bị co giãn.

Enumerator
filter_nearest 

Giữ nguyên từng pixel, sắc cạnh. Dùng cho pixel art.

filter_linear 

Làm mượt.

Mặc định.

filter_mipmap 

Làm mượt ở mọi khoảng cách: tạo mipmap và lọc tam tuyến.

Cho ảnh lặp lại trên mặt 3D lớn nhìn từ xa (tường, sàn), nơi filter_linear lấp lánh. Ảnh trong atlas và render texture lấy filter_linear.

Thông tin về hàm

◆ atlas_create()

atlas_handle njin::atlas_create ( context & ctx,
const atlas_desc & desc = {} )

Tạo một atlas: nơi ghép nhiều ảnh nhỏ vào một texture lớn.

Raylib gom các lệnh vẽ liền nhau dùng chung texture thành một. Sprite xếp theo layer hoặc theo y mà dùng nhiều texture khác nhau thì lệnh vẽ bị ngắt mỗi lần đổi texture; ảnh cùng atlas là cùng texture, nên các lệnh đó nhập lại thành một. Xem số lệnh vẽ ước tính trong njin_inspector.

Ảnh nạp bằng atlas_load() cho ra texture_handle thường, dùng được ở mọi nơi nhận texture_handle: sprite, tilemap, hạt, UI, texture_draw().

Các tham số
ctxContext của engine.
descKích thước trang, viền và bộ lọc.
Giá trị trả về
Handle của atlas.

◆ atlas_load()

texture_handle njin::atlas_load ( context & ctx,
atlas_handle atlas,
const char * path )

Nạp một file ảnh và xếp vào atlas.

Atlas thêm trang mới khi trang cũ hết chỗ. Ảnh chỉ xếp vào atlas được nếu vừa một trang; ảnh lớn hơn được nạp như texture_load() và có cảnh báo trong log.

Khác với texture_load():

  • texture_size() trả về kích thước của ảnh, không phải của cả trang.
  • Shader riêng của game (shader_begin()) lấy mẫu texture0 với toạ độ trên cả trang atlas, không phải trên ảnh, nên shader đọc toạ độ 0..1 của ảnh sẽ sai. Với shader như vậy, hãy nạp ảnh bằng texture_load().
  • texture_set_filter() đổi bộ lọc của cả trang.
  • Hot reload không áp dụng.
  • texture_unload() bỏ handle nhưng không thu hồi chỗ trong atlas.
    Các tham số
    ctxContext của engine.
    atlasAtlas cần xếp vào.
    pathĐường dẫn file ảnh.
    Giá trị trả về
    Handle của texture, hoặc handle có id 0 nếu file thiếu hoặc không giải mã được.

◆ atlas_destroy()

void njin::atlas_destroy ( context & ctx,
atlas_handle atlas )

Hủy atlas: giải phóng các trang.

Mọi texture_handle lấy từ atlas này thành không hợp lệ. Handle không hợp lệ bị bỏ qua.

Các tham số
ctxContext của engine.
atlasAtlas cần hủy.

◆ texture_set_filter()

void njin::texture_set_filter ( context & ctx,
texture_handle handle,
texture_filter filter )

Đổi cách lấy mẫu của texture.

Các tham số
ctxContext của engine.
handleTexture cần đổi.
filterCách lấy mẫu.

◆ render_texture_set_filter()

void njin::render_texture_set_filter ( context & ctx,
render_texture_handle handle,
texture_filter filter )

Đổi cách lấy mẫu của render texture.

Render texture mới tạo lấy mẫu kiểu filter_nearest. Đặt filter_linear khi shader của game (xem draw_instanced()) đọc nó ở kích thước khác kích thước đã vẽ.

Các tham số
ctxContext của engine.
handleRender texture cần đổi.
filterCách lấy mẫu.

◆ texture_draw_ex()

void njin::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.

Các tham số
ctxContext của engine.
handleTexture cần vẽ.
descTham số vẽ.

◆ texture_set_shader()

void njin::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ần vẽ nữa.

Từ lần vẽ kế tiếp, mọi texture_draw() và texture_draw_ex() vẽ handle tự bật shader này (kể cả các ảnh phụ đã gắn bằng shader_set_texture()), rồi tắt lại ngay sau khi vẽ xong. Cách cũ vẫn dùng được nguyên vẹn và luôn thắng: nếu đang ở giữa một shader_begin() khác (kể cả của một texture có vật liệu khác), texture_draw()/texture_draw_ex() giữ nguyên shader đó, không thay bằng vật liệu của mình. Đặt uniform bằng shader_set_*() trước khi vẽ, như shader_begin().

Không áp dụng cho sprite/tilemap/particle vẽ qua ECS: những hệ đó tự quản shader riêng (ví dụ flash/dissolve của njin_fx.h).

Các tham số
ctxContext của engine.
handleTexture cần gắn.
shaderShader cần gắn, hoặc handle có id 0 để gỡ vật liệu.

◆ texture_load()

texture_handle njin::texture_load ( context & ctx,
const char * path )

Nạp một texture từ file ảnh.

Handle không hợp lệ hoặc đã unload bị mọi hàm texture bỏ qua.

Các tham số
ctxContext của engine.
pathĐường dẫn file ảnh.
Giá trị trả về
Handle của texture, hoặc handle có id 0 nếu file thiếu hoặc không giải mã được.

◆ texture_unload()

void njin::texture_unload ( context & ctx,
texture_handle handle )

Giải phóng texture.

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

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

◆ texture_size()

vec2 njin::texture_size ( const context & ctx,
texture_handle handle )

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

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

◆ texture_draw()

void njin::texture_draw ( const context & ctx,
texture_handle handle,
vec2 pos,
rgba tint )

Vẽ texture với góc trên trái tại pos.

tint được nhân vào từng pixel, màu trắng {1, 1, 1, 1} giữ nguyên ảnh. Phải gọi giữa lúc bắt đầu và kết thúc vẽ của frame.

Các tham số
ctxContext của engine.
handleTexture cần vẽ.
posVị trí góc trên trái.
tintMàu nhân vào ảnh.