aboutsummaryrefslogtreecommitdiff
path: root/src/common.c
blob: 249a0d3446d7b84613b0478f13d9248b973e0e39 (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
#include "../include/common.h"
#include <mgl/window/window.h>

static inline float max_float(float a, float b) {
    return a >= b ? a : b;
}

static inline float min_float(float a, float b) {
    return a <= b ? a : b;
}

bool mgui_rectangle_contains(mgl_vec2f rectangle_pos, mgl_vec2f rectangle_size, mgl_vec2f point) {
    return point.x >= rectangle_pos.x && point.x <= rectangle_pos.x + rectangle_size.x
        && point.y >= rectangle_pos.y && point.y <= rectangle_pos.y + rectangle_size.y;
}

bool mgui_rectangle_intersects_with_scissor(mgl_vec2i rectangle_pos, mgl_vec2i rectangle_size, mgl_window *window) {
    mgl_scissor scissor;
    mgl_window_get_scissor(window, &scissor);

    const int left_x    = max_float(rectangle_pos.x, scissor.position.x);
    const int right_x   = min_float(rectangle_pos.x + rectangle_size.x, scissor.position.x + scissor.size.x);
    const int top_y     = max_float(rectangle_pos.y, scissor.position.y);
    const int bottom_y  = min_float(rectangle_pos.y + rectangle_size.y, scissor.position.y + scissor.size.y);

    return left_x <= right_x && top_y <= bottom_y;
}