我正在学习面向对象的编程和allegro库,所以我开始编写一个简单的菜单,但代码显示黑屏,然后崩溃。
我将非常感谢关于类外观的建议,使用标题拆分代码,代码的一般语法以及最重要的是找到代码无效的问题。
的main.cpp
#include "Menu.h"
int main() {
Menu m;
return 0;
}
END_OF_MAIN()
Menu.h
#ifndef MENU_H_
#define MENU_H_
#include <allegro.h>
class Menu {
static bool isWorking;
static const int WIDTH = 800;
static const int HEIGHT = 600;
public:
Menu();
virtual ~Menu();
};
#endif
menu.cpp
#include "MenuPointer.h"
#include "Menu.h"
#include <string>
using namespace std;
bool Menu::isWorking = true;
Menu::Menu() {
allegro_init();
install_keyboard();
install_timer();
set_color_depth(8);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, WIDTH, HEIGHT, 0, 0);
string * menuOptions = new string[2];
menuOptions[0] = "New Game";
menuOptions[2] = "Exit";
// menu pointer is an object highlighting menu options
MenuPointer menuPointer(2, 0, WIDTH, HEIGHT/2, menuOptions);
while (isWorking) {
menuPointer.menuInit();
blit(menuPointer.getBuffer(), screen, 0, 0, 0, 0, WIDTH, HEIGHT);
}
}
Menu::~Menu() {
allegro_exit();
clear_keybuf();
}
MenuPointer.h
#ifndef MENUPOINTER_H_
#define MENUPOINTER_H_
#include <string>
#include <allegro.h>
using namespace std;
class MenuPointer {
int pointerNumber;
BITMAP * buffer;
const int optionsNumber;
const int pointerWidth;
const int pointerHeight;
BITMAP ** optionsImages;
const string * optionsTexts;
void paintPointer();
public:
void menuInit();
BITMAP * getBuffer();
MenuPointer(int, int, int, int, BITMAP **);
MenuPointer(int, int, int, int, const string *);
virtual ~MenuPointer();
};
#endif
MenuPointer.cpp
#include "MenuPointer.h"
#include "Menu.h"
#include <string>
using namespace std;
// this constructor creates menu pointer when options are array of images
MenuPointer::MenuPointer(int i, int j, int k, int l, BITMAP ** optionsImages)
:pointerNumber(j), optionsNumber(i), pointerWidth(k), pointerHeight(l) {
buffer = create_bitmap(pointerWidth, pointerHeight);
optionsTexts = NULL;
this->optionsImages = optionsImages;
}
// this constructor creates menu pointer when options are array of text
MenuPointer::MenuPointer(int i, int j, int k, int l, const string * optionsTexts)
:pointerNumber(j), optionsNumber(i), pointerWidth(k), pointerHeight(l) {
buffer = create_bitmap(pointerWidth, pointerHeight);
optionsImages = NULL;
this->optionsTexts = optionsTexts;
}
// draws a rectangle in the range from the beginning to the next option which highlights
void MenuPointer::paintPointer() {
rectfill(buffer, 0, pointerNumber*pointerHeight, pointerHeight, (pointerNumber+1)*pointerHeight, makecol( 128, 30, 30 ) );
}
void MenuPointer::menuInit() {
clear_to_color(buffer, makecol( 128, 128, 128 ));
paintPointer();
if (optionsTexts != NULL) {
// converts string to char array
for (int i = 0; i < optionsNumber; ++i) {
char *a = new char[optionsTexts[i].size()+1];
a[optionsTexts[i].size()] = 0;
memcpy(a, optionsTexts[i].c_str(), optionsTexts[i].size());
textout_ex(screen, font, a, 0, i*pointerWidth, makecol(255,0,0), -1);
}
} else {
// draws option image for images array
for (int i = 0; i < optionsNumber; ++i) {
masked_blit(optionsImages[i], screen, 0, 0, 0, i*pointerWidth, optionsImages[i]->w, optionsImages[i]->h);
}
}
}
BITMAP * MenuPointer::getBuffer() {
return buffer;
}
MenuPointer::~MenuPointer() {
destroy_bitmap(buffer);
}
答案 0 :(得分:1)
始终检查返回值!特别是当事情开始崩溃时。
您已要求Allegro以8位颜色深度运行。这可能会失败,即使在32位系统上要求24位也可能会失败。您可以使用desktop_color_depth()来查找主机正在运行的内容。
确保set_gfx_mode()在继续之前不返回错误,并且实际加载了所有位图。 (不是== null)
答案 1 :(得分:0)
你有关于崩溃的更多调试信息吗?
首先,导致崩溃的可能问题之一是对阵列菜单选项的错误访问。
string * menuOptions = new string[2];
menuOptions[0] = "New Game";
menuOptions[2] = "Exit"; // THIS IS WRONG
你在这个循环中有泄漏:
for (int i = 0; i < optionsNumber; ++i) {
char *a = new char[optionsTexts[i].size()+1]; //this is never freed
a[optionsTexts[i].size()] = 0;
memcpy(a, optionsTexts[i].c_str(), optionsTexts[i].size());
textout_ex(screen, font, a, 0, i*pointerWidth, makecol(255,0,0), -1);
}
各种提示: