Revision 319
documented font. also changed a bit the timer
proj/include/proj_func.h | ||
---|---|---|
49 | 49 |
typedef struct timer { |
50 | 50 |
int time; |
51 | 51 |
text_t *text; |
52 |
char *array; |
|
53 | 52 |
} text_timer_t; |
54 | 53 |
|
55 | 54 |
text_timer_t* (timer_ctor)(const font_t *fnt); |
proj/libs/graph/include/font.h | ||
---|---|---|
2 | 2 |
#define FONT_H_INCLUDED |
3 | 3 |
|
4 | 4 |
struct font; |
5 |
/** |
|
6 |
* @brief Font. Represents a set of glyphs that can be drawn for showing text. |
|
7 |
*/ |
|
5 | 8 |
typedef struct font font_t; |
9 |
/** |
|
10 |
* @brief Construct font. |
|
11 |
* |
|
12 |
* The font is constructed from a provided path. That path is required to |
|
13 |
* contain glyphs with the name "ascii???.xpm2", files with format XPM2 where |
|
14 |
* instead of having "???" we have 3-digit ASCII codes, where the glyph with |
|
15 |
* that name map to that XPM2 file. |
|
16 |
* @param s Path to directory |
|
17 |
* @return Pointer to font |
|
18 |
*/ |
|
6 | 19 |
font_t* (font_ctor)(const char *s); |
7 |
int (font_dtor)(font_t *p); |
|
20 |
/** |
|
21 |
* @brief Destruct font. |
|
22 |
* @param p Pointer to font to be destroyed |
|
23 |
*/ |
|
24 |
void (font_dtor)(font_t *p); |
|
8 | 25 |
|
26 |
/** |
|
27 |
* @brief Initialize font stuff. |
|
28 |
* |
|
29 |
* More specifically, this involves loading the default font, which is currently consolas. |
|
30 |
* @return SUCCESS if initialization was successful, false otherwise |
|
31 |
*/ |
|
9 | 32 |
int (font_init)(void); |
33 |
/** |
|
34 |
* @brief Get default font. |
|
35 |
* |
|
36 |
* Currently, the default font is Consolas. |
|
37 |
* @return Pointer to default font |
|
38 |
*/ |
|
10 | 39 |
const font_t* font_get_default(void); |
40 |
/** |
|
41 |
* @brief Get Consolas font. |
|
42 |
* @return Pointer to Consolas font |
|
43 |
*/ |
|
11 | 44 |
const font_t* font_get_consolas(void); |
12 |
int (font_free)(void); |
|
45 |
/** |
|
46 |
* @brief Free resources used on initialization of fonts. |
|
47 |
*/ |
|
48 |
void (font_free)(void); |
|
13 | 49 |
|
50 |
/** |
|
51 |
* @brief Enumeration of vertical alignment schemes. |
|
52 |
*/ |
|
14 | 53 |
typedef enum{ |
15 | 54 |
text_valign_top = -1, |
16 | 55 |
text_valign_center = 0, |
17 | 56 |
text_valign_bottom = 1 |
18 | 57 |
} text_valign; |
19 |
|
|
58 |
/** |
|
59 |
* @brief Enumeration of horizontal alignment schemes. |
|
60 |
*/ |
|
20 | 61 |
typedef enum{ |
21 | 62 |
text_halign_left = -1, |
22 | 63 |
text_halign_center = 0, |
... | ... | |
24 | 65 |
} text_halign; |
25 | 66 |
|
26 | 67 |
struct text; |
68 |
/** |
|
69 |
* @brief Drawable text. |
|
70 |
*/ |
|
27 | 71 |
typedef struct text text_t; |
72 |
/** |
|
73 |
* @brief Construct text. |
|
74 |
* @param fnt Font to draw the text |
|
75 |
* @param txt String to be drawn |
|
76 |
* @return Pointer to constructed text |
|
77 |
*/ |
|
28 | 78 |
text_t* (text_ctor)(const font_t *fnt, const char *txt); |
79 |
/** |
|
80 |
* @brief Destruct text. |
|
81 |
* @param p Pointer to text to be destroyed |
|
82 |
*/ |
|
29 | 83 |
void (text_dtor)(text_t *p); |
30 |
char* (text_get_string)(const text_t *p); |
|
84 |
/** |
|
85 |
* @brief Set text string. |
|
86 |
* @param p Pointer to text |
|
87 |
* @param txt String to be set |
|
88 |
*/ |
|
31 | 89 |
void (text_set_string)(text_t *p, const char *txt); |
90 |
/** |
|
91 |
* @brief Set text position. |
|
92 |
* @param p Pointer to text |
|
93 |
* @param x X-position of the text |
|
94 |
* @param y Y-position of the text |
|
95 |
*/ |
|
32 | 96 |
void (text_set_pos) (text_t *p, int16_t x, int16_t y); |
97 |
/** |
|
98 |
* @brief Set text size (letter size). |
|
99 |
* @param p Pointer to text |
|
100 |
* @param size Letter size of the text |
|
101 |
*/ |
|
33 | 102 |
void (text_set_size) (text_t *p, unsigned size); |
103 |
/** |
|
104 |
* @brief Set text color. |
|
105 |
* @param p Pointer to text |
|
106 |
* @param color Text color |
|
107 |
*/ |
|
34 | 108 |
void (text_set_color) (text_t *p, uint32_t color); |
109 |
/** |
|
110 |
* @brief Set text vertical alignment. |
|
111 |
* @param p Pointer to text |
|
112 |
* @param valign Vertical alignment of the text |
|
113 |
*/ |
|
35 | 114 |
void (text_set_valign)(text_t *p, text_valign valign); |
115 |
/** |
|
116 |
* @brief Set text horizontal alignment. |
|
117 |
* @param p Pointer to text |
|
118 |
* @param halign Horizontal alignment of the text |
|
119 |
*/ |
|
36 | 120 |
void (text_set_halign)(text_t *p, text_halign halign); |
121 |
/** |
|
122 |
* @brief Get string of the text. |
|
123 |
* @param p Pointer to text |
|
124 |
* @return Pointer to string |
|
125 |
*/ |
|
126 |
const char* (text_get_string)(const text_t *p); |
|
127 |
/** |
|
128 |
* @brief Get text x-position. |
|
129 |
* @param p Pointer to text |
|
130 |
* @return X-position of text |
|
131 |
*/ |
|
37 | 132 |
int16_t (text_get_x) (const text_t *p); |
133 |
/** |
|
134 |
* @brief Get text y-position. |
|
135 |
* @param p Pointer to text |
|
136 |
* @return Y-position of text |
|
137 |
*/ |
|
38 | 138 |
int16_t (text_get_y) (const text_t *p); |
139 |
/** |
|
140 |
* @brief Draw text on the screen buffer. |
|
141 |
* @param p Pointer to text |
|
142 |
* @return SUCCESS if operation was successful, false otherwise |
|
143 |
*/ |
|
39 | 144 |
int (text_draw) (const text_t *p); |
40 | 145 |
|
41 | 146 |
#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 |
* @brief Text glyph. Represents a letter that can be drawn. |
|
13 |
*/ |
|
12 | 14 |
typedef struct { |
13 | 15 |
uint16_t w, h; |
14 | 16 |
uint8_t *map; |
15 | 17 |
} glyph_t; |
18 |
/** |
|
19 |
* @brief Construct glyph. |
|
20 |
* @param xpm XPM describing the glyph |
|
21 |
* @return Pointer to created glyph |
|
22 |
*/ |
|
16 | 23 |
static glyph_t* (glyph_ctor)(const char **xpm){ |
17 | 24 |
if(xpm == NULL) return NULL; |
18 | 25 |
glyph_t *ret = malloc(sizeof(glyph_t)); |
... | ... | |
37 | 44 |
} |
38 | 45 |
return ret; |
39 | 46 |
} |
47 |
/** |
|
48 |
* @brief Destruct glyph. |
|
49 |
* @param p Pointer to glyph to be destroyed |
|
50 |
*/ |
|
40 | 51 |
static void (glyph_dtor)(glyph_t *p){ |
41 | 52 |
if(p == NULL) return; |
42 | 53 |
free(p->map); |
43 | 54 |
free(p); |
44 | 55 |
} |
56 |
/** |
|
57 |
* @brief Draw glyph to an alpha-buffer. |
|
58 |
* |
|
59 |
* An alpha-buffer is an array of uint8_t that represents transparencies. |
|
60 |
* It proves fit as a tool for drawing text. |
|
61 |
* @param p Pointer to glyph to be drawn |
|
62 |
* @param x X-position to draw the glyph |
|
63 |
* @param y Y-position to draw the glyph |
|
64 |
* @param alp_buf Alpha-buffer to which the glyph will be drawn |
|
65 |
* @param W Width of the alpha-buffer |
|
66 |
* @param H Height of the alpha-buffer |
|
67 |
*/ |
|
45 | 68 |
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 | 69 |
if(p == NULL) return NULL_PTR; |
47 | 70 |
for(int16_t h = 0; h < p->h; ++h){ |
... | ... | |
84 | 107 |
return NULL; |
85 | 108 |
} |
86 | 109 |
} |
87 |
int (font_dtor)(font_t *p){
|
|
88 |
if(p == NULL) return SUCCESS;
|
|
110 |
void (font_dtor)(font_t *p){
|
|
111 |
if(p == NULL) return; |
|
89 | 112 |
for(size_t i = 0; i < p->nchars; ++i) |
90 | 113 |
glyph_dtor(p->glyphs[i]); |
91 | 114 |
free(p->glyphs); |
92 | 115 |
free(p); |
93 |
return SUCCESS; |
|
94 | 116 |
} |
95 | 117 |
|
96 | 118 |
static font_t *consolas = NULL; |
... | ... | |
104 | 126 |
} |
105 | 127 |
const font_t* font_get_default(void){ return default_font; } |
106 | 128 |
const font_t* font_get_consolas(void){ return consolas; } |
107 |
int (font_free)(void){ |
|
108 |
int r; |
|
109 |
if((r = font_dtor(consolas))) return r; |
|
129 |
void (font_free)(void){ |
|
130 |
font_dtor(consolas); |
|
110 | 131 |
consolas = NULL; |
111 | 132 |
default_font = NULL; |
112 |
return SUCCESS; |
|
113 | 133 |
} |
114 | 134 |
|
115 | 135 |
/// TEXT |
... | ... | |
148 | 168 |
if(p->txt == NULL) return; |
149 | 169 |
strcpy(p->txt, txt); |
150 | 170 |
} |
151 |
char* (text_get_string)(const text_t *p){return p->txt; } |
|
171 |
const char* (text_get_string)(const text_t *p){return p->txt; }
|
|
152 | 172 |
void (text_set_pos) (text_t *p, int16_t x, int16_t y){ p->x = x; p->y = y; } |
153 | 173 |
void (text_set_size) (text_t *p, unsigned size ){ p->size = size ; } |
154 | 174 |
void (text_set_color) (text_t *p, uint32_t color ){ p->color = color ; } |
proj/src/proj.c | ||
---|---|---|
165 | 165 |
basic_sprite_dtor (bsp_pistol ); bsp_pistol = NULL; |
166 | 166 |
basic_sprite_dtor (bsp_nothing ); bsp_nothing = NULL; |
167 | 167 |
map_dtor (map1 ); map1 = NULL; |
168 |
if(font_free()){ printf("Failed to free fonts\n"); return 1; }
|
|
168 |
font_free();
|
|
169 | 169 |
|
170 | 170 |
// Unsubscribe interrupts |
171 | 171 |
if (unsubscribe_all()) { |
proj/src/proj_func.c | ||
---|---|---|
284 | 284 |
if (ret == NULL) return NULL; |
285 | 285 |
ret->time = 0; |
286 | 286 |
ret->text = text_ctor(fnt, "000s"); |
287 |
ret->array = text_get_string(ret->text); |
|
288 | 287 |
text_set_color(ret->text, TEXT_COLOR); |
289 | 288 |
return ret; |
290 | 289 |
} |
... | ... | |
292 | 291 |
void (timer_update)(text_timer_t *p){ |
293 | 292 |
if (p->time >= 999) return; |
294 | 293 |
p->time++; |
295 |
p->array[2] = p->time % 10 + '0';
|
|
296 |
p->array[1] = (p->time/10) % 10 + '0';
|
|
297 |
p->array[0] = (p->time/100) % 10 + '0';
|
|
294 |
char buffer[100];
|
|
295 |
sprintf(buffer, "%03ds", p->time);
|
|
296 |
text_set_string(p->text, buffer);
|
|
298 | 297 |
} |
299 | 298 |
|
300 | 299 |
void (timer_reset)(text_timer_t *p){ |
301 |
p->time = 0; |
|
302 |
p->array[2] = '0'; |
|
303 |
p->array[1] = '0'; |
|
304 |
p->array[0] = '0'; |
|
300 |
text_set_string(p->text, "000s"); |
|
305 | 301 |
} |
306 | 302 |
|
307 | 303 |
void (timer_dtor)(text_timer_t *p){ |
Also available in: Unified diff