![]() |
Kukatz 3D
0.1
Török Attila szakdolgozata
|
00001 /* 00002 * profilemanager.cpp - Kukatz 3D 00003 * Copyright (c) 2012 - TÖRÖK Attila (torokati44@gmail.com) 00004 * 00005 * This software is provided 'as-is', without any express or implied 00006 * warranty. In no event will the authors be held liable for any damages 00007 * arising from the use of this software. 00008 * 00009 * Permission is granted to anyone to use this software for any purpose, 00010 * including commercial applications, and to alter it and redistribute it 00011 * freely, subject to the following restrictions: 00012 * 00013 * 1. The origin of this software must not be misrepresented; you must not 00014 * claim that you wrote the original software. If you use this software 00015 * in a product, an acknowledgment in the product documentation would be 00016 * appreciated but is not required. 00017 * 00018 * 2. Altered source versions must be plainly marked as such, and must not be 00019 * misrepresented as being the original software. 00020 * 00021 * 3. This notice may not be removed or altered from any source 00022 * distribution. 00023 */ 00024 00025 #include "profilemanager.hpp" 00026 00027 #include <fstream> 00028 00029 #include <SFML/Config.hpp> 00030 00031 ProfileManager::ProfileManager() 00032 { 00033 std::ifstream file("profiles", std::ios::binary); 00034 00035 if (file.is_open()) 00036 { 00037 file.seekg(0, std::ios::end); 00038 size_t file_length = file.tellg(); 00039 file.seekg(0, std::ios::beg); 00040 00041 if (file_length > 0) 00042 { 00043 sf::Uint16 num_profiles; 00044 file.read((char*)(&num_profiles), 2); 00045 00046 sf::Uint32 buffer[256]; 00047 sf::Uint8 name_length; 00048 sf::Uint8 head = 0; 00049 sf::Uint8 is_human; 00050 float skill; 00051 sf::Key::Code up, left, down, right; 00052 for (unsigned int i = 0; i < num_profiles; ++i) 00053 { 00054 file.read((char*)&name_length, 1); 00055 file.read((char*)buffer, name_length * 4); 00056 buffer[name_length] = 0; 00057 file.read((char*)&head, 1); 00058 file.read((char*)&is_human, 1); 00059 if (is_human) 00060 { 00061 file.read((char*)&up, 4); 00062 file.read((char*)&left, 4); 00063 file.read((char*)&down, 4); 00064 file.read((char*)&right, 4); 00065 profiles.push_back(new Profile(buffer, head, up, left, down, right)); 00066 profiles.back()->id = profiles.size() - 1; 00067 } 00068 else 00069 { 00070 file.read((char*)&skill, 4); 00071 profiles.push_back(new Profile(buffer, head, skill)); 00072 profiles.back()->id = profiles.size() - 1; 00073 } 00074 } 00075 } 00076 } 00077 } 00078 00079 const std::vector< Profile* >& ProfileManager::get_profiles() 00080 { 00081 return profiles; 00082 } 00083 00084 Profile* ProfileManager::new_profile() 00085 { 00086 Profile* prof_ptr = new Profile(L"Név", 0, 0.0f); 00087 prof_ptr->id = profiles.size(); 00088 profiles.push_back(prof_ptr); 00089 save(); 00090 return prof_ptr; 00091 } 00092 00093 void ProfileManager::add_profile(Profile* p) 00094 { 00095 bool contains = false; 00096 for (unsigned int i = 0; i < profiles.size(); ++i) 00097 { 00098 if (profiles[i] == p) 00099 { 00100 contains = true; 00101 } 00102 profiles[i]->id = i; 00103 } 00104 00105 if (!contains) 00106 { 00107 p->id = profiles.size(); 00108 profiles.push_back(p); 00109 save(); 00110 } 00111 } 00112 00113 void ProfileManager::delete_profile(Profile* p) 00114 { 00115 for (unsigned int i = 0; i < profiles.size(); ++i) 00116 { 00117 if (profiles[i] == p) 00118 { 00119 delete profiles[i]; 00120 for (unsigned int j = i; j < profiles.size() - 1; ++j) 00121 { 00122 profiles[j] = profiles[j + 1]; 00123 } 00124 profiles.pop_back(); 00125 } 00126 } 00127 00128 save(); 00129 } 00130 00131 void ProfileManager::save() 00132 { 00133 std::ofstream file("profiles", std::ios::binary | std::ios::trunc); 00134 00135 sf::Uint16 profiles_size = profiles.size(); 00136 00137 if (file.is_open()) 00138 { 00139 file.write((char*)(&profiles_size), 2); 00140 00141 sf::Uint8 name_length; 00142 sf::Uint8 head; 00143 sf::Uint8 is_human; 00144 for (unsigned int i = 0; i < profiles_size; ++i) 00145 { 00146 sf::Unicode::UTF32String name = profiles[i]->name; 00147 name_length = name.length(); 00148 head = profiles[i]->head_style; 00149 00150 file.write((char*)&name_length, 1); 00151 file.write((char*)&(name[0]), name_length * 4); 00152 file.write((char*)&head, 1); 00153 00154 is_human = (profiles[i]->is_human?1:0); 00155 file.write((char*)&is_human, 1); 00156 if (is_human) 00157 { 00158 file.write((char*)&profiles[i]->key_up, 4); 00159 file.write((char*)&profiles[i]->key_left, 4); 00160 file.write((char*)&profiles[i]->key_down, 4); 00161 file.write((char*)&profiles[i]->key_right, 4); 00162 } 00163 else 00164 { 00165 file.write((char*)&profiles[i]->skill, 4); 00166 } 00167 } 00168 } 00169 } 00170 00171 ProfileManager::~ProfileManager() 00172 { 00173 save(); 00174 00175 for (unsigned int i = 0; i < profiles.size(); ++i) 00176 { 00177 delete profiles[i]; 00178 } 00179 }