summary refs log tree commit diff stats
path: root/hslist.cpp
blob: 9ae2aa4fc9e9ad0d5817b50eca4fb7b34d74f343 (plain) (blame)
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
#include "hslist.h"

#include <SDL_net.h>
#include <SDL_ttf.h>

#include <algorithm>
#include <fstream>
#include <memory>
#include <sstream>

#include "gamestate.h"
#include "titlestate.h"
#include "util.h"

HighscoreList::HighscoreList(std::vector<Highscore> hslist) : hslist_(hslist) {
  resetRanks();
}

void HighscoreList::resetRanks() {
  int i = 1;
  for (Highscore& hs : hslist_) {
    hs.setRank(i++);
  }
}

surface_ptr HighscoreList::render() {
  surface_ptr tmp =
      surface_ptr(SDL_CreateRGBSurface(0, 480, 480, 32, 0, 0, 0, 0));
  Uint32 bgColor = SDL_MapRGB(tmp->format, 255, 255, 255);
  SDL_FillRect(tmp.get(), NULL, bgColor);
  SDL_SetColorKey(tmp.get(), SDL_TRUE, bgColor);
  font_ptr posFont = loadFont(40);
  font_ptr dataFont = loadFont(25);
  SDL_Color fontColor = {0, 0, 0, 0};

  for (const Highscore& h : hslist_) {
    int posw, posh;

    std::ostringstream posstr;
    posstr << h.getRank() << ":";

    std::string pos = posstr.str();
    TTF_SizeText(posFont.get(), pos.c_str(), &posw, &posh);
    SDL_Rect posSpace = {0, h.getRank() * 40, posw, posh};
    SDL_BlitSurface(
        TTF_RenderText_Blended(posFont.get(), pos.c_str(), fontColor), NULL,
        tmp.get(), &posSpace);

    std::ostringstream namestr;
    namestr << " " << h.getName();

    int namew, nameh;
    std::string name = namestr.str();
    TTF_SizeText(dataFont.get(), name.c_str(), &namew, &nameh);
    SDL_Rect nameSpace = {posw, h.getRank() * 40 + ((posh / 2) - (nameh / 2)),
                          namew, nameh};
    SDL_BlitSurface(
        TTF_RenderText_Blended(dataFont.get(), name.c_str(), fontColor), NULL,
        tmp.get(), &nameSpace);

    int lvlw, lvlh;
    std::string lvl = std::to_string(h.getLevel());
    TTF_SizeText(dataFont.get(), lvl.c_str(), &lvlw, &lvlh);
    SDL_Rect lvlSpace = {
        480 - lvlw, h.getRank() * 40 + ((posh / 2) - (nameh / 2)), lvlw, lvlh};
    SDL_BlitSurface(
        TTF_RenderText_Blended(dataFont.get(), lvl.c_str(), fontColor), NULL,
        tmp.get(), &lvlSpace);
  }

  return tmp;
}

std::unique_ptr<HighscoreList> HighscoreList::GetLocalHighscores() {
  std::vector<Highscore> hslist;

  std::ifstream hsstream(getDataFile());
  if (hsstream) {
    int num_scores = 0;
    hsstream >> num_scores;

    for (int i = 0; i < num_scores; i++) {
      std::string name;
      int score;

      hsstream >> name;
      hsstream >> score;

      hslist.emplace_back(name, score);
    }
  }

  return std::unique_ptr<HighscoreList>(new HighscoreList(hslist));
}

int HighscoreList::addHighscore(Highscore h) {
  int i = 1;

  for (const Highscore& cur_h : hslist_) {
    if (h.getLevel() > cur_h.getLevel()) {
      break;
    }
    i++;
  }

  hslist_.push_back(h);
  std::sort(hslist_.begin(), hslist_.end(),
            [](const Highscore& lhs, const Highscore& rhs) {
              return lhs.getLevel() > rhs.getLevel();
            });
  resetRanks();

  if (hslist_.size() > 10) {
    hslist_.resize(10);
  }

  return i;
}

void HighscoreList::writeToFile() {
  std::ofstream hsfile(getDataFile());
  hsfile << hslist_.size() << std::endl;

  for (const Highscore& h : hslist_) {
    hsfile << h.getName() << std::endl << h.getLevel() << std::endl;
  }
}