00001 /* 00002 $Id: sprite.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 sprite support. 00016 00017 */ 00018 00019 #include "Core/precomp.h" 00020 #include <stdio.h> 00021 00022 #include "API/Display/Display/palette.h" 00023 #include "API/Display/SurfaceProviders/sprite.h" 00024 #include "API/Core/System/error.h" 00025 #include "API/Core/System/cl_assert.h" 00026 00027 /**************** 00028 CL_SpriteProvider class 00029 *****************/ 00030 00031 CL_Surface *CL_SpriteProvider::create(const char *surface_id, CL_InputSourceProvider *datafile) 00032 { 00033 return CL_Surface::create(new CL_SpriteProvider(surface_id, datafile), true); 00034 } 00035 00036 CL_SpriteProvider::CL_SpriteProvider(const char *_surface_id, CL_InputSourceProvider *datafile) 00037 { 00038 surface_id = _surface_id; 00039 provider = datafile != NULL ? datafile->clone() : CL_InputSourceProvider::create_file_provider("."); 00040 00041 palette = NULL; 00042 surface_data = NULL; 00043 } 00044 00045 CL_SpriteProvider::~CL_SpriteProvider() 00046 { 00047 perform_unlock(); 00048 delete provider; 00049 } 00050 00051 unsigned int CL_SpriteProvider::get_width() const 00052 { 00053 return width; 00054 } 00055 00056 unsigned int CL_SpriteProvider::get_height() const 00057 { 00058 return height; 00059 } 00060 00061 unsigned int CL_SpriteProvider::get_pitch() const 00062 { 00063 return width*((bpp+7)/8); 00064 } 00065 00066 unsigned int CL_SpriteProvider::get_num_frames() const 00067 { 00068 return no_sprs; 00069 } 00070 /* 00071 EPixelFormat CL_SpriteProvider::get_pixel_format() const 00072 { 00073 return pixelformat; 00074 } 00075 */ 00076 CL_Palette *CL_SpriteProvider::get_palette() const 00077 { 00078 return palette; 00079 } 00080 00081 bool CL_SpriteProvider::is_indexed() const 00082 { 00083 return m_is_indexed; 00084 } 00085 00086 bool CL_SpriteProvider::uses_src_colorkey() const 00087 { 00088 return transcol != -1; 00089 } 00090 00091 unsigned int CL_SpriteProvider::get_src_colorkey() const 00092 { 00093 return transcol; 00094 } 00095 00096 unsigned int CL_SpriteProvider::get_red_mask() const 00097 { 00098 return red_mask; 00099 } 00100 00101 unsigned int CL_SpriteProvider::get_green_mask() const 00102 { 00103 return green_mask; 00104 } 00105 00106 unsigned int CL_SpriteProvider::get_blue_mask() const 00107 { 00108 return blue_mask; 00109 } 00110 00111 unsigned int CL_SpriteProvider::get_alpha_mask() const 00112 { 00113 return alpha_mask; 00114 } 00115 00116 unsigned int CL_SpriteProvider::get_depth() const 00117 { 00118 return bpp; 00119 } 00120 00121 void *CL_SpriteProvider::get_data() const 00122 { 00123 return surface_data; 00124 } 00125 00126 void CL_SpriteProvider::load_data() 00127 { 00128 // cout << "Locking spriteprovider from location " << surface_id << endl; 00129 00130 CL_InputSource *datafile = provider->open_source(surface_id.c_str()); 00131 cl_assert(datafile != NULL); 00132 00133 // cout << "Hello?" << endl; 00134 width = datafile->read_int32(); 00135 height = datafile->read_int32(); 00136 no_sprs = datafile->read_int32(); 00137 transcol = datafile->read_int32(); 00138 00139 // printf("width %d, height %d, no_sprs %d, transcol %d\n", width, height, no_sprs, transcol); 00140 00141 char has_palette = datafile->read_char8(); 00142 if (has_palette) 00143 { 00144 unsigned char temp[768]; 00145 datafile->read(temp, 768); 00146 palette = new CL_Palette(temp); 00147 } 00148 00149 char indexed = datafile->read_char8(); 00150 cl_assert(indexed == 0 || indexed == 1); 00151 if (indexed) m_is_indexed = true; 00152 else m_is_indexed = false; 00153 00154 // pixelformat = (EPixelFormat) datafile->read_int32(); 00155 red_mask = datafile->read_int32(); 00156 green_mask = datafile->read_int32(); 00157 blue_mask = datafile->read_int32(); 00158 alpha_mask = datafile->read_int32(); 00159 bpp = datafile->read_int32(); 00160 // printf("pixelformat %d", (int) pixelformat); 00161 00162 // Quick-verify data: 00163 cl_assert(width > 0); 00164 cl_assert(height > 0); 00165 cl_assert(transcol >= -1); 00166 cl_assert(no_sprs > 0); 00167 00168 int size_surface_data = width*height*no_sprs*((bpp+7)/8); 00169 // cout << "Surface data size: " << size_surface_data << endl; 00170 surface_data = new unsigned char[size_surface_data]; 00171 int read = datafile->read(surface_data, size_surface_data); 00172 cl_assert(read == size_surface_data); 00173 00174 delete datafile; 00175 } 00176 00177 void CL_SpriteProvider::perform_lock() 00178 { 00179 load_data(); 00180 } 00181 00182 void CL_SpriteProvider::perform_unlock() 00183 { 00184 delete[] ((char*) surface_data); 00185 surface_data = NULL; 00186 00187 delete palette; 00188 palette = NULL; 00189 } 00190 00191 /*********************** 00192 CL_SpriteTexture class 00193 ************************/ 00194 /* 00195 CL_Texture *CL_SpriteTexture::load(char *surface_id, CL_InputSourceProvider *datafile) 00196 { 00197 return CL_Texture::create(new CL_SpriteTexture(surface_id, datafile), true); 00198 } 00199 00200 CL_SpriteTexture::CL_SpriteTexture(char *_surface_id, CL_InputSourceProvider *datafile) 00201 { 00202 surface_id = _surface_id; 00203 provider = datafile != NULL ? datafile->clone() : CL_InputSourceProvider::create_file_provider("."); 00204 00205 palette = NULL; 00206 surface_data = NULL; 00207 00208 load_data(); 00209 } 00210 00211 CL_SpriteTexture::~CL_SpriteTexture() 00212 { 00213 unlock(); 00214 } 00215 00216 unsigned int CL_SpriteTexture::get_width() 00217 { 00218 return width; 00219 } 00220 00221 unsigned int CL_SpriteTexture::get_height() 00222 { 00223 return height; 00224 } 00225 00226 EPixelFormat CL_SpriteTexture::get_pixel_format() 00227 { 00228 return pixelformat; 00229 } 00230 00231 CL_Palette *CL_SpriteTexture::get_palette() 00232 { 00233 return palette; 00234 } 00235 00236 int CL_SpriteTexture::get_transcol() 00237 { 00238 return transcol; 00239 } 00240 00241 void *CL_SpriteTexture::get_data() 00242 { 00243 return surface_data; 00244 } 00245 00246 void CL_SpriteTexture::load_data() 00247 { 00248 CL_InputSource *datafile = provider->open_source(surface_id); 00249 cl_assert(datafile != NULL); 00250 00251 width = datafile->read_short16(); 00252 height = datafile->read_short16(); 00253 transcol = datafile->read_short16(); 00254 no_sprs = datafile->read_short16(); 00255 int format = datafile->read_short16(); 00256 00257 // Quick-verify data: 00258 cl_assert(width>0&&height>0&&transcol>=-1&&no_sprs>0&&format>=0); 00259 00260 if (format == 0) // 8 bit, indexed/palette format 00261 { 00262 pixelformat = PAL8; 00263 00264 char *palette_id = datafile->read_string(); 00265 00266 datafile->push_position(); 00267 palette = new CL_Palette(palette_id, provider); 00268 datafile->pop_position(); 00269 00270 delete palette_id; 00271 00272 unsigned char *surface_indexed = new unsigned char[width*height*no_sprs]; 00273 cl_assert(surface_indexed != NULL); 00274 datafile->read((char *) surface_indexed, width*height*no_sprs); 00275 00276 surface_data = surface_indexed; 00277 } 00278 else // RGBA format 00279 { 00280 cout << "RGBA surface format not supported" << endl; 00281 exit(1); 00282 00283 // throw new CL_Error_ClanLib_Sprite_Load("RGBA surface format not supported in this version of ClanLib!", __FILE__, __LINE__); 00284 } 00285 00286 delete datafile; 00287 } 00288 00289 void CL_SpriteTexture::lock() 00290 { 00291 if (surface_data != NULL) return; // Data already loaded 00292 00293 load_data(); 00294 } 00295 00296 void CL_SpriteTexture::unlock() 00297 { 00298 delete surface_data; 00299 surface_data = NULL; 00300 00301 delete palette; 00302 palette = NULL; 00303 } 00304 */
1.2.6 written by Dimitri van Heesch,
© 1997-2001