[Scummvm-cvs-logs] SF.net SVN: scummvm:[40178] scummvm/trunk/engines/sci/gfx

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Tue Apr 28 14:33:00 CEST 2009


Revision: 40178
          http://scummvm.svn.sourceforge.net/scummvm/?rev=40178&view=rev
Author:   fingolfin
Date:     2009-04-28 12:32:59 +0000 (Tue, 28 Apr 2009)

Log Message:
-----------
SCI: Renamed text_fragment_t -> TextFragment and changed TextHandle to store the text fragments and pixmaps in Common::Arrays

Modified Paths:
--------------
    scummvm/trunk/engines/sci/gfx/font.cpp
    scummvm/trunk/engines/sci/gfx/font.h
    scummvm/trunk/engines/sci/gfx/operations.cpp
    scummvm/trunk/engines/sci/gfx/operations.h

Modified: scummvm/trunk/engines/sci/gfx/font.cpp
===================================================================
--- scummvm/trunk/engines/sci/gfx/font.cpp	2009-04-28 12:29:44 UTC (rev 40177)
+++ scummvm/trunk/engines/sci/gfx/font.cpp	2009-04-28 12:32:59 UTC (rev 40178)
@@ -42,34 +42,25 @@
 	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.push_back(TextFragment(text));
 
-	fragments[0].offset = text;
-
 	while ((foo = *text++)) {
 		if (foo >= font->chars_nr) {
 			GFXWARN("Invalid char 0x%02x (max. 0x%02x) encountered in text string '%s', font %04x\n",
@@ -77,13 +68,12 @@
 			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 @@
 			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,12 +113,9 @@
 				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;
+				fragments.back().length = text - blank_break - fragments.back().offset;
+				fragments.push_back(TextFragment(text));
 
-				if (current_fragment == fragments_nr)
-					fragments = (text_fragment_t*)sci_realloc(fragments, sizeof(text_fragment_t *) * (fragments_nr <<= 1));
-
 				localmaxwidth = localmaxwidth - last_breakpoint;
 				if (!(flags & kFontCountWhitespace))
 					localmaxwidth -= last_break_width;
@@ -156,12 +140,10 @@
 
 	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) {

Modified: scummvm/trunk/engines/sci/gfx/font.h
===================================================================
--- scummvm/trunk/engines/sci/gfx/font.h	2009-04-28 12:29:44 UTC (rev 40177)
+++ scummvm/trunk/engines/sci/gfx/font.h	2009-04-28 12:32:59 UTC (rev 40178)
@@ -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 @@
 ** 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 @@
 **                               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.

Modified: scummvm/trunk/engines/sci/gfx/operations.cpp
===================================================================
--- scummvm/trunk/engines/sci/gfx/operations.cpp	2009-04-28 12:29:44 UTC (rev 40177)
+++ scummvm/trunk/engines/sci/gfx/operations.cpp	2009-04-28 12:32:59 UTC (rev 40178)
@@ -1875,7 +1875,8 @@
 
 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 @@
 	}
 
 #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 @@
 		return GFX_ERROR;
 	}
 
-	free(textsplits);
+	if (lines_nr)
+		*lines_nr = fragments.size();
 
 	return GFX_OK;
 }
@@ -1910,7 +1912,7 @@
 								  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 @@
 	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 @@
 		                          (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 @@
 }
 
 TextHandle::TextHandle() {
-	lines_nr = 0;
 	line_height = 0;
-	lines = 0;
 	font = 0;
-	text_pixmaps = 0;
 
 	width = height = 0;
 
@@ -2003,20 +2003,14 @@
 }
 
 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 @@
 		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 @@
 		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 @@
 		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 @@
 			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;
 		}
 

Modified: scummvm/trunk/engines/sci/gfx/operations.h
===================================================================
--- scummvm/trunk/engines/sci/gfx/operations.h	2009-04-28 12:29:44 UTC (rev 40177)
+++ scummvm/trunk/engines/sci/gfx/operations.h	2009-04-28 12:32:59 UTC (rev 40178)
@@ -38,7 +38,7 @@
 
 namespace Sci {
 
-struct text_fragment_t;
+struct TextFragment;
 
 #define GFXOP_NO_POINTER -1
 
@@ -50,11 +50,10 @@
 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;
 


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list