Project

General

Profile

Revision 314

some changes in fonts and ent.c

View differences:

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

  
4
enum text_valign{
4
struct font;
5
typedef struct font font_t;
6
font_t* (font_ctor)(const char *s);
7
int     (font_dtor)(font_t *p);
8

  
9
int (font_init)(void);
10
const font_t* font_get_default(void);
11
const font_t* font_get_consolas(void);
12
int (font_free)(void);
13

  
14
typedef enum{
5 15
    text_valign_top    = -1,
6 16
    text_valign_center =  0,
7 17
    text_valign_bottom =  1
8
};
18
} text_valign;
9 19

  
10
enum text_halign{
20
typedef enum{
11 21
    text_halign_left   = -1,
12 22
    text_halign_center =  0,
13 23
    text_halign_right  =  1
14
};
24
} text_halign;
15 25

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

  
21
font_t *consolas;
22
font_t *default_font;
23

  
24
int (font_init)(void);
25
int (font_free)(void);
26

  
27 26
struct text;
28 27
typedef struct text text_t;
29 28
text_t* (text_ctor)(const font_t *fnt, const char *txt);
30 29
void    (text_dtor)(text_t *p);
31 30
char* (text_get_string)(const text_t *p);
32
void (text_set_text)  (text_t *p, const char *txt);
31
void (text_set_string)(text_t *p, const char *txt);
33 32
void (text_set_pos)   (text_t *p, int16_t x, int16_t y);
34 33
void (text_set_size)  (text_t *p, unsigned size);
35 34
void (text_set_color) (text_t *p, uint32_t color);
36
void (text_set_valign)(text_t *p, enum text_valign valign);
37
void (text_set_halign)(text_t *p, enum text_halign halign);
35
void (text_set_valign)(text_t *p, text_valign valign);
36
void (text_set_halign)(text_t *p, text_halign halign);
38 37
int16_t (text_get_x)  (const text_t *p);
39 38
int16_t (text_get_y)  (const text_t *p);
40
int (text_draw)     (const text_t *p);
39
int     (text_draw)   (const text_t *p);
41 40

  
42 41
#endif //FONT_H_INCLUDED
proj/libs/graph/src/font.c
8 8
#include "errors.h"
9 9
#include <assert.h>
10 10

  
11
/// GLYPH
11 12
struct glyph{
12 13
    uint16_t w, h;
13 14
    uint8_t *map;
......
55 56
    return SUCCESS;
56 57
}
57 58

  
59
/// FONT
58 60
struct font{
59 61
    size_t nchars;
60 62
    glyph_t **glyphs;
61 63
};
62

  
63 64
font_t* (font_ctor)(const char *s){
64 65
    font_t *ret = malloc(sizeof(font_t));
65 66
    if(ret == NULL) return NULL;
......
93 94
    return SUCCESS;
94 95
}
95 96

  
96
font_t *consolas = NULL;
97
font_t *default_font = NULL;
97
static font_t *consolas     = NULL;
98
static font_t *default_font = NULL;
98 99

  
99 100
int (font_init)(void){
100 101
    consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
......
102 103
    default_font = consolas;
103 104
    return SUCCESS;
104 105
}
106
const font_t* font_get_default(void){ return default_font; }
107
const font_t* font_get_consolas(void){ return consolas; }
105 108
int (font_free)(void){
106 109
    int r;
107 110
    if((r = font_dtor(consolas))) return r;
......
110 113
    return SUCCESS;
111 114
}
112 115

  
116
/// TEXT
113 117
struct text{
114 118
    const font_t *fnt;
115 119
    char *txt;
116 120
    int16_t x, y;
117 121
    int size;
118 122
    uint32_t color;
119
    enum text_valign valign;
120
    enum text_halign halign;
123
    text_valign valign;
124
    text_halign halign;
121 125
};
122 126
text_t* (text_ctor)(const font_t *fnt, const char *txt){
123 127
    if(fnt == NULL) return NULL;
......
125 129
    if(ret == NULL) return NULL;
126 130
    ret->fnt = fnt;
127 131
    ret->txt = NULL;
128
    text_set_text(ret, txt);
132
    text_set_string(ret, txt);
129 133
    ret->x = 0;
130 134
    ret->y = 0;
131 135
    ret->size = 25;
......
139 143
    free(p->txt);
140 144
    free(p);
141 145
}
142
void (text_set_text) (text_t *p, const char *txt){
146
void (text_set_string) (text_t *p, const char *txt){
143 147
    size_t sz = strlen(txt);
144 148
    p->txt = realloc(p->txt, (sz+1)*sizeof(char));
145 149
    if(p->txt == NULL) return;
146 150
    strcpy(p->txt, txt);
147 151
}
148 152
char* (text_get_string)(const text_t *p){return p->txt; }
149
void (text_set_pos)   (text_t *p, int16_t x, int16_t y   ){ p->x = x; p->y = y; }
150
void (text_set_size)  (text_t *p, unsigned size          ){ p->size = size    ; }
151
void (text_set_color) (text_t *p, uint32_t color         ){ p->color = color  ; }
152
void (text_set_valign)(text_t *p, enum text_valign valign){ p->valign = valign; }
153
void (text_set_halign)(text_t *p, enum text_halign halign){ p->halign = halign; }
153
void (text_set_pos)   (text_t *p, int16_t x, int16_t y){ p->x = x; p->y = y; }
154
void (text_set_size)  (text_t *p, unsigned size       ){ p->size = size    ; }
155
void (text_set_color) (text_t *p, uint32_t color      ){ p->color = color  ; }
156
void (text_set_valign)(text_t *p, text_valign valign  ){ p->valign = valign; }
157
void (text_set_halign)(text_t *p, text_halign halign  ){ p->halign = halign; }
154 158
int16_t (text_get_x)  (const text_t *p){ return p->x; }
155 159
int16_t (text_get_y)  (const text_t *p){ return p->y; }
156 160

  
proj/src/ent.c
28 28
    sprite_t *dude;
29 29
    sprite_t *weapon;
30 30
    double health, current_health;
31
    rectangle_t *green_bar, *red_bar;
31 32
    text_t *txt;
32 33
    gunner_type type;
33 34
    int team;
......
41 42
    ret->y = 0.0;
42 43
    ret->health = 100;
43 44
    ret->current_health = ret->health;
44
    ret->txt = text_ctor(default_font, "");
45
    ret->green_bar = rectangle_ctor(0,0,0,0);
46
    ret->red_bar   = rectangle_ctor(0,0,0,0);
47
    ret->txt = text_ctor(font_get_default(), "");
45 48
    ret->type = type;
46 49
    ret->team = team;
47 50
    ret->dude   = sprite_ctor(dude  );
48 51
    ret->weapon = sprite_ctor(weapon);
49
    if(ret->dude == NULL || ret->weapon == NULL){
52
    if(ret->txt == NULL || ret->dude == NULL || ret->weapon == NULL || ret->green_bar == NULL || ret->red_bar == NULL){
50 53
        gunner_dtor(ret);
51 54
        return NULL;
52 55
    }
56
    rectangle_set_fill_color(ret->green_bar, GREEN_HEALTH_COLOR);
57
    rectangle_set_fill_color(ret->red_bar  , GRAPH_RED         );
53 58
    text_set_size(ret->txt, 15);
54 59
    text_set_valign(ret->txt, text_valign_center);
55 60
    text_set_halign(ret->txt, text_halign_center);
......
60 65
    if(p == NULL) return;
61 66
    sprite_dtor(p->dude);
62 67
    sprite_dtor(p->weapon);
68
    rectangle_dtor(p->green_bar);
69
    rectangle_dtor(p->red_bar);
63 70
    text_dtor(p->txt);
64 71
    free(p);
65 72
}
......
99 106
    sprite_draw     (p->dude  );
100 107
    gunner_draw_health(p);
101 108
}
102

  
103 109
void (gunner_draw_health)(const gunner_t *p) {
104 110
    int16_t w = sprite_get_w(p->dude);
105 111
    int16_t h = sprite_get_h(p->dude);
......
108 114
    double curr_health = gunner_get_curr_health(p);
109 115
    double health = gunner_get_health(p);
110 116
    double perc = curr_health/health;
111
    rectangle_t *green_bar = rectangle_ctor(x, y, (int16_t)(w*perc), 10);
112
    rectangle_set_fill_color(green_bar, GREEN_HEALTH_COLOR);
113
    rectangle_t *red_bar = rectangle_ctor(x+(int16_t)(w*perc), y, (int16_t)(w*(1-perc)), 10);
114
    rectangle_set_fill_color(red_bar, GRAPH_RED);
117
    rectangle_set_pos(p->green_bar, x, y); rectangle_set_size(p->green_bar, (int16_t)(w*perc), 10);
118
    rectangle_set_pos(p->red_bar, x+(int16_t)(w*perc), y); rectangle_set_size(p->red_bar, (int16_t)(w*(1-perc)), 10);
115 119
    char buf[20]; sprintf(buf, "%d/%d", (int)p->current_health, (int)p->health);
116
    text_set_text(p->txt, buf);
120
    text_set_string(p->txt, buf);
117 121
    text_set_pos(p->txt, x+w/2, y+10/2);
118
    rectangle_draw(green_bar);
119
    rectangle_draw(red_bar);
122
    rectangle_draw(p->green_bar);
123
    rectangle_draw(p->red_bar);
120 124
    text_draw(p->txt);
121
    rectangle_dtor(green_bar);
122
    rectangle_dtor(red_bar);
123

  
124 125
}
125 126

  
126 127
double (gunner_distance)(const gunner_t *p1, const gunner_t *p2){
proj/src/proj.c
80 80
    /// Load stuff
81 81
    {
82 82
        graph_clear_screen();
83
        text_t *txt = text_ctor(default_font, "Loading...");
83
        text_t *txt = text_ctor(font_get_default(), "Loading...");
84 84
        text_set_pos(txt, graph_get_XRes()/2, graph_get_YRes()/2);
85 85
        text_set_valign(txt, text_valign_center);
86 86
        text_set_halign(txt, text_halign_center);
......
100 100
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
101 101
    }
102 102

  
103
    menu_t *main_menu = menu_ctor(default_font);
103
    menu_t *main_menu = menu_ctor(font_get_default());
104 104
    menu_add_item(main_menu, "Single player");
105 105
    menu_add_item(main_menu, "Multiplayer");
106 106
    menu_add_item(main_menu, "Chat");
......
205 205
static int (multiplayer)(void) {
206 206
    int r;
207 207

  
208
    menu_t *main_menu = menu_ctor(default_font);
208
    menu_t *main_menu = menu_ctor(font_get_default());
209 209
    menu_add_item(main_menu, "Create");
210 210
    menu_add_item(main_menu, "Connect");
211 211
    menu_add_item(main_menu, "Back");
......
270 270
    nctp_set_processor(multiplayer_process);/*
271 271

  
272 272
    ent_set_scale(DEFAULT_SCALE);
273
    text_timer_t *in_game_timer = timer_ctor(default_font);
273
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
274 274

  
275 275
    list_t *shooter_list = list_ctor();
276 276

  
......
334 334
    nctp_set_processor(multiplayer_process);
335 335

  
336 336
    ent_set_scale(DEFAULT_SCALE);
337
    text_timer_t *in_game_timer = timer_ctor(default_font);
337
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
338 338

  
339 339
    list_t *shooter_list = list_ctor();
340 340

  
......
398 398

  
399 399
    int r;
400 400

  
401
    menu_t *main_menu = menu_ctor(default_font);
401
    menu_t *main_menu = menu_ctor(font_get_default());
402 402
    menu_add_item(main_menu, "Campaign");
403 403
    menu_add_item(main_menu, "Zombies");
404 404
    menu_add_item(main_menu, "Back");
......
462 462
    int r;
463 463

  
464 464
    ent_set_scale(DEFAULT_SCALE);
465
    text_timer_t *in_game_timer = timer_ctor(default_font);
465
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
466 466

  
467 467
    list_t *shooter_list = list_ctor();
468 468

  
......
570 570
    int r;
571 571

  
572 572
    ent_set_scale(DEFAULT_SCALE);
573
    text_timer_t *in_game_timer = timer_ctor(default_font);
573
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
574 574

  
575 575
    list_t *shooter_list = list_ctor();
576 576

  
......
715 715
        strcpy(buffer2, dest);
716 716
        strncat(buffer2, " <", 2);
717 717
        for(size_t i = CHAT_MAX_NUM-1; i; --i)
718
        text_set_text(t_text[i], text_get_string(t_text[i-1]));
719
        text_set_text(t_text[0], buffer2);
718
        text_set_string(t_text[i], text_get_string(t_text[i-1]));
719
        text_set_string(t_text[0], buffer2);
720 720
        for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
721 721
            if(text_get_string(t_text[i])[0] == '>'){
722 722
                text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
......
749 749
        rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT);
750 750
    }
751 751
    text_t      *t_buffer = NULL; {
752
        t_buffer = text_ctor(default_font, "");
752
        t_buffer = text_ctor(font_get_default(), "");
753 753
        text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
754 754
        rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
755 755
        text_set_halign(t_buffer, text_halign_left);
......
757 757
        text_set_color (t_buffer, TEXT_COLOR);
758 758
    }
759 759
    text_t      *t_size   = NULL; {
760
        t_size = text_ctor(default_font, "");
760
        t_size = text_ctor(font_get_default(), "");
761 761
        text_set_pos(t_size, rectangle_get_x(r_buffer)+rectangle_get_w(r_buffer)-5,
762 762
        rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)-5);
763 763
        text_set_halign(t_size, text_halign_right);
......
766 766
        text_set_size  (t_size, 18);
767 767
        char buffer2[20];
768 768
        sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
769
        text_set_text(t_size, buffer2);
769
        text_set_string(t_size, buffer2);
770 770
    }
771 771

  
772 772
    /** r_text */ {
......
780 780
    }
781 781
    /** t_text */ {
782 782
    for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
783
        t_text[i] = text_ctor(default_font, " ");
783
        t_text[i] = text_ctor(font_get_default(), " ");
784 784
        text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
785 785
        rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i);
786 786
        text_set_halign(t_text[i], text_halign_left);
......
820 820
                    char buffer2[CHAT_MAX_SIZE+3] = "> ";
821 821
                    strncat(buffer2, buffer, strlen(buffer));
822 822
                    for(size_t i = CHAT_MAX_NUM-1; i; --i)
823
                    text_set_text(t_text[i], text_get_string(t_text[i-1]));
824
                    text_set_text(t_text[0], buffer2);
823
                    text_set_string(t_text[i], text_get_string(t_text[i-1]));
824
                    text_set_string(t_text[0], buffer2);
825 825
                    for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
826 826
                        if(text_get_string(t_text[i])[0] == '>'){
827 827
                            text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
......
840 840
                    if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1);
841 841
                    else                               printf("Char limit exceeded\n");
842 842
                }
843
                text_set_text(t_buffer, buffer);
843
                text_set_string(t_buffer, buffer);
844 844
                char buffer2[20];
845 845
                sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
846
                text_set_text(t_size, buffer2);
846
                text_set_string(t_size, buffer2);
847 847
                case MOUSE_IRQ:
848 848
                if (counter_mouse_ih >= 3) {
849 849
                    mouse_parse_packet(packet_mouse_ih, &pp);

Also available in: Unified diff