c++ allegro程序边界框碰撞关闭
Posted
技术标签:
【中文标题】c++ allegro程序边界框碰撞关闭【英文标题】:c++ allegro program bounding box collision is off 【发布时间】:2011-07-14 07:29:29 【问题描述】:我正在使用 allegro 图形库为 C++ 游戏制作菜单。我正在使用代码块和 mingw。有许多按钮使用修改后的边界框,如果鼠标悬停在按钮上,则返回 true。它适用于除一个之外的所有按钮,我不知道为什么会发生这种情况。我花了一个小时寻找,但什么也没找到。在损坏的按钮上,当将鼠标悬停在按钮右侧和下方以及上方的某些位置时,它会响应 true。 :/ 任何帮助将非常感激。随意指出我的代码中的任何其他问题,或者我是否以愚蠢的方式做某事。我正在自学 C++,如果我的代码很糟糕,我不会感到惊讶。谢谢! :)
main.cpp
#include<allegro.h>
#include<fstream>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include"menu.h"
using namespace std;
//external functions
extern void setup();
extern void evil_sock_puppet_intro(BITMAP*);
extern void version_number(BITMAP*);
extern bool bounding_box_collision(int, int, int, int, int, int, int, int);
extern bool check_button(button);
extern void credits(BITMAP*, BITMAP*, BITMAP*, button);
extern void instructions(BITMAP*, BITMAP*, BITMAP*, button);
extern void highscores(BITMAP*, BITMAP*, BITMAP*, button);
int main()
//loops
int i, n;
bool loop;
//file input output streams
int save_return;
ifstream load_highscores;
//set up allegro
setup();
//create bitmaps
BITMAP *buffer = create_bitmap(640, 480);
//menu
BITMAP *menu_background = load_bitmap("placeholder_sprites\\menu_background.bmp", NULL);
BITMAP *title = load_bitmap("placeholder_sprites\\title.bmp", NULL);
BITMAP *play_preview = load_bitmap("placeholder_sprites\\play_preview.bmp", NULL);
BITMAP *play_button_bmp = load_bitmap("sprites\\start_button.bmp", NULL);
BITMAP *instructions_button_bmp = load_bitmap("sprites\\instructions_button.bmp", NULL);
BITMAP *highscores_button_bmp = load_bitmap("sprites\\highscores_button.bmp", NULL);
BITMAP *credits_button_bmp = load_bitmap("sprites\\credits_button.bmp", NULL);
BITMAP *exit_button_bmp = load_bitmap("sprites\\exit_button.bmp", NULL);
//other buttons
BITMAP *menu_button_bmp = load_bitmap("sprites\\menu_button.bmp", NULL);
//instructions
BITMAP *instructions_info = load_bitmap("placeholder_sprites\\instructions.bmp", NULL);
//credits
BITMAP *credits_bitmap = load_bitmap("placeholder_sprites\\credits.bmp", NULL);
//highscores
BITMAP *highscore_table = load_bitmap("placeholder_sprites\\highscore_table.bmp", NULL);
//declare variables
//variables from data structures
button play_button;
button instructions_button;
button highscores_button;
button credits_button;
button exit_button;
button menu_button;
//score
int highscore_return;
int score;
//rand seed from clock
srand( time(NULL) );
//buttons
play_button.x = 30, play_button.y = 170, play_button.w = 70, play_button.h = 28, play_button.select = false;
instructions_button.x = 30, instructions_button.y = 210, instructions_button.w = 164, instructions_button.h = 28, instructions_button.select = false;
highscores_button.x = 30, highscores_button.y = 250, highscores_button.w = 150, highscores_button.h = 28, highscores_button.select = false;
credits_button.x = 30, credits_button.y = 290, credits_button.w = 95, credits_button.h = 28, credits_button.select = false;
exit_button.x = 30, exit_button.y = 330, exit_button.w = 64, exit_button.h = 28, exit_button.select = false;
menu_button.x = 300, menu_button.y = 400, menu_button.w = 70, menu_button.h = 28, menu_button.select = false;
//call intro
evil_sock_puppet_intro(buffer);
//menu
while(!key[KEY_ESC])
//blit background, title & version number
blit(menu_background, buffer, 0, 0, 0, 0, 640, 480);
masked_blit(title, buffer, 0, 0, 80, 30, 640, 480);
version_number(buffer);
//blit buttons to buffer
if(!play_button.select)
blit(play_button_bmp, buffer, 0, 0, play_button.x, play_button.y, play_button.w, play_button.h);
else
blit(play_button_bmp, buffer, play_button.w + 1, 0, play_button.x, play_button.y, play_button.w, play_button.h);
if(!instructions_button.select)
blit(instructions_button_bmp, buffer, 0, 0, instructions_button.x, instructions_button.y, instructions_button.w, instructions_button.h);
else
blit(instructions_button_bmp, buffer, instructions_button.w + 1, 0, instructions_button.x, instructions_button.y, instructions_button.w, instructions_button.h);
if(!highscores_button.select)
blit(highscores_button_bmp, buffer, 0, 0, highscores_button.x, highscores_button.y, highscores_button.w, highscores_button.h);
else
blit(highscores_button_bmp, buffer, highscores_button.w + 1, 0, highscores_button.x, highscores_button.y, highscores_button.w, highscores_button.h);
if(!credits_button.select)
blit(credits_button_bmp, buffer, 0, 0, credits_button.x, credits_button.y, credits_button.w, credits_button.h);
else
blit(credits_button_bmp, buffer, credits_button.w + 1, 0, credits_button.x, credits_button.y, credits_button.w, credits_button.h);
if(!exit_button.select)
blit(exit_button_bmp, buffer, 0, 0, exit_button.x, exit_button.y, exit_button.w, exit_button.h);
else
blit(exit_button_bmp, buffer, exit_button.w + 1, 0, exit_button.x, exit_button.y, exit_button.w, exit_button.h);
//check if buttons are hovered
play_button.select = check_button(play_button);
instructions_button.select = check_button(instructions_button);
highscores_button.select = check_button(highscores_button);
credits_button.select = check_button(credits_button);
exit_button.select = check_button(exit_button);
//call function if its respective button is pressed
//if(mouse_b & 1 && play_button.select)
while(loop)
//score = game(buffer);
//highscore_return = highscores(buffer, highscore_table, menu_button, play_button, score, highscore);
if(highscore_return != 1)
loop = false;
loop = true;
//
if(mouse_b & 1 && instructions_button.select)
instructions(buffer, instructions_info, menu_button_bmp, menu_button);
if(mouse_b & 1 && highscores_button.select)
highscores(buffer, highscore_table, menu_button_bmp, menu_button);
if(mouse_b & 1 && credits_button.select)
credits(buffer, credits_bitmap, menu_button_bmp, menu_button);
if(mouse_b & 1 && exit_button.select)
return 0;
//preview
blit(play_preview, buffer, 0, 0, 302, 138, 300, 300);
//blit mouse to buffer
show_mouse(buffer);
//blit buffer to screen and pause
blit(buffer, screen, 0, 0, 0, 0, 640, 480);
rest(25);
clear_bitmap(buffer);
return 0;
END_OF_MAIN()
菜单.h:
//data structures
struct button
int x;
int y;
int w;
int h;
bool select;
;
instructions.cpp(这个很好用)
#include<allegro.h>
#include"menu.h"
//external functions
extern bool check_button(button);
void instructions(BITMAP *buffer, BITMAP *instructions, BITMAP *menu_button_bmp, button menu_button)
while(!key[KEY_ESC])
//check if buttons are hovered
menu_button.select = check_button(menu_button);
//go to the main menu if the menu button is pressed
if(mouse_b & 1 && menu_button.select)
return;
//blit bitmaps to buffer
blit(instructions, buffer, 0, 0, 0, 0, 640, 480);
if(!menu_button.select)
blit(menu_button_bmp, buffer, 0, 0, menu_button.x, menu_button.y, menu_button.w, menu_button.h);
else
blit(menu_button_bmp, buffer, menu_button.w + 1, 0, menu_button.x, menu_button.y, menu_button.w, menu_button.h);
//blit mouse to buffer
show_mouse(buffer);
//blit buffer to screen and pause
blit(buffer, screen, 0, 0, 0, 0, 640, 480);
rest(25);
clear_bitmap(buffer);
highscores.cpp:(而这个没有)
#include<allegro.h>
#include<fstream>
#include"menu.h"
using namespace std;
extern int save_game_highscore(int score[]);
extern int check_button(button);
//menu accessed
void highscores(BITMAP *buffer, BITMAP *highscore_table, BITMAP *menu_button_bmp, button menu_button)
//declare file input output streams
ifstream load_highscores;
//declare variables
int i;
int save_return;
bool change_highscore = true;
bool close = false;
//load highscores from a file
int highscore [5] = 400, 200, 150, 100, 25;
load_highscores.open("savefiles\\highscores.save", ios::in);
for(i = 0; i < 6; i++)
load_highscores >> highscore[i];
load_highscores.close();
while(!key[KEY_ESC] && close == false)
//blit highscore table to buffer
blit(highscore_table, buffer, 0, 0, 0, 0, 640, 480);
textprintf_ex(buffer, font, 119, 145, makecol(255, 255, 255), -1, "%d", highscore[0]);
textprintf_ex(buffer, font, 119, 192, makecol(255, 255, 255), -1, "%d", highscore[1]);
textprintf_ex(buffer, font, 119, 238, makecol(255, 255, 255), -1, "%d", highscore[2]);
textprintf_ex(buffer, font, 119, 280, makecol(255, 255, 255), -1, "%d", highscore[3]);
textprintf_ex(buffer, font, 119, 322, makecol(255, 255, 255), -1, "%d", highscore[4]);
//blit buttons to buffer
if(!menu_button.select)
blit(menu_button_bmp, buffer, 0, 0, menu_button.x, menu_button.y, menu_button.w, menu_button.h);
else
blit(menu_button_bmp, buffer, menu_button.w + 1, 0, menu_button.x, menu_button.y, menu_button.w, menu_button.h);
show_mouse(buffer);
//check if menu button is pressed & if so return to the menu
menu_button.select = check_button(menu_button);
if(mouse_b & 1 && menu_button.select)
return;
//blit buffer to screen
blit(buffer, screen, 0, 0, 0, 0, 640, 480);
rest(25);
【问题讨论】:
不可能让您的最小示例显示问题更小吗? 对不起。我不确定是什么导致了这个问题,所以我把所有的代码都放在那里。 :// 我发现了我将外部函数编写为 int 而不是 bool 的问题。明天当那个愚蠢的限制消失时,我会发布一个正确的答案。 【参考方案1】:我发现了问题! :D 在 highscores.cpp 中,外部 button_check 函数被编写为 int 而不是 bool。现在一切都修好了。 :O 刚刚意识到我忘记在我的问题中包含 button_check 函数的代码。哦,好吧,这不再重要了。
【讨论】:
以上是关于c++ allegro程序边界框碰撞关闭的主要内容,如果未能解决你的问题,请参考以下文章