3 #include "ScreenNames.h"
4 #include "RescourceKeys.h"
6 enum gameType
{ arcade
= 1, life
= 2 };
12 FileIO::FileIO(FileIO
* fio
)
17 FileIO::FileIO(ResourceMaster
* rscrc
)
30 void FileIO::storeGameText(std::vector
<std::string
> item
) {
34 void FileIO::saveUserData(int data
, int line
) {
35 std::ifstream
saveFile("data/save.txt");
36 std::ofstream
tempFile("data/temp.txt");
38 std::array
<std::string
,3> currentData
;
40 while (!saveFile
.eof()) {
42 std::getline(saveFile
, buffer
);
43 if (buffer
!= "" && buffer
!= "\n" && buffer
!= " ") {
44 currentData
.at(counter
) = buffer
;
45 std::cout
<< buffer
<< "\n";
52 if (line
!= counter
+ 1) {
53 tempFile
<< currentData
.at(counter
) + "\n";
56 tempFile
<< std::to_string(data
) + "\n";
64 std::remove("data/save.txt");
65 std::rename("data/temp.txt", "data/save.txt");
68 void FileIO::saveScoreData(int gamemode
, int data
, int line
) {
69 std::ifstream
saveFile("data/scores.txt");
70 std::ofstream
tempFile("data/temp.txt");
71 std::array
<std::string
, 8> currentData
;
73 while (!saveFile
.eof()) {
75 std::getline(saveFile
, buffer
);
76 if (buffer
!= "" && buffer
!= "\n" && buffer
!= " ") {
77 currentData
.at(counter
) = buffer
;
79 std::cout
<< buffer
<< "\n";
85 if (counter
% 2 != gamemode
- 1 || counter
/ 2 + 1 != line
) {
86 tempFile
<< currentData
.at(counter
) + "\n";
89 tempFile
<< std::to_string(data
) + "\n";
97 std::remove("data/scores.txt");
98 std::rename("data/temp.txt", "data/scores.txt");
101 std::vector
<std::string
> FileIO::loadLanguage(std::string path
)
103 std::ifstream langData
;
107 std::vector
<std::string
> result
;
108 while (std::getline(langData
, text
)) {
109 std::cout
<< text
<< "\n";
111 if (text
.length() == 1 && text
.at(0) != ' ') {
112 result
.push_back(text
);
114 else if (text
.at(0) != '/' && text
.at(1) != '/') {
115 result
.push_back(text
);
120 storeGameText(result
);
124 int FileIO::loadUserData(int line
) {
126 std::string storedData
;
128 data
.open("Data/save.txt");
130 std::array
<std::string
, 3> currentData
;
132 while (!data
.eof()) {
134 std::getline(data
, buffer
);
135 if (buffer
!= "" && buffer
!= "\n" && buffer
!= " ") {
136 currentData
.at(counter
) = buffer
;
137 std::cout
<< buffer
<< "\n";
144 std::stringstream
convert(currentData
.at(line
- 1));
145 if (!(convert
>> intStoredData
)) {
147 std::cout
<< "err\n";
149 return intStoredData
;
152 int FileIO::loadScoreData(int gamemode
, int line
) {
153 std::string storedData
;
155 data
.open("Data/scores.txt");
156 std::array
<std::string
, 8> currentData
;
159 while (!data
.eof()) {
161 std::getline(data
, buffer
);
162 if (buffer
!= "" && buffer
!= "\n" && buffer
!= " ") {
163 currentData
.at(counter
) = buffer
;
172 if (gamemode
== arcade
) {
173 line
= (line
- 1) * 2 + 1;
175 else if (gamemode
== life
) {
180 std::stringstream
convert(currentData
.at(line
- 1));
181 if (!(convert
>> intStoredData
)) {
183 std::cout
<< "err\n";
185 return intStoredData
;
189 void FileIO::loadMenuItems()
191 std::vector<SDL_Texture*> menuTextures;
193 for (int fileName = LangBG; fileName <= GenericBar; fileName++) {
194 SDL_Texture* sdlText = loadTexture("Art/" + std::to_string(fileName + 1) + ".png");
195 if (sdlText == NULL) {
196 std::cout << SDL_GetError() << " Number " << (fileName + 1) << ".png\n";
198 menuTextures.push_back(sdlText);
200 storeTextures(menuTextures);
203 void FileIO::loadMenuAnimations() {
204 std::vector<std::vector<SDL_Texture*>> menuAnimations;
206 for (int animationFolder = WhiteBlueBG; animationFolder <= WhiteBlueBG; animationFolder++) {
207 std::vector<SDL_Texture*> sdlTextList;
208 for (int i = 1; i <= 57; i++) {
209 sdlTextList.push_back(loadTexture("Art/" + std::to_string(animationFolder) + "/ (" + std::to_string(i) + ").png"));
211 if (sdlTextList.front() == NULL) {
212 std::cout << SDL_GetError();
214 menuAnimations.push_back(sdlTextList);
216 storeAnimations(menuAnimations);
220 void FileIO::loadGameAnimations()
222 std::vector< std::vector<SDL_Texture*>> gameAnimations;
225 void FileIO::loadGameGraphics()
227 std::vector<SDL_Texture*> gameGraphics;
230 SDL_Texture* FileIO::loadTexture(std::string path) {
232 SDL_Texture* newTexture = NULL;
233 SDL_Surface* loadedSurface = IMG_Load(path.c_str());
235 newTexture = SDL_CreateTextureFromSurface(rsc->rendPtr, loadedSurface);
236 SDL_FreeSurface(loadedSurface);
244 SDL_Texture
* FileIO::loadSpecificImage(std::string path
, int image
) {
245 return extractSpecificTexture(path
, image
);
247 std::vector
<SDL_Texture
*> FileIO::loadSpecificZip(std::string path
) {
248 return extractTexturesFromZip(path
);
250 void FileIO::loadBGZip() {
251 rsc
->gameBG
= extractTexturesFromZip("BG.zip");
253 void FileIO::loadTexturesZip() {
254 rsc
->gameGraphics
= extractTexturesFromZip("Textures.zip");
256 void FileIO::loadAnimationsZip() {
257 rsc
->gameAnimations
.push_back(extractTexturesFromZip("(0).zip"));
259 for (int i
= 1; i
<= 36; i
++) {
260 rsc
->gameAnimations
.push_back(extractTexturesFromZip(" (" + std::to_string(i
) + ").zip"));
263 void FileIO::loadBGMZip() {
264 rsc
->gameBGM
= extractBGMZip("BGM.zip");
266 void FileIO::loadSFXZip() {
267 rsc
->gameSFX
= extractSFXZip("SFX.zip");
270 SDL_Texture
* FileIO::extractSpecificTexture(std::string path
, int image
) {
271 unzFile genericZip
= unzOpen(path
.c_str());
273 unzGetGlobalInfo(genericZip
, &ugi
);
275 SDL_Texture
* specificText
;
276 SDL_RWops
* rw
= NULL
;
278 std::cout
<< "Extracting" << "\n";
279 for (i
= 0; i
< ugi
.number_entry
; i
++) {
282 unzGetCurrentFileInfo(genericZip
, &ufi
, filename
, sizeof(filename
), NULL
, 0, NULL
, 0);
283 std::string name
= getZipFileName(ufi
, genericZip
);
284 if (name
== std::to_string(image
)) {
285 uInt size_buf
= ufi
.uncompressed_size
;
286 void*buf
= (void*)malloc(size_buf
);
287 int err
= unzOpenCurrentFilePassword(genericZip
, password
);
291 err
= unzReadCurrentFile(genericZip
, buf
, size_buf
);
293 printf("ErrReadZip\n");
295 rw
= SDL_RWFromMem(buf
, size_buf
);
299 SDL_Surface
* surf
= IMG_Load_RW(rw
, 1);
301 std::cout
<< "surf\n";
303 specificText
= SDL_CreateTextureFromSurface(rsc
->rendPtr
, surf
);
304 if (specificText
== NULL
) {
305 std::cout
<< "text\n";
307 SDL_FreeSurface(surf
);
309 unzClose(genericZip
);
312 unzGoToNextFile(genericZip
);
316 std::vector
<Mix_Music
*> FileIO::extractBGMZip(std::string path
) {
317 unzFile bgmZip
= unzOpen(path
.c_str());
319 unzGetGlobalInfo(bgmZip
, &ugi
);
321 std::vector
<Mix_Music
*> rtnVect(ugi
.number_entry
);
322 std::vector
<Mix_Music
*>::iterator it
;
324 SDL_RWops
* rw
= NULL
;
326 std::cout
<< "Extracting" << "\n";
327 for (i
= 0; i
< ugi
.number_entry
; i
++) {
330 unzGetCurrentFileInfo(bgmZip
, &ufi
, filename
, sizeof(filename
), NULL
, 0, NULL
, 0);
332 uInt size_buf
= ufi
.uncompressed_size
;
333 //std::cout << size_buf << "\n";
334 void* buf
= (void*)malloc(size_buf
);
335 int err
= unzOpenCurrentFilePassword(bgmZip
, password
);
341 err
= unzReadCurrentFile(bgmZip
, buf
, size_buf
);
343 std::cout
<< "errunzBGM\n";
345 rw
= SDL_RWFromMem(buf
, size_buf
);
350 Mix_Music
* temp
= Mix_LoadMUS_RW(rw
, 1);
352 std::string name
= getZipFileName(ufi
, bgmZip
);
353 char chars
[] = "()-.wav";
355 for (unsigned int i
= 0; i
< strlen(chars
); ++i
)
357 // you need include <algorithm> to use general algorithms like std::remove()
358 name
.erase(std::remove(name
.begin(), name
.end(), chars
[i
]), name
.end());
360 int index
= stoi(name
) - 1;
362 std::cout
<< index
<< "\n";
364 it
= rtnVect
.begin() + index
;
365 rtnVect
.at(index
) = temp
;
366 unzGoToNextFile(bgmZip
);
372 std::vector
<Mix_Chunk
*> FileIO::extractSFXZip(std::string path
) {
373 unzFile sfxZip
= unzOpen(path
.c_str());
375 unzGetGlobalInfo(sfxZip
, &ugi
);
377 std::vector
<Mix_Chunk
*> rtrnVector(ugi
.number_entry
);
379 SDL_RWops
* rw
= NULL
;
381 std::cout
<< "Extracting" << "\n";
382 for (i
= 0; i
< ugi
.number_entry
; i
++) {
385 unzGetCurrentFileInfo(sfxZip
, &ufi
, filename
, sizeof(filename
), NULL
, 0, NULL
, 0);
387 uInt size_buf
= ufi
.uncompressed_size
;
388 //std::cout << size_buf << "\n";
389 void* buf
= (void*)malloc(size_buf
);
390 int err
= unzOpenCurrentFilePassword(sfxZip
, password
);
396 err
= unzReadCurrentFile(sfxZip
, buf
, size_buf
);
398 std::cout
<< "errunzSFX\n";
400 rw
= SDL_RWFromMem(buf
, size_buf
);
404 Mix_Chunk
* temp
= Mix_LoadWAV_RW(rw
, 1);
405 std::string name
= getZipFileName(ufi
, sfxZip
);
406 char chars
[] = "()-.wav";
408 for (unsigned int i
= 0; i
< strlen(chars
); ++i
)
410 // you need include <algorithm> to use general algorithms like std::remove()
411 name
.erase(std::remove(name
.begin(), name
.end(), chars
[i
]), name
.end());
413 int index
= stoi(name
) - 1;
415 std::cout
<< index
<< "\n";
416 rtrnVector
.at(index
) = (temp
);
418 unzGoToNextFile(sfxZip
);
424 std::vector
<SDL_Texture
*> FileIO::extractTexturesFromZip(std::string path
) {
425 unzFile textureZip
= unzOpen(path
.c_str());
427 unzGetGlobalInfo(textureZip
, &ugi
);
429 std::vector
<SDL_Texture
*> rtrnVect(ugi
.number_entry
);
431 SDL_RWops
* rw
= NULL
;
433 std::cout
<< "Extracting" << "\n";
434 for (i
= 0; i
< ugi
.number_entry
; i
++) {
437 unzGetCurrentFileInfo(textureZip
, &ufi
, filename
, sizeof(filename
), NULL
, 0, NULL
, 0);
439 uInt size_buf
= ufi
.uncompressed_size
;
440 //std::cout << size_buf << "\n";
441 void* buf
= (void*)malloc(size_buf
);
442 int err
= unzOpenCurrentFilePassword(textureZip
, password
);
448 err
= unzReadCurrentFile(textureZip
, buf
, size_buf
);
450 std::cout
<< "errunzTxt\n";
452 rw
= SDL_RWFromMem(buf
, size_buf
);
456 SDL_Surface
* surf
= IMG_Load_RW(rw
, 1);
458 std::cout
<< "surf\n";
460 SDL_Texture
* text
= SDL_CreateTextureFromSurface(rsc
->rendPtr
, surf
);
462 std::cout
<< "text\n";
465 std::string name
= getZipFileName(ufi
, textureZip
);
467 char chars
[] = "()-.png";
469 for (unsigned int i
= 0; i
< strlen(chars
); ++i
)
471 // you need include <algorithm> to use general algorithms like std::remove()
472 name
.erase(std::remove(name
.begin(), name
.end(), chars
[i
]), name
.end());
474 int index
= stoi(name
) - 1;
476 std::cout
<< index
<< "\n";
478 rtrnVect
.at(index
) = text
;
480 SDL_FreeSurface(surf
);
483 unzGoToNextFile(textureZip
);
485 unzClose(textureZip
);
489 char* FileIO::getZipFileName(unz_file_info ufi
, unzFile uf
) {
490 char *fileName
= (char *)malloc(ufi
.size_filename
+ 1);
491 unzGetCurrentFileInfo(uf
, &ufi
, fileName
, ufi
.size_filename
+ 1, NULL
, 0, NULL
, 0);
492 fileName
[ufi
.size_filename
] = '\0';
496 void FileIO::eraseUserData()
498 for (int line
= 1; line
<= 3; line
++) {
499 saveUserData(0, line
);
500 saveUserData(0, line
);
501 saveUserData(0, line
);
503 for (int line
= 1; line
<= 4; line
++) {
504 std::cout
<< "skipyyy";
505 saveScoreData(arcade
, 0, line
);
506 saveScoreData(life
, 0, line
);
510 std::vector
<int> FileIO::splitInt(std::string str
, char delimiter
) {
511 std::vector
<int> internal
;
512 std::stringstream
ss(str
);
515 while (std::getline(ss
, result
, delimiter
)) {
516 int i
= atoi(result
.c_str());
517 internal
.push_back(i
);
521 std::vector
<std::string
> FileIO::split(std::string str
, char delimiter
) {
522 std::vector
<std::string
> internal
;
523 std::stringstream
ss(str
);
526 while (std::getline(ss
, result
, delimiter
)) {
527 internal
.push_back(result
);