KUGE
A C++20 game engine made of independent blocks.
Overview
KUGE is a C++20 game engine made of independent building blocks. A game links only the ones it needs: a solo game, a dedicated server and a game where one player hosts the match are all built from the same blocks.
kuge-core holds the fixed-step loop, the scenes and the systems, and kuge-logger the logs. kuge-client adds the window, inputs, 2D drawing, sounds, text and UI. kuge-render3d draws 3D with kronk3d, and kuge-animation handles poses, blends and state machines. kuge-physics handles 2D collisions, kuge-physics3d 3D physics with Jolt. kuge-net, kuge-server and kuge-replication cover the network, lobbies and client-side prediction. A server carries no window and no drawing code: the build system makes it impossible to include what was not linked.
kuge-script opens the game to mods. A mod is a folder: it replaces data (JSON, sprites, sounds) with no code, and can add behaviour in Lua. The game itself describes which components a script may read and write; the script runs in a sandbox with no files, no clock, and a budget of instructions and memory, and reloads while the game runs. In multiplayer, the server picks the mods and turns away a client whose files differ.
The repository also holds a small R-Type, as a server, a client and a host, Arena, a 3D shooter, and Catch, a 250-line game with a mod next to it.
Modules
-
kuge-coreThe fixed-step loop, the scenes and the systems.
-
kuge-loggerLogs: levels, categories, rotating files.
-
kuge-clientThe window, inputs, 2D drawing, sounds, text and UI.
-
kuge-render3d3D with kronk3d: models, lights, camera, particles.
-
kuge-animationPoses, blends and state machines, with no window.
-
kuge-physics2D collisions.
-
kuge-physics3d3D physics, with Jolt.
-
kuge-netThe network.
-
kuge-serverLobbies, on the server side.
-
kuge-replicationClient-side prediction.
-
kuge-scriptLua mods, in a sandbox.
Changelog
1.0.0
Lua modding
- New kuge-script module: Lua scripts in a sandbox, reading and writing the world by name through a single table, world.
- The game chooses what is moddable: a script only sees the components described to the ComponentRegistry.
- ScriptSystem calls every mod’s update(dt) each tick; a mod that fails is stopped alone.
- Hot reload of scripts while the game runs (--dev switch).
- Each mod has its own globals, its own copy of the standard tables and of world, and its own memory limit (16 MiB per mod, 64 MiB in all).
- Sandbox: no io, os, require or load, no bytecode, no clock; 1,000,000 instructions per call.
- Multiplayer: a fingerprint of every mod file; a client without the same ones is turned away.
- Examples: Catch, a 250-line game with the “golden” mod, and the “waves” mod. A full guide chapter on scripts.
UI
- Rounded corners for panels, buttons and text fields; clicks follow the shape.
- Images with rotation, pivot and rounded corners.
- Text too long for its label: clipped, ended with “…”, scrolling or wrapped.
- Children are clipped to their parent’s rectangle; a node over a button takes its clicks.
Rendering
- Base resolution: the game draws at a fixed size, scaled up in fullscreen.
- A picture drawn smaller than it is no longer aliases.
- Camera: view kicks apart from the aim, and roll.
Engine and network
- Localization: texts per language, fallback language, {n} variables and plural forms.
- A client that restarts gets its place back in the match.
3D and levels
- A level is read from a 3D scene: col_ objects are walls, zone_ triggers, spawn_ and lamp markers.
- Mesh colliders for static objects: a roof, a slope, a curved wall.
- Modular characters: one shared model with parts hidden.
- Destructible level, synced between players and handled by prediction.
Windows
- The unit tests build and run on Windows.
- Fixes: frame limit, stop signals, kuge.dll, SDL audio, TCP connections.
0.3.0
Network and rooms
- The game chooses who plays with whom (ServerConfig::roomPicker).
- Player authentication, with a token in room requests.
- Each player’s address is known to the room.
Client
- Music: pause, position, seek and end of playback.
- The UI keeps the order nodes were made in, and removeUiTree removes a whole tree.
0.2.0
3D
- 3D rendering with kronk3d (CPU or OpenGL), models, animation, an orbit camera that stops at walls, instancing.
- 3D physics, hitboxes and character controller; 3D replication.
- Arena, a networked 3D shooter, with placed sounds and particles.
Network
- World history, visibility filter, predicted spawns, replays and rejoin.
- A simulated network (latency, loss) for testing.
Engine and tools
- Mods (data), components by name and cutscenes.
- Profiler, debug options, console with cvars, text chat, log categories.
- Animation (kuge-animation) and restitution in physics.
Client
- UI: clipboard, progress bar, check box, slider, scrolling, images, scaling.
- Audio: dialogue bus, ducking, priorities, muffled 3D sounds, streaming.
- Overlay scenes, logical window size, mouse axes.
0.1.0
First release
- The engine core: fixed-step loop, scenes, systems, window, 2D drawing, animated sprites, sounds, text and UI with a text field.
- Network: named rooms, a list of public rooms, private rooms, client-side prediction.
- Builds and runs on Linux and Windows (MSVC), client libraries from vcpkg, CI on every PR.
Examples
add_executable(my_game main.cpp)
target_link_libraries(my_game PRIVATE kuge-client kuge-physics) # brings kuge-core too class MoveRight : public kw::ISystem
{
public:
bool handle(kw::World& world) override
{
const float dt = static_cast<float>(world.getResource<kuge::Time>().dt);
auto view = world.view<kuge::Transform2D>();
for (kw::Entity entity : view) {
world.get<kuge::Transform2D>(entity).position.x += 100.0f * dt;
}
return true; // false: not called again
}
};
class Hello : public kuge::Scene
{
public:
void onEnter() override
{
addSystem(kw::Schedule::Fixed, kuge::stage::Simulation, std::make_unique<MoveRight>());
}
};
int main()
{
kuge::Engine engine({.mode = kuge::Engine::Mode::Headless, .tickRate = 60});
engine.scenes().change<Hello>();
return engine.run(); // until stop(), Ctrl+C, or no scene is left
} world.add<kuge::Collider>(floor, kuge::Collider::box(400.0f, 20.0f));
world.add<kuge::Collider>(hero, kuge::Collider::box(12.0f, 24.0f));
world.add<kuge::Body>(hero, kuge::Body{kuge::Body::Type::Dynamic}); // velocity, gravityScale...
world.add<kuge::Collider>(coin, kuge::Collider::circle(8.0f).asTrigger()); const kuge::Mods mods = kuge::Mods::scan("mods");
mods.apply(roots); // a mod's files come before the game's
auto registry = std::make_shared<kuge::ComponentRegistry>();
kuge::describeCore(*registry); // Transform2D...
registry->describe<Faller>("Faller", { // ...and what this game lets a script touch
kuge::field<Faller>("speed", &Faller::speed, {.min = 0.0, .max = 1000.0, .help = "Pixels per second"}),
kuge::field<Faller>("value", &Faller::value, {.min = 1.0, .max = 100.0, .help = "Points when caught"}),
});
auto scripts = std::make_shared<kuge::ScriptEngine>(*registry);
for (const auto& script : mods.scripts())
scripts->load(script.file, script.mod); // each mod keeps its own globals -- mods/golden/scripts/golden.lua: every 5 seconds, a gold coin falls twice as fast and is worth 5 points.
local EVERY = 5.0
local timer = 0.0
function update(dt)
timer = timer + dt
if timer >= EVERY then
timer = timer - EVERY
world:spawn{
Transform2D = {position = {math.random(-280, 280), -250}},
Faller = {speed = 240, value = 5},
}
end
end