![]() |
vkmEngine 1.0.15
A C++ game engine · vkmengine.com
|
The mechanical rules: how vkmEngine code is laid out, named, formatted and documented, so that any file's shape can be guessed from its neighbours. Where an example here and the code disagree, the code is right and the example is a defect to report.
The docs suite (tests/docs/docs_tests.cpp) checks most of these in the engine, the examples and the template: an undeclared or static function in a .cpp, a Windows macro name, a stray @param, a log category out of step with the logging, a line past 110 columns or a tab (shaders included), a list broken any way but whole or broken when it fits, a comment inside an argument list, a class missing a special member, a struct with a private section - and a reference page that is not ASCII, cites a line number, or names a file, link or member that does not exist.
Code lives under six include roots:
| Root | Include style | Example |
|---|---|---|
| src/engine/ | module-qualified | #include "system/render/render_view.h" |
| src/backend/opengl/ | module-qualified; its own root files are flat | #include "asset/gl_mesh.h", #include "gl_backend.h" |
| src/tools/ | module-qualified | #include "import/texture_loaders.h" |
| src/editor/ | module-qualified, from two roots | #include "panels/inspector_panel.h" and #include "ecs/scene.h" |
| app/ | repo-root-qualified | #include "app/engine_app.h" |
| tests/ | area-qualified; the harness is bare | #include "physics/physics_support.h", #include "support.h" |
The editor's two roots are its own and the engine's; its own root files are included bare (#include "editor_state.h"). Otherwise always include the module path ("ecs/scene.h"), never the bare filename ("scene.h").
A backend header never shares a name with a vkmGL one: vkmGL's names are flat too, and a quoted include resolves beside the including file first, so which of the two you got would depend on where you wrote it.
Engine code never reaches into backend/. It sees the GPU only through RenderBackend and EditorRenderHooks in system/render/, and the window's WindowSurface in platform/window/ (engine.md). A host constructing GLBackend and GLWindowSurface is the composition root, not an exception.
Includes come in three groups, one blank line between: standard library, third-party (<glm/glm.hpp>, <imgui.h>, ...), then local - last, so a local header that drags in a standard one cannot hide a missing include.
Forward-declare a type you use only by pointer or reference in a signature; include its header when it appears by value, as a base class, or where a template needs the full definition. Forward declarations go immediately inside the namespace; a type from another namespace takes a block of its own, closed with a bare } (backend/opengl/gl_frame_context.h shows both kinds).
VKM_REFLECT_BEGIN opens namespace Vkm::Engine::Reflect itself, so it goes at global scope, after the namespace close, naming the type fully qualified:
What else a component owes the scene format is design.md.
The own header comes first so a missing include inside it fails here. Only #define VKM_LOG_CATEGORY "..." may precede it: logger.h defaults the category when nothing set it, so defining it afterwards redefines the macro.
A file may open several, each directly above the definitions that use it - which is also how a long .cpp marks where a section begins. Helpers inside take no extra prefix (detail_, _internal).
| What | Convention | Example |
|---|---|---|
| Class / struct | PascalCase | RenderView, ObjectDraw |
| Method | camelCase | addSystem(), getScene() |
| Class member | m_ + camelCase | m_scene, m_systemsByStage |
| Struct member | bare camelCase | position, viewportWidth |
| Local variable | camelCase | deltaTime, worldMin |
| File-scope state | g_ + camelCase | g_interrupted |
| thread_local | t_ + camelCase | t_isWorker |
| static local | s_ + camelCase | s_iniPath |
| Constant | UPPER_SNAKE_CASE | ALIVE_BIT, DEFAULT_TICK_RATE |
| Enum class value | PascalCase | SystemStage::Render |
| Type alias | PascalCase | EntityId, MeshHandle |
| Template param | single letter or PascalCase | T, ResourceType |
| Namespace | PascalCase, under Vkm:: | Vkm::Engine, Vkm::GL, Vkm::Log |
| File name | snake_case | render_view.h, gl_forward_pass.cpp |
The three lifetime prefixes mark state that outlives a call - each a threading question. A file-scope g_ lives in the anonymous namespace.
Helper namespaces nest under their umbrella (Vkm::Engine::Math); a file whose content lives in one opens it in the one-line form, namespace Vkm::Engine::Math {, closed by one }.
The OpenGL backend is Vkm::Engine. Vkm::GL is vkmGL; every Vkm::GL block in the backend forward-declares a vkmGL type.
glm::pi<float>(), glm::epsilon<float>() and the rest of glm/gtc/constants.hpp are the engine's vocabulary; a local PI or EPSILON is a second name that drifts. Declare your own constant only when the value is the engine's - a slope limit, a sleep threshold, a cascade count.
windef.h defines near, far and pascal as empty macros: on Windows a local of those names vanishes and the next line is a syntax error naming neither.
| Kind | Members | Rule of 5 | Examples |
|---|---|---|---|
| Data-only struct | bare name | none | Transform, ObjectDraw, FrameContext |
| Class with behavior | m_name | explicit | Engine, RenderSystem, SparseSet<T>, Resource |
If you want m_ on a struct member, the struct is probably a class. If you are skipping the Rule of 5 on a class, it is probably a struct.
| Rule | Setting |
|---|---|
| Indent | 4 spaces, never tabs |
| Braces | K&R - opening brace on the same line |
| Access specifier | indented 4 spaces from class |
| Member body | indented 8 spaces from class (4 inside the access specifier) |
| Keyword spacing | if (, for (, while (, switch ( |
| Call spacing | fn(), obj.method() - no space before ( |
| Pointer / ref | T& name, T* name - the &/* binds to the type |
| Rvalue ref | T && name - one space each side |
| Callable param | Fn&& fn - unspaced (5.2) |
| Line length | 110 columns; a list that would pass it breaks whole (5.3) |
| Statements | one to a line; a body of more than one takes its own lines |
| Namespace close | } // namespace Name; a forward-declaration block, bare } |
Note the double indent on members:
Align related initializers, defaults and trailing comments when the columns read better, never across a blank line. Alignment is for columns of declarations; a continuation line is indented, never aligned to an open paren or an operand above it (5.3).
A forwarded callable or pack is unspaced - Fn&& fn, Args&&... args, auto&&... f - since it reads as one token. Every other && is spaced, forwarding references included: Scene::add(EntityId, T && component).
A parameter list, an argument list or a braced initializer list sits on one line when the line fits in 110 columns. When it does not, it breaks whole: the opener ends its line, each item takes a line of its own one indent past the statement, and the closer starts a line at the statement's indent, carrying whatever follows it ();, ) const override {):
Never the forms between: items aligned under the open paren, several items to a continuation line, or the closer tucked after the last item - each is redone by hand the day a name changes length. tools/reflow_lists <file>... rewrites a file's parenthesised lists to these two forms, changing only whitespace; a braced list, a table and a control statement's condition it leaves to you, and the docs suite names what remains.
Five shapes keep a layout of their own:
A constructor's initializer list that does not fit on the signature's line starts on the next, one indent in, one member to a line with the comma leading, and the body's brace takes a line of its own so it does not read as part of the last member:
A long expression is better split into named steps than wrapped. When one must wrap, it breaks before an operator and the continuation is indented one level past the statement, never aligned under an operand:
No // ----- BAD ----- or // === Section ===; organize with access sections, blank lines, anonymous namespaces and @brief blocks. A separator inside a runtime log string (the boot banner) is output, and exempt.
No comment goes inside an argument list:
Nothing keeps such a comment in step with the parameter it names. Where the call has to show what a value means, name the value: a field set by name, a local, a constant, an enum in place of a bool.
The default is no comment. Write one only when a reader would otherwise have to ask: why does this exist, what invariant does it hold that the types cannot, why is the obvious alternative wrong. First try to make the code say it - a name for the value, a function for the step, a type for the state. A declaration whose name and signature say everything takes no doc block.
| Style | Use for |
|---|---|
| /** @brief ... */ | Public API a caller needs more than the signature for |
| /// | A note on a member or function, above it |
| ///< trailing | A short note on a data member, beside it when the line fits |
| // | Inside a function body - the why |
Inside a function, a comment is bounded by lines: // runs 1-3 lines, and /// on a plain member is one. Past three lines it is usually a diagnosis (implementation.md); the exception is a correctness argument the code cannot state, at the one place a reader would otherwise reconstruct it. A body comment that needs a second paragraph may have become a document, which belongs under docs/reference/ with a pointer left behind.
On a declaration, a comment is bounded by relevance to a caller, who reads it instead of the implementation: one sentence of @brief, then whatever a caller cannot work out - what it is, why it has this shape, what it must not be asked to do. A block is too long when a line of it is something the caller already knew, never because it passed a count.
At any length, a comment may not:
The test that settles most cases: strike the comment and reread. If a competent reader of this engine would now ask a question, keep the answer to it and nothing else.
Doxygen rules:
6.1 reads the same in a CMakeLists.txt, a shader and the Python under tools/: a # run inside a foreach() is a body comment; a block above a target, a function or a uniform is a declaration block. Build files earn more explanation per line - an install() destination, PUBLIC against PRIVATE - since none of it is inferable from the line. The build as a whole belongs in ../reference/building.md.
No check sees a sentence that names real code and says something untrue about it; that is what a review reads for (review.md).
The docs and the code split the work. A page under docs/reference/ explains how a subsystem works - its idea and its concepts; the code holds the detail - fields, steps, constants, edge cases - and a page names the file rather than copying it. The guides hold how we work: the practices, what is good and what is bad, and the workflow. Both exist to help their reader, most often an agent working in this repo with a limited context: a line it does not need costs it, and every sentence is one more thing to keep true.
Every class spells out its five special members, in this order - a resource owner deletes copy and move, a value type defaults them:
The parameter is always other, even when deleted; a blank line separates the copy pair from the move pair; = default for trivial members, = delete to forbid. A gameplay Behavior is the exception: clone() copies it, and the authored fields are its whole interface (10.2).
A System subclass writes the block too, though core/system.h already deletes copy and move. Its class @brief also says which stage it runs at and why that one, against a named sibling (system/sky/sky_system.h).
A class is laid out in this order, each block present only when it has something, a blank line between blocks:
Anything owning a GPU handle, file handle, thread or unique scene state is non-copyable and non-movable; ownership moves with std::unique_ptr<T>, never a hand-written or = default move. A value class (Clock) defaults its five.
An override carries override and never also virtual (~VisibilitySystem() override = default;).
A template anyone else can instantiate lives entirely in a header. A .cpp may define one in three shapes, all of which keep that rule:
If a second translation unit could want an instantiation you have not named, it belongs in the header.
Logging is categorized: #define VKM_LOG_CATEGORY "RENDER" above the own header (section 3), then LOG_TRACE / LOG_INFO / LOG_WARNING / LOG_ERROR with printf-style formatting.
Which channel a failure takes - the log, reportError or a toast - is judgment, not mechanics: implementation.md.
Profile zones go through a facade and compile to nothing without the profiler. Engine code never includes Tracy; only debug/profiler.h and backend/opengl/gl_profiler.h do.
| Family | Header | Macros |
|---|---|---|
| CPU | debug/profiler.h | PROFILE_SCOPE / _NAMED, PROFILE_PLOT |
| GPU | backend/opengl/gl_profiler.h | PROFILE_GPU_CONTEXT, PROFILE_GPU_COLLECT, PROFILE_GPU_SCOPE / _NAMED |
Deliberate deviations. Do not add one without the owner.
It holds std::vector<std::unique_ptr<Behavior>>, so it cannot be copied; deep copy goes through Behavior::clone(). Do not generalize from it.
Authored fields on a Behavior subclass are bare public members, breaking 4.1 on purpose: the field name is the serialized key and the inspector's label, so m_ would leak into both. Runtime state on the same class takes m_. The model is Spinner::degreesPerSecond (templates/default/src/game.h).