Is there a way to check if a certain screen area already has an Image being drawn on it?

I want to draw an image n times in random positions without overlapping. I could store positions of Image already drawn and compare with those but was wondering if there was a simpler/direct way for it.

In the C API, you can get the pixels of the last or current frame and check if a pixel is black to know if an image has already been drawn.

It should look something like that:

int isSomethingDrawnInRect(LCDRect rect) {
    const int rowStride = 52;
    const int bitsInAByte = 8;
    uint8_t *frame = playdate->graphics->getFrame();
    for (int y = rect.top; y < rect.bottom; y++) {
        for (int x = rect.left; x < rect.right; x++) {
            const int byteIndex = x / bitsInAByte + y * rowStride;
            const int bitIndex = (1 << (bitsInAByte - 1)) >> (x % bitsInAByte);
            const int isBlack = (frame[byteIndex] & bitIndex) != 0;
            if (isBlack) {
                return 1;
            }
        }
    }
    return 0;
}
1 Like

I do this with the golf greens/holes in Ball und Panzer Golf.

You keep track of previous positions and check new positions against all previous positions. Just a few standard logic constructs are required.

It's the type of problem that ai code tools are very good at. After writing my version i asked ChatGPT to see how it did.

Here's a ChatGPT log. I get a working answer and then ask it to take into account minimum distance which it does easily.

https://chat.openai.com/share/923c903a-d244-4fe4-b667-09626314fed5

Thanks for this. I should've mentioned I'm using Lua for my game. But I will keep this in mind!

Ah, amazing. had something similar in mind, might've to go with it. I just wished there was a direct way using Lua for this.

interested to hear more about what you mean here. what/why?

Something similar to what @Daeke suggested using C.
If I could directly check if a certain screen position is being drawn to this frame/last frame using Lua.

You can do that with this function and lockfocus/pushcontext.

https://sdk.play.date/inside-playdate/#m-graphics.image.sample

playdate.graphics.image:sample(x, y)

Returns playdate.graphics.kColorWhite if the image is white at (x, y), playdate.graphics.kColorBlack if it’s black, or playdate.graphics.kColorClear if it’s transparent.

Let me know if it does get you what you want.

@matt That's useful to know :slight_smile: . Is there a way to get the current or last frame bitmap in Lua ?

Sure!

I recommend reading the docs :slight_smile:

I think they could be improved by linking equivalent functions between the Lua and C docs.

1 Like

Yes, I think that's it. Thank you!