Skip to content

andogensi/CppLiveTuner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

6 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽš๏ธ CppLiveTuner

Stop Recompiling. Start Tuning.

License: MIT C++17 Header-only Platform

A live parameter tuning library for C++.
Edit a config file โ†’ See changes instantly in your running program. No rebuild required.

๐Ÿ“– ๆ—ฅๆœฌ่ชž็‰ˆใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆ


๐Ÿ“‹ STB-style Single Header Library
Just #define LIVETUNER_IMPLEMENTATION in one .cpp file before including.
All other files simply #include "LiveTuner.h" โ€” no Windows.h pollution, no linker headaches!


โœจ Why CppLiveTuner?

Tired of the edit โ†’ compile โ†’ run โ†’ check โ†’ repeat cycle just to tweak a single value?

โŒ Traditional workflow:
   Change value โ†’ Recompile (30 sec~) โ†’ Restart โ†’ Test โ†’ Repeat...

โœ… With CppLiveTuner:
   Change value โ†’ Save file โ†’ See result instantly!

CppLiveTuner eliminates recompilation entirely for parameter tuning. Whether you're adjusting game physics, tweaking shader values, or fine-tuning UI animations, you'll see results in real-time.

๐ŸŽฏ Perfect For

Use Case Example
๐ŸŽฎ Game Development Tweak jump height, movement speed, spawn rates while playing
๐ŸŽจ Graphics/Shaders Adjust exposure, bloom, color grading in real-time
๐Ÿค– Robotics/Simulation Tune PID parameters, sensor thresholds live
๐Ÿ”ง Debug/Profiling Toggle debug displays, adjust log levels on the fly

๐Ÿš€ Features

Feature Description
โšก Zero Recompilation Change parameters while the program is running
๐Ÿ”Œ Header-only Single file, no dependencies. Just #include and go
๐Ÿ›ก๏ธ STB-style No Windows.h pollution in your project
๐ŸŽฏ Non-blocking API Perfect for game loops and real-time apps
๐Ÿ“ Multiple Formats JSON, YAML, INI, plain text
๐Ÿ‘๏ธ Event-driven Watching Native OS APIs (inotify/FSEvents/ReadDirectoryChanges)
๐Ÿงต Thread-safe Full mutex protection
๐ŸŒ Cross-platform Windows, Linux, macOS
๐Ÿงช Testable Design DI support, mock interfaces, test fixtures
๐Ÿ“š nlohmann/json Support Optional adapter for nlohmann/json users

๐ŸŽฌ Demo

CppLiveTuner Demo

Edit the config file โ†’ Changes appear instantly in the running program!


๐Ÿ“ฆ Installation

Just copy one file โ€” that's it!

# Option 1: Copy directly
cp LiveTuner.h /your/project/include/

# Option 2: Git submodule
git submodule add https://github.com/andogensi/CppLiveTuner.git vendor/CppLiveTuner

๐Ÿ Quick Start

1๏ธโƒฃ Setup (STB-style Header-only)

// In main.cpp (or ONE dedicated file)
#define LIVETUNER_IMPLEMENTATION
#include "LiveTuner.h"
// In all other files โ€” just include, no Windows.h pollution!
#include "LiveTuner.h"

2๏ธโƒฃ Simple Usage โ€” Single Value

#define LIVETUNER_IMPLEMENTATION
#include "LiveTuner.h"

int main() {
    float speed = 1.0f;
    
    while (running) {
        tune_try(speed);         // โ† Checks params.txt, updates if changed
        player.move(speed);
    }
}

Edit params.txt while running:

2.5

โ†’ speed instantly becomes 2.5! ๐ŸŽ‰

3๏ธโƒฃ Recommended Usage โ€” Named Parameters

#define LIVETUNER_IMPLEMENTATION
#include "LiveTuner.h"

int main() {
    livetuner::Params params("config.json");
    
    float speed = 1.0f;
    float gravity = 9.8f;
    bool debug = false;
    
    // Bind variables โ€” they auto-update when file changes!
    params.bind("speed", speed, 1.0f);
    params.bind("gravity", gravity, 9.8f);
    params.bind("debug", debug, false);
    
    while (running) {
        params.update();         // โ† All bound variables updated automatically!
        
        player.move(speed);
        physics.setGravity(gravity);
        if (debug) showDebugInfo();
    }
}

Edit config.json while running:

{
    "speed": 2.5,
    "gravity": 15.0,
    "debug": true
}

โ†’ All three values update instantly! ๐Ÿš€


๐Ÿ”จ Build

# GCC / MinGW (C++17+)
g++ -std=c++17 your_program.cpp -I include -o program

# MSVC (C++17+)
cl /std:c++17 your_program.cpp /I include

# Linux (requires pthread)
g++ -std=c++17 your_program.cpp -I include -pthread -o program

๐Ÿ“– API Reference

Simple API โ€” Single Value Tuning

Function Description
tune_init(path) Set parameter file (default: params.txt)
tune_try(value) Non-blocking: Update if changed, returns true if updated
tune(value) Blocking: Wait until value is available
tune_timeout(value, dur) Returns false if timeout
tune_async<T>() Returns std::future<T>
tune_async<T>(callback) Async with callback
tune_set_event_driven(bool) Enable/disable event-driven mode

Params API โ€” Named Parameters

Function Description
Params(path, format) Constructor (format: Auto/Json/Yaml/KeyValue/Plain)
bind(name, var, default) Bind variable to parameter
update() Non-blocking: Update all bindings if changed
get<T>(name) Get as std::optional<T>
get_or<T>(name, default) Get with default value
on_change(callback) Set change callback
start_watching() / poll() Background file monitoring

๐Ÿ“ Supported File Formats

Format Extension Example
JSON .json {"speed": 1.5, "debug": true}
YAML โš ๏ธ .yaml, .yml speed: 1.5
INI .ini, .cfg speed = 1.5
Plain Text .txt 1.5

โš ๏ธ YAML Limitation: Only simple key: value pairs are supported. No nested objects, arrays, or advanced YAML features. See details below.

๐Ÿ“„ Format Examples (click to expand)

JSON

{
    "speed": 1.5,
    "gravity": 9.8,
    "debug": true
}

โš ๏ธ YAML (Simple Key-Value Only)

[!WARNING] CppLiveTuner is NOT a full YAML parser!

Only simple key: value pairs are supported. Nested objects, arrays, multi-line strings, and other advanced YAML features will silently fail or produce unexpected results.

For complex configurations, use JSON instead.

โœ… Supported:

# Comments are supported
speed: 1.5
gravity: 9.8
debug: true
name: "player1"
---
# Document markers are ignored

โŒ NOT Supported:

# Nested structures
player:
  speed: 1.5      # โŒ Nested objects not supported
  position:
    x: 100        # โŒ Multi-level nesting not supported

# Arrays/Lists
items:            # โŒ Arrays not supported
  - sword
  - shield

scores: [1, 2, 3] # โŒ Inline arrays not supported

# Multi-line strings
description: |    # โŒ Block scalars not supported
  This is a
  multi-line text

# Anchors and aliases
defaults: &defaults  # โŒ Anchors not supported
  speed: 1.0

๐Ÿ’ก Need full YAML support?
Use JSON format instead, or integrate a full YAML parser library (like yaml-cpp) and convert to JSON before passing to CppLiveTuner.

INI / Key-Value

speed = 1.5
gravity = 9.8
debug = true

Plain Text

1.5

โšก Event-Driven File Watching

CppLiveTuner uses native OS APIs for maximum efficiency:

OS API Latency
Windows ReadDirectoryChangesW ~1ms
Linux inotify ~1ms
macOS FSEvents ~1ms

Polling vs Event-Driven

Polling ๐Ÿข Event-Driven โšก
CPU Usage High (constant) Near zero
Latency 50-100ms Real-time
SSD Impact Wear over time None

๐ŸŽฎ Real-World Use Cases

Game Development

float jump_height = 10.0f, move_speed = 5.0f;
params.bind("jump_height", jump_height);
params.bind("move_speed", move_speed);

while (game_running) {
    params.update();
    player.jump(jump_height);    // Tweak while playing!
    player.move(move_speed);
}

Shader/Graphics Tuning

float exposure = 1.0f, bloom = 0.8f;
params.bind("exposure", exposure);
params.bind("bloom_threshold", bloom);

while (rendering) {
    params.update();
    shader.setUniform("exposure", exposure);
    shader.setUniform("bloom", bloom);
}

Debug Toggles

bool show_fps = true, show_hitboxes = false;
params.bind("show_fps", show_fps);
params.bind("show_hitboxes", show_hitboxes);

params.on_change([]() {
    std::cout << "Debug settings changed!\n";
});

๐Ÿงช Testable Design

CppLiveTuner supports dependency injection for enterprise-grade testability.

๐Ÿ”ง Testing Patterns (click to expand)

TestFixture for Unit Tests

TEST(MyTest, TestSomething) {
    livetuner::TestFixture fixture;  // Auto-reset state
    tune_init("test.txt");
    // Test runs in isolation
}

Interface-Based DI

class ConfigManager {
    livetuner::IParams& params_;
public:
    explicit ConfigManager(livetuner::IParams& p) : params_(p) {}
    float get_speed() { return params_.get_or("speed", 1.0f); }
};

// Production
livetuner::Params real_params("config.json");
livetuner::ParamsAdapter adapter(real_params);
ConfigManager config(adapter);

// Test with mock
class MockParams : public livetuner::IParams { /* ... */ };

ScopedContext for Parallel Tests

void test_thread_1() {
    livetuner::LiveTuner tuner("test1.txt");
    livetuner::ScopedContext ctx(tuner, params);
    game_loop();  // Uses test1.txt (thread-local)
}

โš™๏ธ Advanced Configuration

Preprocessor Options (click to expand)

External picojson

#define LIVETUNER_USE_EXTERNAL_PICOJSON
#include "picojson.h"
#include "LiveTuner.h"

nlohmann/json Support

#define LIVETUNER_USE_NLOHMANN_JSON
#include <nlohmann/json.hpp>
#define LIVETUNER_IMPLEMENTATION
#include "LiveTuner.h"

livetuner::NlohmannParams params("config.json");

Windows.h Configuration

#define LIVETUNER_NO_WIN32_LEAN  // Keep full Windows.h
#define LIVETUNER_NO_NOMINMAX    // Keep min/max macros
#include "LiveTuner.h"

Logging Control

// Disable default stderr logging
#define LIVETUNER_ENABLE_DEFAULT_LOGGING 0
#include "LiveTuner.h"

// Or use custom logger
livetuner::set_log_callback([](LogLevel lvl, const std::string& msg) {
    MyEngine::Log(lvl, msg);
});

๐Ÿ“‚ Project Structure

CppLiveTuner/
โ”œโ”€โ”€ include/
โ”‚   โ””โ”€โ”€ LiveTuner.h          # ๐ŸŽฏ Main header (just include this!)
โ”œโ”€โ”€ examples/
โ”‚   โ””โ”€โ”€ example.cpp          # Usage examples
โ”œโ”€โ”€ Test/                    # Comprehensive test suite
โ”œโ”€โ”€ cmake/                   # CMake support files
โ””โ”€โ”€ README.md

โ“ Troubleshooting

Problem Solution
File not detected Use absolute paths, check permissions
Values not updating Verify JSON syntax, ensure update() is called
Build errors Enable C++17: -std=c++17 or /std:c++17

๐Ÿค Contributing

Contributions welcome! Feel free to:

  • ๐Ÿ› Report bugs
  • ๐Ÿ’ก Suggest features
  • ๐Ÿ”ง Submit pull requests

๐Ÿ“œ License

MIT License โ€” Free for personal and commercial use.

This project includes third-party components:

  • picojson (embedded in LiveTuner.h) โ€” BSD 2-Clause License ยฉ Kazuho Oku, Cybozu Labs, Inc.
  • nlohmann/json (Json.hpp, optional) โ€” MIT License ยฉ Niels Lohmann

See LICENSE for full details.


โญ Star this repo if you find it useful!

Made with โค๏ธ for the C++ community

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published