载入中...
搜索中...
未找到
Create.cpp
浏览该文件的文档.
1#include "cmdline.h"
2
3#include <CLI11.hpp>
4#include <filesystem>
5#include <fstream>
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 projectName) {
39 if (projectName.empty()) projectName = "mygame";
40 std::filesystem::path dir = std::filesystem::path(path) / projectName;
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"(// EVEngine project configuration.
49config <- {
50 width = 800
51 height = 600
52 title = ")" + projectName +
53 R"("
54 hotReload = true
55 modules = ["gfx"]
56}
57)";
58 const std::string main = R"(// EVEngine minimal game template.
59// Frame: eve_init (once) -> eve_update(dt) -> eve_render().
60// EveScript persist declarations retain mutable state across hot reload.
61persist boxX: float = 0.0
62persist boxY: float = 100.0
63persist vx: float = 240.0
64
65eve_init = function() {
66 gfx.setBackgroundColor(0.08, 0.10, 0.20, 1.0);
67};
68
69eve_update = function(dt: float) {
70 boxX += vx * dt;
71 if (boxX > config.width || boxX < 0.0) vx = -vx;
72};
73
74eve_render = function() {
75 gfx.clear();
76 gfx.drawSolidRect(boxX - 20.0, boxY - 20.0, 40.0, 40.0, 1.3, 0.8, 0.4, 1.0);
77};
78)";
79
80 auto writeIfMissing = [&](const char* file, const std::string& content) {
81 auto p = dir / file;
82 if (std::filesystem::exists(p)) {
83 cout << " skip existing " << file << endl;
84 return;
85 }
86 std::ofstream out(p, std::ios::binary);
87 out << content;
88 out.close();
89 };
90 writeIfMissing("config.nut", config);
91 writeIfMissing("main.nut", main);
92 return 0;
93}
94
95} // namespace eve::cmd
glm::vec4 p[6]
filesystem::File * file
std::string name
std::string path
Definition PlayHost.cpp:110
V3 dir
Definition TreeMesh.cpp:150
命令行模块(eve.cmd):run / build / package / test / zip / dev-server 等子命令入口。
Definition cmdline.h:33
static std::string get_remaining(CLI::App *sub, std::string default_path=".")
子命令剩余位置参数。
Definition cmdline.cpp:80
int Create(std::string path, std::string name)
创建新项目。
Definition Create.cpp:38
#define CMD_REG(name)
Definition cmdline.h:101
int main(int argc, char **argv)
Definition main.cpp:46
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:23