altos/draw: Add a reasonable API for drawing, add lines.
[fw/altos] / src / draw / ao_rect.c
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 #include "ao.h"
16 #include "ao_draw.h"
17 #include "ao_draw_int.h"
18
19 #define bound(val,max) do {                     \
20                 if (val < 0) {                  \
21                         val = 0;                \
22                 }                               \
23                 if (val > max) {                \
24                         val = max;              \
25                 }                               \
26         } while (0)
27
28 void
29 ao_rect(const struct ao_bitmap  *dst,
30         int16_t                 x,
31         int16_t                 y,
32         int16_t                 width,
33         int16_t                 height,
34         uint32_t                fill,
35         uint8_t                 rop)
36 {
37         int16_t x2 = x + width;
38         int16_t y2 = y + height;
39
40         bound(x, dst->width);
41         bound(x2, dst->width);
42         bound(y, dst->height);
43         bound(y2, dst->height);
44
45         if (x < x2 && y < y2) {
46                 ao_solid(ao_and(rop, fill),
47                          ao_xor(rop, fill),
48                          dst->base + y * dst->stride,
49                          dst->stride,
50                          x,
51                          x2 - x,
52                          y2 - y);
53         }
54 }
55