C:/DevKitPro/!MDuel/source/spriteManager.cpp

Go to the documentation of this file.
00001 /*
00002  * Marshmallow Duel DS v2
00003  * Copyright © 2007 Sam Pospischil http://pospi.spadgos.com
00004  * 
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  * 
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013  * GNU General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00018  */
00019 
00020 #include "spriteManager.h"
00021 #include "spriteObject.h"
00022 #include "singleFireSprite.h"
00023 
00034 u16 spriteManager::loadGFX(const unsigned char *newGFX, u8 sizex, u8 sizey, u8 colorMode)
00035 {
00036         u16 temp = PA_CreateGfx(screen, static_cast<void *>(const_cast<unsigned char *>(newGFX)), sizex, sizey, colorMode);
00037         //GFXids.push_back(temp);
00038         return temp;
00039 }
00040 
00055 s8 spriteManager::loadSpriteFromGFX(u16 GFXid, u8 sizex, u8 sizey, u8 palleteID, u8 minIndex, u8 colorMode, s16 x, s16 y)
00056 {
00057         s8 next = getNextSpriteID(minIndex);
00058         if (next != -1)
00059         {
00060                 PA_CreateSpriteFromGfx(screen, next, GFXid, sizex, sizey, colorMode, palleteID, x, y);
00061                 sprites[next] = 1;
00062         }
00063         return next;
00064 }
00065 
00072 void spriteManager::updateSpriteGFX(s8 spriteID, void* GFXid)
00073 {
00074         PA_UpdateSpriteGfx(screen, spriteID, GFXid);
00075 }
00076 
00091 void spriteManager::updateSpritePallete(u8 spriteID, u16 gfxID, u8 palleteID, u8 sizex, u8 sizey, u8 colorMode, u8 x, u8 y)
00092 {
00093         PA_CreateSpriteFromGfx(screen, spriteID, gfxID, sizex, sizey, colorMode, palleteID, x, y);
00094 }
00095 
00104 void spriteManager::setAlpha(u8 transLevel)
00105 {
00106         u8 fxon;
00107         if (transLevel < 15)
00108                 fxon = SFX_ALPHA;
00109         else
00110                 fxon = SFX_NONE;
00111         PA_SetSFXAlpha(screen, transLevel, 15);
00112         PA_EnableSpecialFx(screen, fxon, 0, SFX_BG0 | SFX_BG1 | SFX_BG2 | SFX_BG3 | SFX_BD);
00113 }
00114 
00131 s8 spriteManager::loadSprite(const unsigned char *newSprite, u8 sizex, u8 sizey, u8 minIndex, u8 palleteID, s16 x, s16 y, u8 colorMode)
00132 {
00133         s8 next = getNextSpriteID(minIndex);
00134         if (next != -1)
00135         {
00136                 PA_CreateSprite(screen, next, static_cast<void *>(const_cast<unsigned char *>(newSprite)), sizex, sizey, colorMode, palleteID, x, y);
00137                 sprites[next] = 1;
00138         }
00139         return next;
00140 }
00141 
00148 void spriteManager::replaceSprite(u8 id, void *newSprite)
00149 {
00150         PA_UpdateSpriteGfx(screen, id, newSprite);
00151 }
00152 
00160 s8 spriteManager::loadPallete(void *newpallete)
00161 {
00162         s8 next = getNextPalleteID();
00163         if (next != -1)
00164         {
00165                 PA_LoadSpritePal(screen, next, newpallete);
00166                 palletes[next] = 1;
00167         }
00168         return next;
00169 }
00170 
00176 s8 spriteManager::getNextPalleteID()
00177 {
00178         for(int i=0;i<16;++i)
00179         {
00180                 if(palletes[i]==0)
00181                         return i;
00182         }
00183         #ifdef __MDDEBUG
00184         macros::debugMessage(ERRTEXT, "out of palletes!", ERRORPOS);
00185         #endif
00186         return -1;      //out of palletes!
00187 }
00188 
00194 s8 spriteManager::getNextSpriteID(u8 minIndex)
00195 {
00196         for(int i=minIndex;i<128;++i)
00197         {
00198                 if(sprites[i]==0)
00199                         return i;
00200         }
00201         #ifdef __MDDEBUG
00202         macros::debugMessage(ERRTEXT, "out of sprites!", ERRORPOS);
00203         #endif
00204         return -1;      //out of sprites!
00205 }
00206 
00213 bool spriteManager::spriteSetHasPallete(spriteSet *s)
00214 {
00215         return (s->palleteID != -1);
00216 }
00217 
00224 void spriteManager::spriteSetLoadPallete(spriteSet *s)
00225 {
00226         if (!spriteSetHasPallete(s))
00227                 s->palleteID = loadPallete((void*)s->palleteData);
00228 }
00229 
00238 void spriteManager::loadSpriteSet(spriteSet *target, const unsigned short *p, const unsigned char *s)
00239 {
00240         target->palleteData = p;
00241         target->spriteData = s;
00242         spriteSetLoadPallete(target);
00243 }
00244 
00245 //=====================================================
00246 
00265 void spriteManager::createSingleFireSprite(u8 palID, const unsigned char *gfx, vector<u8> frames, u8 ticksPerFrame, s16 nx, s16 ny, u8 objSize1, u8 objSize2, u8 cx, u8 cy, u8 minIndex, u8 colorMode)
00266 {
00267         if (!totalReset)
00268         {
00269                 frames.push_back(frames[frames.size()-1]);      //this makes the last frame actually show... hax.
00270                 new singleFireSprite(this, palID, gfx, frames, ticksPerFrame, objSize1, objSize2, cx, cy, nx, ny, minIndex, colorMode);
00271         }
00272 }
00273 
00279 void spriteManager::createSingleFireSprite(u8 palID, u16 gfxID, vector<u8> frames, u8 ticksPerFrame, s16 nx, s16 ny, u8 objSize1, u8 objSize2, u8 cx, u8 cy, u8 minIndex, u8 colorMode)
00280 {
00281         if (!totalReset)
00282         {
00283                 frames.push_back(frames[frames.size()-1]);      //this makes the last frame actually show... hax.
00284                 new singleFireSprite(this, palID, gfxID, frames, ticksPerFrame, objSize1, objSize2, cx, cy, nx, ny, minIndex, colorMode);
00285         }
00286 }
00287 
00293 void spriteManager::clearOutSprites(bool keepSound)
00294 {
00295         totalReset = true;
00296         #ifdef __MDDEBUG
00297         char buffer[255];
00298         sprintf(buffer, "%d: CLEARING ALL SPRITES", screen);
00299         macros::debugMessage("spriteManager", buffer);
00300         #endif
00301         for (vector<spriteObject*>::iterator it = gameSprites.begin(); it != gameSprites.end(); ++it)
00302         {
00303                 delete *it;
00304         }
00305         gameSprites.clear();
00306         totalReset = false;
00307         
00308         //reset all sprite indexes
00309         for (u8 i = 0; i < 128; ++i)
00310                 sprites[i] = 0;
00311         //clear out all palletes
00312         for (u8 i = 0; i < 16; ++i)
00313                 palletes[i] = 0;
00314         
00315         #ifdef __WITHSOUND
00316         if (!keepSound)
00317         {
00318                 for (u8 i = 0; i < 8; ++i)              //stop all sounds as well
00319                         stopSound(i);
00320         }
00321         #endif
00322         setAlpha(15);
00323         PA_ResetSpriteSysScreen(screen);
00324 }
00325 
00326 #ifdef __WITHSOUND
00327 
00338 s8 spriteManager::playSound(const u8 *data, const u32* sizePointer, bool repeat, u8 vol)
00339 {
00340         s8 channel = PA_GetFreeSoundChannel();
00341         u32 size = (u32)sizePointer;
00342         if (channel != -1)
00343         {
00344                 PA_PlaySoundEx2(channel, (void*)data, size, vol, 11025, 0, repeat, 0);
00345                 soundLoopings[channel] = repeat;
00346         }
00347         return channel;
00348 }
00349 
00355 void spriteManager::stopSound(s8 channel)
00356 {
00357         if (channel <= -1 || channel > 7)
00358                 return;
00359         PA_StopSound(channel);
00360         soundLoopings[channel] = false;
00361 }
00362 
00369 bool spriteManager::soundLooping(s8 channel)
00370 {
00371         if (channel <= -1 || channel > 7) return false;
00372         return soundLoopings[channel];
00373 }
00374 #endif
00375 
00381 void spriteManager::gameTick()
00382 {
00383         //update screen
00384         vector<spriteObject*>::iterator it = gameSprites.begin();
00385         
00386         while ( it != gameSprites.end() )
00387         {
00388                 if ((*it)->markedForDeletion())
00389                 {
00390                         #ifdef __MDDEBUG
00391                         char buffer[255];
00392                         sprintf(buffer, "%d: DELETING SPRITEOBJECT #%d", screen, it-gameSprites.begin());
00393                         macros::debugMessage("spriteManager", buffer);
00394                         #endif
00395                         delete *it;
00396                         it = gameSprites.erase(it);
00397                 } else {
00398                         (*it)->updateSprite();
00399                         ++it;
00400                 }
00401         }
00402 }
00403 
00408 void spriteManager::resetBackground()
00409 {
00410         PA_ResetBg(screen);
00411         PA_SetVideoMode(screen, 0);
00412 }
00413 
00418 void spriteManager::unloadBackground()
00419 {
00420         PA_DeleteBg(screen, 3);
00421         PA_SetVideoMode(screen, 0);
00422 }
00423 
00429 void spriteManager::resetText()
00430 {
00431         PA_ClearTextBg(screen);
00432         PA_InitCustomText(screen, 2, Font);
00433         PA_SetTextCol(screen, 20, 0, 31);
00434 }

Generated on Tue Mar 13 23:27:53 2007 for MDuel DS by  doxygen 1.5.1-p1