C++ Bitmap out of bounds error

Hello denziens of the fine website cplusplus.com. I recently started using allegro and bitmaps in my projects and I ran into a problem. This may be a rediculously simple error on my part and I apologize if it is trivial and I would be extremely grateful of any help offered.
This is my main:

int main() {
init();
int x = 0;
int y = 0;
BITMAP *background;

background = load_bitmap("Concept6.bmp", NULL);
while (!key[KEY_ESC])
{
stretch_blit(background, screen, x+1,y+1, 496,496,0,0,background->w*2,background ->h*2);
if(key[KEY_W])
{
if(y>1)
y--;
else
y++;
}
if(key[KEY_A])
{
if(x>1)
x--;
else
x++;
}
if(key[KEY_S])
{
if(y<493)
y++;
else
y--;
}
if(key[KEY_D])
{
if(x<493)
x++;
else
x--;
}

rest(1);
}

deinit();
return 0;
}
END_OF_MAIN()



This issue is that when I pan to the right, it starts to tile the background, and when I pan to the bottom, it crahes. My top and left sides work fine. The bitmap is 495x495
Last edited on
First things first, use code braces around code to make it more readable (either the <> symbol in the format section or type code and /code with square braces)
Secondly, this is an allegro specific problem, and is probably better suited to the allegro forums, but luckly for you, I think I know the source of your problem.
1:
You need to call
acquire_screen();
before and
release_screen();
after any drawing function.
Also, you need to make sure you actualy have the file "Concept6.bmp" in the folder, check for spelling errors too, if they aren't the same, it won't load. A simple way to check if it loaded is to check if background is null, as load_bitmap returns null if it fails.

To solve the tileing issue, you should call clear_to_color( background, makecol(0, 0, 0));to clear the background before drawing to it.

I've found this site to be quite useful in explaining the basics of allegro, you should run through its tutorials before you try experimenting on your own:
http://www.cppgameprogramming.com/cgi/nav.cgi?page=index

I left a link on this thread:
http://www.cplusplus.com/forum/beginner/60813/
for anyone who wants to download some of the stuff I've made - make sure you read what I wrote though, as the games were done in the past, while I still had quite a few bad programming practices.
Last edited on
Topic archived. No new replies allowed.