C++ Game Engine Basics: ECS, Rendering, Physics, Input, Lua [#50-3]
이 글의 핵심
Entity-Component-System, sprite render with z-order, simple AABB physics and collision response, input, and Lua integration for gameplay scripts.
Introduction
Without Unity/Unreal you still need stable simulation, draw order, responsive input, and scriptable logic. This article sketches ECS, 2D rendering (SDL2/SFML), AABB physics, input, and Lua embedding.
Goals: ECS layout, render pipeline with z-index, gravity + collision resolution, input + callbacks, Lua bindings (high level).
Requirements: C++17, SDL2 or SFML, optional Box2D, Lua 5.4.
ECS
- Entity = id + map type_index → unique_ptr
. - Transform, Sprite, RigidBody, Collider components.
- Systems: RenderSystem (sort by z), PhysicsSystem (integrate + resolve), InputSystem.
Note: Naive get_entities_with each frame allocates—production engines use archetypes or sparse sets.
Rendering
- Clear → sort sprites by z_index → SDL_RenderCopyEx → present.
Physics (simple)
- Apply gravity → integrate positions → pairwise AABB test → resolve (bounce/swap velocities); triggers skip impulse.
Input
- Poll keys; optional event callbacks for UI.
Scripting
- Lua loads game data / rules; bind C++ functions with sol2 or C API (article-level detail).
Related posts
- Game engine architecture
- Earlier game engine intro
Summary
ECS + sorted render + simple AABB gives a teachable 2D loop. Profile and replace hot paths (allocation, collision broadphase) as the project grows.