00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "mxpixel.h"
00016 #include<cstring>
00017 #include<cstdlib>
00018 #include<iostream>
00019
00020 namespace mx
00021 {
00022
00023
00024
00025 mxPixel::mxPixel() : dead(false), no_respawn(false)
00026 {
00027 x = 0, y = 0, speed = 0, direction = 0;
00028 alpha = 0, distance = 0, vibration = 0;
00029 color.value = 0;
00030 sx = sy = sw = sh = 0;
00031 }
00032
00033 mxPixel::~mxPixel()
00034 {
00035
00036 }
00037
00038 mxPixel::mxPixel(unsigned int color_value) : dead(false), no_respawn(false)
00039 {
00040 x = 0, y = 0, speed = 0, direction = 0;
00041 alpha = 0, distance = 0, vibration = 0;
00042 color.value = color_value;
00043 sx = sy = sw = sh = 0;
00044
00045
00046 }
00047
00048 mxPixel::mxPixel(unsigned char r, unsigned char g, unsigned char b, unsigned char a) : dead(false), no_respawn(false)
00049 {
00050
00051 x = 0, y = 0, speed = 0, direction = 0;
00052 alpha = 0, distance = 0, vibration = 0;
00053 sx = sy = sw = sh = 0;
00054
00055 color.rgb[0] = r;
00056 color.rgb[1] = g;
00057 color.rgb[2] = b;
00058 color.rgb[3] = a;
00059 }
00060
00061 mxPixel::mxPixel(const mxPixel &p) : dead(false), no_respawn(false)
00062 {
00063 setPixel(p);
00064
00065 }
00066
00067 void mxPixel::setPixel(const mxPixel &p)
00068 {
00069 direction = p.direction;
00070 color.value = p.color.value;
00071 x = p.x;
00072 y = p.y;
00073 speed = p.speed;
00074 alpha = p.alpha;
00075 distance = p.distance;
00076 vibration = p.vibration;
00077 sx = p.sx;
00078 sy = p.sy;
00079 sw = p.sw;
00080 sh = p.sh;
00081 no_respawn = p.no_respawn;
00082 dead = p.dead;
00083 }
00084
00085 void mxPixel::setPixel(int x, int y, int direction)
00086 {
00087
00088 this->x = x, this->y = y, this->direction = direction;
00089
00090 }
00091
00092 void mxPixel::clearPixel()
00093 {
00094
00095 x = 0;
00096 y = 0;
00097 direction = 0;
00098 memset(&color, 0, sizeof(color));
00099 vibration = 0;
00100 speed = 0;
00101 alpha = 0;
00102 distance = 0;
00103 vibration = 0;
00104 }
00105
00106
00107 mxPixel &mxPixel::operator=(const mxPixel &p)
00108 {
00109
00110 setPixel(p);
00111
00112 return *this;
00113 }
00114
00115 mxPixel &mxPixel::operator+=(const mxPixel &p)
00116 {
00117 color.value += p.color.value;
00118 vibration += p.vibration;
00119 return *this;
00120 }
00121
00122
00123 Uint32 mxPixel::GetMappedColor(SDL_PixelFormat *format)
00124 {
00125 return SDL_MapRGB(format, color.rgb[0], color.rgb[1], color.rgb[2]);
00126 }
00127
00128
00129 void mxPixel::SetMappedColor(SDL_PixelFormat *format, Uint32 color_i)
00130 {
00131
00132 SDL_GetRGBA(color_i, format, &color.rgb[0], &color.rgb[1], &color.rgb[2], &color.rgb[3]);
00133
00134 }
00135
00136 void mxPixel::setColor(unsigned int color)
00137 {
00138 this->color.value = color;
00139 }
00140
00141 void mxPixel::GetSDLColor(SDL_Color *col)
00142 {
00143 col->r = color.rgb[0];
00144 col->g = color.rgb[1];
00145 col->b = color.rgb[2];
00146 }
00147
00148 void mxPixel::setDirection(int direction)
00149 {
00150 this->direction = direction;
00151 }
00152
00153 void mxPixel::setAlpha(float alpha, bool on)
00154 {
00155
00156 if(on) this->alpha = alpha;
00157 else alpha = 0.0f;
00158
00159 }
00160
00161 void mxPixel::setVibration(float alpha, bool on)
00162 {
00163 if(on) vibration = alpha;
00164 else vibration = 0.0f;
00165
00166 }
00167
00168 void mxPixel::setSpeed(int speed)
00169 {
00170 this->speed = speed;
00171 }
00172
00173
00174 mxPixel operator+(const mxPixel &p, const mxPixel &p2)
00175 {
00176 mxPixel ptemp(p);
00177 ptemp += p2;
00178 return ptemp;
00179 }
00180
00181
00182 mxPixel &mxPixel::operator-=(const mxPixel &p)
00183 {
00184 color.value -= p.color.value;
00185 vibration -= p.vibration;
00186 return *this;
00187
00188 }
00189
00190 mxPixel operator-(const mxPixel &p, const mxPixel &p2)
00191 {
00192 mxPixel temp(p);
00193 temp += p2;
00194 return temp;
00195 }
00196
00197 mxPixel &mxPixel::operator*=(const mxPixel &p)
00198 {
00199
00200 color.value *= p.color.value;
00201 vibration *= p.vibration;
00202 return *this;
00203
00204 }
00205
00206 mxPixel operator*(const mxPixel &p, const mxPixel &p2)
00207 {
00208 mxPixel temp(p);
00209 temp+=p2;
00210 return temp;
00211 }
00212
00213 mxPixel &mxPixel::operator/=(const mxPixel &p)
00214 {
00215
00216 if(p.color.value != 0 && color.value != 0)
00217 color.value /= p.color.value;
00218
00219 if(p.vibration != 0 && vibration != 0)
00220 vibration /= p.vibration;
00221
00222 return *this;
00223
00224
00225 }
00226
00227 mxPixel operator/(const mxPixel &p, const mxPixel &p2)
00228 {
00229 mxPixel ptr(p);
00230 ptr += p2;
00231 return ptr;
00232 }
00233
00234
00235
00236 const bool mxPixel::isActive() const
00237 {
00238
00239 if(no_respawn == true && dead == true)
00240 return false;
00241
00242 return true;
00243 }
00244
00245
00246 void mxPixel::moveByDirection()
00247 {
00248
00249 if(dead == true) return;
00250
00251 int &x_pos = this->x;
00252 int &y_pos = this->y;
00253
00254
00255 switch ( direction )
00256 {
00257 case 0:
00258 x_pos += 2;
00259 y_pos -= 2;
00260 break;
00261 case 1:
00262 x_pos++;
00263 y_pos++;
00264 break;
00265 case 2:
00266
00267 x_pos--;
00268 y_pos++;
00269 break;
00270 case 3:
00271
00272 y_pos--;
00273 x_pos--;
00274 break;
00275 case 4:
00276
00277 x_pos++;
00278 y_pos--;
00279 break;
00280 case 5:
00281 y_pos--;
00282 break;
00283 case 6:
00284 y_pos++;
00285 break;
00286
00287 }
00288
00289 boundsCheck();
00290
00291 }
00292
00293 void mxPixel::boundsCheck()
00294 {
00295
00296 if(!(x > 0 && x < sw && y > 0 && y < sh))
00297 {
00298
00299 resetPixel();
00300
00301 }
00302
00303 }
00304
00305 void mxPixel::setBounds(const int bx, const int by, const int bw, const int bh)
00306 {
00307 sx = bx, sy = by, sw = bw, sh = bh;
00308 }
00309
00310 void mxPixel::resetPixel()
00311 {
00312 if(no_respawn == true)
00313 {
00314 this->dead = true;
00315 x = 0;
00316 y = 0;
00317 return;
00318 }
00319 setPixel(rand()%sw, rand()%sh, rand()%6);
00320 }
00321
00322
00323 void mxPixel::operator++(int)
00324 {
00325 moveByDirection();
00326 }
00327
00328 void mxPixel::operator++()
00329 {
00330 moveByDirection();
00331 }
00332
00333 void mxPixel::setNoRespawn(bool spawn)
00334 {
00335 no_respawn = spawn;
00336 }
00337
00338
00339
00340
00341
00342 mxPixelData::mxPixelData()
00343 {
00344 data = 0;
00345 surf = 0;
00346
00347 }
00348
00349 mxPixelData::~mxPixelData()
00350 {
00351 if(data != 0)
00352 {
00353 for(int z = 0; z < sx; z++)
00354 delete [] data[z];
00355
00356 delete [] data;
00357 }
00358 }
00359
00360 bool mxPixelData::initDataFromSurface(mx::mxSurface &surface)
00361 {
00362
00363 sx = surface.getSurface()->w;
00364 sy = surface.getSurface()->h;
00365
00366 surface.lockSurface();
00367
00368 try
00369 {
00370
00371
00372 data = new mxPixel*[surface.getSurface()->w];
00373
00374 for(int x = 0; x < surface.getSurface()->w; x++)
00375 {
00376 data[x] = new mxPixel[surface.getSurface()->h];
00377
00378 for(int y = 0; y < surface.getSurface()->h; y++)
00379 {
00380 Uint32 color;
00381 color = surface[static_cast<unsigned int>(x+(y*surface.width()))];
00382 data[x][y].setBounds(0, 0, surface.getSurface()->w, surface.getSurface()->h);
00383 data[x][y].SetMappedColor(surface, color);
00384 data[x][y].setPos(x,y);
00385 data[x][y].setDirection(rand()%6);
00386
00387 }
00388
00389 }
00390
00391 surface.unlockSurface();
00392
00393 }
00394
00395 catch(...)
00396 {
00397
00398 for(int z = 0; z < surface.width(); z++)
00399 delete [] data[z];
00400
00401 delete [] data;
00402 std::cerr << "error converting image, prob out of memory.\n";
00403 throw;
00404 }
00405
00406
00407 return true;
00408 }
00409
00410
00411
00412 void mxPixelData::copyDataToSurface(mx::mxSurface &surf, int amt)
00413 {
00414
00415 surf.lockSurface();
00416
00417 int index = 0;
00418
00419
00420 for(int i = 0; i < sx; i++)
00421 {
00422 for(int z = 0; z < sy; z++)
00423 {
00424
00425 if(((++index)%amt) != 0) continue;
00426
00427 data[i][z]++;
00428
00429 surf[static_cast<unsigned int>(data[i][z].x+(data[i][z].y*surf.getSurface()->w))] = data[i][z].GetMappedColor(surf);
00430
00431
00432 }
00433
00434 }
00435
00436 surf.unlockSurface();
00437
00438 }
00439
00440
00441 }
00442
00443
00444