diff options
author | Starla Insigna <starla4444@gmail.com> | 2013-08-27 11:39:37 -0400 |
---|---|---|
committer | Starla Insigna <starla4444@gmail.com> | 2013-08-27 11:39:37 -0400 |
commit | ffd335aca284c286030e2b26f1a02a0441748f46 (patch) | |
tree | bcc1c241621fe159ae7ef178122fb41c5f23eb00 /hsnew.cpp | |
parent | d47da18958b5214def5127e201f60668c566d9bb (diff) | |
download | mazeoflife-ffd335aca284c286030e2b26f1a02a0441748f46.tar.gz mazeoflife-ffd335aca284c286030e2b26f1a02a0441748f46.tar.bz2 mazeoflife-ffd335aca284c286030e2b26f1a02a0441748f46.zip |
Started rewriting game from scratch with SDL2
Only the title screen is currently implemented
Diffstat (limited to 'hsnew.cpp')
-rw-r--r-- | hsnew.cpp | 220 |
1 files changed, 0 insertions, 220 deletions
diff --git a/hsnew.cpp b/hsnew.cpp deleted file mode 100644 index b4894c2..0000000 --- a/hsnew.cpp +++ /dev/null | |||
@@ -1,220 +0,0 @@ | |||
1 | #include "includes.h" | ||
2 | |||
3 | NewHighscoreState::NewHighscoreState(int level) | ||
4 | { | ||
5 | this->level = level; | ||
6 | |||
7 | options = SDL_LoadBMP("resources/hlo_passartm.bmp"); | ||
8 | pointer = SDL_LoadBMP("resources/pointer.bmp"); | ||
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_EnableUNICODE(1); | ||
42 | } | ||
43 | |||
44 | void NewHighscoreState::input(SDL_keysym key) | ||
45 | { | ||
46 | if (enterName) | ||
47 | { | ||
48 | if ((key.sym == SDLK_BACKSPACE) && (lp > 0)) | ||
49 | { | ||
50 | hsname[--lp] = 0; | ||
51 | |||
52 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
53 | char name[26]; // 25 max characters in username plus the space at the beginning | ||
54 | sprintf(name, " %s", hsname); | ||
55 | newName = TTF_RenderText_Blended(loadFont(25), name, fontColor); | ||
56 | rntSpace.w = newName->w; | ||
57 | rntSpace.h = newName->h; | ||
58 | } else if ((key.sym == SDLK_RETURN) && (hsname[0] != 0)) | ||
59 | { | ||
60 | SDL_EnableUNICODE(0); | ||
61 | enterName = false; | ||
62 | |||
63 | lhl->addToList(hsname); | ||
64 | LocalHighscoreList* lhl2 = new LocalHighscoreList(); | ||
65 | list = lhl2->render(); | ||
66 | |||
67 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
68 | SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "New Highscore!", fontColor); | ||
69 | SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; | ||
70 | SDL_BlitSurface(title, NULL, list, &tSpace); | ||
71 | |||
72 | SDL_Rect oSpace = {0, 440, options->w, options->h}; | ||
73 | SDL_BlitSurface(options, NULL, list, &oSpace); | ||
74 | } else if (((key.unicode & 0xFF80) == 0) && (key.unicode >= 32 && key.unicode < 127) && (lp < 25)) | ||
75 | { | ||
76 | hsname[lp++] = key.unicode & 0x7f; | ||
77 | hsname[lp] = 0; | ||
78 | |||
79 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
80 | char name[26]; // 25 max characters in username plus the space at the beginning | ||
81 | sprintf(name, " %s", hsname); | ||
82 | newName = TTF_RenderText_Blended(loadFont(25), name, fontColor); | ||
83 | rntSpace.w = newName->w; | ||
84 | rntSpace.h = newName->h; | ||
85 | } | ||
86 | } else { | ||
87 | if ((key.sym == SDLK_LEFT) && (selection != 0)) | ||
88 | { | ||
89 | selection--; | ||
90 | } else if ((key.sym == SDLK_RIGHT) && (selection != 2)) | ||
91 | { | ||
92 | selection++; | ||
93 | } else if (key.sym == SDLK_RETURN) | ||
94 | { | ||
95 | switch (selection) | ||
96 | { | ||
97 | case 0: | ||
98 | changeState(new GameState()); | ||
99 | |||
100 | break; | ||
101 | case 1: | ||
102 | changeState(new SubmitHighscoreListState(hsname, level)); | ||
103 | |||
104 | break; | ||
105 | case 2: | ||
106 | changeState(new TitleState()); | ||
107 | |||
108 | break; | ||
109 | } | ||
110 | } | ||
111 | } | ||
112 | } | ||
113 | |||
114 | void NewHighscoreState::render(SDL_Surface* screen) | ||
115 | { | ||
116 | SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255)); | ||
117 | |||
118 | SDL_Rect eSpace = {0, newpos*40, 480, 40}; | ||
119 | SDL_FillRect(screen, &eSpace, SDL_MapRGB(screen->format, 255, 255, 0)); | ||
120 | |||
121 | SDL_BlitSurface(list, NULL, screen, NULL); | ||
122 | |||
123 | if (enterName) | ||
124 | { | ||
125 | SDL_BlitSurface(newName, NULL, screen, &rntSpace); | ||
126 | } else { | ||
127 | SDL_Rect pSpace; | ||
128 | pSpace.x = (selection==0?13:(selection==1?138:284)); | ||
129 | pSpace.y = 448; | ||
130 | pSpace.w = pointer->w; | ||
131 | pSpace.h = pointer->h; | ||
132 | |||
133 | SDL_BlitSurface(pointer, NULL, screen, &pSpace); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | NewHighscoreState::NewHighscoreList::NewHighscoreList(int level) | ||
138 | { | ||
139 | this->hslist = getLocalHighscores(); | ||
140 | |||
141 | if (this->hslist.empty()) | ||
142 | { | ||
143 | Highscore n = Highscore("", level); | ||
144 | n.setRank(1); | ||
145 | |||
146 | this->hslist.push_back(n); | ||
147 | this->newpos = n.getRank(); | ||
148 | } else { | ||
149 | std::vector<Highscore>::iterator it; | ||
150 | bool found = false; | ||
151 | int lastrank; | ||
152 | int i=0; | ||
153 | |||
154 | for (it=this->hslist.begin(); it<this->hslist.end(); it++) | ||
155 | { | ||
156 | Highscore h = *it; | ||
157 | lastrank = h.getRank(); | ||
158 | |||
159 | if (!found) | ||
160 | { | ||
161 | if (h.getLevel() < level) | ||
162 | { | ||
163 | Highscore n = Highscore("", level); | ||
164 | n.setRank(h.getRank()); | ||
165 | |||
166 | this->hslist.insert(it, n); | ||
167 | this->newpos = n.getRank(); | ||
168 | |||
169 | if (this->hslist.size() > 10) | ||
170 | { | ||
171 | this->hslist.pop_back(); | ||
172 | } | ||
173 | |||
174 | found = true; | ||
175 | } | ||
176 | } else { | ||
177 | //this->hslist[i].setRank(h.getRank()+1); | ||
178 | } | ||
179 | |||
180 | i++; | ||
181 | } | ||
182 | |||
183 | if (!found) | ||
184 | { | ||
185 | Highscore n = Highscore("", level); | ||
186 | n.setRank(lastrank+1); | ||
187 | |||
188 | this->hslist.push_back(n); | ||
189 | this->newpos = n.getRank(); | ||
190 | } | ||
191 | } | ||
192 | } | ||
193 | |||
194 | int NewHighscoreState::NewHighscoreList::getNewPos() | ||
195 | { | ||
196 | return newpos; | ||
197 | } | ||
198 | |||
199 | void NewHighscoreState::NewHighscoreList::addToList(char* name) | ||
200 | { | ||
201 | FILE* hsfile = fopen(getDataFile(), "w"); | ||
202 | fprintf(hsfile, "%d ", this->hslist.size()); | ||
203 | |||
204 | std::vector<Highscore>::iterator it; | ||
205 | |||
206 | for (it=this->hslist.begin(); it<this->hslist.end(); it++) | ||
207 | { | ||
208 | Highscore h = *it; | ||
209 | |||
210 | if (h.getName() == "") | ||
211 | { | ||
212 | h = Highscore(name, h.getLevel()); | ||
213 | h.setRank(newpos); | ||
214 | } | ||
215 | |||
216 | fprintf(hsfile, "%d%s%d ", strlen(h.getName()), h.getName(), h.getLevel()); | ||
217 | } | ||
218 | |||
219 | fclose(hsfile); | ||
220 | } | ||