// The mouse cursor, drawn into frames read back from Desktop Duplication, which leaves it out of the // image and hands over its shape and position separately. Three kinds of shape: // // COLOR BGRA, blended by its alpha. // MASKED_COLOR BGRA, where alpha 0 replaces the pixel under it and 0xFF XORs it. // MONOCHROME 1 bit a pixel, twice as tall: an AND mask over an XOR mask. AND 1 XOR 1 inverts the // pixel under it, which is how the text I-beam shows on any background. Cursor_Shape :: struct { type: DXGI_OUTDUPL_POINTER_SHAPE_TYPE; width, height, pitch: s32; // height as handed over: twice the cursor's for MONOCHROME data: [..] u8; } // Draws shape with its top left at x, y (in the frame's pixels; it may reach past the edges) into // BGRA pixels, rows packed. cursor_draw :: (pixels: *u8, width: s32, height: s32, shape: *Cursor_Shape, x: s32, y: s32) { if !shape.data.count return; rows := ifx shape.type == .MONOCHROME then shape.height / 2 else shape.height; for row: 0..rows - 1 { dy := y + row; if dy < 0 || dy >= height continue; for column: 0..shape.width - 1 { dx := x + column; if dx < 0 || dx >= width continue; d := pixels + (cast(s64) dy * width + dx) * 4; if shape.type == { case .COLOR; s := shape.data.data + row * shape.pitch + column * 4; a := cast(u32) s[3]; if a == 0 continue; for c: 0..2 d[c] = cast(u8) ((cast(u32) s[c] * a + cast(u32) d[c] * (255 - a) + 127) / 255); case .MASKED_COLOR; s := shape.data.data + row * shape.pitch + column * 4; if s[3] == 0 for c: 0..2 d[c] = s[c]; else for c: 0..2 d[c] ^= s[c]; case .MONOCHROME; bit := cast(u8) (0x80 >> (column % 8)); and_mask := shape.data[row * shape.pitch + column / 8] & bit; xor_mask := shape.data[(row + rows) * shape.pitch + column / 8] & bit; if and_mask && !xor_mask continue; // transparent if and_mask { for c: 0..2 d[c] = 255 - d[c]; continue; } // inverted value: u8 = ifx xor_mask then cast(u8) 255 else 0; // white or black for c: 0..2 d[c] = value; } } } }