Allegro Draw Sprite()将无法正常运行C ++

时间:2012-12-22 14:39:15

标签: c++ allegro allegro5

简单的x和O的游戏。如果按下1-9键,则在电路板上的正确位置绘制X或O:

7 | 8 | 9

4 | 5 | 6

1 | 2 | 3

代码如下,changePlayer()函数只是更改播放器(不包括在内) 我用board [0]来控制它是哪个玩家。

当我运行时,程序板显示正确,但按下键时不会绘制X和O.

我试图将所有内容绘制到缓冲区,然后将缓冲区绘制到屏幕上但不会工作。如果我不使用缓冲区并直接将所有内容绘制到屏幕上 - 它完美无缺!

我确定我遗漏了一些简单的东西:S这是代码:

int main()
{
int winner = 0;
BITMAP *xSprite;
BITMAP *oSprite;
BITMAP *newSprite;

int board[10];
int i = 0;

for(i=0; i<10; i++)
{
    board[i] = 0;
}

allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT, 1920, 1080, 0, 0);

xSprite = load_bitmap( "x.bmp", NULL);

oSprite = load_bitmap( "o.bmp", NULL);

BITMAP *buffer = create_bitmap(1920, 1080);

//draw black background
rectfill(buffer,0,0,1920,1080,makecol(0,0,0));
//draw white grid
line( buffer, 200, 0, 200, 480, makecol( 255, 255, 255));
line( buffer, 400, 0, 400, 480, makecol( 255, 255, 255));
line( buffer, 0, 150, 680, 150, makecol( 255, 255, 255));
line( buffer, 0, 300, 680, 300, makecol( 255, 255, 255));

acquire_screen();
blit(buffer, screen, 0, 0, 0, 0, 1920, 1080);
clear_bitmap(buffer);
release_screen();

while(!key[KEY_ESC])
{
  clear_keybuf();

  if (board[0] == 0)
    newSprite = xSprite;
  else
    newSprite = oSprite;

  acquire_screen();

  if(key[KEY_7] && board[7] == 0) {draw_sprite( screen, newSprite, 0, 0); board[7] = 1+board[0]; board[0] = changePlayer(board[0]);}
  if(key[KEY_8] && board[8] == 0) {draw_sprite( screen, newSprite, 200, 0); board[8] = 1+board[0]; board[0] = changePlayer(board[0]);}
  if(key[KEY_9] && board[9] == 0) {draw_sprite( screen, newSprite, 400, 0); board[9] = 1+board[0]; board[0] = changePlayer(board[0]);}

  if(key[KEY_4] && board[4] == 0) {draw_sprite( screen, newSprite, 0, 150); board[4] = 1+board[0]; board[0] = changePlayer(board[0]);}
  if(key[KEY_5] && board[5] == 0) {draw_sprite( screen, newSprite, 200, 150); board[5] = 1+board[0]; board[0] = changePlayer(board[0]);}
  if(key[KEY_6] && board[6] == 0) {draw_sprite( screen, newSprite, 400, 150); board[6] = 1+board[0]; board[0] = changePlayer(board[0]);}

  if(key[KEY_1] && board[1] == 0) {draw_sprite( screen, newSprite, 0, 300); board[1] = 1+board[0]; board[0] = changePlayer(board[0]);}
  if(key[KEY_2] && board[2] == 0) {draw_sprite( screen, newSprite, 200, 300); board[2] = 1+board[0]; board[0] = changePlayer(board[0]);}
  if(key[KEY_3] && board[3] == 0) {draw_sprite( screen, newSprite, 400, 300); board[3] = 1+board[0]; board[0] = changePlayer(board[0]);}

  release_screen();
}
return 0;

}   
END_OF_MAIN(); 

1 个答案:

答案 0 :(得分:1)

鉴于Allegro 4真的很老并且在现代操作系统上并不总能正常运行,因此给出了一些一般性的指示:

  • 如果您刚刚开始,请使用Allegro 5

  • 始终使用set_color_depth(desktop_color_depth())以获得最大兼容性。

  • 使用GFX_GDI获得最大兼容性。 (显然不是最快的驱动程序,但对于简单的游戏,它并不重要。)

  • 请勿使用acquire_screen()release_screen()

对于您的特定问题,没有什么可说的,因为您发布的代码显然适合您。如果你正在使用缓冲区,当然你需要确保在每一帧上将它blit到屏幕上。