author | František Kučera <franta-hg@frantovo.cz> |
Sun, 03 Dec 2023 19:22:25 +0100 | |
branch | v_0 |
changeset 17 | f53a040bdc89 |
parent 6 | fd93a46db15b |
child 26 | e7ceb915177e |
permissions | -rw-r--r-- |
0 | 1 |
/** |
2 |
* ShaderShark |
|
3 |
* Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info) |
|
4 |
* |
|
5 |
* This program is free software: you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation, version 3 of the License. |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 |
*/ |
|
17 |
||
18 |
#pragma once |
|
19 |
||
20 |
#include <vector> |
|
21 |
#include <string> |
|
22 |
||
23 |
#include "Configuration.h" |
|
24 |
||
25 |
class CLIParser { |
|
26 |
private: |
|
27 |
||
28 |
static const std::string OPTION_TEXTURE; |
|
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
29 |
static const std::string OPTION_SHADER; |
0 | 30 |
static const std::string OPTION_BACKGROUND_COLOR; |
31 |
static const std::string OPTION_ROOT_WINDOW; |
|
32 |
||
33 |
const std::string |
|
34 |
readNext(const std::vector<std::string>& arguments, int& i) { |
|
35 |
if (i < arguments.size()) return arguments[i++]; |
|
36 |
else throw std::logic_error("Missing CLI argument" |
|
37 |
+ (i > 0 ? (" after " + arguments[i - 1]) : "")); |
|
38 |
} |
|
39 |
||
40 |
bool parseBoolean(const std::string& value) { |
|
41 |
if (value == "true") return true; |
|
42 |
else if (value == "false") return false; |
|
43 |
else throw std::logic_error("Unable to parse boolean value: " |
|
44 |
+ value + " (expecting true or false)"); |
|
45 |
} |
|
46 |
||
47 |
unsigned long |
|
48 |
parseHexColor(const std::string& hex) { |
|
49 |
int r, g, b; |
|
50 |
int count = sscanf(hex.c_str(), "%02x%02x%02x", &r, &g, &b); |
|
51 |
if (count == 3) { |
|
52 |
unsigned long lr, lg, lb; |
|
53 |
lr = r; |
|
54 |
lg = g; |
|
55 |
lb = b; |
|
56 |
return (lr << 16 | lg << 8 | lb); |
|
57 |
} else { |
|
58 |
throw std::logic_error("Invalid hex color string"); |
|
59 |
} |
|
60 |
} |
|
61 |
||
62 |
public: |
|
63 |
||
64 |
const Configuration parse(const std::vector<std::string>& arguments) { |
|
65 |
Configuration c; |
|
66 |
||
67 |
for (int i = 0; i < arguments.size();) { |
|
68 |
const std::string& option = readNext(arguments, i); |
|
69 |
||
70 |
if (option == OPTION_TEXTURE) { |
|
71 |
Configuration::Texture tex; |
|
72 |
tex.fileName = readNext(arguments, i); |
|
73 |
c.textures.push_back(tex); |
|
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
74 |
} else if (option == OPTION_SHADER) { |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
75 |
const auto type = readNext(arguments, i); |
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
76 |
const auto file = readNext(arguments, i); |
6
fd93a46db15b
support custom shaders
František Kučera <franta-hg@frantovo.cz>
parents:
5
diff
changeset
|
77 |
c.shaders.push_back({file, type}); |
0 | 78 |
} else if (option == OPTION_BACKGROUND_COLOR) { |
79 |
c.backgroundColor = parseHexColor(readNext(arguments, i)); |
|
80 |
} else if (option == OPTION_ROOT_WINDOW) { |
|
81 |
c.rootWindow = std::stoul(readNext(arguments, i)); |
|
82 |
} else throw std::logic_error("Unsupported CLI option: " + option); |
|
83 |
} |
|
84 |
||
85 |
return c; |
|
86 |
} |
|
87 |
}; |
|
88 |
||
89 |
const std::string CLIParser::OPTION_TEXTURE = "--texture"; |
|
5
ee4ba9f5a053
OOP for Shader and ShaderProgram
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
90 |
const std::string CLIParser::OPTION_SHADER = "--shader"; |
0 | 91 |
const std::string CLIParser::OPTION_BACKGROUND_COLOR = "--background-color"; |
92 |
const std::string CLIParser::OPTION_ROOT_WINDOW = "--root-window"; |