Only the background image will not draw??!
AmnesiA

I have been going over this code for 2 days now trying to figure out why the background image for this loading screen will not appear. I have created an object named BG_INDEX that keeps track of all the background information for a given room including the background image, whether the background is tiled or stretched, and scrolling speeds. I also created an object named BG_CTRL that will handle the BG_INDEX objects; for example, during the loading phase it will create all of the BG_INDEX objects with all of the desired parameters and the main.cpp program interfaces through BG_CTRL to control the BG_INDEX from there on out.

The program executes without error and going through the code in debug mode line by line leads me to believe that each variable has been assigned the correct values and each function the correct parameters but for some reason the background image just never appears on the loading screen. Here is the code:

bg_ctrl.h:

#SelectExpand
1#ifndef BG_CTRL_H 2#define BG_CTRL_H 3 4#include<allegro5/allegro.h> 5#include<allegro5/allegro_image.h> 6#include<allegro5/allegro_primitives.h> 7#include "bg_index.h" 8 9extern ALLEGRO_DISPLAY *display; 10extern int roomIndex; 11 12class BG_CTRL{ 13 14public: 15 BG_CTRL(); 16 ~BG_CTRL(); 17 void initiate(); 18 19 ALLEGRO_BITMAP* step(); 20 21protected: 22 enum ROOM_INDEX{ 23 LOAD, MENU, CREDITS 24 }; 25 26 BG_INDEX bgIndex[CREDITS]; 27 28}; 29 30#endif //BG_CTRL_H

bg_ctrl.cpp:

#include "bg_ctrl.h"

BG_CTRL::BG_CTRL(){}
BG_CTRL::~BG_CTRL(){}

void BG_CTRL::initiate(){
    bgIndex[LOAD].initiate( 102, 576, 380-102, 717-576, 1);
    bgIndex[MENU].initiate( 268, 367, 32, 32, 2, 0, 10);
    bgIndex[CREDITS].initiate( 268, 367, 32, 32, 2, 0, 10);
}

ALLEGRO_BITMAP* BG_CTRL::step(){
    return bgIndex[roomIndex].step();
}

bg_index.h:

#SelectExpand
1#ifndef BG_INDEX_H 2#define BG_INDEX_H 3 4#include<stdio.h> 5#include<string> 6#include<allegro5/allegro.h> 7#include<allegro5/allegro_image.h> 8#include<allegro5/allegro_primitives.h> 9 10using std::string; 11 12extern ALLEGRO_DISPLAY *display; 13extern ALLEGRO_BITMAP *allSprites; 14 15class BG_INDEX{ 16 17public: 18 BG_INDEX(); 19 ~BG_INDEX(); 20 void initiate( const int sx, const int sy, const int sw, 21 const int sh, int type, 22 const int argHSpeed = 0, const int argVSpeed = 0 23 ); //type: 1 - stretch, 2 - tile 24 25 void set_hspeed( const int argHSpeed); 26 void set_vspeed( const int argVSpeed); 27 int get_hspeed(); 28 int get_vspeed(); 29 30 ALLEGRO_BITMAP* step(); 31 32protected: 33 int DISPLAY_W, DISPLAY_H; 34 int IMAGE_W, IMAGE_H; 35 36 ALLEGRO_BITMAP *canvas; //Large canvas with room for extra tiles on all sides 37 ALLEGRO_BITMAP *fullImage; //Medium canvas with room for extra tiles on 2 sides 38 ALLEGRO_BITMAP *bgImage; //Small bitmap representing the display 39 int bgType; 40 int hspeed, vspeed; 41 int hoffset, voffset; 42}; 43 44#endif //BG_INDEX_H

bg_index.cpp:

#SelectExpand
1#include "bg_index.h" 2 3BG_INDEX::BG_INDEX(){} 4BG_INDEX::~BG_INDEX(){} 5 6void BG_INDEX::initiate( const int sx, const int sy, 7 const int sw, const int sh, const int type, 8 const int argHSpeed, const int argVSpeed 9 ){ 10 ALLEGRO_BITMAP *tempBitmap = al_create_sub_bitmap( allSprites, sx, sy, sw, sh); //used to hold original size of bitmap 11 bgImage = al_create_bitmap(DISPLAY_W, DISPLAY_H); 12 fullImage = al_create_bitmap( IMAGE_W + DISPLAY_W, IMAGE_H + DISPLAY_H); 13 canvas = al_create_bitmap( DISPLAY_W + IMAGE_W * 2, DISPLAY_H + IMAGE_H * 2); 14 if(!tempBitmap){ 15 fprintf(stderr, "Failed to create sub bitmap in bg_index.initiate()\n"); 16 } 17 DISPLAY_W = al_get_display_width(display); 18 DISPLAY_H = al_get_display_height(display); 19 IMAGE_W = sw; 20 IMAGE_H = sh; 21 if(type == 1){ //Stretch 22 al_set_target_bitmap(fullImage); 23 al_draw_scaled_bitmap( tempBitmap, 0, 0, sw, sh, 0, 0, DISPLAY_W, DISPLAY_H, 0); 24 al_set_target_bitmap(al_get_backbuffer(display)); 25 }else{ //Tile 26 al_set_target_bitmap(fullImage); 27 for(int i = 0; i <= DISPLAY_H / IMAGE_H + 1; i++){ 28 for(int j = 0; j <= DISPLAY_W / IMAGE_W + 1; j++){ 29 al_draw_bitmap( tempBitmap, IMAGE_W * j, IMAGE_H * i, 0); 30 } 31 } 32 al_set_target_bitmap(al_get_backbuffer(display)); 33 } 34 bgType = type; 35 hspeed = argHSpeed; 36 vspeed = argVSpeed; 37 hoffset = 0; 38 voffset = 0; 39} 40 41void BG_INDEX::set_hspeed( const int argHSpeed){ 42 hspeed = argHSpeed; 43} 44 45void BG_INDEX::set_vspeed( const int argVSpeed){ 46 vspeed = argVSpeed; 47} 48 49int BG_INDEX::get_hspeed(){ 50 return hspeed; 51} 52 53int BG_INDEX::get_vspeed(){ 54 return vspeed; 55} 56 57ALLEGRO_BITMAP* BG_INDEX::step(){ 58 hoffset += hspeed; 59 voffset += vspeed; 60 61 while(hoffset >= IMAGE_H){ 62 hoffset -= IMAGE_H; 63 } 64 while(voffset >= IMAGE_W){ 65 voffset -= IMAGE_W; 66 } 67 while(hoffset <= -IMAGE_H){ 68 hoffset += IMAGE_H; 69 } 70 while(voffset <= -IMAGE_W){ 71 voffset += IMAGE_W; 72 } 73 74 //Draw fullImage to canvas 75 al_set_target_bitmap( canvas); 76 al_draw_scaled_bitmap( fullImage, 0, 0, DISPLAY_W + IMAGE_W, DISPLAY_H + IMAGE_H, //Take entire fullImage 77 (hspeed <= 0 ? IMAGE_W : 0) + hoffset, (vspeed <= 0 ? IMAGE_H : 0) + voffset, //If speed is neg draw 1 full tile ahead 78 DISPLAY_W + IMAGE_W, DISPLAY_H + IMAGE_H, 0 79 ); 80 al_set_target_bitmap( bgImage); 81 al_draw_scaled_bitmap( canvas, IMAGE_W, IMAGE_H, DISPLAY_W, DISPLAY_H, //Take only the middle of the canvas 82 0, 0, DISPLAY_W, DISPLAY_H, 0 //Draw to display dimensions 83 ); 84 al_set_target_bitmap( al_get_backbuffer(display)); 85 return bgImage; 86}

main.cpp:

#SelectExpand
1#include<stdio.h> 2#include<string> 3#include<vector> 4#include<allegro5/allegro.h> 5#include<allegro5/allegro_image.h> 6#include<allegro5/allegro_font.h> 7#include<allegro5/allegro_ttf.h> 8#include<allegro5/allegro_audio.h> 9#include<allegro5/allegro_acodec.h> 10#include<allegro5/allegro_primitives.h> 11#include "sprite_library.h" 12#include "bg_ctrl.h" 13 14using std::string; 15 16//Global Constants 17const int FPS = 60; 18const int SCREEN_W = 640, SCREEN_H = 480; 19const int LOAD_SCREEN_W = 600, LOAD_SCREEN_H = 250; 20 21enum ROOM_INDEX{ 22 LOAD, MENU, CREDITS 23 }; 24 25//Global Variables 26ALLEGRO_DISPLAY *display; 27ALLEGRO_EVENT_QUEUE *event_queue; 28ALLEGRO_TIMER *timer; 29int roomIndex; //Tracks which room we're in 30 31ALLEGRO_BITMAP *allSprites; 32SPRITE_LIBRARY spriteLib; 33BG_CTRL bgCtrl; 34 35//BG Index 36ALLEGRO_BITMAP *roomBg[CREDITS]; 37 38//Prints the resource that failed to initiate 39int init_fail( string failType){ 40 string tempStr = "Failed to initiate " + failType + "!\n"; 41 fprintf( stderr, tempStr.c_str()); 42 return -1; 43} 44 45void update_progress_bar(ALLEGRO_BITMAP *loadProgressBar, 46 const int LOAD_BAR_HEIGHT, const float percentage, 47 const ALLEGRO_FONT *fntLoad, const string nextToLoad){ 48 al_set_target_bitmap(loadProgressBar); 49 al_set_clipping_rectangle(0, 0, percentage*(LOAD_SCREEN_W*.9), LOAD_BAR_HEIGHT); 50 al_clear_to_color(al_map_rgb(255,0,0)); 51 al_set_target_bitmap(al_get_backbuffer(display)); 52 al_draw_bitmap( bgCtrl.step(), 0, 0, 0); 53 al_draw_bitmap(loadProgressBar, LOAD_SCREEN_W * .05, LOAD_SCREEN_H / 4 * 3, 0); 54 al_draw_text(fntLoad, al_map_rgb(255,255,255), 55 LOAD_SCREEN_W * .05, LOAD_SCREEN_H / 4 * 3 - 28, 56 ALLEGRO_ALIGN_LEFT, nextToLoad.c_str() 57 ); 58 al_flip_display(); 59} 60 61//Loads everything needed for the game to execute 62int load_game(){ 63 const int BLOCKS_TO_LOAD = CREDITS; 64 const int LOAD_BAR_HEIGHT = 30; 65 66 ALLEGRO_BITMAP *sprProgressBar; 67 float loadProgress = 0; 68 ALLEGRO_FONT *fntLogoBackup, *fntLoadProgress; 69 70 /////////////////// 71 //LOAD BLOCK 1 72 //Initiate Allegro, Primitives, Images, Display, and Font 73 //Draw the Load Screen 74 //Load main bitmap and extract load screen 75 /////////////////// 76 if(!al_init()){ //Initiate Allegro 77 return init_fail("Allegro"); 78 } 79 if(!al_init_primitives_addon()){ //Initiate primitives to draw load screen 80 return init_fail("Primitives Addon"); 81 } 82 if(!al_init_image_addon()){ //Initiate images for loading screen 83 return init_fail("Image Addon"); 84 } 85 display = al_create_display(LOAD_SCREEN_W, LOAD_SCREEN_H); //Initiate the display 86 if(!display){ 87 return init_fail("Display"); 88 } 89 al_init_font_addon(); //Initiate font 90 al_init_ttf_addon(); 91 92 allSprites = al_load_bitmap("sprites.bmp"); 93 if(!allSprites){ 94 return init_fail("sprites"); 95 } 96 bgCtrl.initiate(); 97 98 sprProgressBar = al_load_bitmap("load_progress.bmp"); //Create Loading Bar 99 if(!sprProgressBar){ 100 fprintf( stderr, "Failed to load load_progress.bmp\n"); 101 sprProgressBar = al_create_bitmap(LOAD_SCREEN_W * .9, LOAD_BAR_HEIGHT); 102 if(!sprProgressBar){ //If can't create bitmaps 103 fprintf( stderr, "Cannot create bitmaps!\n"); 104 return -1; 105 } 106 } 107 fntLoadProgress = al_load_ttf_font("256Bytes.ttf", 24, 0); 108 update_progress_bar(sprProgressBar, LOAD_BAR_HEIGHT, 109 ++loadProgress/BLOCKS_TO_LOAD, fntLoadProgress, "Loading Sprites..." 110 ); //Update Progress Bar 111 al_rest(3); 112 113 /////////////////// 114 //LOAD BLOCK 2 115 //Load sprites 116 /////////////////// 117 118 update_progress_bar(sprProgressBar, LOAD_BAR_HEIGHT, 119 ++loadProgress/BLOCKS_TO_LOAD, fntLoadProgress, "Complete!" 120 ); 121 spriteLib.initiate(); 122 al_rest(3); 123 124 return 0; 125} 126 127int main(int argc, char **argv){ 128 roomIndex = LOAD; 129 int loadSuccess = load_game(); 130 131 if(loadSuccess){ 132 return loadSuccess; 133 } 134 135 return 0; 136}

BG_CTRL is referenced in main.cpp on lines 33, 52, and 96.

My logic for drawing the background the way I do is this:
I have a canvas which is huge and has room for a full extra tile on each side of it (so a scrolling image can go in any direction without drawing out of bounds) and I have a fullImage which is extended ONE tile past what is needed for the display because that's all that's needed to make the scrolling appearance and it draws to the canvas at the end of each step(). bgImage is made the same size as the display and will be directly drawn to the backbuffer. bgImage is made by extracting the middle of the canvas the same size as the display (keeping an extra "unseen" tile size on all sides to work the magic).

The problem is first noticeable on line 58 when the display should be updated (in update_progress_bar function called from load_game called from main())

Thread #612888. Printed from Allegro.cc