Project

General

Profile

Revision 182

adding font

View differences:

proj/font/setup.sh
1
python3 split_font.py
2
python3 xpm_to_xpm2.py
0 3

  
proj/font/setup_clean.sh
1
rm -f png/*.png
2
rm -f xpm/*.xpm
3
rm -f xpm2/*.xpm2
0 4

  
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 17

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

  
16
for h in range(Nh):
17
    t = int(H/Nh)*h
18
    b = int(H/Nh)*(h+1)
19
    for w in range(Nw):
20
        #l = int((W*w)/Nw)
21
        #r = int((W*(w+1))/Nw)
22
        l = 59*w
23
        r = 59*(w+1)
24
        im1 = im.crop((l,t,r,b))
25
        im1.save("%03d.png"%i)
26
        os.system("convert %03d.png xpm/%03d.xpm"%(i,i))
27
        os.system("rm %03d.png"%i)
28
        i += 1
22

  
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))
34

  
35
            
36
            includes += '#include "%03d.xpm"\n'%i
37
            if lst != "": lst += ","
38
            lst += "%03d_xpm"%i
39

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

  
43
            i += 1
44

  
45
    f.write(includes)
46
    f.write("xpm_map_t font[] = {"+lst+"}\n")
proj/font/xpm_to_xpm2.py
1
import glob
2
import os
3

  
4
files = [s[4:12] for s in glob.glob("xpm/*.xpm")]
5
files.sort()
6

  
7
with open("convert.c", 'w') as f:
8
    f.write('#include "xpm_utils.h"\n')
9
    f.write("".join(['#include "%s.xpm"\n'%s for s in files]))
10
    f.write("int main(){\n")
11
    f.write("".join(['    xpm_save_as_xpm2(%s,"xpm2/%s.xpm2");\n'%(s,s) for s in files]))
12
    f.write('    char **r = xpm_load_xpm2("xpm2/ascii065.xpm2");\n')
13
    f.write("    return 0;\n}\n")
14

  
15
os.system("gcc -I./xpm -I../include convert.c ../src/xpm_utils.c -o convert.app")
16
os.system("./convert.app")
17

  
18
os.system("rm -f convert.c convert.app")
19
os.system("rm -f xpm/*.xpm")
0 20

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

  
4
struct glyph;
5
typedef struct glyph glyph_t;
6

  
7
glyph_t* (glyph_ctor)(const char **xpm);
8
void     (glyph_dtor)(glyph_t *p);
9

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

  
12
#endif //FONT_H_INCLUDED
0 13

  
proj/include/xpm_utils.h
1
#ifndef XMP_UTILS_H_INCLUDED
2
#define XMP_UTILS_H_INCLUDED
3

  
4
void xpm_save_as_xpm2(char **p, const char *s);
5

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

  
8
#endif //XMP_UTILS_H_INCLUDED
0 9

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

  
3
#include "font.h"
4

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

  
10
glyph_t* (glyph_ctor)(const char **xpm){
11
    glyph_t *ret = malloc(sizeof(glyph_t));
12
    if(ret == NULL) return NULL;
13
    enum xpm_image_type type = XPM_8_8_8_8;
14
    xpm_image_t img;
15
    ret->map = xpm_load((xpm_map_t)xpm, type, &img);
16
    if(ret->map == NULL){
17
        free(ret);
18
        return NULL;
19
    }
20
    ret->w = img.width;
21
    ret->h = img.height;
22
    return ret;
23
}
24

  
25
void (glyph_dtor)(glyph_t *p){
26
    if(p == NULL) return;
27
    free(p->map);
28
    free(p);
29
}
0 30

  
proj/src/proj.c
14 14
#include "graph.h"
15 15
#include "sprite.h"
16 16
#include "rectangle.h"
17
#include "font.h"
17 18

  
18
#ifdef DIOGO
19
    #include "shooter.h"
20
#endif
21 19
#ifdef TELMO
22 20
    #include "crosshair.h"
23 21
#endif
......
85 83

  
86 84
        graph_draw();
87 85
        */
86

  
87

  
88

  
88 89
    #endif
89 90

  
90 91
    #ifdef TELMO
proj/src/sprite.c
11 11
    int16_t x, y;
12 12
    uint16_t w, h;
13 13
    int16_t u0, v0;
14
    float theta;
14
    double theta;
15 15
    uint8_t *map;
16 16
};
17 17

  
proj/src/xpm_utils.c
1
#ifdef LCOM_MACRO
2
    #include <lcom/lcf.h>
3
#endif
4

  
5
#include "xpm_utils.h"
6

  
7
#include <stdio.h>
8
#include <stdlib.h>
9
#include <string.h>
10

  
11
void xpm_save_as_xpm2(char **p, const char *s){
12
    FILE *f = fopen(s, "w");
13
    int w, h, num_colors, chars_per_pixel;
14
    sscanf(p[0], "%d %d %d %d", &w, &h, &num_colors, &chars_per_pixel);
15
    for(int i = 0; i < 1+num_colors+h; ++i){
16
        fprintf(f, "%s\n", p[i]);
17
    }
18
    fclose(f); f = NULL;
19
}
20

  
21
char** xpm_load_xpm2(const char *fpath){
22
    FILE *f = fopen(fpath, "r");
23
    if(f == NULL) return NULL;
24
    char *line_buf = malloc(1024*sizeof(char)); size_t len = 1024;
25
    int sz;
26

  
27
    char **ret = NULL;
28

  
29
    int w, h, num_colors, chars_per_pixel;
30

  
31
    sz = getline(&line_buf, &len, f);{
32
        sscanf(line_buf, "%d %d %d %d", &w, &h, &num_colors, &chars_per_pixel);
33
        ret = malloc((1+num_colors+h)*sizeof(char*));
34
    }
35
    ret[0] = malloc((sz+1)*sizeof(char));
36
    strcpy(ret[0], line_buf);
37
    
38
    for(int i = 1; i < 1+num_colors+h; ++i){
39
        sz = getline(&line_buf, &len, f);
40
        ret[i] = malloc((sz+1)*sizeof(char));
41
        strcpy(ret[i], line_buf);
42
        ret[i][sz-1] = '\0';
43
        printf("%s\n", ret[i]);
44
    }
45
    fclose(f); f = NULL;
46
    return ret;
47
}
0 48

  
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
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
6 6

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

  
9 9
DPADD += ${LIBLCF}
10 10
LDADD += -llcf
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
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
6 6

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

  
9 9
DPADD += ${LIBLCF}
10 10
LDADD += -llcf
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
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
6 6

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

  
9 9
DPADD += ${LIBLCF}
10 10
LDADD += -llcf
proj/setup.sh
1
cd font
2
./setup.sh
0 3

  
proj/setup_clean.sh
1
cd font
2
./setup_clean.sh
0 3

  

Also available in: Unified diff