0

最近、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」と、アレグロやグラフィックスなどを初期化する「initialize」という 2 つの新しい関数を追加しました。コンパイルすると、宇宙船やバッファなどの定義済みのビットマップが存在しないというエラーが表示されます。ただし、int main: load_externs(); に関数を追加しました。その上、それは allegro を初期化していませんでした。つまり、int main() に追加した関数を読み取っていませんでしたが、なぜですか? 私はおそらくここで何かを見逃しています。助けていただければ幸いです、ありがとう。:)

編集: allegro.cc でこれを尋ねることができませんでした。また、既にアカウントを作成していて、ログインできませんでした。別のアカウントを作成したときにも同じことが起こりました。ログイン エラーが発生し続けます... :/ でも... Stackoverflow のすばらしいメンバーがいつでも助けに来てくれます。:D

4

1 に答える 1

0

でビットマップ ローカル変数を作成しましたload_externs。これらは のような他の関数では表示されませんmain

最も速い解決策はbufferspaceshipなどをグローバルにすることです。

于 2013-10-29T15:00:33.580 に答える