Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

sprite2.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: sprite2.cpp,v 1.1 2001/03/06 15:09:22 mbn Exp $
00003 
00004         ------------------------------------------------------------------------
00005         ClanLib, the platform independent game SDK.
00006 
00007         This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00008         version 2. See COPYING for details.
00009 
00010         For a total list of contributers see CREDITS.
00011 
00012         ------------------------------------------------------------------------
00013 
00014         File purpose:
00015                 Simple sprite2 support.
00016 
00017 */
00018 
00019 #include "Core/precomp.h"
00020 #include <stdio.h>
00021 #include "API/Display/SurfaceProviders/sprite2.h"
00022 #include "API/Core/IOData/inputsource.h"
00023 #include "API/Display/Display/palette.h"
00024 #include "API/Core/System/cl_assert.h"
00025 
00026 CL_Sprite2Provider::CL_Sprite2Provider(
00027         CL_InputSource *datafile)
00028 {
00029         palette = NULL;
00030         surface_data = NULL;
00031 
00032         load_data(datafile);
00033 }
00034 
00035 CL_Sprite2Provider::CL_Sprite2Provider(
00036         CL_SurfaceProvider *src,
00037         int x, int y,
00038         int width, int height,
00039         int *tcols, int tcols_num)
00040 {
00041         this->width = width;
00042         this->height = height;
00043         no_sprs = 1;
00044         if (tcols_num > 0) transcol = tcols[0];
00045         else transcol = -1;
00046 
00047         if (src->get_palette() != NULL)
00048                 palette = new CL_Palette(
00049                         src->get_palette()->palette,
00050                         src->get_palette()->num_colors);
00051 
00052 //      cout << "Palette: " << palette << endl;
00053 
00054 //      pixelformat = src->get_pixel_format();
00055         m_is_indexed = src->is_indexed();
00056         red_mask = src->get_red_mask();
00057         green_mask = src->get_green_mask();
00058         blue_mask = src->get_blue_mask();
00059         alpha_mask = src->get_alpha_mask();
00060         bpp = src->get_depth();
00061 
00062 //      cout << "Pixelformat: " << pixelformat << endl;
00063 
00064         // Quick-verify data:
00065         cl_assert(width > 0);
00066         cl_assert(height > 0);
00067         cl_assert(transcol >= -1);
00068         cl_assert(no_sprs > 0);
00069  
00070         int size_surface_data = width*height*no_sprs*((get_pitch()+7)/8);
00071         unsigned char *dest_data = new unsigned char[size_surface_data];
00072         surface_data = dest_data;
00073 
00074         src->lock();
00075         unsigned char *src_data = (unsigned char *) src->get_data();
00076         
00077         bool transcol_table[256];
00078         for (int i=0; i<256; i++) transcol_table[i] = false;
00079         for (int j=0; j<tcols_num; j++) transcol_table[tcols[j]] = true;
00080 
00081         for (int spr_y=0; spr_y<height; spr_y++)
00082         {
00083                 for (int spr_x=0; spr_x<width; spr_x++)
00084                 {
00085                         unsigned char val = src_data[
00086                                 x+spr_x+
00087                                 (y+spr_y)*src->get_pitch()];
00088 
00089                         if (transcol_table[val])
00090                         {
00091                                 dest_data[spr_x+spr_y*get_pitch()] = transcol;
00092                         }
00093                         else
00094                         {
00095                                 dest_data[spr_x+spr_y*get_pitch()] = val;
00096                         }
00097 
00098 /*                      if (spr_x == 0 || spr_y == 0 || spr_x+1 == width || spr_y+1 == height)
00099                                 dest_data[spr_x+spr_y*width] = 16;*/
00100                 }
00101         }
00102 
00103         src->unlock();
00104 }
00105 
00106 CL_Sprite2Provider::~CL_Sprite2Provider()
00107 {
00108         delete[] ((char*) surface_data);
00109         surface_data = NULL;
00110 
00111         delete palette;
00112         palette = NULL;
00113 }
00114 
00115 unsigned int CL_Sprite2Provider::get_width() const
00116 {
00117         return width;
00118 }
00119 
00120 unsigned int CL_Sprite2Provider::get_height() const
00121 {
00122         return height;
00123 }
00124 
00125 unsigned int CL_Sprite2Provider::get_pitch() const
00126 {
00127         return width*((get_depth()+7)/8);
00128 }
00129 
00130 unsigned int CL_Sprite2Provider::get_num_frames() const
00131 {
00132         return no_sprs;
00133 }
00134 /*
00135 EPixelFormat CL_Sprite2Provider::get_pixel_format() const
00136 {
00137         return pixelformat;
00138 }
00139 */
00140 CL_Palette *CL_Sprite2Provider::get_palette() const
00141 {
00142         return palette;
00143 }
00144 
00145 bool CL_Sprite2Provider::uses_src_colorkey() const
00146 {
00147         return transcol != -1;
00148 }
00149 
00150 unsigned int CL_Sprite2Provider::get_src_colorkey() const
00151 {
00152         return transcol;
00153 }
00154 
00155 void *CL_Sprite2Provider::get_data() const
00156 {
00157         return surface_data;
00158 }
00159 
00160 void CL_Sprite2Provider::load_data(CL_InputSource *datafile)
00161 {
00162         cl_assert(datafile != NULL);
00163 
00164         width = datafile->read_int32();
00165         height = datafile->read_int32();
00166         no_sprs = datafile->read_int32();
00167         transcol = datafile->read_int32();
00168 
00169         char has_palette = datafile->read_char8();
00170         if (has_palette)
00171         {
00172                 unsigned char temp[768];
00173                 datafile->read(temp, 768);
00174                 palette = new CL_Palette(temp);
00175         }
00176 
00177         char indexed = datafile->read_char8();
00178         cl_assert(indexed == 0 || indexed == 1);
00179         if (indexed) m_is_indexed = true;
00180         else m_is_indexed = false;
00181 
00182 //      pixelformat = (EPixelFormat) datafile->read_int32();
00183         red_mask = datafile->read_int32();
00184         green_mask = datafile->read_int32();
00185         blue_mask = datafile->read_int32();
00186         alpha_mask = datafile->read_int32();
00187         bpp = datafile->read_int32();
00188         
00189         // Quick-verify data:
00190         cl_assert(width > 0);
00191         cl_assert(height > 0);
00192         cl_assert(transcol >= -1);
00193         cl_assert(no_sprs > 0);
00194  
00195         int size_surface_data = width*height*no_sprs*((get_depth()+7)/8);
00196         surface_data = new unsigned char[size_surface_data];
00197         int read = datafile->read(surface_data, size_surface_data);
00198         cl_assert(read == size_surface_data);
00199 
00200 //      delete datafile;
00201 }
00202 
00203 void CL_Sprite2Provider::perform_lock()
00204 {
00205 }
00206 
00207 void CL_Sprite2Provider::perform_unlock()
00208 {
00209 }
00210 
00211 bool CL_Sprite2Provider::is_indexed() const
00212 {
00213         return m_is_indexed;
00214 }
00215 
00216 unsigned int CL_Sprite2Provider::get_red_mask() const
00217 {
00218         return red_mask;
00219 }
00220 
00221 unsigned int CL_Sprite2Provider::get_green_mask() const
00222 {
00223         return green_mask;
00224 }
00225 
00226 unsigned int CL_Sprite2Provider::get_blue_mask() const
00227 {
00228         return blue_mask;
00229 }
00230 
00231 unsigned int CL_Sprite2Provider::get_alpha_mask() const
00232 {
00233         return alpha_mask;
00234 }
00235 
00236 unsigned int CL_Sprite2Provider::get_depth() const
00237 {
00238         return bpp;
00239 }

Generated at Wed Apr 4 19:54:03 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001