aboutsummaryrefslogtreecommitdiff
path: root/include/ui/rect.h
blob: 68f05d78a8e6b0cadc80c229dc2a1fce889e1adf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 * SPDX-License-Identifier: GPL-2.0-or-later
 */
#ifndef QEMU_RECT_H
#define QEMU_RECT_H

#include <stdint.h>
#include <stdbool.h>

typedef struct QemuRect {
    int16_t x;
    int16_t y;
    uint16_t width;
    uint16_t height;
} QemuRect;

static inline void qemu_rect_init(QemuRect *rect,
                                  int16_t x, int16_t y,
                                  uint16_t width, uint16_t height)
{
    rect->x = x;
    rect->y = y;
    rect->width = width;
    rect->height = height;
}

static inline void qemu_rect_translate(QemuRect *rect,
                                       int16_t dx, int16_t dy)
{
    rect->x += dx;
    rect->y += dy;
}

static inline bool qemu_rect_intersect(const QemuRect *a, const QemuRect *b,
                                       QemuRect *res)
{
    int16_t x1, x2, y1, y2;

    x1 = MAX(a->x, b->x);
    y1 = MAX(a->y, b->y);
    x2 = MIN(a->x + a->width, b->x + b->width);
    y2 = MIN(a->y + a->height, b->y + b->height);

    if (x1 >= x2 || y1 >= y2) {
        if (res) {
            qemu_rect_init(res, 0, 0, 0, 0);
        }

        return false;
    }

    if (res) {
        qemu_rect_init(res, x1, y1, x2 - x1, y2 - y1);
    }

    return true;
}

#endif