Project

General

Profile

Statistics
| Revision:

root / proj / libs / graph / src / rectangle.c @ 309

History | View | Annotate | Download (3.01 KB)

1 178 up20180642
#include <lcom/lcf.h>
2
3
#include "rectangle.h"
4
5 179 up20180642
#include "graph.h"
6
#include "utils.h"
7
8 178 up20180642
struct rectangle{
9 179 up20180642
    int16_t  x, y;
10 178 up20180642
    uint16_t w, h;
11
    uint32_t fill_color;
12 286 up20180642
    uint8_t  fill_alpha;
13 178 up20180642
    uint32_t outline_color;
14 179 up20180642
    int16_t  outline_width;
15 178 up20180642
};
16
17
rectangle_t* (rectangle_ctor)(int16_t x, int16_t y, uint16_t w, uint16_t h){
18
    rectangle_t *ret = (rectangle_t*)malloc(sizeof(rectangle_t));
19 179 up20180642
    if(ret == NULL) return NULL;
20
    ret->x = x;
21
    ret->y = y;
22
    ret->w = w;
23
    ret->h = h;
24
    ret->fill_color    = 0xFFFFFF;
25 286 up20180642
    ret->fill_alpha    = 0xFF;
26 179 up20180642
    ret->outline_color = 0x000000;
27
    ret->outline_width = 0;
28 178 up20180642
    return ret;
29
}
30 179 up20180642
void (rectangle_dtor)(rectangle_t *p){
31 178 up20180642
    if(p == NULL) return;
32
    free(p);
33
}
34 179 up20180642
35
void (rectangle_set_pos)          (rectangle_t *p,  int16_t x,  int16_t y){ p->x = x; p->y = y; }
36
void (rectangle_set_size)         (rectangle_t *p, uint16_t w, uint16_t h){ p->w = w; p->h = h; }
37
void (rectangle_set_fill_color)   (rectangle_t *p, uint32_t color        ){ p->fill_color = color; }
38 286 up20180642
void (rectangle_set_fill_trans)   (rectangle_t *p, uint8_t  alpha        ){ p->fill_alpha = alpha; }
39 179 up20180642
void (rectangle_set_outline_color)(rectangle_t *p, uint32_t color        ){ p->outline_color = color; }
40
void (rectangle_set_outline_width)(rectangle_t *p,  int16_t width        ){ p->outline_width = width; }
41
42
int16_t  (rectangle_get_x)(const rectangle_t *p){ return p->x; }
43
int16_t  (rectangle_get_y)(const rectangle_t *p){ return p->y; }
44
uint16_t (rectangle_get_w)(const rectangle_t *p){ return p->w; }
45
uint16_t (rectangle_get_h)(const rectangle_t *p){ return p->h; }
46
47 251 up20180655
int (rectangle_collide_point)(const rectangle_t *p, int x, int y) {
48
    int16_t x0 = p->x, y0 = p->y;
49
    return (x >= x0 && x <= x0 + p->w) && (y >= y0 && y <= y0 + p->h);
50
}
51
52 179 up20180642
static void (rectangle_draw_hline)(int16_t x, int16_t y, int16_t l, uint32_t color){
53
    if(l < 0){ rectangle_draw_hline(x+l, y, -l, color); return; }
54
    for(int16_t x_ = max(0,x); x_ < min(x+l,graph_get_XRes()); ++x_){
55
        graph_set_pixel(x_, y, color);
56
    }
57
}
58
static void (rectangle_draw_vline)(int16_t x, int16_t y, int16_t l, uint32_t color){
59
    if(l < 0){ rectangle_draw_vline(x, y+l, -l, color); return; }
60
    for(int16_t y_ = max(0,y); y_ < min(y+l,graph_get_YRes()); ++y_){
61
        graph_set_pixel(x, y_, color);
62
    }
63
}
64
65
void (rectangle_draw)(const rectangle_t *p){
66 286 up20180642
    /// fill
67
    if(p->fill_alpha > ALPHA_THRESHOLD)
68
        for(int16_t y = max(p->y,0); y < min(p->y+p->h, graph_get_YRes()); ++y)
69
            rectangle_draw_hline(p->x, y, p->w, p->fill_color);
70
    /// border
71 179 up20180642
    int16_t step = (p->outline_width > 0 ? 1 : -1);
72
    int16_t l = p->x, r = p->x+p->w, t = p->y, b = p->y+p->h;
73
    if(step > 0){
74
        --l; ++r; --t; ++b;
75
    }
76
    for(int16_t i = 0; i != p->outline_width; i += step, l -= step, r += step, t -= step, b += step){
77
        rectangle_draw_hline(l  , t  , r-l, p->outline_color);
78
        rectangle_draw_hline(l  , b-1, r-l, p->outline_color);
79
        rectangle_draw_vline(l  , t  , b-t, p->outline_color);
80
        rectangle_draw_vline(r-1, t  , b-t, p->outline_color);
81
    }
82
}