Trouble drawing scrolling background
AmnesiA

EDIT: omg nevermind, I stepped away for a bit and came back and debugged it again... Since I was only changing Y variables that's all I was paying attention to but then I realized I had the x coords mixed up so I was trying to created a sub bitmap outside of the original bitmap (x1 being at the far right side of the bitmap) so that was my problem. Sorry!
tl/dr: SOLVED, stupid mistake

Okay so I have a background object with a draw() function that will scroll the background by the instance's speed and then draw to whichever bitmap is currently the target. For simplicity sake, when the BACKGROUND object is initialized it stretches or tiles the loaded image to the size of the display.

The problem is with the scrolling, whatever part of the image goes off screen is supposed to be drawn to the top of the screen (creating a constant scrolling effect). For some reason though, it doesn't draw the top part of the image.

#SelectExpand
1int BACKGROUND::draw(){ 2 xOffset += hspeed; 3 yOffset += vspeed; 4 while( xOffset >= DISPLAY_W){ 5 xOffset -= DISPLAY_W; 6 } 7 while( yOffset >= DISPLAY_H){ 8 yOffset -= DISPLAY_H; 9 } 10 while( xOffset <= -DISPLAY_W){ 11 xOffset += DISPLAY_W; 12 } 13 while( yOffset <= -DISPLAY_H){ 14 yOffset += DISPLAY_H; 15 } 16 17 ALLEGRO_BITMAP* tempBitmap1 = al_create_sub_bitmap( bgImage, 18 0, 0, DISPLAY_W - xOffset, DISPLAY_H - yOffset); 19 ALLEGRO_BITMAP* tempBitmap2 = al_create_sub_bitmap( bgImage, 20 DISPLAY_W - xOffset, DISPLAY_H - yOffset, xOffset, yOffset); 21 if( (!tempBitmap1) || (!tempBitmap2)){ 22 return -1; 23 } 24 25 al_draw_bitmap( tempBitmap2, 0, 0, 0); 26 al_draw_bitmap( tempBitmap1, xOffset, yOffset, 0); 27 28 return 1; 29}

If it's relevant, here's the code to initialize it:

#SelectExpand
1int BACKGROUND::load_from_bitmap( ALLEGRO_BITMAP* bg_image, int sx, int sy, int sw, int sh, 2 ALLEGRO_COLOR bg_color, int bg_type, 3 float h_speed, float v_speed){ 4 ALLEGRO_BITMAP* targetBitmap = al_get_target_bitmap(); 5 6 //Set variables 7 bgColor = bg_color; 8 bgType = bg_type; 9 hspeed = h_speed; 10 vspeed = v_speed; 11 12 //Load bg image 13 ALLEGRO_BITMAP* tempBitmap = al_create_bitmap( sw, sh); 14 bgImage = al_create_bitmap( DISPLAY_W, DISPLAY_H); 15 if(!bgImage){ 16 return -1; 17 } 18 if(!tempBitmap){ 19 hasImage = false; 20 al_set_target_bitmap( bgImage); 21 al_clear_to_color( bgColor); 22 }else{ 23 hasImage = true; 24 al_set_target_bitmap( tempBitmap); 25 al_draw_bitmap_region( bg_image, sx, sy, sw, sh, 0, 0, 0); 26 al_set_target_bitmap( bgImage); 27 switch( bgType){ 28 case BG_TILED: //Tile tempBitmap 29 for( int i = 0; i < DISPLAY_W / sw; ++i){ 30 for( int j = 0; j < DISPLAY_H / sh; ++j){ 31 al_draw_bitmap( tempBitmap, i*sw, j*sh, 0); 32 } 33 } 34 break; 35 case BG_STRETCH: //Stretch tempBitmap 36 al_draw_scaled_bitmap( tempBitmap, 0, 0, sw, sh, 0, 0, DISPLAY_W, DISPLAY_H, 0); 37 break; 38 case BG_CENTER: //Center tempBitmap 39 al_clear_to_color( bgColor); 40 al_draw_bitmap( tempBitmap, DISPLAY_W / 2 - sw / 2, DISPLAY_H / 2 - sh / 2, 0); 41 break; 42 default: 43 break; 44 } 45 } 46 al_set_target_bitmap( targetBitmap); 47 48 return 1; 49}

Thread #612952. Printed from Allegro.cc