SDL_GetRGBA exits program

This is probably just a stupid mistake, but I can’t get SDL_GetRGBA to work:

var theimage : PSDL_Surface;
pixel : Uint32;
fmt : PSDL_PixelFormat;
r, g, b, a : PUint8;
[…]
theimage := IMG_Load(‘test.png’);
fmt := theimage^.format;
pixel := SDL_GetPixel(theimage, y, x);
SDL_GetRGBA(pixel, fmt, r, g, b, a);

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?

Hi.

I don’t know much about Pascal, but assuming that r,g,b and a are
declared as Uin8 pointers, where do you set what they are pointing at?

Regards,
Brian.On Tue, Sep 1, 2009 at 12:05 PM, Christian Knudsen wrote:

This is probably just a stupid mistake, but I can’t get SDL_GetRGBA to work:

var theimage : PSDL_Surface;
? ?pixel : Uint32;
? ?fmt : PSDL_PixelFormat;
? ?r, g, b, a : PUint8;
[…]
? theimage := IMG_Load(‘test.png’);
? fmt := theimage^.format;
? pixel := SDL_GetPixel(theimage, y, x);
? SDL_GetRGBA(pixel, fmt, r, g, b, a);

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?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Brian <brian.ripoff gmail.com> writes:

Hi.

I don’t know much about Pascal, but assuming that r,g,b and a are
declared as Uin8 pointers, where do you set what they are pointing at?

Regards,
Brian.

I assumed that’s what the SDL_GetRGBA procedure does:

http://jedi-sdl.pascalgamedevelopment.com/html_docs/sdlgetrgba.html

Or am I just waaay off base?

Brian <brian.ripoff gmail.com> writes:

Hi.

I don’t know much about Pascal, but assuming that r,g,b and a are
declared as Uin8 pointers, where do you set what they are pointing at?

Regards,
Brian.

I assumed that’s what the SDL_GetRGBA procedure does:

http://jedi-sdl.pascalgamedevelopment.com/html_docs/sdlgetrgba.html

Or am I just waaay off base?

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.

Mason Wheeler <masonwheeler yahoo.com> writes:

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.

Mason Wheeler <masonwheeler yahoo.com> writes:

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.

I’m on WinXP Home Edition SP2 and using SDL 1.2.13. I’ve got a Linux machine
as well. I’ll see if I get the same result there.

Christian Knudsen <webmaster asciisector.net> writes:

I’m on WinXP Home Edition SP2 and using SDL 1.2.13. I’ve got a Linux machine
as well. I’ll see if I get the same result there.

Same exact thing in Ubuntu 8.04. The program just exits when reaching the
SDL_GetRGBA call without any error messages.

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:

Christian Knudsen <webmaster asciisector.net> writes:

I’m on WinXP Home Edition SP2 and using SDL 1.2.13. I’ve got a Linux machine
as well. I’ll see if I get the same result there.

Same exact thing in Ubuntu 8.04. The program just exits when reaching the
SDL_GetRGBA call without any error messages.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Jonathan Dearborn <grimfang4 gmail.com> writes:

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. :slight_smile:

That did it. Thanks to everybody that helped! :slight_smile:

Jonathan Dearborn <grimfang4 gmail.com> writes:

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. :slight_smile:

That did it. Thanks to everybody that helped! :slight_smile:

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

Jonathan Dearborn <grimfang4 gmail.com> writes:

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. :slight_smile:

That did it. Thanks to everybody that helped! :slight_smile:

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.