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!