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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
#include <SDL.h>
#include <SDL_image.h>
#include <stdlib.h>
/*
* this program loads a few picture files and creates a file with information
* for the program for the scroller
* the input pictures must be .PNG
*
* the data file should allow for any number of layers, each layer can be up to 480 pixel high
* but can have a different width
* you must specify the speed by which the image is moved with 2 numbers. the speed
* will actually be the fraction of number one and two
*
* so you can create a standing background image by specifying a 0 for speed
*/
/*
* ok this structure contains the data for one layer
*/
typedef struct layer {
unsigned int xpos, ypos;
unsigned int width, height;
unsigned int numerator, denominator;
unsigned int xrepeat;
SDL_Surface * colors;
SDL_Surface * mask;
struct layer * next;
} layer;
/*
* the layer list anchor
*/
layer * layer_ancor = NULL;
Uint8 get_color(SDL_Surface *s, int x, int y) {
return ((Uint8*)s->pixels)[y*s->pitch+x];
}
Uint8 get_alpha(SDL_Surface *s, int x, int y) {
return s->format->palette->colors[((Uint8*)s->pixels)[y*s->pitch+x]].r;
}
void write_palette(FILE *out, SDL_Surface *s) {
int i;
Uint8 c = s->format->palette->ncolors - 1;
fwrite(&c, 1, 1, out);
for (i = 0; i < s->format->palette->ncolors; i++) {
fwrite(&s->format->palette->colors[i].r, 1, 1, out);
fwrite(&s->format->palette->colors[i].g, 1, 1, out);
fwrite(&s->format->palette->colors[i].b, 1, 1, out);
}
}
void write_uint(unsigned int x, FILE * out) {
unsigned char c;
c = x & 0xff;
fwrite(&c, 1, 1, out);
c = x >> 8;
fwrite(&c, 1, 1, out);
}
/*
* writes on layer to the file
*/
void write_layer(layer * l, FILE * out) {
int x, y;
static int first = 0;
Uint8 b;
write_uint(l->xpos, out);
write_uint(l->ypos, out);
write_uint(l->width, out);
write_uint(l->height, out);
write_uint(l->numerator, out);
write_uint(l->denominator, out);
write_uint(l->xrepeat, out);
write_palette(out, l->colors);
for (y = 0; y < l->height; y++)
for (x = 0; x < l->width; x++) {
b = get_color(l->colors, x, y);
fwrite(&b, 1, 1, out);
if (first) {
b = get_alpha(l->mask, x, y);
fwrite(&b, 1, 1, out);
}
}
first = 1;
}
void save(layer * l, FILE * o) {
if (l) {
save(l->next, o);
write_layer(l, o);
}
}
/*
*
* scroller number_of_layers tower_pos tower_spd_num/tower_spd_den \
* [ pic_file mask_file xpos/ypos/xrepeat spd_num/spd_den ]...
*/
int main(int argv, char* args[]) {
int l;
int layers; // number of layers
unsigned int towerpos;
unsigned int towerspeed_num, towerspeed_den;
if (argv < 3) return 1;
printf("start\n");
sscanf(args[1], "%i", &layers);
printf("generating %i layers\n", layers);
sscanf(args[2], "%i", &towerpos);
sscanf(args[3], "%i/%i", &towerspeed_num, &towerspeed_den);
printf("putting tower as layer %i with speed %i/%i\n", towerpos, towerspeed_num, towerspeed_den);
for (l = 0; l < layers; l++) {
layer * la = (layer*)malloc(sizeof(layer));
if (l == towerpos) printf("tower, speed %i/%i\n", towerspeed_num, towerspeed_den);
la->colors = IMG_LoadPNG_RW(SDL_RWFromFile(args[4+4*l], "rb"));
la->mask = IMG_LoadPNG_RW(SDL_RWFromFile(args[4+4*l+1], "rb"));
la->width = la->colors->w;
la->height = la->colors->h;
sscanf(args[4+4*l+2], "%i/%i/%i", &la->xpos, &la->ypos, &la->xrepeat);
sscanf(args[4+4*l+3], "%i/%i", &la->numerator, &la->denominator);
printf("image %s, size (%i,%i), at (%i,%i), speed %i/%i, repeat %i\n", args[4+4*l],
la->width, la->height,
la->xpos, la->ypos,
la->numerator, la->denominator,
la->xrepeat);
la->next = layer_ancor;
layer_ancor = la;
}
printf("\n saving...\n");
FILE * o = fopen("scroller.dat", "wb");
fwrite(&layers, 1, 1, o);
fwrite(&towerpos, 1, 1, o);
write_uint(towerspeed_num, o);
write_uint(towerspeed_den, o);
save(layer_ancor, o);
return 0;
}
|