2016 project-added to repo
[EroBeats.git] / Release / fileio.cpp
blob6ed9fddc1ba18abf8887fdfcbc4b88b594a98978
1 #include "FileIO.h"
3 #include "ScreenNames.h"
4 #include "RescourceKeys.h"
6 enum gameType { arcade = 1, life = 2 };
8 FileIO::FileIO()
10 password = "Test";
12 FileIO::FileIO(FileIO* fio)
14 rsc = fio->rsc;
15 password = "Test";
17 FileIO::FileIO(ResourceMaster* rscrc)
19 rsc = rscrc;
20 password = "Test";
24 FileIO::~FileIO()
28 //old methods
30 void FileIO::storeGameText(std::vector<std::string> item) {
31 rsc->gameText = item;
33 //problematic
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;
39 int counter = 0;
40 while (!saveFile.eof()) {
41 std::string buffer;
42 std::getline(saveFile, buffer);
43 if (buffer != "" && buffer != "\n" && buffer != " ") {
44 currentData.at(counter) = buffer;
45 std::cout << buffer << "\n";
46 counter++;
50 counter = 0;
51 while (counter < 3) {
52 if (line != counter + 1) {
53 tempFile << currentData.at(counter) + "\n";
55 else {
56 tempFile << std::to_string(data) + "\n";
58 counter++;
61 saveFile.close();
62 tempFile.close();
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;
72 int counter = 0;
73 while (!saveFile.eof()) {
74 std::string buffer;
75 std::getline(saveFile, buffer);
76 if (buffer != "" && buffer != "\n" && buffer != " ") {
77 currentData.at(counter) = buffer;
78 counter++;
79 std::cout << buffer << "\n";
82 std::cout << "skip";
83 counter = 0;
84 while (counter < 8) {
85 if (counter % 2 != gamemode - 1 || counter / 2 + 1 != line) {
86 tempFile << currentData.at(counter) + "\n";
88 else {
89 tempFile << std::to_string(data) + "\n";
91 counter++;
94 saveFile.close();
95 tempFile.close();
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;
104 langData.open(path);
106 std::string text;
107 std::vector<std::string> result;
108 while (std::getline(langData, text)) {
109 std::cout << text << "\n";
110 if (!text.empty()) {
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);
119 langData.close();
120 storeGameText(result);
121 return result;
124 int FileIO::loadUserData(int line) {
126 std::string storedData;
127 std::ifstream data;
128 data.open("Data/save.txt");
130 std::array<std::string, 3> currentData;
131 int counter = 0;
132 while (!data.eof()) {
133 std::string buffer;
134 std::getline(data, buffer);
135 if (buffer != "" && buffer != "\n" && buffer != " ") {
136 currentData.at(counter) = buffer;
137 std::cout << buffer << "\n";
138 counter++;
141 data.close();
143 int intStoredData;
144 std::stringstream convert(currentData.at(line - 1));
145 if (!(convert >> intStoredData)) {
146 intStoredData = -1;
147 std::cout << "err\n";
149 return intStoredData;
152 int FileIO::loadScoreData(int gamemode, int line) {
153 std::string storedData;
154 std::ifstream data;
155 data.open("Data/scores.txt");
156 std::array<std::string, 8> currentData;
157 int counter = 0;
159 while (!data.eof()) {
160 std::string buffer;
161 std::getline(data, buffer);
162 if (buffer != "" && buffer != "\n" && buffer != " ") {
163 currentData.at(counter) = buffer;
164 std::cout << buffer;
165 counter++;
168 std::cout << "\t";
170 data.close();
172 if (gamemode == arcade) {
173 line = (line - 1) * 2 + 1;
175 else if (gamemode == life) {
176 line = line * 2;
179 int intStoredData;
180 std::stringstream convert(currentData.at(line - 1));
181 if (!(convert >> intStoredData)) {
182 intStoredData = -1;
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);
238 return newTexture;
241 //new methods
243 //problematic
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"));
258 /**/
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());
272 unz_global_info ugi;
273 unzGetGlobalInfo(genericZip, &ugi);
275 SDL_Texture* specificText;
276 SDL_RWops* rw = NULL;
277 uLong i;
278 std::cout << "Extracting" << "\n";
279 for (i = 0; i < ugi.number_entry; i++) {
280 unz_file_info ufi;
281 char filename[256];
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);
288 if (err != UNZ_OK) {
289 printf("errPass\n");
291 err = unzReadCurrentFile(genericZip, buf, size_buf);
292 if (err < 0) {
293 printf("ErrReadZip\n");
295 rw = SDL_RWFromMem(buf, size_buf);
296 if (rw == NULL) {
297 printf("RW\n");
299 SDL_Surface* surf = IMG_Load_RW(rw, 1);
300 if (surf == NULL) {
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);
308 free(buf);
309 unzClose(genericZip);
310 return specificText;
312 unzGoToNextFile(genericZip);
316 std::vector<Mix_Music*> FileIO::extractBGMZip(std::string path) {
317 unzFile bgmZip = unzOpen(path.c_str());
318 unz_global_info ugi;
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;
325 uLong i;
326 std::cout << "Extracting" << "\n";
327 for (i = 0; i < ugi.number_entry; i++) {
328 unz_file_info ufi;
329 char filename[256];
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);
336 if (err != UNZ_OK)
338 printf("errPass\n");
341 err = unzReadCurrentFile(bgmZip, buf, size_buf);
342 if (err < 0) {
343 std::cout << "errunzBGM\n";
345 rw = SDL_RWFromMem(buf, size_buf);
346 if (rw == NULL) {
347 std::cout << "RW\n";
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);
368 unzClose(bgmZip);
369 return rtnVect;
372 std::vector<Mix_Chunk*> FileIO::extractSFXZip(std::string path) {
373 unzFile sfxZip = unzOpen(path.c_str());
374 unz_global_info ugi;
375 unzGetGlobalInfo(sfxZip, &ugi);
377 std::vector<Mix_Chunk*> rtrnVector(ugi.number_entry);
379 SDL_RWops* rw = NULL;
380 uLong i;
381 std::cout << "Extracting" << "\n";
382 for (i = 0; i < ugi.number_entry; i++) {
383 unz_file_info ufi;
384 char filename[256];
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);
391 if (err != UNZ_OK)
393 printf("errPass\n");
396 err = unzReadCurrentFile(sfxZip, buf, size_buf);
397 if (err < 0) {
398 std::cout << "errunzSFX\n";
400 rw = SDL_RWFromMem(buf, size_buf);
401 if (rw == NULL) {
402 std::cout << "RW\n";
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);
420 unzClose(sfxZip);
421 return rtrnVector;
424 std::vector<SDL_Texture*> FileIO::extractTexturesFromZip(std::string path) {
425 unzFile textureZip = unzOpen(path.c_str());
426 unz_global_info ugi;
427 unzGetGlobalInfo(textureZip, &ugi);
429 std::vector<SDL_Texture*> rtrnVect(ugi.number_entry);
431 SDL_RWops* rw = NULL;
432 uLong i;
433 std::cout << "Extracting" << "\n";
434 for (i = 0; i < ugi.number_entry; i++) {
435 unz_file_info ufi;
436 char filename[256];
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);
443 if (err != UNZ_OK)
445 printf("errPass\n");
448 err = unzReadCurrentFile(textureZip, buf, size_buf);
449 if (err < 0) {
450 std::cout << "errunzTxt\n";
452 rw = SDL_RWFromMem(buf, size_buf);
453 if (rw == NULL) {
454 std::cout << "RW\n";
456 SDL_Surface* surf = IMG_Load_RW(rw, 1);
457 if (surf == NULL) {
458 std::cout << "surf\n";
460 SDL_Texture* text = SDL_CreateTextureFromSurface(rsc->rendPtr, surf);
461 if (text == NULL) {
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);
481 free(buf);
483 unzGoToNextFile(textureZip);
485 unzClose(textureZip);
486 return rtrnVect;
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';
493 return fileName;
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);
513 std::string result;
515 while (std::getline(ss, result, delimiter)) {
516 int i = atoi(result.c_str());
517 internal.push_back(i);
519 return internal;
521 std::vector<std::string> FileIO::split(std::string str, char delimiter) {
522 std::vector<std::string> internal;
523 std::stringstream ss(str);
524 std::string result;
526 while (std::getline(ss, result, delimiter)) {
527 internal.push_back(result);
529 return internal;