Reputation: 459
I'm learning object-oriented programming and allegro library so I start to write a simple menu but code displays black screen and then crash.
I will be grateful for the suggestions about classes appearance, splitting code with headers, general syntax of the code and above all to find a problem for which the code does not work.
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);
}
Upvotes: 1
Views: 499
Reputation: 1044
Always check return values! Especially when things start to crash.
You have asked Allegro to run in an 8-bit color depth. This can fail, Even asking for 24-bit on a 32-bit system can fail. You can use desktop_color_depth() to find out what the host is running in.
Ensure that set_gfx_mode() doesn't return an error before proceeding, and that all bitmaps are actually loaded. (not == null)
Upvotes: 1
Reputation: 66
Have you more debug-info about the crash?
First of all, one of the possible problem that can cause the crash is a bad access to the array menuoptions.
string * menuOptions = new string[2];
menuOptions[0] = "New Game";
menuOptions[2] = "Exit"; // THIS IS WRONG
You have leaks inside this loop:
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);
}
Various Hints:
Upvotes: 0