为什么我的Allegro 5 Map Editor会出现问题?

时间:2015-11-19 14:15:56

标签: c++ allegro5

我使用Allegro 5制作地图编辑器,它适用于瓷砖。但是,其中的位图非常错误,我真的不知道为什么。

这是我的代码:

    #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);
}

这个地图编辑器的问题在于,有时候,只需移动鼠标, index ,这是一个在bmpindex数组中选择位图的整数,就会出现错误并出现一个完全随机的位图在鼠标的位置。例如,我在屏幕上放置了一些带有2号索引的位图,但随后突然间索引号变为4.此外,每当我按下键盘上的某个键时,实际选择的位图都是完全随机绘制的屏幕的一部分。为什么会发生这个错误?我该如何解决?

1 个答案:

答案 0 :(得分:1)

我之所以这么说是因为那里有一个小错字。那些条件,即:

if (ALLEGRO_EVENT_MOUSE_AXES)
if (ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
if (ALLEGRO_EVENT_KEY_DOWN)

将始终评估为TRUE,因此语句将始终运行。通过这种方式,你最终会遇到你认为没有意义的情况(至少在第一眼看上去),但这实际上是非常合乎逻辑的

喜欢阅读ev.mouse.x,尽管鼠标根本没有移动(但这并不重要,因为你的条件if (ALLEGRO_EVENT_MOUSE_AXES)总是正确的)并且ev的内容与鼠标联合部分不匹配,因为它可能有是导致事件的键盘,因此生成的坐标是无意义的(因为内存的那部分没有)

解决方案实际上非常简单 - 您只需要在条件中检查该值的ev类型,即替换:

if (ALLEGRO_EVENT_MOUSE_AXES)

使用:

if (ev.type == ALLEGRO_EVENT_MOUSE_AXES)

等等。