Reputation: 161
I am making a Map Editor using Allegro 5, and it works with tiles. However, the bitmaps in it are extremely buggy, and I don't really know why.
Here's my code:
#include <allegro5\allegro.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_image.h>
#include <allegro5\allegro_primitives.h>
#include <allegro5\allegro_native_dialog.h>
#include <stdio.h>
#include <vector>
//ENUMS DEFINITION SECTION
enum TYPE{LEFT,RIGHT,UP,DOWN};
//CLASS DEFINITION SECTION
class bmplist
{
public:
ALLEGRO_BITMAP *bmp;
TYPE type;
};
class T_center
{
public:
//place
int p;
int y;
//place bound y up, x left, y down, x right
int pbyu;
int pbxl;
int pbyd;
int pbxr;
ALLEGRO_BITMAP *img;
bmplist a;
};
class bmpdata
{
public:
bmplist bmp;
};
class bmphold
{
public:
ALLEGRO_BITMAP *bmp;
};
const int tileamount = 196;
T_center tile[tileamount];
int height = 700;
int width = 700;
ALLEGRO_DISPLAY *display;
ALLEGRO_EVENT ev;
ALLEGRO_EVENT_QUEUE *queue;
ALLEGRO_BITMAP *curr;
ALLEGRO_BITMAP *tilelr;
ALLEGRO_BITMAP *tileud;
ALLEGRO_BITMAP *tileltd;
ALLEGRO_BITMAP *tileltu;
ALLEGRO_BITMAP *tilertd;
ALLEGRO_BITMAP *tilertu;
ALLEGRO_BITMAP *pathup;
ALLEGRO_BITMAP *pathdown;
ALLEGRO_BITMAP *pathleft;
ALLEGRO_BITMAP *pathright;
ALLEGRO_FONT *actual;
bool isClicked;
bmplist bmps[2];
bool MAPMOUNTING = true;
bool MAPCOORDINATING = false;
bmplist arrows[4];
bmplist bmpindex[20];
bmpdata arrowindex[4];
char tilenow[15];
//FUNCTION DECLARATION SECTION
void func_start_system();
void func_start_tiles(int tilesize, T_center *tile, int tilearray, int xsize);
void func_start_variables();
void func_detect_mouse_move(T_center *tile, int tilearray, int bmpsize);
void mapper_init();
//THE TOOL ITSELF
int main()
{
al_init();
al_init_font_addon();
al_init_image_addon();
al_init_primitives_addon();
mapper_init();
}
//FUNCTION DEFINITION SECTION
void func_start_system()
{
al_install_keyboard();
al_install_mouse();
}
void func_start_tiles(int tilesize, T_center *tile, int tilearray, int xsize)
{
int xcount = 0;
int tile_x = tilesize;
int tile_y = tilesize;
int y_count = 1;
for (int i = 0; i < tilearray; i++)
{
if (xcount == 14)
{
xcount = 0;
tile_y = tile[i-1].pbyd + tilesize;
tile_x = tilesize;
}
tile[i].p = tile_x, tile[i].y = tile_y, tile[i].pbxl = tile_x - tilesize, tile[i].pbxr = tile_x + tilesize, tile[i].pbyu = tile_y - tilesize, tile[i].pbyd = tile_y + tilesize;
tile_x = tile[i].pbxr + tilesize;
printf("%i, %i, left: %i\n", tile[i].p, tile[i].y, tile[i].pbxl);
xcount++;
}
}
void func_start_variables()
{
display = al_create_display(width, height);
tilelr = al_load_bitmap("tilelr.png");
tileud = al_load_bitmap("tileud.png");
tileltu = al_load_bitmap("tireltu.png"), tileltd = al_load_bitmap("tireltd.png"), tilertu = al_load_bitmap("tirertu.png"), tilertd = al_load_bitmap("tirertd.png");
pathup = al_load_bitmap("up.png"), pathdown = al_load_bitmap("down.png"), pathleft = al_load_bitmap("left.png"), pathright = al_load_bitmap("right.png");
arrows[1].bmp = al_load_bitmap("up.png"), arrows[1].type = UP, arrows[2].bmp = al_load_bitmap("down.png"), arrows[2].type = DOWN;
arrows[3].bmp = al_load_bitmap("left.png"), arrows[3].type = LEFT, arrows[4].bmp = al_load_bitmap("right.png"), arrows[4].type = RIGHT;
bmpindex[1].bmp = tilelr, bmpindex[2].bmp = tileud, bmpindex[3].bmp = tileltu, bmpindex[4].bmp = tileltd, bmpindex[5].bmp = tilertu, bmpindex[6].bmp = tilertd;
bmpindex[7].bmp = pathup, bmpindex[7].type = UP, bmpindex[8].bmp = pathdown, bmpindex[8].type = DOWN;
bmpindex[9].bmp = pathleft, bmpindex[9].type = LEFT, bmpindex[10].bmp = pathright, bmpindex[10].type = RIGHT;
curr = bmpindex[1].bmp;
actual = al_load_font("app850.ttf", 12, 0);
queue = al_create_event_queue();
al_register_event_source(queue, al_get_mouse_event_source());
al_register_event_source(queue, al_get_keyboard_event_source());
al_register_event_source(queue, al_get_display_event_source(display));
}
void func_detect_mouse_move(T_center *tile, int tilearray, int bmpsize)
{
int index = 1;
int a = true;
while (a)
{
printf("%i", index);
al_clear_to_color(al_map_rgb(0, 0, 0));
for (int i = 0; i < tilearray; i++)
{
if (tile[i].img != NULL)
al_draw_bitmap(tile[i].img, tile[i].pbxl, tile[i].pbyu, 0);
if (tile[i].a.bmp != NULL)
al_draw_bitmap(tile[i].a.bmp, tile[i].pbxl, tile[i].pbyu, 0);
}
al_wait_for_event(queue, &ev);
{
if (ALLEGRO_EVENT_MOUSE_AXES)
{
for (int i = 0; i < tilearray; i++)
if (ev.mouse.x >= tile[i].pbxl && ev.mouse.x < tile[i].pbxr && ev.mouse.y >= tile[i].pbyu && ev.mouse.y < tile[i].pbyd)
{
al_draw_tinted_bitmap(bmpindex[index].bmp, al_map_rgb(200, 200, 200), tile[i].pbxl, tile[i].pbyu, 0);
al_flip_display();
}
}
if (ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
{
if (ev.mouse.button & 1)
for (int i = 0; i < tilearray; i++)
{
if (ev.mouse.x >= tile[i].pbxl && ev.mouse.x < tile[i].pbxr && ev.mouse.y >= tile[i].pbyu && ev.mouse.y < tile[i].pbyd)
{
if (bmpindex[index].type == NULL)
{
tile[i].img = curr;
al_flip_display();
}
if (bmpindex[index].type != NULL)
{
tile[i].a.bmp = bmpindex[index].bmp;
tile[i].a.type = bmpindex[index].type;
}
}
}
if (ev.mouse.button & 2)
for (int i = 0; i < tilearray; i++)
{
if (ev.mouse.x >= tile[i].pbxl && ev.mouse.x < tile[i].pbxr && ev.mouse.y >= tile[i].pbyu && ev.mouse.y < tile[i].pbyd)
{
tile[i].img = NULL;
al_flip_display();
}
}
if (ALLEGRO_EVENT_KEY_DOWN)
{
if (ev.keyboard.keycode == ALLEGRO_KEY_1)
index = 1;
if (ev.keyboard.keycode == ALLEGRO_KEY_2)
index = 2;
if (ev.keyboard.keycode == ALLEGRO_KEY_3)
index = 3;
if (ev.keyboard.keycode == ALLEGRO_KEY_4)
index = 4;
if (ev.keyboard.keycode == ALLEGRO_KEY_5)
index = 5;
if (ev.keyboard.keycode == ALLEGRO_KEY_6)
index = 6;
if (ev.keyboard.keycode == ALLEGRO_KEY_UP)
index = 7;
if (ev.keyboard.keycode == ALLEGRO_KEY_DOWN)
index = 8;
if (ev.keyboard.keycode == ALLEGRO_KEY_LEFT)
index = 9;
if (ev.keyboard.keycode == ALLEGRO_KEY_RIGHT)
index = 10;
al_flush_event_queue(queue);
}
curr = bmpindex[index].bmp;
}
}
}
}
void mapper_init()
{
func_start_system();
func_start_variables();
func_start_tiles(25, tile, tileamount, tileamount);
al_flip_display();
func_detect_mouse_move(tile, tileamount, 10);
}
The problem with this map editor is that sometimes, just by moving the mouse, the index, which is an integer that selects a bitmap in the bmpindex array, bugs out and a completely random bitmap appears in the mouse's position. For example, I am placing some bitmaps on the screen with a number 2 index, but then all of a sudden the index number becomes 4. Also, whenever I press a key in the keyboard, the actual selected bitmap is drawn in a completely random part of the screen. Why is this bug happening? How can I fix it?
Upvotes: 1
Views: 86
Reputation: 11
I'd say it's because you have a small typo there. Those conditions, i.e.:
if (ALLEGRO_EVENT_MOUSE_AXES)
if (ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
if (ALLEGRO_EVENT_KEY_DOWN)
Will always evaluate as TRUE and therefore the statements will always run. This way you'll end up in situations you described that does not seem make sense (at least on the first sight) but that actually are quite logical
Like reading ev.mouse.x
even though mouse did not move at all (but that did not matter because your condition if (ALLEGRO_EVENT_MOUSE_AXES)
is always true) and contents of ev do not match mouse union part because it could have been the keyboard that caused the event, so the resulting coordinates are nonsensical (because there were none in that part of the memory)
Solution is actually quite simple - you just need to check type of ev for that value in condition, i.e. replace:
if (ALLEGRO_EVENT_MOUSE_AXES)
With:
if (ev.type == ALLEGRO_EVENT_MOUSE_AXES)
And so on.
Upvotes: 1