XBasic3000
XBasic3000

Reputation: 3486

C header to delphi

I have a piece of C code. I need help to translate it to Delphi code.

1)

/*
 * Color is packed into 16-bit word as follows:
 *
 *  15      8 7      0 
 *   XXggbbbb XXrrrrgg
 *
 * Note that green bits 12 and 13 are the lower bits of green component
 * and bits 0 and 1 are the higher ones.
 * 
 */
#define CLR_RED(spec)      (((spec) >> 2) & 0x0F)
#define CLR_GREEN(spec)    ((((spec) & 0x03) << 2) | ((spec & 0x3000) >> 12))
#define CLR_BLUE(spec)     (((spec) >> 8) & 0x0F)

2)

#define CDG_GET_SCROLL_COMMAND(scroll)    (((scroll) & 0x30) >> 4)
#define CDG_GET_SCROLL_HOFFSET(scroll)     ((scroll) & 0x07)
#define CDG_GET_SCROLL_VOFFSET(scroll)     ((scroll) & 0x0F)

Upvotes: 2

Views: 222

Answers (1)

CodesInChaos
CodesInChaos

Reputation: 108880

These are parameterized macros. Since Delphi doesn't support these, you'll need to use functions instead, which is cleaner anyways.

  • >> is a right-shift, shr in Delphi
  • << is a left-shift, shl in Delphi
  • & is "bitwise and", and in Delphi
    Delphi uses bitwise operators when working on integers, and logical operators when working on booleans, so there is only one operator and to replace both && and &.
  • | is "bitwise or", or in Delphi
  • 0x is the prefix for hex literals, $ in Delphi

So #define CLR_GREEN(spec) ((((spec) & 0x03) << 2) | ((spec & 0x3000) >> 12)) becomes something like:

function CLR_GREEN(spec: word):byte;
begin
  result := byte(((spec and $03) shl 2) or ((spec and $3000) shr 12));
end;

(I don't have delphi at hand, so there might be minor bugs)

Convert the other macros in a similar manner.

Upvotes: 11

Related Questions