static void wiiu_font_render_line( video_frame_info_t *video_info, wiiu_font_t* font, const char* msg, unsigned msg_len, float scale, const unsigned int color, float pos_x, float pos_y, unsigned text_align) { unsigned i; wiiu_video_t* wiiu = (wiiu_video_t*)video_info->userdata; unsigned width = video_info->width; unsigned height = video_info->height; int x = roundf(pos_x * width); int y = roundf((1.0 - pos_y) * height); if( !wiiu || wiiu->vertex_cache.current + (msg_len * 4) > wiiu->vertex_cache.size) return; switch (text_align) { case TEXT_ALIGN_RIGHT: x -= wiiu_font_get_message_width(font, msg, msg_len, scale); break; case TEXT_ALIGN_CENTER: x -= wiiu_font_get_message_width(font, msg, msg_len, scale) / 2; break; } sprite_vertex_t* v = wiiu->vertex_cache.v + wiiu->vertex_cache.current; for (i = 0; i < msg_len; i++) { const char* msg_tmp = &msg[i]; unsigned code = utf8_walk(&msg_tmp); unsigned skip = msg_tmp - &msg[i]; if (skip > 1) i += skip - 1; const struct font_glyph* glyph = font->font_driver->get_glyph(font->font_data, code); if (!glyph) /* Do something smarter here ... */ glyph = font->font_driver->get_glyph(font->font_data, '?'); if (!glyph) continue; v->pos.x = x + glyph->draw_offset_x * scale; v->pos.y = y + glyph->draw_offset_y * scale; v->pos.width = glyph->width * scale; v->pos.height = glyph->height * scale; v->coord.u = glyph->atlas_offset_x; v->coord.v = glyph->atlas_offset_y; v->coord.width = glyph->width; v->coord.height = glyph->height; v->color = color; v++; x += glyph->advance_x * scale; y += glyph->advance_y * scale; } int count = v - wiiu->vertex_cache.v - wiiu->vertex_cache.current; if (!count) return; GX2Invalidate(GX2_INVALIDATE_MODE_CPU_ATTRIBUTE_BUFFER, wiiu->vertex_cache.v + wiiu->vertex_cache.current, count * sizeof(wiiu->vertex_cache.v)); if(font->atlas->dirty) { for (i = 0; (i < font->atlas->height) && (i < font->texture.surface.height); i++) memcpy(font->texture.surface.image + (i * font->texture.surface.pitch), font->atlas->buffer + (i * font->atlas->width), font->atlas->width); GX2Invalidate(GX2_INVALIDATE_MODE_CPU_TEXTURE, font->texture.surface.image, font->texture.surface.imageSize); font->atlas->dirty = false; } GX2SetPixelTexture(&font->texture, sprite_shader.ps.samplerVars[0].location); GX2SetVertexUniformBlock(sprite_shader.vs.uniformBlocks[1].offset, sprite_shader.vs.uniformBlocks[1].size, font->ubo_tex); GX2DrawEx(GX2_PRIMITIVE_MODE_POINTS, count, wiiu->vertex_cache.current, 1); GX2SetVertexUniformBlock(sprite_shader.vs.uniformBlocks[1].offset, sprite_shader.vs.uniformBlocks[1].size, wiiu->ubo_tex); wiiu->vertex_cache.current = v - wiiu->vertex_cache.v; }
static void wiiu_font_render_line( video_frame_info_t *video_info, wiiu_font_t* font, const char* msg, unsigned msg_len, float scale, const unsigned int color, float pos_x, float pos_y, unsigned text_align) { unsigned i; wiiu_video_t* wiiu = (wiiu_video_t*)video_driver_get_ptr(false); unsigned width = video_info->width; unsigned height = video_info->height; int x = roundf(pos_x * width); int y = roundf((1.0f - pos_y) * height); int delta_x = 0; int delta_y = 0; if(wiiu->vertex_cache.current + (msg_len * 4) > wiiu->vertex_cache.size) return; switch (text_align) { case TEXT_ALIGN_RIGHT: x -= wiiu_font_get_message_width(font, msg, msg_len, scale); break; case TEXT_ALIGN_CENTER: x -= wiiu_font_get_message_width(font, msg, msg_len, scale) / 2; break; } position_t* pos = wiiu->vertex_cache.positions + wiiu->vertex_cache.current; tex_coord_t* coord = wiiu->vertex_cache.tex_coords + wiiu->vertex_cache.current; for (i = 0; i < msg_len; i++) { int off_x, off_y, tex_x, tex_y, width, height; const char* msg_tmp = &msg[i]; unsigned code = utf8_walk(&msg_tmp); unsigned skip = msg_tmp - &msg[i]; if (skip > 1) i += skip - 1; const struct font_glyph* glyph = font->font_driver->get_glyph(font->font_data, code); if (!glyph) /* Do something smarter here ... */ glyph = font->font_driver->get_glyph(font->font_data, '?'); if (!glyph) continue; off_x = glyph->draw_offset_x; off_y = glyph->draw_offset_y; tex_x = glyph->atlas_offset_x; tex_y = glyph->atlas_offset_y; width = glyph->width; height = glyph->height; float x0 = x + off_x + delta_x * scale; float y0 = y + off_y + delta_y * scale + height * scale; float u0 = tex_x; float v0 = tex_y; float x1 = x0 + width * scale; float y1 = y0 - height * scale; float u1 = u0 + width; float v1 = v0 + height; pos[0].x = (2.0f * x0 / wiiu->color_buffer.surface.width) - 1.0f; pos[0].y = (-2.0f * y0 / wiiu->color_buffer.surface.height) + 1.0f; pos[1].x = (2.0f * x1 / wiiu->color_buffer.surface.width) - 1.0f;; pos[1].y = (-2.0f * y0 / wiiu->color_buffer.surface.height) + 1.0f; pos[2].x = (2.0f * x1 / wiiu->color_buffer.surface.width) - 1.0f;; pos[2].y = (-2.0f * y1 / wiiu->color_buffer.surface.height) + 1.0f; pos[3].x = (2.0f * x0 / wiiu->color_buffer.surface.width) - 1.0f;; pos[3].y = (-2.0f * y1 / wiiu->color_buffer.surface.height) + 1.0f; pos += 4; coord[0].u = u0 / font->texture.surface.width; coord[0].v = v1 / font->texture.surface.height; coord[1].u = u1 / font->texture.surface.width; coord[1].v = v1 / font->texture.surface.height; coord[2].u = u1 / font->texture.surface.width; coord[2].v = v0 / font->texture.surface.height; coord[3].u = u0 / font->texture.surface.width; coord[3].v = v0 / font->texture.surface.height; coord += 4; delta_x += glyph->advance_x; delta_y += glyph->advance_y; } int count = pos - wiiu->vertex_cache.positions - wiiu->vertex_cache.current; if (!count) return; GX2Invalidate(GX2_INVALIDATE_MODE_CPU_ATTRIBUTE_BUFFER, wiiu->vertex_cache.positions + wiiu->vertex_cache.current, count * sizeof(position_t)); GX2Invalidate(GX2_INVALIDATE_MODE_CPU_ATTRIBUTE_BUFFER, wiiu->vertex_cache.tex_coords + wiiu->vertex_cache.current, count * sizeof(tex_coord_t)); if(font->atlas->dirty) { for (i = 0; (i < font->atlas->height) && (i < font->texture.surface.height); i++) memcpy(font->texture.surface.image + (i * font->texture.surface.pitch), font->atlas->buffer + (i * font->atlas->width), font->atlas->width); GX2Invalidate(GX2_INVALIDATE_MODE_CPU_TEXTURE, font->texture.surface.image, font->texture.surface.imageSize); font->atlas->dirty = false; } #if 0 printf("%s\n", msg); DEBUG_VAR(color); #endif GX2SetPixelTexture(&font->texture, wiiu->shader->sampler.location); GX2SetBlendConstantColor(((color >> 0) & 0xFF) / 255.0f, ((color >> 8) & 0xFF) / 255.0f, ((color >> 16) & 0xFF) / 255.0f, ((color >> 24) & 0xFF) / 255.0f); GX2SetBlendControl(GX2_RENDER_TARGET_0, GX2_BLEND_MODE_BLEND_FACTOR, GX2_BLEND_MODE_INV_SRC_ALPHA, GX2_BLEND_COMBINE_MODE_ADD, GX2_ENABLE, GX2_BLEND_MODE_SRC_ALPHA, GX2_BLEND_MODE_INV_SRC_ALPHA, GX2_BLEND_COMBINE_MODE_ADD); GX2DrawEx(GX2_PRIMITIVE_MODE_QUADS, count, wiiu->vertex_cache.current, 1); GX2SetBlendControl(GX2_RENDER_TARGET_0, GX2_BLEND_MODE_SRC_ALPHA, GX2_BLEND_MODE_INV_SRC_ALPHA, GX2_BLEND_COMBINE_MODE_ADD, GX2_ENABLE, GX2_BLEND_MODE_SRC_ALPHA, GX2_BLEND_MODE_INV_SRC_ALPHA, GX2_BLEND_COMBINE_MODE_ADD); wiiu->vertex_cache.current = pos - wiiu->vertex_cache.positions; }