diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2009-10-18 15:24:07 -0400 |
|---|---|---|
| committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2009-10-18 15:24:07 -0400 |
| commit | c027f1b9cd6c9deb60931a7f9f75bb4ee130291b (patch) | |
| tree | 49426a9cf3ee24434141c903b01481110b2808b6 /hsnew.cpp | |
| parent | a157cd82a86390f1fcb1a2086f86af5187e85a69 (diff) | |
| download | mazeoflife-c027f1b9cd6c9deb60931a7f9f75bb4ee130291b.tar.gz mazeoflife-c027f1b9cd6c9deb60931a7f9f75bb4ee130291b.tar.bz2 mazeoflife-c027f1b9cd6c9deb60931a7f9f75bb4ee130291b.zip | |
Added local highscore list
You can now view and add to a local highscore list. A change to State was necessary due to how the user's name would be entered into the highscore list and thus all states have been modified. Refs #104
Diffstat (limited to 'hsnew.cpp')
| -rw-r--r-- | hsnew.cpp | 219 |
1 files changed, 219 insertions, 0 deletions
| diff --git a/hsnew.cpp b/hsnew.cpp new file mode 100644 index 0000000..3bec468 --- /dev/null +++ b/hsnew.cpp | |||
| @@ -0,0 +1,219 @@ | |||
| 1 | #include "includes.h" | ||
| 2 | |||
| 3 | NewHighscoreState::NewHighscoreState(int level) | ||
| 4 | { | ||
| 5 | this->level = level; | ||
| 6 | |||
| 7 | LOADIMAGE(options,hlo_passartm) | ||
| 8 | LOADIMAGE(pointer,pointer) | ||
| 9 | |||
| 10 | lhl = new NewHighscoreList(level); | ||
| 11 | newpos = lhl->getNewPos(); | ||
| 12 | list = lhl->render(); | ||
| 13 | |||
| 14 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 15 | SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "New Highscore!", fontColor); | ||
| 16 | SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; | ||
| 17 | SDL_BlitSurface(title, NULL, list, &tSpace); | ||
| 18 | |||
| 19 | this->enterName = true; | ||
| 20 | this->lp = 0; | ||
| 21 | this->hsname = (char*) calloc(25, sizeof(char)); | ||
| 22 | |||
| 23 | SDL_Surface* text = TTF_RenderText_Blended(loadFont(25), "Enter Your Name", fontColor); | ||
| 24 | SDL_Rect oSpace = {240-(text->w/2), 440, text->w, text->h}; | ||
| 25 | SDL_BlitSurface(text, NULL, list, &oSpace); | ||
| 26 | |||
| 27 | selection = 0; | ||
| 28 | |||
| 29 | int posw, posh; | ||
| 30 | char pos[3]; // 2 max characters in rank plus the colon at the end | ||
| 31 | sprintf(pos, "%d:", newpos); | ||
| 32 | char name[26]; // 25 max characters in username plus the space at the beginning | ||
| 33 | sprintf(name, " %s", hsname); | ||
| 34 | newName = TTF_RenderText_Blended(loadFont(25), name, fontColor); | ||
| 35 | TTF_SizeText(loadFont(40), pos, &posw, &posh); | ||
| 36 | rntSpace.x = posw; | ||
| 37 | rntSpace.y = newpos*40+((posh/2)-(newName->h/2)); | ||
| 38 | rntSpace.w = newName->w; | ||
| 39 | rntSpace.h = newName->h; | ||
| 40 | |||
| 41 | SDL_WM_SetCaption("Maze Of Life - New Highscore!", NULL); | ||
| 42 | SDL_EnableUNICODE(1); | ||
| 43 | } | ||
| 44 | |||
| 45 | void NewHighscoreState::input(SDL_keysym key) | ||
| 46 | { | ||
| 47 | if (enterName) | ||
| 48 | { | ||
| 49 | if ((key.sym == SDLK_BACKSPACE) && (lp > 0)) | ||
| 50 | { | ||
| 51 | hsname[--lp] = 0; | ||
| 52 | |||
| 53 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 54 | char name[26]; // 25 max characters in username plus the space at the beginning | ||
| 55 | sprintf(name, " %s", hsname); | ||
| 56 | newName = TTF_RenderText_Blended(loadFont(25), name, fontColor); | ||
| 57 | rntSpace.w = newName->w; | ||
| 58 | rntSpace.h = newName->h; | ||
| 59 | } else if ((key.sym == SDLK_RETURN) && (hsname[0] != 0)) | ||
| 60 | { | ||
| 61 | SDL_EnableUNICODE(0); | ||
| 62 | enterName = false; | ||
| 63 | |||
| 64 | lhl->addToList(hsname); | ||
| 65 | LocalHighscoreList* lhl2 = new LocalHighscoreList(); | ||
| 66 | list = lhl2->render(); | ||
| 67 | |||
| 68 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 69 | SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "New Highscore!", fontColor); | ||
| 70 | SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; | ||
| 71 | SDL_BlitSurface(title, NULL, list, &tSpace); | ||
| 72 | |||
| 73 | SDL_Rect oSpace = {0, 440, options->w, options->h}; | ||
| 74 | SDL_BlitSurface(options, NULL, list, &oSpace); | ||
| 75 | } else if (((key.unicode & 0xFF80) == 0) && (key.unicode >= 32 && key.unicode < 127) && (lp < 25)) | ||
| 76 | { | ||
| 77 | hsname[lp++] = key.unicode & 0x7f; | ||
| 78 | hsname[lp] = 0; | ||
| 79 | |||
| 80 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 81 | char name[26]; // 25 max characters in username plus the space at the beginning | ||
| 82 | sprintf(name, " %s", hsname); | ||
| 83 | newName = TTF_RenderText_Blended(loadFont(25), name, fontColor); | ||
| 84 | rntSpace.w = newName->w; | ||
| 85 | rntSpace.h = newName->h; | ||
| 86 | } | ||
| 87 | } else { | ||
| 88 | if ((key.sym == SDLK_LEFT) && (selection != 0)) | ||
| 89 | { | ||
| 90 | selection--; | ||
| 91 | } else if ((key.sym == SDLK_RIGHT) && (selection != 2)) | ||
| 92 | { | ||
| 93 | selection++; | ||
| 94 | } else if (key.sym == SDLK_RETURN) | ||
| 95 | { | ||
| 96 | switch (selection) | ||
| 97 | { | ||
| 98 | case 0: | ||
| 99 | changeState(new GameState()); | ||
| 100 | |||
| 101 | break; | ||
| 102 | case 1: // Submit score to Global highscore list | ||
| 103 | break; | ||
| 104 | case 2: | ||
| 105 | changeState(new TitleState()); | ||
| 106 | |||
| 107 | break; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | void NewHighscoreState::render(SDL_Surface* screen) | ||
| 114 | { | ||
| 115 | SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255)); | ||
| 116 | |||
| 117 | SDL_Rect eSpace = {0, newpos*40, 480, 40}; | ||
| 118 | SDL_FillRect(screen, &eSpace, SDL_MapRGB(screen->format, 255, 255, 0)); | ||
| 119 | |||
| 120 | SDL_BlitSurface(list, NULL, screen, NULL); | ||
| 121 | |||
| 122 | if (enterName) | ||
| 123 | { | ||
| 124 | SDL_BlitSurface(newName, NULL, screen, &rntSpace); | ||
| 125 | } else { | ||
| 126 | SDL_Rect pSpace; | ||
| 127 | pSpace.x = (selection==0?13:(selection==1?138:284)); | ||
| 128 | pSpace.y = 448; | ||
| 129 | pSpace.w = pointer->w; | ||
| 130 | pSpace.h = pointer->h; | ||
| 131 | |||
| 132 | SDL_BlitSurface(pointer, NULL, screen, &pSpace); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | NewHighscoreState::NewHighscoreList::NewHighscoreList(int level) | ||
| 137 | { | ||
| 138 | this->hslist = getLocalHighscores(); | ||
| 139 | |||
| 140 | if (this->hslist.empty()) | ||
| 141 | { | ||
| 142 | Highscore n = Highscore("", level); | ||
| 143 | n.setRank(1); | ||
| 144 | |||
| 145 | this->hslist.push_back(n); | ||
| 146 | this->newpos = n.getRank(); | ||
| 147 | } else { | ||
| 148 | std::vector<Highscore>::iterator it; | ||
| 149 | bool found = false; | ||
| 150 | int lastrank; | ||
| 151 | int i=0; | ||
| 152 | |||
| 153 | for (it=this->hslist.begin(); it<this->hslist.end(); it++) | ||
| 154 | { | ||
| 155 | Highscore h = *it; | ||
| 156 | lastrank = h.getRank(); | ||
| 157 | |||
| 158 | if (!found) | ||
| 159 | { | ||
| 160 | if (h.getLevel() < level) | ||
| 161 | { | ||
| 162 | Highscore n = Highscore("", level); | ||
| 163 | n.setRank(h.getRank()); | ||
| 164 | |||
| 165 | this->hslist.insert(it, n); | ||
| 166 | this->newpos = n.getRank(); | ||
| 167 | |||
| 168 | if (this->hslist.size() > 10) | ||
| 169 | { | ||
| 170 | this->hslist.pop_back(); | ||
| 171 | } | ||
| 172 | |||
| 173 | found = true; | ||
| 174 | } | ||
| 175 | } else { | ||
| 176 | //this->hslist[i].setRank(h.getRank()+1); | ||
| 177 | } | ||
| 178 | |||
| 179 | i++; | ||
| 180 | } | ||
| 181 | |||
| 182 | if (!found) | ||
| 183 | { | ||
| 184 | Highscore n = Highscore("", level); | ||
| 185 | n.setRank(lastrank+1); | ||
| 186 | |||
| 187 | this->hslist.push_back(n); | ||
| 188 | this->newpos = n.getRank(); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | int NewHighscoreState::NewHighscoreList::getNewPos() | ||
| 194 | { | ||
| 195 | return newpos; | ||
| 196 | } | ||
| 197 | |||
| 198 | void NewHighscoreState::NewHighscoreList::addToList(char* name) | ||
| 199 | { | ||
| 200 | FILE* hsfile = fopen(getDataFile(), "w"); | ||
| 201 | fprintf(hsfile, "%d ", this->hslist.size()); | ||
| 202 | |||
| 203 | std::vector<Highscore>::iterator it; | ||
| 204 | |||
| 205 | for (it=this->hslist.begin(); it<this->hslist.end(); it++) | ||
| 206 | { | ||
| 207 | Highscore h = *it; | ||
| 208 | |||
| 209 | if (h.getName() == "") | ||
| 210 | { | ||
| 211 | h = Highscore(name, h.getLevel()); | ||
| 212 | h.setRank(newpos); | ||
| 213 | } | ||
| 214 | |||
| 215 | fprintf(hsfile, "%d%s%d ", strlen(h.getName()), h.getName(), h.getLevel()); | ||
| 216 | } | ||
| 217 | |||
| 218 | fclose(hsfile); | ||
| 219 | } | ||
