Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

FrontierBitmap.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TSGL - Teddy Space Game Library
00004     Copyright (C) 2002 Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id: FrontierBitmap.cpp,v 1.1 2002/01/08 20:47:05 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "Teddy/SpaceGame/FrontierBitmap.h"
00026 #include "Teddy/SpaceGame/FrontierFile.h"
00027 #include "Teddy/SysSupport/Types.h"
00028 #include <cstdio>
00029 #include <cstring>
00030 
00031 
00032 namespace Teddy     {
00033 namespace SpaceGame {
00034 
00035 
00036 static Uint8 ffe_palette[128*3] = {
00037     0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x08, 0x08, 0x08,
00038     0x0c, 0x0c, 0x0c, 0x10, 0x10, 0x10, 0x14, 0x14, 0x14,
00039     0x18, 0x18, 0x18, 0x1c, 0x1c, 0x1c, 0x20, 0x20, 0x20,
00040     0x24, 0x24, 0x24, 0x28, 0x28, 0x28, 0x2c, 0x2c, 0x2c,
00041     0x30, 0x30, 0x30, 0x34, 0x34, 0x34, 0x38, 0x38, 0x38,
00042     0x3c, 0x3c, 0x3c, 0x3c, 0x00, 0x00, 0x00, 0x3c, 0x00,
00043     0x3c, 0x3c, 0x00, 0x04, 0x08, 0x04, 0x0a, 0x02, 0x06,
00044     0x12, 0x04, 0x06, 0x0a, 0x0a, 0x08, 0x1a, 0x02, 0x02,
00045     0x0a, 0x0a, 0x10, 0x12, 0x0a, 0x0a, 0x22, 0x02, 0x02,
00046     0x04, 0x08, 0x1c, 0x0c, 0x12, 0x10, 0x2a, 0x02, 0x02,
00047     0x1a, 0x0a, 0x0c, 0x22, 0x0a, 0x0a, 0x32, 0x02, 0x02,
00048     0x0e, 0x1a, 0x10, 0x0c, 0x20, 0x0c, 0x06, 0x0a, 0x2a,
00049     0x14, 0x12, 0x14, 0x2a, 0x0a, 0x08, 0x08, 0x2c, 0x08,
00050     0x0c, 0x26, 0x0c, 0x1a, 0x12, 0x14, 0x08, 0x30, 0x08,
00051     0x3a, 0x04, 0x04, 0x14, 0x1a, 0x14, 0x22, 0x12, 0x0e,
00052     0x0e, 0x2c, 0x0a, 0x32, 0x0a, 0x08, 0x12, 0x20, 0x14,
00053     0x12, 0x26, 0x0e, 0x18, 0x20, 0x10, 0x2a, 0x12, 0x0e,
00054     0x18, 0x26, 0x0c, 0x1a, 0x1a, 0x16, 0x22, 0x14, 0x16,
00055     0x22, 0x1a, 0x10, 0x3a, 0x0a, 0x08, 0x1e, 0x20, 0x0e,
00056     0x34, 0x12, 0x06, 0x18, 0x20, 0x16, 0x2a, 0x1a, 0x0c,
00057     0x32, 0x1a, 0x04, 0x1e, 0x26, 0x0e, 0x12, 0x16, 0x2a,
00058     0x14, 0x26, 0x18, 0x2a, 0x14, 0x14, 0x10, 0x32, 0x10,
00059     0x24, 0x22, 0x0e, 0x0c, 0x12, 0x36, 0x3c, 0x12, 0x08,
00060     0x32, 0x12, 0x12, 0x22, 0x1a, 0x1a, 0x18, 0x26, 0x18,
00061     0x1e, 0x20, 0x18, 0x16, 0x2e, 0x14, 0x2a, 0x22, 0x0e,
00062     0x1a, 0x1a, 0x26, 0x34, 0x20, 0x08, 0x16, 0x22, 0x24,
00063     0x3c, 0x1a, 0x06, 0x10, 0x14, 0x38, 0x2a, 0x1a, 0x18,
00064     0x1e, 0x26, 0x1a, 0x24, 0x22, 0x18, 0x3a, 0x12, 0x12,
00065     0x14, 0x34, 0x16, 0x30, 0x20, 0x10, 0x32, 0x1a, 0x16,
00066     0x32, 0x2a, 0x06, 0x3c, 0x22, 0x04, 0x1e, 0x2e, 0x18,
00067     0x2a, 0x22, 0x18, 0x1e, 0x20, 0x26, 0x12, 0x1a, 0x3a,
00068     0x34, 0x32, 0x02, 0x1a, 0x36, 0x18, 0x3c, 0x2a, 0x04,
00069     0x2a, 0x2a, 0x16, 0x1e, 0x26, 0x26, 0x1a, 0x1a, 0x38,
00070     0x3a, 0x1a, 0x18, 0x24, 0x2c, 0x1c, 0x24, 0x22, 0x26,
00071     0x1e, 0x30, 0x20, 0x32, 0x26, 0x16, 0x32, 0x32, 0x0c,
00072     0x2a, 0x22, 0x24, 0x24, 0x2a, 0x26, 0x3c, 0x34, 0x04,
00073     0x30, 0x22, 0x22, 0x1e, 0x24, 0x36, 0x24, 0x32, 0x22,
00074     0x2a, 0x2a, 0x26, 0x3c, 0x3a, 0x04, 0x24, 0x24, 0x32,
00075     0x24, 0x28, 0x30, 0x3a, 0x22, 0x22, 0x2a, 0x30, 0x28,
00076     0x24, 0x24, 0x3c, 0x24, 0x28, 0x3a, 0x32, 0x2a, 0x2c,
00077     0x2a, 0x2a, 0x36, 0x3a, 0x2a, 0x28, 0x2c, 0x30, 0x30,
00078     0x32, 0x32, 0x2e, 0x2c, 0x30, 0x3c, 0x32, 0x32, 0x38,
00079     0x3a, 0x32, 0x32, 0x38, 0x3a, 0x38
00080 };
00081 
00082 
00083 FrontierBitmap::FrontierBitmap( FrontierFile *f, char *label ){
00084     this->f = f;
00085 
00086     int     x, y;
00087     int     i, w, h, sw;
00088     Uint32  zero;
00089     Uint16  head[27];       // Complete header size
00090     FILE   *out_file;
00091 
00092 
00093     f->reset();
00094     f->seek( label );
00095     char *tmp = f->get_label_def();
00096 
00097     Uint8   flags  = f->read_Uint8();
00098     Uint8   height = f->read_Uint8();
00099     Uint16  width  = f->read_Uint16();
00100     Sint16  xoff   = f->read_Sint16();
00101     Sint16  yoff   = f->read_Sint16();
00102 
00103     int  ret = 0;
00104     char tmps [200];
00105     char fname[200];
00106     strcpy( tmps, label );
00107     int ll = strlen( tmps );
00108     tmps[ll-1] = 0;
00109     sprintf( fname, "ffe_%s.bmp", tmps );
00110 
00111 /*  
00112     cout << "Frontier Bitmap " << fname << endl;
00113     cout << "Flags:  " << (int)(flags)  << endl;
00114     cout << "Height: " << (int)(height) << endl;
00115     cout << "Width:  " << (int)(width)  << endl;
00116     cout << "XOff:   " << (int)(xoff)   << endl;
00117     cout << "YOff:   " << (int)(yoff)   << endl;*/
00118 
00119     for( i=0; i<27; i++ ){
00120         head[i] = 0;        // Zero whole header
00121     }
00122     w    = width;
00123     h    = height;
00124     sw   = w + 3 & -4;
00125     zero = 0;
00126 
00127     out_file = fopen( fname, "wb" );
00128     if( out_file == NULL ){
00129         printf( "could not open out file %s\n", fname );
00130         return;
00131     }
00132 
00133     head[0]  = 0x4D42;       // RgbMap type 'BM'
00134     *(Uint32*)(head+1) = 54 + 256*4 + sw*h;  // Total filesize
00135     head[5]  = 54 + 256*4;   // Offset to data
00136     head[7]  = 40;           // Size of info structure
00137     head[9]  = sw;           // Width
00138     head[11] = height;       // Height
00139     head[13] = 1;            // Planes
00140     head[14] = 8;            // Bpp
00141 
00142     ret = fwrite( head, 2, 27, out_file );   // Write header
00143     if( ret!=27 ){
00144         printf( "write fails, ret = %d\n", ret );
00145     }
00146     for( i=0; i<128; i++ ){
00147         Uint32 col =
00148             (Uint32(ffe_palette[i*3  ]) << 18) +
00149             (Uint32(ffe_palette[i*3+1]) << 10) +
00150             (Uint32(ffe_palette[i*3+2]) <<  2);
00151         ret = fwrite( &col, 4, 1, out_file );  // Write Palette
00152         if( ret!=1 ){
00153             printf( "write fails, ret = %d\n", ret );
00154         }
00155     }
00156     for( i=0; i<128; i++ ){
00157         ret = fwrite( &zero, 4, 1, out_file );
00158         if( ret!=1 ){
00159             printf( "write fails, ret = %d\n", ret );
00160         }
00161     }
00162 
00163     //  Read in down top order
00164     Uint8 t[320*256];
00165     for( i=0; i<w*h; i++ ){
00166         t[i] = 0;
00167     }
00168 
00169     for( y=h-1; y>=0; y-- ){
00170         for( x=0; x<w; x++ ){
00171             t[x+y*sw] = f->get_byte();
00172         }
00173     }
00174 
00175     //  Write in top down order
00176     for( y=0; y<h; y++ ){
00177         for( x=0; x<sw; x++ ){
00178             fwrite( &t[x+y*sw], 1, 1, out_file );  // Write data
00179             if( ret!=1 ){
00180                 printf( "write fails, ret = %d\n", ret );
00181             }
00182         }
00183     }
00184 
00185     ret = fflush( out_file );
00186     if( ret!=0 ){
00187         printf( "fflush fails\n" );
00188     }
00189     ret = fclose( out_file );
00190     if( ret!=0 ){
00191         printf( "fclose fails\n" );
00192     }
00193 //  cout << "file written and closed" << endl;
00194 }
00195 
00196 
00197 };  //  namespace SpaceGame
00198 };  //  namespace Teddy
00199