Small Blit program won't run unless breakpoint set (Mac/XCode)

Hello,

In the attempts to make an example for another problem I was having, I
started to put together a code sample. But, naturally, I can’t get the code
sample to work.

The following is the beginning of my sample. Two problems: first, it only
works if I set a breakpoint early in the “main” and second, the alpha calls
aren’t doing anything.==========================================
#include <Carbon/Carbon.h>
#include <SDL/SDL.h>
#include <SDL_image/SDL_image.h>

void GetPixelData(void** aData, int* aSize, const char* aPath)
{
// Get file ref.
FSRef fileRef;
FSPathMakeRef((const UInt8*)(aPath), &fileRef, NULL);

// Get catalog info and data fork name (which is nothing).
FSCatalogInfo catalogInfo;
FSGetCatalogInfo(&fileRef, kFSCatInfoDataSizes, &catalogInfo, NULL, 

NULL, NULL);
HFSUniStr255 dataForkName;
FSGetDataForkName(&dataForkName);

// Open data fork.
SInt16 dataForkNumber = -1;
OSStatus error = FSOpenFork(&fileRef, dataForkName.length, 

dataForkName.unicode, fsCurPerm, &dataForkNumber);

// Get position of fork.
SInt64 position = -1;
error = FSGetForkPosition(dataForkNumber, &position);

// Set size.
*aSize = catalogInfo.dataLogicalSize;

// Read the fork.
ByteCount actualCount;
*aData = new char[*aSize];
error = FSReadFork(dataForkNumber, fsFromStart, position, *aSize, 

(void*)(*aData), &actualCount);

// Close the fork.
FSCloseFork(dataForkNumber);

}

int main(int argc, char*argv[])
{
// Initialize SDL with the given driverName. This driver is specified by
setting the
// environment variable, “SDL_VIDEODRIVER” to driverName.
int error = SDL_Init(SDL_INIT_VIDEO);
SDL_putenv(“SDL_VIDEODRIVER=Quartz”);

// Create window and display surface.

SDL_Surface* pDisplaySurface = SDL_SetVideoMode(800, 600, 32,
SDL_SWSURFACE);
pDisplaySurface->format->Amask = 0;

// Get pixel data.
void* pPixelDataOne;
int sizePixelDataOne = 0;
GetPixelData(&pPixelDataOne, &sizePixelDataOne, "/1.jpg");

// Create surfaces.

SDL_RWops *rwOne = SDL_RWFromMem(pPixelDataOne, sizePixelDataOne);
SDL_Surface *pSurfaceOne = IMG_Load_RW(rwOne, 0);

// Display
int alpha = 0;
while(true)
{
    alpha += 10;
    if(alpha > 255)
    {
        alpha = 0;
    }
    pSurfaceOne->format->Amask = 0;
    SDL_SetAlpha(pSurfaceOne, SDL_SRCALPHA, alpha);
    SDL_BlitSurface(pSurfaceOne, NULL, pDisplaySurface, NULL);
}

return 0;

}

Please note that I’m using XCode 2.3 on Mac OS 10.4.6. The SDL frameworks
are universal binary (as is my project). The code is experiencing problems
with both the PPC and i386. Also I just stuck my jpg resource (1.jpg) in
the volume root…I’m lazy.

Thanks,

Ryan

ps - I now it sounds stupid that I couldn’t make a simple sample even though
I had an already working bigger SDL project, but I’m working on a port…so
it wasn’t originally my code :slight_smile:

Sorry…I was not doing an SDL_UpdateRect on the display surface. The jpeg
appears each time, but the alpha calls that I’m doing still don’t seem to be
working.

“Michael Ryan Bannon” <ryan.bannon at humagade.com> wrote in message
news:e8b8ck$olg$1 at sea.gmane.org…> Hello,

In the attempts to make an example for another problem I was having, I
started to put together a code sample. But, naturally, I can’t get the
code sample to work.

The following is the beginning of my sample. Two problems: first, it only
works if I set a breakpoint early in the “main” and second, the alpha
calls aren’t doing anything.

==========================================
#include <Carbon/Carbon.h>
#include <SDL/SDL.h>
#include <SDL_image/SDL_image.h>

void GetPixelData(void** aData, int* aSize, const char* aPath)
{
// Get file ref.
FSRef fileRef;
FSPathMakeRef((const UInt8*)(aPath), &fileRef, NULL);

// Get catalog info and data fork name (which is nothing).
FSCatalogInfo catalogInfo;
FSGetCatalogInfo(&fileRef, kFSCatInfoDataSizes, &catalogInfo, NULL,
NULL, NULL);
HFSUniStr255 dataForkName;
FSGetDataForkName(&dataForkName);

// Open data fork.
SInt16 dataForkNumber = -1;
OSStatus error = FSOpenFork(&fileRef, dataForkName.length,
dataForkName.unicode, fsCurPerm, &dataForkNumber);

// Get position of fork.
SInt64 position = -1;
error = FSGetForkPosition(dataForkNumber, &position);

// Set size.
*aSize = catalogInfo.dataLogicalSize;

// Read the fork.
ByteCount actualCount;
*aData = new char[*aSize];
error = FSReadFork(dataForkNumber, fsFromStart, position, aSize,
(void
)(*aData), &actualCount);

// Close the fork.
FSCloseFork(dataForkNumber);
}

int main(int argc, char*argv[])
{
// Initialize SDL with the given driverName. This driver is specified by
setting the
// environment variable, “SDL_VIDEODRIVER” to driverName.
int error = SDL_Init(SDL_INIT_VIDEO);
SDL_putenv(“SDL_VIDEODRIVER=Quartz”);

// Create window and display surface.
SDL_Surface* pDisplaySurface = SDL_SetVideoMode(800, 600, 32,
SDL_SWSURFACE);
pDisplaySurface->format->Amask = 0;

// Get pixel data.
void* pPixelDataOne;
int sizePixelDataOne = 0;
GetPixelData(&pPixelDataOne, &sizePixelDataOne, “/1.jpg”);

// Create surfaces.
SDL_RWops *rwOne = SDL_RWFromMem(pPixelDataOne, sizePixelDataOne);
SDL_Surface *pSurfaceOne = IMG_Load_RW(rwOne, 0);

// Display
int alpha = 0;
while(true)
{
alpha += 10;
if(alpha > 255)
{
alpha = 0;
}
pSurfaceOne->format->Amask = 0;
SDL_SetAlpha(pSurfaceOne, SDL_SRCALPHA, alpha);
SDL_BlitSurface(pSurfaceOne, NULL, pDisplaySurface, NULL);
}

return 0;
}

Please note that I’m using XCode 2.3 on Mac OS 10.4.6. The SDL frameworks
are universal binary (as is my project). The code is experiencing
problems with both the PPC and i386. Also I just stuck my jpg resource
(1.jpg) in the volume root…I’m lazy.

Thanks,

Ryan

ps - I now it sounds stupid that I couldn’t make a simple sample even
though I had an already working bigger SDL project, but I’m working on a
port…so it wasn’t originally my code :slight_smile:

Michael Ryan Bannon wrote:

But, naturally, I can’t get the code sample to work.

I didn’t try your code, but a few comments anyway:

  • Why are you reading the file into memory before decoding it with
    SDL_image, when you could just have SDL_image read the file itself? (And
    using this complicated Carbon code at that, when simple stdio would do
    the job?)

#include <SDL/SDL.h>
#include <SDL_image/SDL_image.h>

Although I guess it doesn’t matter for this sample code, I’d like to
point out that this isn’t portable. SDL.h happens to be in an “SDL"
directory on Linux (and I guess other Unixes), so that would work, but
SDL_image.h probably isn’t in an “SDL_image” directory (or framework)
anywhere but on Mac OS X. The portable way is to #include “SDL.h” and
tell the compiler where it is in the platform-specific build system
(e.g. on Mac OS X add “/Library/Frameworks/SDL.framework/Headers” to the
"Header Search Paths” setting in Xcode, or specify
"-I/Library/Frameworks/SDL.framework/Headers" on the command line). The
same for SDL_image.h.

the alpha calls that I’m doing still don’t seem to be working.

// Display
int alpha = 0;
while(true)
{
alpha += 10;
if(alpha > 255)
{
alpha = 0;
}
pSurfaceOne->format->Amask = 0;
SDL_SetAlpha(pSurfaceOne, SDL_SRCALPHA, alpha);
SDL_BlitSurface(pSurfaceOne, NULL, pDisplaySurface, NULL);
}

Care to explain how you expect it to work (after you add the missing
SDL_UpdateRect)? I expect this code to blit the same image onto itself
repeatedly in very fast succession (with varying alpha values), so
except for the first split second you won’t see anything but this image.
Perhaps you meant to clear the display surface between blits, but even
then you’ll probably only get flicker unless you add some SDL_Delay to
the loop.

Hope this helps

-Christian

pDisplaySurface->format->Amask = 0;

I don’t think you can do this and have it work.

–ryan.