-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
97 lines (90 loc) · 2.71 KB
/
Copy pathmain.cpp
File metadata and controls
97 lines (90 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
** EPITECH PROJECT, 2021
** B-OOP-400-BDX-4-1-arcade-honore.dupieu
** File description:
** main
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include "Arcade.hpp"
#include "Tools.hpp"
#include "ArcErrors.hpp"
static void print_help()
{
std::cout << '\n' << "Usage: ./arcade <first-graphic-lib-to-use>" << '\n';
std::cout << " [./lib/]: the folder with all the necessary libs." << '\n';
std::cout << " [./ressources/]: the folder with all the sprites and other ressources." << '\n';
arcade::Arcade::printLibconfHelp();
}
bool check_env(char **env)
{
unsigned short found = 0;
for (; *env; ++env) {
if (!strcmp(*env, "DISPLAY=:0.0") || !strcmp(*env, "XDG_RUNTIME_DIR=/run/user/1000")) {
++found;
}
}
return found == 2;
}
static void setArcade(arcade::Arcade &arcade, const std::string &firstGrLib)
{
arcade.setLibsList("./lib");
if (!arcade.getGalibsPath().size()) {
throw arcade::errors::LibError("No games were found in the './lib' folder.");
}
auto &grlibs = arcade.getGrlibsPath();
if (!grlibs.size()) {
throw arcade::errors::LibError("No graphical lib were found in the './lib' folder.");
}
unsigned int i = 0;
for (; i < grlibs.size(); ++i) {
if (grlibs[0] == firstGrLib) {
break;
}
std::rotate(grlibs.begin(), grlibs.begin() + 1, grlibs.end());
}
if (i == grlibs.size()) {
throw arcade::errors::LibError("The lib '" + firstGrLib + "' could not been loaded.");
}
}
int main(int ac, char **av, char **env)
{
if (ac != 2) {
std::cerr << "Error: You must give the first graphical lib to use." << std::endl;
return 84;
}
if (!strcmp(av[1], "-h") || !strcmp(av[1], "--help")) {
print_help();
return 0;
} else if (!check_env(env)) {
std::cerr << "Make sure the DISPLAY and XDG_RUNTIME_DIR environment variable is set correctly." << '\n';
return 84;
}
std::srand(time(0));
arcade::Arcade arcade;
std::string firstLib = av[1];
firstLib = (Tools::Strings::startsWith(firstLib, "./") || firstLib[0] == '/' ? "" : "./") + firstLib;
int status = 0;
try
{
setArcade(arcade, firstLib);
status = arcade.run();
}
catch(const arcade::errors::Error& e)
{
std::cerr << e.what() << '\n';
status = 84;
}
catch(const arcade::errors::LibError& e)
{
std::cerr << e.what() << '\n';
std::cerr << '\n' << "You should maybe check the 'lib.conf' file." << '\n';
std::cerr << "Get more info with the -h flag." << '\n';
status = 84;
}
catch(...) {
std::cerr << "Ooooh nooooooo" << '\n';
}
return status;
}