Whenever it gets to the SDL_GetRGBA call, the program just closes with no error
messages (I’m using FreePascal and have compiled with debug information). If I
comment out the SDL_GetRGBA call, everything’s fine. Am I declaring my variables
incorrectly, or can you spot some other error?
Whenever it gets to the SDL_GetRGBA call, the program just closes with no error
messages (I’m using FreePascal and have compiled with debug information). If I
comment out the SDL_GetRGBA call, everything’s fine. Am I declaring my variables
incorrectly, or can you spot some other error?
Sort of. You can’t make assumptions like that. But part of the problem is
whoever made the header. r, g, b, and a should be declared as "var byte,"
not “PUint8”. Fix your header, adjust your code accordingly, and see if
the problem doesn’t go away.>----- Original Message ----
From: Christian Knudsen
Subject: Re: [SDL] SDL_GetRGBA exits program.
Sort of. You can’t make assumptions like that. But part of the problem is
whoever made the header. r, g, b, and a should be declared as "var byte,"
not “PUint8”. Fix your header, adjust your code accordingly, and see if
the problem doesn’t go away.
I changed my code and the header. No change. And I’d honestly be surprised if
that header was wrong, as those FreePascal headers from the JEDI-SDL project
have been used for a couple of years and nobody has ever complained about them.
Sort of. You can’t make assumptions like that. But part of the problem is
whoever made the header. r, g, b, and a should be declared as "var byte,"
not “PUint8”. Fix your header, adjust your code accordingly, and see if
the problem doesn’t go away.
I changed my code and the header. No change. And I’d honestly be surprised if
that header was wrong, as those FreePascal headers from the JEDI-SDL project
have been used for a couple of years and nobody has ever complained about them.
Strange. That really should have fixed it. Looks to me like the problem was that you
were passing pointers that you hadn’t initialized yet. And yes, the header is wrong,
or at least sub-optimal. It’s technically correct, but that’s not the Pascal way to do
pass-by-reference, mostly because it can lead to problems with bad pointers. A
declaration like that is a classic case of “writing C in Pascal,” which is ugly.
If you’ve changed the code and it still doesn’t work, then there’s probably some
problem either with your SDL library or with FPC’s binding. What OS are you on,
and what version of SDL are you using?>----- Original Message ----
From: Christian Knudsen
Subject: Re: [SDL] SDL_GetRGBA exits program.
Just to make the usage clear, in C you set up the variables:
Uint8 r, g, b, a;
SDL_Surface* theimage = IMG_Load(“test.png”);
Uint32 pixel = SDL_GetPixel(theimage, x, y);
Then pass the color components by reference (pass pointers to them):
SDL_GetRGBA(pixel, theimage->format, &r, &g, &b, &a);
SDL fills in the color components found in ‘pixel’ according to the
format that is given. The memory for the color component variables
must be allocated already (I’ve done this statically here, instead of
using ‘malloc’ or ‘new’) or else bad things will happen (like
crashes).
Also, I should note that your SDL_GetPixel call has swapped ‘x’ and ‘y’.
Hope that helps,
Jonny DOn Tue, Sep 1, 2009 at 9:33 AM, Christian Knudsen wrote:
SDL fills in the color components found in ‘pixel’ according to the
format that is given. The memory for the color component variables
must be allocated already (I’ve done this statically here, instead of
using ‘malloc’ or ‘new’) or else bad things will happen (like
crashes).
Also, I should note that your SDL_GetPixel call has swapped ‘x’ and ‘y’.
Hope that helps,
Jonny D
I guess I’m not allocating memory for the ‘r’, ‘g’, ‘b’ and ‘a’ variables, then.
And the swapped ‘x’ and ‘y’ are just a typo when typing my code here.
SDL fills in the color components found in ‘pixel’ according to the
format that is given. The memory for the color component variables
must be allocated already (I’ve done this statically here, instead of
using ‘malloc’ or ‘new’) or else bad things will happen (like
crashes).
Also, I should note that your SDL_GetPixel call has swapped ‘x’ and ‘y’.
Hope that helps,
Jonny D
I guess I’m not allocating memory for the ‘r’, ‘g’, ‘b’ and ‘a’ variables, then.
And the swapped ‘x’ and ‘y’ are just a typo when typing my code here.
That did it. Thanks to everybody that helped!
If this code is called many times, and depending on what it does, then
maybe allocating r, g, b, and a is overkill. And it can cause heap
fragmentation.
My Pascal is a little bit rusty nowadays, but I think the code below
should work:
var theimage : PSDL_Surface;
pixel : Uint32;
fmt : PSDL_PixelFormat;
r, g, b, a : Uint8; { Uint8, not PUint8 which are pointers }
[…]
theimage := IMG_Load(‘test.png’);
fmt := theimage^.format;
pixel := SDL_GetPixel(theimage, x, y);
SDL_GetRGBA(pixel, fmt, @r, @g, @b, @a); { Pass the address of r, g,
b, and a }
SDL fills in the color components found in ‘pixel’ according to the
format that is given. The memory for the color component variables
must be allocated already (I’ve done this statically here, instead of
using ‘malloc’ or ‘new’) or else bad things will happen (like
crashes).
Also, I should note that your SDL_GetPixel call has swapped ‘x’ and ‘y’.
Hope that helps,
Jonny D
I guess I’m not allocating memory for the ‘r’, ‘g’, ‘b’ and ‘a’ variables, then.
And the swapped ‘x’ and ‘y’ are just a typo when typing my code here.
That did it. Thanks to everybody that helped!
If this code is called many times, and depending on what it does, then
maybe allocating r, g, b, and a is overkill. And it can cause heap fragmentation.
My Pascal is a little bit rusty nowadays, but I think the code below should work:
var theimage : PSDL_Surface;
pixel : Uint32;
fmt : PSDL_PixelFormat;
r, g, b, a : Uint8; { Uint8, not PUint8 which are pointers }
[…]
theimage := IMG_Load(‘test.png’);
fmt := theimage^.format;
pixel := SDL_GetPixel(theimage, x, y);
SDL_GetRGBA(pixel, fmt, @r, @g, @b, @a); { Pass the address of r, g, b, and a }
Cheers,
Andre
Yes, that will work. But if rewriting the declaration as
procedure SDL_GetRGBA( pixel : UInt32; fmt : PSDL_PixelFormat;
var r, g, b, a : byte );
doesn’t work, you’ve got a nasty codegen bug in the FPC compiler. That works just
fine under Delphi and should work for any Pascal dialect.>----- Original Message ----
From: Andre de Leiradella
Subject: Re: [SDL] SDL_GetRGBA exits program.