SpritePtr CreateStatsSpriteClone( SpritePtr masterSpriteP, short numDigits, JustifyType justification, int fillWithZeros) { StatsStructPtr spriteStructP; SpritePtr newSpriteP; SWError err = kNoError; SW_ASSERT(masterSpriteP != NULL); SW_ASSERT(masterSpriteP->maxFrames >= 10); // Allocate memory for the StatsStruct spriteStructP = (StatsStructPtr)calloc(1,sizeof(StatsStruct)); if (!spriteStructP) { err = kMemoryAllocationError; } if (err == kNoError) { err = SWCloneSprite(masterSpriteP, &newSpriteP, spriteStructP); } if (err == kNoError) { // Set the Sprite's DrawProc to our special digit-drawing DrawProc. newSpriteP->frameDrawProc = StatsItemDrawProc; // Adjust the Sprite's width to match the width of all the digits put together. newSpriteP->drawData->scaledWidth = SW_RECT_WIDTH(newSpriteP->destFrameRect) * numDigits; newSpriteP->drawData->scaledHeight = SW_RECT_HEIGHT(newSpriteP->destFrameRect); SWSetCurrentFrameIndex(newSpriteP, newSpriteP->curFrameIndex); spriteStructP->numDigits = numDigits; spriteStructP->theNum = -1; spriteStructP->justification = justification; spriteStructP->fillWithZeros = fillWithZeros; spriteStructP->drawProc = SWStdSpriteDrawProc; } else { newSpriteP = NULL; } return newSpriteP; }
SWError st_CreateStaticText( const char* string, const char* fontPath, int fontSize, SWRect* bounds, int wrap, StaticTextPtr* newStaticTextPP ) { SWError err = kNoError; FramePtr frameP = NULL; StaticTextPtr tempStaticTextP = NULL; *newStaticTextPP = 0; if( ! gSTHasBeenInitialized ) err = kSTHasNotBeenInitedError; if( err == kNoError ) if( strlen( string ) > kMaxStringLength - 1 ) err = kSTStringTooLongError; if( err == kNoError ) if( strlen( fontPath ) > kMaxPathLength - 1 ) err = kSTPathTooLongError; if( err == kNoError ) { tempStaticTextP = malloc( sizeof( StaticTextRec ) ); if( ! tempStaticTextP ) err = kMemoryAllocationError; } if( err == kNoError ) err = SWCreateSprite( (SpritePtr*)&tempStaticTextP, tempStaticTextP, 1 ); if( err == kNoError ) { SWSetSpriteMoveProc( (SpritePtr)tempStaticTextP, handleStaticText ); // make the frame to which we will be copying each line of text err = SWCreateBlankFrame( &frameP, SW_RECT_WIDTH( *bounds ), SW_RECT_HEIGHT( *bounds ), SDL_GetVideoInfo()->vfmt->BitsPerPixel, true ); } if( err == kNoError ) { SWSetSpriteLocation( (SpritePtr)tempStaticTextP, bounds->left, bounds->top ); err = SWAddFrame( (SpritePtr)tempStaticTextP, frameP ); } if( err == kNoError ) { strcpy( tempStaticTextP->string, string ); strcpy( tempStaticTextP->fontPath, fontPath ); tempStaticTextP->fontSize = fontSize; tempStaticTextP->fontStyle = gDefaultStyle; tempStaticTextP->wrap = wrap; tempStaticTextP->bounds = *bounds; tempStaticTextP->align = gDefaultAlign; tempStaticTextP->rendering = gDefaultRendering; tempStaticTextP->textColor = gDefaultTextColor; tempStaticTextP->backColor = gDefaultBackColor; err = SWSetCurrentFrameIndex( (SpritePtr)tempStaticTextP, 0 ); } if( err == kNoError ) err = renderStaticText( tempStaticTextP ); if( err == kNoError ) { SWLockSprite( (SpritePtr)tempStaticTextP ); *newStaticTextPP = tempStaticTextP; } if( err != kNoError ) { // clean up what we can if( tempStaticTextP ) st_DisposeStaticText( &tempStaticTextP ); if( frameP ) SWDisposeFrame( &frameP ); } SWSetStickyIfError( err ); return err; }
void StatsItemDrawProc( FramePtr srcFrameP, FramePtr dstFrameP, SWRect* srcRectP, SWRect* dstRectP) { short n, theDigit, width, index, fillValue; long theNum; char digitArray[10]; // Enough digits for a long StatsStructPtr statsStructP; FramePtr *srcFrameArray; SWRect destRect, srcRect, frameRect, clippedDstRect; SW_ASSERT(gSWCurrentElementDrawData != NULL); statsStructP = (StatsStructPtr)gSWCurrentElementDrawData->parentSpriteP; srcFrameArray = gSWCurrentElementDrawData->parentSpriteP->frameArray; theNum = statsStructP->theNum; if (statsStructP->fillWithZeros) fillValue = 0; else fillValue = -1; // Fill the unused part of the display with either nothing or 0s. for (n=0; n < statsStructP->numDigits; n++) digitArray[n] = fillValue; // Put the number into the digitArray if (theNum >= 0) { if (statsStructP->justification == kLeftJustify) index = GetNumberLength(theNum)-1; else index = statsStructP->numDigits-1; do { // Fill digitArray with the digits from the new number digitArray[index] = theNum%10; // Get the right-most digit index--; theNum /= 10; // Remove the right-most digit } while (theNum > 0 && index >= 0); } destRect = *dstRectP; frameRect = srcFrameArray[0]->frameRect; width = SW_RECT_WIDTH(frameRect); // Expand destRect's left and top to its original size before it was clipped if (srcRectP->top > frameRect.top) destRect.top += frameRect.top - srcRectP->top; if (srcRectP->left > frameRect.left) destRect.left += frameRect.left - srcRectP->left; // The destRect's width and height should be the size of one digit. destRect.bottom = destRect.top + SW_RECT_HEIGHT(frameRect); destRect.right = destRect.left + width; // Draw the number for (index = 0; index < statsStructP->numDigits; index++) { theDigit = digitArray[index]; if (theDigit >= 0) { srcFrameP = srcFrameArray[theDigit]; srcRect = srcFrameP->frameRect; // Clip each digit with the dstRect passed to this drawProc. clippedDstRect = destRect; SW_CLIP_DST_AND_SRC_RECT(clippedDstRect, srcRect, (*dstRectP)); // Set the sprite's location (used by hardware drawprocs) gSWCurrentElementDrawData->horizLoc = clippedDstRect.left; gSWCurrentElementDrawData->vertLoc = clippedDstRect.top; if (gSWCurrentSpriteWorld != NULL) { gSWCurrentElementDrawData->horizLoc += gSWCurrentSpriteWorld->visScrollRect.left; gSWCurrentElementDrawData->vertLoc += gSWCurrentSpriteWorld->visScrollRect.top; } // Draw the digit (*statsStructP->drawProc)(srcFrameP, dstFrameP, &srcRect, &clippedDstRect); } destRect.left += width; destRect.right += width; } }