Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "m7sprites.h"
- #if ENABLE_SKY == 1
- extern unsigned char SkyChange;
- #endif
- unsigned char LoadedSpritesSlots[NBSPRT];
- LoadedSprite tabLoadedSprite[NBSPRT];
- unsigned char AddedSprites[MAXSPRT];
- Sprite * AllSpriteList[MAXSPRT];
- unsigned char nbDisplayedSprites;
- DisplayedSprite * DisplayedSpriteList[MAXSPRT];
- DisplayedSprite * Displayed1stSpriteList[MAXSPRT];
- void m7sprites_Init()
- {
- register short i;
- for(i=0;i<NBSPRT;i++)
- LoadedSpritesSlots[i] = 0;
- for(i=0;i<MAXSPRT;i++)
- {
- AddedSprites[i] = 0;
- AllSpriteList[i] = NULL;
- DisplayedSpriteList[i] = NULL;
- Displayed1stSpriteList[i] = NULL;
- }
- nbDisplayedSprites = 0;
- }
- short m7sprites_LoadSprite(ARCH * arch, const char * fileName)
- {
- register unsigned short i;
- short slot;
- short cumul=0;
- ARCH * spriteFile;
- for (i=0;(i<NBSPRT) && (LoadedSpritesSlots[i]);i++);
- if (i == NBSPRT) return -1;
- slot = i;
- spriteFile = aopen(arch, fileName);
- if (spriteFile == NULL) return -1;
- aseek (spriteFile, 2, SEEK_SET);
- aread (&(tabLoadedSprite[slot].NbScales), sizeof(short), 1, spriteFile);
- aread (&(tabLoadedSprite[slot].NbImg), sizeof(short), 1, spriteFile);
- aread (&(tabLoadedSprite[slot].NbAnim), sizeof(short), 1, spriteFile);
- aread (&(tabLoadedSprite[slot].NbDir), sizeof(short), 1, spriteFile);
- tabLoadedSprite[slot].NbImgAnim = (unsigned short *)malloc(sizeof(short) * tabLoadedSprite[slot].NbAnim);
- tabLoadedSprite[slot].ScaledSizes = (unsigned short *)malloc(sizeof(short) * 2 * tabLoadedSprite[slot].NbScales);
- aread ((tabLoadedSprite[slot].NbImgAnim), sizeof(short), tabLoadedSprite[slot].NbAnim, spriteFile);
- aread ((tabLoadedSprite[slot].ScaledSizes), sizeof(short), 2 * tabLoadedSprite[slot].NbScales, spriteFile);
- for (i=0; i<tabLoadedSprite[slot].NbAnim; i++)
- cumul += tabLoadedSprite[slot].NbImgAnim[i];
- cumul *= 2 * tabLoadedSprite[slot].NbDir;
- tabLoadedSprite[slot].Data = (unsigned char *)malloc(sizeof(unsigned char) * cumul);
- aread ((tabLoadedSprite[slot].Data), sizeof(unsigned char), cumul, spriteFile);
- cumul = 0;
- for(i=0; i<tabLoadedSprite[slot].NbScales; i++)
- {
- cumul += tabLoadedSprite[slot].ScaledSizes[i*2] * tabLoadedSprite[slot].ScaledSizes[i*2 + 1];
- }
- cumul *= tabLoadedSprite[slot].NbImg;
- tabLoadedSprite[slot].ImgData = (unsigned char *)malloc(sizeof(unsigned char) * cumul);
- aread ((tabLoadedSprite[slot].ImgData), sizeof(unsigned char), cumul, spriteFile);
- LoadedSpritesSlots[slot] = 1;
- aclose (spriteFile);
- return slot;
- }
- void m7sprites_FreeSprite(unsigned short slot)
- {
- free (tabLoadedSprite[slot].NbImgAnim);
- free (tabLoadedSprite[slot].ScaledSizes);
- free (tabLoadedSprite[slot].Data);
- free (tabLoadedSprite[slot].ImgData);
- LoadedSpritesSlots[slot] = 0;
- }
- void m7sprites_FreeAllSprite()
- {
- register unsigned short i;
- for (i=0; i<NBSPRT; i++)
- if (LoadedSpritesSlots[i])
- m7sprites_FreeSprite(i);
- }
- short m7sprite_SearchFirstID()
- {
- register unsigned short i;
- for (i=0; i<MAXSPRT; i++)
- {
- if (AddedSprites[i] == 0)
- return i;
- }
- return -1;
- }
- short m7sprites_AddSprite(unsigned short slot)
- {
- Sprite * sprite;
- short id = m7sprite_SearchFirstID();
- if (id == -1)
- return -1;
- sprite = (Sprite *)malloc(sizeof(Sprite));
- sprite->id = id;
- sprite->x = 0;
- sprite->y = 0;
- sprite->z = 0;
- sprite->dir = 0;
- sprite->anim = 0;
- sprite->imganim = 0;
- sprite->slot = slot;
- sprite->nbScales = tabLoadedSprite[slot].NbScales;
- AllSpriteList[id] = sprite;
- AddedSprites[id] = 1;
- return id;
- }
- short m7sprites_Add1stSprite(unsigned short slot)
- {
- short id = m7sprites_AddSprite(slot);
- if (id == -1)
- return -1;
- AddedSprites[id] = 2;
- return id;
- }
- void m7sprites_RemoveSprite(unsigned short id)
- {
- free (AllSpriteList[id]);
- AddedSprites[id] = 0;
- }
- void m7sprites_RemoveAllSprites()
- {
- register unsigned short i;
- for (i=0; i<MAXSPRT; i++)
- if (AddedSprites[i])
- m7sprites_RemoveSprite(i);
- }
- void m7sprites_Animate(unsigned short id)
- {
- Sprite * sprite = AllSpriteList[id];
- sprite->imganim++;
- sprite->imganim%=tabLoadedSprite[sprite->slot].NbImgAnim[sprite->anim];
- }
- void m7sprites_SetDisplayed(unsigned short id,
- unsigned short Xscreen,
- unsigned short Yscreen,
- unsigned short Zscreen,
- unsigned short cameraDir,
- unsigned short dir,
- unsigned short scale)
- {
- register unsigned short i;
- unsigned short cumul=0;
- unsigned short imgNum;
- unsigned short maskNum;
- LoadedSprite * ls;
- Sprite * sprite = AllSpriteList[id];
- ls = &(tabLoadedSprite[sprite->slot]);
- sprite->dsprite.Xscreen = Xscreen;
- sprite->dsprite.Yscreen = Yscreen;
- sprite->dsprite.Zscreen = Zscreen;
- sprite->dsprite.width = ls->ScaledSizes[scale * 2];
- sprite->dsprite.height = ls->ScaledSizes[scale * 2 + 1];
- for (i = 0; i<sprite->anim ;i++)
- cumul += 2 * ls->NbImgAnim[i] * ls->NbDir;
- cumul += 2 * (sprite->imganim + (ls->NbImgAnim[sprite->anim] * (((((cameraDir + NBDIR) - dir + (NBDIR/(2*ls->NbDir)))%NBDIR) * ls->NbDir)/NBDIR)));
- imgNum = ls->Data[cumul];
- maskNum = ls->Data[cumul + 1];
- cumul=0;
- for (i=0; i<scale; i++)
- cumul += ls->ScaledSizes[i * 2] * ls->ScaledSizes[i * 2 + 1] * ls->NbImg;
- sprite->dsprite.img = ls->ImgData + cumul + sprite->dsprite.width * sprite->dsprite.height * imgNum;
- sprite->dsprite.mask = ls->ImgData + cumul + sprite->dsprite.width * sprite->dsprite.height * maskNum;
- DisplayedSpriteList[nbDisplayedSprites] = &(sprite->dsprite);
- nbDisplayedSprites++;
- }
- void m7sprites_Set1stDisplayed(unsigned short id, unsigned short dir)
- {
- Sprite * sprite = AllSpriteList[id];
- m7sprites_SetDisplayed(id, sprite->dsprite.Xscreen, sprite->dsprite.Yscreen, sprite->dsprite.Zscreen, 0, dir, sprite->nbScales - 1);
- nbDisplayedSprites--;
- }
- CALLBACK short DisplayedSpriteComp(const void *a, const void *b)
- {
- return (short)(((*((DisplayedSprite**)a)))->Yscreen) -
- (short)(((*((DisplayedSprite**)b)))->Yscreen);
- }
- void m7sprites_SortDisplayedSpriteList()
- {
- qsort (DisplayedSpriteList, nbDisplayedSprites, sizeof (DisplayedSprite *), DisplayedSpriteComp);
- }
- #if ENABLE_GRAYSCALE == 1
- void m7sprite_Display(unsigned short num, unsigned char *Plane0, unsigned char *Plane1)
- #else
- void m7sprite_Display(unsigned short num, unsigned char *Plane0)
- #endif
- {
- register short i, j;
- DisplayedSprite * dsprite = DisplayedSpriteList[num];
- short width = dsprite->width, width2, width3;
- short height = dsprite->height;
- short Xscreen = (dsprite->Xscreen) - (width*4);
- short Yscreen = (dsprite->Yscreen) - height - (dsprite->Zscreen);
- short offset = (Yscreen * 30);
- short clippedup, clippeddown;
- unsigned short shift = Xscreen % 8, hmshift;
- unsigned char * img0 = dsprite->img;
- #if ENABLE_GRAYSCALE == 1
- unsigned char * img1 = dsprite->img + width * height;
- #endif
- unsigned char * mask = dsprite->mask;
- BYTE byteMask;
- BYTE byteImg0;
- #if ENABLE_GRAYSCALE == 1
- BYTE byteImg1;
- #endif
- if (Yscreen < OFFSET_SCR_VERT)
- {
- #if ENABLE_SKY == 1
- #if ENABLE_GRAYSCALE == 1
- SkyChange = 2;
- #else
- SkyChange = 1;
- #endif
- #endif
- }
- if (Xscreen < 0)
- {
- shift = (8 + shift) % 8;
- offset += (Xscreen-7) / 8;
- clippeddown = (CLIP_DOWN - 1) * 30;
- clippedup = (CLIP_UP - 1) * 30;
- }
- else
- {
- offset += (Xscreen / 8);
- clippeddown = CLIP_DOWN*30;
- clippedup = CLIP_UP*30;
- }
- if (shift)
- width2 = width + 1;
- else
- width2 = width;
- hmshift = 8 - shift;
- width3 = width2 - 1;
- for (i=0; (i<height) && (offset < clippeddown); i++)
- {
- if (offset >= clippedup)
- {
- j=0;
- if ((offset + j - ((Yscreen+i) * 30)) < CLIP_RIGHT)
- {
- if ((offset + j - ((Yscreen+i) * 30)) >= CLIP_LEFT)
- {
- if (shift)
- {
- byteMask = (*mask >> shift) | (0xFF << (hmshift));
- byteImg0 = (*img0 >> shift);
- #if ENABLE_GRAYSCALE == 1
- byteImg1 = (*img1 >> shift);
- #endif
- }
- else
- {
- byteMask = *mask;
- byteImg0 = *img0;
- #if ENABLE_GRAYSCALE == 1
- byteImg1 = *img1;
- #endif
- mask++;
- img0++;
- #if ENABLE_GRAYSCALE == 1
- img1++;
- #endif
- }
- Plane0[offset + j] &= byteMask;
- Plane0[offset + j] |= byteImg0;
- #if ENABLE_GRAYSCALE == 1
- Plane1[offset + j] &= byteMask;
- Plane1[offset + j] |= byteImg1;
- #endif
- }
- else
- {
- if (!shift)
- {
- mask++;
- img0++;
- #if ENABLE_GRAYSCALE == 1
- img1++;
- #endif
- }
- }
- }
- else
- {
- mask++;
- img0++;
- #if ENABLE_GRAYSCALE == 1
- img1++;
- #endif
- }
- for (j=1; j<width3; j++)
- {
- if ((offset + j - ((Yscreen+i) * 30)) < CLIP_RIGHT)
- {
- if ((offset + j - ((Yscreen+i) * 30)) >= CLIP_LEFT)
- {
- if (shift)
- {
- byteMask = (*mask << (hmshift)) | (*(mask+1) >> shift);
- byteImg0 = (*img0 << (hmshift)) | (*(img0+1) >> shift);
- #if ENABLE_GRAYSCALE == 1
- byteImg1 = (*img1 << (hmshift)) | (*(img1+1) >> shift);
- #endif
- }
- else
- {
- byteMask = *mask;
- byteImg0 = *img0;
- #if ENABLE_GRAYSCALE == 1
- byteImg1 = *img1;
- #endif
- }
- mask++;
- img0++;
- #if ENABLE_GRAYSCALE == 1
- img1++;
- #endif
- Plane0[offset + j] &= byteMask;
- Plane0[offset + j] |= byteImg0;
- #if ENABLE_GRAYSCALE == 1
- Plane1[offset + j] &= byteMask;
- Plane1[offset + j] |= byteImg1;
- #endif
- }
- else
- {
- mask++;
- img0++;
- #if ENABLE_GRAYSCALE == 1
- img1++;
- #endif
- }
- }
- else
- {
- mask++;
- img0++;
- #if ENABLE_GRAYSCALE == 1
- img1++;
- #endif
- }
- }
- if (j == width3)
- {
- if ((offset + j - ((Yscreen+i) * 30)) < CLIP_RIGHT)
- {
- if ((offset + j - ((Yscreen+i) * 30)) >= CLIP_LEFT)
- {
- if (shift)
- {
- byteMask = (*mask << (hmshift)) | (0xFF >> shift);
- byteImg0 = (*img0 << (hmshift));
- #if ENABLE_GRAYSCALE == 1
- byteImg1 = (*img1 << (hmshift));
- #endif
- }
- else
- {
- byteMask = *mask;
- byteImg0 = *img0;
- #if ENABLE_GRAYSCALE == 1
- byteImg1 = *img1;
- #endif
- }
- mask++;
- img0++;
- #if ENABLE_GRAYSCALE == 1
- img1++;
- #endif
- Plane0[offset + j] &= byteMask;
- Plane0[offset + j] |= byteImg0;
- #if ENABLE_GRAYSCALE == 1
- Plane1[offset + j] &= byteMask;
- Plane1[offset + j] |= byteImg1;
- #endif
- }
- else
- {
- mask++;
- img0++;
- #if ENABLE_GRAYSCALE == 1
- img1++;
- #endif
- }
- }
- else
- {
- mask++;
- img0++;
- #if ENABLE_GRAYSCALE == 1
- img1++;
- #endif
- }
- }
- }
- else
- {
- mask+=width;
- img0+=width;
- #if ENABLE_GRAYSCALE == 1
- img1+=width;
- #endif
- }
- offset+=30;
- }
- }
- #if ENABLE_GRAYSCALE == 1
- void m7sprite_Display1stPlane(unsigned short id, unsigned char *Plane0, unsigned char *Plane1)
- #else
- void m7sprite_Display1stPlane(unsigned short id, unsigned char *Plane0)
- #endif
- {
- register short i, j;
- DisplayedSprite * dsprite = &(AllSpriteList[id]->dsprite);
- short width = dsprite->width;
- short height = dsprite->height;
- short Xscreen = ((dsprite->Xscreen) - (width*4)) >> 3;
- short Yscreen = (dsprite->Yscreen) - height - (dsprite->Zscreen);
- unsigned char * offset0 = Plane0 + (Yscreen * 30) + Xscreen;
- unsigned char * img0 = dsprite->img;
- #if ENABLE_GRAYSCALE == 1
- unsigned char * offset1 = Plane1 + (Yscreen * 30) + Xscreen;
- unsigned char * img1 = dsprite->img + width * height;
- #endif
- unsigned char * mask = dsprite->mask;
- if (Yscreen < OFFSET_SCR_VERT)
- {
- #if ENABLE_SKY == 1
- #if ENABLE_GRAYSCALE == 1
- SkyChange = 2;
- #else
- SkyChange = 1;
- #endif
- #endif
- }
- for(i=height; i--;)
- {
- for(j=width; j--;)
- {
- *offset0 &= *mask;
- #if ENABLE_GRAYSCALE == 1
- *offset1 &= *mask;
- #endif
- mask++;
- *offset0 |= *img0;
- img0++;
- offset0++;
- #if ENABLE_GRAYSCALE == 1
- *offset1 |= *img1;
- img1++;
- offset1++;
- #endif
- }
- offset0 += 30 - width;
- #if ENABLE_GRAYSCALE == 1
- offset1 += 30 - width;
- #endif
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement