root / proj / src / rectangle.c @ 246
History | View | Annotate | Download (2.62 KB)
1 |
#include <lcom/lcf.h> |
---|---|
2 |
|
3 |
#include "rectangle.h" |
4 |
|
5 |
#include "graph.h" |
6 |
#include "utils.h" |
7 |
|
8 |
struct rectangle{
|
9 |
int16_t x, y; |
10 |
uint16_t w, h; |
11 |
uint32_t fill_color; |
12 |
uint32_t outline_color; |
13 |
int16_t outline_width; |
14 |
}; |
15 |
|
16 |
rectangle_t* (rectangle_ctor)(int16_t x, int16_t y, uint16_t w, uint16_t h){ |
17 |
rectangle_t *ret = (rectangle_t*)malloc(sizeof(rectangle_t));
|
18 |
if(ret == NULL) return NULL; |
19 |
ret->x = x; |
20 |
ret->y = y; |
21 |
ret->w = w; |
22 |
ret->h = h; |
23 |
ret->fill_color = 0xFFFFFF;
|
24 |
ret->outline_color = 0x000000;
|
25 |
ret->outline_width = 0;
|
26 |
return ret;
|
27 |
} |
28 |
void (rectangle_dtor)(rectangle_t *p){
|
29 |
if(p == NULL) return; |
30 |
free(p); |
31 |
} |
32 |
|
33 |
void (rectangle_set_pos) (rectangle_t *p, int16_t x, int16_t y){ p->x = x; p->y = y; }
|
34 |
void (rectangle_set_size) (rectangle_t *p, uint16_t w, uint16_t h){ p->w = w; p->h = h; }
|
35 |
void (rectangle_set_fill_color) (rectangle_t *p, uint32_t color ){ p->fill_color = color; }
|
36 |
void (rectangle_set_outline_color)(rectangle_t *p, uint32_t color ){ p->outline_color = color; }
|
37 |
void (rectangle_set_outline_width)(rectangle_t *p, int16_t width ){ p->outline_width = width; }
|
38 |
|
39 |
int16_t (rectangle_get_x)(const rectangle_t *p){ return p->x; } |
40 |
int16_t (rectangle_get_y)(const rectangle_t *p){ return p->y; } |
41 |
uint16_t (rectangle_get_w)(const rectangle_t *p){ return p->w; } |
42 |
uint16_t (rectangle_get_h)(const rectangle_t *p){ return p->h; } |
43 |
|
44 |
static void (rectangle_draw_hline)(int16_t x, int16_t y, int16_t l, uint32_t color){ |
45 |
if(l < 0){ rectangle_draw_hline(x+l, y, -l, color); return; } |
46 |
for(int16_t x_ = max(0,x); x_ < min(x+l,graph_get_XRes()); ++x_){ |
47 |
graph_set_pixel(x_, y, color); |
48 |
} |
49 |
} |
50 |
|
51 |
static void (rectangle_draw_vline)(int16_t x, int16_t y, int16_t l, uint32_t color){ |
52 |
if(l < 0){ rectangle_draw_vline(x, y+l, -l, color); return; } |
53 |
for(int16_t y_ = max(0,y); y_ < min(y+l,graph_get_YRes()); ++y_){ |
54 |
graph_set_pixel(x, y_, color); |
55 |
} |
56 |
} |
57 |
|
58 |
void (rectangle_draw)(const rectangle_t *p){ |
59 |
for(int16_t y = max(p->y,0); y < min(p->y+p->h, graph_get_YRes()); ++y) |
60 |
rectangle_draw_hline(p->x, y, p->w, p->fill_color); |
61 |
|
62 |
int16_t step = (p->outline_width > 0 ? 1 : -1); |
63 |
int16_t l = p->x, r = p->x+p->w, t = p->y, b = p->y+p->h; |
64 |
if(step > 0){ |
65 |
--l; ++r; --t; ++b; |
66 |
} |
67 |
for(int16_t i = 0; i != p->outline_width; i += step, l -= step, r += step, t -= step, b += step){ |
68 |
rectangle_draw_hline(l , t , r-l, p->outline_color); |
69 |
rectangle_draw_hline(l , b-1, r-l, p->outline_color);
|
70 |
rectangle_draw_vline(l , t , b-t, p->outline_color); |
71 |
rectangle_draw_vline(r-1, t , b-t, p->outline_color);
|
72 |
} |
73 |
} |