Hi all,
I am new to game programming for my own interest. I just tried to use allegro to draw something according to a tutorial online. But I have a little problem, below is my code:

#include<allegro.h>
int main()
{
	int x = 10;
	int y = 10;


	allegro_init();
	set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
	install_keyboard();
	
	while(!key[KEY_ESC])
	{
		clear_keybuf();
		acquire_screen();
		textout_ex(screen, font, " ", x, y, makecol(0,0,0), makecol(0,0,0));

		if(key[KEY_UP])
			--y;
		else if(key[KEY_DOWN])
			++y;
		else if(key[KEY_LEFT])
			--x;
		else if(key[KEY_RIGHT])
			++x;
		textout_ex(screen, font, "LLLLL", x, y, makecol(255,0,0), makecol(0,0,0));

		release_screen();

		rest(10);

	}



	return 0;
}
END_OF_MAIN();

This program will draw some text on the screen, and when we press any of the arrow keys, the position of the text will be adjust. But in this test program, the letter "LLLLL" will leave some vertical line on the screen when I press down, and the vertical lines will disappear accordingly when I press up. What is the problem???

Dani AI

Generated

As hinted, the symptom is caused by not actually erasing the previous text before drawing the next frame. Drawing a single-space (or failing to clear the whole area) only removes a tiny slice of pixels; the rest of the old characters remain on the screen and become visible as vertical trails when you move the text.

Three reliable fixes (ordered from simplest to most robust):

  • Clear the whole frame each loop. Wiping the screen to your background color at the start of every frame guarantees there’s nothing left behind before you draw the updated text.
  • Erase exactly the previous text rectangle. Before changing the coordinates, draw a filled rectangle or redraw the same string in the background color at the old x,y so the entire text bounding box is cleared (don’t clear only one character). Use the font metrics to compute width/height so you erase the full area.
  • Use double buffering (recommended). Draw to an off-screen bitmap every frame, then blit the completed bitmap to the screen in one operation. That both removes trails and avoids tearing from per-primitive writes to video memory.

Example (double-buffer pattern):

BITMAP *buf = create_bitmap(640,480);

/* inside the game loop: */
clear_to_color(buf, makecol(0,0,0));         /* clear frame */
textout_ex(buf, font, "LLLLL", x, y, makecol(255,0,0), -1);
acquire_screen();
blit(buf, screen, 0,0, 0,0, 640,480);        /* swap finished frame */
release_screen();

destroy_bitmap(buf); /* when done */

Notes: if you switch Visual Studio versions you may need Allegro libraries built for that compiler (or rebuild Allegro from source). Double-buffering also makes it easier to add a background image or sprites later without artifact workarounds.

Recommended Answers

All 4 Replies

Wow I haven't done allegro in a long time...
I think the problem may be that you need to clear the screen before you re-draw the text.

Wow I haven't done allegro in a long time...
I think the problem may be that you need to clear the screen before you re-draw the text.

dude, does allegro old? What tools/api you use to make game?

I'd say allegro is fairly old. Doesn't mean it's not any good. I started out with it and it certainly helped me. I use PyGame and OpenGL with Python nowadays.
Did I solve your problem?

Dunno...after I uninstalled the configured vs2010 and install vs2008 instead,
I cannot make use of allegro anymore because of wrong configuration!

gave up. Now I am able to use opengl with glut instead

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.