-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacker.c
157 lines (143 loc) · 4.88 KB
/
packer.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Define the structs
typedef struct Animation Animation;
typedef struct GetAnimationReturnValues GetAnimationReturnValues;
/**
* Structs
* =======
* GetAnimationReturnValues
* ========================
* Contains the return values for the GetAnimations function
*
* Animation* animations - The array of animations
* int numAnimations - The number of animations
*/
struct GetAnimationReturnValues {
Animation* animations;
int numAnimations;
int numOtherNumber;
};
/*
* Animation
* =========
* Contains the name of the animation and the length of the animation
*
* char* name - The name of the animation
* int length - The length of the animation
*/
struct Animation {
char* name;
int length;
};
/**
* WriteFile()
* ===========
* Writes the given data to the given file
*
* Inputs:
* char* filename - The name of the file to write to
* GetAnimationReturnValues animationData - The array of animations to write to the file
* char* name - The name of the sprite sheet
* int frameWidth - The width of each frame
* int frameHeight - The height of each frame
* int framesPerRow - The number of frames per row
*/
void WriteFile(char* filename, GetAnimationReturnValues animationData, char* name, int frameWidth, int frameHeight, int framesPerRow) {
// Some math to figure out the size of the image and other needed numbers
int framesPerColumn = ceil(animationData.numAnimations / framesPerRow);
int imageWidth = frameWidth * framesPerRow;
int imageHeight = frameHeight * framesPerColumn;
// Open the file for writing
FILE* file = fopen(filename, "w");
fprintf(file, "%s\nsize: (%d, %d)\nformat: RGBA8888\nfilter: Nearest,Nearest\nrepeast: none", name, imageWidth, imageHeight);
// Loop vars
int animationNumber = 0;
int index = 0;
int k = -1;
int x;
int y;
// Loop through all the animations
for (int i = 0; i < animationData.numAnimations; i++) {
if ((i % framesPerRow) == 0) {
k++;
} if (animationData.animations[animationNumber].length == index) {
animationNumber++;
index = 0;
}
x = (i % framesPerRow) * frameWidth;
y = k * frameHeight;
fprintf(file, "\n%s:\n rotate: false\n xy: (%d, %d)\n size: (%d, %d)\n orig: (%d, %d)\n offset: 0,0\n index: %d", animationData.animations[animationNumber].name, x, y, frameWidth, frameHeight, frameWidth, frameHeight, index);
index++;
}
// Close the file
fclose(file);
// Free the memory used by the animation data
for (int i = 0; i < animationData.numOtherNumber; i++) {
free(animationData.animations[i].name);
}
free(animationData.animations);
return;
}
/**
* GetAnimations()
* ===============
* Asks the user for animation names and the length of animations, until there is a blank input
*
* Inputs:
* Nothing
*
*
* Outputs:
* Array of Structs containing the animation names and lengths
*/
GetAnimationReturnValues GetAnimations() {
// Create a pointer to an array of animations
Animation* animations = malloc(sizeof(Animation));
int numAnimations = 0;
int numFrames = 0;
// Loop until the user enters a blank name
while (1) {
// Get the name of the animation
char* name = malloc(26);
name[0] = '\0';
printf("Enter the name of the animation (Max 25 Characters): ");
scanf("%25s", name);
// If the name is blank, break the loop
if (name[0] == '\0' || name[0] == '\n') {
GetAnimationReturnValues returnValues;
returnValues.animations = animations;
returnValues.numAnimations = numFrames;
returnValues.numOtherNumber = numAnimations;
return returnValues;
}
// Get the length of the animation
int length = 0;
printf("Enter the length of the animation: ");
scanf("%d", &length);
// Create a new animation struct
Animation newAnimation;
newAnimation.name = name;
newAnimation.length = length;
// Add the new animation to the array
numAnimations++;
// Add length to numFrames
numFrames += length;
animations = realloc(animations, numAnimations * sizeof(Animation));
animations[numAnimations - 1] = newAnimation;
}
}
// Main function
int main(int argc, char* argv[]) {
// Basic error checking for params
if (argc != 6) {
printf("Usage: %s <name> <output file> <frame width> <frame height> <frames per row>\n", argv[0]);
return 1;
}
// Gets all the animations provided by the user from the command line
GetAnimationReturnValues ani = GetAnimations();
// Make and write to the file (also does some math to figure out numbers)
WriteFile(argv[2], ani, argv[1], atoi(argv[3]), atoi(argv[4]), atoi(argv[5]));
return 0;
}