Thanks guys! I have one public question right away. If I want to draw single pixels on a 1bit depth area, what is the recommended way? MoveTo+Line(0,0) combo seems to be pretty slow.
Yeah it’s slow if you’re doing it a lot.
If you’re on a compact Mac (no graphics card), write directly to screenBits: (qd.screenBits in some later compilers)
I did this from memory so check my math. I will test it tonight to ensure I got it right! It also assumes a screen that’s 512 pixels wide:
[SIZE=1.4rem]#define SET_PIXEL(x, y)[/SIZE]
[SIZE=1.4rem] *(((short *) [/SIZE][SIZE=1.4rem]screenBits.baseAddr) [/SIZE][SIZE=1.4rem]+ (
<< 5) + ((x) >> 4)) |= (0x8000 >> ((x) & 0x000F))[/SIZE]
Translation:
- grab start of screenBits bitmap, cast as a short* so we can do pointer arithmetic word-by-word
- advance by y<<5 == y*32, where 32 = #words in a 512-pixel row. This gets us to the start of the right row
- then advance by x/16 to grab the right word in the row
- now we need to apply a bitwise OR to that word to set one of the bits...
- the thing we will OR it with is 0x8000 = 0b100000000000000, right-shifted by the low 4 bits of x (which tell us which bit to use in the word of interest)
For multiple invocations, faster if you put screenBits.baseAddr in a register first of course. Or do this in an asm{...} and use a BSET instruction rather than the |=.
If you’re on a Mac that might have multiple monitors/varying bit depths, it’s just slightly tricker, you have to get the current gDevice first.
If you’re on a color screen, uncertain of the PixMap width and use rowBytes, be sure to remember the high 3 bits contain flags and should be stripped off. This one gets me every time.