I am working on a menu for a game in c++ using the allegro graphics library. I am using code blocks and mingw. There are many buttons that use a modified bounding box that returns true if the mouse is over the button. It works fine for all of the buttons except for one and I have no clue why this happens. I have spent a good hour looking and have found nothing. On the broken button it responds true while hovered over the button to the right and below it and some places above. :/ Any help would be much appreciated. Feel free to point out anything else wrong with my code or if I'm doing something in a stupid way. I'm teaching my self c++ and I wouldn't be surprised if my code is terrible. Thanks! :)
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()
menu.h:
//data structures
struct button{
int x;
int y;
int w;
int h;
bool select;
};
instructions.cpp (this one works fine)
#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: (and this one doesnt)
#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开发者_如何学Go] && 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);
}
}
I found the problem! :D in highscores.cpp the external button_check function was written as an int rather then a bool. Its all fixed now. :O just realised that I somehow forgot to include the code for the button_check function in my question. Oh well it doesn't matter anymore.
精彩评论