diff options
author | Max Horn | 2009-04-28 12:32:59 +0000 |
---|---|---|
committer | Max Horn | 2009-04-28 12:32:59 +0000 |
commit | e6a7b8ef343f7e4fd2637500afe800794fe03a78 (patch) | |
tree | dbecdd69cdd7c5a391f9027f83c27bb978f01d44 | |
parent | 9108871833313f88b2f10aa5c5998fa3fb23e008 (diff) | |
download | scummvm-rg350-e6a7b8ef343f7e4fd2637500afe800794fe03a78.tar.gz scummvm-rg350-e6a7b8ef343f7e4fd2637500afe800794fe03a78.tar.bz2 scummvm-rg350-e6a7b8ef343f7e4fd2637500afe800794fe03a78.zip |
SCI: Renamed text_fragment_t -> TextFragment and changed TextHandle to store the text fragments and pixmaps in Common::Arrays
svn-id: r40178
-rw-r--r-- | engines/sci/gfx/font.cpp | 38 | ||||
-rw-r--r-- | engines/sci/gfx/font.h | 11 | ||||
-rw-r--r-- | engines/sci/gfx/operations.cpp | 54 | ||||
-rw-r--r-- | engines/sci/gfx/operations.h | 7 |
4 files changed, 44 insertions, 66 deletions
diff --git a/engines/sci/gfx/font.cpp b/engines/sci/gfx/font.cpp index a562219932..4b29aeef3d 100644 --- a/engines/sci/gfx/font.cpp +++ b/engines/sci/gfx/font.cpp @@ -42,33 +42,24 @@ void gfxr_free_font(gfx_bitmap_font_t *font) { free(font); } -text_fragment_t *gfxr_font_calculate_size(gfx_bitmap_font_t *font, int max_width, +bool gfxr_font_calculate_size(Common::Array<TextFragment> &fragments, gfx_bitmap_font_t *font, int max_width, const char *text, int *width, int *height, - int *lines, int *line_height_p, int *last_offset_p, int flags) { + int *line_height_p, int *last_offset_p, int flags) { - int est_char_width = font->widths[(font->chars_nr > 'M')? 'M' : font->chars_nr - 1]; - // 'M' is typically among the widest chars - int fragments_nr; - text_fragment_t *fragments; int lineheight = font->line_height; int maxheight = lineheight; int last_breakpoint = 0; int last_break_width = 0; int max_allowed_width = max_width; int maxwidth = 0, localmaxwidth = 0; - int current_fragment = 1; const char *breakpoint_ptr = NULL; unsigned char foo; if (line_height_p) *line_height_p = lineheight; - if (max_width > 1) fragments_nr = 3 + (strlen(text) * est_char_width) * 3 / (max_width << 1); - else fragments_nr = 1; - fragments = (text_fragment_t *)sci_calloc(sizeof(text_fragment_t), fragments_nr); - - fragments[0].offset = text; + fragments.push_back(TextFragment(text)); while ((foo = *text++)) { if (foo >= font->chars_nr) { @@ -77,13 +68,12 @@ text_fragment_t *gfxr_font_calculate_size(gfx_bitmap_font_t *font, int max_width if (font->chars_nr > ' ') foo = ' '; else { - free(fragments); - return NULL; + return false; } } if (((foo == '\n') || (foo == 0x0d)) && !(flags & kFontNoNewlines)) { - fragments[current_fragment-1].length = text - 1 - fragments[current_fragment-1].offset; + fragments.back().length = text - 1 - fragments.back().offset; if (*text) maxheight += lineheight; @@ -91,14 +81,11 @@ text_fragment_t *gfxr_font_calculate_size(gfx_bitmap_font_t *font, int max_width if (foo == 0x0d && *text == '\n') text++; // Interpret DOS-style CR LF as single NL - fragments[current_fragment++].offset = text; + fragments.push_back(TextFragment(text)); if (localmaxwidth > maxwidth) maxwidth = localmaxwidth; - if (current_fragment == fragments_nr) - fragments = (text_fragment_t*)sci_realloc(fragments, sizeof(text_fragment_t) * (fragments_nr <<= 1)); - localmaxwidth = 0; } else { // foo != '\n' @@ -126,11 +113,8 @@ text_fragment_t *gfxr_font_calculate_size(gfx_bitmap_font_t *font, int max_width if (last_breakpoint > maxwidth) maxwidth = last_breakpoint; - fragments[current_fragment-1].length = text - blank_break - fragments[current_fragment-1].offset; - fragments[current_fragment++].offset = text; - - if (current_fragment == fragments_nr) - fragments = (text_fragment_t*)sci_realloc(fragments, sizeof(text_fragment_t *) * (fragments_nr <<= 1)); + fragments.back().length = text - blank_break - fragments.back().offset; + fragments.push_back(TextFragment(text)); localmaxwidth = localmaxwidth - last_breakpoint; if (!(flags & kFontCountWhitespace)) @@ -156,12 +140,10 @@ text_fragment_t *gfxr_font_calculate_size(gfx_bitmap_font_t *font, int max_width if (height) *height = maxheight; - if (lines) - *lines = current_fragment; - fragments[current_fragment-1].length = text - fragments[current_fragment-1].offset - 1; + fragments.back().length = text - fragments.back().offset - 1; - return fragments; + return true; } static void render_char(byte *dest, byte *src, int width, int line_width, int lines, int bytes_per_src_line, int fg0, int fg1, int bg) { diff --git a/engines/sci/gfx/font.h b/engines/sci/gfx/font.h index 8bd5f6b33a..935414f550 100644 --- a/engines/sci/gfx/font.h +++ b/engines/sci/gfx/font.h @@ -31,9 +31,12 @@ namespace Sci { -struct text_fragment_t { +struct TextFragment { const char *offset; int length; + + TextFragment() : offset(0), length(0) {} + TextFragment(const char *o) : offset(o), length(0) {} }; @@ -86,8 +89,9 @@ void gfxr_free_font(gfx_bitmap_font_t *font); ** Returns : (void) */ -text_fragment_t *gfxr_font_calculate_size(gfx_bitmap_font_t *font, int max_width, const char *text, - int *width, int *height, int *lines, int *line_height, int *last_offset, int flags); +bool gfxr_font_calculate_size(Common::Array<TextFragment> &fragments, + gfx_bitmap_font_t *font, int max_width, const char *text, + int *width, int *height, int *line_height, int *last_offset, int flags); /* Calculates the size that would be occupied by drawing a specified text ** Parameters: (gfx_bitmap_font_t *) font: The font to calculate with ** (int) max_width: Maximum pixel width allowed for the output @@ -98,7 +102,6 @@ text_fragment_t *gfxr_font_calculate_size(gfx_bitmap_font_t *font, int max_width ** segment ** (int) *width: The resulting width ** (int) *height: The resulting height -** (int) *lines: Number of lines used ** (int) *line_height: Pixel height of a single line of text ** (int) *last_offset: Pixel offset after the last drawn line ** This function assumes 320x200 mode. diff --git a/engines/sci/gfx/operations.cpp b/engines/sci/gfx/operations.cpp index 13e0e8755b..0559a677b3 100644 --- a/engines/sci/gfx/operations.cpp +++ b/engines/sci/gfx/operations.cpp @@ -1875,7 +1875,8 @@ int gfxop_get_font_height(GfxState *state, int font_nr) { int gfxop_get_text_params(GfxState *state, int font_nr, const char *text, int maxwidth, int *width, int *height, int text_flags, int *lines_nr, int *lineheight, int *lastline_width) { - text_fragment_t *textsplits; + Common::Array<TextFragment> fragments; + bool textsplits; gfx_bitmap_font_t *font; BASIC_CHECKS(GFX_FATAL); @@ -1889,10 +1890,10 @@ int gfxop_get_text_params(GfxState *state, int font_nr, const char *text, int ma } #ifdef CUSTOM_GRAPHICS_OPTIONS - textsplits = gfxr_font_calculate_size(font, maxwidth, text, width, height, lines_nr, lineheight, lastline_width, + textsplits = gfxr_font_calculate_size(fragments, font, maxwidth, text, width, height, lineheight, lastline_width, (state->options->workarounds & GFX_WORKAROUND_WHITESPACE_COUNT) | text_flags); #else - textsplits = gfxr_font_calculate_size(font, maxwidth, text, width, height, lines_nr, lineheight, lastline_width, text_flags); + textsplits = gfxr_font_calculate_size(fragments, font, maxwidth, text, width, height, lineheight, lastline_width, text_flags); #endif if (!textsplits) { @@ -1901,7 +1902,8 @@ int gfxop_get_text_params(GfxState *state, int font_nr, const char *text, int ma return GFX_ERROR; } - free(textsplits); + if (lines_nr) + *lines_nr = fragments.size(); return GFX_OK; } @@ -1910,7 +1912,7 @@ TextHandle *gfxop_new_text(GfxState *state, int font_nr, const Common::String &t gfx_alignment_t valign, gfx_color_t color1, gfx_color_t color2, gfx_color_t bg_color, int flags) { TextHandle *handle; gfx_bitmap_font_t *font; - int i, err = 0; + int err = 0; BASIC_CHECKS(NULL); // mapping text colors to palette @@ -1936,29 +1938,30 @@ TextHandle *gfxop_new_text(GfxState *state, int font_nr, const Common::String &t handle->valign = valign; handle->line_height = font->line_height; + bool result; #ifdef CUSTOM_GRAPHICS_OPTIONS - handle->lines = gfxr_font_calculate_size(font, maxwidth, handle->_text.c_str(), &(handle->width), &(handle->height), &(handle->lines_nr), + result = gfxr_font_calculate_size(handle->lines, font, maxwidth, handle->_text.c_str(), &(handle->width), &(handle->height), NULL, NULL, ((state->options->workarounds & GFX_WORKAROUND_WHITESPACE_COUNT) ? kFontCountWhitespace : 0) | flags); #else - handle->lines = gfxr_font_calculate_size(font, maxwidth, handle->_text.c_str(), &(handle->width), &(handle->height), &(handle->lines_nr), + result = gfxr_font_calculate_size(handle->lines, font, maxwidth, handle->_text.c_str(), &(handle->width), &(handle->height), NULL, NULL, flags); #endif - if (!handle->lines) { + if (!result) { GFXERROR("Could not calculate text parameters in font #%d\n", font_nr); delete handle; return NULL; } if (flags & kFontNoNewlines) { - handle->lines_nr = 1; - handle->lines->length = text.size(); + handle->lines.resize(1); + handle->lines[0].length = text.size(); } - handle->text_pixmaps = (gfx_pixmap_t **)calloc(sizeof(gfx_pixmap_t *), handle->lines_nr); + handle->text_pixmaps.resize(handle->lines.size()); - for (i = 0; i < handle->lines_nr; i++) { + for (uint i = 0; i < handle->lines.size(); i++) { int chars_nr = handle->lines[i].length; handle->text_pixmaps[i] = gfxr_draw_font(font, handle->lines[i].offset, chars_nr, @@ -1967,7 +1970,7 @@ TextHandle *gfxop_new_text(GfxState *state, int font_nr, const Common::String &t (bg_color.mask & GFX_MASK_VISUAL) ? &bg_color.visual : NULL); if (!handle->text_pixmaps[i]) { - GFXERROR("Failed to draw text pixmap for line %d/%d\n", i, handle->lines_nr); + GFXERROR("Failed to draw text pixmap for line %d/%d\n", i, handle->lines.size()); delete handle; return NULL; } @@ -1990,11 +1993,8 @@ int gfxop_free_text(GfxState *state, TextHandle *handle) { } TextHandle::TextHandle() { - lines_nr = 0; line_height = 0; - lines = 0; font = 0; - text_pixmaps = 0; width = height = 0; @@ -2003,20 +2003,14 @@ TextHandle::TextHandle() { } TextHandle::~TextHandle() { - if (text_pixmaps) { - for (int j = 0; j < lines_nr; j++) - if (text_pixmaps[j]) - gfx_free_pixmap(text_pixmaps[j]); - free(text_pixmaps); - } - - free(lines); + for (uint j = 0; j < text_pixmaps.size(); j++) + if (text_pixmaps[j]) + gfx_free_pixmap(text_pixmaps[j]); } int gfxop_draw_text(GfxState *state, TextHandle *handle, rect_t zone) { int line_height; rect_t pos; - int i; BASIC_CHECKS(GFX_FATAL); _gfxop_full_pointer_refresh(state); @@ -2025,7 +2019,7 @@ int gfxop_draw_text(GfxState *state, TextHandle *handle, rect_t zone) { return GFX_ERROR; } - if (!handle->lines_nr) { + if (handle->lines.empty()) { GFXDEBUG("Skipping draw_text operation because number of lines is zero\n"); return GFX_OK; } @@ -2042,11 +2036,11 @@ int gfxop_draw_text(GfxState *state, TextHandle *handle, rect_t zone) { break; case ALIGN_CENTER: - pos.y += (zone.height - (line_height * handle->lines_nr)) >> 1; + pos.y += (zone.height - (line_height * handle->lines.size())) >> 1; break; case ALIGN_BOTTOM: - pos.y += (zone.height - (line_height * handle->lines_nr)); + pos.y += (zone.height - (line_height * handle->lines.size())); break; default: @@ -2054,7 +2048,7 @@ int gfxop_draw_text(GfxState *state, TextHandle *handle, rect_t zone) { return GFX_FATAL; // Internal error... } - for (i = 0; i < handle->lines_nr; i++) { + for (uint i = 0; i < handle->lines.size(); i++) { gfx_pixmap_t *pxm = handle->text_pixmaps[i]; @@ -2067,7 +2061,7 @@ int gfxop_draw_text(GfxState *state, TextHandle *handle, rect_t zone) { gfxr_endianness_adjust(pxm, state->driver->mode); // FIXME: resmgr layer! } if (!pxm) { - GFXERROR("Could not find text pixmap %d/%d\n", i, handle->lines_nr); + GFXERROR("Could not find text pixmap %d/%d\n", i, handle->lines.size()); return GFX_ERROR; } diff --git a/engines/sci/gfx/operations.h b/engines/sci/gfx/operations.h index 47126324be..e3dae6a317 100644 --- a/engines/sci/gfx/operations.h +++ b/engines/sci/gfx/operations.h @@ -38,7 +38,7 @@ namespace Sci { -struct text_fragment_t; +struct TextFragment; #define GFXOP_NO_POINTER -1 @@ -50,11 +50,10 @@ struct text_fragment_t; struct TextHandle { Common::String _text; /**< Copy of the actual text */ - int lines_nr; int line_height; - text_fragment_t *lines; /**< Text offsets */ + Common::Array<TextFragment> lines; /**< Text offsets */ gfx_bitmap_font_t *font; - gfx_pixmap_t **text_pixmaps; + Common::Array<gfx_pixmap_t *> text_pixmaps; int width, height; |