Allegro 5键盘干扰鼠标位置?

时间:2016-04-20 00:34:35

标签: c allegro5

我正在使用C和Allegro 5进行简单的游戏。我目前正在隐藏鼠标并将其交换为自定义鼠标图标。每当检测到鼠标移动时,光标图标的位置都会改变。

让我困扰的是,例如,如果我的鼠标光标位于坐标mouse_x = 100和mouse_y = 200,并且我按下键盘上的任意键,鼠标光标将被传送到坐标mouse_x = 0和mouse_y =按下键时为0。

我不知道会发生什么,但每当我使用al_uninstall_keyboard()卸载键盘时,该错误就消失了。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <allegro5\allegro.h>
#include <allegro5\allegro_image.h>
#include <allegro5\allegro_primitives.h>
#include <allegro5\allegro_audio.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include <allegro5\allegro_acodec.h>

enum gameMode{menu, farmGame, stdend};
enum soil{normal, wet};
enum soil2{unplow, plow};
enum cropType{none, tomato};

typedef struct gDisp //game display
{
    int width;
    int height;
    ALLEGRO_DISPLAY *display;
}gDisp;

typedef struct fCrop //farm crop
{
    cropType crop;
    ALLEGRO_TIMEOUT *waterNeed;
    ALLEGRO_TIMEOUT *growth[4]; //Crop's growth time on all stages
}fCrop;

typedef struct fTile //farm tiles
{
    int xpoint;
    int ypoint;
    int size;
    soil ground;
    soil2 ground2;
    fCrop crop;
}fTile;

int main()
{
    //Allegro Initialization Area
    al_init();
    al_init_image_addon();
    al_init_primitives_addon();
    al_init_font_addon();
    al_init_ttf_addon();
    al_init_acodec_addon();

    //Allegro Installation Area
    al_install_keyboard();
    al_install_mouse();

    //Allegro Variable Area
    ALLEGRO_EVENT_QUEUE *queue = al_create_event_queue();
    ALLEGRO_EVENT ev;
    ALLEGRO_FONT *font[3];

    //Allegro Variable Declaration Area

    //My Enums Area
    gameMode gamemode = menu;
    ALLEGRO_BITMAP *bmp[30]; //20-30: Menu Bitmaps
    ALLEGRO_BITMAP *ico;

    //My Structs Area
    gDisp disp;
    disp = {0, 0, NULL};

    //Games... Here we go!
    if (gamemode == menu)
    {
        char *currgamelist[4] = { "PLAY", "TUTORIAL", "CREDITS" };
        disp.width = 600, disp.height = 600, disp.display = al_create_display(disp.width, disp.height);
        ico = al_load_bitmap("tomato_seedbase.png");
        bmp[0] = al_load_bitmap("Normal-select.png");
        bmp[1] = al_load_bitmap("Link-select.png");
        al_set_window_title(disp.display, "Farming Hero");
        al_set_display_icon(disp.display, ico);
        al_hide_mouse_cursor(disp.display);
        font[0] = al_load_font("PressStart2P.ttf", 32, 0);
        al_clear_to_color(al_map_rgb(0, 0, 0));
        al_register_event_source(queue, al_get_mouse_event_source());
        al_register_event_source(queue, al_get_keyboard_event_source());
        int mouse_x;
        int mouse_y;
        while (gamemode == menu)
        {
            al_wait_for_event(queue, &ev);
            al_clear_to_color(al_map_rgb(0, 0, 0));
            if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
                if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
                {
                    al_destroy_display(disp.display);
                    al_destroy_font(font[0]);
                    return 0;
                }
            al_draw_text(font[0], al_map_rgb(255, 255, 255), 105, 50, 0, "FARMING HERO");
            al_draw_text(font[0], al_map_rgb(255, 255, 255), 30, 200, 0, currgamelist[0]);
            al_draw_text(font[0], al_map_rgb(255, 255, 255), 30, 300, 0, currgamelist[1]);
            al_draw_text(font[0], al_map_rgb(255, 255, 255), 30, 400, 0, currgamelist[2]);
            al_draw_text(font[0], al_map_rgb(255, 255, 255), 30, 500, 0, "EXIT");
            al_draw_text(font[0], al_map_rgb(255, 255, 255), 30, 500, 0, " ");
            if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
                if (ev.mouse.button & 1)
                {
                    if (ev.mouse.x >= 30 && ev.mouse.y >= 200 && ev.mouse.x <= al_get_text_width(font[0], currgamelist[0]) + 30 && ev.mouse.y <= 232)
                        gamemode = farmGame;
                    if (ev.mouse.x >= 30 && ev.mouse.y >= 300 && ev.mouse.x <= al_get_text_width(font[0], currgamelist[1]) + 30 && ev.mouse.y <= 332)
                        al_destroy_display(disp.display), al_destroy_font(font[0]), exit(0);
                    if (ev.mouse.x >= 30 && ev.mouse.y >= 400 && ev.mouse.x <= al_get_text_width(font[0], currgamelist[2]) + 30 && ev.mouse.y <= 432)
                        al_destroy_display(disp.display), al_destroy_font(font[0]), exit(0);
                    if (ev.mouse.x >= 30 && ev.mouse.y >= 500 && ev.mouse.x <= al_get_text_width(font[0], "EXIT") + 30 && ev.mouse.y <= 532)
                        al_destroy_display(disp.display), al_destroy_font(font[0]), exit(0);
                }
            if (ev.type == ALLEGRO_EVENT_MOUSE_AXES)
                mouse_x = ev.mouse.x, mouse_y = ev.mouse.y;
            if (mouse_x >= 30 && mouse_x <= al_get_text_width(font[0], currgamelist[0]) + 30 && mouse_y >= 195 && mouse_y <= 235)
                al_draw_text(font[0], al_map_rgb(255, 239, 0), 30, 200, 0, currgamelist[0]), al_draw_bitmap(bmp[1], mouse_x, mouse_y, 0);
            else if (mouse_x >= 30 && mouse_x <= al_get_text_width(font[0], currgamelist[1]) + 30 && mouse_y >= 295 && mouse_y <= 335)
                al_draw_text(font[0], al_map_rgb(255, 239, 0), 30, 300, 0, currgamelist[1]), al_draw_bitmap(bmp[1], mouse_x, mouse_y, 0);
            else if (mouse_x >= 30 && mouse_x <= al_get_text_width(font[0], currgamelist[2]) + 30 && mouse_y >= 395 && mouse_y <= 435)
                al_draw_text(font[0], al_map_rgb(255, 239, 0), 30, 400, 0, currgamelist[2]), al_draw_bitmap(bmp[1], mouse_x, mouse_y, 0);
            else if (mouse_x >= 30 && mouse_x <= al_get_text_width(font[0], "EXIT") + 30 && mouse_y >= 495 && mouse_y <= 535)
                al_draw_text(font[0], al_map_rgb(255, 239, 0), 30, 500, 0, "EXIT"), al_draw_bitmap(bmp[1], mouse_x, mouse_y, 0);
            else{ al_draw_bitmap(bmp[0], mouse_x, mouse_y, 0); };
            al_flip_display();
        }
    }
    if (gamemode == farmGame)
    {
        ALLEGRO_TIMER *fps = al_create_timer(1.00 / 60.00);
        font[0] = al_load_font("PressStart2P.ttf", 8, 0);
        font[1] = al_load_font("PressStart2P.ttf", 16, 0);
        al_register_event_source(queue, al_get_timer_event_source(fps));
        int mouse_x, mouse_y;
        mouse_x = -50, mouse_y = 0;
        fCrop empty = { none, 0, 0 };
        fTile tile[100];
        int x_prep, y_prep; //prepare the tile's configuration
        x_prep = 0, y_prep = 0;
        for (int i = 0; i < 100; i++)
        {
            if (x_prep == 10)
                x_prep = 0, y_prep++;
            tile[i] = { x_prep, y_prep, 51, normal, unplow, empty};
            x_prep++;
        }
        al_clear_to_color(al_map_rgb(0, 0, 0));
        al_flip_display();
        bmp[0] = al_load_bitmap("link-select.png");
        bmp[1] = al_load_bitmap("ground_normal.png");
        bmp[9] = al_create_bitmap(disp.width, disp.height);
        bmp[30] = al_load_bitmap("tomato_seedbase.png");
        al_uninstall_keyboard();
        al_start_timer(fps);
        al_set_target_bitmap(bmp[9]);
        al_draw_filled_rectangle(512, 2, disp.width - 2, 508, al_map_rgb(150, 120, 0));
        al_draw_filled_rectangle(2, 512, 512, disp.height - 2, al_map_rgb(150, 120, 0));
        al_draw_filled_rectangle(512, 512, disp.width - 2, disp.height - 2, al_map_rgb(150, 120, 0));
        al_draw_rectangle(512, 2, disp.width - 2, 511, al_map_rgb(70, 50, 0), 5);
        al_draw_rectangle(2, 512, 512, disp.height - 2, al_map_rgb(70, 50, 0), 5);
        al_draw_rectangle(512, 512, disp.width - 2, disp.height - 2, al_map_rgb(70, 50, 0), 5);
        al_set_target_backbuffer(disp.display);
        while (gamemode == farmGame)
        {
            char tileinfo[5][15];
            al_clear_to_color(al_map_rgb(0, 0, 0));
            al_wait_for_event(queue, &ev);
            for (int i = 0; i < 100; i++)
            {
                if (tile[i].ground == normal && tile[i].ground2 == unplow)
                    al_draw_bitmap(bmp[1], tile[i].xpoint * tile[i].size, tile[i].ypoint * tile[i].size, 0);
                if (mouse_x >= 0 && mouse_x <= 508 && mouse_y >= 0 && mouse_y <= 508)
                    if (mouse_x >= tile[i].xpoint * tile[i].size && mouse_x <= tile[i].xpoint * tile[i].size + tile[i].size && mouse_y >= tile[i].ypoint * tile[i].size && mouse_y <= tile[i].ypoint * tile[i].size + tile[i].size)
                    {
                        tile[i].crop.crop == none ? strcpy(tileinfo[1], "NONE") : 0, tile[i].crop.crop == tomato ? strcpy(tileinfo[1], "TOMATOES") : 0;
                        tile[i].ground == normal ? strcpy(tileinfo[2], "DRY") : 0, tile[i].ground == wet ? strcpy(tileinfo[2], "WET") : 0;
                        tile[i].ground2 == unplow ? strcpy(tileinfo[3], "UNPLOW") : 0, tile[i].ground2 == plow ? strcpy(tileinfo[3], "PLOW") : 0;
                    }
            }
            al_draw_bitmap(bmp[0], ev.mouse.x, ev.mouse.y, 0);
            if (ev.type == ALLEGRO_EVENT_MOUSE_AXES)
                mouse_x = ev.mouse.x, mouse_y = ev.mouse.y;
            if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
                if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
                    gamemode = stdend;
            al_draw_bitmap(bmp[9], 0, 0, 0);
            if (mouse_x >= 503 && mouse_x <= disp.height && mouse_y >= 10 && mouse_y <= al_get_bitmap_height(bmp[30]))
                al_draw_tinted_bitmap(bmp[30], al_map_rgb(125, 255, 37), 503, 10, 0), al_draw_text(font[1], al_map_rgb(125, 255, 37), 525, 100, 0, "SHOP");
            else{ al_draw_bitmap(bmp[30], 503, 10, 0), al_draw_text(font[1], al_map_rgb(255, 255, 255), 525, 100, 0, "SHOP"); }
            if (tileinfo[1] != NULL && tileinfo[2] != NULL && tileinfo[3] != NULL)
            {
                al_draw_textf(font[0], al_map_rgb(255, 255, 255), 520, 520, 0, "%s", tileinfo[1]);
                al_draw_textf(font[0], al_map_rgb(255, 255, 255), 520, 550, 0, "%s", tileinfo[2]);
                al_draw_textf(font[0], al_map_rgb(255, 255, 255), 520, 580, 0, "%s", tileinfo[3]);
            }
            al_draw_bitmap(bmp[0], mouse_x, mouse_y, 0);
            al_flip_display();
        }
    }
    if (gamemode == stdend)
    {
        al_rest(2);
        al_destroy_display(disp.display);
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您从未在循环之前初始化mouse_xmouse_y。这意味着它们可以是任意随机值,但很可能它们是0.当你按一个键时,循环继续,你的鼠标光标在循环底部的(0,0)绘制。您可以使用al_get_mouse_state将其初始化为正确的值。