Hey Guys,
i tried to implement a highscore
![Cheesy :D](https://www.sfml-dev.org/forums/Smileys/default/cheesy.gif)
#game crashes
Made a new gameState, added it to the main menu bla bla bla.
The constructor should load data from the highscore file. Which looks like this:
Points 0,Enemy Missed 0,Enemy Killed 0,Monkey Killed 0,Shots Fired 0,Shots Got 0
Points 0,Enemy Missed 0,Enemy Killed 0,Monkey Killed 0,Shots Fired 0,Shots Got 0
Points 370,Enemy Missed 18,Enemy Killed 28,Monkey Killed 1,Shots Fired 129,Shots Got 78
and my class looks like this:
public:
HighscoreList();
~HighscoreList();
void HandleEvents(Game &game);
void Update(Game &game);
void Render(Game &game);
private:
static bool sortScoreDesc(const sf::Vector3<int>& a, const sf::Vector3<int>& b);
//mystuff
HighscoreManager _mng;
IOHighscore _ioHighscore;
Background _bg;
Text _score, _back, _highscore;
//streams
std::vector<sf::Vector3i> _numInput; ///< trägt die 3 wichtigen zahlen
std::vector<std::string> _highScoreDataV;
std::stringstream _scoreStream;
};
#endif //__HIGHSCORELIST_H__
//HighscoreList.cpp
#include "HighscoreList.h"
#include <fstream>
#include <iostream>
#include <algorithm>
HighscoreList::HighscoreList(){
//set the background
_bg.setFilePath("graphics//core//menu.png");
//show something
_back.setStringAndSize("BACK", 50);
_back.setPosition(325, 500);
_highscore.setStringAndSize("HIGHSCORE", 50);
_highscore.setPosition(275, 10);
//init highscorelist+++++++++++++++++++++++++++++++++
//first get a vector that has the exact size of lines as the file
std::fstream file;
file.open("highscore.txt");
if (file.fail()){
perror("could not load highscore.txt file\n");
}
//create some stuff for temp save
std::string tmp;
int tmpNumPoints, tmpNumFired, tmpNumGot, tmpint;
//now extract the integer we need (points, shots fired, shots got)
while (!file.eof()){
std::getline(file, tmp, '\n');
//IN-(1)STRING--POINTS--------(2)--------------(3)---------------(4)--------------(5)----Shots fired----(6)----Shots got
file >> tmp >> tmpNumPoints >> tmp >> tmpint >> tmp >> tmpint >> tmp >> tmpint >> tmp >> tmpNumFired >> tmp >> tmpNumGot;
//and store it into the integer
//------------------------X--------------Y----------Z-------
_numInput.emplace_back(tmpNumPoints, tmpNumFired, tmpNumGot);
}
file.close();
//convert the values of Vector3i to what we need (Points, Accuracy, Score)
for (unsigned int i = 0; i < _numInput.size(); i++){
int tmpY, tmpZ;
tmpY = _numInput[i].z * 100 / _numInput[i].z; //get Accuracy
tmpZ = _numInput[i].x / tmpY; //get Score (Points / Accuracy)
//set new values
_numInput[i].y = tmpY;
_numInput[i].z = tmpZ;
}
//sort the Vec for Score (Z) descending
std::stable_sort(_numInput.begin(), _numInput.end(), sortScoreDesc);
//std::sort(_numInput.begin(),_numInput.end());
//erase all entries that are not in the top ten
for (auto it = _numInput.begin() + 10; it != _numInput.end();it++){
it = _numInput.erase(it);
}
//now we have a vector of integer, sorted by score which holds the TOP 10 plays
//we need to print it out now
for (unsigned int i = 0; i < _numInput.size(); i++){
std::cout << "Points: " << _numInput[i].x << " Accuracy: " << _numInput[i].y << " Score: " << _numInput[i].z << std::endl;
}
_score.setStringAndSize("", 20);
_score.setPosition(150, 195);
}
HighscoreList::~HighscoreList(){
_numInput.clear();
}
void HighscoreList::HandleEvents(Game &game){
sf::Event pEvent;
while (game.window.pollEvent(pEvent)){
//close
if (pEvent.type == sf::Event::Closed)
game.setRunning(false);
//menu
if (pEvent.type == sf::Event::KeyPressed){
if (pEvent.key.code == sf::Keyboard::Return)
game.ChangeState(Game::gameStates::MAINMENU);
}
}
}
void HighscoreList::Update(Game &game){
_mng = game.highscore;
//Update stuff (there is the convertion from int to stringstream)
//_score.Update(_scoreStream, _mng.getPoints());
}
void HighscoreList::Render(Game &game){
_bg.Render(game.window);
//Render stuff
_score.Render(game.window);
_back.Render(game.window);
_highscore.Render(game.window);
}
//if the first integer is greater than the second, this returns true
bool HighscoreList::sortScoreDesc(const sf::Vector3<int>& a, const sf::Vector3<int>& b){
return (a.z > b.z);
}
If i enter the highscore overview in the game, it simply does nothing more.
only thing i could get was this:
Ausnahme (erste Chance) bei 0x77BA604C (ntdll.dll) in Pew ReWork.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0xFEEEFEF6
Ausnahmefehler bei 0x77BA604C (ntdll.dll) in Pew ReWork.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0xFEEEFEF6
Ausnahme (erste Chance) bei 0x77BA604C (ntdll.dll) in Pew ReWork.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0xFEEEFEF6
Ausnahmefehler bei 0x77BA604C (ntdll.dll) in Pew ReWork.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0xFEEEFEF6
Ausnahme (erste Chance) bei 0x77BA604C (ntdll.dll) in Pew ReWork.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0xFEEEFEF6
Ausnahmefehler bei 0x77BA604C (ntdll.dll) in Pew ReWork.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0xFEEEFEF6
Das Programm "[11728] Pew ReWork.exe" wurde mit Code -1073741510 (0xc000013a) beendet.
I will keep you updated guys
![Cheesy :D](https://www.sfml-dev.org/forums/Smileys/default/cheesy.gif)