Help splitting string without STL

I have two functions below, one that renders a string at the give X,Y coordinate and one that attempts to plot the coordinates of each word in a string so that the string wraps inside a bounding rectangle.

The first function is the one I have trouble with. Right now it works somewhat but each new line is beginning with a space. If I uncomment line 37 it fixes it but then adds an extra space after the first word of each new line.

I know this should be very simple but I keep rewriting it and even when it looks like it should work exactly how I want I still end up with unwanted spaces. So what am I doing wrong here?

The GetTextExtentPoint32() function returns the width and height of the string(2nd parameter) as size.cx and size.cy. The 3rd parameter is the number of characters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
VOID WriteText(LPCWSTR text, RECT extent)
{
	SIZE size = {0};
	TEXTMETRIC tm = {0};

	HDC hdc = wglGetCurrentDC();

	GetTextMetrics(hdc, &tm);

	int cx = extent.left;
	int cy = extent.top;

	size_t len = wcslen(text);
	size_t start = 0;
	size_t i = 0;

	for (; i < len; i++)
	{
		if (L' ' == *(text+i))
		{
			// get width & height of next word
			GetTextExtentPoint32(hdc, text + start, i - start, &size);

			if (size.cx > (extent.right - extent.left))
				return;

			// new line
			if ((cx + size.cx) > extent.right)
			{
				cx = extent.left;
				cy += tm.tmHeight;

				if ((cy + tm.tmHeight) > extent.bottom)
					return;

				//start += 1;
			}

			WriteText(text + start, cx, cy, i - start);
			start = i;
			cx += size.cx;
		}
	}

	WriteText(text + start, cx, cy, i - start);
}


VOID WriteText(LPCWSTR text, INT x, INT y, INT len)
{
	if (0 == FontBase || NULL == text)
		return;

	glDisable(GL_TEXTURE_2D);

	glColor4f(FontColor[0], FontColor[1], FontColor[2], FontColor[3]);

	GLfloat pos[] = { (float)x, (float)y, 1.0f };
	glRasterPos3fv(pos);

	glPushAttrib(GL_LIST_BIT);
		glListBase(FontBase - 32);
		glCallLists(len, GL_UNSIGNED_SHORT, text);
	glPopAttrib();
}
Topic archived. No new replies allowed.