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 | |
| 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
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | chlstate.cpp | 12 | ||||
| -rw-r--r-- | chlstate.h | 2 | ||||
| -rw-r--r-- | gamestate.cpp | 47 | ||||
| -rw-r--r-- | gamestate.h | 2 | ||||
| -rw-r--r-- | highscore.cpp | 27 | ||||
| -rw-r--r-- | highscore.h | 18 | ||||
| -rw-r--r-- | hslist.cpp | 85 | ||||
| -rw-r--r-- | hslist.h | 21 | ||||
| -rw-r--r-- | hslocal.cpp | 84 | ||||
| -rw-r--r-- | hslocal.h | 18 | ||||
| -rw-r--r-- | hsnew.cpp | 219 | ||||
| -rw-r--r-- | hsnew.h | 35 | ||||
| -rw-r--r-- | htpstate.cpp | 8 | ||||
| -rw-r--r-- | htpstate.h | 2 | ||||
| -rw-r--r-- | includes.h | 7 | ||||
| -rw-r--r-- | mazeoflife.cpp | 37 | ||||
| -rw-r--r-- | mazeoflife.h | 2 | ||||
| -rw-r--r-- | resources.h | 4 | ||||
| -rw-r--r-- | resources/hlo_paartm.bmp | bin | 0 -> 76938 bytes | |||
| -rw-r--r-- | resources/hlo_passartm.bmp | bin | 0 -> 76938 bytes | |||
| -rw-r--r-- | resources/hlo_rtm.bmp | bin | 0 -> 76938 bytes | |||
| -rw-r--r-- | resources/mono.ttf | bin | 0 -> 259628 bytes | |||
| -rw-r--r-- | state.h | 2 | ||||
| -rw-r--r-- | titlestate.cpp | 8 | ||||
| -rw-r--r-- | titlestate.h | 2 |
26 files changed, 621 insertions, 25 deletions
| diff --git a/Makefile b/Makefile index 9bad9b2..b4a9b9b 100644 --- a/Makefile +++ b/Makefile | |||
| @@ -12,9 +12,9 @@ WINSRC = $(addsuffix win,$(SOURCES)) | |||
| 12 | RES = $(wildcard resources/*) | 12 | RES = $(wildcard resources/*) |
| 13 | CRES = $(patsubst resources/%,build/%,$(addsuffix .o,$(RES))) | 13 | CRES = $(patsubst resources/%,build/%,$(addsuffix .o,$(RES))) |
| 14 | LINCCFL = `sdl-config --cflags` | 14 | LINCCFL = `sdl-config --cflags` |
| 15 | LINLDFL = `sdl-config --libs` | 15 | LINLDFL = `sdl-config --libs` -lSDL_ttf |
| 16 | WINCCFL = `/opt/SDL-1.2.9/bin/i386-mingw32msvc-sdl-config --cflags` -DWINDOWS | 16 | WINCCFL = `/opt/SDL-1.2.9/bin/i386-mingw32msvc-sdl-config --cflags` -DWINDOWS |
| 17 | WINLDFL = `/opt/SDL-1.2.9/bin/i386-mingw32msvc-sdl-config --libs` | 17 | WINLDFL = `/opt/SDL-1.2.9/bin/i386-mingw32msvc-sdl-config --libs` -lSDL_ttf |
| 18 | 18 | ||
| 19 | all: init $(LTARGET) $(WTARGET) | 19 | all: init $(LTARGET) $(WTARGET) |
| 20 | linux: init $(LTARGET) | 20 | linux: init $(LTARGET) |
| diff --git a/chlstate.cpp b/chlstate.cpp index 24dd674..3f3000d 100644 --- a/chlstate.cpp +++ b/chlstate.cpp | |||
| @@ -10,19 +10,21 @@ ChooseHighscoreListState::ChooseHighscoreListState() | |||
| 10 | SDL_WM_SetCaption("Maze Of Life - Choose Highscore List", NULL); | 10 | SDL_WM_SetCaption("Maze Of Life - Choose Highscore List", NULL); |
| 11 | } | 11 | } |
| 12 | 12 | ||
| 13 | void ChooseHighscoreListState::input(SDLKey key) | 13 | void ChooseHighscoreListState::input(SDL_keysym key) |
| 14 | { | 14 | { |
| 15 | if ((key == SDLK_UP) && (selection != 0)) | 15 | if ((key.sym == SDLK_UP) && (selection != 0)) |
| 16 | { | 16 | { |
| 17 | selection--; | 17 | selection--; |
| 18 | } else if ((key == SDLK_DOWN) && (selection != 2)) | 18 | } else if ((key.sym == SDLK_DOWN) && (selection != 2)) |
| 19 | { | 19 | { |
| 20 | selection++; | 20 | selection++; |
| 21 | } else if (key == SDLK_RETURN) | 21 | } else if (key.sym == SDLK_RETURN) |
| 22 | { | 22 | { |
| 23 | switch (selection) | 23 | switch (selection) |
| 24 | { | 24 | { |
| 25 | case 0: // Go to local highscore list | 25 | case 0: |
| 26 | changeState(new LocalHighscoreListState(false)); | ||
| 27 | |||
| 26 | break; | 28 | break; |
| 27 | case 1: // Go to global highscore list | 29 | case 1: // Go to global highscore list |
| 28 | break; | 30 | break; |
| diff --git a/chlstate.h b/chlstate.h index a8905e2..15f1d05 100644 --- a/chlstate.h +++ b/chlstate.h | |||
| @@ -4,7 +4,7 @@ | |||
| 4 | class ChooseHighscoreListState : public State { | 4 | class ChooseHighscoreListState : public State { |
| 5 | public: | 5 | public: |
| 6 | ChooseHighscoreListState(); | 6 | ChooseHighscoreListState(); |
| 7 | void input(SDLKey key); | 7 | void input(SDL_keysym key); |
| 8 | void render(SDL_Surface* screen); | 8 | void render(SDL_Surface* screen); |
| 9 | 9 | ||
| 10 | private: | 10 | private: |
| diff --git a/gamestate.cpp b/gamestate.cpp index c312bfe..9a50b32 100644 --- a/gamestate.cpp +++ b/gamestate.cpp | |||
| @@ -16,11 +16,11 @@ GameState::GameState() | |||
| 16 | SDL_WM_SetCaption("Maze Of Life - Level 1", NULL); | 16 | SDL_WM_SetCaption("Maze Of Life - Level 1", NULL); |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | void GameState::input(SDLKey key) | 19 | void GameState::input(SDL_keysym key) |
| 20 | { | 20 | { |
| 21 | if (info.doneMaking) | 21 | if (info.doneMaking) |
| 22 | { | 22 | { |
| 23 | switch (key) | 23 | switch (key.sym) |
| 24 | { | 24 | { |
| 25 | case SDLK_LEFT: | 25 | case SDLK_LEFT: |
| 26 | move(info.playerx-1, info.playery); | 26 | move(info.playerx-1, info.playery); |
| @@ -39,7 +39,48 @@ void GameState::input(SDLKey key) | |||
| 39 | 39 | ||
| 40 | break; | 40 | break; |
| 41 | case SDLK_ESCAPE: | 41 | case SDLK_ESCAPE: |
| 42 | changeState(new TitleState()); | 42 | std::ifstream exists(getDataFile()); |
| 43 | if (exists) | ||
| 44 | { | ||
| 45 | FILE* hslist = fopen(getDataFile(), "r"); | ||
| 46 | int scores; | ||
| 47 | Highscore* h; | ||
| 48 | |||
| 49 | fscanf(hslist, "%d%*c", &scores); | ||
| 50 | |||
| 51 | if (scores < 10) | ||
| 52 | { | ||
| 53 | fclose(hslist); | ||
| 54 | |||
| 55 | changeState(new NewHighscoreState(info.level.getLevel())); | ||
| 56 | } else { | ||
| 57 | for (int i=0; i<scores; i++) | ||
| 58 | { | ||
| 59 | int namelen; | ||
| 60 | char namelens[4]; | ||
| 61 | char* name = (char*) calloc(25, sizeof(char)); | ||
| 62 | int score; | ||
| 63 | |||
| 64 | fscanf(hslist, "%d", &namelen); | ||
| 65 | sprintf(namelens, "%%%dc", namelen); | ||
| 66 | fscanf(hslist, namelens, name); | ||
| 67 | fscanf(hslist, "%d%*c", &score); | ||
| 68 | |||
| 69 | h = new Highscore(name, score); | ||
| 70 | } | ||
| 71 | |||
| 72 | fclose(hslist); | ||
| 73 | |||
| 74 | if (h->getLevel() < info.level.getLevel()) | ||
| 75 | { | ||
| 76 | changeState(new NewHighscoreState(info.level.getLevel())); | ||
| 77 | } else { | ||
| 78 | changeState(new LocalHighscoreListState(true)); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } else { | ||
| 82 | changeState(new NewHighscoreState(info.level.getLevel())); | ||
| 83 | } | ||
| 43 | 84 | ||
| 44 | break; | 85 | break; |
| 45 | } | 86 | } |
| diff --git a/gamestate.h b/gamestate.h index 7d9d798..d1b90ad 100644 --- a/gamestate.h +++ b/gamestate.h | |||
| @@ -4,7 +4,7 @@ | |||
| 4 | class GameState : public State { | 4 | class GameState : public State { |
| 5 | public: | 5 | public: |
| 6 | GameState(); | 6 | GameState(); |
| 7 | void input(SDLKey key); | 7 | void input(SDL_keysym key); |
| 8 | void tick(); | 8 | void tick(); |
| 9 | void render(SDL_Surface* screen); | 9 | void render(SDL_Surface* screen); |
| 10 | 10 | ||
| diff --git a/highscore.cpp b/highscore.cpp new file mode 100644 index 0000000..6811fa8 --- /dev/null +++ b/highscore.cpp | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | #include "includes.h" | ||
| 2 | |||
| 3 | Highscore::Highscore(char* name, int level) | ||
| 4 | { | ||
| 5 | this->name = name; | ||
| 6 | this->level = level; | ||
| 7 | } | ||
| 8 | |||
| 9 | char* Highscore::getName() | ||
| 10 | { | ||
| 11 | return name; | ||
| 12 | } | ||
| 13 | |||
| 14 | int Highscore::getLevel() | ||
| 15 | { | ||
| 16 | return level; | ||
| 17 | } | ||
| 18 | |||
| 19 | void Highscore::setRank(int rank) | ||
| 20 | { | ||
| 21 | this->rank = rank; | ||
| 22 | } | ||
| 23 | |||
| 24 | int Highscore::getRank() | ||
| 25 | { | ||
| 26 | return rank; | ||
| 27 | } | ||
| diff --git a/highscore.h b/highscore.h new file mode 100644 index 0000000..721b226 --- /dev/null +++ b/highscore.h | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | #ifndef HIGHSCORE_H | ||
| 2 | #define HIGHSCORE_H | ||
| 3 | |||
| 4 | class Highscore { | ||
| 5 | public: | ||
| 6 | Highscore(char* name, int level); | ||
| 7 | char* getName(); | ||
| 8 | int getLevel(); | ||
| 9 | void setRank(int rank); | ||
| 10 | int getRank(); | ||
| 11 | |||
| 12 | private: | ||
| 13 | char* name; | ||
| 14 | int level; | ||
| 15 | int rank; | ||
| 16 | }; | ||
| 17 | |||
| 18 | #endif | ||
| diff --git a/hslist.cpp b/hslist.cpp new file mode 100644 index 0000000..d2901b4 --- /dev/null +++ b/hslist.cpp | |||
| @@ -0,0 +1,85 @@ | |||
| 1 | #include "includes.h" | ||
| 2 | |||
| 3 | SDL_Surface* HighscoreList::render() | ||
| 4 | { | ||
| 5 | SDL_Surface* tmp = SDL_CreateRGBSurface(SDL_SWSURFACE || SDL_SRCCOLORKEY, 480, 480, 32, 0,0,0,0); | ||
| 6 | Uint32 bgColor = SDL_MapRGB(tmp->format, 255, 255, 255); | ||
| 7 | SDL_FillRect(tmp, NULL, bgColor); | ||
| 8 | SDL_SetColorKey(tmp, SDL_SRCCOLORKEY, bgColor); | ||
| 9 | TTF_Font* posFont = loadFont(40); | ||
| 10 | TTF_Font* dataFont = loadFont(25); | ||
| 11 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 12 | |||
| 13 | for (int i=0; i<hslist.size(); i++) | ||
| 14 | { | ||
| 15 | Highscore h = hslist[i]; | ||
| 16 | |||
| 17 | int posw, posh; | ||
| 18 | char pos[3]; // 2 max characters in rank plus the colon at the end | ||
| 19 | sprintf(pos, "%d:", h.getRank()); | ||
| 20 | TTF_SizeText(posFont, pos, &posw, &posh); | ||
| 21 | SDL_Rect posSpace = {0, (i+1)*40, posw, posh}; | ||
| 22 | SDL_BlitSurface(TTF_RenderText_Blended(posFont, pos, fontColor), NULL, tmp, &posSpace); | ||
| 23 | |||
| 24 | int namew, nameh; | ||
| 25 | char name[26]; // 25 max characters in username plus the space at the beginning | ||
| 26 | sprintf(name, " %s", h.getName()); | ||
| 27 | TTF_SizeText(dataFont, name, &namew, &nameh); | ||
| 28 | SDL_Rect nameSpace = {posw, (i+1)*40+((posh/2)-(nameh/2)), namew, nameh}; | ||
| 29 | SDL_BlitSurface(TTF_RenderText_Blended(dataFont, name, fontColor), NULL, tmp, &nameSpace); | ||
| 30 | |||
| 31 | int lvlw, lvlh; | ||
| 32 | char lvl[10]; // 10 max characters in level (based off the fact that 2^32-1 is 10 characters long, and is the highest int) | ||
| 33 | sprintf(lvl, "%d", h.getLevel()); | ||
| 34 | TTF_SizeText(dataFont, lvl, &lvlw, &lvlh); | ||
| 35 | SDL_Rect lvlSpace = {480-lvlw, (i+1)*40+((posh/2)-(nameh/2)), lvlw, lvlh}; | ||
| 36 | SDL_BlitSurface(TTF_RenderText_Blended(dataFont, lvl, fontColor), NULL, tmp, &lvlSpace); | ||
| 37 | } | ||
| 38 | |||
| 39 | return tmp; | ||
| 40 | } | ||
| 41 | |||
| 42 | std::vector<Highscore> HighscoreList::getLocalHighscores() | ||
| 43 | { | ||
| 44 | std::vector<Highscore> temp = std::vector<Highscore>(); | ||
| 45 | |||
| 46 | std::ifstream exists(getDataFile()); | ||
| 47 | if (exists) | ||
| 48 | { | ||
| 49 | FILE* hslist = fopen(getDataFile(), "r"); | ||
| 50 | int scores; | ||
| 51 | fscanf(hslist, "%d%*c", &scores); | ||
| 52 | |||
| 53 | for (int i=0; i<scores; i++) | ||
| 54 | { | ||
| 55 | int namelen; | ||
| 56 | char namelens[4]; | ||
| 57 | char* name = (char*) calloc(25, sizeof(char)); | ||
| 58 | int score; | ||
| 59 | |||
| 60 | fscanf(hslist, "%d", &namelen); | ||
| 61 | sprintf(namelens, "%%%dc", namelen); | ||
| 62 | fscanf(hslist, namelens, name); | ||
| 63 | fscanf(hslist, "%d%*c", &score); | ||
| 64 | |||
| 65 | Highscore h = Highscore(name, score); | ||
| 66 | h.setRank(i+1); | ||
| 67 | |||
| 68 | temp.push_back(h); | ||
| 69 | } | ||
| 70 | |||
| 71 | fclose(hslist); | ||
| 72 | } | ||
| 73 | |||
| 74 | return temp; | ||
| 75 | } | ||
| 76 | |||
| 77 | std::vector<Highscore> HighscoreList::getGlobalHighscores() | ||
| 78 | { | ||
| 79 | |||
| 80 | } | ||
| 81 | |||
| 82 | LocalHighscoreList::LocalHighscoreList() | ||
| 83 | { | ||
| 84 | this->hslist = getLocalHighscores(); | ||
| 85 | } | ||
| diff --git a/hslist.h b/hslist.h new file mode 100644 index 0000000..88c47c4 --- /dev/null +++ b/hslist.h | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | #ifndef HSLIST_H | ||
| 2 | #define HSLIST_H | ||
| 3 | |||
| 4 | class HighscoreList | ||
| 5 | { | ||
| 6 | public: | ||
| 7 | SDL_Surface* render(); | ||
| 8 | |||
| 9 | protected: | ||
| 10 | std::vector<Highscore> getLocalHighscores(); | ||
| 11 | std::vector<Highscore> getGlobalHighscores(); | ||
| 12 | |||
| 13 | std::vector<Highscore> hslist; | ||
| 14 | }; | ||
| 15 | |||
| 16 | class LocalHighscoreList : public HighscoreList { | ||
| 17 | public: | ||
| 18 | LocalHighscoreList(); | ||
| 19 | }; | ||
| 20 | |||
| 21 | #endif | ||
| diff --git a/hslocal.cpp b/hslocal.cpp new file mode 100644 index 0000000..17cae1b --- /dev/null +++ b/hslocal.cpp | |||
| @@ -0,0 +1,84 @@ | |||
| 1 | #include "includes.h" | ||
| 2 | |||
| 3 | LocalHighscoreListState::LocalHighscoreListState(bool fromGame) | ||
| 4 | { | ||
| 5 | this->fromGame = fromGame; | ||
| 6 | |||
| 7 | if (fromGame) | ||
| 8 | { | ||
| 9 | LOADIMAGE(options,hlo_paartm) | ||
| 10 | } else { | ||
| 11 | LOADIMAGE(options,hlo_rtm) | ||
| 12 | } | ||
| 13 | |||
| 14 | LOADIMAGE(pointer,pointer) | ||
| 15 | |||
| 16 | LocalHighscoreList* lhl = new LocalHighscoreList(); | ||
| 17 | list = lhl->render(); | ||
| 18 | |||
| 19 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 20 | SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); | ||
| 21 | SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; | ||
| 22 | SDL_BlitSurface(title, NULL, list, &tSpace); | ||
| 23 | |||
| 24 | SDL_Rect oSpace = {0, 440, options->w, options->h}; | ||
| 25 | SDL_BlitSurface(options, NULL, list, &oSpace); | ||
| 26 | |||
| 27 | selection = 0; | ||
| 28 | |||
| 29 | SDL_WM_SetCaption("Maze Of Life - Highscore List", NULL); | ||
| 30 | } | ||
| 31 | |||
| 32 | void LocalHighscoreListState::input(SDL_keysym key) | ||
| 33 | { | ||
| 34 | if (fromGame) | ||
| 35 | { | ||
| 36 | if ((key.sym == SDLK_LEFT) && (selection != 0)) | ||
| 37 | { | ||
| 38 | selection--; | ||
| 39 | } else if ((key.sym == SDLK_RIGHT) && (selection != 1)) | ||
| 40 | { | ||
| 41 | selection++; | ||
| 42 | } else if (key.sym == SDLK_RETURN) | ||
| 43 | { | ||
| 44 | switch (selection) | ||
| 45 | { | ||
| 46 | case 0: | ||
| 47 | changeState(new GameState()); | ||
| 48 | |||
| 49 | break; | ||
| 50 | case 1: | ||
| 51 | changeState(new TitleState()); | ||
| 52 | |||
| 53 | break; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | } else { | ||
| 57 | if (key.sym == SDLK_RETURN) | ||
| 58 | { | ||
| 59 | changeState(new ChooseHighscoreListState()); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | void LocalHighscoreListState::render(SDL_Surface* screen) | ||
| 65 | { | ||
| 66 | SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255)); | ||
| 67 | |||
| 68 | SDL_BlitSurface(list, NULL, screen, NULL); | ||
| 69 | |||
| 70 | SDL_Rect pSpace; | ||
| 71 | pSpace.w = pointer->w; | ||
| 72 | pSpace.h = pointer->h; | ||
| 73 | |||
| 74 | if (fromGame) | ||
| 75 | { | ||
| 76 | pSpace.x = (selection==0?52:225); | ||
| 77 | pSpace.y = 447; | ||
| 78 | } else { | ||
| 79 | pSpace.x = 137; | ||
| 80 | pSpace.y = 449; | ||
| 81 | } | ||
| 82 | |||
| 83 | SDL_BlitSurface(pointer, NULL, screen, &pSpace); | ||
| 84 | } | ||
| diff --git a/hslocal.h b/hslocal.h new file mode 100644 index 0000000..8708d03 --- /dev/null +++ b/hslocal.h | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | #ifndef HSLOCAL_H | ||
| 2 | #define HSLOCAL_H | ||
| 3 | |||
| 4 | class LocalHighscoreListState : public State { | ||
| 5 | public: | ||
| 6 | LocalHighscoreListState(bool fromGame); | ||
| 7 | void input(SDL_keysym key); | ||
| 8 | void render(SDL_Surface* screen); | ||
| 9 | |||
| 10 | private: | ||
| 11 | SDL_Surface* list; | ||
| 12 | SDL_Surface* options; | ||
| 13 | SDL_Surface* pointer; | ||
| 14 | int selection; | ||
| 15 | bool fromGame; | ||
| 16 | }; | ||
| 17 | |||
| 18 | #endif | ||
| 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 | } | ||
| diff --git a/hsnew.h b/hsnew.h new file mode 100644 index 0000000..123ac53 --- /dev/null +++ b/hsnew.h | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | #ifndef HSNEW_H | ||
| 2 | #define HSNEW_H | ||
| 3 | |||
| 4 | class NewHighscoreState : public State { | ||
| 5 | public: | ||
| 6 | NewHighscoreState(int level); | ||
| 7 | void input(SDL_keysym key); | ||
| 8 | void render(SDL_Surface* screen); | ||
| 9 | |||
| 10 | private: | ||
| 11 | class NewHighscoreList : public HighscoreList { | ||
| 12 | public: | ||
| 13 | NewHighscoreList(int level); | ||
| 14 | int getNewPos(); | ||
| 15 | void addToList(char* name); | ||
| 16 | |||
| 17 | private: | ||
| 18 | int newpos; | ||
| 19 | }; | ||
| 20 | |||
| 21 | SDL_Surface* list; | ||
| 22 | SDL_Surface* options; | ||
| 23 | SDL_Surface* pointer; | ||
| 24 | int selection; | ||
| 25 | int level; | ||
| 26 | int newpos; | ||
| 27 | int lp; | ||
| 28 | char* hsname; | ||
| 29 | bool enterName; | ||
| 30 | SDL_Rect rntSpace; | ||
| 31 | SDL_Surface* newName; | ||
| 32 | NewHighscoreList* lhl; | ||
| 33 | }; | ||
| 34 | |||
| 35 | #endif | ||
| diff --git a/htpstate.cpp b/htpstate.cpp index 711dca4..da4b6a2 100644 --- a/htpstate.cpp +++ b/htpstate.cpp | |||
| @@ -12,15 +12,15 @@ HowToPlayState::HowToPlayState() | |||
| 12 | SDL_WM_SetCaption("Maze Of Life - How To Play", NULL); | 12 | SDL_WM_SetCaption("Maze Of Life - How To Play", NULL); |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | void HowToPlayState::input(SDLKey key) | 15 | void HowToPlayState::input(SDL_keysym key) |
| 16 | { | 16 | { |
| 17 | if ((key == SDLK_LEFT) && (selection != 0)) | 17 | if ((key.sym == SDLK_LEFT) && (selection != 0)) |
| 18 | { | 18 | { |
| 19 | selection--; | 19 | selection--; |
| 20 | } else if ((key == SDLK_RIGHT) && (selection != 1)) | 20 | } else if ((key.sym == SDLK_RIGHT) && (selection != 1)) |
| 21 | { | 21 | { |
| 22 | selection++; | 22 | selection++; |
| 23 | } else if (key == SDLK_RETURN) | 23 | } else if (key.sym == SDLK_RETURN) |
| 24 | { | 24 | { |
| 25 | switch (selection) | 25 | switch (selection) |
| 26 | { | 26 | { |
| diff --git a/htpstate.h b/htpstate.h index 2639d1a..79c66ba 100644 --- a/htpstate.h +++ b/htpstate.h | |||
| @@ -4,7 +4,7 @@ | |||
| 4 | class HowToPlayState : public State { | 4 | class HowToPlayState : public State { |
| 5 | public: | 5 | public: |
| 6 | HowToPlayState(); | 6 | HowToPlayState(); |
| 7 | void input(SDLKey key); | 7 | void input(SDL_keysym key); |
| 8 | void render(SDL_Surface* screen); | 8 | void render(SDL_Surface* screen); |
| 9 | 9 | ||
| 10 | private: | 10 | private: |
| diff --git a/includes.h b/includes.h index a36c456..cd5c1e9 100644 --- a/includes.h +++ b/includes.h | |||
| @@ -1,7 +1,10 @@ | |||
| 1 | #include <SDL.h> | 1 | #include <SDL.h> |
| 2 | #include <SDL_ttf.h> | ||
| 2 | #include <stdio.h> | 3 | #include <stdio.h> |
| 3 | #include <time.h> | 4 | #include <time.h> |
| 4 | #include <string> | 5 | #include <string> |
| 6 | #include <vector> | ||
| 7 | #include <fstream> | ||
| 5 | #include "state.h" | 8 | #include "state.h" |
| 6 | #include "mazeoflife.h" | 9 | #include "mazeoflife.h" |
| 7 | #include "resources.h" | 10 | #include "resources.h" |
| @@ -9,3 +12,7 @@ | |||
| 9 | #include "htpstate.h" | 12 | #include "htpstate.h" |
| 10 | #include "chlstate.h" | 13 | #include "chlstate.h" |
| 11 | #include "gamestate.h" | 14 | #include "gamestate.h" |
| 15 | #include "highscore.h" | ||
| 16 | #include "hslist.h" | ||
| 17 | #include "hslocal.h" | ||
| 18 | #include "hsnew.h" | ||
| diff --git a/mazeoflife.cpp b/mazeoflife.cpp index 0dbca55..c744b28 100644 --- a/mazeoflife.cpp +++ b/mazeoflife.cpp | |||
| @@ -7,13 +7,21 @@ int main(int argc, char *argv[]) | |||
| 7 | { | 7 | { |
| 8 | srand(time(NULL)); | 8 | srand(time(NULL)); |
| 9 | 9 | ||
| 10 | if((SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER)==-1)) { | 10 | if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) |
| 11 | { | ||
| 11 | printf("Could not initialize SDL: %s.\n", SDL_GetError()); | 12 | printf("Could not initialize SDL: %s.\n", SDL_GetError()); |
| 12 | exit(-1); | 13 | exit(-1); |
| 13 | } | 14 | } |
| 14 | 15 | ||
| 16 | if (TTF_Init() == -1) | ||
| 17 | { | ||
| 18 | printf("Could not initialize SDL_ttf: %s.\n", TTF_GetError()); | ||
| 19 | exit(-1); | ||
| 20 | } | ||
| 21 | |||
| 15 | /* Clean up on exit */ | 22 | /* Clean up on exit */ |
| 16 | atexit(SDL_Quit); | 23 | atexit(SDL_Quit); |
| 24 | atexit(TTF_Quit); | ||
| 17 | 25 | ||
| 18 | SDL_WM_SetCaption("Maze Of Life", NULL); | 26 | SDL_WM_SetCaption("Maze Of Life", NULL); |
| 19 | 27 | ||
| @@ -49,7 +57,7 @@ int main(int argc, char *argv[]) | |||
| 49 | 57 | ||
| 50 | break; | 58 | break; |
| 51 | case SDL_KEYDOWN: | 59 | case SDL_KEYDOWN: |
| 52 | state->input(anEvent.key.keysym.sym); | 60 | state->input(anEvent.key.keysym); |
| 53 | 61 | ||
| 54 | break; | 62 | break; |
| 55 | } | 63 | } |
| @@ -96,3 +104,28 @@ Uint32 tick(Uint32 interval, void *param) | |||
| 96 | 104 | ||
| 97 | return interval; | 105 | return interval; |
| 98 | } | 106 | } |
| 107 | |||
| 108 | TTF_Font* loadFont(int size) | ||
| 109 | { | ||
| 110 | SDL_RWops* mono_rw = SDL_RWFromMem(&RESNAME(mono_ttf,start), (int) &RESNAME(mono_ttf,size)); | ||
| 111 | TTF_Font* tmpfont = TTF_OpenFontRW(mono_rw, 1, size); | ||
| 112 | |||
| 113 | if (tmpfont == NULL) | ||
| 114 | { | ||
| 115 | printf("Unable to load font: %s\n", TTF_GetError()); | ||
| 116 | exit(1); | ||
| 117 | } | ||
| 118 | |||
| 119 | return tmpfont; | ||
| 120 | } | ||
| 121 | |||
| 122 | const char* getDataFile() | ||
| 123 | { | ||
| 124 | #ifdef WINDOWS | ||
| 125 | char* dir = getenv("USERPROFILE"); | ||
| 126 | #else | ||
| 127 | char* dir = getenv("HOME"); | ||
| 128 | #endif | ||
| 129 | |||
| 130 | return (std::string(dir) + "/.molhslist").c_str(); | ||
| 131 | } | ||
| diff --git a/mazeoflife.h b/mazeoflife.h index 1f43214..411a87b 100644 --- a/mazeoflife.h +++ b/mazeoflife.h | |||
| @@ -9,5 +9,7 @@ void wrap(int* x, int* y); | |||
| 9 | Uint32 getColor(int r, int g, int b); | 9 | Uint32 getColor(int r, int g, int b); |
| 10 | void changeState(State* nState); | 10 | void changeState(State* nState); |
| 11 | Uint32 tick(Uint32 interval, void *param); | 11 | Uint32 tick(Uint32 interval, void *param); |
| 12 | TTF_Font* loadFont(int size); | ||
| 13 | const char* getDataFile(); | ||
| 12 | 14 | ||
| 13 | #endif | 15 | #endif |
| diff --git a/resources.h b/resources.h index fc9229a..4743950 100644 --- a/resources.h +++ b/resources.h | |||
| @@ -16,5 +16,9 @@ DEFRES(pointer_bmp) | |||
| 16 | DEFRES(htp1_bmp) | 16 | DEFRES(htp1_bmp) |
| 17 | DEFRES(htp2_bmp) | 17 | DEFRES(htp2_bmp) |
| 18 | DEFRES(chl_bmp) | 18 | DEFRES(chl_bmp) |
| 19 | DEFRES(hlo_rtm_bmp) | ||
| 20 | DEFRES(hlo_paartm_bmp) | ||
| 21 | DEFRES(hlo_passartm_bmp) | ||
| 22 | DEFRES(mono_ttf) | ||
| 19 | 23 | ||
| 20 | #endif | 24 | #endif |
| diff --git a/resources/hlo_paartm.bmp b/resources/hlo_paartm.bmp new file mode 100644 index 0000000..72a1bf2 --- /dev/null +++ b/resources/hlo_paartm.bmp | |||
| Binary files differ | |||
| diff --git a/resources/hlo_passartm.bmp b/resources/hlo_passartm.bmp new file mode 100644 index 0000000..6c57e98 --- /dev/null +++ b/resources/hlo_passartm.bmp | |||
| Binary files differ | |||
| diff --git a/resources/hlo_rtm.bmp b/resources/hlo_rtm.bmp new file mode 100644 index 0000000..c05c208 --- /dev/null +++ b/resources/hlo_rtm.bmp | |||
| Binary files differ | |||
| diff --git a/resources/mono.ttf b/resources/mono.ttf new file mode 100644 index 0000000..1feefc2 --- /dev/null +++ b/resources/mono.ttf | |||
| Binary files differ | |||
| diff --git a/state.h b/state.h index bcc7e4a..7373940 100644 --- a/state.h +++ b/state.h | |||
| @@ -4,7 +4,7 @@ | |||
| 4 | class State | 4 | class State |
| 5 | { | 5 | { |
| 6 | public: | 6 | public: |
| 7 | virtual void input(SDLKey key) {}; | 7 | virtual void input(SDL_keysym key) {}; |
| 8 | virtual void tick() {}; | 8 | virtual void tick() {}; |
| 9 | virtual void render(SDL_Surface* screen) {}; | 9 | virtual void render(SDL_Surface* screen) {}; |
| 10 | }; | 10 | }; |
| diff --git a/titlestate.cpp b/titlestate.cpp index 980e3b6..ae18c48 100644 --- a/titlestate.cpp +++ b/titlestate.cpp | |||
| @@ -10,15 +10,15 @@ TitleState::TitleState() | |||
| 10 | SDL_WM_SetCaption("Maze Of Life", NULL); | 10 | SDL_WM_SetCaption("Maze Of Life", NULL); |
| 11 | } | 11 | } |
| 12 | 12 | ||
| 13 | void TitleState::input(SDLKey key) | 13 | void TitleState::input(SDL_keysym key) |
| 14 | { | 14 | { |
| 15 | if ((key == SDLK_UP) && (selection != 0)) | 15 | if ((key.sym == SDLK_UP) && (selection != 0)) |
| 16 | { | 16 | { |
| 17 | selection--; | 17 | selection--; |
| 18 | } else if ((key == SDLK_DOWN) && (selection != 3)) | 18 | } else if ((key.sym == SDLK_DOWN) && (selection != 3)) |
| 19 | { | 19 | { |
| 20 | selection++; | 20 | selection++; |
| 21 | } else if (key == SDLK_RETURN) | 21 | } else if (key.sym == SDLK_RETURN) |
| 22 | { | 22 | { |
| 23 | switch (selection) | 23 | switch (selection) |
| 24 | { | 24 | { |
| diff --git a/titlestate.h b/titlestate.h index 9056626..6519ecf 100644 --- a/titlestate.h +++ b/titlestate.h | |||
| @@ -4,7 +4,7 @@ | |||
| 4 | class TitleState : public State { | 4 | class TitleState : public State { |
| 5 | public: | 5 | public: |
| 6 | TitleState(); | 6 | TitleState(); |
| 7 | void input(SDLKey key); | 7 | void input(SDL_keysym key); |
| 8 | void render(SDL_Surface* screen); | 8 | void render(SDL_Surface* screen); |
| 9 | 9 | ||
| 10 | private: | 10 | private: |
