Idiomatic C preprocessor macro metaprogramming.

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andrew Davie

OK, so a bit of background:

This is a character-graphics engine. Tiles, if you like that term. So, various characters have to be defined - their shapes must be coded with an array of bytes.  Because characters must be drawn in rigid positions (in this case, the characters are 5 pixels wide and 27 scanlines deep) that means that we can only get really blocky movement. Characters themselves can only move in minimum units of 5 pixels horizontally, or 27 scanlines vertically. That's 1/8th of the screen horizontally, which is awful.

For Sokoban, I want the boxes to move smoothly when you push them. So, I cheat. I define multiple characters. Consider a box (5 pixels) moving to the right: First I show the whole box, but next I want to sift the box 1 pixel to the right, then two pixels to the right... until the box is in the next character position.  So I define 10 characters to do this. Urk!

That is, given positions A (original position) and B (one character square to the right), then the sequence is...

A (original box)  B (blank)
A (shifted 1 right) B (rightmost pixels of original box)
B (shifted 2 right) B (rightmost 2 pixels..)
... etc


That might not be easy to understand.  Another way of saying this is that the box definition shifts conceptually over two character positions. And each of those 5 possible shifts (each character being 5 pixels wide) means that we have to have 2 characters (left and right) to hold that shifted position. So, essentially, 10 characters in our character set are required to allow us to continue to draw the box using only character graphics.  Ugly, but it works.

But that means, when I am designing/drawing the box to make it look good (I do this in code by modifying on/off pixels manually in the source code), I actually have to do this for 5 boxes in shifted positions. That is, 5 times. And it gets worse - vertically we have 9 'trixels' (conceptual ICC pixels composed of 3 scanlines each). So if I modify a pixel in the box shape, I have to do that 9 times in the code. And it gets messy, confusing, and unreliable. And it takes ages.

So I thought, hey, let's have some fun and write some macros to generate the shifted character pair definitions (in code) automatically. Can it be done?  Well, yes... but the macros are insane!


#define LINE0 0x00, 0x00, 0x00
#define BLANK3 LINE0
#define BLANK6 LINE0, LINE0
#define BLANK9 LINE0, LINE0, LINE0
#define BLANK12 LINE0, LINE0, LINE0, LINE0
#define BLANK15 LINE0, LINE0, LINE0, LINE0, LINE0
#define BLANK18 LINE0, LINE0, LINE0, LINE0, LINE0, LINE0
#define BLANK21 LINE0, LINE0, LINE0, LINE0, LINE0, LINE0, LINE0
#define BLANK24 LINE0, LINE0, LINE0, LINE0, LINE0, LINE0, LINE0, LINE0
#define BLANK27 LINE0, LINE0, LINE0, LINE0, LINE0, LINE0, LINE0, LINE0, LINE0

// The only required shape definition
// All 9 shifted versions of the shape are created by the macros!

#define BOX_BYTES                                                                                  \
    0b11111, \
    0b11111, \
    0b11111, \
    0b10001, \
    0b10001, \
    0b10001, \
    0b10001, \
    0b10001, \
    0b10001, \
    0b10001, \
    0b10001, \
    0b10001, \
    0b10001, \
    0b10001, \
    0b10001, \
    0b10001, \
    0b10001, \
    0b10001, \
    0b10001, \
    0b10001, \
    0b10001, \
    0b10001, \
    0b10001, \
    0b10001, \
    0b11111, \
    0b11111, \
    0b11111

#define EXPAND(x) x

// Shift 0 (no blank lines on top)
#define CHAR_DEFINE_0(name, \
 b0,b1,b2, b3,b4,b5, b6,b7,b8, \
 b9,b10,b11, b12,b13,b14, b15,b16,b17, \
 b18,b19,b20, b21,b22,b23, b24,b25,b26) \
static const unsigned char name##_top_0[27] = { \
     b0,b1,b2, b3,b4,b5, b6,b7,b8, \
 b9,b10,b11, b12,b13,b14, b15,b16,b17, \
 b18,b19,b20, b21,b22,b23, b24,b25,b26 \
 }; \
static const unsigned char name##_bottom_0[27] = { BLANK27 };

// Shift 1 line (3 bytes blank on top)
#define CHAR_DEFINE_1(name, \
 b0,b1,b2, b3,b4,b5, b6,b7,b8, \
 b9,b10,b11, b12,b13,b14, b15,b16,b17, \
 b18,b19,b20, b21,b22,b23, b24,b25,b26) \
static const unsigned char name##_top_1[27] = { \
  BLANK3, \
  b0,b1,b2, b3,b4,b5, b6,b7,b8, \
  b9,b10,b11, b12,b13,b14, b15,b16,b17, \
  b18,b19,b20, b21,b22,b23 \
}; \
static const unsigned char name##_bottom_1[27] = { \
  b24,b25,b26, \
  BLANK24 \
};

// Shift 2 lines (6 bytes blank on top)
#define CHAR_DEFINE_2(name, \
 b0,b1,b2, b3,b4,b5, b6,b7,b8, \
 b9,b10,b11, b12,b13,b14, b15,b16,b17, \
 b18,b19,b20, b21,b22,b23, b24,b25,b26) \
static const unsigned char name##_top_2[27] = { \
  BLANK6, \
  b0,b1,b2, b3,b4,b5, b6,b7,b8, \
  b9,b10,b11, b12,b13,b14, \
  b15,b16,b17, b18,b19, b20 \
}; \
static const unsigned char name##_bottom_2[27] = { \
  b21,b22,b23, b24,b25,b26, \
  BLANK21 \
};

// Shift 3 lines (9 bytes blank on top)
#define CHAR_DEFINE_3(name, \
 b0,b1,b2, b3,b4,b5, b6,b7,b8, \
 b9,b10,b11, b12,b13,b14, b15,b16,b17, \
 b18,b19,b20, b21,b22,b23, b24,b25,b26) \
static const unsigned char name##_top_3[27] = { \
  BLANK9, \
  b0,b1,b2, b3,b4,b5, b6,b7,b8, \
  b9,b10,b11, b12,b13,b14, b15,b16,b17, \
}; \
static const unsigned char name##_bottom_3[27] = { \
  b18,b19,b20, b21,b22,b23, b24,b25,b26, \
  BLANK18 \
};

// Shift 4 lines (12 bytes blank on top)
#define CHAR_DEFINE_4(name, \
 b0,b1,b2, b3,b4,b5, b6,b7,b8, \
 b9,b10,b11, b12,b13,b14, b15,b16,b17, \
 b18,b19,b20, b21,b22,b23, b24,b25,b26) \
static const unsigned char name##_top_4[27] = { \
  BLANK12, \
  b0,b1,b2, b3,b4,b5, \
  b6,b7,b8, b9,b10,b11, b12,b13,b14, \
}; \
static const unsigned char name##_bottom_4[27] = { \
  b15,b16,b17, b18,b19,b20, b21,b22,b23, \
  b24,b25,b26, \
  BLANK15 \
};

// Shift 5 lines (15 bytes blank on top)
#define CHAR_DEFINE_5(name, \
 b0,b1,b2, b3,b4,b5, b6,b7,b8, \
 b9,b10,b11, b12,b13,b14, b15,b16,b17, \
 b18,b19,b20, b21,b22,b23, b24,b25,b26) \
static const unsigned char name##_top_5[27] = { \
  BLANK15, \
  b0,b1,b2, \
  b3,b4,b5, b6,b7,b8, b9,b10,b11, \
}; \
static const unsigned char name##_bottom_5[27] = { \
  b12,b13,b14, b15,b16,b17, b18,b19,b20, \
  b21,b22,b23, b24,b25,b26, \
  BLANK12 \
};

// Shift 6 lines (18 bytes blank on top)
#define CHAR_DEFINE_6(name, \
 b0,b1,b2, b3,b4,b5, b6,b7,b8, \
 b9,b10,b11, b12,b13,b14, b15,b16,b17, \
 b18,b19,b20, b21,b22,b23, b24,b25,b26) \
static const unsigned char name##_top_6[27] = { \
  BLANK18, \
  b0,b1,b2, \
  b3,b4,b5, b6,b7,b8 \
}; \
static const unsigned char name##_bottom_6[27] = { \
  b9,b10,b11, \
  b12,b13,b14, b15,b16,b17, b18,b19,b20, \
  b21,b22,b23, b24,b25,b26, \
  BLANK9 \
};

// Shift 7 lines (21 bytes blank on top)
#define CHAR_DEFINE_7(name, \
 b0,b1,b2, b3,b4,b5, b6,b7,b8, \
 b9,b10,b11, b12,b13,b14, b15,b16,b17, \
 b18,b19,b20, b21,b22,b23, b24,b25,b26) \
static const unsigned char name##_top_7[27] = { \
  BLANK21, \
  b0,b1,b2, b3,b4,b5 \
}; \
static const unsigned char name##_bottom_7[27] = { \
  b6,b7,b8, b9,b10,b11, \
  b12,b13,b14, b15,b16,b17, b18,b19,b20, \
  b21,b22,b23, b24,b25,b26, \
  BLANK6 \
};

// Shift 8 lines (24 bytes blank on top)
#define CHAR_DEFINE_8(name, \
 b0,b1,b2, b3,b4,b5, b6,b7,b8, \
 b9,b10,b11, b12,b13,b14, b15,b16,b17, \
 b18,b19,b20, b21,b22,b23, b24,b25,b26) \
static const unsigned char name##_top_8[27] = { \
  BLANK24, \
  b0,b1,b2 \
}; \
static const unsigned char name##_bottom_8[27] = { \
  b3,b4,b5, b6,b7,b8, b9,b10,b11, \
  b12,b13,b14, b15,b16,b17, b18,b19,b20, \
  b21,b22,b23, b24,b25,b26, \
  BLANK3 \
};

#define CONCAT(a, b) a##b
#define EXPAND(x) x
#define SELECT_CHAR_DEFINE(n) CONCAT(CHAR_DEFINE_, n)
#define SELECT_CHAR_CALL(n, name, bytes) EXPAND(SELECT_CHAR_DEFINE(n)(name, bytes))

SELECT_CHAR_CALL(0, char_box_UtoD, BOX_BYTES)
SELECT_CHAR_CALL(1, char_box_UtoD, BOX_BYTES)
SELECT_CHAR_CALL(2, char_box_UtoD, BOX_BYTES)
SELECT_CHAR_CALL(3, char_box_UtoD, BOX_BYTES)
SELECT_CHAR_CALL(4, char_box_UtoD, BOX_BYTES)
SELECT_CHAR_CALL(5, char_box_UtoD, BOX_BYTES)
SELECT_CHAR_CALL(6, char_box_UtoD, BOX_BYTES)
SELECT_CHAR_CALL(7, char_box_UtoD, BOX_BYTES)
SELECT_CHAR_CALL(8, char_box_UtoD, BOX_BYTES)


Essentially I just modify the 27 bytes (= 27 scanlines) in BOX_BYTES and all 9 vertically shifted character pairs are created for me.  The BOX_BYTES, for those interested, consists of 9 groups of 3 scanlines each, where those 3 scanlines represent respectively conceptual 'red', 'green' and 'blue' pixels. The three are blended to give an ICC 'trixel'.

So now, I may have saved myself time. Or maybe not. I have a very basic 5-pixel wide x 9 trixel deep box defined here - with all trixels around the edge the same colour (#7) composed of binary 111, and the center blank (colour #0, binary 000).