/*
|
/*
|
Per Lenander 2008
|
Per Lenander 2008
|
|
|
Aniconv tool:
|
Aniconv tool:
|
-------------
|
-------------
|
|
|
The tool utilize SDL/SDL_image for graphics conversion
|
The tool utilize SDL/SDL_image for graphics conversion
|
*/
|
*/
|
|
|
#include <SDL/SDL.h>
|
#include <SDL/SDL.h>
|
#include <SDL/SDL_image.h>
|
#include <SDL/SDL_image.h>
|
|
|
#define SCREEN_WIDTH 1024
|
#define SCREEN_WIDTH 1024
|
#define SCREEN_HEIGHT 1500
|
#define SCREEN_HEIGHT 1500
|
#define SCREEN_DEPTH 32
|
#define SCREEN_DEPTH 32
|
|
|
#include <sstream>
|
#include <sstream>
|
#include <iostream>
|
#include <iostream>
|
#include <fstream>
|
#include <fstream>
|
#include <cstdlib>
|
#include <cstdlib>
|
|
|
using namespace std;
|
using namespace std;
|
|
|
SDL_Surface *screen;
|
SDL_Surface *screen;
|
|
|
int spriteW = 0;
|
int spriteW = 0;
|
int spriteH = 0;
|
int spriteH = 0;
|
|
|
/* Ordinary inits */
|
/* Ordinary inits */
|
int InitSDL(void)
|
int InitSDL(void)
|
{
|
{
|
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
{
|
{
|
cerr << "Unable to init SDL: " << SDL_GetError() << endl;
|
cerr << "Unable to init SDL: " << SDL_GetError() << endl;
|
return 1;
|
return 1;
|
}
|
}
|
|
|
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_HWSURFACE | SDL_DOUBLEBUF);
|
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_HWSURFACE | SDL_DOUBLEBUF);
|
|
|
if (!screen)
|
if (!screen)
|
{
|
{
|
cerr << "Unable to set video: " << SDL_GetError() << endl;
|
cerr << "Unable to set video: " << SDL_GetError() << endl;
|
return 1;
|
return 1;
|
}
|
}
|
// make sure SDL cleans up before exit
|
// make sure SDL cleans up before exit
|
atexit(SDL_Quit);
|
atexit(SDL_Quit);
|
|
|
return 0;
|
return 0;
|
};
|
};
|
|
|
/* Loads the spritesheet (or any image, but this is the only thing it is used for) */
|
/* Loads the spritesheet (or any image, but this is the only thing it is used for) */
|
SDL_Surface * LoadImage(std::string s)
|
SDL_Surface * LoadImage(std::string s)
|
{
|
{
|
SDL_Surface *loaded = NULL;
|
SDL_Surface *loaded = NULL;
|
|
|
loaded = IMG_Load(s.c_str());
|
loaded = IMG_Load(s.c_str());
|
|
|
if(!loaded) return NULL;
|
if(!loaded) return NULL;
|
|
|
SDL_Surface *optimized = NULL;
|
SDL_Surface *optimized = NULL;
|
|
|
optimized = SDL_DisplayFormatAlpha(loaded);
|
optimized = SDL_DisplayFormatAlpha(loaded);
|
|
|
SDL_FreeSurface(loaded);
|
SDL_FreeSurface(loaded);
|
|
|
if(!optimized) return NULL;
|
if(!optimized) return NULL;
|
|
|
return optimized;
|
return optimized;
|
};
|
};
|
|
|
SDL_Color GetPixel ( SDL_Surface* pSurface , int x , int y )
|
SDL_Color GetPixel ( SDL_Surface* pSurface , int x , int y )
|
{
|
{
|
SDL_Color color ;
|
SDL_Color color ;
|
Uint32 col = 0 ;
|
Uint32 col = 0 ;
|
|
|
//determine position
|
//determine position
|
char* pPosition = ( char* ) pSurface->pixels ;
|
char* pPosition = ( char* ) pSurface->pixels ;
|
|
|
//offset by y
|
//offset by y
|
pPosition += ( pSurface->pitch * y ) ;
|
pPosition += ( pSurface->pitch * y ) ;
|
|
|
//offset by x
|
//offset by x
|
pPosition += ( pSurface->format->BytesPerPixel * x ) ;
|
pPosition += ( pSurface->format->BytesPerPixel * x ) ;
|
|
|
//copy pixel data
|
//copy pixel data
|
memcpy ( &col , pPosition , pSurface->format->BytesPerPixel ) ;
|
memcpy ( &col , pPosition , pSurface->format->BytesPerPixel ) ;
|
|
|
//convert color
|
//convert color
|
SDL_GetRGB ( col , pSurface->format , &color.r , &color.g , &color.b ) ;
|
SDL_GetRGB ( col , pSurface->format , &color.r , &color.g , &color.b ) ;
|
return ( color ) ;
|
return ( color ) ;
|
}
|
}
|
|
|
int main ( int argc, char** argv )
|
int main ( int argc, char** argv )
|
{
|
{
|
if( argc < 2 )
|
if( argc < 2 )
|
{
|
{
|
cout << "Usage: " << argv[0] << " filename.<jpg/png/bmp> [bpp]" << endl;
|
cout << "Usage: " << argv[0] << " filename.<jpg/png/bmp> [bpp]" << endl;
|
return 1;
|
return 1;
|
}
|
}
|
|
|
if( InitSDL() )
|
if( InitSDL() )
|
return 1;
|
return 1;
|
cout << "SDL successfully initialized." << endl;
|
cout << "SDL successfully initialized." << endl;
|
|
|
SDL_Surface *spritesheet = LoadImage( argv[1] );
|
SDL_Surface *spritesheet = LoadImage( argv[1] );
|
if( spritesheet == NULL )
|
if( spritesheet == NULL )
|
{
|
{
|
cout << "Couldn't load " << argv[1] << "!" << endl;
|
cout << "Couldn't load " << argv[1] << "!" << endl;
|
return 1;
|
return 1;
|
}
|
}
|
|
|
/* Open an output file */
|
/* Open an output file */
|
string filename(argv[1]);
|
string filename(argv[1]);
|
filename += ".h";
|
filename += ".h";
|
ofstream output(filename.c_str(),ofstream::out);
|
ofstream output(filename.c_str(),ofstream::out);
|
if( !output )
|
if( !output )
|
{
|
{
|
cout << "Couldn't open output file!" << endl;
|
cout << "Couldn't open output file!" << endl;
|
return 1;
|
return 1;
|
}
|
}
|
|
|
int outputBpp = 8;
|
int outputBpp = 8;
|
if(argc >= 3)
|
if(argc >= 3)
|
{
|
{
|
stringstream ss(argv[2]);
|
stringstream ss(argv[2]);
|
ss >> outputBpp;
|
ss >> outputBpp;
|
|
|
if(outputBpp != 8 && outputBpp != 16 && outputBpp != 24 && outputBpp != 32)
|
if(outputBpp != 8 && outputBpp != 16 && outputBpp != 24 && outputBpp != 32)
|
{
|
{
|
cout << "Mode: '" << argv[2] << "' not supported, choose between 8, 16, 24, and 32" << endl;
|
cout << "Mode: '" << argv[2] << "' not supported, choose between 8, 16, 24, and 32" << endl;
|
return 1;
|
return 1;
|
}
|
}
|
else if(outputBpp == 8 && spritesheet->w % 4)
|
else if(outputBpp == 8 && spritesheet->w % 4)
|
{
|
{
|
cout << "Mode: 8bpp requires the width to be divisible by 4!" << endl;
|
cout << "Mode: 8bpp requires the width to be divisible by 4!" << endl;
|
return 1;
|
return 1;
|
}
|
}
|
else if(outputBpp == 16 && spritesheet->w % 2)
|
else if(outputBpp == 16 && spritesheet->w % 2)
|
{
|
{
|
cout << "Mode: 16bpp requires the width to be divisible by 2!" << endl;
|
cout << "Mode: 16bpp requires the width to be divisible by 2!" << endl;
|
return 1;
|
return 1;
|
}
|
}
|
}
|
}
|
|
|
int stride = 1;
|
int stride = 1;
|
if(outputBpp == 8)
|
if(outputBpp == 8)
|
stride = 4;
|
stride = 4;
|
else if(outputBpp == 16)
|
else if(outputBpp == 16)
|
stride = 2;
|
stride = 2;
|
|
|
filename = filename.substr(0, filename.find('.'));
|
filename = filename.substr(0, filename.find('.'));
|
|
|
output << "/* Sprite definition */" << endl << endl;
|
output << "/* Sprite definition */" << endl << endl;
|
output << "/* size: " << spritesheet->w << " * " << spritesheet->h << " at " << outputBpp << "BPP */" << endl << endl;
|
output << "/* size: " << spritesheet->w << " * " << spritesheet->h << " at " << outputBpp << "BPP */" << endl << endl;
|
output << "#ifndef " << filename << "_H" << endl;
|
output << "#ifndef " << filename << "_H" << endl;
|
output << "#define " << filename << "_H" << endl;
|
output << "#define " << filename << "_H" << endl;
|
|
|
output << "unsigned int " << filename << "[] = {" << endl;
|
output << "unsigned int " << filename << "[] = {" << endl;
|
|
|
for(int y = 0; y < spritesheet->h; y++)
|
for(int y = 0; y < spritesheet->h; y++)
|
{
|
{
|
for(int x = 0; x < spritesheet->w; x+=stride)
|
for(int x = 0; x < spritesheet->w; x+=stride)
|
{
|
{
|
unsigned int pixel = 0;
|
unsigned int pixel = 0;
|
for(int s = 0; s < stride; s++)
|
for(int s = 0; s < stride; s++)
|
{
|
{
|
SDL_Color c = GetPixel(spritesheet, x+s, y);
|
SDL_Color c = GetPixel(spritesheet, x+s, y);
|
|
|
if(outputBpp == 8)
|
if(outputBpp == 8)
|
{
|
{
|
pixel += (unsigned char)(0.3 * c.r + 0.59 * c.g + 0.11 * c.b);
|
pixel += (unsigned char)(0.3 * c.r + 0.59 * c.g + 0.11 * c.b);
|
if(s != 3) pixel = pixel << 8;
|
if(s != 3) pixel = pixel << 8;
|
}
|
}
|
else if(outputBpp == 16)
|
else if(outputBpp == 16)
|
{
|
{
|
c.r = c.r >> 3;
|
c.r = c.r >> 3;
|
c.g = c.g >> 2;
|
c.g = c.g >> 2;
|
c.b = c.b >> 3;
|
c.b = c.b >> 3;
|
pixel += (c.r << 11) | (c.g << 5) | c.b;
|
pixel += (c.r << 11) | (c.g << 5) | c.b;
|
if(s != 1) pixel = pixel << 16;
|
if(s != 1) pixel = pixel << 16;
|
}
|
}
|
else
|
else
|
pixel = (c.r << 16) | (c.g << 8) | c.b;
|
pixel = (c.r << 16) | (c.g << 8) | c.b;
|
}
|
}
|
output << pixel << "u, ";
|
output << pixel << "u, ";
|
}
|
}
|
output << endl;
|
output << endl;
|
}
|
}
|
|
|
output << "};" << endl << endl;
|
output << "};" << endl << endl;
|
output << "#endif // " << filename << "_H" << endl;
|
output << "#endif // " << filename << "_H" << endl;
|
|
|
output.close();
|
output.close();
|
SDL_FreeSurface( spritesheet );
|
SDL_FreeSurface( spritesheet );
|
|
|
return 0;
|
return 0;
|
}
|
}
|
|
|