refactor: format code

This commit is contained in:
2021-09-19 17:33:16 +02:00
parent 52a143769e
commit 0856ca47ca
71 changed files with 1102 additions and 1110 deletions

View File

@@ -20,7 +20,7 @@
#include <iostream>
#include <cmath>
namespace TowerGui{
namespace TowerGui {
static GLFWwindow* window;
static std::unique_ptr<td::client::Client> client;
@@ -29,7 +29,7 @@ static td::render::Renderer* renderer;
bool serverShouldStop = false;
void init(GLFWwindow* glfw_window, td::render::Renderer* render){
void init(GLFWwindow* glfw_window, td::render::Renderer* render) {
window = glfw_window;
IMGUI_CHECKVERSION();
ImGui::CreateContext();
@@ -44,47 +44,47 @@ void init(GLFWwindow* glfw_window, td::render::Renderer* render){
client = std::make_unique<td::client::Client>(render);
}
void beginFrame(){
void beginFrame() {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
}
void endFrame(){
void endFrame() {
ImGui::EndFrame();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
void renderFPSCounter(){
void renderFPSCounter() {
ImGui::Begin("FPS Counter");
ImGui::Text("FPS : %i", (int)ImGui::GetIO().Framerate);
static bool vsync = true;
if (ImGui::Checkbox("V-Sync", &vsync)){
if (ImGui::Checkbox("V-Sync", &vsync)) {
glfwSwapInterval(vsync);
}
static bool isometric = true;
if (ImGui::Checkbox("Vue Isometrique ?", &isometric)){
if (ImGui::Checkbox("Vue Isometrique ?", &isometric)) {
renderer->setIsometricView(isometric);
}
ImGui::End();
}
bool startServer(int port, const std::string& worldFilePath){
bool startServer(int port, const std::string& worldFilePath) {
if (worldFilePath.empty())
return false;
std::shared_ptr<td::server::Server> server = std::make_shared<td::server::Server>(worldFilePath);
if (!server->start(port)){
if (!server->start(port)) {
return false;
}
serverThread = new std::thread([server](){
while (!serverShouldStop){
serverThread = new std::thread([server]() {
while (!serverShouldStop) {
static std::uint64_t lastTime = td::utils::getTime();
std::uint64_t time = td::utils::getTime();
std::uint64_t delta = time - lastTime;
if (delta >= SERVER_TICK){
if (delta >= SERVER_TICK) {
server->tick(delta);
lastTime = td::utils::getTime();
std::uint64_t sleepTime = SERVER_TICK - (delta - SERVER_TICK);
@@ -97,38 +97,37 @@ bool startServer(int port, const std::string& worldFilePath){
return true;
}
void renderMainMenu(){
void renderMainMenu() {
ImGui::Begin("Main Menu");
if (ImGui::Button("Rejoindre une partie##join")){
if (ImGui::Button("Rejoindre une partie##join")) {
ImGui::OpenPopup("Rejoindre une partie##join_popup");
}
if (ImGui::Button("Créer une partie")){
if (ImGui::Button("Créer une partie")) {
ImGui::OpenPopup("Créer une partie##create_popup");
}
if (ImGui::Button("Options")){
if (ImGui::Button("Options")) {
}
static bool triedToConnect = false;
if (ImGui::BeginPopup("Rejoindre une partie##join_popup")){
if (ImGui::BeginPopup("Rejoindre une partie##join_popup")) {
static char buffer[512] = "localhost";
static int port = 25565;
ImGui::InputText("Server Adress", buffer, sizeof(buffer));
ImGui::InputInt("Port", &port, -1);
if (ImGui::Button("Rejoindre")){
if (ImGui::Button("Rejoindre")) {
client->connect(buffer, port);
triedToConnect = true;
}
if (triedToConnect){
if (triedToConnect) {
ImGui::Text("Impossible de se connecter");
}
ImGui::EndPopup();
}
else{
} else {
triedToConnect = false;
}
static bool triedToCreate = false;
if (ImGui::BeginPopup("Créer une partie##create_popup")){
if (ImGui::BeginPopup("Créer une partie##create_popup")) {
static imgui_addons::ImGuiFileBrowser file_dialog;
static int port = 25565;
static std::string worldFilePath;
@@ -136,33 +135,31 @@ void renderMainMenu(){
ImGui::InputInt("Server Port", &port, -1);
ImGui::Text(std::string("Fichier de monde sélectionné : " + (worldFilePath.empty() ? std::string("Aucun") : worldFilePath)).c_str());
ImGui::SameLine();
if (ImGui::Button("Ouvrir un fichier")){
if (ImGui::Button("Ouvrir un fichier")) {
ImGui::OpenPopup("WorldFileDialog");
}
if (file_dialog.showFileDialog("WorldFileDialog", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, ImVec2(600, 300), ".tdmap")){
if (file_dialog.showFileDialog("WorldFileDialog", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, ImVec2(600, 300), ".tdmap")) {
worldFilePath = file_dialog.selected_path;
}
if (ImGui::Button("Créer")){
if (!startServer(port, worldFilePath)){
if (ImGui::Button("Créer")) {
if (!startServer(port, worldFilePath)) {
triedToCreate = true;
}
else{
} else {
client->connect("localhost", port);
}
}
if (triedToCreate)
ImGui::Text("Failed to launch server");
ImGui::EndPopup();
}
else{
} else {
triedToCreate = false;
}
ImGui::End();
}
ImVec4 getImGuiTeamColor(td::game::TeamColor color){
switch (color){
ImVec4 getImGuiTeamColor(td::game::TeamColor color) {
switch (color) {
case td::game::TeamColor::None:
break;
case td::game::TeamColor::Red:
@@ -173,9 +170,9 @@ ImVec4 getImGuiTeamColor(td::game::TeamColor color){
return ImVec4(1, 1, 1, 1);
}
void showPlayers(){
if (ImGui::TreeNode(std::string("Players (" + std::to_string(client->getGame().getPlayers().size()) + ")##player_list").c_str())){
for (auto pair : client->getGame().getPlayers()){
void showPlayers() {
if (ImGui::TreeNode(std::string("Players (" + std::to_string(client->getGame().getPlayers().size()) + ")##player_list").c_str())) {
for (auto pair : client->getGame().getPlayers()) {
const td::game::Player& player = pair.second;
ImGui::PushStyleColor(ImGuiCol_Text, getImGuiTeamColor(player.getTeamColor()));
ImGui::Text(player.getName().c_str());
@@ -185,19 +182,19 @@ void showPlayers(){
}
}
void showTeamSelection(){
void showTeamSelection() {
if (client->getGame().getPlayer() == nullptr)
return;
td::game::TeamColor playerTeam = client->getGame().getPlayer()->getTeamColor();
if (ImGui::Button(std::string((playerTeam == td::game::TeamColor::Red ? "Leave" : "Join") + std::string(" Red Team")).c_str())){
if (ImGui::Button(std::string((playerTeam == td::game::TeamColor::Red ? "Leave" : "Join") + std::string(" Red Team")).c_str())) {
if (playerTeam == td::game::TeamColor::Red)
client->selectTeam(td::game::TeamColor::None);
else
client->selectTeam(td::game::TeamColor::Red);
}
ImGui::SameLine();
if (ImGui::Button(std::string((playerTeam == td::game::TeamColor::Blue ? "Leave" : "Join") + std::string(" Blue Team")).c_str())){
if (ImGui::Button(std::string((playerTeam == td::game::TeamColor::Blue ? "Leave" : "Join") + std::string(" Blue Team")).c_str())) {
if (playerTeam == td::game::TeamColor::Blue)
client->selectTeam(td::game::TeamColor::None);
else
@@ -205,36 +202,35 @@ void showTeamSelection(){
}
}
void showLobbyProgress(){
void showLobbyProgress() {
const int timePassed = LOBBY_WAITING_TIME - client->getGame().getLobbyTime();
const float progress = (float)timePassed / (float)(LOBBY_WAITING_TIME);
if (progress > 0 && progress < 1){
if (progress > 0 && progress < 1) {
ImGui::ProgressBar(progress, ImVec2(0.0f, 0.0f), std::string(std::to_string(client->getGame().getLobbyTime() / 1000) + "s").c_str());
ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x);
ImGui::Text("Time Remaining");
}
else{
} else {
ImGui::Text("Waiting for players ...\n");
}
}
void showTPS(){
void showTPS() {
ImGui::Text("Server TPS : %.1f", client->getConnexion().getServerTPS());
ImGui::Text("Server Ping : %i", client->getConnexion().getServerPing());
}
void showStats(){
void showStats() {
ImGui::Text("Gold : %i", client->getGame().getPlayer()->getGold());
}
void renderSummonMenu(){
void renderSummonMenu() {
static bool menu_open = false;
if (menu_open){
if (menu_open) {
ImGui::Begin("Summon", &menu_open);
static int width = 100;
ImTextureID my_tex_id = ImGui::GetIO().Fonts->TexID;
for (int i = 0; i < 8; i++){
for (int i = 0; i < 8; i++) {
ImGui::SameLine();
ImGui::PushID(i);
ImGui::Image(my_tex_id, ImVec2(100, 100));
@@ -243,7 +239,7 @@ void renderSummonMenu(){
ImGui::Separator();
static int values[16];
ImGui::PushItemWidth(width);
for (int i = 0; i < 8; i++){
for (int i = 0; i < 8; i++) {
ImGui::SameLine();
ImGui::PushID(i);
ImGui::InputInt("", values + i, 1, 10);
@@ -251,7 +247,7 @@ void renderSummonMenu(){
}
ImGui::PopItemWidth();
ImGui::Separator();
for (int i = 0; i < 8; i++){
for (int i = 0; i < 8; i++) {
ImGui::SameLine();
ImGui::PushID(i);
ImGui::Image(my_tex_id, ImVec2(100, 100));
@@ -259,7 +255,7 @@ void renderSummonMenu(){
}
ImGui::Separator();
ImGui::PushItemWidth(width);
for (int i = 8; i < 16; i++){
for (int i = 8; i < 16; i++) {
ImGui::SameLine();
ImGui::PushID(i);
ImGui::InputInt("", values + i, 1, 10);
@@ -270,8 +266,8 @@ void renderSummonMenu(){
}
}
void renderGame(){
if (client->getGame().getGameState() == td::game::GameState::Lobby){
void renderGame() {
if (client->getGame().getGameState() == td::game::GameState::Lobby) {
ImGui::Begin("Lobby");
showTPS();
@@ -281,7 +277,7 @@ void renderGame(){
ImGui::End();
}
if (client->getGame().getGameState() == td::game::GameState::Game){
if (client->getGame().getGameState() == td::game::GameState::Game) {
ImGui::Begin("Game");
showTPS();
@@ -292,7 +288,7 @@ void renderGame(){
}
}
void tick(){
void tick() {
static std::uint64_t lastTime = td::utils::getTime();
std::uint64_t time = td::utils::getTime();
@@ -303,7 +299,7 @@ void tick(){
lastTime = td::utils::getTime();
}
void render(){
void render() {
tick();
beginFrame();
client->render();
@@ -319,11 +315,11 @@ void render(){
endFrame();
}
void destroy(){
void destroy() {
client->closeConnection();
client.reset();
serverShouldStop = true;
if (serverThread != nullptr){
if (serverThread != nullptr) {
serverThread->join();
delete serverThread;
}