Results 1 to 6 of 6

Thread: Converting C Arrays/Pointers to Pascal

  1. #1

    Converting C Arrays/Pointers to Pascal

    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:

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

  2. #2

    Converting C Arrays/Pointers to Pascal

    Please try the following code:
    Code:
    var
     Keys: PByteArray;
    begin
     Keys = SDL_GetKeyState(nil); 
     if (Boolean(Keys[SDLK_UP])) then
      // . . .

  3. #3

    Converting C Arrays/Pointers to Pascal

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

  4. #4

    Converting C Arrays/Pointers to Pascal

    Alternatively, you can use more "dirty" approach:
    Code:
    var
     Key: PByte;
    begin
     Key:= PByte(SDL_GetKeyState(nil));
     Inc(Integer(Key), SDLK_UP);
     if (Boolean(Key^)) then // do something
    end;

  5. #5

    Converting C Arrays/Pointers to Pascal

    Nevermind, I'm just about an idiot--I'd forgotten to have my code do anything when it evaluated to true...ops:

    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:

    [pascal]
    if (true) then
    begin
    end;
    [/pascal]

    Thanks again Lifepower, big help.

  6. #6

    Converting C Arrays/Pointers to Pascal

    Hehe good stuff I've tried that a few times too
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •