Allegro c++;闪烁位图

发布于 2024-12-03 04:39:54 字数 1335 浏览 0 评论 0原文

我正在尝试制作一个简单的程序,其中一个位图是“背景”,另一个位图是我可以移动的,我尝试了不同的方法,例如将背景直接绘制到屏幕上,尝试制作两个缓冲区,我尝试将两个位图放在一个中缓冲。现在我正在运行程序,缓冲区中的两者都在循环中调用两次。但可移动的位图会闪烁。

#include <allegro.h> 


int main(int argc, char *argv[]) 


{ 
allegro_init(); 
install_keyboard(); 
set_color_depth(16); 
set_gfx_mode(GFX_AUTODETECT, 640,480,0,0); 



BITMAP *my_pic = NULL;
my_pic = load_bitmap("image.bmp", NULL);
BITMAP *my_pics;
my_pics = load_bitmap("picture.bmp", NULL);
BITMAP *buffer = NULL;
buffer = create_bitmap(640,480);
BITMAP *bitty=NULL;
bitty = create_bitmap(640,480);





int my_pic_x = 0;
int my_pic_y = 0;
int my_pics_x=0;
int my_pics_y=0;

while(!key[KEY_ESC]) 
{


    if(key[KEY_RIGHT]) 
    { 
        my_pic_x ++;
    } 
    else if(key[KEY_LEFT])  
    { 
        my_pic_x --; 
    }
    else if(key[KEY_UP]) 
    {
        my_pic_y --; 
    } 
    else if(key[KEY_DOWN])  
    { 
        my_pic_y ++; 
    }

draw_sprite(bitty,my_pic,my_pic_x,my_pic_y);                        
//draw_sprite( screen, my_pic,  0, 0);
blit(bitty, screen, 0,0,0,0,640,480);
clear_bitmap(bitty);

draw_sprite(buffer,my_pics,my_pics_x,my_pics_y);
blit(buffer, screen, 0,0,0,0,640,480);
clear_bitmap(buffer);




}


destroy_bitmap(my_pic); 
destroy_bitmap(my_pics);
destroy_bitmap(buffer); 
destroy_bitmap(bitty);
return 0;  
} 
END_OF_MAIN() 

im trying to make a simple program that has one BITMAP that is the "background" and another BITMAP that i can move, ive tried different ways, like drawing the background directly to screen, tried making two buffers, ive tried putting both BITMAPS in one buffer. right now im running the program with both in the buffer called twice in a loop. but the moveable BITMAP flashes.

#include <allegro.h> 


int main(int argc, char *argv[]) 


{ 
allegro_init(); 
install_keyboard(); 
set_color_depth(16); 
set_gfx_mode(GFX_AUTODETECT, 640,480,0,0); 



BITMAP *my_pic = NULL;
my_pic = load_bitmap("image.bmp", NULL);
BITMAP *my_pics;
my_pics = load_bitmap("picture.bmp", NULL);
BITMAP *buffer = NULL;
buffer = create_bitmap(640,480);
BITMAP *bitty=NULL;
bitty = create_bitmap(640,480);





int my_pic_x = 0;
int my_pic_y = 0;
int my_pics_x=0;
int my_pics_y=0;

while(!key[KEY_ESC]) 
{


    if(key[KEY_RIGHT]) 
    { 
        my_pic_x ++;
    } 
    else if(key[KEY_LEFT])  
    { 
        my_pic_x --; 
    }
    else if(key[KEY_UP]) 
    {
        my_pic_y --; 
    } 
    else if(key[KEY_DOWN])  
    { 
        my_pic_y ++; 
    }

draw_sprite(bitty,my_pic,my_pic_x,my_pic_y);                        
//draw_sprite( screen, my_pic,  0, 0);
blit(bitty, screen, 0,0,0,0,640,480);
clear_bitmap(bitty);

draw_sprite(buffer,my_pics,my_pics_x,my_pics_y);
blit(buffer, screen, 0,0,0,0,640,480);
clear_bitmap(buffer);




}


destroy_bitmap(my_pic); 
destroy_bitmap(my_pics);
destroy_bitmap(buffer); 
destroy_bitmap(bitty);
return 0;  
} 
END_OF_MAIN() 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老娘不死你永远是小三 2024-12-10 04:39:54

你的代码是这样工作的:

// draw the my_pic sprite to bitty
draw_sprite(bitty,my_pic,my_pic_x,my_pic_y);
// status now: bitty contains my_pic

// draw bitty to the screen
blit(bitty, screen, 0,0,0,0,640,480);
// status now: screen contains my_pic by way of bitty

// clear bitty
clear_bitmap(bitty);
// status now: screen contains my_pic by way of a former
// version of bitty, bitty is now empty

// draw my_pics to buffer
draw_sprite(buffer,my_pics,my_pics_x,my_pics_y);
// status now: screen contains my_pic, bitty is empty,
// buffer contains my_pics

// draw buffer to the screen
blit(buffer, screen, 0,0,0,0,640,480);
// status now: screen and buffer both contain my_pics,
// bitty is empty

// clear the buffer
clear_bitmap(buffer);
// status now:
//
//    screen contains my_pics
//    bitty and buffer are empty

我想你会想要更多类似的东西:

// clear buffer
clear_bitmap(buffer);
// status now: buffer is empty

// draw my_pic to buffer
draw_sprite(buffer,my_pic,my_pic_x,my_pic_y);
// status now: buffer contains my_pic

// draw my_pics to buffer
draw_sprite(buffer,my_pics,my_pics_x,my_pics_y);
// status now: buffer contains my_pic, with my_pics on top

// copy buffer to the screen
blit(buffer, screen, 0,0,0,0,640,480);
// status now: buffer and screen contain my_pic, with my_pics on top

The code you have works like this:

// draw the my_pic sprite to bitty
draw_sprite(bitty,my_pic,my_pic_x,my_pic_y);
// status now: bitty contains my_pic

// draw bitty to the screen
blit(bitty, screen, 0,0,0,0,640,480);
// status now: screen contains my_pic by way of bitty

// clear bitty
clear_bitmap(bitty);
// status now: screen contains my_pic by way of a former
// version of bitty, bitty is now empty

// draw my_pics to buffer
draw_sprite(buffer,my_pics,my_pics_x,my_pics_y);
// status now: screen contains my_pic, bitty is empty,
// buffer contains my_pics

// draw buffer to the screen
blit(buffer, screen, 0,0,0,0,640,480);
// status now: screen and buffer both contain my_pics,
// bitty is empty

// clear the buffer
clear_bitmap(buffer);
// status now:
//
//    screen contains my_pics
//    bitty and buffer are empty

I would imagine you'd want something more like:

// clear buffer
clear_bitmap(buffer);
// status now: buffer is empty

// draw my_pic to buffer
draw_sprite(buffer,my_pic,my_pic_x,my_pic_y);
// status now: buffer contains my_pic

// draw my_pics to buffer
draw_sprite(buffer,my_pics,my_pics_x,my_pics_y);
// status now: buffer contains my_pic, with my_pics on top

// copy buffer to the screen
blit(buffer, screen, 0,0,0,0,640,480);
// status now: buffer and screen contain my_pic, with my_pics on top
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文