Пример #1
0
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;
}
Пример #2
0
void SWUnlockFrame(
    FramePtr srcFrameP)
{
    SW_ASSERT(srcFrameP != 0);

    if (srcFrameP->isFrameLocked)
    {
        srcFrameP->isFrameLocked = false;
    }
}
Пример #3
0
void SWLockFrame(
    FramePtr srcFrameP)
{
    SW_ASSERT(srcFrameP != 0);

    if (!srcFrameP->isFrameLocked)
    {
        srcFrameP->isFrameLocked = true;
    }
}
Пример #4
0
SWError SWCreateFrameFromSurfaceAndRect(
    FramePtr* newFrameP,
    SDL_Surface *surfaceP,
    SWRect* frameRectP )
{
    SWError     err = kNoError;
    FramePtr    tempFrameP;
    
    SW_ASSERT( surfaceP != 0 );
    SW_ASSERT( frameRectP->top >= 0 && frameRectP->left >= 0 &&
        frameRectP->right > frameRectP->left && frameRectP->bottom > frameRectP->top);
    
    tempFrameP = NULL;
    *newFrameP = NULL;
    
    err = SWCreateFrame( &tempFrameP );
    
    if( err == kNoError )
    {
        tempFrameP->frameSurfaceP = surfaceP;
        tempFrameP->frameRect = *frameRectP;
        tempFrameP->sharesSurface = true;
                
                err = SWUpdateFrame( tempFrameP );
    }
    
    if( err == kNoError )
        {
            *newFrameP = tempFrameP;
        }
        else
        {
            if ( tempFrameP )
            {
                free( tempFrameP );
            }
        }
    
    return err;
}
Пример #5
0
SWError SWCloneSprite(
	SpritePtr	cloneSpriteP,
	SpritePtr*	newSpriteP,
	void*		spriteStorageP)
{
	SpritePtr 		tempSpriteP;
	short 			index;
	int				frameArraySize;
	SWError 		err = kNoError;

	SW_ASSERT(cloneSpriteP != NULL);

		// Allocate memory for the new Sprite
	tempSpriteP = (spriteStorageP == NULL) ? (SpritePtr)malloc(sizeof(SpriteRec)) : (SpritePtr)spriteStorageP;
	if (tempSpriteP == NULL)
		err = kMemoryAllocationError;

	if (err == kNoError)
	{
			// Make sure the Ptr passed to this function is large enough.
		//SW_ASSERT( GetPtrSize((Ptr)tempSpriteP) >= sizeof(SpriteRec) );
		
			// copy the clone sprite into the temp sprite
		*tempSpriteP = *cloneSpriteP;

			// clear the layer fields, in case the parent sprite is in a layer
		tempSpriteP->parentSpriteLayerP = NULL;
		tempSpriteP->nextSpriteP = NULL;
		tempSpriteP->prevSpriteP = NULL;

			// Initialize other fields
		tempSpriteP->spriteMoveProc = SWTempSpriteMoveProc;
		tempSpriteP->realSpriteMoveProc = cloneSpriteP->realSpriteMoveProc;
		tempSpriteP->drawData = (DrawDataPtr)calloc(1,sizeof(DrawDataRec));

		*(tempSpriteP->drawData) = *(cloneSpriteP->drawData);
		tempSpriteP->drawData->parentSpriteP = tempSpriteP;
	}

	if (err == kNoError)
	{
			// Create a new frameArray for this sprite
		frameArraySize = cloneSpriteP->maxFrames * sizeof(FramePtr);
		tempSpriteP->frameArray = (FramePtr *)calloc(1,frameArraySize);
		
		if (tempSpriteP->frameArray != NULL)
		{
				// copy the frame array, and increment useCount for each frame
			for (index = 0; index < cloneSpriteP->maxFrames; index++)
			{
				tempSpriteP->frameArray[index] = cloneSpriteP->frameArray[index];
				SW_ASSERT(tempSpriteP->frameArray[index] != NULL );
				tempSpriteP->frameArray[index]->useCount++;
			}

		}
		else
		{
			err = kMemoryAllocationError;
			free((void*)tempSpriteP);
		}
	}

	if (err == kNoError)
		*newSpriteP = tempSpriteP;

	SWSetStickyIfError( err );
	return err;
}
Пример #6
0
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;
    }
}
Пример #7
0
SWError SWUpdateFrame (
    FramePtr srcFrameP)
{
    SWError err = kNoError;
    SDL_Surface *tempSurfaceP = 0;
    //int hasAlpha;
    SDL_Surface *surf;
    SDL_PixelFormat *fmt;
  //  SDL_Rect rect;
    
    SW_ASSERT(srcFrameP != 0);

    if (srcFrameP->originalSurfaceP)
    {
        surf = srcFrameP->originalSurfaceP;
        
        if (SDL_GetVideoSurface() != NULL)
        {
               /* NOTE: If this is an offscreen frame (used for work area, etc...),
                        and it has an alpha channel this will use "SDL_DisplayFormatAlpha"
                        to create the formatted surface... which DOES NOT WORK */
            if ( surf->flags & SDL_SRCALPHA )
            {
                tempSurfaceP = SDL_DisplayFormatAlpha(surf);
            }
            else
            {
                tempSurfaceP = SDL_DisplayFormat(surf);
            }
        }
        else
        {
            fmt = surf->format; // should be the destination surface format

            tempSurfaceP = SDL_CreateRGBSurfaceFrom(
                surf->pixels, surf->w, surf->h, fmt->BitsPerPixel, surf->pitch,
                fmt->Rmask, fmt->Gmask, fmt->Bmask, fmt->Amask );
        }
        
        if (!tempSurfaceP)
        {
            err = kSDLSurfaceConversion;
        }
        
        if (err == kNoError)
        {
            if( srcFrameP->frameSurfaceP )
            {
                SDL_FreeSurface(srcFrameP->frameSurfaceP);
            }
            
            srcFrameP->frameSurfaceP = tempSurfaceP;
        }
    }

#ifdef HAVE_OPENGL
    if (gSWUseOpenGL && err == kNoError && !srcFrameP->isVideoSurface)
    {
        if (srcFrameP->glSurfaceP != NULL)
        {
            BKFreeGLSurface( (BK_GL_Surface *) srcFrameP->glSurfaceP );
        }
        
        SW_CONVERT_SW_TO_SDL_RECT(srcFrameP->frameRect, rect);
        
        srcFrameP->glSurfaceP = BKCreateNewGLSurface( srcFrameP->frameSurfaceP, srcFrameP->sharesSurface ? &rect : NULL );
        if (srcFrameP->glSurfaceP == NULL)
        {
            fprintf(stderr, "BKCreateNewGLSurface: %s\n", SDL_GetError() );
            err =  kSDLSurfaceConversion;
        }
    }
#endif // OpenGL
        
        if ( err == kNoError )
        {
            // Update the frame rect if the frame surface isn't shared (it isn't a portion of a surface)
            if ( !srcFrameP->sharesSurface )
            {
                SW_SET_RECT( srcFrameP->frameRect, 0, 0, srcFrameP->frameSurfaceP->w, srcFrameP->frameSurfaceP->h );
            }
            
            // Set mask type
            if ( srcFrameP->frameSurfaceP->flags & SDL_SRCALPHA ) {
                srcFrameP->maskType = kAlphaChannelMask;
            } else {
                srcFrameP->maskType = kNoMask;
            }
        }
        else
        {
            if ( tempSurfaceP )
            {
                SDL_FreeSurface( tempSurfaceP );
            }
        }
        
    return err;
}
void run_tests()
{
    HashTable table, self_destruct_table;
    unsigned long hashres;
    int i;
    char* str, *filename;
    TokenizerT tok = NULL;
    Entry ent;
    
    /* Test table Creation */
    
    table = createHT(NULL, NULL, NULL, NULL, NULL);
    SW_ASSERT(table == NULL, "Hashing function must be defined.", tests_run, failures);
    
    table = createHT(hash, NULL, NULL, NULL, NULL);
    SW_ASSERT(table == NULL, "Comparison function must be defined.", tests_run, failures);
    
    table = createHT(hash, compStrings, NULL, NULL, NULL);
    SW_ASSERT(table != NULL, "All required inputs defined, No destroy functions.", tests_run, failures);
    
    self_destruct_table = createHT(hash, compStrings, destroyString, destroyEntry, printPair);
    SW_ASSERT(self_destruct_table != NULL, "All valid inputs produces a new HashTable.", tests_run, failures);
    
    /* Test the Hash / Comp functions */
    hashres = hash("Test String");
    SW_ASSERT(hashres == 631841783, "Basic string hash.", tests_run, failures);
    
    hashres = hash("REALLLY REALLLY LONG, out of Contr0l $tr1nG th@t g3tz h(@)$hEd 4 Wh*t3v34 rezzzion....;';");
    SW_ASSERT(hashres == 238349644, "Long string hash.", tests_run, failures);

    hashres = hash("");
    SW_ASSERT(hashres == 0, "Empty string hash.", tests_run, failures);
    
    /* Test insert function */
    i = insertHT(NULL, (void *)"REALLLY REALLLY LONG, out of Contr0l $tr1nG th@t g3tz h(@)$hEd 4 Wh*t3v34 rezzzion....;';", (void *)1);
    SW_ASSERT(i == 0, "Cannot insert into NULL table.", tests_run, failures);
    
    i = insertHT(table, (void *)"REALLLY REALLLY LONG, out of Contr0l $tr1nG th@t g3tz h(@)$hEd 4 Wh*t3v34 rezzzion....;';", (void *)1);
    SW_ASSERT(i == 1, "Insert normal data into table", tests_run, failures);
    
    tok = TKCreate(ALLOWED_CHARS, "/home/swift/Documents/Class/S2011/cs214/Project/bin/files/file1.txt");
    
    filename = "file1.txt";
    
    while((str = TKGetNextToken(tok)) != 0)
    {
        ent = (Entry) malloc( sizeof(struct Entry_) );
        ent->filename = strdup(filename);
        ent->frequency = 1;
        
        i = insertHT(self_destruct_table, (void *)str, (void *) ent);
    }
    
    TKDestroy(tok);
    tok = NULL;
    
    /* Testing Search Functions */
    searchHT(NULL, NULL);
    searchHT(table, NULL);
    
    ent = (Entry) searchHT(self_destruct_table, "varius");
    SW_ASSERT(ent != NULL, "Found valid key.", tests_run, failures);    
    
    ent = (Entry) searchHT(self_destruct_table, "");
    SW_ASSERT(ent == NULL, "Could not find invalid key.", tests_run, failures);   
    
    /* Testing removal functions */
    removeHT(self_destruct_table, "tempor");
    removeHT(self_destruct_table, "tempor");
    removeHT(self_destruct_table, "tempor");
    removeHT(self_destruct_table, "donec");
    
    
    /* Testing Print Functions */
    toStringHT(NULL);
    toStringHT(table);
    toStringHT(self_destruct_table);
    
    rehash(self_destruct_table);
    
    toStringHT(self_destruct_table);
    
    /* Destroy the tables */
    destroyHT(table);
    table = NULL;
    
    destroyHT(self_destruct_table);
    self_destruct_table = NULL;
}