PDA

View Full Version : Converting C Arrays/Pointers to Pascal



mcory1
13-01-2006, 03:36 AM
Hi everyone,
I'm working on a series of tutorials in C/C++ that I'm wanting to convert to Pascal. Unfortunately, my Pascal was never that great, and I never had a need to do something like this before. I'm working with SDL (and JEDI-SDL for Pascal), and the SDL_GetKeyState function returns a PUint8/Uint8*.

In C, this works out fine--you get the pointer and access the individual keys as an array using the SDL key codes as the indexer:



Uint8* keys;

keys = SDL_GetKeyState(NULL);
if (keys[SDLK_UP]){
...


Of course, a straight Pascal translation won't cut it; it says that an array type is needed when you try keys[SDLK_UP] (or whatever key code). I know this is because arrays and pointers are pretty much the same thing, and it isn't the same story in Pascal.

I'm really wanting to use (at least roughly) this same train of thought for the Pascal versions of the tutorials. Anyone have any suggestions?

As kind of a PS, is JEDI still even active? The last update was last May...

LP
13-01-2006, 03:47 AM
Please try the following code:


var
Keys: PByteArray;
begin
Keys = SDL_GetKeyState(nil);
if (Boolean(Keys[SDLK_UP])) then
// . . .

mcory1
13-01-2006, 04:06 AM
Thanks, but that still choked--"Incompatible types: Byte and TByteArray". I tried it with a direct typecast (PByteArray(SDL_Get...)); it compiled but it didn't do anything once I got it running. I'll play around with that though, and if you (or anyone else for that matter) has any additional ideas I'd appreciate it. Thanks again.

Edit: Tried typecast after original reply; no need for a new post on that...

LP
13-01-2006, 04:27 AM
Alternatively, you can use more "dirty" approach:

var
Key: PByte;
begin
Key:= PByte(SDL_GetKeyState(nil));
Inc(Integer(Key), SDLK_UP);
if (Boolean(Key^)) then // do something
end;

mcory1
13-01-2006, 04:29 AM
Nevermind, I'm just about an idiot--I'd forgotten to have my code do anything when it evaluated to true...:oops:

I'd been too focused on getting it to compile, and forgot about what it was trying to do. For future reference, the following code is pointless:


if (true) then
begin
end;


Thanks again Lifepower, big help.

JSoftware
13-01-2006, 06:12 AM
Hehe good stuff :P I've tried that a few times too