Skip to content

Pattern trong Game Engine

Game engine đẩy các pattern tới giới hạn — mỗi frame đều quan trọng khi chạy 60fps.

PatternDự ánỞ đâuTác dụng
Object PoolGodotcore/templates/pooled_list.hPool dựa trên freelist cho entity, particle, physics body
Double BufferingSDLsrc/render/SDL_render.cHoán đổi front/back buffer cho render không tearing
Free ListGodotcore/templates/pooled_list.hAllocator freelist không xen cho cấp/giải phóng entity O(1)
Ring BufferAudio gameNhiều engineBuffer streaming audio lock-free giữa main thread và audio thread
State MachineGodotscene/animation/animation_tree.hState machine animation cho blending hoạt cảnh nhân vật
Arena AllocatorFrame allocatorPattern phổ biếnBump allocator mỗi frame — reset mỗi frame, chi phí giải phóng bằng 0
FlyweightGodotservers/rendering/Tài nguyên mesh/texture chia sẻ được tham chiếu bởi nhiều instance
Batch ProcessingGodot / UnityRender batchingGom các draw call để giảm tối đa thay đổi state GPU
Tagged UnionGodotvariant.hEnum Variant::Type + union — mọi giá trị GDScript đều là một Variant
Dirty FlagGodot / UnityHệ transformDirty flag trên transform cha vô hiệu ma trận thế giới của con — tính lại chỉ khi truy cập
Event LoopGodotmain_loop.hVòng lặp game chính — xử lý input, update, render theo chu kỳ bước cố định

Cách chúng kết hợp: Một frame game

Ở 60fps, mỗi frame có ~16ms. Nhiều pattern phối hợp trong khoảng thời gian đó:

Frame N starts
1
Event Loop

The main loop ticks: process input, run physics, update game state, render. Fixed timestep ensures deterministic simulation.

2
Object Pool + Free List

Spawning a bullet grabs a slot from the pool (O(1)), not malloc. Destroyed entities return to the free list.

3
Dirty Flag

Moving a parent transform marks children dirty. Only recompute world matrices for nodes that actually changed this frame.

4
State Machine

Character animation transitions (idle → run → jump) driven by state machine. Each state knows its blend tree and exit rules.

5
Batch Processing

Draw calls are batched by material/texture to minimize GPU state changes. 100 sprites with same texture = 1 draw call.

6
Double Buffering

The back buffer is swapped to front atomically. Players see a complete frame, never a half-drawn one.

7
Arena Allocator

Per-frame temp allocations (particles, debug draws) use a bump allocator. At frame end: reset pointer to zero. Done.

Insight then chốt: game engine giảm tối đa overhead mỗi object. Pool tránh malloc, dirty flag tránh tính lại, batching tránh GPU call và arena tránh giải phóng từng object. Tất cả đều chia sẻ cùng triết lý thiết kế — trả O(1) mỗi thao tác, hoãn hoặc phân bổ công việc tốn kém.

Đọc thêm

Released under the MIT License.