Project

General

Profile

Revision 183

can reduce letter size; working on increasing

View differences:

proj/font/split_font.py
11 11

  
12 12
i = 32
13 13

  
14
with open("font.h", 'w') as f:
15
    includes = ""
16
    lst = ""
14
includes = ""
15
lst = ""
17 16

  
18
    for j in range(i):
19
        if lst != "": lst += ","
20
        lst += "NULL"
17
for j in range(i):
18
    if lst != "": lst += ","
19
    lst += "NULL"
21 20

  
22 21

  
23
    for h in range(Nh):
24
        t = int(H/Nh)*h
25
        b = int(H/Nh)*(h+1)
26
        for w in range(Nw):
27
            #l = int((W*w)/Nw)
28
            #r = int((W*(w+1))/Nw)
29
            l = 59*w
30
            r = 59*(w+1)
31
            im1 = im.crop((l,t,r,b))
32
            im1.save("png/ascii%03d.png"%i)
33
            os.system("convert png/ascii%03d.png xpm/ascii%03d.xpm"%(i,i))
22
for h in range(Nh):
23
    t = int(H/Nh)*h
24
    b = int(H/Nh)*(h+1)
25
    for w in range(Nw):
26
        #l = int((W*w)/Nw)
27
        #r = int((W*(w+1))/Nw)
28
        l = 59*w
29
        r = 59*(w+1)
30
        im1 = im.crop((l,t,r,b))
31
        im1.save("png/ascii%03d.png"%i)
32
        os.system("convert png/ascii%03d.png xpm/ascii%03d.xpm"%(i,i))
33
        
34
        includes += '#include "%03d.xpm"\n'%i
35
        if lst != "": lst += ","
36
        lst += "%03d_xpm"%i
34 37

  
35
            
36
            includes += '#include "%03d.xpm"\n'%i
37
            if lst != "": lst += ","
38
            lst += "%03d_xpm"%i
38
        #DEV
39
        os.system("rm png/ascii%03d.png"%i)
39 40

  
40
            #DEV
41
            os.system("rm png/ascii%03d.png"%i)
41
        i += 1
42 42

  
43
            i += 1
44

  
45
    f.write(includes)
46
    f.write("xpm_map_t font[] = {"+lst+"}\n")
43
os.system('sed -i "s/black/#000000/g" xpm/*.xpm')
proj/include/font.h
1 1
#ifndef FONT_H_INCLUDED
2 2
#define FONT_H_INCLUDED
3 3

  
4
struct glyph;
5
typedef struct glyph glyph_t;
4
struct font;
5
typedef struct font font_t;
6
font_t* (font_ctor)(const char *s);
7
void    (font_dtor)(font_t *p);
6 8

  
7
glyph_t* (glyph_ctor)(const char **xpm);
8
void     (glyph_dtor)(glyph_t *p);
9
struct text;
10
typedef struct text text_t;
11
text_t* (text_ctor)(const font_t *fnt, const char *txt);
12
void    (text_dtor)(text_t *p);
13
void (text_set_text) (text_t *p, const char *txt);
14
void (text_set_pos)  (text_t *p, int16_t x, int16_t y);
15
void (text_set_size) (text_t *p, unsigned size);
16
void (text_set_color)(text_t *p, uint32_t color);
17
void (text_draw)     (const text_t *p);
9 18

  
10
//glyph_t** (get_font)(const char *s);
11

  
12 19
#endif //FONT_H_INCLUDED
proj/include/graph.h
1 1
#ifndef GRAPH_H_INCLUDED
2 2
#define GRAPH_H_INCLUDED
3 3

  
4
#include "graph_macros.h"
5

  
4 6
/// MACROS
5
//#define GET_ALP(n)          (0xFF & ((n) >> 24))
7
#define GET_ALP(n)          (0xFF & ((n) >> 24))
6 8
#define GET_RED(n)          (0xFF & ((n) >> 16))
7 9
#define GET_GRE(n)          (0xFF & ((n) >>  8))
8 10
#define GET_BLU(n)          (0xFF & (n      ))
9
//#define SET_ALP(n)          (((n)&0xFF) << 24)
11
#define GET_COLOR(n)        (0xFFFFFF & (n))
12
#define SET_ALP(n)          (((n)&0xFF) << 24)
10 13
#define SET_RED(n)          (((n)&0xFF) << 16)
11 14
#define SET_GRE(n)          (((n)&0xFF) <<  8)
12 15
#define SET_BLU(n)          (((n)&0xFF)      )
......
23 26
int (graph_cleanup)(void);
24 27

  
25 28
/// PIXEL DRAWING
26
int (graph_set_pixel)      (uint16_t x, uint16_t y, uint32_t color);
27
int (graph_set_pixel_alpha)(uint16_t x, uint16_t y, uint32_t color, uint8_t alpha);
29
int (graph_set_pixel)             (uint16_t x, uint16_t y, uint32_t color);
30
int (graph_set_pixel_buffer)      (uint16_t x, uint16_t y, uint32_t color, uint8_t *buf, uint16_t W, uint16_t H);
31
int (graph_set_pixel_alpha_buffer)(uint16_t x, uint16_t y, uint8_t alpha, uint8_t *alp_buf, uint16_t W, uint16_t H);
32
int (graph_set_pixel_alpha)       (uint16_t x, uint16_t y, uint32_t color, uint8_t alpha);
28 33

  
29 34
/// SCREEN
30 35
int (graph_clear_screen)(void);
proj/include/graph_macros.h
28 28

  
29 29
// Colors in RBG (8 bit)
30 30
#define BLACK               0x000000
31
#define WHITE               0xFFFFFF
31 32

  
32 33
#endif /* end of include guard: GRAPHICS_MACROS_H_INCLUDED */
proj/include/xpm_utils.h
5 5

  
6 6
char** xpm_load_xpm2(const char *s);
7 7

  
8
#endif //XMP_UTILS_H_INCLUDED
8
#endif //XMP_UTILS_H_INCLUDED
proj/src/font.c
2 2

  
3 3
#include "font.h"
4 4

  
5
#include "xpm_utils.h"
6
#include "graph.h"
7
#include "utils.h"
8
#include "errors.h"
9

  
5 10
struct glyph{
6 11
    uint16_t w, h;
7 12
    uint8_t *map;
8 13
};
9

  
10
glyph_t* (glyph_ctor)(const char **xpm){
14
typedef struct glyph glyph_t;
15
static glyph_t* (glyph_ctor)(const char **xpm){
16
    if(xpm == NULL) return NULL;
11 17
    glyph_t *ret = malloc(sizeof(glyph_t));
12 18
    if(ret == NULL) return NULL;
13 19
    enum xpm_image_type type = XPM_8_8_8_8;
......
21 27
    ret->h = img.height;
22 28
    return ret;
23 29
}
24

  
25
void (glyph_dtor)(glyph_t *p){
30
static void (glyph_dtor)(glyph_t *p){
26 31
    if(p == NULL) return;
27 32
    free(p->map);
28 33
    free(p);
29 34
}
35
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){
36
    if(p == NULL) return NULL_PTR;
37
    int r;
38
    for(int16_t h = 0; h < p->h; ++h){
39
        for(int16_t w = 0; w < p->w; ++w){
40
            uint32_t c = *((uint32_t*)p->map + w + h*p->w);
41
            uint8_t a = GET_ALP(c);
42
            //c = c & 0xFF000000;
43
            //if(c != 0) printf("%d %d 0x%X\n", w, h, c);
44
            //else       printf("%d %d <========================\n");
45

  
46
            r = graph_set_pixel_alpha_buffer(x+w, y-p->h+h, a, alp_buf, W, H);
47

  
48

  
49
            //r = graph_set_pixel_alpha(x+w, y-p->h+h, GET_COLOR(c), GET_ALP(c));
50

  
51
            //printf("%d %d 0x%X\n", x, y, *(buf+(x+y*p->w)*sizeof(uint32_t)));
52
            if(r != SUCCESS && r != OUT_OF_RANGE) return r;
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
    char filepath[1024];
75
    for(size_t i = 0; i < ret->nchars; ++i){
76
        sprintf(filepath, "%s/ascii%03d.xpm2", s, i);
77
        char **xpm = xpm_load_xpm2(filepath);
78
        ret->glyphs[i] = glyph_ctor((const char**)xpm);
79
    }
80
    return ret;
81
}
82
void (font_dtor)(font_t *p){
83
    if(p == NULL) return;
84
    for(size_t i = 0; i < p->nchars; ++i)
85
        glyph_dtor(p->glyphs[i]);
86
    free(p->glyphs);
87
    free(p);
88
}
89

  
90
struct text{
91
    const font_t *fnt;
92
    char *txt;
93
    int16_t x, y;
94
    int size;
95
    uint32_t color;
96
};
97
text_t* (text_ctor)(const font_t *fnt, const char *txt){
98
    if(fnt == NULL) return NULL;
99
    text_t *ret = malloc(sizeof(text_t));
100
    if(ret == NULL) return NULL;
101
    ret->fnt = fnt;
102
    ret->txt = NULL;
103
    text_set_text(ret, txt);
104
    ret->x = 0;
105
    ret->y = 0;
106
    ret->size = 70;
107
    ret->color = BLACK;
108
    return ret;
109
}
110
void (text_dtor)(text_t *p){
111
    if(p == NULL) return;
112
    free(p->txt);
113
    free(p);
114
}
115
void (text_set_text) (text_t *p, const char *txt){
116
    size_t sz = strlen(txt);
117
    p->txt = realloc(p->txt, (sz+1)*sizeof(char));
118
    if(p->txt == NULL) return;
119
    strcpy(p->txt, txt);
120
}
121
void (text_set_pos)  (text_t *p, int16_t x, int16_t y){ p->x = x; p->y = y; }
122
void (text_set_size) (text_t *p, unsigned size       ){ p->size = size;     }
123
void (text_set_color)(text_t *p, uint32_t color      ){ p->color = color;   }
124
void (text_draw)(const text_t *p){
125
    printf("-%s-\n", p->txt);
126
    const size_t len = strlen(p->txt);
127
    uint16_t W = 0, H = 0;
128
    for(size_t i = 0; i < len; ++i){
129
        const glyph_t *g = p->fnt->glyphs[(size_t)p->txt[i]];
130
        if(g != NULL){ W += g->w; H = max(H, g->h); }
131
    }
132
    printf("%d %d\n", W, H);
133

  
134
    uint8_t *alp_buf = malloc(W*H);
135

  
136
    {
137
        int16_t y = H;
138
        int16_t x = 0;
139
        for(size_t i = 0; i < len; ++i){
140
            const glyph_t *g = p->fnt->glyphs[(size_t)p->txt[i]];
141
            if(g != NULL){
142
                if(glyph_draw_to_alpha_buffer(g, x, y, alp_buf, W, H)) return;
143
                x += g->w;
144
            }
145
        }
146
    }
147
    double factor = (double)p->size/(double)H;
148

  
149
    uint16_t newH = H*factor;
150
    uint16_t newW = W*factor;
151

  
152
    for(size_t newy = 0; newy < newH; ++newy){
153
        size_t y = newy/factor;
154
        for(size_t newx = 0; newx < newW; ++newx){
155
            size_t x = newx/factor;
156
            *(alp_buf+newx+newy*W) = *(alp_buf+x+y*W);
157
            (void)x;
158
            (void)y;
159
        }
160
    }
161

  
162
    printf("L163\n");
163
    for(int16_t y = 0; y < newH; ++y){ //printf("L163,%d\n", y);
164
        for(int16_t x = 0; x < newW; ++x){ //if(y > 147) printf("L164, %d %d\n", x, y);
165
            uint8_t a = *(alp_buf+x+y*W); //if(y > 147) printf("L165\n");
166
            graph_set_pixel_alpha(x+p->x,y+p->y,p->color, a); //if(y > 147) printf("L166\n");
167
        }
168
    }
169
    printf("L170\n");
170
    free(alp_buf);
171
    printf("L172\n");
172
}
proj/src/graph.c
1 1
#include <lcom/lcf.h>
2 2

  
3 3
#include "graph.h"
4
#include "graph_macros.h"
4

  
5 5
#include "errors.h"
6

  
7 6
#include <stdio.h>
8 7

  
9 8
/// MACROS
......
245 244

  
246 245
/// PIXEL DRAWING
247 246
int (graph_set_pixel)(uint16_t x, uint16_t y, uint32_t color) {
248
    if (x >= vbe_mem_info.XResolution || y >= vbe_mem_info.YResolution) {
247
    /*
248
    if (x < 0 || vbe_mem_info.XResolution <= x || y < 0 || vbe_mem_info.YResolution <= y) {
249 249
        //printf("%s: invalid pixel.\n", __func__);
250 250
        return OUT_OF_RANGE;
251 251
    }
252 252
    unsigned int pos = (x + y * vbe_mem_info.XResolution) * graph_get_bytes_pixel();
253 253
    memcpy(video_buf + pos, &color, graph_get_bytes_pixel());
254 254
    return SUCCESS;
255
    */
256
    return graph_set_pixel_buffer(x, y, color, video_buf, graph_get_XRes(), graph_get_YRes());
255 257
}
258
int (graph_set_pixel_buffer)(uint16_t x, uint16_t y, uint32_t color, uint8_t *buf, uint16_t W, uint16_t H) {
259
    if (x < 0 || W <= x || y < 0 || H <= y) {
260
        //printf("%s: invalid pixel.\n", __func__);
261
        return OUT_OF_RANGE;
262
    }
263
    unsigned int pos = (x + y * W) * graph_get_bytes_pixel();
264
    memcpy(buf + pos, &color, graph_get_bytes_pixel());
265
    return SUCCESS;
266
}
267
int (graph_set_pixel_alpha_buffer)(uint16_t x, uint16_t y, uint8_t alpha, uint8_t *alp_buf, uint16_t W, uint16_t H) {
268
    if (x < 0 || W <= x || y < 0 || H <= y) {
269
        //printf("%s: invalid pixel.\n", __func__);
270
        return OUT_OF_RANGE;
271
    }
272
    unsigned int pos = x + y * W;
273
    memcpy(alp_buf + pos, &alpha, 1);
274
    return SUCCESS;
275
}
256 276
int (graph_set_pixel_alpha)(uint16_t x, uint16_t y, uint32_t color, uint8_t alpha){
257 277
    if (x >= vbe_mem_info.XResolution || y >= vbe_mem_info.YResolution) {
258 278
        //printf("%s: invalid pixel.\n", __func__);
259 279
        return OUT_OF_RANGE;
260 280
    }
281
    //printf("COLOR= %X, ALPHA = %X\n", color, alpha);
261 282
    unsigned int pos = (x + y * vbe_mem_info.XResolution) * graph_get_bytes_pixel();
262 283
    uint32_t color_;
263 284
    memcpy(&color_, video_buf + pos, graph_get_bytes_pixel());
proj/src/proj.c
16 16
#include "rectangle.h"
17 17
#include "font.h"
18 18

  
19
#ifdef DIOGO
20
    #include "shooter.h"
21
#endif
22

  
19 23
#ifdef TELMO
20 24
    #include "crosshair.h"
21 25
#endif
......
53 57
        //printf("%d\n", 1000000-(int)(1000000*fm_sin(0.5*M_PI)));
54 58
        //printf("%d\n", (int)(1000000*fm_cos(0.5*M_PI)));
55 59
        /*
60
        rectangle_t *rect = rectangle_ctor(0,0,graph_get_XRes(), graph_get_YRes());
61
        rectangle_set_fill_color(rect, WHITE);
62

  
56 63
        clock_t t = clock();
57 64
        sprite_t *shooter1 = get_shooter(); sprite_set_pos(shooter1, 100, 100);
58 65
        for(double angle = 0; angle <= 6.283185; angle += 0.006283185){
59 66
             sprite_set_angle(shooter1, angle);
60 67
             graph_clear_screen();
68
             rectangle_draw(rect);
61 69
             sprite_draw(shooter1);
62 70
             graph_draw();
63 71
        }
......
84 92
        graph_draw();
85 93
        */
86 94

  
95
        font_t      *fnt  = font_ctor("/home/lcom/labs/proj/font/xpm2");
87 96

  
88 97

  
98
        rectangle_t *rect = rectangle_ctor(100,100,100, 100);
99
        rectangle_set_fill_color(rect, WHITE);
100
        rectangle_draw(rect);
101
        rectangle_dtor(rect);
102

  
103
        text_t      *txt  = text_ctor(fnt, "Hello world!");
104
        text_set_color(txt, 0x00FF00);
105
        text_set_pos(txt, 100, 100);
106
        text_draw(txt);
107
        text_dtor(txt);
108

  
109
        graph_draw();
110

  
111
        font_dtor(fnt);
112

  
113
        tickdelay(micros_to_ticks(1000000));
114

  
89 115
    #endif
90 116

  
91 117
    #ifdef TELMO
......
100 126
    message msg;
101 127
    int good = 1;
102 128

  
129
    #ifdef DIOGO
130
        good = 0;
131
    #endif
132

  
103 133
    while (good) {
104 134
        /* Get a request message. */
105 135
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
proj/src/sprite.c
65 65
        for(int16_t x = max(0,p->x-diag); x < min(p->x+diag,graph_get_XRes()); ++x){
66 66
            sprite_src2pic(p, x, y, &u, &v);
67 67
            if(0 <= u && u < p->w && 0 <= v && v < p->h){
68
                uint8_t *m = p->map + (v*p->w + u)*4;
69
                uint32_t color = SET_RGB(*(m+2), *(m+1), *(m));
70
                graph_set_pixel_alpha(p->x + x, p->y + y, color, *(m+3));
68
                uint32_t c = *(uint32_t*)(p->map + (v*p->w + u)*4);
69
                graph_set_pixel_alpha(p->x + x, p->y + y, GET_COLOR(c), GET_ALP(c));
71 70
            }
72 71
        }
73 72
    }
proj/src/xpm_utils.c
32 32
        sscanf(line_buf, "%d %d %d %d", &w, &h, &num_colors, &chars_per_pixel);
33 33
        ret = malloc((1+num_colors+h)*sizeof(char*));
34 34
    }
35
    ret[0] = malloc((sz+1)*sizeof(char));
35
    ret[0] = malloc((sz+1)*sizeof(char)); if(ret[0] == NULL){ free(ret); return NULL; }
36 36
    strcpy(ret[0], line_buf);
37
    
37

  
38 38
    for(int i = 1; i < 1+num_colors+h; ++i){
39 39
        sz = getline(&line_buf, &len, f);
40 40
        ret[i] = malloc((sz+1)*sizeof(char));
41
        if(ret[i] == NULL){
42
            for(int j = 0; j < i; ++j)
43
                free(ret[i]);
44
            free(ret);
45
            return NULL;
46
        }
41 47
        strcpy(ret[i], line_buf);
42 48
        ret[i][sz-1] = '\0';
43
        printf("%s\n", ret[i]);
44 49
    }
45 50
    fclose(f); f = NULL;
46 51
    return ret;
proj/DR.mk
2 2

  
3 3
.PATH: ${.CURDIR}/src
4 4

  
5
SRCS= proj.c graph.c sprite.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c
5
SRCS= proj.c graph.c sprite.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c
6 6

  
7 7
CPPFLAGS += -pedantic -I./include -I./bmp -I./xpm -D LCOM_MACRO -D DIOGO -D __LCOM_OPTIMIZED_
8 8

  
proj/Makefile
2 2

  
3 3
.PATH: ${.CURDIR}/src
4 4

  
5
SRCS= proj.c graph.c sprite.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c
5
SRCS= proj.c graph.c sprite.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c
6 6

  
7 7
CPPFLAGS += -pedantic -I./include -I./bmp -I./xpm -D LCOM_MACRO -D __LCOM_OPTIMIZED_
8 8

  
proj/TB.mk
2 2

  
3 3
.PATH: ${.CURDIR}/src
4 4

  
5
SRCS= proj.c graph.c sprite.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c
5
SRCS= proj.c graph.c sprite.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c
6 6

  
7 7
CPPFLAGS += -pedantic -I./include -I./bmp -I./xpm -D LCOM_MACRO -D TELMO #-D __LCOM_OPTIMIZED_
8 8

  

Also available in: Unified diff