68 static void asc_text_update(AscSceneNode *node) { |
68 static void asc_text_update(AscSceneNode *node) { |
69 asc_ptr_cast(AscText, text, node); |
69 asc_ptr_cast(AscText, text, node); |
70 |
70 |
71 // Render text onto a surface |
71 // Render text onto a surface |
72 TTF_Font *font = asc_font_load(text->font); |
72 TTF_Font *font = asc_font_load(text->font); |
73 if (font == NULL) return; |
73 if (font == NULL) { |
|
74 // cannot load font - hide the text node to avoid errors when trying to draw |
|
75 asc_set_flag(node->flags, ASC_SCENE_NODE_HIDDEN); |
|
76 return; |
|
77 } |
74 static int alignments[] = { |
78 static int alignments[] = { |
75 TTF_WRAPPED_ALIGN_LEFT, |
79 TTF_WRAPPED_ALIGN_LEFT, |
76 TTF_WRAPPED_ALIGN_CENTER, |
80 TTF_WRAPPED_ALIGN_CENTER, |
77 TTF_WRAPPED_ALIGN_RIGHT |
81 TTF_WRAPPED_ALIGN_RIGHT |
78 }; |
82 }; |
139 cx_strfree(&text->text); |
143 cx_strfree(&text->text); |
140 } |
144 } |
141 |
145 |
142 AscSceneNode *asc_text_create(struct asc_text_create_args args) { |
146 AscSceneNode *asc_text_create(struct asc_text_create_args args) { |
143 AscText *text = cxZallocDefault(sizeof(AscText)); |
147 AscText *text = cxZallocDefault(sizeof(AscText)); |
144 AscSceneNode *node = &text->base; |
148 asc_ptr_cast(AscSceneNode, node, text); |
145 |
149 |
146 // node properties |
150 // node properties |
147 asc_scene_node_name(node, args.name); |
151 asc_scene_node_init(node, |
148 node->render_group = ASC_RENDER_GROUP_2D_BLEND; |
152 ASC_SCENE_NODE_FUNCS(asc_text), |
149 node->destroy_func = asc_text_destroy; |
153 .name = args.name, |
150 node->update_func = asc_text_update; |
154 .render_group = ASC_RENDER_GROUP_2D_BLEND, |
151 node->draw_func = asc_text_draw; |
155 .pos2d = ASC_VEC2I(args.x, args.y) |
152 node->position = ASC_VEC3F(args.x, args.y, ASC_SCENE_2D_DEPTH_OFFSET); |
156 ); |
153 node->scale = ASC_VEC3F_1; |
|
154 asc_mat4f_unit(node->rotation); |
|
155 |
157 |
156 // text properties |
158 // text properties |
157 node->flags = args.alignment; // use flags variable to save some space |
159 node->flags = args.alignment; // use flags variable to save some space |
158 text->max_width = args.max_width; |
160 text->max_width = args.max_width; |
159 text->font = asc_active_font; |
161 text->font = asc_active_font; |
169 |
171 |
170 // initialize texture |
172 // initialize texture |
171 // mesh will be created in the update func |
173 // mesh will be created in the update func |
172 text->texture = cxMallocDefault(sizeof(AscTexture)); |
174 text->texture = cxMallocDefault(sizeof(AscTexture)); |
173 asc_texture_init_rectangle(text->texture, 1); |
175 asc_texture_init_rectangle(text->texture, 1); |
|
176 // TODO: check why we can't just wait for the first normal update to happen |
174 asc_text_update(node); |
177 asc_text_update(node); |
175 |
178 |
176 return node; |
179 return node; |
177 } |
180 } |
178 |
181 |