I've finally got my game scrolling sideways, but...

1) There's a really bad flicker to the whole screen.

2) Also, how do I get the background to loop?

3) The whole thing seems to run quite a bit slower than before I added the background. Why is that? How do I fix it?

Any help would be greatly appreciated!

Below is my code so far...

#include <stdio.h>
#include <allegro.h>

#define WIDTH 640
#define HEIGHT 480
#define MODE GFX_AUTODETECT_WINDOWED
#define WHITE makecol(255,255,255)
#define BLACK makecol(0,0,0)

BITMAP *kitty[7], *kittyb[7], *scene;
char s[20];
int curframe=0, framedelay=5, framecount=0, x_offset=0;
int x=0, y=340, volume=128, n, b;

int main(void) 
{
    BITMAP *buffer;
    BITMAP *bg;
    MIDI *music;
    //int pos, length;
    
	//initialize the program
    allegro_init();
    install_keyboard(); 
    install_timer();
	set_color_depth(16);
	set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
	textout_ex(screen, font, "Scrolling Game (ESC to quit)",
        0, 0, WHITE,0);
     
    //create the back buffer
	buffer = create_bitmap(WIDTH,HEIGHT);
    //load background
	bg = load_bitmap("background.bmp", NULL);
	if (!bg) {
		allegro_message("Error loading background image\n%s", allegro_error);
		return 1;
	} 
        
    //install the sound driver
    if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL) != 0) {
       allegro_message("Error initializing sound system\n%s\n", allegro_error);
       return 1;
    }
    //load the MIDI file
    music = load_midi("Head.mid");
    if (!music) {
       allegro_message("Error loading MIDI file!");
       return 1;
    }
    //play the music
    if (play_midi(music, 0) != 0) {
       allegro_message("Error playing MIDI\n%s", allegro_error);
       return 1;
    }

	//load the animated sprites
    for (n=0; n<6; n++)
    {
        sprintf(s,"cat%d.bmp",n+1);
        kitty[n] = load_bitmap(s, NULL);
    }
    for (n=0; n<6; n++)
    {
        sprintf(s,"cat%db.bmp",n+1);
        kittyb[n] = load_bitmap(s, NULL);
    }
    
    /* Create a scene buffer to draw into. */
    scene = create_bitmap(screen->w, screen->h);
    
    /***************************************************** -THE GAME LOOP!- ***/
    while(!key[KEY_ESC])
    {
        //fill screen with background image
        blit(bg, screen, 0, 0, x_offset, 0, 5144, HEIGHT);
                            
        if (key[KEY_RIGHT])
        {
    		b = 0;
    		
            //update the position of cat 
            if (x < 150)
            {
               x += 5;
            }
            else if (x == 150/*> SCREEN_W - kitty[0]->w*/)
            {
                x = 150/*SCREEN_W - kitty[0]->w*/;
                x_offset -= 5;
            }
    		//update the frame
    		if (framecount++ > framedelay)
    		{
    			framecount = 0;
    			curframe++;
    			if (curframe > 5)
    				curframe = 0;
            }
            /* Clear the drawing buffer. */
    		clear_to_color(scene, BLACK);
            //draw the sprite
    		draw_sprite(scene, kitty[curframe], x, y);
        }
        if (key[KEY_LEFT])
        {
            b = 1;
            x_offset += 5;
            //update the position of cat
            if (x < 0)
            {
               x = 0;
            }
            else if (x < 150)
            {
               x -= 5;
            }
            else if (x == 150)
            {
               x = 150;      
            }
            
            //update the frame
            if (framecount++ > framedelay)
            {
                framecount = 0;
                curframe++;
                if (curframe > 5)
                    curframe = 0;
            }
            //clear the drawing buffer
            clear_to_color(scene, BLACK);
            //draw the sprite
            draw_sprite(scene, kittyb[curframe], x, y);
        }
        if (key[KEY_UP])
        {
            //update the position of cat
            y -= 5;
            if (y < 260)
            {
               y = 260;
            }  
            //update the frame
            if (framecount++ > framedelay)
            {
                framecount = 0;
                curframe++;
                if (curframe > 5)
                   curframe = 0;
            }
            //clear the drawing buffer
            clear_to_color(scene, BLACK);
            //draw the sprite
            if (b == 0)
            {
               draw_sprite(scene, kitty[curframe], x, y);
            }
            else if (b == 1)
            {
                 draw_sprite(scene, kittyb[curframe], x, y);
            }
        }
        if (key[KEY_DOWN])
        {
            //update the position of cat
            y += 5;
            if (y > SCREEN_H-100)
            {
               y = SCREEN_H-100;
            }  
            //update the frame
            if (framecount++ > framedelay)
            {
                framecount = 0;
                curframe++;
                if (curframe > 5)
                   curframe = 0;
            }
            //clear the drawing buffer
            clear_to_color(scene, BLACK);
            //draw the sprite
            if (b == 0)
            {
               draw_sprite(scene, kitty[curframe], x, y);
            }
            else if (b == 1)
            {
                 draw_sprite(scene, kittyb[curframe], x, y);
            }
        }
        if (key[KEY_SPACE])
        {
           //update the position of cat
            y -= 5;
            if (y < 220)
            {
               y = 220;
            }  
            //update the frame
            if (framecount++ > framedelay)
            {
                framecount = 0;
                curframe++;
                if (curframe > 5)
                   curframe = 0;
            }
            //clear the drawing buffer
            clear_to_color(scene, BLACK);
            //draw the sprite
            if (b == 0)
            {
               draw_sprite(scene, kitty[curframe], x, y);
            }
            else if (b == 1)
            {
                 draw_sprite(scene, kittyb[curframe], x, y);
            }
        }  
        
        if (key[KEY_EQUALS] && volume<255) {
           volume++;
           set_volume(0,volume);
        }
        else if (key[KEY_MINUS] && volume>0) {
             volume--;
             set_volume(0,volume);
        }
                          
        //DISPLAY SOME INFO
		textprintf_ex(/*screen*/scene,font,0,0,WHITE,0,
            "-,+         -  Volume Down,Up");
        textprintf_ex(/*screen*/scene,font,0,10,WHITE,0,
            "Arrow Keys  -  Movement");
        textprintf_ex(/*screen*/scene,font,0,30,WHITE,0,
            "Sprite X,Y: %3d,%3d", x, y);
		textprintf_ex(/*screen*/scene,font,0,40,WHITE,0,
            "Frame,Count,Delay: %2d,%2d,%2d",
            curframe,framecount,framedelay);
        
        /* Put buffer to display. */
        acquire_screen();
        blit(scene, screen, 0, 0, 0, 0, WIDTH, HEIGHT);
		release_screen();
		
        rest(10);
    }
    stop_midi();
    destroy_midi(music);
    remove_sound();
    allegro_exit();
    return 0;
}
END_OF_MAIN()

Recommended Answers

All 2 Replies

Although I'm not familiar with Allegro, I am almost certain your game uses a single bitblt() (or "blit()" in Allegro I think) to constantly update the screen.. you'll need to learn how to "double buffer" your screen in order to gain that fluid 'non-flickering' update that ye' desire.

Although this is a win32 animation tutorial, glance down at the section on 'Double Buffering' and you'll at least get an idea of what needs to be done. The concept seems quite simple and could probably be implemented in your Allegro application with great ease:

http://www.winprog.org/tutorial/animation.html

Have fun & good luck with the game !

Although I'm not familiar with Allegro, I am almost certain your game uses a single bitblt() (or "blit()" in Allegro I think) to constantly update the screen.. you'll need to learn how to "double buffer" your screen in order to gain that fluid 'non-flickering' update that ye' desire.

Although this is a win32 animation tutorial, glance down at the section on 'Double Buffering' and you'll at least get an idea of what needs to be done. The concept seems quite simple and could probably be implemented in your Allegro application with great ease:

http://www.winprog.org/tutorial/animation.html

Have fun & good luck with the game !

Sweet...thanks for the link. I will look it over and hopefully learn something!

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.