diff options
| author | Star Rauchenberger <fefferburbia@gmail.com> | 2023-11-02 18:38:53 -0400 |
|---|---|---|
| committer | Star Rauchenberger <fefferburbia@gmail.com> | 2023-11-02 18:38:53 -0400 |
| commit | a59fcafb2e81f3cb40ff320b106030e8fed4bd66 (patch) | |
| tree | 7e7396a9422814365a5f903a53d7391d3e7b22fd /hs_state.cpp | |
| parent | 45d6e635c880a7fae8711fba366519dd314d9faf (diff) | |
| download | mazeoflife-master.tar.gz mazeoflife-master.tar.bz2 mazeoflife-master.zip | |
Diffstat (limited to 'hs_state.cpp')
| -rw-r--r-- | hs_state.cpp | 276 |
1 files changed, 276 insertions, 0 deletions
| diff --git a/hs_state.cpp b/hs_state.cpp new file mode 100644 index 0000000..f6aab86 --- /dev/null +++ b/hs_state.cpp | |||
| @@ -0,0 +1,276 @@ | |||
| 1 | #include "hs_state.h" | ||
| 2 | |||
| 3 | #include <sstream> | ||
| 4 | |||
| 5 | #include "gamestate.h" | ||
| 6 | #include "titlestate.h" | ||
| 7 | #include "util.h" | ||
| 8 | |||
| 9 | DisplayLocalHighscoreListState::DisplayLocalHighscoreListState(Game& game) { | ||
| 10 | pointer_ = loadImage(game.renderer.get(), "resources/pointer.bmp"); | ||
| 11 | |||
| 12 | std::unique_ptr<HighscoreList> lhl = HighscoreList::GetLocalHighscores(); | ||
| 13 | |||
| 14 | surface_ptr list_s = lhl->render(); | ||
| 15 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 16 | font_ptr font = loadFont(40); | ||
| 17 | surface_ptr title = surface_ptr( | ||
| 18 | TTF_RenderText_Blended(font.get(), "Highscore List", fontColor)); | ||
| 19 | SDL_Rect tSpace = {240 - (title->w / 2), 0, title->w, title->h}; | ||
| 20 | SDL_BlitSurface(title.get(), NULL, list_s.get(), &tSpace); | ||
| 21 | |||
| 22 | surface_ptr options_s = surface_ptr(SDL_LoadBMP("resources/hlo_rtm.bmp")); | ||
| 23 | SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; | ||
| 24 | SDL_BlitSurface(options_s.get(), NULL, list_s.get(), &oSpace); | ||
| 25 | |||
| 26 | list_ = texture_ptr( | ||
| 27 | SDL_CreateTextureFromSurface(game.renderer.get(), list_s.get())); | ||
| 28 | } | ||
| 29 | |||
| 30 | std::unique_ptr<State> DisplayLocalHighscoreListState::operator()(Game& game) { | ||
| 31 | SDL_Event e; | ||
| 32 | |||
| 33 | SDL_SetRenderDrawColor(game.renderer.get(), 255, 255, 255, 255); | ||
| 34 | SDL_RenderClear(game.renderer.get()); | ||
| 35 | SDL_RenderCopy(game.renderer.get(), list_.get(), NULL, NULL); | ||
| 36 | applyTexture(game.renderer.get(), pointer_.get(), 137, 449); | ||
| 37 | SDL_RenderPresent(game.renderer.get()); | ||
| 38 | |||
| 39 | while (SDL_PollEvent(&e)) { | ||
| 40 | if (e.type == SDL_QUIT) { | ||
| 41 | game.should_quit = true; | ||
| 42 | break; | ||
| 43 | } else if (e.type == SDL_KEYDOWN) { | ||
| 44 | if (e.key.keysym.sym == SDLK_RETURN) { | ||
| 45 | return std::make_unique<TitleState>(game); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | return nullptr; | ||
| 51 | } | ||
| 52 | |||
| 53 | DisplayAndReturnLocalHighscoreListState:: | ||
| 54 | DisplayAndReturnLocalHighscoreListState(Game& game) { | ||
| 55 | pointer_ = loadImage(game.renderer.get(), "resources/pointer.bmp"); | ||
| 56 | |||
| 57 | std::unique_ptr<HighscoreList> lhl = HighscoreList::GetLocalHighscores(); | ||
| 58 | surface_ptr list_s = lhl->render(); | ||
| 59 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 60 | font_ptr font = loadFont(40); | ||
| 61 | surface_ptr title = surface_ptr( | ||
| 62 | TTF_RenderText_Blended(font.get(), "Highscore List", fontColor)); | ||
| 63 | SDL_Rect tSpace = {240 - (title->w / 2), 0, title->w, title->h}; | ||
| 64 | SDL_BlitSurface(title.get(), NULL, list_s.get(), &tSpace); | ||
| 65 | |||
| 66 | surface_ptr options_s = surface_ptr(SDL_LoadBMP("resources/hlo_paartm.bmp")); | ||
| 67 | SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; | ||
| 68 | SDL_BlitSurface(options_s.get(), NULL, list_s.get(), &oSpace); | ||
| 69 | |||
| 70 | list_ = texture_ptr( | ||
| 71 | SDL_CreateTextureFromSurface(game.renderer.get(), list_s.get())); | ||
| 72 | } | ||
| 73 | |||
| 74 | std::unique_ptr<State> DisplayAndReturnLocalHighscoreListState::operator()( | ||
| 75 | Game& game) { | ||
| 76 | SDL_Event e; | ||
| 77 | |||
| 78 | SDL_SetRenderDrawColor(game.renderer.get(), 255, 255, 255, 255); | ||
| 79 | SDL_RenderClear(game.renderer.get()); | ||
| 80 | SDL_RenderCopy(game.renderer.get(), list_.get(), NULL, NULL); | ||
| 81 | applyTexture(game.renderer.get(), pointer_.get(), selection_ == 0 ? 52 : 225, | ||
| 82 | 447); | ||
| 83 | SDL_RenderPresent(game.renderer.get()); | ||
| 84 | |||
| 85 | while (SDL_PollEvent(&e)) { | ||
| 86 | if (e.type == SDL_QUIT) { | ||
| 87 | game.should_quit = true; | ||
| 88 | break; | ||
| 89 | } else if (e.type == SDL_KEYDOWN) { | ||
| 90 | if ((e.key.keysym.sym == SDLK_LEFT) && (selection_ != 0)) { | ||
| 91 | selection_--; | ||
| 92 | } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection_ != 1)) { | ||
| 93 | selection_++; | ||
| 94 | } else if (e.key.keysym.sym == SDLK_RETURN) { | ||
| 95 | switch (selection_) { | ||
| 96 | case 0: | ||
| 97 | return std::make_unique<GameState>(); | ||
| 98 | case 1: | ||
| 99 | return std::make_unique<TitleState>(game); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | return nullptr; | ||
| 106 | } | ||
| 107 | |||
| 108 | EnterHighscoreState::EnterHighscoreState(Game& game, int level) | ||
| 109 | : level_(level) { | ||
| 110 | pointer_ = loadImage(game.renderer.get(), "resources/pointer.bmp"); | ||
| 111 | |||
| 112 | // Render highscore list | ||
| 113 | std::unique_ptr<HighscoreList> lhl = HighscoreList::GetLocalHighscores(); | ||
| 114 | newpos_ = lhl->addHighscore(Highscore("", level_)); | ||
| 115 | |||
| 116 | surface_ptr list_s = lhl->render(); | ||
| 117 | |||
| 118 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 119 | font_ptr bigfont = loadFont(40); | ||
| 120 | surface_ptr title = surface_ptr( | ||
| 121 | TTF_RenderText_Blended(bigfont.get(), "New Highscore!", fontColor)); | ||
| 122 | SDL_Rect tSpace = {240 - (title->w / 2), 0, title->w, title->h}; | ||
| 123 | SDL_BlitSurface(title.get(), NULL, list_s.get(), &tSpace); | ||
| 124 | |||
| 125 | font_ptr font = loadFont(25); | ||
| 126 | surface_ptr text = surface_ptr( | ||
| 127 | TTF_RenderText_Blended(font.get(), "Enter Your Name", fontColor)); | ||
| 128 | SDL_Rect oSpace = {240 - (text->w / 2), 440, text->w, text->h}; | ||
| 129 | SDL_BlitSurface(text.get(), NULL, list_s.get(), &oSpace); | ||
| 130 | |||
| 131 | list_ = texture_ptr( | ||
| 132 | SDL_CreateTextureFromSurface(game.renderer.get(), list_s.get())); | ||
| 133 | |||
| 134 | int posw, posh; | ||
| 135 | std::ostringstream posstr; | ||
| 136 | posstr << newpos_ << ":"; | ||
| 137 | std::string pos = posstr.str(); | ||
| 138 | |||
| 139 | surface_ptr newName_s = | ||
| 140 | surface_ptr(TTF_RenderText_Blended(font.get(), " ", fontColor)); | ||
| 141 | TTF_SizeText(bigfont.get(), pos.c_str(), &posw, &posh); | ||
| 142 | rntSpace_.x = posw; | ||
| 143 | rntSpace_.y = newpos_ * 40 + ((posh / 2) - (newName_s->h / 2)); | ||
| 144 | rntSpace_.w = newName_s->w; | ||
| 145 | rntSpace_.h = newName_s->h; | ||
| 146 | newName_ = texture_ptr( | ||
| 147 | SDL_CreateTextureFromSurface(game.renderer.get(), newName_s.get())); | ||
| 148 | } | ||
| 149 | |||
| 150 | std::unique_ptr<State> EnterHighscoreState::operator()(Game& game) { | ||
| 151 | SDL_Event e; | ||
| 152 | |||
| 153 | SDL_SetRenderDrawColor(game.renderer.get(), 255, 255, 255, 255); | ||
| 154 | SDL_RenderClear(game.renderer.get()); | ||
| 155 | |||
| 156 | SDL_Rect eSpace = {0, newpos_ * 40, 480, 40}; | ||
| 157 | SDL_SetRenderDrawColor(game.renderer.get(), 255, 255, 0, 255); | ||
| 158 | SDL_RenderFillRect(game.renderer.get(), &eSpace); | ||
| 159 | |||
| 160 | SDL_RenderCopy(game.renderer.get(), list_.get(), NULL, NULL); | ||
| 161 | SDL_RenderCopy(game.renderer.get(), newName_.get(), NULL, &rntSpace_); | ||
| 162 | |||
| 163 | SDL_RenderPresent(game.renderer.get()); | ||
| 164 | |||
| 165 | while (SDL_PollEvent(&e)) { | ||
| 166 | if (e.type == SDL_QUIT) { | ||
| 167 | game.should_quit = true; | ||
| 168 | break; | ||
| 169 | } else if (e.type == SDL_KEYDOWN) { | ||
| 170 | if ((e.key.keysym.sym == SDLK_BACKSPACE) && (lp_ > 0)) { | ||
| 171 | --lp_; | ||
| 172 | hsname_.pop_back(); | ||
| 173 | |||
| 174 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 175 | std::ostringstream name; | ||
| 176 | name << " " << hsname_; | ||
| 177 | |||
| 178 | font_ptr font = loadFont(25); | ||
| 179 | surface_ptr newName_s = surface_ptr( | ||
| 180 | TTF_RenderText_Blended(font.get(), name.str().c_str(), fontColor)); | ||
| 181 | rntSpace_.w = newName_s->w; | ||
| 182 | rntSpace_.h = newName_s->h; | ||
| 183 | newName_ = texture_ptr( | ||
| 184 | SDL_CreateTextureFromSurface(game.renderer.get(), newName_s.get())); | ||
| 185 | } else if ((e.key.keysym.sym == SDLK_RETURN) && !hsname_.empty()) { | ||
| 186 | std::unique_ptr<HighscoreList> lhl = | ||
| 187 | HighscoreList::GetLocalHighscores(); | ||
| 188 | Highscore h2(hsname_, level_); | ||
| 189 | lhl->addHighscore(h2); | ||
| 190 | lhl->writeToFile(); | ||
| 191 | |||
| 192 | return std::make_unique<NewHighscoreState>(game, h2); | ||
| 193 | } | ||
| 194 | } else if (e.type == SDL_TEXTINPUT) { | ||
| 195 | if (((*e.text.text & 0xFF80) == 0) && | ||
| 196 | (*e.text.text >= 32 && *e.text.text < 127) && (lp_ < 25)) { | ||
| 197 | lp_++; | ||
| 198 | hsname_.push_back(*e.text.text & 0x7f); | ||
| 199 | |||
| 200 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 201 | std::ostringstream name; | ||
| 202 | name << " " << hsname_; | ||
| 203 | |||
| 204 | font_ptr font = loadFont(25); | ||
| 205 | surface_ptr newName_s = surface_ptr( | ||
| 206 | TTF_RenderText_Blended(font.get(), name.str().c_str(), fontColor)); | ||
| 207 | rntSpace_.w = newName_s->w; | ||
| 208 | rntSpace_.h = newName_s->h; | ||
| 209 | newName_ = texture_ptr( | ||
| 210 | SDL_CreateTextureFromSurface(game.renderer.get(), newName_s.get())); | ||
| 211 | } | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | return nullptr; | ||
| 216 | } | ||
| 217 | |||
| 218 | NewHighscoreState::NewHighscoreState(Game& game, Highscore h) : h_(h) { | ||
| 219 | pointer_ = loadImage(game.renderer.get(), "resources/pointer.bmp"); | ||
| 220 | |||
| 221 | // Render highscore list | ||
| 222 | std::unique_ptr<HighscoreList> lhl = HighscoreList::GetLocalHighscores(); | ||
| 223 | surface_ptr list_s = lhl->render(); | ||
| 224 | |||
| 225 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 226 | font_ptr bigfont = loadFont(40); | ||
| 227 | surface_ptr title = surface_ptr( | ||
| 228 | TTF_RenderText_Blended(bigfont.get(), "New Highscore!", fontColor)); | ||
| 229 | SDL_Rect tSpace = {240 - (title->w / 2), 0, title->w, title->h}; | ||
| 230 | SDL_BlitSurface(title.get(), NULL, list_s.get(), &tSpace); | ||
| 231 | |||
| 232 | surface_ptr options_s = surface_ptr(SDL_LoadBMP("resources/hlo_paartm.bmp")); | ||
| 233 | SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; | ||
| 234 | SDL_BlitSurface(options_s.get(), NULL, list_s.get(), &oSpace); | ||
| 235 | |||
| 236 | list_ = texture_ptr( | ||
| 237 | SDL_CreateTextureFromSurface(game.renderer.get(), list_s.get())); | ||
| 238 | } | ||
| 239 | |||
| 240 | std::unique_ptr<State> NewHighscoreState::operator()(Game& game) { | ||
| 241 | SDL_Event e; | ||
| 242 | |||
| 243 | SDL_SetRenderDrawColor(game.renderer.get(), 255, 255, 255, 255); | ||
| 244 | SDL_RenderClear(game.renderer.get()); | ||
| 245 | |||
| 246 | SDL_Rect eSpace = {0, h_.getRank() * 40, 480, 40}; | ||
| 247 | SDL_SetRenderDrawColor(game.renderer.get(), 255, 255, 0, 255); | ||
| 248 | SDL_RenderFillRect(game.renderer.get(), &eSpace); | ||
| 249 | |||
| 250 | SDL_RenderCopy(game.renderer.get(), list_.get(), NULL, NULL); | ||
| 251 | applyTexture(game.renderer.get(), pointer_.get(), selection_ == 0 ? 52 : 225, | ||
| 252 | 447); | ||
| 253 | SDL_RenderPresent(game.renderer.get()); | ||
| 254 | |||
| 255 | while (SDL_PollEvent(&e)) { | ||
| 256 | if (e.type == SDL_QUIT) { | ||
| 257 | game.should_quit = true; | ||
| 258 | break; | ||
| 259 | } else if (e.type == SDL_KEYDOWN) { | ||
| 260 | if ((e.key.keysym.sym == SDLK_LEFT) && (selection_ != 0)) { | ||
| 261 | selection_--; | ||
| 262 | } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection_ != 1)) { | ||
| 263 | selection_++; | ||
| 264 | } else if (e.key.keysym.sym == SDLK_RETURN) { | ||
| 265 | switch (selection_) { | ||
| 266 | case 0: | ||
| 267 | return std::make_unique<GameState>(); | ||
| 268 | case 1: | ||
| 269 | return std::make_unique<TitleState>(game); | ||
| 270 | } | ||
| 271 | } | ||
| 272 | } | ||
| 273 | } | ||
| 274 | |||
| 275 | return nullptr; | ||
| 276 | } | ||
