Project

General

Profile

Revision 285

deleting sprite.h

View differences:

proj/include/rectangle.h
1
#ifndef RECTANGLE_H_INCLUDED
2
#define RECTANGLE_H_INCLUDED
3

  
4
struct rectangle;
5
typedef struct rectangle rectangle_t;
6

  
7
rectangle_t* (rectangle_ctor)(int16_t x, int16_t y, uint16_t w, uint16_t h);
8
void         (rectangle_dtor)(rectangle_t *p);
9

  
10
void (rectangle_set_pos)          (rectangle_t *p,  int16_t x,  int16_t y);
11
void (rectangle_set_size)         (rectangle_t *p, uint16_t w, uint16_t h);
12
void (rectangle_set_fill_color)   (rectangle_t *p, uint32_t color);
13
void (rectangle_set_outline_color)(rectangle_t *p, uint32_t color);
14
void (rectangle_set_outline_width)(rectangle_t *p,  int16_t width);
15

  
16
int16_t  (rectangle_get_x)(const rectangle_t *p);
17
int16_t  (rectangle_get_y)(const rectangle_t *p);
18
uint16_t (rectangle_get_w)(const rectangle_t *p);
19
uint16_t (rectangle_get_h)(const rectangle_t *p);
20
int      (rectangle_collide_point)(const rectangle_t *p, int x, int y);
21

  
22
void (rectangle_draw)(const rectangle_t *p);
23

  
24
#endif //RECTANGLE_H_INCLUDED
25 0

  
proj/include/font.h
1
#ifndef FONT_H_INCLUDED
2
#define FONT_H_INCLUDED
3

  
4
enum text_valign{
5
    text_valign_top    = -1,
6
    text_valign_center =  0,
7
    text_valign_bottom =  1
8
};
9

  
10
enum text_halign{
11
    text_halign_left   = -1,
12
    text_halign_center =  0,
13
    text_halign_right  =  1
14
};
15

  
16
struct font;
17
typedef struct font font_t;
18
font_t* (font_ctor)(const char *s);
19
void    (font_dtor)(font_t *p);
20

  
21
struct text;
22
typedef struct text text_t;
23
text_t* (text_ctor)(const font_t *fnt, const char *txt);
24
void    (text_dtor)(text_t *p);
25
char* (text_get_string)(const text_t *p);
26
void (text_set_text)  (text_t *p, const char *txt);
27
void (text_set_pos)   (text_t *p, int16_t x, int16_t y);
28
void (text_set_size)  (text_t *p, unsigned size);
29
void (text_set_color) (text_t *p, uint32_t color);
30
void (text_set_valign)(text_t *p, enum text_valign valign);
31
void (text_set_halign)(text_t *p, enum text_halign halign);
32
int (text_draw)     (const text_t *p);
33

  
34
#endif //FONT_H_INCLUDED
35 0

  
proj/include/errors.h
1
#ifndef ERRORS_H_INCLUDED
2
#define ERRORS_H_INCLUDED
3

  
4
/** @brief Error Codes */
5
enum errors {
6
    SUCCESS = 0,        /** @brief Sucessful */
7
    NULL_PTR = 250,     /** @brief Null Pointer Error */
8
    LCF_ERROR,          /** @brief Error originated on LCF */
9
    SBCR_ERROR,         /** @brief Error on Subscribing Interrupt */
10
    UNSBCR_ERROR,       /** @brief Error on Unsubscring Interrupt*/
11
    READ_ERROR,         /** @brief Error on Reading from Port */
12
    WRITE_ERROR,        /** @brief Error on Writing to Port */
13
    TIMEOUT_ERROR,      /** @brief Timeout error */
14
    INVALID_COMMAND,    /** @brief Invalid Command issued */
15
    INVALID_STATE,      /** @brief State machine reached an invalid state */
16
    BIOS_CALL_ERROR,    /** @brief Error upon BIOS call */
17
    OUT_OF_RANGE,       /** @brief Accessing area out of range of memory */
18
    ALLOC_ERROR,        /** @brief Memory allocation error */
19
    LOGIC_ERROR,        /** @brief Logic error */
20
    INVALID_ARG,        /** @brief Invalid argument */
21
    NOK,                /** @brief Not OK */
22
    TRANS_REFUSED,      /** @brief Transmission refused */
23
    TRANS_FAILED,       /** @brief Transmission failed */
24
    OTHER_ERROR         /** @brief Unspecified error */
25
};
26

  
27
#endif //ERRORS_H_INCLUDED
28 0

  
proj/src/rectangle.c
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
int (rectangle_collide_point)(const rectangle_t *p, int x, int y) {
45
    int16_t x0 = p->x, y0 = p->y;
46
    return (x >= x0 && x <= x0 + p->w) && (y >= y0 && y <= y0 + p->h);
47
}
48

  
49
static void (rectangle_draw_hline)(int16_t x, int16_t y, int16_t l, uint32_t color){
50
    if(l < 0){ rectangle_draw_hline(x+l, y, -l, color); return; }
51
    for(int16_t x_ = max(0,x); x_ < min(x+l,graph_get_XRes()); ++x_){
52
        graph_set_pixel(x_, y, color);
53
    }
54
}
55

  
56
static void (rectangle_draw_vline)(int16_t x, int16_t y, int16_t l, uint32_t color){
57
    if(l < 0){ rectangle_draw_vline(x, y+l, -l, color); return; }
58
    for(int16_t y_ = max(0,y); y_ < min(y+l,graph_get_YRes()); ++y_){
59
        graph_set_pixel(x, y_, color);
60
    }
61
}
62

  
63
void (rectangle_draw)(const rectangle_t *p){
64
    for(int16_t y = max(p->y,0); y < min(p->y+p->h, graph_get_YRes()); ++y)
65
        rectangle_draw_hline(p->x, y, p->w, p->fill_color);
66

  
67
    int16_t step = (p->outline_width > 0 ? 1 : -1);
68
    int16_t l = p->x, r = p->x+p->w, t = p->y, b = p->y+p->h;
69
    if(step > 0){
70
        --l; ++r; --t; ++b;
71
    }
72
    for(int16_t i = 0; i != p->outline_width; i += step, l -= step, r += step, t -= step, b += step){
73
        rectangle_draw_hline(l  , t  , r-l, p->outline_color);
74
        rectangle_draw_hline(l  , b-1, r-l, p->outline_color);
75
        rectangle_draw_vline(l  , t  , b-t, p->outline_color);
76
        rectangle_draw_vline(r-1, t  , b-t, p->outline_color);
77
    }
78
}
79 0

  
proj/src/font.c
1
#include <lcom/lcf.h>
2

  
3
#include "font.h"
4

  
5
#include "xpm_utils.h"
6
#include "graph.h"
7
#include "utils.h"
8
#include "errors.h"
9
#include <assert.h>
10

  
11
struct glyph{
12
    uint16_t w, h;
13
    uint8_t *map;
14
};
15
typedef struct glyph glyph_t;
16
static glyph_t* (glyph_ctor)(const char **xpm){
17
    if(xpm == NULL) return NULL;
18
    glyph_t *ret = malloc(sizeof(glyph_t));
19
    if(ret == NULL) return NULL;
20
    enum xpm_image_type type = XPM_8_8_8_8;
21
    xpm_image_t img;
22
    ret->map = NULL;
23
    uint8_t *map = xpm_load((xpm_map_t)xpm, type, &img);
24
    if(map == NULL){
25
        free(ret);
26
        return NULL;
27
    }
28
    ret->w = img.width;
29
    ret->h = img.height;
30
    ret->map = malloc(ret->w*ret->h*sizeof(uint8_t));
31
    if(ret->map == NULL){
32
        free(ret);
33
        return NULL;
34
    }
35
    for(unsigned i = 0; i < ret->w*ret->h; ++i){
36
        ret->map[i] = map[4*i+3];
37
    }
38
    return ret;
39
}
40
static void (glyph_dtor)(glyph_t *p){
41
    if(p == NULL) return;
42
    free(p->map);
43
    free(p);
44
}
45
static int (glyph_draw_to_alpha_buffer)(const glyph_t *p, int16_t x, int16_t y, uint8_t *alp_buf, uint16_t W, uint16_t H){
46
    if(p == NULL) return NULL_PTR;
47
    for(int16_t h = 0; h < p->h; ++h){
48
        for(int16_t w = 0; w < p->w; ++w){
49
            uint8_t a = *(p->map + w + h*p->w);
50
            int16_t x_ = x+w, y_ = y-p->h+h;
51
            unsigned pos = x_ +y_*W;
52
            alp_buf[pos] = a;
53
        }
54
    }
55
    return SUCCESS;
56
}
57

  
58
struct font{
59
    size_t nchars;
60
    glyph_t **glyphs;
61
};
62
#include "sprite.h"
63
#include "rectangle.h"
64
font_t* (font_ctor)(const char *s){
65
    font_t *ret = malloc(sizeof(font_t));
66
    if(ret == NULL) return NULL;
67
    ret->nchars = 128;
68
    ret->glyphs = malloc(ret->nchars*sizeof(glyph_t*));
69
    if(ret->glyphs == NULL){
70
        free(ret->glyphs);
71
        free(ret);
72
        return NULL;
73
    }
74
    int good = false;
75
    char filepath[1024];
76
    for(size_t i = 0; i < ret->nchars; ++i){
77
        sprintf(filepath, "%s/ascii%03d.xpm2", s, i);
78
        char **xpm = xpm_load_xpm2(filepath);
79
        ret->glyphs[i] = glyph_ctor((const char**)xpm);
80
        if(ret->glyphs[i] != NULL) good = true;
81
    }
82
    if(good) return ret;
83
    else{
84
        //font_dtor(ret);
85
        return NULL;
86
    }
87
}
88
void (font_dtor)(font_t *p){
89
    if(p == NULL) return;
90
    for(size_t i = 0; i < p->nchars; ++i)
91
        glyph_dtor(p->glyphs[i]);
92
    free(p->glyphs);
93
    free(p);
94
}
95

  
96
struct text{
97
    const font_t *fnt;
98
    char *txt;
99
    int16_t x, y;
100
    int size;
101
    uint32_t color;
102
    enum text_valign valign;
103
    enum text_halign halign;
104
};
105
text_t* (text_ctor)(const font_t *fnt, const char *txt){
106
    if(fnt == NULL) return NULL;
107
    text_t *ret = malloc(sizeof(text_t));
108
    if(ret == NULL) return NULL;
109
    ret->fnt = fnt;
110
    ret->txt = NULL;
111
    text_set_text(ret, txt);
112
    ret->x = 0;
113
    ret->y = 0;
114
    ret->size = 25;
115
    ret->color = GRAPH_BLACK;
116
    ret->valign = text_valign_top;
117
    ret->halign = text_halign_left;
118
    return ret;
119
}
120
void (text_dtor)(text_t *p){
121
    if(p == NULL) return;
122
    free(p->txt);
123
    free(p);
124
}
125
void (text_set_text) (text_t *p, const char *txt){
126
    size_t sz = strlen(txt);
127
    p->txt = realloc(p->txt, (sz+1)*sizeof(char));
128
    if(p->txt == NULL) return;
129
    strcpy(p->txt, txt);
130
}
131
char* (text_get_string)(const text_t *p){return p->txt; }
132
void (text_set_pos)   (text_t *p, int16_t x, int16_t y   ){ p->x = x; p->y = y; }
133
void (text_set_size)  (text_t *p, unsigned size          ){ p->size = size    ; }
134
void (text_set_color) (text_t *p, uint32_t color         ){ p->color = color  ; }
135
void (text_set_valign)(text_t *p, enum text_valign valign){ p->valign = valign; }
136
void (text_set_halign)(text_t *p, enum text_halign halign){ p->halign = halign; }
137

  
138
int (text_draw)(const text_t *p){
139
    if(p == NULL) return NULL_PTR;
140
    int ret = SUCCESS;
141
    // Get buffer with rescaled text
142
    uint8_t *alp_new_buf = NULL;
143
    uint16_t newH, newW;{
144
        const size_t len = strlen(p->txt);
145
        uint16_t W = 0, H = 0; {
146
            for(size_t i = 0; i < len; ++i){
147
                const glyph_t *g = p->fnt->glyphs[(size_t)p->txt[i]];
148
                if(g != NULL){ W += g->w; H = max(H, g->h); }
149
            }
150
        }
151
        uint8_t *alp_buf = malloc(W*H);
152
        if(alp_buf == NULL) return ALLOC_ERROR;{
153
            int16_t y = H;
154
            int16_t x = 0;
155
            for(size_t i = 0; i < len; ++i){
156
                const glyph_t *g = p->fnt->glyphs[(size_t)p->txt[i]];
157
                if(g != NULL){
158
                    if((ret = glyph_draw_to_alpha_buffer(g, x, y, alp_buf, W, H))) return ret;
159
                    x += g->w;
160
                }
161
            }
162
        }
163

  
164
        double factor = (double)p->size/(double)H;
165

  
166
        newH = H*factor;
167
        newW = W*factor;
168
        alp_new_buf = malloc(newW*newH);
169
        if(alp_new_buf == NULL) return ALLOC_ERROR;
170

  
171
        for(size_t newy = 0; newy < newH; ++newy){
172
            size_t y = newy/factor;
173
            for(size_t newx = 0; newx < newW; ++newx){
174
                size_t x = newx/factor;
175
                *(alp_new_buf+newx+newy*newW) = *(alp_buf+x+y*W);
176
            }
177
        }
178
        free(alp_buf);
179

  
180

  
181
    }
182
    // Get initial value of x
183
    int16_t initx;{
184
        switch(p->halign){
185
            case text_halign_left  : initx = p->x         ; break;
186
            case text_halign_center: initx = p->x - newW/2; break;
187
            case text_halign_right : initx = p->x - newW  ; break;
188
            default: return LOGIC_ERROR;
189
        }
190
    }
191
    // Get initial value of y
192
    int16_t inity;{
193
        switch(p->valign){
194
            case text_valign_top   : inity = p->y         ; break;
195
            case text_valign_center: inity = p->y - newH/2; break;
196
            case text_valign_bottom: inity = p->y - newH  ; break;
197
            default: return LOGIC_ERROR;
198
        }
199
    }
200
    // Draw text
201
    for(int16_t newy = 0; newy < newH; ++newy){
202
        for(int16_t newx = 0; newx < newW; ++newx){
203
            uint8_t a = *(alp_new_buf+newx+newy*newW);
204
            if(a < 0x7F) graph_set_pixel(initx+newx,inity+newy,p->color);
205
        }
206
    }
207
    free(alp_new_buf);
208
    return SUCCESS;
209
}
210 0

  
proj/include/sprite.h
1 1
#ifndef SPRITE_H_INCLUDED
2 2
#define SPRITE_H_INCLUDED
3 3

  
4
struct basic_sprite;
5
typedef struct basic_sprite basic_sprite_t;
6 4

  
7
basic_sprite_t* (basic_sprite_ctor)(const char **xpm, int16_t u0, int16_t v0);
8
void            (basic_sprite_dtor)(basic_sprite_t *p);
9
const uint8_t* (basic_sprite_get_map)(const basic_sprite_t *p);
10
uint16_t       (basic_sprite_get_w)  (const basic_sprite_t *p);
11
uint16_t       (basic_sprite_get_h)  (const basic_sprite_t *p);
12
int16_t        (basic_sprite_get_u0) (const basic_sprite_t *p);
13
int16_t        (basic_sprite_get_v0) (const basic_sprite_t *p);
14
/*
15
struct basic_sprite_alpha;
16
typedef struct basic_sprite_alpha basic_sprite_alpha_t;
17 5

  
18
basic_sprite_alpha_t* (basic_sprite_alpha_ctor)(const char **xpm, int16_t u0, int16_t v0);
19
void                  (basic_sprite_alpha_dtor)(basic_sprite_alpha_t *p);
20
const uint8_t*        (basic_sprite_alpha_get_map)(const basic_sprite_alpha_t *p);
21
uint16_t              (basic_sprite_alpha_get_w)  (const basic_sprite_alpha_t *p);
22
uint16_t              (basic_sprite_alpha_get_h)  (const basic_sprite_alpha_t *p);
23
int16_t               (basic_sprite_alpha_get_u0) (const basic_sprite_alpha_t *p);
24
int16_t               (basic_sprite_alpha_get_v0) (const basic_sprite_alpha_t *p);
25
*/
26
struct sprite;
27
typedef struct sprite sprite_t;
28

  
29
sprite_t* (sprite_ctor)(const basic_sprite_t *bsp);
30
/*
31
 * /!\ WARNING: Entity destructor does not destruct the basic_sprite passed as
32
 * constructor arguments, since it is assumed the same basic_sprite may be used to
33
 * draw several sprites. It is thus YOUR responsibility to delete basic_sprite's.
34
 * @param   p   Pointer to sprite_t to be destructed
35
 */
36
void      (sprite_dtor)(sprite_t *p);
37

  
38
void (sprite_set_pos)   (sprite_t *p, int16_t x, int16_t y);
39
void (sprite_set_center)(sprite_t *p, int16_t u0, int16_t v0);
40
void (sprite_set_angle) (sprite_t *p, double angle);
41
void (sprite_set_scale) (sprite_t *p, double scale);
42

  
43
int16_t  (sprite_get_x)(const sprite_t *p);
44
int16_t  (sprite_get_y)(const sprite_t *p);
45
double   (sprite_get_angle)(const sprite_t *p);
46
uint16_t (sprite_get_w)(const sprite_t *p);
47
uint16_t (sprite_get_h)(const sprite_t *p);
48

  
49
void (sprite_draw)(const sprite_t *p);
50

  
51 6
#endif //SPRITE_H_INCLUDED
proj/libs/graph/include/font.h
1
#ifndef FONT_H_INCLUDED
2
#define FONT_H_INCLUDED
3

  
4
enum text_valign{
5
    text_valign_top    = -1,
6
    text_valign_center =  0,
7
    text_valign_bottom =  1
8
};
9

  
10
enum text_halign{
11
    text_halign_left   = -1,
12
    text_halign_center =  0,
13
    text_halign_right  =  1
14
};
15

  
16
struct font;
17
typedef struct font font_t;
18
font_t* (font_ctor)(const char *s);
19
void    (font_dtor)(font_t *p);
20

  
21
struct text;
22
typedef struct text text_t;
23
text_t* (text_ctor)(const font_t *fnt, const char *txt);
24
void    (text_dtor)(text_t *p);
25
char* (text_get_string)(const text_t *p);
26
void (text_set_text)  (text_t *p, const char *txt);
27
void (text_set_pos)   (text_t *p, int16_t x, int16_t y);
28
void (text_set_size)  (text_t *p, unsigned size);
29
void (text_set_color) (text_t *p, uint32_t color);
30
void (text_set_valign)(text_t *p, enum text_valign valign);
31
void (text_set_halign)(text_t *p, enum text_halign halign);
32
int (text_draw)     (const text_t *p);
33

  
34
#endif //FONT_H_INCLUDED
0 35

  
proj/libs/graph/include/graph.h
50 50
/// DRAW
51 51
int (graph_draw)(void);
52 52

  
53
/// SPRITE ================================
54
struct basic_sprite;
55
typedef struct basic_sprite basic_sprite_t;
56

  
57
basic_sprite_t* (basic_sprite_ctor)(const char **xpm, int16_t u0, int16_t v0);
58
void            (basic_sprite_dtor)(basic_sprite_t *p);
59
const uint8_t* (basic_sprite_get_map)(const basic_sprite_t *p);
60
uint16_t       (basic_sprite_get_w)  (const basic_sprite_t *p);
61
uint16_t       (basic_sprite_get_h)  (const basic_sprite_t *p);
62
int16_t        (basic_sprite_get_u0) (const basic_sprite_t *p);
63
int16_t        (basic_sprite_get_v0) (const basic_sprite_t *p);
64

  
65
struct sprite;
66
typedef struct sprite sprite_t;
67

  
68
sprite_t* (sprite_ctor)(const basic_sprite_t *bsp);
69
/*
70
 * /!\ WARNING: Entity destructor does not destruct the basic_sprite passed as
71
 * constructor arguments, since it is assumed the same basic_sprite may be used to
72
 * draw several sprites. It is thus YOUR responsibility to delete basic_sprite's.
73
 * @param   p   Pointer to sprite_t to be destructed
74
 */
75
void      (sprite_dtor)(sprite_t *p);
76

  
77
void (sprite_set_pos)   (sprite_t *p, int16_t x, int16_t y);
78
void (sprite_set_center)(sprite_t *p, int16_t u0, int16_t v0);
79
void (sprite_set_angle) (sprite_t *p, double angle);
80
void (sprite_set_scale) (sprite_t *p, double scale);
81

  
82
int16_t  (sprite_get_x)(const sprite_t *p);
83
int16_t  (sprite_get_y)(const sprite_t *p);
84
double   (sprite_get_angle)(const sprite_t *p);
85
uint16_t (sprite_get_w)(const sprite_t *p);
86
uint16_t (sprite_get_h)(const sprite_t *p);
87

  
88
void (sprite_draw)(const sprite_t *p);
89

  
53 90
#endif /* end of include guard: GRAPH_H_INCLUDED */
proj/libs/graph/include/rectangle.h
1
#ifndef RECTANGLE_H_INCLUDED
2
#define RECTANGLE_H_INCLUDED
3

  
4
struct rectangle;
5
typedef struct rectangle rectangle_t;
6

  
7
rectangle_t* (rectangle_ctor)(int16_t x, int16_t y, uint16_t w, uint16_t h);
8
void         (rectangle_dtor)(rectangle_t *p);
9

  
10
void (rectangle_set_pos)          (rectangle_t *p,  int16_t x,  int16_t y);
11
void (rectangle_set_size)         (rectangle_t *p, uint16_t w, uint16_t h);
12
void (rectangle_set_fill_color)   (rectangle_t *p, uint32_t color);
13
void (rectangle_set_outline_color)(rectangle_t *p, uint32_t color);
14
void (rectangle_set_outline_width)(rectangle_t *p,  int16_t width);
15

  
16
int16_t  (rectangle_get_x)(const rectangle_t *p);
17
int16_t  (rectangle_get_y)(const rectangle_t *p);
18
uint16_t (rectangle_get_w)(const rectangle_t *p);
19
uint16_t (rectangle_get_h)(const rectangle_t *p);
20
int      (rectangle_collide_point)(const rectangle_t *p, int x, int y);
21

  
22
void (rectangle_draw)(const rectangle_t *p);
23

  
24
#endif //RECTANGLE_H_INCLUDED
0 25

  
proj/libs/graph/src/font.c
1
#include <lcom/lcf.h>
2

  
3
#include "font.h"
4

  
5
#include "xpm_utils.h"
6
#include "graph.h"
7
#include "utils.h"
8
#include "errors.h"
9
#include <assert.h>
10

  
11
struct glyph{
12
    uint16_t w, h;
13
    uint8_t *map;
14
};
15
typedef struct glyph glyph_t;
16
static glyph_t* (glyph_ctor)(const char **xpm){
17
    if(xpm == NULL) return NULL;
18
    glyph_t *ret = malloc(sizeof(glyph_t));
19
    if(ret == NULL) return NULL;
20
    enum xpm_image_type type = XPM_8_8_8_8;
21
    xpm_image_t img;
22
    ret->map = NULL;
23
    uint8_t *map = xpm_load((xpm_map_t)xpm, type, &img);
24
    if(map == NULL){
25
        free(ret);
26
        return NULL;
27
    }
28
    ret->w = img.width;
29
    ret->h = img.height;
30
    ret->map = malloc(ret->w*ret->h*sizeof(uint8_t));
31
    if(ret->map == NULL){
32
        free(ret);
33
        return NULL;
34
    }
35
    for(unsigned i = 0; i < ret->w*ret->h; ++i){
36
        ret->map[i] = map[4*i+3];
37
    }
38
    return ret;
39
}
40
static void (glyph_dtor)(glyph_t *p){
41
    if(p == NULL) return;
42
    free(p->map);
43
    free(p);
44
}
45
static int (glyph_draw_to_alpha_buffer)(const glyph_t *p, int16_t x, int16_t y, uint8_t *alp_buf, uint16_t W, uint16_t H){
46
    if(p == NULL) return NULL_PTR;
47
    for(int16_t h = 0; h < p->h; ++h){
48
        for(int16_t w = 0; w < p->w; ++w){
49
            uint8_t a = *(p->map + w + h*p->w);
50
            int16_t x_ = x+w, y_ = y-p->h+h;
51
            unsigned pos = x_ +y_*W;
52
            alp_buf[pos] = a;
53
        }
54
    }
55
    return SUCCESS;
56
}
57

  
58
struct font{
59
    size_t nchars;
60
    glyph_t **glyphs;
61
};
62
#include "sprite.h"
63
#include "rectangle.h"
64
font_t* (font_ctor)(const char *s){
65
    font_t *ret = malloc(sizeof(font_t));
66
    if(ret == NULL) return NULL;
67
    ret->nchars = 128;
68
    ret->glyphs = malloc(ret->nchars*sizeof(glyph_t*));
69
    if(ret->glyphs == NULL){
70
        free(ret->glyphs);
71
        free(ret);
72
        return NULL;
73
    }
74
    int good = false;
75
    char filepath[1024];
76
    for(size_t i = 0; i < ret->nchars; ++i){
77
        sprintf(filepath, "%s/ascii%03d.xpm2", s, i);
78
        char **xpm = xpm_load_xpm2(filepath);
79
        ret->glyphs[i] = glyph_ctor((const char**)xpm);
80
        if(ret->glyphs[i] != NULL) good = true;
81
    }
82
    if(good) return ret;
83
    else{
84
        //font_dtor(ret);
85
        return NULL;
86
    }
87
}
88
void (font_dtor)(font_t *p){
89
    if(p == NULL) return;
90
    for(size_t i = 0; i < p->nchars; ++i)
91
        glyph_dtor(p->glyphs[i]);
92
    free(p->glyphs);
93
    free(p);
94
}
95

  
96
struct text{
97
    const font_t *fnt;
98
    char *txt;
99
    int16_t x, y;
100
    int size;
101
    uint32_t color;
102
    enum text_valign valign;
103
    enum text_halign halign;
104
};
105
text_t* (text_ctor)(const font_t *fnt, const char *txt){
106
    if(fnt == NULL) return NULL;
107
    text_t *ret = malloc(sizeof(text_t));
108
    if(ret == NULL) return NULL;
109
    ret->fnt = fnt;
110
    ret->txt = NULL;
111
    text_set_text(ret, txt);
112
    ret->x = 0;
113
    ret->y = 0;
114
    ret->size = 25;
115
    ret->color = GRAPH_BLACK;
116
    ret->valign = text_valign_top;
117
    ret->halign = text_halign_left;
118
    return ret;
119
}
120
void (text_dtor)(text_t *p){
121
    if(p == NULL) return;
122
    free(p->txt);
123
    free(p);
124
}
125
void (text_set_text) (text_t *p, const char *txt){
126
    size_t sz = strlen(txt);
127
    p->txt = realloc(p->txt, (sz+1)*sizeof(char));
128
    if(p->txt == NULL) return;
129
    strcpy(p->txt, txt);
130
}
131
char* (text_get_string)(const text_t *p){return p->txt; }
132
void (text_set_pos)   (text_t *p, int16_t x, int16_t y   ){ p->x = x; p->y = y; }
133
void (text_set_size)  (text_t *p, unsigned size          ){ p->size = size    ; }
134
void (text_set_color) (text_t *p, uint32_t color         ){ p->color = color  ; }
135
void (text_set_valign)(text_t *p, enum text_valign valign){ p->valign = valign; }
136
void (text_set_halign)(text_t *p, enum text_halign halign){ p->halign = halign; }
137

  
138
int (text_draw)(const text_t *p){
139
    if(p == NULL) return NULL_PTR;
140
    int ret = SUCCESS;
141
    // Get buffer with rescaled text
142
    uint8_t *alp_new_buf = NULL;
143
    uint16_t newH, newW;{
144
        const size_t len = strlen(p->txt);
145
        uint16_t W = 0, H = 0; {
146
            for(size_t i = 0; i < len; ++i){
147
                const glyph_t *g = p->fnt->glyphs[(size_t)p->txt[i]];
148
                if(g != NULL){ W += g->w; H = max(H, g->h); }
149
            }
150
        }
151
        uint8_t *alp_buf = malloc(W*H);
152
        if(alp_buf == NULL) return ALLOC_ERROR;{
153
            int16_t y = H;
154
            int16_t x = 0;
155
            for(size_t i = 0; i < len; ++i){
156
                const glyph_t *g = p->fnt->glyphs[(size_t)p->txt[i]];
157
                if(g != NULL){
158
                    if((ret = glyph_draw_to_alpha_buffer(g, x, y, alp_buf, W, H))) return ret;
159
                    x += g->w;
160
                }
161
            }
162
        }
163

  
164
        double factor = (double)p->size/(double)H;
165

  
166
        newH = H*factor;
167
        newW = W*factor;
168
        alp_new_buf = malloc(newW*newH);
169
        if(alp_new_buf == NULL) return ALLOC_ERROR;
170

  
171
        for(size_t newy = 0; newy < newH; ++newy){
172
            size_t y = newy/factor;
173
            for(size_t newx = 0; newx < newW; ++newx){
174
                size_t x = newx/factor;
175
                *(alp_new_buf+newx+newy*newW) = *(alp_buf+x+y*W);
176
            }
177
        }
178
        free(alp_buf);
179

  
180

  
181
    }
182
    // Get initial value of x
183
    int16_t initx;{
184
        switch(p->halign){
185
            case text_halign_left  : initx = p->x         ; break;
186
            case text_halign_center: initx = p->x - newW/2; break;
187
            case text_halign_right : initx = p->x - newW  ; break;
188
            default: return LOGIC_ERROR;
189
        }
190
    }
191
    // Get initial value of y
192
    int16_t inity;{
193
        switch(p->valign){
194
            case text_valign_top   : inity = p->y         ; break;
195
            case text_valign_center: inity = p->y - newH/2; break;
196
            case text_valign_bottom: inity = p->y - newH  ; break;
197
            default: return LOGIC_ERROR;
198
        }
199
    }
200
    // Draw text
201
    for(int16_t newy = 0; newy < newH; ++newy){
202
        for(int16_t newx = 0; newx < newW; ++newx){
203
            uint8_t a = *(alp_new_buf+newx+newy*newW);
204
            if(a < 0x7F) graph_set_pixel(initx+newx,inity+newy,p->color);
205
        }
206
    }
207
    free(alp_new_buf);
208
    return SUCCESS;
209
}
0 210

  
proj/libs/graph/src/rectangle.c
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
int (rectangle_collide_point)(const rectangle_t *p, int x, int y) {
45
    int16_t x0 = p->x, y0 = p->y;
46
    return (x >= x0 && x <= x0 + p->w) && (y >= y0 && y <= y0 + p->h);
47
}
48

  
49
static void (rectangle_draw_hline)(int16_t x, int16_t y, int16_t l, uint32_t color){
50
    if(l < 0){ rectangle_draw_hline(x+l, y, -l, color); return; }
51
    for(int16_t x_ = max(0,x); x_ < min(x+l,graph_get_XRes()); ++x_){
52
        graph_set_pixel(x_, y, color);
53
    }
54
}
55

  
56
static void (rectangle_draw_vline)(int16_t x, int16_t y, int16_t l, uint32_t color){
57
    if(l < 0){ rectangle_draw_vline(x, y+l, -l, color); return; }
58
    for(int16_t y_ = max(0,y); y_ < min(y+l,graph_get_YRes()); ++y_){
59
        graph_set_pixel(x, y_, color);
60
    }
61
}
62

  
63
void (rectangle_draw)(const rectangle_t *p){
64
    for(int16_t y = max(p->y,0); y < min(p->y+p->h, graph_get_YRes()); ++y)
65
        rectangle_draw_hline(p->x, y, p->w, p->fill_color);
66

  
67
    int16_t step = (p->outline_width > 0 ? 1 : -1);
68
    int16_t l = p->x, r = p->x+p->w, t = p->y, b = p->y+p->h;
69
    if(step > 0){
70
        --l; ++r; --t; ++b;
71
    }
72
    for(int16_t i = 0; i != p->outline_width; i += step, l -= step, r += step, t -= step, b += step){
73
        rectangle_draw_hline(l  , t  , r-l, p->outline_color);
74
        rectangle_draw_hline(l  , b-1, r-l, p->outline_color);
75
        rectangle_draw_vline(l  , t  , b-t, p->outline_color);
76
        rectangle_draw_vline(r-1, t  , b-t, p->outline_color);
77
    }
78
}
0 79

  
proj/libs/utils/include/errors.h
1
#ifndef ERRORS_H_INCLUDED
2
#define ERRORS_H_INCLUDED
3

  
4
/** @brief Error Codes */
5
enum errors {
6
    SUCCESS = 0,        /** @brief Sucessful */
7
    NULL_PTR = 250,     /** @brief Null Pointer Error */
8
    LCF_ERROR,          /** @brief Error originated on LCF */
9
    SBCR_ERROR,         /** @brief Error on Subscribing Interrupt */
10
    UNSBCR_ERROR,       /** @brief Error on Unsubscring Interrupt*/
11
    READ_ERROR,         /** @brief Error on Reading from Port */
12
    WRITE_ERROR,        /** @brief Error on Writing to Port */
13
    TIMEOUT_ERROR,      /** @brief Timeout error */
14
    INVALID_COMMAND,    /** @brief Invalid Command issued */
15
    INVALID_STATE,      /** @brief State machine reached an invalid state */
16
    BIOS_CALL_ERROR,    /** @brief Error upon BIOS call */
17
    OUT_OF_RANGE,       /** @brief Accessing area out of range of memory */
18
    ALLOC_ERROR,        /** @brief Memory allocation error */
19
    LOGIC_ERROR,        /** @brief Logic error */
20
    INVALID_ARG,        /** @brief Invalid argument */
21
    NOK,                /** @brief Not OK */
22
    TRANS_REFUSED,      /** @brief Transmission refused */
23
    TRANS_FAILED,       /** @brief Transmission failed */
24
    OTHER_ERROR         /** @brief Unspecified error */
25
};
26

  
27
#endif //ERRORS_H_INCLUDED
0 28

  

Also available in: Unified diff