libpsxsave/psxicontool.cpp

93 lines
2.7 KiB
C++
Raw Permalink Normal View History

2010-08-10 13:40:01 +00:00
#include "psxicontool.h"
#include <iostream>
#include <fstream>
using namespace std;
PSXIconTool::PSXIconTool() {
this->error[0] = 0;
}
bool PSXIconTool::loadMCS( char* filename ) {
fstream savefile;
savefile.open( filename, ios::in );
if( !savefile.is_open() ) {
sprintf( this->error, "Unable to open file \"%s\" for reading", filename );
return false;
}
savefile.read( reinterpret_cast<char*>( &PSX_Save ), sizeof( _PSX_Save ) );
savefile.close();
// Convert icon data
for( int i = 0; i < 16; i++ ) {
icon.palette[i].red = ( PSX_Save.save_header.palette[i][0] & 31 ) << 3;
icon.palette[i].green = ( ( PSX_Save.save_header.palette[i][0] & 224 ) >> 2 ) + ( ( PSX_Save.save_header.palette[i][1] & 3 ) << 6 );
icon.palette[i].blue = ( PSX_Save.save_header.palette[i][1] & 124 ) << 1;
}
for( int y = 0; y < 16; y++ ) {
for( int x = 0; x < 8; x++ ) {
icon.pixels[y][x * 2] = PSX_Save.save_header.icons[0][y][x] & 15;
icon.pixels[y][x * 2 + 1] = ( PSX_Save.save_header.icons[0][y][x] & 240 ) >> 4;
}
}
}
bool PSXIconTool::exportXPM( char* filename ) {
FILE *xpm = fopen( filename, "w" );
if( xpm < 0 ) {
sprintf( this->error, "Unable to open file \"%s\" for writing", filename );
return false;
}
fprintf( xpm, "/* XPM */\n" );
fprintf( xpm, "static char *psx[] = {\n" );
fprintf( xpm, "/* columns rows colors chars-per-pixel */\n" );
fprintf( xpm, "\"16 16 16 1\",\n" );
// Palette Conversion
char palette[17] = " .XoO+@#$%&*=-;:";
for( int i = 0; i < 16; i++ ) {
fprintf( xpm, "\"%c c #%.2x%.2x%.2x\",\n", palette[i], icon.palette[i].red, icon.palette[i].green, icon.palette[i].blue );
}
fprintf( xpm, "/* pixels */\n" );
// Pixel Conversion
for( int y = 0; y < 16; y++ ) {
fprintf( xpm, "\"" );
for( int x = 0; x < 16; x++ ) {
fprintf( xpm, "%c", palette[ icon.pixels[y][x] ] );
}
if( y < 15 ) {
fprintf( xpm, "\",\n" );
} else {
fprintf( xpm, "\"\n}" );
}
}
fclose( xpm );
return true;
}
char* PSXIconTool::errorMessage() {
return error;
}
void PSXIconTool::printInfo() {
char* var;
printf( "Block Type: %d\n", PSX_Save.block_header.block_type );
printf( "Save Size: %d\n", PSX_Save.block_header.save_size );
printf( "Next Block: %d\n", PSX_Save.block_header.next_block );
strncpy( var, PSX_Save.block_header.territory_code, 2 ); var[2] = 0;
printf( "Territory Code: %s\n", var );
strncpy( var, PSX_Save.block_header.license_code, 10 ); var[10] = 0;
printf( "License Code: %s\n", var );
strncpy( var, PSX_Save.block_header.save_code, 8 ); var[8] = 0;
printf( "Save Code: %s\n", var );
}