Pac-Man Evolution
Loading...
Searching...
No Matches
HoF.cpp
1/*----------------------------------------------------------------------
2Pac-Man Evolution - Roberto Prieto
3Copyright (C) 2018-2024 MegaStorm Systems
4contact@megastormsystems.com - http://www.megastormsystems.com
5
6This software is provided 'as-is', without any express or implied
7warranty. In no event will the authors be held liable for any damages
8arising from the use of this software.
9
10Permission is granted to anyone to use this software for any purpose,
11including commercial applications, and to alter it and redistribute it
12freely, subject to the following restrictions:
13
141. The origin of this software must not be misrepresented; you must not
15claim that you wrote the original software. If you use this software
16in a product, an acknowledgment in the product documentation would be
17appreciated but is not required.
182. Altered source versions must be plainly marked as such, and must not be
19misrepresented as being the original software.
203. This notice may not be removed or altered from any source distribution.
21
22------------------------------------------------------------------------
23
24Hall of Fame class
25
26------------------------------------------------------------------------ */
27
28#include "HoF.h"
29#include "Pac-Man_Evolution.h"
30#include "ResourceManager.h"
31
32HoF::HoF()
33{
34 Sint32 i, j, idCDC;
35 Main &mC64 = Main::Instance();
36 Archive::Block myBlock;
37 Archive *mCDC;
38
39 // The scores are saved in an embedded file
40 strcpy(myBlock.sParam1,"HoF.dat");
41 strcpy(myBlock.sParam2, "C64_FILE");
42
43 // First, try to load it
44 memset(scores, 0, sizeof(scores));
45 idCDC = mC64.IArchiveMgr().load(PME_CONFIG);
46 mCDC = mC64.IArchiveMgr().get(idCDC);
47 if(mCDC != nullptr)
48 {
49 if(mCDC->blockLoad(myBlock) == 0)
50 {
51 if(myBlock.iSize == sizeof(scores)) memcpy(scores, myBlock.pData, myBlock.iSize);
52 mCDC->blockLoadReleaseData(myBlock);
53 }
54 }
55 if(idCDC > 0) mC64.IArchiveMgr().close(idCDC);
56
57 // If they are not present, we randomly generated them
58 if(scores[0][0].iPoints == 0)
59 {
60 for(i = 0; i < 2; i++)
61 {
62 for(j = 0; j < PME_HOF_SCORES; j++)
63 {
64 switch(mC64.ITool().randMWC() % 8)
65 {
66 case 0:
67 strcpy(scores[i][j].sName, "Soraya");
68 break;
69 case 1:
70 strcpy(scores[i][j].sName, "Lorena");
71 break;
72 case 2:
73 strcpy(scores[i][j].sName, "Eduardo");
74 break;
75 case 3:
76 strcpy(scores[i][j].sName, "Alvaro");
77 break;
78 case 4:
79 strcpy(scores[i][j].sName, "Bob");
80 break;
81 case 5:
82 strcpy(scores[i][j].sName, "Arturo");
83 break;
84 case 6:
85 strcpy(scores[i][j].sName, "Diego");
86 break;
87 case 7:
88 strcpy(scores[i][j].sName, "Shelly");
89 break;
90 }
91 scores[i][j].iPoints = (500 * PME_HOF_SCORES) - (j * 500);
92 }
93 }
94 }
95}
96
97HoF::~HoF()
98{
99 Sint32 idCDC;
100 Main &mC64 = Main::Instance();
101 Archive::Block myBlock;
102 Archive *mCDC;
103
104 // The scores are saved in an embedded file
105 strcpy(myBlock.sParam1, "HoF.dat");
106 strcpy(myBlock.sParam2, "C64_FILE");
107
108 // Save current scores
109 idCDC = mC64.IArchiveMgr().load(PME_CONFIG);
110 if(idCDC < 0) idCDC = mC64.IArchiveMgr().create(PME_CONFIG);
111 mCDC = mC64.IArchiveMgr().get(idCDC);
112 if(mCDC != NULL)
113 {
114 myBlock.iSize = sizeof(scores);
115 myBlock.pData = scores;
116 mCDC->blockAdd(myBlock);
117 }
118 if(idCDC > 0) mC64.IArchiveMgr().close(idCDC);
119}
120
121Sint32 HoF::store(Sint32 iGameType, Sint32 iPoints, char* szName)
122{
123 Sint32 i;
124
125 // Security check of the gametype
126 if(iGameType < PME_GAME_EVOLUTION) iGameType = 0;
127 else iGameType = 1;
128
129 for(i = 0; i < PME_HOF_SCORES; i++)
130 {
131 if(iPoints > scores[iGameType][i].iPoints)
132 {
133 insert(iGameType, i, iPoints, szName);
134 break;
135 }
136 }
137
138 return 0;
139}
140
141// Get highest score of the given gametype
142Sint32 HoF::getHighest(Sint32 iGameType)
143{
144 // Security check of the gametype
145 if(iGameType < PME_GAME_EVOLUTION) iGameType = 0;
146 else iGameType = 1;
147 return scores[iGameType][0].iPoints;
148}
149
150// Get lowest score of the given gametype
151Sint32 HoF::getLowest(Sint32 iGameType)
152{
153 // Security check of the gametype
154 if(iGameType < PME_GAME_EVOLUTION) iGameType = 0;
155 else iGameType = 1;
156 return scores[iGameType][PME_HOF_SCORES - 1].iPoints;
157}
158
159// Render current HoF (position + name + elemental + points)
160#define SCORES_X 100
161#define SCORES_Y 280
162#define SCORES_X_SPACE 440
163#define SCORES_Y_SPACE 43
164Sint32 HoF::render(Sint32 iMode)
165{
166 int iGT, i, iPos, iX, iLX, iY, iMaxPos, iMaxName, iMaxPoints;
167 Main &mC64 = Main::Instance();
168 Font* fontPlayer;
169 Font* fontScore;
170 string sTmp;
171 SDL_Rect rDst;
172 Sint32 iSW = 0, iSH = 0;
173 Screen* pScreen = Main::Instance().IConfigMgr().get();
174 if(pScreen != nullptr) pScreen->getSize(&iSW, &iSH);
175
176 // Initial position
177 iX = SCORES_X; iY = SCORES_Y;
178
179 // Load score and air fonts
180 fontScore = mC64.IFontMgr().get(ResourceManager::Instance().get(RM_FONT_SCORE));
181 fontPlayer = mC64.IFontMgr().get(ResourceManager::Instance().get(RM_FONT_INFO));
182
183 // Get the maximum "position text" size used for alignment purposes
184 iMaxPos = 0;
185 for(iPos = 0; iPos < PME_HOF_SCORES; iPos++)
186 {
187 mC64.ITool().intToStrDec(iPos + 1, sTmp);
188 i = fontScore->getWidth(sTmp);
189 if(i > iMaxPos) iMaxPos = i;
190 }
191
192 // Render background centered
193 Image* pImage = Main::Instance().IImageMgr().get(ResourceManager::Instance().get(RM_IMG_HOF));
194 if(pImage != nullptr)
195 {
196 rDst.w = pImage->getSurface()->w;
197 rDst.h = pImage->getSurface()->h;
198 rDst.x = (iSW - rDst.w) / 2;
199 rDst.y = (iSH - rDst.h) / 2;
200 pImage->render(0, nullptr, &rDst);
201 }
202
203 // Render player names + points in the two different game types
204 for(iGT = 0; iGT < 2; iGT++)
205 {
206 // Get the maximum "name text" and "points text" size used for aligment purposes
207 // Assumes all custom fonts' players have the same size
208 iMaxName = iMaxPoints = 0;
209 for(iPos = 0; iPos < PME_HOF_SCORES; iPos++)
210 {
211 i = fontPlayer->getWidth(scores[iGT][iPos].sName);
212 if(i > iMaxName) iMaxName = i;
213 mC64.ITool().intToStrDec(scores[iGT][iPos].iPoints, sTmp);
214 i = fontPlayer->getWidth(sTmp);
215 if(i > iMaxPoints) iMaxPoints = i;
216 }
217
218 // Loop through all the scores rendering them aligned
219 for(iPos = 0; iPos < PME_HOF_SCORES; iPos++)
220 {
221 iLX = iX;
222
223 // Position (from 1 to HOF_SCORES)
224 mC64.ITool().intToStrDec(iPos + 1, sTmp);
225 fontPlayer->setPosition(iLX - (fontScore->getWidth(sTmp) - iMaxPos), iY);
226 fontPlayer->render(sTmp);
227 iLX = iLX + iMaxPos + 10;
228
229 // Player name
230 fontPlayer->setPosition(iLX - (fontPlayer->getWidth(scores[iGT][iPos].sName) - iMaxName), iY);
231 fontPlayer->render(scores[iGT][iPos].sName);
232 iLX = iLX + iMaxName + 15;
233
234 // Player points
235 mC64.ITool().intToStrDec(scores[iGT][iPos].iPoints, sTmp);
236 fontScore->setPosition(iLX - (fontScore->getWidth(sTmp) - iMaxPoints), iY);
237 fontScore->render(sTmp);
238
239 iY = iY + SCORES_Y_SPACE;
240 }
241 // Change the position for the next scores
242 iX = iX + SCORES_X_SPACE;
243 iY = SCORES_Y;
244 }
245
246 return PME_LOOP;
247}
248
249Sint32 HoF::insert(Sint32 iGameType, Sint32 iPos, Sint32 iPoints, char* szName)
250{
251 int i;
252
253 // Before to insert the new player, we shift one position from the end to post
254 for(i = PME_HOF_SCORES - 1; i > iPos; i--)
255 {
256 scores[iGameType][i].iPoints = scores[iGameType][i - 1].iPoints;
257 strcpy(scores[iGameType][i].sName, scores[iGameType][i - 1].sName);
258 }
259
260 // Insert the new player
261 scores[iGameType][iPos].iPoints = iPoints;
262 strcpy(scores[iGameType][iPos].sName, szName);
263
264 return 0;
265}