[SDL] Small Blit program won't run unlessbreakpointset(Mac/XCode)
Michael Ryan Bannon
ryan.bannon at humagade.com
Mon Jul 3 13:00:39 PDT 2006
And here's the code (just in case somebody makes the same mistakes as me):
#include <SDL/SDL.h>
#include <SDL_image/SDL_image.h>
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 surfaces.
SDL_Surface* pSurfaceOne = IMG_Load("/1.jpg");
SDL_Surface *pSurfaceTwo = IMG_Load("/2.jpg");
// Create window and display surface.
SDL_Surface* pDisplaySurface = SDL_SetVideoMode(800, 600, 32,
SDL_SWSURFACE);
// Display
int alpha = 0;
while(true)
{
// Set alpha.
alpha += 10;
if(alpha > 255)
{
alpha = 0;
SDL_Surface* temp = pSurfaceOne;
pSurfaceOne = pSurfaceTwo;
pSurfaceTwo = temp;
}
// Apply to surfaces and blit.
pSurfaceOne->format->Amask = 0;
pSurfaceTwo->format->Amask = 0;
SDL_SetAlpha(pSurfaceTwo, SDL_SRCALPHA, 255 - alpha);
SDL_BlitSurface(pSurfaceTwo, NULL, pDisplaySurface, NULL);
SDL_SetAlpha(pSurfaceOne, SDL_SRCALPHA, alpha);
SDL_BlitSurface(pSurfaceOne, NULL, pDisplaySurface, NULL);
// Update the surface.
SDL_UpdateRect(pDisplaySurface, 0, 0, 0, 0);
// Make surface black.
Uint32 mappedColor = SDL_MapRGBA(pDisplaySurface->format, 0, 0, 0,
0);
SDL_FillRect(pDisplaySurface, NULL, mappedColor);
}
return 0;
}
"Michael Ryan Bannon" <ryan.bannon at humagade.com> wrote in message
news:e8bs70$u7m$1 at sea.gmane.org...
> Nevermind...just figured it out...have to fill the display surface with a
> black rectangle (or some other color).
>
> Thanks,
>
> Ryan
>
> "Michael Ryan Bannon" <ryan.bannon at humagade.com> wrote in message
> news:e8bp1d$j6g$1 at sea.gmane.org...
>> Hey,
>>
>> Thanks for the response.
>>
>> First, I'm trying to recreate some code from the engine I'm porting to
>> Mac, so that's why I read the file into memory (that's what they were
>> doing originally, so I just kept with it). Also, I don't know SDL or
>> SDL_Image that well, so I missed that part.
>>
>> Regarding portability, I'm not really worrying about that right now.
>> I've got bigger problems :)
>>
>> As for explaining how I expect it to work, I guess I can't considering
>> that it's not working. Again, I don't know SDL that well. What I
>> thought would happen is to blit the picture surface onto the display
>> surface every iteration. And at every iteration the alpha would increase
>> until it's reset to 0. What actually is happening is that it goes up to
>> 255 but doesn't reset itself.
>>
>> I assumed that the blit cleared the display when I called it, but I guess
>> not. What should I be doing differently? It should start as black, fade
>> into full alpha, then start again. So, how exactly should I be clearing
>> the blit?
>>
>> Thanks,
>>
>> Ryan
>>
>> "Christian Walther" <cwalther at gmx.ch> wrote in message
>> news:e8bnrg$f6b$1 at sea.gmane.org...
>>> 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
More information about the SDL
mailing list