-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpriteManager.c
141 lines (117 loc) · 3.61 KB
/
SpriteManager.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "SpriteManager.h"
int getFileSize(char* fileName)
{
FILE* pFile;
pFile = fopen(fileName, "r");
// checking if the file exist or not
if (pFile == NULL) {
printf("File Not Found!\n");
return -1;
}
fseek(pFile, 0, SEEK_END);
// calculating the size of the file
long int fileSize = ftell(pFile);
fclose(pFile);
return fileSize;
}
char* getFilePathFromDexNumber(int dexNumber)
{
char *filePath, *sizeAsString;
int filePathSize;
//3 digits for the dex number
filePathSize = strlen(ASCIISPRITEDIR) + DEXNUMBERDIGITS + strlen(ASCIIEXTENSION) + 1;
sizeAsString = (char*) malloc((sizeof(char)*DEXNUMBERDIGITS)+1);
sprintf(sizeAsString, "%03d", dexNumber);
filePath = (char*) malloc(sizeof(char)*filePathSize);
//Directory + Base File Name + Vector Size + Extension
strcpy(filePath, ASCIISPRITEDIR);
strcat(filePath, sizeAsString);
strcat(filePath, ASCIIEXTENSION);
#ifdef DEBUG
printf("Sprite file Path: %s\n", filePath);
#endif
free(sizeAsString);
return filePath;
}
char* loadASCII(int dexNumber)
{
FILE* pFile;
char *fileName, *spriteASCII;
int fileSize;
fileName = getFilePathFromDexNumber(dexNumber);
fileSize = getFileSize(fileName)/sizeof(char);
pFile = fopen(fileName, "r");
size_t result;
// checking if the file exist or not
if (pFile == NULL) {
printf("Dex Number: %d\n", dexNumber);
printf("FileName: %s\n", fileName);
printf("File Not Found!\n");
exit(EXIT_FAILURE);
}
spriteASCII = (char*) malloc(sizeof(char)*(fileSize+1));
result = fread(spriteASCII, sizeof(char), fileSize, pFile);
if (result != fileSize)
{
printf("Reading error\n");
exit(EXIT_FAILURE);
}
spriteASCII[result] = '\0';
// closing the file
fclose(pFile);
free(fileName);
return spriteASCII;
}
void printBoxLine(pokemon_t **pokeBoxLine, int lineSize)
{
char** spritesASCII;
int currentIndex, startIndex = 0;
spritesASCII = (char**) malloc(sizeof(char*)*lineSize);
int dexNumber, hasFinished = 0;
for(int i = 0; i < lineSize; ++i)
{
if(pokeBoxLine[i] != NULL)
dexNumber = pokeBoxLine[i]->dexNumber-1;
else
dexNumber = -1;
spritesASCII[i] = loadASCII(dexNumber);
}
while(!hasFinished)
{
for(int i = 0; i < lineSize; ++i)
{
currentIndex = startIndex;
while(spritesASCII[i][currentIndex] != '\n' && spritesASCII[i][currentIndex] != '\r' && spritesASCII[i][currentIndex] != '\0')
{
printf("%c", spritesASCII[i][currentIndex]);
if(spritesASCII[i][currentIndex] != '\0')
currentIndex++;
else
hasFinished = 1;
}
printf("\t");
}
if(spritesASCII[lineSize-1][currentIndex] == '\0')
startIndex = currentIndex;
else if(spritesASCII[lineSize-1][currentIndex] == '\r')
currentIndex++;
if(spritesASCII[lineSize-1][currentIndex] == '\n')
startIndex = ++currentIndex;
if(spritesASCII[lineSize-1][currentIndex] == '\0')
hasFinished = 1;
printf("\n");
}
for(int i = 0; i < lineSize; ++i)
{
if(pokeBoxLine[i] != NULL)
printf("\t\t\t #%03d - %-30s\t\t", pokeBoxLine[i]->dexNumber, pokeBoxLine[i]->species);
else
printf("\t\t\t\t\t\t\t\t\t");
}
printf("\n");
for(int i = 0; i < lineSize; ++i)
{
free(spritesASCII[i]);
}
free(spritesASCII);
}