All pastes #636380 Raw Edit

Unnamed

public text v1 · immutable
#636380 ·published 2007-07-27 21:12 UTC
rendered paste body
#include <allegro.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define USAGE "Store all the bitmap-frames using <basename>_<frame#>.bmp \n\
	then invoke: <exe> <basename> <n_frames> <n_columns> <frame_width> \n\
	the output will be a .bmp file named <basename> with all the frames \n\
	aligned using x<n_columns>. <n_columns> must be a multipel of <n_frames>.\n\
	max number of frames (<n_frames>) is 999. \n\
	DO NOT USE ZEROES AT THE BEGINNING OF <n_frames>!\n\
	Frames must be quare"

void blit_to_chart(BITMAP *chart, BITMAP *src, int width, \
		int columns, int frame){
	
	int x = (frame % columns) * width;
	int y = (frame / columns) * width;
	
	blit(src, chart, 0, 0, x, y, width, width);
}

int main(int argc, char **argv){
	char *basename;
	char *currentfile;
	int frames;
	int width;
	int columns;
	int n;
	
	BITMAP *chart;
	BITMAP *tmp;
	tmp = NULL;

	allegro_init();
	set_color_depth(24);
	set_gfx_mode(GFX_AUTODETECT_WINDOWED, 50, 50, 0, 0);
	if(argc != 5){
		printf(USAGE);
		return 0;
	}

	frames = atoi(argv[2]);
	columns = atoi(argv[3]);
	width = atoi(argv[4]);
	n = strlen(argv[1]); /* argv[1] = <basename> */
	
	if(1 > frames || frames > 999 ||  columns < 1 || width < 1 || n < 1 || (frames % columns) != 0){
		printf(USAGE);
		return 0;
	}
	
	basename = (char*)malloc(n+1);
	sprintf(basename, "%s", argv[1]);
	
	/* Just make sure all went well...*/
	if(strcmp(basename, argv[1]) != 0){
		free(basename);
		return 0;
	}
	
	/* n+9 to compensate for "_XXX.bmp\0" */
	currentfile = (char*)malloc(n+20);
	chart = create_bitmap(width*columns, width*(frames/columns));

	for(n=0; n<frames; n++){
		sprintf(currentfile, "%s_%i.bmp", basename, n+1);
		
		tmp = load_bmp(currentfile, NULL);

		if(tmp == NULL){
			destroy_bitmap(chart);
			destroy_bitmap(tmp);
			
			free(currentfile);
			free(basename);
			
			allegro_exit();
			return 0;
		}
		blit_to_chart(chart, tmp, width, columns, n);
	}
	
	sprintf(currentfile, "%s.bmp", basename);
	save_bitmap(currentfile, chart, NULL);
	
	destroy_bitmap(chart);
	destroy_bitmap(tmp);
	
	free(currentfile);
	free(basename);
	
	allegro_exit();
	return 0;
}END_OF_MAIN();