I am working on my first scrolling game.
I am pretty new to game programming in general, but have so far successfully made a couple very simple games. (Pong, etc.)

I am using Dev-C++ and the Allegro library for this game.
It's basically going to be a cat that runs around and kills mice...and whatever other little things I can think to add later.

I have the can animated and running around, but I run into trouble with the background. I've tried some different combinations, but can't seem to get both the background and the cat on the screen at the same time. It's also not yet set up to scroll. I have been researching it for a while, but am stumped on how to get it going.

The game's resolution is 640x580, and my background is 5144x480.
For now, I just want it to continuously scroll...either by itself or preferably as the cat is moved forward/backward.

Please help!
Here 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;
int x=50, 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, "AnimSprite Program (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, buffer, 0, 0, 0, 0, WIDTH, HEIGHT);
                            
        if (key[KEY_RIGHT])
        {
    		b = 0;
            //update the position of cat 
            x += 5;
            if (x > SCREEN_W - kitty[0]->w)
            {
                x = SCREEN_W - kitty[0]->w;
            }
    		//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;
            //update the position of cat
            x -= 5;
            if (x < 0)
            {
               x = 0;
            }
            //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 < 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_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()

UPDATE....

Ok... I added a x_offset variable, which is changes when the player moves left or right (left/right arrow keys ).
It works, but the background image doesn't continue... Whether I scroll right or left, it reveals a black background instead of the continuing background image... as if the background image was only the one single 640x480 frame, although it is actually 5144x480.

I'm also still having the flashing problem!

Any suggestions?


UPDATE....

I have it scrolling the full width of the background image now, but the screen is still flickering.
It also seems to run a bit slower than it was originally.

I have it scrolling the full width of the background image now, but the screen is still flickering.
It also seems to run a bit slower than it was originally.

Draw the background image to the buffer first. That will eliminate the flickering. Move the background image around the buffer to solve this problem.

Draw the background image to the buffer first. That will eliminate the flickering. Move the background image around the buffer to solve this problem.

Would you mind elaborating a bit on where exactly you mean?
In what line above should I do something different?

Thanks for your reply

Get used to using functions like this;

//put in a function like this
void updatescreen(); //create the function
...
void updatescreen(){ //define the function
draw_sprite ( screen, buffer, 0, 0);
//supose you have a menu
if (menu == 1){
...
}
else if (menu == 2){
draw_sprite ( bg, buffer, bgmovex, bgmovey); // bgmovex,y being the coordinates of current side scrolling position
if ( key[KEY_W]){
draw_sprite ( kittenjump, buffer, posx, posy); 
// or whatever the hell it happens to be (this should draw over the buffer and your background, this should eliminate all flickering or possible pixel drag)
}
else if (...){
...
}
...
}
updatescreen(); //call the function
}

Just use this function within your main loop (or loop only the functions) and this will draw all the sprites/backgrounds etc. all at the same time, and to the buffer. You never want to draw somthing not to the buffer, even though that option is available.

Keep a diagram tree so you know what is being draw upon what, it will be easier to track.


Also, define/initialize everything before int main(){ This will help for your code readability. We don't want to find initializations in the middle of your code because that could lead to all sorts of errors.

Just use this function within your main loop (or loop only the functions) and this will draw all the sprites/backgrounds etc. all at the same time, and to the buffer. You never want to draw somthing not to the buffer, even though that option is available.

Keep a diagram tree so you know what is being draw upon what, it will be easier to track.


Also, define/initialize everything before int main(){ This will help for your code readability. We don't want to find initializations in the middle of your code because that could lead to all sorts of errors.

Thanks a lot for your reply and input. It is appreciated!
I finally got it figured out last night and got it working correctly...AND got the background wrapping/looping! It runs smooth!

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.