MainLoop
A base timing loop for C++ games and apps.
Install / Use
/learn @mattearly/MainLoopREADME
MainLoop
MainLoop, as the name might suggest, is a main loop built with C++. It includes calculating the delta time between updates for the programmer to use for calculations, timings, physics, or whatever you need delta time for. MainLoop can be dropped into any project as a single header.
Examples
A Sample Text RPG Simulation using this loop
#include "MainLoop.h"
#include <iostream>
#include <string>
int runDemoTestGame()
{
struct Player {
int str = 1;
int hp = 1;
int exp = 0;
};
static Player player;
static std::string playername;
static std::string action;
static bool keepRunning;
MainLoop::Get()->SetRunCondition([]() { return keepRunning; });
MainLoop::Get()->AddToOnBegin([]() {
keepRunning = true;
std::cout << "Welcome to a new game of trainer.\n";
std::cout << "Enter your character's name: ";
std::getline(std::cin, playername);
});
MainLoop::Get()->AddToOnUpdate([]() {
std::cout << "Enter an action('attack' or 'train' or 'quit'): ";
std::getline(std::cin, action);
if (action == "quit")
{
keepRunning = false;
}
else if (action == "attack")
{
if (player.str > 4)
player.exp++;
if (player.exp > player.str)
player.hp++;
}
else if (action == "train")
{
player.str++;
}
});
MainLoop::Get()->AddToOnPostUpdate([]() {
std::cout << playername << ' ' << action << "s!\n";
std::cout << " hp: " << player.hp << '\n';
std::cout << " exp: " << player.exp << '\n';
std::cout << " str: " << player.str << '\n';
});
return MainLoop::Get()->Run();
}
int main(){ return runDemoTestGame(); }
Related Skills
node-connect
343.3kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
92.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
343.3kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
343.3kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
