gpt4 book ai didi

c++ - Allegro 程序显示黑屏然后崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 11:32:23 27 4
gpt4 key购买 nike

我正在学习面向对象编程和 allegro 库,所以我开始编写一个简单的菜单,但代码显示黑屏然后崩溃。

对于有关类外观、使用 header 拆分代码、代码的一般语法以及最重要的是找到代码无法解决的问题的建议,我将不胜感激。

主要.cpp

#include "Menu.h"

int main() {
Menu m;
return 0;
}
END_OF_MAIN()

菜单.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

菜单.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();
}

菜单指针.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

菜单指针.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);
}

最佳答案

总是检查返回值!尤其是当事情开始崩溃时。

您已要求 Allegro 在 8 位颜色深度下运行。这可能会失败,即使在 32 位系统上请求 24 位也可能会失败。您可以使用 desktop_color_depth() 找出主机正在运行的内容。

确保 set_gfx_mode() 在继续之前没有返回错误,并且所有位图都已实际加载。 (不是== null)

关于c++ - Allegro 程序显示黑屏然后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24234739/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com