Allegro 4功能错误

时间:2013-10-27 10:54:39

标签: c++ function allegro

我最近一直在学习一些与allegro的游戏编程,但我遇到了一个问题。 我的“游戏”是我现在学到的很多教程的编译类型。无论如何,我遇到了Allegro的功能问题。 我的第一个代码是:

#include <allegro.h>
#include <sstream>
#include <string>
#include <cstring>
#include <fstream>
volatile bool Close = false;
void handler(){
     Close = true;}
/*timer*/
volatile long speed_counter = 0;
void increment_speed_counter(){
     speed_counter++;
     }
END_OF_FUNCTION(increment_speed_counter);

using namespace std;


int main() 
{   
if(!allegro_init()){
                    fstream error;
                    error.open("error_log.txt", ios::ate);
                    error << "ERROR: Failed to initialized allegro!!\n";
                    error.close();
                    }
    install_keyboard();
    install_timer();

    LOCK_VARIABLE(speed_counter);
    LOCK_FUNCTION(increment_speed_counter);
 install_int_ex(increment_speed_counter, BPS_TO_TIMER(120));

    set_color_depth(24);
    set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);

    BITMAP *buffer = create_bitmap(640, 480);
    BITMAP *spaceship = load_bitmap("ship.bmp", NULL); //up
    BITMAP *spaceship2 = load_bitmap("ship2.bmp", NULL); //down
    BITMAP *spaceship3 = load_bitmap("ship3.bmp", NULL); //right
    BITMAP *spaceship4 = load_bitmap("ship4.bmp", NULL); //left
    FONT *myriad = load_font("myriad.pcx", NULL, NULL);
    BITMAP *meteor = load_bitmap("meteor.bmp", NULL);
    BITMAP *options = create_bitmap(640, 480);

//srand(time(NULL));
LOCK_FUNCTION(handler);
//int mx = rand() % 100;
//int my = rand() % 100;
long int ship_x = 300;
long int ship_y = 200;
int Bx [1000];
int By [1000];
    bool done = false;
set_close_button_callback(handler);




    while(!Close){                 
  while(speed_counter > 0){
textprintf_ex(screen, font, 200,10, makecol(255,100,200), -1, "Space Void: Asonomia"); 

                 if(ship_x >= 620){
                           ship_x = 620;
                           }
                 if(ship_y >= 460){
                           ship_y = 460;
                           }
                 if(ship_x <= 0){
                           ship_x = 0;
                           }
                 if(ship_y <= 0){
                           ship_y = 0;
                           }

                 if(key[KEY_ESC]){
                                 Close = true;
                                  }
                 if(key[KEY_LEFT]){

                                   ship_x -=5;
                                 clear(buffer);
                                 draw_sprite(buffer, spaceship4, ship_x, ship_y);
                                 blit(buffer, screen, 0,0,0,0, 640, 480);
                                 rest(20);
                                   }
                 if(key[KEY_RIGHT]){
                                    ship_x +=5;
                                 clear(buffer);
                                 draw_sprite(buffer, spaceship3, ship_x, ship_y);
                                 blit(buffer, screen, 0,0,0,0, 640, 480);
                                 rest(20);
                                    }
                 if(key[KEY_UP]){
                                 ship_y -= 5;
                                 clear(buffer);
                                 draw_sprite(buffer, spaceship, ship_x, ship_y);
                                 blit(buffer, screen, 0,0,0,0, 640, 480);
                                 rest(20);
                                 }
                 if(key[KEY_DOWN]){
                                   ship_y += 5;
                                   clear(buffer);
                                 draw_sprite(buffer, spaceship2, ship_x, ship_y);
                                 blit(buffer, screen, 0,0,0,0, 640, 480);
                                 rest(20);
                                   }

speed_counter--;
                }
}



   destroy_bitmap(spaceship);
    destroy_bitmap(spaceship2);
    destroy_bitmap(spaceship3);
    destroy_bitmap(spaceship4);
    destroy_bitmap(buffer);

    return 0;
}
END_OF_MAIN()

非常混乱所以我决定通过添加功能来清理它。

现在就是这样:

#include <allegro.h>
#include <sstream>
#include <string>
#include <cstring>
#include <fstream>

//X Button
volatile bool Close = false;
void handler(){
     Close = true;}
//FPS 
volatile long speed_counter = 0;
void increment_speed_counter(){
    speed_counter++;
}

void initialize(){
    if(!allegro_init()){
    allegro_message("Error Initializing Allegro");
    }
    install_keyboard();
    install_timer();
    LOCK_VARIABLE(speed_counter);
    LOCK_FUNCTION(increment_speed_counter);
    install_int_ex(increment_speed_counter, BPS_TO_TIMER(60));

    //Video Mode

    set_color_depth(24);
    set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0,0);

}


void load_externs(){
    BITMAP *buffer = create_bitmap(640, 480);
    BITMAP *spaceship = load_bitmap("ship.bmp", NULL); //up
    BITMAP *spaceship2 = load_bitmap("ship2.bmp", NULL); //down
    BITMAP *spaceship3 = load_bitmap("ship3.bmp", NULL); //right
    BITMAP *spaceship4 = load_bitmap("ship4.bmp", NULL); //left
    BITMAP *meteor = load_bitmap("meteor.bmp", NULL);
    BITMAP *options = create_bitmap(640, 480);
}

int main(){
initialize();
load_externs();

    long int ship_x = 300;
    long int ship_y = 200;
    bool done = false;
    set_close_button_callback(handler);

    while(!Close){                 
while(speed_counter > 0){
textprintf_ex(screen, font, 200,10, makecol(255,100,200), -1, "Space Void: Asonomia"); 

                 if(ship_x >= 620){
                           ship_x = 620;
                           }
                 if(ship_y >= 460){
                           ship_y = 460;
                           }
                 if(ship_x <= 0){
                           ship_x = 0;
                           }
                 if(ship_y <= 0){
                           ship_y = 0;
                           }

                 if(key[KEY_ESC]){
                                 Close = true;
                                  }
                 if(key[KEY_LEFT]){

                                   ship_x -=5;
                                 clear(buffer);
                                 draw_sprite(buffer, spaceship4, ship_x, ship_y);
                                 blit(buffer, screen, 0,0,0,0, 640, 480);
                                 rest(20);
                                   }
                 if(key[KEY_RIGHT]){
                                    ship_x +=5;
                                 clear(buffer);
                                 draw_sprite(buffer, spaceship3, ship_x, ship_y);
                                 blit(buffer, screen, 0,0,0,0, 640, 480);
                                 rest(20);
                                    }
                 if(key[KEY_UP]){
                                 ship_y -= 5;
                                 clear(buffer);
                                 draw_sprite(buffer, spaceship, ship_x, ship_y);
                                 blit(buffer, screen, 0,0,0,0, 640, 480);
                                 rest(20);
                                 }
                 if(key[KEY_DOWN]){
                                   ship_y += 5;
                                   clear(buffer);
                                 draw_sprite(buffer, spaceship2, ship_x, ship_y);
                                 blit(buffer, screen, 0,0,0,0, 640, 480);
                                 rest(20);
                                   }

speed_counter--;
                }
}



   destroy_bitmap(spaceship);
    destroy_bitmap(spaceship2);
    destroy_bitmap(spaceship3);
    destroy_bitmap(spaceship4);
    destroy_bitmap(buffer);
    return 0;

}
END_OF_MAIN()

我添加了两个新的函数,它们是“load_externs”,它会加载位图和“初始化”,它会初始化allegro,图形等。 现在,当我编译它时,它给我一个错误,即没有定义BITMAP,如太空船,缓冲区等。虽然,我已经在int main中添加了函数:load_externs(); 最重要的是它没有初始化allegro,这意味着它没有读取我在int main()中添加的函数,但为什么呢?我可能在这里遗漏了一些东西,我很感激一些帮助,谢谢。 :)

编辑:我不能在allegro.cc上问这个问题以及我已经在那里开了一个帐户而且我无法使用它登录,当我创建另一个帐户时也是如此。我一直收到登录错误......:/但是......我知道stackoverflow的很棒的成员总是在这里提供帮助。 :d

1 个答案:

答案 0 :(得分:0)

您已在load_externs中创建位图局部变量,但在main等其他函数中不会显示它们。

最快的解决方案是让bufferspaceship等全球化。