// first and simplest 2d module #include "2d1.h" // NO overall initialiser void InitialiseTexture (long address) { RECT rect; GsIMAGE timInfo; GsGetTimInfo((u_long *)(address+4), &timInfo); rect.x = timInfo.px; rect.y = timInfo.py; rect.w = timInfo.pw; rect.h = timInfo.ph; LoadImage(&rect, timInfo.pixel); if ( (timInfo.pmode>>3) & 0x01 ) { rect.x = timInfo.cx; rect.y = timInfo.cy; rect.w = timInfo.cw; rect.h = timInfo.ch; LoadImage(&rect, timInfo.clut); } DrawSync(0); } // the second argument is a global declared next to the #define for // the TIM file main memory address; // it allows endless sprites to be easily linked to it later on void ProperInitialiseTexture (long address, GsIMAGE* imageInfo) { RECT rect; // +4 because we need to skip head of TIM file GsGetTimInfo( (u_long *)(address+4), imageInfo); rect.x = imageInfo->px; rect.y = imageInfo->py; rect.w = imageInfo->pw; rect.h = imageInfo->ph; LoadImage( &rect, imageInfo->pixel); // if image needs a CLUT, load it up // (pmode: 8 or 9 ==> texture is 4-bit or 8-bit) if ( (imageInfo->pmode>>3)&0x01) { rect.x = imageInfo->cx; rect.y = imageInfo->cy; rect.w = imageInfo->cw; rect.h = imageInfo->ch; LoadImage( &rect, imageInfo->clut); } // wait for loading to finish DrawSync(0); } void LinkSpriteToImageInfo (GsSPRITE* sprite, GsIMAGE* imageInfo) { int widthCompression; // on frame buffer int tPageType; int texturePageX, texturePageY, xOffset, yOffset; InitGsSprite(sprite); FindTopLeftOfTexturePage(imageInfo, &texturePageX, &texturePageY, &xOffset, &yOffset); switch(imageInfo->pmode) { case 0: // 16-bit sprite->attribute |= SIXTEEN_BIT_MASK; widthCompression = 1; tPageType = 2; break; case 8: // 4-bit widthCompression = 4; tPageType = 0; sprite->cx = imageInfo->cx; sprite->cy = imageInfo->cy; break; case 9: // 8-bit sprite->attribute |= EIGHT_BIT_MASK; widthCompression = 2; tPageType = 1; sprite->cx = imageInfo->cx; sprite->cy = imageInfo->cy; break; default: printf("Only 4, 8 and 16 bit modes supported\n"); assert(FALSE); // other modes not supported } sprite->tpage = GetTPage(tPageType, 0, texturePageX, texturePageY); sprite->w = imageInfo->pw * widthCompression; sprite->h = imageInfo->ph; sprite->u = xOffset; sprite->v = yOffset; } void InitGsSprite (GsSPRITE* sprite) { // initialise sprite to dummy sprite->attribute = 0; sprite->x = 0; sprite->y = 0; sprite->w = 0; sprite->h = 0; sprite->tpage = 0; sprite->u = 0; sprite->v = 0; sprite->cx = 0; sprite->cy = 0; sprite->r = 128; sprite->g = 128; sprite->b = 128; sprite->mx = 0; sprite->my = 0; sprite->scalex = ONE; sprite->scaley = ONE; sprite->rotate = 0; } // find coords wrt tpage top left points void FindTopLeftOfTexturePage (GsIMAGE* imageInfo, int* x, int* y, int* u, int* v) { int testX, testY; testX = imageInfo->px; testY = imageInfo->py; *u = 0; *v = 0; for (;;) { if (testX % 320 != 0) { testX--; (*u)++; } else break; } for (;;) { if (testY % 256 != 0) { testY--; (*v)++; } else break; } *x = testX; *y = testY; }