载入中...
搜索中...
未找到
Create.cpp
浏览该文件的文档.
1#include "cmdline.h"
2
3#include <CLI11.hpp>
4#include <fstream>
5#include <filesystem>
6#include <rang.hpp>
7
8using namespace std::filesystem;
9using namespace std;
10
11namespace eve::cmd {
12
14 void setup(CLI::App& app, std::shared_ptr<CLI::Formatter> formatter) override {
15 auto create = app.add_subcommand("create", "Create a new game from template");
16 create->allow_extras();
17 }
18
19 int parse(CLI::App& app, Cmdline& cmd) override {
20 auto create = app.get_subcommand("create");
21 if (create->parsed()) {
22 string name = cmd.get_remaining(create, "mygame");
23 int res = cmd.Create(".", name);
24 if (res == 0) {
25 cout << rang::fg::green << "Created " << name << " in current path" << rang::fg::reset << endl;
26 cout << " run it: eve run " << name << endl;
27 }
28 return res;
29 }
30 return -1; // not handle
31 }
32};
33
35
36
37// create a new project
38int Cmdline::Create(std::string path, std::string name) {
39 if (name.empty()) name = "mygame";
40 std::filesystem::path dir = std::filesystem::path(path) / name;
41 std::error_code ec;
42 std::filesystem::create_directories(dir, ec);
43 if (ec) {
44 cerr << rang::fg::red << "Failed to create " << dir << ": " << ec.message() << rang::fg::reset << endl;
45 return 1;
46 }
47
48 const std::string config = R"(config = { width=800 height=600 title=")" + name +
49 R"(" hotReload=true };)";
50 const std::string main = R"(// EVEngine minimal game template.
51// Frame: eve_init (once) -> eve_update(dt) -> eve_render().
52// Guard mutable state so hot reload (re-dofile) does not reset it.
53if (!("boxX" in getroottable())) boxX <- 0.0;
54if (!("boxY" in getroottable())) boxY <- 100.0;
55if (!("vx" in getroottable())) vx <- 240.0;
56
57eve_init = function() {
58 gfx.setBackgroundColor(0.08, 0.10, 0.20, 1.0);
59};
60
61eve_update = function(dt) {
62 boxX += vx * dt;
63 if (boxX > config.width || boxX < 0.0) vx = -vx;
64};
65
66eve_render = function() {
67 gfx.clear();
68 gfx.drawSolidRect(boxX - 20.0, boxY - 20.0, 40.0, 40.0, 1.3, 0.8, 0.4, 1.0);
69};
70)";
71
72 auto writeIfMissing = [&](const char* file, const std::string& content) {
73 auto p = dir / file;
74 if (std::filesystem::exists(p)) {
75 cout << " skip existing " << file << endl;
76 return;
77 }
78 std::ofstream out(p, std::ios::binary);
79 out << content;
80 out.close();
81 };
82 writeIfMissing("config.nut", config);
83 writeIfMissing("main.nut", main);
84 return 0;
85}
86
87} // namespace eve::cmd
filesystem::File * file
glm::vec4 p[6]
const char * name
Definition RockMesh.cpp:21
V3 dir
Definition TreeMesh.cpp:121
命令行模块(eve.cmd):run / build / package / test / zip / dev-server 等子命令入口。
Definition cmdline.h:30
static std::string get_remaining(CLI::App *sub, std::string default_path=".")
子命令剩余位置参数。
Definition cmdline.cpp:79
int Create(std::string path, std::string name)
创建新项目。
Definition Create.cpp:38
#define CMD_REG(name)
Definition cmdline.h:92
int main(int argc, char **argv)
Definition main.cpp:56
int parse(CLI::App &app, Cmdline &cmd) override
Parses arguments and runs the command; returns the exit code.
Definition Create.cpp:19
void setup(CLI::App &app, std::shared_ptr< CLI::Formatter > formatter) override
Registers options on the CLI11 sub-app.
Definition Create.cpp:14
One eve <subcommand> handler: CLI setup + argument parsing.
Definition cmdline.h:21