-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.cpp
More file actions
157 lines (149 loc) · 4.92 KB
/
Copy pathcore.cpp
File metadata and controls
157 lines (149 loc) · 4.92 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
** EPITECH PROJECT, 2022
** B-OOP-400-MAR-4-1-arcade-corentin.fortes
** File description:
** core.cpp
*/
#include "core.hpp"
#include <dirent.h>
void Core::openLibs(std::string libr, std::string gamee, std::string user)
{
void *handle = openlib(libr.c_str());
if (!handle)
return;
create = (creator) dlsym(handle, "create");
display = create();
void *game_ = openlib(gamee.c_str());
if (!game_)
return;
jeu_menu jeu = (jeu_menu) dlsym(game_, "create");
game = jeu();
}
std::string Core::changelib(std::string *lib)
{
DIR* dir = opendir("lib");
std::vector <std::string> libs;
struct dirent* doss;
while ((doss = readdir(dir)) != nullptr) {
if (doss->d_type == DT_REG) {
std::string filename(doss->d_name);
if (filename.length() >= 3 && filename.substr(filename.length() - 3) == ".so") {
void *handle = openlib("lib/" + filename);
if (!handle)
return "";
typelib libt = (typelib) dlsym(handle, "islib");
if (libt()) {
libs.push_back("lib/" + filename);
}
dlclose(handle);
}
}
}
closedir(dir);
for (int i = 0; i < libs.size(); i++) {
if (libs[i] == *lib) {
if (i + 1 >= libs.size()) {
(*lib) = libs[0];
return (libs[0]);
} else {
(*lib) = libs[i + 1];
return (libs[i + 1]);
}
}
}
return "";
}
Core::Core(std::string lib, std::string gamee, std::string user)
{
openLibs(lib, gamee, user);
display->openWindow();
std::vector <text> text;
std::vector <image> sprite;
std::string retour = "";
game->init(&sprite, &text);
display->createTexts(text);
display->createSprites(sprite);
int swap = 0;
while (game->quit() == false) {
std::this_thread::sleep_for(std::chrono::milliseconds(150));
retour = game->input(display->event(), &sprite, &text, &swap);
if (swap == 1) {
void *handle = openlib(changelib(&lib).c_str());
if (!handle)
return;
create = (creator) dlsym(handle, "create");
display->closeWindow();
free(display);
display = create();
display->openWindow();
display->createTexts(text);
display->createSprites(sprite);
swap = 0;
}
game->play(&sprite, &text);
display->drawTexts(text);
display->drawSprites(sprite);
display->displayWindow();
}
if (retour != "") {
user = retour.substr(0, retour.find(" "));
retour.erase(0, retour.find(" ") + 1);
lib = retour.substr(0, retour.find(" "));
retour.erase(0, retour.find(" ") + 1);
gamee = retour.substr(0, retour.find(" "));
display->closeWindow();
Core(lib, gamee, user);
}
display->closeWindow();
free(display);
free(game);
}
// Core::Core(std::string lib, std::string game, std::string user)
// {
// void *handle = openlib(lib.c_str());
// if (!handle)
// return;
// creator create = (creator) dlsym(handle, "create");
// IDisplay *cr = create();
// void *game_ = openlib(game.c_str());
// if (!game_)
// return;
// jeu_menu jeu = (jeu_menu) dlsym(game_, "create");
// IGame *j = jeu(cr);
// bool boolean = j->quit();
// std::string retour = j->finish();
// if (retour.length() > 0) {
// lib = retour.substr(0, retour.find(" "));
// retour.erase(0, retour.find(" ") + 1);
// game = retour.substr(0, retour.find(" "));
// retour.erase(0, retour.find(" ") + 1);
// user = retour.substr(0, retour.find(" "));
// }
// while (boolean == false) {
// void *handle = openlib(lib.c_str());
// if (!handle)
// return;
// creator create = (creator) dlsym(handle, "create");
// IDisplay *cr = create();
// void *game_ = openlib(game.c_str());
// if (!game_)
// return;
// jeu_menu jeu = (jeu_menu) dlsym(game_, "create");
// IGame *j = jeu(cr);
// boolean = j->quit();
// std::string retour = j->finish();
// if (retour.length() > 0) {
// lib = retour.substr(0, retour.find(" "));
// retour.erase(0, retour.find(" ") + 1);
// game = retour.substr(0, retour.find(" "));
// retour.erase(0, retour.find(" ") + 1);
// user = retour.substr(0, retour.find(" "));
// }
// }
// }
// text::text (std::string name, std::string str, int x, int y, std::string color) : name(name), str(str), x(x), y(y), color(color)
// {
// }
// image::image (std::string name, std::string path, int x, int y, char chara, std::string color) : name(name), path(path), x(x), y(y), chara(chara), color(color)
// {
// }