src/text.c

changeset 116
bfb2a7d62047
parent 115
e5f8c99b0987
equal deleted inserted replaced
115:e5f8c99b0987 116:bfb2a7d62047
52 ); 52 );
53 if (surface == NULL) { 53 if (surface == NULL) {
54 asc_error("Rendering TTF surface failed: %s", SDL_GetError()); 54 asc_error("Rendering TTF surface failed: %s", SDL_GetError());
55 return; 55 return;
56 } 56 }
57 // TODO: don't use scale - create an own fitting mesh
58 asc_set_scale(node, (float)surface->w, (float)surface->h, 1.f);
59 if (asc_test_flag(text->base.data.flags, ASC_TEXT_CENTERED_FLAG)) { 57 if (asc_test_flag(text->base.data.flags, ASC_TEXT_CENTERED_FLAG)) {
60 unsigned short newoffx = surface->w / 2; 58 unsigned short newoffx = surface->w / 2;
61 asc_vec2i pos = asc_get_position2d(node); 59 node->position.x = node->position.x + (float)(text->offx - newoffx);
62 asc_set_position2d(node, pos.x + text->offx - newoffx, pos.y);
63 text->offx = newoffx; 60 text->offx = newoffx;
61 }
62
63 // If dimensions changed, update the mesh
64 if (text->dimension.x != (unsigned)surface->w || text->dimension.y != (unsigned)surface->h) {
65 text->dimension.x = surface->w;
66 text->dimension.y = surface->h;
67 asc_mesh_plane_2d(&text->base.mesh, .size = asc_vec2f_new(surface->w, surface->h));
64 } 68 }
65 69
66 // Transfer Image Data 70 // Transfer Image Data
67 asc_texture_from_surface(sprite->texture, surface); 71 asc_texture_from_surface(sprite->texture, surface);
68 72
69 // Free the surface 73 // Free the surface
70 SDL_FreeSurface(surface); 74 SDL_FreeSurface(surface);
75
76 // Schedule for transform update
77 asc_node_update_transform(node);
71 } 78 }
72 79
73 static void asc_text_destroy(AscSceneNode *node) { 80 static void asc_text_destroy(AscSceneNode *node) {
74 AscText *text = (AscText*) node; 81 AscText *text = (AscText*) node;
75 AscSprite *sprite = (AscSprite*) node; 82 AscSprite *sprite = (AscSprite*) node;
88 asc_scene_node_name(node, args.name); 95 asc_scene_node_name(node, args.name);
89 node->render_group = ASC_RENDER_GROUP_SPRITE_BLEND; 96 node->render_group = ASC_RENDER_GROUP_SPRITE_BLEND;
90 node->destroy_func = asc_text_destroy; 97 node->destroy_func = asc_text_destroy;
91 node->update_func = asc_text_update; 98 node->update_func = asc_text_update;
92 node->position = asc_vec3f_new(args.x, args.y, ASC_SCENE_2D_DEPTH_OFFSET); 99 node->position = asc_vec3f_new(args.x, args.y, ASC_SCENE_2D_DEPTH_OFFSET);
93 node->scale.depth = 1.f; 100 node->scale = asc_vec3f_one;
94 101
95 // text properties 102 // text properties
96 node->flags = args.alignment; // use flags variable to save some space 103 node->flags = args.alignment; // use flags variable to save some space
97 text->max_width = args.max_width; 104 text->max_width = args.max_width;
98 text->font = asc_active_font; 105 text->font = asc_active_font;
101 text->text = cx_mutstr(strdup(" ")); 108 text->text = cx_mutstr(strdup(" "));
102 } else { 109 } else {
103 text->text = cx_mutstr(strdup(args.text)); 110 text->text = cx_mutstr(strdup(args.text));
104 } 111 }
105 112
106 // initialize mesh and texture 113 // initialize texture
107 asc_mesh_plane_2d(&text->base.mesh); 114 // mesh will be created in the update func
108 text->base.texture = malloc(sizeof(AscTexture)); 115 text->base.texture = malloc(sizeof(AscTexture));
109 asc_texture_init_rectangle(text->base.texture, 1); 116 asc_texture_init_rectangle(text->base.texture, 1);
110 asc_text_update(node); 117 asc_text_update(node);
111 118
112 return node; 119 return node;

mercurial