Kukatz 3D  0.1
Török Attila szakdolgozata
projects/Kukatz 3D/src/scoremanager.cpp
00001 /*
00002  * scoremanager.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 "scoremanager.hpp"
00026 
00027 #include <fstream>
00028 
00029 #include <SFML/Config.hpp>
00030 
00031 ScoreManager::ScoreManager()
00032 {
00033         std::ifstream file("highscores", 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::Uint8 num_scores;
00044                         file.read((char*)(&num_scores), 1);
00045                         
00046                         scores.resize(num_scores);
00047                         
00048                         sf::Uint32 buffer[256];
00049                         sf::Uint8 name_length;
00050                         sf::Uint32 points;
00051                         for (unsigned int i = 0; i < num_scores; ++i)
00052                         {
00053                                 file.read((char*)&name_length, 1);
00054                                 file.read((char*)buffer, name_length * 4);
00055                                 buffer[name_length] = 0;
00056                                 
00057                                 scores[i].first = buffer;
00058                                 
00059                                 file.read((char*)&points, 4);
00060                                 scores[i].second = points;
00061                         }
00062                 }
00063         }
00064 }
00065 
00066 bool ScoreManager::submit(const sf::Unicode::Text& n, sf::Uint32 s)
00067 {
00068         bool ret = false;
00069         
00070         int before_this = scores.size();
00071         
00072         for (unsigned int i = 0; i < scores.size(); ++i)
00073         {
00074                 if (scores[i].second < s)
00075                 {
00076                         before_this = i;
00077                         ret = true;
00078                         break;
00079                 }
00080         }
00081         
00082         scores.push_back(std::make_pair("", 0));
00083         
00084         for (int i = scores.size() - 2; i >= before_this; --i)
00085         {
00086                 scores[i + 1].first = scores[i].first;
00087                 scores[i + 1].second = scores[i].second;
00088         }
00089         
00090         scores[before_this].first = n;
00091         scores[before_this].second = s;
00092         
00093         if (scores.size() > 10)
00094         {
00095                 scores.resize(10);
00096         }
00097         
00098         save();
00099         return ret;
00100 }
00101 
00102 const std::vector< std::pair< sf::Unicode::Text, sf::Uint32 > >& ScoreManager::get_scores() const // RESPECT Z FOR THE CONST!
00103 {
00104         return scores;
00105 }
00106 
00107 void ScoreManager::save()
00108 {
00109         std::ofstream file("highscores", std::ios::binary | std::ios::trunc);
00110         
00111         sf::Uint8 scores_size = scores.size();
00112         
00113         if (file.is_open())
00114         {
00115                 file.write((char*)(&scores_size), 1);
00116                 
00117                 sf::Uint8 name_length;
00118                 for (unsigned int i = 0; i < scores_size; ++i)
00119                 {
00120                         sf::Unicode::UTF32String name = scores[i].first;
00121                         name_length = name.length();
00122                         file.write((char*)&name_length, 1);
00123                         file.write((char*)&(name[0]), name_length * 4);
00124                         
00125                         file.write((char*)&scores[i].second, 4);
00126                 }
00127         }
00128 }
00129 
00130 ScoreManager::~ScoreManager()
00131 {
00132         save();
00133 }
 Összes Osztályok