I am not sure why but this code (an attampt at a point and click game engine using Allegro 5) throws up an unhandled exception. The program starts and displays a picture, but as soon as the mouse is moved the exception pops up and the program crashes. I have highlighted the line (154) the IDE points to when it pops the exception message.
Any ideas on why would be greatly appreciated. I've been thinking about this all day and can't find the problem :(
// Adventure.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <allegro5/allegro.h>
#include <allegro5/allegro5.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>
#include <iostream>
#include "string.h"
#include <windows.h>
using namespace std;
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_BITMAP *screenBitmapBuffer;
ALLEGRO_EVENT ev;
ALLEGRO_EVENT_QUEUE* event_queue = NULL;
class hotspot
{
private:
string pairedScreen;
string link;
public:
int a,b,c,d;
hotspot(string link, int a, int b, int c, int d)
{
this->link = link;
this->a = a;
this->b = b;
this->c = c;
this->d = d;
}
};
class screen
{
private:
ALLEGRO_BITMAP* imageStatic;
hotspot* hotspots[8];
public:
screen(const char* imageStatic)
{
this->imageStatic = al_load_bitmap(imageStatic);
this->hotspots[1] = new hotspot("test2",0,0,100,100);
this->hotspots[2] = NULL;
this->hotspots[3] = NULL;
this->hotspots[4] = NULL;
this->hotspots[5] = NULL;
this->hotspots[6] = NULL;
this->hotspots[7] = NULL;
this->hotspots[8] = NULL;
}
screen()
{
this->imageStatic = NULL;
}
void display()
{
cout << this->imageStatic << endl;
al_draw_bitmap(this->imageStatic,0,0,0);
al_flip_display();
}
void setBitmap(const char* filename)
{
this->imageStatic = al_load_bitmap(filename);
}
hotspot* checkHotspot(int x)
{
return hotspots[x];
}
};
void loadBitmapToScreen(screen* x, const char* filename)
{
screenBitmapBuffer = al_load_bitmap(filename);
if(screenBitmapBuffer)
{
x->setBitmap(filename);
al_destroy_bitmap(screenBitmapBuffer);
}
else
{
cout << "Failed to load bitmap!" << endl;
}
}
bool mouseWithin(hotspot* hot, int x, int y)
{
cout << hot->a << endl;
cout << hot->d << endl;
//hotspot* hotCheck = hot;
if(x > hot->a && x < hot->d && y > hot->a && y < hot->d)
{
return true;
}
else
{
return false;
}
}
void initEngine()
{
al_init();
al_init_image_addon();
al_init_font_addon();
al_init_ttf_addon();
al_install_audio();
al_init_acodec_addon();
al_install_mouse();
display = al_create_display(500, 500);
al_set_target_bitmap(al_get_backbuffer(display));
if(!display)
{
cout << "Fatal Error: Unable to create display" << endl;
}
}
int main(int argc, char** argv)
{
initEngine();
screen* Test = new screen();
loadBitmapToScreen(Test, "Images\\01-ahead.png");
Test->display();
cout << "Hello World" << endl;
al_set_new_display_flags(ALLEGRO_NOFRAME);
event_queue = al_create_event_queue();
al_register_event_source(event_queue, al_get_mouse_event_source());
while(true)
{
al_wait_for_event(event_queue, &ev);
if(ev.type == ALLEGRO_EVENT_MOUSE_AXES)
{
//system("cls");
THIS LINE --> if(mouseWithin(Test->checkHotspot(1),ev.mouse.x,ev.mouse.y) == true)
{
cout << "mouse is within hotspot" << endl;
}
else
{
cout << "No hotspot" << endl;
}
//int gay = Test->checkHotspot(1)->a;
cout << "X:" << ev.mouse.x << " Y:" << ev.mouse.y << endl;
//
}
}
system("PAUSE");
return 0;
}