Bogus characters - SDL and C++

This topic is 4376 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Ok, I am working on some bitmap fonts. I have a little engine set up, but I have a problem. I have this system of "renderers", where every "renderer" has it's own "space" to render into. This can be the whole screen or only part of it. These "renderers" render a surface like this:
void video::renderer::render(pointer<SDL_Surface> raw, int x, int y, pointer<SDL_Rect> s) {
pointer<SDL_Surface> screen = SDL_GetVideoSurface();

if (!raw || !screen)
throw rendering_exception("Invalid image/animation passed to render or screen is invalid.");

SDL_Rect src;
if (s)
src = *s;
else {
src.x = 0;
src.y = 0;
src.w = raw->w;
src.h = raw->h;
}

if (x < 0) {
if (x * -1 > src.w)
return;
src.x += x * -1;
x = 0;
}

if (y < 0) {
if (y * -1 > src.h)
return;
src.y += y * -1;
y = 0;
}

if (m_viewport.w - x < src.w)
src.w = m_viewport.w - x;
if (m_viewport.h - y < src.h)
src.h = m_viewport.h - y;

x += m_viewport.x;
y += m_viewport.y;

SDL_Rect dst = {x, y, x + src.w, y + src.h};
SDL_BlitSurface(raw, &src, screen, &dst);
}


Basically I wanted to "cut" off the part that the renderers "cannot see". This works well and all for bitmaps (and animations), tests passed with flying colors. But not I am having a problem. I set up a "renderer" for the whole screen to draw me some scrolling text. Works well and all until it gets to the top and outputs some bogus: // Ok, begins great // Ok, a little scrolling done... // HuH? Bogus alert! Bogus Alert! What I don't understand is why this happens. The bitmap font is a simple 16x16-layed-out-in-ascii-order bitmapped font. I'm sure I get all the characters correct, because without scrolling it's great! Here is the code that I use to render it:
#include "Benefit/Benefit_Video.h"
#include "Benefit/Benefit_Input.h"
#include "Benefit/Benefit_Timer.h"
#include "Benefit/Benefit_Image.h"
#include "Benefit/Benefit_Animation.h"
#include "Benefit/Benefit_BitmapFont.h"

#include <SDL/SDL.h>

#include <string>

int main(int argc, char **argv) {
benefit::video v;

// her tr is the "renderer" - the whole screen
benefit::video::renderer r, br(0, 0, 640, 480), tr(0, 0, 640, 480/* / 4*/);
v.initialize_window(640, 480, "Alpha Animation Demo"); // initialize

// snip

// snip

benefit::bitmap_font font("Font.bmp", 0, 0, 0);
font.s() = "";
std::string &s = font.s();

s += "\nHello, world!\n";
s += "\tHello, and my name is Agi.\n";
s += "I have programmed this text thingy\n";
s += "with the help of Lazy_Foo's tutorials.\n";
s += "\tThe numbers of the 10th base are:\n";
s += "1, 2, 3, 4, 5, 6, 7, 8, and then 9.\n";
s += "\n\nThis is quite interesting!\n";
s += "\nThis font is a font found on #GameDev\n";
s += "and it is it's property.  This is only for\n";
s += "testing purposes only.\n";
s += "\n\n\t\t~Agi Shi.";

//snip
int fontx = 0;
int fonty = 480;

while (true) {
// snip

// snip
tr.clear(background, true);
// snip

tr.render(font, fontx, fonty);
if (tt.get_ticks() > 10) {
tt.set_delta();

if (fonty-- < 0 - font.sheet().raw_image()->h / 16 * 16)
fonty = 480;
}

// snip
tr.update();
// snip
//benefit::video::renderer::global_update();
while (t.get_ticks() < 1000 / 60)
/* 60 fps */;
}

return 0;
}


I *seem* to have isolated it (the bug) to this code (without it it works):
if (x < 0) {
if (x * -1 > src.w)
return;
src.x += x * -1;
x = 0;
}

if (y < 0) {
if (y * -1 > src.h)
return;
src.y += y * -1;
y = 0;
}

But I'm not sure... (It's from the above function.) But - without this code my renderer will render places it shouldn't! Any ideas? Thank you for your time!

Share on other sites
You are setting y to zero:

if (y < 0)
{
if (y * -1 > src.h)
return;

src.y += y * -1;

y = 0; // WHAT THIS ALL ABOOT!
}

Share on other sites
What DanDanger said. It looks like you have several lines of text being rendered over each other.

Share on other sites
Setting y to 0 is expected here, as you still want to render at the very top of the screen. The only thing I see missing is that you're not changing the height you want to render, which will obviously be less than the full surface height.

Share on other sites
Quote:
 Original post by Beer HunterWhat DanDanger said. It looks like you have several lines of text being rendered over each other.

No no no guys, [even though I just tried to make sure] it's not that. Basically I don't want anything to be renderer whole if the renderer can't "see" it whole.

So if you pass -5 for the x, you won't see 5 pixels of the left side of the picture, and the picture part that you WILL see is at 0. (Same for the y.) So you add the abs() of the amount into the src.x and do the same for the y if it is negative too...

Any ideas???

@AP:
if (m_viewport.w - x < src.w)            src.w = m_viewport.w - x;        if (m_viewport.h - y < src.h)            src.h = m_viewport.h - y;

Won't this change the height if you can't see all of it?

Share on other sites
To me it looks like your rendering characters from the upper 128 bytes of the font on top of already printed text or the other way around.

Although the relevent code isn't displayed, I would imagine it is a problem with displaying text that shouldn't be displayed. Having done bitmap fonts in a similar way in the past I would suggest that it is an offset problem somewhere.

Suggestion: Check your font drawing code. Does it skip characters that aren't on the screen or does it attempt to draw them anyway? At this point it would be good to walk through with the debugger if you know how or the old stand by is to throw in a printf line (console project?) around there to see what characters are being drawn and if they even should be.

On a side note, I don't know what context the code you are having the problem with is being used in. It could be correct in some form I understand but I think like previously stated the x=0 and y=0 are out of place and should probably be done somewhere else.

Share on other sites
Gues, I messed up a windows RECT (left, right, top, bottom) with a SDL_Rect (x, y, w, h) so as I increased x/y the height stayed the same so it drew bogus (the characters at the x/y position of the incrememented stuff...)

• 10
• 17
• 9
• 13
• 41