Surfaces drawing in incorrect order

Hi, I’m a first time poster and new to the library. Please excuse me if I’m
posting to the wrong newsgroup.

I’m having a problem blitting two surfaces to the screen in the correct order.
The code in question is as follows:

void SDLTextBox::display() {
if(str.size() > 0) {
SDL_Surface *apply = 0;
string render;
int i = 1;
render = str;
int w = 0, j = 0, h = 0;
do {
do {
TTF_SizeText(font, render.substr(0, i++).c_str(), &w, &h);
} while(w <= 615 && i < (render.size()));
if(i > 0) {
j++;
apply_surface(0, (480 - (h * j + 5)), textBoxBody, screen);
apply = TTF_RenderText_Solid(font, render.substr(0, i).c_str(), textColor);
apply_surface(5, (480 - (apply->h * ((lines - j) + 1))), apply, screen);
}
render.erase(0, i);
i = 1;
SDL_FreeSurface(apply);
} while(i < render.size());
lines = j;
}
}

apply_surface(x, y, src, dst) is a self-explanatory function that calls
SDL_BlitSurface().

The software I’m writing uses this bit of code to draw the text and background
of a texbox to the screen. This works fine until the second line of text is
drawn and the first line becomes hidden behind the background image. Subsequent
lines also appear as normal. If I set the y value of the second call to
apply_surface() to (200 - …) all lines appear normally, though obviously about
300 pixels above where they should.

Any help would be appreciated.

Nicole